1c2c66affSColin Finck /*
2c2c66affSColin Finck  * PROJECT:         ReactOS Keyboard Layout Switcher
3c2c66affSColin Finck  * FILE:            base/applications/kbswitch/kbsdll/kbsdll.c
4c2c66affSColin Finck  * PROGRAMMER:      Dmitry Chapyshev <dmitry@reactos.org>
5c2c66affSColin Finck  *
6c2c66affSColin Finck  */
7c2c66affSColin Finck 
8c2c66affSColin Finck #include "../kbswitch.h"
9c2c66affSColin Finck 
10c2c66affSColin Finck HHOOK hWinHook = NULL;
11c2c66affSColin Finck HHOOK hShellHook = NULL;
12c2c66affSColin Finck HINSTANCE hInstance = NULL;
13c2c66affSColin Finck HWND hKbSwitchWnd = NULL;
14c2c66affSColin Finck 
15c2c66affSColin Finck static VOID
PostMessageToMainWnd(UINT Msg,WPARAM wParam,LPARAM lParam)16*36f7d1a9SKatayama Hirofumi MZ PostMessageToMainWnd(UINT Msg, WPARAM wParam, LPARAM lParam)
17c2c66affSColin Finck {
18c2c66affSColin Finck     PostMessage(hKbSwitchWnd, Msg, wParam, lParam);
19c2c66affSColin Finck }
20c2c66affSColin Finck 
21c2c66affSColin Finck LRESULT CALLBACK
WinHookProc(int code,WPARAM wParam,LPARAM lParam)22c2c66affSColin Finck WinHookProc(int code, WPARAM wParam, LPARAM lParam)
23c2c66affSColin Finck {
24*36f7d1a9SKatayama Hirofumi MZ     if (code < 0)
25*36f7d1a9SKatayama Hirofumi MZ     {
26*36f7d1a9SKatayama Hirofumi MZ         return CallNextHookEx(hWinHook, code, wParam, lParam);
27*36f7d1a9SKatayama Hirofumi MZ     }
28c2c66affSColin Finck 
29c2c66affSColin Finck     switch (code)
30c2c66affSColin Finck     {
31c2c66affSColin Finck         case HCBT_SETFOCUS:
32c2c66affSColin Finck         {
33*36f7d1a9SKatayama Hirofumi MZ             HWND hwndFocus = (HWND)wParam;
34*36f7d1a9SKatayama Hirofumi MZ             if (hwndFocus && hwndFocus != hKbSwitchWnd)
35c2c66affSColin Finck             {
36*36f7d1a9SKatayama Hirofumi MZ                 PostMessageToMainWnd(WM_WINDOW_ACTIVATE, wParam, lParam);
37c2c66affSColin Finck             }
38c2c66affSColin Finck         }
39c2c66affSColin Finck         break;
40c2c66affSColin Finck     }
41c2c66affSColin Finck 
42c2c66affSColin Finck     return CallNextHookEx(hWinHook, code, wParam, lParam);
43c2c66affSColin Finck }
44c2c66affSColin Finck 
45c2c66affSColin Finck LRESULT CALLBACK
ShellHookProc(int code,WPARAM wParam,LPARAM lParam)46c2c66affSColin Finck ShellHookProc(int code, WPARAM wParam, LPARAM lParam)
47c2c66affSColin Finck {
48*36f7d1a9SKatayama Hirofumi MZ     if (code < 0)
49*36f7d1a9SKatayama Hirofumi MZ     {
50*36f7d1a9SKatayama Hirofumi MZ         return CallNextHookEx(hShellHook, code, wParam, lParam);
51*36f7d1a9SKatayama Hirofumi MZ     }
52*36f7d1a9SKatayama Hirofumi MZ 
53c2c66affSColin Finck     switch (code)
54c2c66affSColin Finck     {
55c2c66affSColin Finck         case HSHELL_LANGUAGE:
56c2c66affSColin Finck         {
57*36f7d1a9SKatayama Hirofumi MZ             PostMessageToMainWnd(WM_LANG_CHANGED, wParam, lParam);
58c2c66affSColin Finck         }
59c2c66affSColin Finck         break;
60c2c66affSColin Finck     }
61c2c66affSColin Finck 
62c2c66affSColin Finck     return CallNextHookEx(hShellHook, code, wParam, lParam);
63c2c66affSColin Finck }
64c2c66affSColin Finck 
65c2c66affSColin Finck BOOL WINAPI
KbSwitchSetHooks(VOID)66c2c66affSColin Finck KbSwitchSetHooks(VOID)
67c2c66affSColin Finck {
68c2c66affSColin Finck     hWinHook = SetWindowsHookEx(WH_CBT, WinHookProc, hInstance, 0);
69c2c66affSColin Finck     hShellHook = SetWindowsHookEx(WH_SHELL, ShellHookProc, hInstance, 0);
70c2c66affSColin Finck 
71c2c66affSColin Finck     if (!hWinHook || !hShellHook)
72c2c66affSColin Finck     {
73c2c66affSColin Finck         return FALSE;
74c2c66affSColin Finck     }
75c2c66affSColin Finck 
76c2c66affSColin Finck     return TRUE;
77c2c66affSColin Finck }
78c2c66affSColin Finck 
79c2c66affSColin Finck VOID WINAPI
KbSwitchDeleteHooks(VOID)80c2c66affSColin Finck KbSwitchDeleteHooks(VOID)
81c2c66affSColin Finck {
82*36f7d1a9SKatayama Hirofumi MZ     if (hWinHook)
83*36f7d1a9SKatayama Hirofumi MZ     {
84*36f7d1a9SKatayama Hirofumi MZ         UnhookWindowsHookEx(hWinHook);
85*36f7d1a9SKatayama Hirofumi MZ         hWinHook = NULL;
86*36f7d1a9SKatayama Hirofumi MZ     }
87*36f7d1a9SKatayama Hirofumi MZ     if (hShellHook)
88*36f7d1a9SKatayama Hirofumi MZ     {
89*36f7d1a9SKatayama Hirofumi MZ         UnhookWindowsHookEx(hShellHook);
90*36f7d1a9SKatayama Hirofumi MZ         hShellHook = NULL;
91*36f7d1a9SKatayama Hirofumi MZ     }
92c2c66affSColin Finck }
93c2c66affSColin Finck 
94c2c66affSColin Finck BOOL WINAPI
DllMain(IN HINSTANCE hinstDLL,IN DWORD dwReason,IN LPVOID lpvReserved)95c2c66affSColin Finck DllMain(IN HINSTANCE hinstDLL,
96c2c66affSColin Finck         IN DWORD dwReason,
97c2c66affSColin Finck         IN LPVOID lpvReserved)
98c2c66affSColin Finck {
99c2c66affSColin Finck     switch (dwReason)
100c2c66affSColin Finck     {
101c2c66affSColin Finck         case DLL_PROCESS_ATTACH:
102c2c66affSColin Finck         {
103c2c66affSColin Finck             hInstance = hinstDLL;
104c2c66affSColin Finck             hKbSwitchWnd = FindWindow(szKbSwitcherName, NULL);
105c2c66affSColin Finck             if (!hKbSwitchWnd)
106c2c66affSColin Finck             {
107c2c66affSColin Finck                 return FALSE;
108c2c66affSColin Finck             }
109c2c66affSColin Finck         }
110c2c66affSColin Finck         break;
111c2c66affSColin Finck     }
112c2c66affSColin Finck 
113c2c66affSColin Finck     return TRUE;
114c2c66affSColin Finck }
115