-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlanguage_indicator.cpp
More file actions
388 lines (322 loc) · 11.8 KB
/
Copy pathlanguage_indicator.cpp
File metadata and controls
388 lines (322 loc) · 11.8 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#include <windows.h>
#include <shellapi.h>
#include <fstream>
#include <string>
#include <ctime>
#include <sstream>
HINSTANCE hInstance;
HWND hwnd;
HFONT font;
NOTIFYICONDATA nid;
HKL previousKeyboardLayout = 0;
HHOOK hKeyboardLayoutHook = NULL;
class LanguageIndicator;
static void LogMessage(const std::string& message);
static void UpdateLanguageText(HWND hWnd);
static void ShowWindowWithTimer(HWND hWnd);
static void CenterWindow(HWND hWnd);
static void FadeInWindow(HWND hWnd);
static void FadeOutWindow(HWND hWnd);
std::string GetTimestamp() {
auto now = std::time(nullptr);
auto localTime = *std::localtime(&now);
std::ostringstream oss;
oss << '[' << (localTime.tm_year + 1900) << '-'
<< (localTime.tm_mon + 1) << '-'
<< localTime.tm_mday << ' '
<< localTime.tm_hour << ':'
<< localTime.tm_min << ':'
<< localTime.tm_sec << "] ";
return oss.str();
}
std::string GetLastErrorAsString() {
DWORD error = GetLastError();
if (error == 0) return "No error";
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
error,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(LPTSTR)&lpMsgBuf,
0,
NULL
);
std::string message((char*)lpMsgBuf);
LocalFree(lpMsgBuf);
return "Error " + std::to_string(error) + ": " + message;
}
static void LogMessage(const std::string& message) {
std::ofstream logFile("lang_indicator_debug.log", std::ios::app);
if (logFile.is_open()) {
logFile << GetTimestamp() << message << std::endl;
logFile.close();
}
}
LRESULT CALLBACK KeyboardLayoutProc(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
static HKL currentLayout = GetKeyboardLayout(0);
HKL newLayout = GetKeyboardLayout(0);
std::ostringstream oss;
oss << "Hook - Current layout: 0x" << std::hex << (UINT_PTR)currentLayout
<< ", New layout: 0x" << (UINT_PTR)newLayout
<< ", Active window handle: 0x" << std::hex << (UINT_PTR)GetForegroundWindow();
LogMessage(oss.str());
if (newLayout != currentLayout) {
currentLayout = newLayout;
LogMessage("Layout change detected - Posting WM_INPUTLANGCHANGE");
PostMessage(hwnd, WM_INPUTLANGCHANGE, 0, (LPARAM)newLayout);
}
}
return CallNextHookEx(hKeyboardLayoutHook, nCode, wParam, lParam);
}
class LanguageIndicator {
public:
static LanguageIndicator& GetInstance() {
static LanguageIndicator instance;
return instance;
}
bool Initialize(HINSTANCE hInst) {
LogMessage("Initializing Language Indicator");
hInstance = hInst;
WNDCLASS wc = { 0 };
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = CLASS_NAME;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClass(&wc)) {
LogMessage("Failed to register window class: " + GetLastErrorAsString());
return false;
}
hwnd = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT,
CLASS_NAME,
"Language Indicator",
WS_POPUP,
0, 0, 150, 50,
NULL, NULL, hInstance, NULL);
if (!hwnd) {
LogMessage("Failed to create window: " + GetLastErrorAsString());
return false;
}
font = CreateFont(24, 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE,
DEFAULT_CHARSET, OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS, CLEARTYPE_QUALITY,
DEFAULT_PITCH | FF_SWISS, "Arial");
SetLayeredWindowAttributes(hwnd, RGB(255, 255, 255), 200, LWA_ALPHA);
AddTrayIcon(hwnd);
CenterWindow(hwnd);
hKeyboardLayoutHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardLayoutProc, hInstance, 0);
if (!hKeyboardLayoutHook) {
LogMessage("Failed to set keyboard layout hook: " + GetLastErrorAsString());
return false;
}
LogMessage("Initialization complete");
return true;
}
void Run() {
LogMessage("Starting message loop");
MSG msg = { 0 };
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
LogMessage("Message loop ended");
}
void Cleanup() {
LogMessage("Starting cleanup");
if (hKeyboardLayoutHook) {
UnhookWindowsHookEx(hKeyboardLayoutHook);
hKeyboardLayoutHook = NULL;
}
RemoveTrayIcon();
LogMessage("Cleanup complete");
}
private:
LanguageIndicator() {
LogMessage("LanguageIndicator constructor called");
}
~LanguageIndicator() {
LogMessage("LanguageIndicator destructor called");
if (font) DeleteObject(font);
}
static LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
std::ostringstream oss;
oss << "WindowProc - Message: 0x" << std::hex << uMsg
<< ", wParam: 0x" << wParam
<< ", lParam: 0x" << lParam;
LogMessage(oss.str());
switch (uMsg) {
case WM_CREATE:
UpdateLanguageText(hWnd);
break;
case WM_INPUTLANGCHANGE: {
LogMessage("Processing WM_INPUTLANGCHANGE");
HWND activeWindow = GetForegroundWindow();
oss.str("");
oss << "Active window handle: 0x" << std::hex << (UINT_PTR)activeWindow;
LogMessage(oss.str());
char className[256];
GetClassNameA(activeWindow, className, sizeof(className));
oss.str("");
oss << "Active window class: " << className;
LogMessage(oss.str());
UpdateLanguageText(hWnd);
ShowWindowWithTimer(hWnd);
break;
}
case WM_PAINT: {
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
SetTextColor(hdc, RGB(0, 0, 0));
SetBkMode(hdc, TRANSPARENT);
SelectObject(hdc, font);
RECT rect;
GetClientRect(hWnd, &rect);
TCHAR text[10];
GetWindowText(hWnd, text, 10);
DrawText(hdc, text, -1, &rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
EndPaint(hWnd, &ps);
break;
}
case WM_TIMER:
if (wParam == 1) {
LogMessage("Timer triggered - hiding window");
FadeOutWindow(hWnd);
KillTimer(hWnd, 1);
}
break;
case WM_APP + 1:
if (LOWORD(lParam) == WM_RBUTTONDOWN) {
LogMessage("Tray icon right-clicked - showing menu");
HMENU hMenu = CreatePopupMenu();
AppendMenu(hMenu, MF_STRING, 1, "Exit");
POINT pt;
GetCursorPos(&pt);
SetForegroundWindow(hWnd);
int cmd = TrackPopupMenu(hMenu, TPM_RETURNCMD | TPM_NONOTIFY, pt.x, pt.y, 0, hWnd, NULL);
DestroyMenu(hMenu);
if (cmd == 1) {
LogMessage("Exit menu item selected");
PostQuitMessage(0);
}
}
break;
case WM_DESTROY:
LogMessage("WM_DESTROY received");
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
static void UpdateLanguageText(HWND hWnd) {
LogMessage("UpdateLanguageText - Start");
HKL hkl = GetKeyboardLayout(0);
std::ostringstream oss;
oss << "Current keyboard layout: 0x" << std::hex << (UINT_PTR)hkl;
LogMessage(oss.str());
if (hkl != previousKeyboardLayout) {
LANGID langId = LOWORD(hkl);
TCHAR langName[10] = { 0 };
if (GetLocaleInfo(langId, LOCALE_SISO639LANGNAME, langName, sizeof(langName) / sizeof(TCHAR))) {
oss.str("");
oss << "Language name: " << langName;
LogMessage(oss.str());
SetWindowText(hWnd, langName);
InvalidateRect(hWnd, NULL, TRUE);
UpdateWindow(hWnd);
LogMessage("Window text and display updated");
}
else {
LogMessage("Failed to get language info: " + GetLastErrorAsString());
}
previousKeyboardLayout = hkl;
}
LogMessage("UpdateLanguageText - End");
}
static void AddTrayIcon(HWND hWnd) {
LogMessage("Adding tray icon");
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = 1;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_APP + 1;
nid.hIcon = LoadIcon(NULL, IDI_APPLICATION);
lstrcpyn(nid.szTip, "Language Indicator", sizeof(nid.szTip) / sizeof(TCHAR));
if (Shell_NotifyIcon(NIM_ADD, &nid)) {
LogMessage("Tray icon added successfully");
}
else {
LogMessage("Failed to add tray icon: " + GetLastErrorAsString());
}
}
static void RemoveTrayIcon() {
LogMessage("Removing tray icon");
if (Shell_NotifyIcon(NIM_DELETE, &nid)) {
LogMessage("Tray icon removed successfully");
}
else {
LogMessage("Failed to remove tray icon: " + GetLastErrorAsString());
}
}
static void CenterWindow(HWND hWnd) {
LogMessage("Centering window");
RECT rect;
GetWindowRect(hWnd, &rect);
int width = rect.right - rect.left;
int height = rect.bottom - rect.top;
int screenWidth = GetSystemMetrics(SM_CXSCREEN);
int screenHeight = GetSystemMetrics(SM_CYSCREEN);
int x = (screenWidth - width) / 2;
int y = (screenHeight - height) / 2;
std::ostringstream oss;
oss << "Window position - X: " << x << ", Y: " << y
<< ", Width: " << width << ", Height: " << height;
LogMessage(oss.str());
SetWindowPos(hWnd, HWND_TOPMOST, x, y, width, height, SWP_NOZORDER);
}
static void ShowWindowWithTimer(HWND hWnd) {
LogMessage("ShowWindowWithTimer - Starting fade in");
FadeInWindow(hWnd);
SetTimer(hWnd, 1, 1000, NULL);
LogMessage("Timer set for 1 second");
}
static void FadeInWindow(HWND hWnd) {
LogMessage("FadeInWindow - Start");
ShowWindow(hWnd, SW_SHOW);
CenterWindow(hWnd);
SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE);
for (int i = 0; i <= 255; i += 10) {
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), i, LWA_ALPHA);
Sleep(20);
}
LogMessage("FadeInWindow - Complete");
}
static void FadeOutWindow(HWND hWnd) {
LogMessage("FadeOutWindow - Start");
for (int i = 255; i >= 0; i -= 10) {
SetLayeredWindowAttributes(hWnd, RGB(255, 255, 255), i, LWA_ALPHA);
Sleep(20);
}
ShowWindow(hWnd, SW_HIDE);
LogMessage("FadeOutWindow - Complete");
}
static const TCHAR CLASS_NAME[];
};
const TCHAR LanguageIndicator::CLASS_NAME[] = TEXT("LanguageIndicatorClass");
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
LogMessage("Application starting");
LanguageIndicator& app = LanguageIndicator::GetInstance();
if (!app.Initialize(hInstance)) {
LogMessage("Initialization failed - exiting");
return 1;
}
app.Run();
app.Cleanup();
LogMessage("Application ending");
return 0;
}