-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
274 lines (233 loc) · 11.5 KB
/
Program.cs
File metadata and controls
274 lines (233 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.Linq;
using System.Runtime.InteropServices;
namespace SteamVRKeyboardFix
{
internal static class Program
{
private const string DebugFlag = "--debug";
private const string InstallFlag = "--install";
private const string UninstallFlag = "--uninstall";
// Win32 API: Find console window and show/hide it for debug mode
[DllImport("kernel32.dll")]
static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int SW_HIDE = 0;
const int SW_SHOW = 5;
// Declare Win32 API delegate and P/Invoke for shutdown event handling
public delegate bool ConsoleCtrlDelegate(int ctrlType);
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(ConsoleCtrlDelegate handler, bool add);
// Windows control signal constants
private const int CTRL_C_EVENT = 0;
private const int CTRL_BREAK_EVENT = 1;
private const int CTRL_CLOSE_EVENT = 2;
private const int CTRL_LOGOFF_EVENT = 5;
private const int CTRL_SHUTDOWN_EVENT = 6;
// 🚨 [CRITICAL] Keep the delegate as a static variable!
// If it's a local variable, the Garbage Collector (GC) might collect it,
// causing a fatal "Attempted to read or write protected memory" crash during shutdown.
private static ConsoleCtrlDelegate _ctrlHandler;
static void Main(string[] args)
{
// --install / --uninstall: service registration (requires admin).
// Handled before EnsureEventLogSource so the event log source is
// created by ServiceManager.Install() itself.
if (args.Contains(InstallFlag, StringComparer.OrdinalIgnoreCase))
{
try
{
ServiceManager.Install();
}
catch(InvalidOperationException ex)
{
Console.WriteLine($"{ex.Message}");
Console.WriteLine("Press any key to continue");
Console.ReadKey(); // Prevent console from closing so log can be read during installation process via installer
throw ex;
}
return;
}
if (args.Contains(UninstallFlag, StringComparer.OrdinalIgnoreCase))
{
try
{
ServiceManager.Uninstall();
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"{ex.Message}");
Console.WriteLine("Press any key to continue");
Console.ReadKey(); // Prevent console from closing so log can be read during uninstallation process via uninstaller
throw ex;
}
return;
}
// 이벤트 로그 소스가 없으면 생성 (관리자 권한 필요, 설치 시 수행)
EnsureEventLogSource();
if (!args.Contains(DebugFlag, StringComparer.OrdinalIgnoreCase))
{
ShowWindow(GetConsoleWindow(), SW_HIDE);
}
else
{
ShowWindow(GetConsoleWindow(), SW_SHOW);
}
RunDebugRepl(args);
}
// ── Debug REPL ────────────────────────────────────────────────────────
private static SteamVRKeyboardFixService? _serviceInstance;
/// <summary>
/// Interactive command-line REPL for testing without SteamVR.
/// The service WMI watcher is also started so real events are captured.
///
/// Commands:
/// install — (Admin) Install as service with current exe file path
/// uninstall — (Admin) Uninstall exisiting service
/// run — RemoveEnUsKeyboardLayout() (full auto-detect + remove)
/// registry — IsEnUsInRegistry()
/// hkllist — IsEnUsInHklList()
/// ghost — RemoveGhostLayout() (force Case A path)
/// registered — RemoveRegisteredLayout() (force Case B path)
/// load — LoadKeyboardLayout("00000409", KLF_NOTELLSHELL | KLF_REPLACELANG) (Reproduce ghost layout)
/// broadcast — BroadcastSettingChange()
/// help — show this list
/// exit / quit — stop service and exit
/// </summary>
private static void RunDebugRepl(string[] args)
{
// Register the exit event handler
_ctrlHandler = new ConsoleCtrlDelegate(CtrlHandlerCallback);
SetConsoleCtrlHandler(_ctrlHandler, true);
Console.WriteLine("=== SteamVRKeyboardFix [DEBUG MODE] ===");
Console.WriteLine($"Version: {System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}");
Console.WriteLine();
using var svc = new SteamVRKeyboardFixService();
_serviceInstance = svc;
svc.TestStart(args);
Console.WriteLine("Type 'help' for commands.");
while (true)
{
Console.Write("> ");
string? input = Console.ReadLine()?.Trim().ToLowerInvariant();
if (string.IsNullOrEmpty(input)) continue;
switch (input)
{
case "run":
Console.WriteLine("[REPL] Calling RemoveEnUsKeyboardLayout()...");
Console.WriteLine("Executing full auto-detect + remove");
svc.RemoveEnUsKeyboardLayout();
break;
case "registry":
bool inReg = svc.IsEnUsInRegistry();
Console.WriteLine($"[REPL] IsEnUsInRegistry() = {inReg}");
break;
case "hkllist":
bool inHkl = svc.IsEnUsInHklList(out nint foundHkl);
Console.WriteLine($"[REPL] IsEnUsInHklList() = {inHkl} (HKL: 0x{foundHkl:X8})");
break;
case "ghost":
Console.WriteLine("[REPL] Forcing Case A: RemoveGhostLayout()...");
if (svc.IsEnUsInHklList(out nint ghostHkl))
svc.RemoveGhostLayout(ghostHkl);
else
Console.WriteLine("[REPL] en-US not found in HKL list. Nothing to unload.");
break;
case "registered":
Console.WriteLine("[REPL] Forcing Case B: RemoveRegisteredLayout()...");
svc.RemoveRegisteredLayout();
break;
case "load":
Console.WriteLine("[REPL] Calling LoadKeyboardLayout(\"00000409\", KLF_NOTELLSHELL | KLF_REPLACELANG)...");
Console.WriteLine("Reproduce ghost layout");
nint loaded = SteamVRKeyboardFixService.LoadKeyboardLayout(
"00000409",
SteamVRKeyboardFixService.KLF_NOTELLSHELL |
SteamVRKeyboardFixService.KLF_REPLACELANG);
Console.WriteLine(loaded == (nint) 0
? $"[REPL] LoadKeyboardLayout failed (error {Marshal.GetLastWin32Error()})"
: $"[REPL] LoadKeyboardLayout succeeded. HKL = 0x{loaded:X8}");
break;
case "broadcast":
Console.WriteLine("[REPL] Calling BroadcastSettingChange()...");
svc.BroadcastSettingChange();
break;
case "help":
PrintHelp();
break;
case "exit":
case "quit":
svc.TestStop();
Console.WriteLine("Service stopped. Goodbye.");
return;
case "install":
try
{
ServiceManager.Install();
}
catch(InvalidOperationException ex)
{
Console.WriteLine($"{ex.Message}");
}
break;
case "uninstall":
try
{
ServiceManager.Uninstall();
}
catch (InvalidOperationException ex)
{
Console.WriteLine($"{ex.Message}");
}
break;
default:
Console.WriteLine($"Unknown command: '{input}'. Type 'help' for a list.");
break;
}
Console.WriteLine();
}
}
private static void PrintHelp()
{
Console.WriteLine();
Console.WriteLine(" run RemoveEnUsKeyboardLayout() — auto-detect and remove");
Console.WriteLine(" registry IsEnUsInRegistry() — check Preload registry key");
Console.WriteLine(" hkllist IsEnUsInHklList() — check runtime HKL list");
Console.WriteLine(" ghost RemoveGhostLayout() — force Case A removal path");
Console.WriteLine(" registered RemoveRegisteredLayout() — force Case B removal path");
Console.WriteLine(" load LoadKeyboardLayout(\"00000409\", KLF_NOTELLSHELL|KLF_REPLACELANG)");
Console.WriteLine(" broadcast BroadcastSettingChange() — send WM_SETTINGCHANGE(intl)");
Console.WriteLine(" help Show this help");
Console.WriteLine(" exit/quit Stop service and exit");
}
// ── Helpers ───────────────────────────────────────────────────────────
private static void EnsureEventLogSource()
{
const string source = "SteamVRKeyboardFix";
try
{
if (!System.Diagnostics.EventLog.SourceExists(source))
System.Diagnostics.EventLog.CreateEventSource(source, "Application");
}
catch { /* requires admin rights; handled by installer */ }
}
private static bool CtrlHandlerCallback(int ctrlType)
{
switch (ctrlType)
{
case CTRL_C_EVENT:
case CTRL_CLOSE_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT: // System shutdown signal!
// e.g., Unsubscribe from WMI events, close open FileStreams, etc.
Console.WriteLine($"\n[INFO] Received Windows exit signal ({ctrlType})! Cleaning up resources gracefully...");
_serviceInstance?.TestStop();
// Tell Windows: "I've handled the cleanup, proceed with termination!"
return true;
default:
return false;
}
}
}
}