[HEADERS]
[reactos.git] / reactos / base / applications / kbswitch / kbsdll / kbsdll.c
1 /*
2 * PROJECT: ReactOS Keyboard Layout Switcher
3 * FILE: kbswitch/kbsdll/kbsdll.c
4 * PROGRAMMER: Dmitry Chapyshev <dmitry@reactos.org>
5 *
6 */
7
8 #include "../kbswitch.h"
9
10 HHOOK hKeyboardHook, hLangHook, hWinHook;
11 HINSTANCE hInstance;
12 HWND hKbSwitchWnd;
13
14 static VOID
15 SendMessageToMainWnd(UINT Msg, WPARAM wParam, LPARAM lParam)
16 {
17 PostMessage(hKbSwitchWnd, Msg, wParam, lParam);
18 }
19
20 /* Not used yet */
21 LRESULT CALLBACK
22 KeyboardHookProc(int code, WPARAM wParam, LPARAM lParam)
23 {
24 return CallNextHookEx(hKeyboardHook, code, wParam, lParam);
25 }
26
27 LRESULT CALLBACK
28 LangHookProc(int code, WPARAM wParam, LPARAM lParam)
29 {
30 PMSG msg;
31 msg = (PMSG) lParam;
32
33 switch (msg->message)
34 {
35 case WM_INPUTLANGCHANGEREQUEST:
36 {
37 SendMessageToMainWnd(WM_LANG_CHANGED, wParam, msg->lParam);
38 }
39 break;
40
41 case WM_HOTKEY:
42 {
43 if (msg->hwnd)
44 {
45 SendMessageToMainWnd(WM_LOAD_LAYOUT, (WPARAM)msg->hwnd, msg->lParam);
46 }
47 }
48 break;
49 }
50
51 return CallNextHookEx(hLangHook, code, wParam, lParam);
52 }
53
54 LRESULT CALLBACK
55 WinHookProc(int code, WPARAM wParam, LPARAM lParam)
56 {
57 int id = GlobalAddAtom(_T("KBSWITCH"));
58
59 switch (code)
60 {
61 case HCBT_SETFOCUS:
62 {
63 if ((HWND)wParam != NULL)
64 {
65 if ((HWND)wParam != hKbSwitchWnd)
66 {
67 SendMessageToMainWnd(WM_WINDOW_ACTIVATE, wParam, lParam);
68 }
69 }
70 }
71 break;
72
73 case HCBT_CREATEWND:
74 {
75 RegisterHotKey((HWND)wParam, id, MOD_ALT, VK_F10);
76 }
77 break;
78
79 case HCBT_DESTROYWND:
80 {
81 UnregisterHotKey((HWND)wParam, id);
82 }
83 break;
84 }
85
86 GlobalDeleteAtom(id);
87
88 return CallNextHookEx(hWinHook, code, wParam, lParam);
89 }
90
91 BOOL WINAPI
92 KbSwitchSetHooks(VOID)
93 {
94 hKeyboardHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardHookProc, hInstance, 0);
95 hLangHook = SetWindowsHookEx(WH_GETMESSAGE, LangHookProc, hInstance, 0);
96 hWinHook = SetWindowsHookEx(WH_CBT, WinHookProc, hInstance, 0);
97
98 if ((hKeyboardHook)&&(hLangHook)&&(hWinHook))
99 return TRUE;
100 else
101 return FALSE;
102 }
103
104 VOID WINAPI
105 KbSwitchDeleteHooks(VOID)
106 {
107 if (hKeyboardHook) UnhookWindowsHookEx(hKeyboardHook);
108 if (hLangHook) UnhookWindowsHookEx(hLangHook);
109 if (hWinHook) UnhookWindowsHookEx(hWinHook);
110 }
111
112 BOOL WINAPI
113 DllMain(IN HINSTANCE hinstDLL,
114 IN DWORD dwReason,
115 IN LPVOID lpvReserved)
116 {
117 switch (dwReason)
118 {
119 case DLL_PROCESS_ATTACH:
120 hInstance = hinstDLL;
121 hKbSwitchWnd = FindWindow(szKbSwitcherName, NULL);
122 if (!hKbSwitchWnd) return FALSE;
123 break;
124 }
125
126 return TRUE;
127 }