OSDN Git Service

Fix a bug where CallNextHookEx is wrongly called twice in
[xkeymacs/xkeymacs.git] / xkeymacsdll / xkeymacsdll.cpp
1 // xkeymacsdll.cpp : Defines the initialization routines for the DLL.\r
2 //\r
3 \r
4 #include "stdafx.h"\r
5 #include "resource.h"\r
6 #include "Utils.h"\r
7 #include "Commands.h"\r
8 #include <afxdllx.h>\r
9 #include <math.h>\r
10 #include <Imm.h>\r
11 #include <vector>\r
12 \r
13 #ifdef _DEBUG\r
14 #define new DEBUG_NEW\r
15 #undef THIS_FILE\r
16 static char THIS_FILE[] = __FILE__;\r
17 #endif\r
18 \r
19 struct Modifier {\r
20         LPCTSTR name;\r
21         int id;\r
22 };\r
23 \r
24 static const Modifier Modifiers[] = {\r
25 //      { _T("A-"), ALT },\r
26         { _T("C-"), CONTROL},\r
27 //      { _T("H-"), HYPER },\r
28         { _T("M-"), META },\r
29         { _T("S-"), SHIFT },\r
30 //      { _T("s-"), SUPER },\r
31         { _T("Ctrl+"), WIN_CTRL },\r
32         { _T("Alt+"), WIN_ALT },\r
33         { _T("Win+"), WIN_WIN },\r
34 };\r
35 static const int MAX_MODIFIER = _countof(Modifiers);\r
36 \r
37 static const KeyName KeyNames[] = {\r
38 //      { VK_LBUTTON,           _T("mouse-1") },                                // does not work well\r
39 //      { VK_RBUTTON,           _T("mouse-3") },                                // does not work well\r
40         { VK_CANCEL,            _T("break") },\r
41 //      { VK_MBUTTON,           _T("mouse-2") },                                // does not work well\r
42         { VK_BACK,                      _T("backspace") },\r
43         { VK_TAB,                       _T("tab") },\r
44         { VK_RETURN,            _T("return") },\r
45         { VK_CAPITAL,           _T("capslock") },\r
46         { VK_KANA,                      _T("kana") },\r
47         { VK_KANJI,                     _T("kanji") },\r
48         { VK_ESCAPE,            _T("escape") },\r
49         { VK_CONVERT,           _T("convert") },\r
50         { VK_NONCONVERT,        _T("nonconvert") },\r
51 //      { VK_SPACE,                     _T("SPC") },                                    // [? ]\r
52         { VK_PRIOR,                     _T("prior") },\r
53         { VK_NEXT,                      _T("next") },\r
54         { VK_END,                       _T("end") },\r
55         { VK_HOME,                      _T("home") },\r
56         { VK_LEFT,                      _T("left") },\r
57         { VK_UP,                        _T("up") },\r
58         { VK_RIGHT,                     _T("right") },\r
59         { VK_DOWN,                      _T("down") },\r
60         { VK_SELECT,            _T("select") },\r
61         { VK_PRINT,                     _T("print") },\r
62         { VK_EXECUTE,           _T("execute") },\r
63         { VK_SNAPSHOT,          _T("printscreen") },                    // work as print\r
64         { VK_INSERT,            _T("insert") },\r
65         { VK_DELETE,            _T("delete") },\r
66         { VK_LWIN,                      _T("lwindow") },\r
67         { VK_RWIN,                      _T("rwindow") },\r
68         { VK_APPS,                      _T("apps") },\r
69         { VK_SLEEP,                     _T("sleep") },\r
70         { VK_NUMPAD0,           _T("kp-0") },\r
71         { VK_NUMPAD1,           _T("kp-1") },\r
72         { VK_NUMPAD2,           _T("kp-2") },\r
73         { VK_NUMPAD3,           _T("kp-3") },\r
74         { VK_NUMPAD4,           _T("kp-4") },\r
75         { VK_NUMPAD5,           _T("kp-5") },\r
76         { VK_NUMPAD6,           _T("kp-6") },\r
77         { VK_NUMPAD7,           _T("kp-7") },\r
78         { VK_NUMPAD8,           _T("kp-8") },\r
79         { VK_NUMPAD9,           _T("kp-9") },\r
80         { VK_MULTIPLY,          _T("kp-multiply") },\r
81         { VK_ADD,                       _T("kp-add") },\r
82         { VK_SUBTRACT,          _T("kp-subtract") },\r
83         { VK_DECIMAL,           _T("kp-decimal") },\r
84         { VK_DIVIDE,            _T("kp-divide") },\r
85 //      { VK_F1,                        _T("f1") },                                             // FIXME\r
86 //      { VK_F2,                        _T("f2") },                                             // Move at the end of definition of funcgtion keys to keep away confliction f1/f2 and f1?/f2? by _tcsncmp() i.e. strncmp()\r
87         { VK_F3,                        _T("f3") },\r
88         { VK_F4,                        _T("f4") },\r
89         { VK_F5,                        _T("f5") },\r
90         { VK_F6,                        _T("f6") },\r
91         { VK_F7,                        _T("f7") },\r
92         { VK_F8,                        _T("f8") },\r
93         { VK_F9,                        _T("f9") },\r
94         { VK_F10,                       _T("f10") },\r
95         { VK_F11,                       _T("f11") },\r
96         { VK_F12,                       _T("f12") },\r
97         { VK_F13,                       _T("f13") },\r
98         { VK_F14,                       _T("f14") },\r
99         { VK_F15,                       _T("f15") },\r
100         { VK_F16,                       _T("f16") },\r
101         { VK_F17,                       _T("f17") },\r
102         { VK_F18,                       _T("f18") },\r
103         { VK_F19,                       _T("f19") },\r
104         { VK_F20,                       _T("f20") },\r
105         { VK_F21,                       _T("f21") },\r
106         { VK_F22,                       _T("f22") },\r
107         { VK_F23,                       _T("f23") },\r
108         { VK_F24,                       _T("f24") },\r
109         { VK_F1,                        _T("f1") },\r
110         { VK_F2,                        _T("f2") },\r
111         { VK_NUMLOCK,           _T("kp-numlock") },\r
112         { VK_SCROLL,            _T("scroll") },\r
113         { 0xa6,                         _T("browser-back") },                   // VK_BROWSER_BACK\r
114         { 0xa7,                         _T("browser-forward") },                // VK_BROWSER_FORWARD\r
115         { 0xa8,                         _T("browser-refresh") },                // VK_BROWSER_REFRESH\r
116         { 0xa9,                         _T("browser-stop") },                   // VK_BROWSER_STOP\r
117         { 0xaa,                         _T("browser-search") },                 // VK_BROWSER_SEARCH\r
118         { 0xab,                         _T("browser-favorites") },              // VK_BROWSER_FAVORITES\r
119         { 0xac,                         _T("browser-home") },                   // VK_BROWSER_HOME\r
120         { 0xad,                         _T("volume-mute") },                    // VK_VOLUME_MUTE\r
121         { 0xae,                         _T("volume-down") },                    // VK_VOLUME_DOWN\r
122         { 0xaf,                         _T("volume-up") },                              // VK_VOLUME_UP\r
123         { 0xb0,                         _T("media-next-track") },               // VK_MEDIA_NEXT_TRACK\r
124         { 0xb1,                         _T("media-prev-track") },               // VK_MEDIA_PREV_TRACK\r
125         { 0xb2,                         _T("media-stop") },                             // VK_MEDIA_STOP\r
126         { 0xb3,                         _T("media-play-pause") },               // VK_MEDIA_PLAY_PAUSE\r
127         { 0xb4,                         _T("launch-mail") },                    // VK_LAUNCH_MAIL\r
128         { 0xb5,                         _T("launch-media-select") },    // VK_LAUNCH_MEDIA_SELECT\r
129         { 0xb6,                         _T("launch-1") },                               // VK_LAUNCH_APP1\r
130         { 0xb7,                         _T("launch-2") },                               // VK_LAUNCH_APP2\r
131 };\r
132 static const int MAX_KEYNAME = _countof(KeyNames);\r
133 \r
134 static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL };\r
135 \r
136 HINSTANCE g_hDllInst = NULL;\r
137 UINT g_ImeManipulationMessage = 0;\r
138 #pragma data_seg(".xkmcs")\r
139 HHOOK g_hHookKeyboard = NULL;\r
140 HHOOK g_hHookDummy = NULL;\r
141 #pragma data_seg()\r
142 \r
143 inline bool IsWow64(HANDLE mod) {\r
144         typedef BOOL (WINAPI *pfnIsWow64Process_t)(HANDLE, PBOOL);\r
145         if (const pfnIsWow64Process_t IsWow64Process = (pfnIsWow64Process_t)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process")) {\r
146                 BOOL b;\r
147                 return IsWow64Process(mod, &b) && b;\r
148         }\r
149         return false;\r
150 }\r
151 \r
152 inline bool Is64System() {\r
153         SYSTEM_INFO info;\r
154         GetNativeSystemInfo(&info);\r
155         return info.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL;\r
156 }\r
157 \r
158 inline bool Is64Process(HANDLE mod) {\r
159         return Is64System() && !IsWow64(mod);\r
160 }\r
161 \r
162 const bool IsDll64 = sizeof(void *) == 8;\r
163 \r
164 inline bool Is64ProcessHwnd(HWND hwnd) {\r
165         DWORD pid;\r
166         GetWindowThreadProcessId(hwnd, &pid);\r
167         HANDLE hmod = OpenProcess(PROCESS_ALL_ACCESS, 0, pid);\r
168         bool b = Is64Process(hmod);\r
169         CloseHandle(hmod);\r
170         return b;\r
171 }\r
172 \r
173 extern "C" int APIENTRY\r
174 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)\r
175 {\r
176         g_hDllInst = hInstance;\r
177         \r
178         // Remove this if you use lpReserved\r
179         UNREFERENCED_PARAMETER(lpReserved);\r
180 \r
181         switch (dwReason) {\r
182         case DLL_PROCESS_ATTACH:\r
183                 TRACE0("XKEYMACSDLL.DLL Initializing!\n");\r
184                 g_ImeManipulationMessage = RegisterWindowMessage(_T("XkManipulateIME"));\r
185 \r
186                 // Extension DLL one-time initialization\r
187                 if (!AfxInitExtensionModule(XkeymacsdllDLL, hInstance)) {\r
188                         return 0;\r
189                 }\r
190 \r
191                 // Insert this DLL into the resource chain\r
192                 // NOTE: If this Extension DLL is being implicitly linked to by\r
193                 //  an MFC Regular DLL (such as an ActiveX Control)\r
194                 //  instead of an MFC application, then you will want to\r
195                 //  remove this line from DllMain and put it in a separate\r
196                 //  function exported from this Extension DLL.  The Regular DLL\r
197                 //  that uses this Extension DLL should then explicitly call that\r
198                 //  function to initialize this Extension DLL.  Otherwise,\r
199                 //  the CDynLinkLibrary object will not be attached to the\r
200                 //  Regular DLL's resource chain, and serious problems will\r
201                 //  result.\r
202 \r
203                 try {\r
204                         new CDynLinkLibrary(XkeymacsdllDLL);\r
205                 }\r
206                 catch (CMemoryException* e) {\r
207                         e->Delete();\r
208 //                      CUtils::Log("DllMain: 'new' threw an exception");\r
209                 }\r
210                 break;\r
211         case DLL_PROCESS_DETACH:\r
212                 TRACE0("XKEYMACSDLL.DLL Terminating!\n");\r
213                 // Terminate the library before destructors are called\r
214                 AfxTermExtensionModule(XkeymacsdllDLL);\r
215                 break;\r
216         }\r
217         return 1;   // ok\r
218 }\r
219 \r
220 //////////////////////////////////////////////////////////////////////\r
221 // CXkeymacsDll Class\r
222 //////////////////////////////////////////////////////////////////////\r
223 \r
224 #include "xkeymacsDll.h"\r
225 #pragma data_seg(".xkmcs")\r
226         DWORD   CXkeymacsDll::m_nHookAltRelease = 0;\r
227         HHOOK   CXkeymacsDll::m_hHookCallWnd = NULL;\r
228         HHOOK   CXkeymacsDll::m_hHookCallWndRet = NULL;\r
229         HHOOK   CXkeymacsDll::m_hHookGetMessage = NULL;\r
230         HHOOK   CXkeymacsDll::m_hHookShell = NULL;\r
231         BOOL    CXkeymacsDll::m_bRightControl   = FALSE;\r
232         BOOL    CXkeymacsDll::m_bRightAlt               = FALSE;\r
233         BOOL    CXkeymacsDll::m_bRightShift             = FALSE;\r
234         BOOL    CXkeymacsDll::m_bHook                   = TRUE;\r
235         CList<CClipboardSnap *, CClipboardSnap *> CXkeymacsDll::m_oKillRing;\r
236         int             CXkeymacsDll::m_nKillRing = 0;\r
237         int             CXkeymacsDll::m_nOriginal[MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};\r
238         int             CXkeymacsDll::m_nApplicationID = 0;\r
239         int             CXkeymacsDll::m_nAccelerate = 0;\r
240         int             CXkeymacsDll::m_nKeyboardSpeed = 31;\r
241         HCURSOR CXkeymacsDll::m_hCursor[MAX_STATUS] = {'\0'};\r
242         HCURSOR CXkeymacsDll::m_hCurrentCursor = NULL;\r
243         BOOL    CXkeymacsDll::m_bCursor = FALSE;\r
244         TCHAR   CXkeymacsDll::m_M_xTip[128] = "";\r
245         CONFIG  CXkeymacsDll::m_Config = {0};\r
246 #pragma data_seg()\r
247 BOOL CXkeymacsDll::m_bRecordingMacro = FALSE;\r
248 BOOL CXkeymacsDll::m_bDown[MAX_KEY] = {0};\r
249 std::list<KbdMacro> CXkeymacsDll::m_Macro;\r
250 \r
251 //////////////////////////////////////////////////////////////////////\r
252 // Construction/Destruction\r
253 //////////////////////////////////////////////////////////////////////\r
254 \r
255 CXkeymacsDll::CXkeymacsDll()\r
256 {\r
257 \r
258 }\r
259 \r
260 CXkeymacsDll::~CXkeymacsDll()\r
261 {\r
262 \r
263 }\r
264 \r
265 BOOL CXkeymacsDll::SaveConfig()\r
266 {\r
267         TCHAR szTmp[MAX_PATH];\r
268         if (!GetTempPath(MAX_PATH, szTmp))\r
269                 return FALSE;\r
270         if (_tmakepath_s(szTmp, NULL, szTmp, _T("xkeymacs"), _T("tmp")))\r
271                 return FALSE;\r
272         HANDLE hFile = CreateFile(szTmp, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);\r
273         if (hFile == INVALID_HANDLE_VALUE)\r
274                 return FALSE;\r
275         DWORD written;\r
276         BOOL res = WriteFile(hFile, &m_Config, sizeof(m_Config), &written, NULL) || written != sizeof(m_Config);\r
277         CloseHandle(hFile);\r
278         return res;\r
279 }\r
280 \r
281 BOOL CXkeymacsDll::LoadConfig()\r
282 {\r
283         TCHAR szTmp[MAX_PATH];\r
284         if (!GetTempPath(MAX_PATH, szTmp))\r
285                 return FALSE;\r
286         if (_tmakepath_s(szTmp, NULL, szTmp, _T("xkeymacs"), _T("tmp")))\r
287                 return FALSE;\r
288         HANDLE hFile = CreateFile(szTmp, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL);\r
289         if (hFile == INVALID_HANDLE_VALUE)\r
290                 return FALSE;\r
291         DWORD read;\r
292         BOOL res = ReadFile(hFile, &m_Config, sizeof(m_Config), &read, NULL) && read == sizeof(m_Config);\r
293         CloseHandle(hFile);\r
294         return res;\r
295 }\r
296 \r
297 // set hooks\r
298 LRESULT WINAPI DummyProc(int code, WPARAM wp, LPARAM lp) {\r
299         return CallNextHookEx(0, code, wp, lp);\r
300 }\r
301 \r
302 void CXkeymacsDll::SetHooks()\r
303 {\r
304         m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, g_hDllInst, 0);\r
305         m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, g_hDllInst, 0);\r
306         m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hDllInst, 0);\r
307         m_hHookShell = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDllInst, 0);\r
308         g_hHookDummy = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)DummyProc, g_hDllInst, 0);\r
309         g_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, 0);\r
310 }\r
311 \r
312 inline void unhook(HHOOK &hh) {\r
313         if (hh)\r
314                 UnhookWindowsHookEx(hh);\r
315         hh = NULL;\r
316 }\r
317 \r
318 void CXkeymacsDll::ResetHooks() \r
319 {\r
320         ReleaseHooks();\r
321         SetHooks();\r
322 }\r
323 \r
324 // release hooks\r
325 void CXkeymacsDll::ReleaseHooks()\r
326 {\r
327         unhook(m_hHookCallWnd);\r
328         unhook(m_hHookCallWndRet);\r
329         unhook(m_hHookGetMessage);\r
330         unhook(m_hHookShell);\r
331         unhook(g_hHookKeyboard);\r
332         unhook(g_hHookDummy);\r
333 }\r
334 \r
335 void CXkeymacsDll::ToggleKeyboardHookState()\r
336 {\r
337         m_bHook = !m_bHook;\r
338         ShowKeyboardHookState();\r
339 }\r
340 \r
341 void CXkeymacsDll::ShowKeyboardHookState()\r
342 {\r
343         ICONMSG msg = {MAIN_ICON,};\r
344         if (m_bHook) {\r
345                 if (CCommands::IsTemporarilyDisableXKeymacs()) {\r
346                         msg.nState = STATUS_DISABLE_TMP;\r
347                         m_hCurrentCursor = m_hCursor[STATUS_DISABLE_TMP];\r
348                 } else {\r
349                         msg.nState = STATUS_ENABLE;\r
350                         m_hCurrentCursor = m_hCursor[STATUS_ENABLE];\r
351                 }\r
352         } else {\r
353                 msg.nState = STATUS_DISABLE_WOCQ;\r
354         }\r
355         if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE\r
356          || (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default"))\r
357           && CUtils::IsDefaultIgnoreApplication())) {\r
358                 msg.nState = STATUS_DISABLE;\r
359                 m_hCurrentCursor = m_hCursor[STATUS_DISABLE];\r
360         }\r
361         SendIconMessage(&msg, 1);\r
362         DoSetCursor();\r
363 }\r
364 \r
365 BOOL CXkeymacsDll::IsKeyboardHook()\r
366 {\r
367         return m_bHook;\r
368 }\r
369 \r
370 LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)\r
371 {\r
372         if (nCode < 0)\r
373                 return CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);\r
374         const CWPSTRUCT *cwps = reinterpret_cast<CWPSTRUCT *>(lParam);\r
375         switch (cwps->message) {\r
376         case WM_IME_STARTCOMPOSITION:\r
377                 InitKeyboardProc(TRUE);\r
378                 break;\r
379         case WM_IME_ENDCOMPOSITION:\r
380                 InitKeyboardProc(FALSE);\r
381                 break;\r
382         case WM_SETFOCUS:\r
383                 if (cwps->hwnd == GetForegroundWindow()) {\r
384                         InitKeyboardProc(FALSE);\r
385                         ShowKeyboardHookState();\r
386                 }\r
387                 break;\r
388         case WM_NCACTIVATE:\r
389                 if (cwps->wParam && cwps->hwnd == GetForegroundWindow()) {\r
390                         InitKeyboardProc(FALSE);\r
391                         ShowKeyboardHookState();\r
392                 }\r
393                 break;\r
394         }\r
395         return CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);\r
396 }\r
397 \r
398 LRESULT CALLBACK CXkeymacsDll::CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)\r
399 {\r
400         if (nCode >= 0) {\r
401                 const CWPRETSTRUCT *cwprets = reinterpret_cast<CWPRETSTRUCT *>(lParam);\r
402                 switch (cwprets->message) {\r
403                 case WM_SETTEXT:\r
404                         if (cwprets->hwnd == GetForegroundWindow())\r
405                                 InitKeyboardProc(FALSE);\r
406                         break;\r
407                 case WM_SETCURSOR:\r
408                         DoSetCursor();\r
409                         break;\r
410                 }\r
411         }\r
412         return CallNextHookEx(m_hHookCallWndRet, nCode, wParam, lParam);\r
413 }\r
414 \r
415 LRESULT CALLBACK CXkeymacsDll::GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)\r
416 {\r
417         const MSG *msg = reinterpret_cast<MSG *>(lParam);\r
418         if (msg->message == g_ImeManipulationMessage) {\r
419                 if (wParam)\r
420                         CCommands::DoSetInputMethodOpenStatus(static_cast<INPUT_METHOD_OPEN_STATUS>(msg->wParam), static_cast<BOOL>(msg->lParam));\r
421                 return 1;\r
422         }\r
423         switch (msg->message) {\r
424         case WM_IME_STARTCOMPOSITION:\r
425                 InitKeyboardProc(TRUE);\r
426                 break;\r
427         case WM_IME_ENDCOMPOSITION:\r
428                 InitKeyboardProc(FALSE);\r
429                 break;\r
430         }\r
431         return CallNextHookEx(m_hHookGetMessage, nCode, wParam, lParam);\r
432 }\r
433 \r
434 LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam)\r
435 {\r
436         if (nCode == HSHELL_WINDOWACTIVATED) {\r
437                 TCHAR className[256];\r
438                 GetClassName((HWND)wParam, className, 255);\r
439                 if (!_tcsicmp(className, _T("ConsoleWindowClass"))) {\r
440                         InitKeyboardProc(FALSE);\r
441                         ShowKeyboardHookState();\r
442                 }\r
443         }\r
444         return CallNextHookEx( m_hHookShell, nCode, wParam, lParam );\r
445 }\r
446 \r
447 UINT CXkeymacsDll::GetModifierState(BOOL bPhysicalKey)\r
448 {\r
449         UINT result = 0;\r
450         if (IsDown(VK_SHIFT, bPhysicalKey))\r
451                 result |= SHIFT;\r
452         if (IsDown(VK_CONTROL, bPhysicalKey))\r
453                 result |= CONTROL;\r
454         if (IsDown(VK_MENU, bPhysicalKey))\r
455                 result |= META;\r
456         return result;\r
457 }\r
458 \r
459 void CXkeymacsDll::SetModifierState(const UINT after, const UINT before)\r
460 {\r
461         if (after & SHIFT && !(before & SHIFT))\r
462                 DepressKey(VK_SHIFT);\r
463         else if (!(after & SHIFT) && before & SHIFT)\r
464                 ReleaseKey(VK_SHIFT);\r
465 \r
466         if (after & CONTROL && !(before & CONTROL)) {\r
467                 UpdateKeyboardState(VK_CONTROL, 1);\r
468                 DepressKey(VK_CONTROL);\r
469         } else if (!(after & CONTROL) && before & CONTROL) {\r
470                 ReleaseKey(VK_CONTROL);\r
471                 UpdateKeyboardState(VK_CONTROL, 0);\r
472         }\r
473 \r
474         const BOOL bHookApp = CUtils::IsVisualCpp() || CUtils::IsFirefox() || CUtils::IsVisualStudio() || CUtils::IsInternetExplorer();\r
475         if (after & META && !(before & META)) {\r
476                 if (bHookApp)\r
477                         m_nHookAltRelease |= HOOK_ALT_LATER;\r
478                 DepressKey(VK_MENU);\r
479         } else if (!(after & META) && before & META) {\r
480                 if (bHookApp)\r
481                         m_nHookAltRelease++;\r
482                 ReleaseKey(VK_MENU);\r
483         }\r
484 }\r
485 \r
486 BOOL CXkeymacsDll::UpdateKeyboardState(BYTE bVk, BYTE bState)\r
487 {\r
488         BYTE ks[256] = {'\0'};\r
489         if (!GetKeyboardState(ks))\r
490                 return FALSE;\r
491         ks[bVk] = bState;\r
492         return SetKeyboardState(ks);\r
493 }\r
494 \r
495 BOOL CXkeymacsDll::IsDown(BYTE bVk, BOOL bPhysicalKey)\r
496 {\r
497         return bPhysicalKey ? GetAsyncKeyState(bVk) < 0 : GetKeyState(bVk) < 0;\r
498 }\r
499 \r
500 // Do keybd_event\r
501 void CXkeymacsDll::DoKeybd_event(BYTE bVk, DWORD dwFlags)\r
502 {\r
503         // Set KEYEVENTF_EXTENDEDKEY if needed\r
504         switch (bVk) {\r
505         case VK_CONTROL:\r
506                 if (m_bRightControl)\r
507                         dwFlags |= KEYEVENTF_EXTENDEDKEY;\r
508                 break;\r
509 \r
510         case VK_MENU:\r
511                 if (m_bRightAlt)\r
512                         dwFlags |= KEYEVENTF_EXTENDEDKEY;\r
513                 break;\r
514 \r
515         case VK_SHIFT:\r
516                 if (m_bRightShift) {\r
517                         if (CUtils::IsXPorLater())\r
518                                 bVk = VK_RSHIFT;\r
519                         else\r
520                                 dwFlags |= KEYEVENTF_EXTENDEDKEY;\r
521                 }\r
522                 break;\r
523         case VK_PAUSE:\r
524                 if (IsDown(VK_CONTROL, FALSE)) // Break\r
525                         dwFlags |= KEYEVENTF_EXTENDEDKEY;\r
526                 break;\r
527         case VK_INSERT:\r
528         case VK_DELETE:\r
529         case VK_HOME:\r
530         case VK_END:\r
531         case VK_NEXT:\r
532         case VK_PRIOR:\r
533         case VK_UP:\r
534         case VK_DOWN:\r
535         case VK_RIGHT:\r
536         case VK_LEFT:\r
537         case VK_NUMLOCK:\r
538         case VK_PRINT:\r
539                 dwFlags |= KEYEVENTF_EXTENDEDKEY;\r
540                 break;\r
541         }\r
542 //      CUtils::Log(_T("b: %x, %x, %x, %#hx, %#hx"), bVk, dwFlags, GetMessageExtraInfo(), GetKeyState(bVk), GetAsyncKeyState(bVk));\r
543         keybd_event(bVk, 0, dwFlags, GetMessageExtraInfo());\r
544 //      CUtils::Log(_T("a: %x, %x, %x, %#hx, %#hx"), bVk, dwFlags, GetMessageExtraInfo(), GetKeyState(bVk), GetAsyncKeyState(bVk));\r
545 }\r
546 \r
547 // the key is being depressed\r
548 void CXkeymacsDll::DepressKey(BYTE bVk, BOOL bOriginal) // bVk is virtual-key code, MSDN said\r
549 {\r
550         if (bOriginal) {\r
551 //              CUtils::Log(_T("i: %x, %d, %d, %d, %d, %d, %d, %d, %d"), bVk,\r
552 //                      IsDown(VK_CONTROL), IsDown(VK_CONTROL, FALSE), IsDepressedModifier(CCommands::C_), IsDepressedModifier(CCommands::C_, FALSE),\r
553 //                      IsDown(VK_MENU), IsDown(VK_MENU, FALSE), IsDepressedModifier(CCommands::MetaAlt), IsDepressedModifier(CCommands::MetaAlt, FALSE));\r
554                 SetOriginal(GetModifierState(), bVk);\r
555         }\r
556         DoKeybd_event(bVk, 0);\r
557 }\r
558 \r
559 // the key is being released\r
560 void CXkeymacsDll::ReleaseKey(BYTE bVk) // bVk is virtual-key code, MSDN said\r
561 {\r
562         DoKeybd_event(bVk, KEYEVENTF_KEYUP);\r
563 }\r
564 \r
565 // bVk down, bVk up\r
566 void CXkeymacsDll::Kdu(BYTE bVk, DWORD n, BOOL bOriginal)\r
567 {\r
568         while (n--) {\r
569                 DepressKey(bVk, bOriginal);\r
570                 ReleaseKey(bVk);\r
571         }\r
572 }\r
573 \r
574 void CXkeymacsDll::InitKeyboardProc(BOOL bImeComposition)\r
575 {\r
576         CUtils::SetApplicationName(bImeComposition);\r
577         if (_tcsnicmp(m_Config.szSpecialApp[m_nApplicationID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[m_nApplicationID])) {     // PROCESSENTRY32 has only 0xF bytes of Name\r
578                 m_nApplicationID = -1;\r
579                 for (int nAppID = 0; nAppID < MAX_APP; nAppID++) {\r
580                         if (_tcsnicmp(m_Config.szSpecialApp[nAppID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[nAppID]))\r
581                                 continue;\r
582                         if (m_nApplicationID < 0)\r
583                                 m_nApplicationID = nAppID;\r
584                         else {\r
585                                 const LPCSTR curText = m_Config.szWindowText[m_nApplicationID];\r
586                                 const LPCSTR newText = m_Config.szWindowText[nAppID];\r
587                                 const int curType = CUtils::GetWindowTextType(curText);\r
588                                 const int newType = CUtils::GetWindowTextType(newText);\r
589                                 if (curType < newType || curType == newType && _tcscmp(curText, newText) <= 0)\r
590                                         m_nApplicationID = nAppID;\r
591                         }\r
592                 }\r
593                 if (m_nApplicationID < 0)\r
594                         m_nApplicationID = GetAppID(_T("Default"), 0);\r
595         }\r
596         if (m_Config.nSettingStyle[m_nApplicationID] != SETTING_DISABLE &&\r
597                         (_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default")) || !CUtils::IsDefaultIgnoreApplication()) &&\r
598                         !bImeComposition && CUtils::IsDialog() && m_Config.bUseDialogSetting[m_nApplicationID])\r
599                 // Use Dialog Setting\r
600                 m_nApplicationID = GetAppID(_T("Dialog"), m_nApplicationID);\r
601 \r
602         ICONMSG msg[3] = {\r
603                 {CX_ICON, OFF_ICON, ""},\r
604                 {MX_ICON, OFF_ICON, ""},\r
605                 {META_ICON, OFF_ICON, ""}\r
606         };\r
607         SendIconMessage(msg, 3);\r
608         CCommands::SetMark(FALSE);\r
609         CCommands::SetTemporarilyDisableXKeymacs(FALSE);\r
610         CCommands::Reset();\r
611 }\r
612 \r
613 int CXkeymacsDll::GetAppID(const LPCSTR szName, const int fallback)\r
614 {\r
615         for (int i = 0; i < MAX_APP; i++)\r
616                 if (!_tcsicmp(m_Config.szSpecialApp[i], szName))\r
617                         return i;\r
618         return fallback;\r
619 }\r
620 \r
621 LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)\r
622 {\r
623         ASSERT(0 <= wParam && wParam <= UCHAR_MAX);\r
624 \r
625         UINT nCommandType = NONE;\r
626         BYTE nOrigKey = (BYTE)wParam;\r
627 \r
628         static BOOL bLocked = FALSE;\r
629         static const BYTE RECURSIVE_KEY = 0x07;\r
630         static int (*fCommand)() = NULL;\r
631         static BYTE nOneShotModifier[MAX_KEY] = {'\0'};\r
632         static BOOL bCherryOneShotModifier = FALSE;\r
633 \r
634 //      CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %p, %d, %d"), nOrigCode, nKey, lParam, IsDll64, Is64ProcessHwnd(GetForegroundWindow()));\r
635 \r
636         if (Is64ProcessHwnd(GetForegroundWindow()) != IsDll64 || CUtils::IsXkeymacs())\r
637                 return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);\r
638 \r
639         if (nCode < 0 || nCode == HC_NOREMOVE) {\r
640                 goto DO_NOTHING;\r
641         }\r
642 \r
643 //      CUtils::Log(_T("nKey = %#x, ext = %d, rel = %d, pre = %d, %#hx, %#hx"), nOrigKey,\r
644 //              (lParam & EXTENDED_KEY) ? 1 : 0, (lParam & BEING_RELEASED) ? 1 : 0, (lParam & REPEATED_KEY) ? 1 : 0,\r
645 //              GetKeyState(nOrigKey), GetAsyncKeyState(nOrigKey));\r
646 \r
647         if (nOrigKey == RECURSIVE_KEY) {\r
648                 if (lParam & BEING_RELEASED) {\r
649                         goto HOOK_RECURSIVE_KEY;\r
650                 } else {\r
651                         goto RECURSIVE_COMMAND;\r
652                 }\r
653         }\r
654 \r
655         {\r
656                 static BOOL bShift = FALSE;\r
657                 if (IsDepressedShiftKeyOnly(nOrigKey)) {\r
658                         if (lParam & BEING_RELEASED) {\r
659                                 if (bShift) {\r
660                                         CCommands::SetMark(FALSE);\r
661                                 }\r
662                         } else {\r
663                                 bShift = TRUE;\r
664                         }\r
665                 } else {\r
666                         bShift = FALSE;\r
667                 }\r
668         }\r
669 \r
670         BYTE nKey = nOrigKey;\r
671         switch (nKey) {\r
672         case VK_CONTROL:\r
673                 if (lParam & EXTENDED_KEY) {\r
674                         nKey = VK_RCONTROL;\r
675                 } else {\r
676                         nKey = VK_LCONTROL;\r
677                 }\r
678                 break;\r
679         case VK_MENU:\r
680                 if (lParam & EXTENDED_KEY) {\r
681                         nKey = VK_RMENU;\r
682                 } else {\r
683                         nKey = VK_LMENU;\r
684                 }\r
685                 break;\r
686         case VK_SHIFT:\r
687                 if (lParam & EXTENDED_KEY) {\r
688                         nKey = VK_RSHIFT;\r
689                 } else {\r
690                         nKey = VK_LSHIFT;\r
691                 }\r
692                 break;\r
693         default:\r
694                 break;\r
695         }\r
696 \r
697         if (lParam & BEING_RELEASED) {\r
698                 switch (nOrigKey) {\r
699                 case VK_MENU:\r
700                         if (m_nHookAltRelease) {\r
701                                 if (m_nHookAltRelease & ~HOOK_ALT_LATER)\r
702                                         m_nHookAltRelease--;\r
703                                 else if (m_nHookAltRelease & HOOK_ALT_LATER)\r
704                                         m_nHookAltRelease = 0;\r
705                                 goto HOOK;\r
706                         }\r
707                         // pass through\r
708                 case VK_LWIN:\r
709                 case VK_RWIN:\r
710                 case VK_APPS:\r
711                         for (int i = 0; i < MAX_COMMAND_TYPE; i++) {\r
712                                 int (*func)() = Commands[m_Config.nCommandID[m_nApplicationID][i][nKey]].fCommand;\r
713                                 if (func && !(nOrigKey == VK_MENU && func == CCommands::MetaAlt))\r
714                                         goto HOOK;\r
715                         }\r
716                 }\r
717                 if (nOneShotModifier[nKey]) {\r
718                         ReleaseKey(nOneShotModifier[nKey]);\r
719                         nOneShotModifier[nKey] = 0;\r
720 \r
721                         if (bCherryOneShotModifier) {\r
722                                 bCherryOneShotModifier = FALSE;\r
723                                 Kdu(nKey);\r
724                         }\r
725                 }\r
726                 goto DO_NOTHING;\r
727         }\r
728 \r
729         if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE) {\r
730                 goto DO_NOTHING;\r
731         }\r
732 \r
733         // Do Nothing for Meadow, Mule for Win32, ... if those use default setting.\r
734         if (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default"))\r
735          && CUtils::IsDefaultIgnoreApplication()) {\r
736                 goto DO_NOTHING;\r
737         }\r
738 \r
739         switch (IsPassThrough(nKey)) {\r
740         case GOTO_DO_NOTHING:\r
741                 goto DO_NOTHING;\r
742         case GOTO_HOOK:\r
743                 goto HOOK;\r
744         case CONTINUE:\r
745                 break;\r
746         default:\r
747                 ASSERT(0);\r
748                 break;\r
749         }\r
750 \r
751         // set command type\r
752         nCommandType = IsDown(VK_SHIFT) * SHIFT | IsControl() * CONTROL | IsMeta() * META | CCommands::bC_x() * CONTROLX;\r
753         // Ignore undefined C-x ?\r
754         if (nCommandType & CONTROLX) {\r
755                 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == NULL\r
756                  && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < 0) {\r
757                         if (m_Config.bIgnoreUndefinedC_x[m_nApplicationID]) {\r
758                                 CCommands::Reset(GOTO_HOOK);\r
759                                 goto HOOK;\r
760                         }\r
761                         nCommandType &= ~CONTROLX;\r
762                 }\r
763         }\r
764         // Ignore undefined Meta Ctrl+?\r
765         if (CCommands::bM_() && (nCommandType & CONTROL)) {\r
766                 if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == NULL\r
767                  && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < 0) {\r
768                         if (m_Config.bIgnoreUndefinedMetaCtrl[m_nApplicationID]) {\r
769                                 if (CheckOriginal(CONTROL, nKey)) {\r
770                                         goto DO_NOTHING;\r
771                                 }\r
772                                 CCommands::Reset(GOTO_HOOK);\r
773                                 goto HOOK;\r
774                         }\r
775                         nCommandType &= ~META;\r
776                 }\r
777         }\r
778 \r
779         int nVirtualCommandType = GetModifierState(FALSE);\r
780         if (nOrigKey == VK_CONTROL)\r
781                 nVirtualCommandType &= ~CONTROL;\r
782         if (nOrigKey == VK_MENU)\r
783                 nVirtualCommandType &= ~META;\r
784         if (CheckOriginal(nVirtualCommandType, nOrigKey)) {\r
785                 goto DO_NOTHING;\r
786         }\r
787 \r
788         if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EnableOrDisableXKeymacs) {\r
789                 ToggleKeyboardHookState();\r
790                 goto HOOK;\r
791         }\r
792         if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EnableXKeymacs) {\r
793                 if (!m_bHook) {\r
794                         ToggleKeyboardHookState();\r
795                 }\r
796                 goto HOOK;\r
797         }\r
798         if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DisableXKeymacs) {\r
799                 if (m_bHook) {\r
800                         ToggleKeyboardHookState();\r
801                 }\r
802                 goto HOOK;\r
803         }\r
804         if (!m_bHook) {\r
805                 goto DO_NOTHING;\r
806         }\r
807 \r
808         if (CCommands::bM_x()) {\r
809                 static unsigned int index = 0;\r
810                 static TCHAR szPath[MAX_PATH] = {'\0'};\r
811 \r
812                 if (lParam & BEING_RELEASED) {\r
813                         // ignore\r
814                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::BackwardChar) {\r
815                         if (index) {\r
816                                 --index;\r
817                         }\r
818                         goto HOOKX;\r
819                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::BeginningOfLine) {\r
820                         index = 0;\r
821                         goto HOOKX;\r
822                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DeleteBackwardChar) {\r
823                         if (index) {\r
824                                 --index;\r
825                                 memmove(&szPath[index], &szPath[index + 1], _tcslen(szPath) - index);\r
826                                 SetM_xTip(szPath);\r
827                         }\r
828                         goto HOOKX;\r
829                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::DeleteChar) {\r
830                         if (index < _tcslen(szPath)) {\r
831                                 memmove(&szPath[index], &szPath[index + 1], _tcslen(szPath) - index);\r
832                                 SetM_xTip(szPath);\r
833                         }\r
834                         goto HOOKX;\r
835                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::EndOfLine) {\r
836                         index = _tcslen(szPath);\r
837                         goto HOOKX;\r
838                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::ForwardChar) {\r
839                         if (index < _tcslen(szPath)) {\r
840                                 ++index;\r
841                         }\r
842                         goto HOOKX;\r
843                 } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::KeyboardQuit) {\r
844                         CCommands::bM_x(FALSE);\r
845                         index = 0;\r
846                         memset(szPath, 0, sizeof(szPath));\r
847                         goto HOOK;\r
848                 } else if (nKey == VK_RETURN\r
849                                 || Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::Newline) {\r
850                         InvokeM_x(szPath);\r
851 \r
852                         CCommands::bM_x(FALSE);\r
853                         index = 0;\r
854                         memset(szPath, 0, sizeof(szPath));\r
855                         goto HOOK;\r
856                 } else if (index < MAX_PATH - 1) {\r
857                         const BOOL bIsShiftDown = IsDown(VK_SHIFT, FALSE);\r
858                         for (TCHAR nAscii = 1; nAscii != 0; ++nAscii) { // repeat until overflow\r
859                                 if (nKey != 0 && a2v(nAscii) == nKey && bIsShiftDown == IsShift(nAscii)) {\r
860 //                                      CUtils::Log("M-x: %#X (%c), %#X (%c)", nKey, nKey, nAscii, nAscii);\r
861                                         if (index < _tcslen(szPath)) {\r
862                                                 memmove(&szPath[index + 1], &szPath[index], __min(_tcslen(szPath) - index, MAX_PATH - (index + 1) - 1));\r
863                                         }\r
864                                         szPath[index++] = nAscii;\r
865 //                                      CUtils::Log("M-x: %c(%#04x)", nAscii, nAscii);\r
866                                         SetM_xTip(szPath);\r
867                                         goto HOOKX;\r
868                                 }\r
869                         }\r
870                 }\r
871         }\r
872 \r
873         if (CCommands::bC_u()) {\r
874                 if ((nCommandType == NONE) && ('0' <= nKey) && (nKey <= '9')) {\r
875                         CCommands::NumericArgument(nKey - '0');\r
876                         goto HOOK0_9;\r
877                 }\r
878                 if ((nCommandType == NONE) && (nKey == 0xBD)) {\r
879                         CCommands::NumericArgumentMinus();\r
880                         goto HOOK0_9;\r
881                 }\r
882         }\r
883 \r
884         if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~CONTROL][nKey]].fCommand == CCommands::OneShotModifierCtrl) {\r
885                 nOneShotModifier[nKey] = VK_CONTROL;\r
886                 DepressKey(VK_CONTROL);\r
887                 bCherryOneShotModifier = TRUE;\r
888                 goto HOOK;\r
889         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierCtrlRepeat) {\r
890                 nOneShotModifier[nKey] = VK_CONTROL;\r
891                 DepressKey(VK_CONTROL);\r
892                 bCherryOneShotModifier = TRUE;\r
893                 goto HOOK;\r
894         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~CONTROL][nKey]].fCommand == CCommands::OneShotModifierCtrlRepeat) {\r
895                 ReleaseKey(VK_CONTROL);\r
896                 bCherryOneShotModifier = FALSE;\r
897                 Kdu(nKey);\r
898                 goto HOOK;\r
899         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~META][nKey]].fCommand == CCommands::OneShotModifierAlt) {\r
900                 nOneShotModifier[nKey] = VK_MENU;\r
901                 DepressKey(VK_MENU);\r
902                 bCherryOneShotModifier = TRUE;\r
903                 goto HOOK;\r
904         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierAltRepeat) {\r
905                 nOneShotModifier[nKey] = VK_MENU;\r
906                 DepressKey(VK_MENU);\r
907                 bCherryOneShotModifier = TRUE;\r
908                 goto HOOK;\r
909         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~META][nKey]].fCommand == CCommands::OneShotModifierAltRepeat) {\r
910                 ReleaseKey(VK_MENU);\r
911                 bCherryOneShotModifier = FALSE;\r
912                 Kdu(nKey);\r
913                 goto HOOK;\r
914         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~SHIFT][nKey]].fCommand == CCommands::OneShotModifierShift) {\r
915                 nOneShotModifier[nKey] = VK_SHIFT;\r
916                 DepressKey(VK_SHIFT);\r
917                 bCherryOneShotModifier = TRUE;\r
918                 goto HOOK;\r
919         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand == CCommands::OneShotModifierShiftRepeat) {\r
920                 nOneShotModifier[nKey] = VK_SHIFT;\r
921                 DepressKey(VK_SHIFT);\r
922                 bCherryOneShotModifier = TRUE;\r
923                 goto HOOK;\r
924         } else if (Commands[m_Config.nCommandID[m_nApplicationID][nCommandType & ~SHIFT][nKey]].fCommand == CCommands::OneShotModifierShiftRepeat) {\r
925                 ReleaseKey(VK_SHIFT);\r
926                 bCherryOneShotModifier = FALSE;\r
927                 Kdu(nKey);\r
928                 goto HOOK;\r
929         } else {\r
930                 for (int i = 0; i < MAX_KEY; ++i) {\r
931                         if (nOneShotModifier[i] == nKey) {\r
932                                 break;\r
933                         }\r
934                 }\r
935                 if (i == MAX_KEY) {\r
936                         bCherryOneShotModifier = FALSE;\r
937                 }\r
938         }\r
939 \r
940         if (0 <= m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]\r
941          && m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey] < MAX_FUNCTION\r
942          && _tcslen(m_Config.szFunctionDefinition[m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]])) {\r
943                 CallFunction(m_Config.nFunctionID[m_nApplicationID][nCommandType][nKey]);\r
944                 CCommands::Reset(GOTO_HOOK);\r
945                 goto HOOK;\r
946         }\r
947 \r
948         if (!Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand) {\r
949                 if (nOrigKey == VK_CONTROL || nOrigKey == VK_MENU || nOrigKey == VK_SHIFT) {\r
950                         goto DO_NOTHING;\r
951                 }\r
952 \r
953                 if (!(nCommandType & SHIFT)) {\r
954                         if (CCommands::IsSetMark()) {\r
955                                 if (CCommands::MoveCaret(nKey, nCommandType & CONTROL) != CONTINUE) {\r
956                                         CCommands::ClearNumericArgument();\r
957                                         goto HOOK;\r
958                                 }\r
959                                 CCommands::SetMark(FALSE);\r
960                         }\r
961                 }\r
962 \r
963                 if (1 < CCommands::GetNumericArgument()) {\r
964                         Kdu(nKey, CCommands::GetNumericArgument());\r
965                         CCommands::ClearNumericArgument();\r
966                         goto HOOK;\r
967                 }\r
968 \r
969                 goto DO_NOTHING;\r
970         }\r
971 \r
972         if (CCommands::IsTemporarilyDisableXKeymacs()\r
973          && Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand != CCommands::KeyboardQuit) {\r
974                 CCommands::SetTemporarilyDisableXKeymacs(FALSE);\r
975                 goto DO_NOTHING;\r
976         }\r
977 \r
978         m_bRightControl = IsDown(VK_RCONTROL, FALSE);\r
979         m_bRightAlt = IsDown(VK_RMENU, FALSE);\r
980         m_bRightShift = IsDown(VK_RSHIFT, FALSE);\r
981 \r
982         if (!bLocked) {\r
983                 bLocked = TRUE;\r
984                 fCommand = Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][nKey]].fCommand;\r
985 RECURSIVE_COMMAND:\r
986                 switch (fCommand()) {\r
987                 case GOTO_DO_NOTHING:\r
988                         bLocked = FALSE;\r
989                         goto DO_NOTHING;\r
990                 case GOTO_HOOK:\r
991                         bLocked = FALSE;\r
992                         goto HOOK;\r
993                 case GOTO_RECURSIVE:\r
994                         goto RECURSIVE;\r
995                 case GOTO_HOOKX:\r
996                         bLocked = FALSE;\r
997                         goto HOOKX;\r
998                 case GOTO_HOOK0_9:\r
999                         bLocked = FALSE;\r
1000                         goto HOOK0_9;\r
1001                 default:\r
1002                         ASSERT(0);\r
1003                         bLocked = FALSE;\r
1004                         goto DO_NOTHING;\r
1005                 }\r
1006         } else {\r
1007                 goto HOOK_RECURSIVE_KEY;\r
1008         }\r
1009 \r
1010 DO_NOTHING:\r
1011         SetModifierIcons();\r
1012         if (m_bRecordingMacro && (!(lParam & BEING_RELEASED) || m_bDown[wParam])) {\r
1013                 KbdMacro m = { nCode, wParam, lParam, TRUE };\r
1014                 m_Macro.push_back(m);\r
1015                 m_bDown[wParam] |= !(lParam & BEING_RELEASED);\r
1016         }\r
1017         return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);\r
1018 \r
1019 RECURSIVE:\r
1020         Kdu(RECURSIVE_KEY, 1, FALSE);\r
1021         goto HOOKX;\r
1022 HOOK:\r
1023         CCommands::SetLastCommand(fCommand);\r
1024 HOOK0_9:\r
1025 HOOKX:\r
1026         SetModifierIcons();\r
1027 HOOK_RECURSIVE_KEY:\r
1028         return TRUE;\r
1029 }\r
1030 \r
1031 void CXkeymacsDll::SetModifierIcons()\r
1032 {\r
1033 #define IconState(x) ((x) ? ON_ICON : OFF_ICON)\r
1034         ICONMSG msg[6] = {\r
1035                 {MX_ICON, IconState(CCommands::bM_x()), ""},\r
1036                 {CX_ICON, IconState(CCommands::bC_x()), ""},\r
1037                 {META_ICON, IconState(CCommands::bM_()), ""},\r
1038                 {SHIFT_ICON, IconState(IsDown(VK_SHIFT, FALSE)), ""},\r
1039                 {CTRL_ICON, IconState(IsControl()), ""},\r
1040                 {ALT_ICON, IconState(IsDown(VK_MENU, FALSE)), ""}\r
1041         };\r
1042         _tcscpy_s(msg[0].szTip, m_M_xTip);\r
1043         SendIconMessage(msg, 6);\r
1044 }\r
1045 \r
1046 void CXkeymacsDll::SetApplicationName(int nApplicationID, CString szApplicationName)\r
1047 {\r
1048         ZeroMemory(m_Config.szSpecialApp[nApplicationID], CLASS_NAME_LENGTH);\r
1049         _tcsncpy_s(m_Config.szSpecialApp[nApplicationID], szApplicationName, _TRUNCATE);\r
1050 }\r
1051 \r
1052 void CXkeymacsDll::SetWindowText(int nApplicationID, CString szWindowText)\r
1053 {\r
1054         ZeroMemory(m_Config.szWindowText[nApplicationID], WINDOW_TEXT_LENGTH);\r
1055         _tcsncpy_s(m_Config.szWindowText[nApplicationID], szWindowText, _TRUNCATE);\r
1056 }\r
1057 \r
1058 void CXkeymacsDll::SetCommandID(int nApplicationID, int nCommandType, int nKey, int nCommandID)\r
1059 {\r
1060         m_Config.nCommandID[nApplicationID][nCommandType][nKey] = nCommandID;\r
1061 }\r
1062 \r
1063 void CXkeymacsDll::SetKillRingMax(int nApplicationID, int nKillRingMax)\r
1064 {\r
1065         m_Config.nKillRingMax[nApplicationID] = nKillRingMax;\r
1066 }\r
1067 \r
1068 void CXkeymacsDll::SetUseDialogSetting(int nApplicationID, BOOL bUseDialogSetting)\r
1069 {\r
1070         m_Config.bUseDialogSetting[nApplicationID] = bUseDialogSetting;\r
1071 }\r
1072 \r
1073 // Clear data of nApplicationID\r
1074 void CXkeymacsDll::Clear(int nApplicationID)\r
1075 {\r
1076         if (0 <= nApplicationID && nApplicationID < MAX_APP) {\r
1077                 ZeroMemory(m_Config.szSpecialApp[nApplicationID], sizeof(m_Config.szSpecialApp[nApplicationID]));\r
1078                 ZeroMemory(m_Config.nCommandID[nApplicationID], sizeof(m_Config.nCommandID[nApplicationID]));\r
1079                 m_Config.nKillRingMax[nApplicationID] = 0;\r
1080                 m_Config.bUseDialogSetting[nApplicationID] = FALSE;\r
1081                 m_Config.nSettingStyle[nApplicationID] = 0;\r
1082         } else {\r
1083                 ASSERT(0);\r
1084         }\r
1085 }\r
1086 \r
1087 BOOL CXkeymacsDll::IsValidKey(BYTE bVk)\r
1088 {\r
1089         if (bVk == 0xf0) {      // 0xf0: Eisu key. GetAsyncKeyState returns the wrong state of Eisu key.\r
1090                 return FALSE;\r
1091         }\r
1092 \r
1093         switch (bVk) {\r
1094         case VK_CONTROL:\r
1095         case VK_MENU:\r
1096         case VK_SHIFT:\r
1097                 return FALSE;\r
1098         default:\r
1099                 break;\r
1100         }\r
1101 \r
1102         return TRUE;\r
1103 }\r
1104 \r
1105 BOOL CXkeymacsDll::IsDepressedModifier(int (__cdecl *Modifier)(void), BOOL bPhysicalKey)\r
1106 {\r
1107         BYTE bVk = 0;\r
1108         do {\r
1109                 if (IsValidKey(bVk) && IsDown(bVk, bPhysicalKey) &&\r
1110                                 Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == Modifier)\r
1111                         return TRUE;\r
1112         } while (++bVk);\r
1113         return FALSE;\r
1114 }\r
1115 \r
1116 BOOL CXkeymacsDll::IsDepressedShiftKeyOnly(BYTE nKey)\r
1117 {\r
1118         if (nKey != VK_SHIFT\r
1119          && nKey != VK_LSHIFT\r
1120          && nKey != VK_RSHIFT) {\r
1121                 return FALSE;\r
1122         }\r
1123 \r
1124         BYTE bVk = 0;\r
1125         do {\r
1126                 if (bVk == VK_SHIFT\r
1127                  || bVk == VK_LSHIFT\r
1128                  || bVk == VK_RSHIFT) {\r
1129                         continue;\r
1130                 }\r
1131 \r
1132                 if (IsDown(bVk, FALSE)) {\r
1133                         return FALSE;\r
1134                 }\r
1135         } while (++bVk);\r
1136         return TRUE;\r
1137 }\r
1138 \r
1139 BOOL CXkeymacsDll::IsControl()\r
1140 {\r
1141         return CCommands::bC_() || IsDepressedModifier(CCommands::C_);\r
1142 }\r
1143 \r
1144 BOOL CXkeymacsDll::IsMeta()\r
1145 {\r
1146         return CCommands::bM_() || IsDepressedModifier(CCommands::MetaAlt);\r
1147 }\r
1148 \r
1149 void CXkeymacsDll::AddKillRing(BOOL bNewData)\r
1150 {\r
1151         if (m_Config.nKillRingMax[m_nApplicationID] == 0) {\r
1152                 return;\r
1153         }\r
1154 \r
1155         CClipboardSnap *pSnap = new CClipboardSnap;\r
1156         if( !pSnap ) return;\r
1157 \r
1158         BOOL bCapture = pSnap->Capture();\r
1159         bCapture = pSnap->Capture();    // for "office drawing shape format". Can CClipboardSnap care this problem?\r
1160 \r
1161         if( bCapture ) {\r
1162                 if (bNewData) {\r
1163                         m_oKillRing.AddHead(pSnap);\r
1164                 } else {\r
1165                         if (m_oKillRing.IsEmpty()) {\r
1166                                 m_oKillRing.AddHead(pSnap);\r
1167                         } else {\r
1168                                 for (CClipboardSnap *pParent = m_oKillRing.GetHead(); pParent->GetNext(); pParent = pParent->GetNext()) {\r
1169                                         ;\r
1170                                 }\r
1171                                 pParent->SetNext(pSnap);\r
1172                         }\r
1173                 }\r
1174         } else {\r
1175                 delete pSnap;\r
1176                 pSnap = NULL;\r
1177         }\r
1178 \r
1179         m_nKillRing = 0;\r
1180 \r
1181         if (m_Config.nKillRingMax[m_nApplicationID] < m_oKillRing.GetCount()) {\r
1182                 CClipboardSnap *pSnap = m_oKillRing.GetTail();\r
1183                 delete pSnap;\r
1184                 pSnap = NULL;\r
1185                 m_oKillRing.RemoveTail();\r
1186         }\r
1187 }\r
1188 \r
1189 // Return TRUE if there is another data\r
1190 // Return FALSE if there is no more data\r
1191 CClipboardSnap* CXkeymacsDll::GetKillRing(CClipboardSnap* pSnap, BOOL bForce)\r
1192 {\r
1193         if (m_Config.nKillRingMax[m_nApplicationID] == 0) {\r
1194                 return NULL;\r
1195         }\r
1196 \r
1197         if (m_oKillRing.IsEmpty()) {\r
1198                 return NULL;\r
1199         }\r
1200 \r
1201         m_nKillRing %= m_oKillRing.GetCount();\r
1202 \r
1203         if (!bForce) {\r
1204                 CClipboardSnap oCurrentSnap;\r
1205                 oCurrentSnap.Capture();\r
1206 \r
1207                 CClipboardSnap *pKillRing = m_oKillRing.GetAt(m_oKillRing.FindIndex(m_nKillRing));\r
1208                 if (!pKillRing) {\r
1209                         return NULL;\r
1210                 }\r
1211                 for (; pKillRing->GetNext(); pKillRing = pKillRing->GetNext()) {\r
1212                         ;\r
1213                 }\r
1214                 if (*pKillRing != oCurrentSnap) {\r
1215                         return NULL;\r
1216                 }\r
1217         }\r
1218 \r
1219         if (!pSnap) {\r
1220                 pSnap = m_oKillRing.GetAt(m_oKillRing.FindIndex(m_nKillRing));\r
1221         }\r
1222         pSnap->Restore();\r
1223 \r
1224         return pSnap->GetNext();\r
1225 }\r
1226 \r
1227 void CXkeymacsDll::SetOriginal(UINT nCommandType, BYTE bVk)\r
1228 {\r
1229         m_nOriginal[nCommandType & ~SHIFT][bVk]++;\r
1230 }\r
1231 \r
1232 int CXkeymacsDll::CheckOriginal(UINT nCommandType, BYTE bVk)\r
1233 {\r
1234         nCommandType &= ~SHIFT;\r
1235         if (m_nOriginal[nCommandType][bVk])\r
1236                 return m_nOriginal[nCommandType][bVk]--;\r
1237         return 0;\r
1238 }\r
1239 \r
1240 void CXkeymacsDll::IncreaseKillRingIndex(int nKillRing)\r
1241 {\r
1242         m_nKillRing += nKillRing;\r
1243 }\r
1244 \r
1245 void CXkeymacsDll::SetSettingStyle(int nApplicationID, int nSettingStyle)\r
1246 {\r
1247         m_Config.nSettingStyle[nApplicationID] = nSettingStyle;\r
1248 }\r
1249 \r
1250 void CXkeymacsDll::SetIgnoreUndefinedMetaCtrl(int nApplicationID, BOOL bIgnoreUndefinedMetaCtrl)\r
1251 {\r
1252         m_Config.bIgnoreUndefinedMetaCtrl[nApplicationID] = bIgnoreUndefinedMetaCtrl;\r
1253 }\r
1254 \r
1255 void CXkeymacsDll::SetIgnoreUndefinedC_x(int nApplicationID, BOOL bIgnoreUndefinedC_x)\r
1256 {\r
1257         m_Config.bIgnoreUndefinedC_x[nApplicationID] = bIgnoreUndefinedC_x;\r
1258 }\r
1259 \r
1260 void CXkeymacsDll::SetEnableCUA(int nApplicationID, BOOL bEnableCUA)\r
1261 {\r
1262         m_Config.bEnableCUA[nApplicationID] = bEnableCUA;\r
1263 }\r
1264 \r
1265 BOOL CXkeymacsDll::GetEnableCUA()\r
1266 {\r
1267         return m_Config.bEnableCUA[m_nApplicationID];\r
1268 }\r
1269 \r
1270 void CXkeymacsDll::StartRecordMacro()\r
1271 {\r
1272         if (CCommands::bC_u())\r
1273                 CallMacro();\r
1274         m_bRecordingMacro = TRUE;\r
1275         m_Macro.erase(m_Macro.begin(), m_Macro.end());\r
1276         ZeroMemory(m_bDown, MAX_KEY);\r
1277 }\r
1278 \r
1279 void CXkeymacsDll::EndRecordMacro()\r
1280 {\r
1281         m_bRecordingMacro = FALSE;\r
1282         while (!m_Macro.empty()) { // remove not released push\r
1283                 const KbdMacro& m = m_Macro.back();\r
1284                 if (m.lParam & BEING_RELEASED)\r
1285                         break;\r
1286                 m_Macro.pop_back();\r
1287         }\r
1288 }\r
1289 \r
1290 void CXkeymacsDll::CallMacro()\r
1291 {\r
1292         if (m_bRecordingMacro)\r
1293                 m_bRecordingMacro = FALSE;\r
1294         UINT before = GetModifierState(FALSE);\r
1295         SetModifierState(0, before);\r
1296         for (auto m = m_Macro.begin(); m != m_Macro.end(); m++)\r
1297                 if (m->lParam & BEING_RELEASED)\r
1298                         ReleaseKey(static_cast<BYTE>(m->wParam));\r
1299                 else\r
1300                         DepressKey(static_cast<BYTE>(m->wParam), m->bOriginal);\r
1301         SetModifierState(before, 0);\r
1302 }\r
1303 \r
1304 void CXkeymacsDll::Set106Keyboard(BOOL b106Keyboard)\r
1305 {\r
1306         m_Config.b106Keyboard = b106Keyboard;\r
1307 }\r
1308 \r
1309 BOOL CXkeymacsDll::Is106Keyboard()\r
1310 {\r
1311         return m_Config.b106Keyboard;\r
1312 }\r
1313 \r
1314 int CXkeymacsDll::IsPassThrough(BYTE nKey)\r
1315 {\r
1316         BYTE bVk = 0;\r
1317         do {\r
1318                 if (IsDown(bVk)\r
1319                  && (Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == CCommands::PassThrough)) {\r
1320                         if (bVk == nKey) {\r
1321                                 return GOTO_HOOK;\r
1322                         }\r
1323 \r
1324                         return GOTO_DO_NOTHING;\r
1325                 }\r
1326         } while (++bVk);\r
1327         return CONTINUE;\r
1328 }\r
1329 \r
1330 void CXkeymacsDll::SetFunctionKey(int nFunctionID, int nApplicationID, int nCommandType, int nKey)\r
1331 {\r
1332         if (nApplicationID      < 0 || MAX_APP                  <= nApplicationID\r
1333          || nCommandType        < 0 || MAX_COMMAND_TYPE <= nCommandType\r
1334          || nKey                        < 0 || MAX_KEY                  <= nKey) {\r
1335                 return;\r
1336         }\r
1337 \r
1338         m_Config.nFunctionID[nApplicationID][nCommandType][nKey] = nFunctionID;\r
1339 }\r
1340 \r
1341 void CXkeymacsDll::ClearFunctionDefinition()\r
1342 {\r
1343         memset(m_Config.nFunctionID, -1, sizeof(m_Config.nFunctionID));\r
1344         memset(m_Config.szFunctionDefinition, 0, sizeof(m_Config.szFunctionDefinition));\r
1345 }\r
1346 \r
1347 void CXkeymacsDll::SetFunctionDefinition(int nFunctionID, CString szDefinition)\r
1348 {\r
1349         if (nFunctionID < 0 || nFunctionID >= MAX_FUNCTION)\r
1350                 return;\r
1351         memset(m_Config.szFunctionDefinition[nFunctionID], 0, sizeof(m_Config.szFunctionDefinition[nFunctionID]));\r
1352         _tcscpy_s(m_Config.szFunctionDefinition[nFunctionID], szDefinition);\r
1353         return;\r
1354 }\r
1355 \r
1356 // call an original command which is defined in dot.xkeymacs\r
1357 void CXkeymacsDll::CallFunction(int nFunctionID)\r
1358 {\r
1359         if (nFunctionID < 0 || nFunctionID >= MAX_FUNCTION)\r
1360                 return;\r
1361         LPCTSTR def = m_Config.szFunctionDefinition[nFunctionID];\r
1362         if (!def[0])\r
1363                 return;\r
1364         std::vector<KeyBind> keybinds;\r
1365         const LPCTSTR last = def + _tcslen(def) - 1;\r
1366         if (*def == _T('"') && *last == _T('"')) {\r
1367                 def++; // skip '"'\r
1368                 while (def < last)\r
1369                         keybinds.push_back(ParseKey(def));\r
1370         } else if (*def == _T('[') && *last == _T(']')) {\r
1371                 while (++def < last) { // skip '[', ']', and ' '\r
1372                         if (*def == _T('?')) { // [?f ?o ?o]\r
1373                                 keybinds.push_back(ParseKey(++def));\r
1374                                 continue;\r
1375                         }\r
1376                         // [VK]\r
1377                         for (int i = 0; i < MAX_KEYNAME; i++) {\r
1378                                 size_t keylen = _tcslen(KeyNames[i].name);\r
1379                                 if (!_tcsncmp(def, KeyNames[i].name, keylen)) {\r
1380                                         KeyBind keybind = {NONE, KeyNames[i].bVk};\r
1381                                         keybinds.push_back(keybind);\r
1382                                         def += keylen;\r
1383                                         break;\r
1384                                 }\r
1385                         }\r
1386                 }\r
1387         } else\r
1388                 return;\r
1389 \r
1390         BOOL bM_x = FALSE;\r
1391         TCHAR szPath[MAX_PATH] = {'\0'};\r
1392         unsigned int index = 0;\r
1393         BOOL bInitialized = FALSE;\r
1394         UINT before = GetModifierState(FALSE);\r
1395 \r
1396         for (auto p = keybinds.begin(); p != keybinds.end(); p++) {\r
1397                 const int nCommandType = p->nCommandType;\r
1398                 const BYTE bVk = p->bVk;\r
1399                 int (*fCommand)() = nCommandType < MAX_COMMAND_TYPE ? Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][bVk]].fCommand : NULL;\r
1400                 if (fCommand) {\r
1401                         if (fCommand == CCommands::ExecuteExtendedCommand)\r
1402                                 bM_x = TRUE;\r
1403                         else if (!bInitialized) {\r
1404                                 SetModifierState(0, before);\r
1405                                 bInitialized = TRUE;\r
1406                         }\r
1407 //                      CUtils::Log("CallFunction: Command Name: %s", Commands[m_Config.nCommandID[m_nApplicationID][nCommandType][bVk]].szCommandName);\r
1408                         while (fCommand() == GOTO_RECURSIVE)\r
1409                                 ;\r
1410                         continue;\r
1411                 }\r
1412                 if (bM_x) {\r
1413                         if (bVk == VK_RETURN)\r
1414                                 InvokeM_x(szPath);\r
1415                         else if (bVk != 0) {\r
1416                                 TCHAR nAscii = 0;\r
1417                                 do { // 1-127\r
1418                                         if (a2v(++nAscii) == bVk && ((nCommandType & SHIFT) != 0) == IsShift(nAscii)) {\r
1419 //                                              CUtils::Log("M-x: %#X (%c), %#X (%c)", bVk, bVk, nAscii, nAscii);\r
1420                                                 szPath[index++] = nAscii;\r
1421                                                 break;\r
1422                                         }\r
1423                                 } while (nAscii != 127);\r
1424                         }\r
1425                         continue;\r
1426                 }\r
1427                 if (!bInitialized) {\r
1428                         SetModifierState(0, before);\r
1429                         bInitialized = TRUE;\r
1430                 }\r
1431                 if (nCommandType & WIN_WIN)\r
1432                         DepressKey(VK_LWIN);\r
1433                 if (nCommandType & WIN_CTRL)\r
1434                         DepressKey(VK_CONTROL);\r
1435                 if (nCommandType & WIN_ALT)\r
1436                         DepressKey(VK_MENU);\r
1437                 if (nCommandType & SHIFT)\r
1438                         DepressKey(VK_SHIFT);\r
1439                 Kdu(bVk);\r
1440                 const int nNextType = (p + 1) != keybinds.end() ? (p + 1)->nCommandType : 0;\r
1441                 if (nCommandType & SHIFT && !(nNextType & SHIFT))\r
1442                         ReleaseKey(VK_SHIFT);\r
1443                 if (nCommandType & WIN_ALT && !(nNextType & WIN_ALT))\r
1444                         ReleaseKey(VK_MENU);\r
1445                 if (nCommandType & WIN_CTRL && !(nNextType & WIN_CTRL))\r
1446                         ReleaseKey(VK_CONTROL);\r
1447                 if (nCommandType & WIN_WIN && !(nNextType & WIN_WIN))\r
1448                         ReleaseKey(VK_LWIN);\r
1449         }\r
1450 \r
1451         if (bInitialized)\r
1452                 // If this lines is invoked on M-x, a window transition does not work well.\r
1453                 SetModifierState(before, 0);\r
1454         return;\r
1455 }\r
1456 \r
1457 KeyBind CXkeymacsDll::ParseKey(LPCTSTR& def)\r
1458 {\r
1459         KeyBind keybind = {NONE};\r
1460         if (*def == _T('\\')) { // set modifiers\r
1461                 def++;\r
1462         LOOP:\r
1463                 for (int i = 0; i < MAX_MODIFIER; i++) {\r
1464                         size_t len = _tcslen(Modifiers[i].name);\r
1465                         if (!_tcsncmp(def, Modifiers[i].name, len)) {\r
1466                                 keybind.nCommandType |= Modifiers[i].id;\r
1467                                 def += len;\r
1468                                 goto LOOP;\r
1469                         }\r
1470                 }\r
1471         }\r
1472         if (IsShift(*def) && !(keybind.nCommandType & (WIN_CTRL | WIN_ALT | WIN_WIN)))\r
1473                 keybind.nCommandType |= SHIFT;\r
1474         int i = 0;\r
1475         for (; i < MAX_KEYNAME; i++) {\r
1476                 size_t len = _tcslen(KeyNames[i].name);\r
1477                 if (!_tcsncmp(def, KeyNames[i].name, len)) {\r
1478                         def += len;\r
1479                         break;\r
1480                 }\r
1481         }\r
1482         keybind.bVk = i < MAX_KEYNAME ? KeyNames[i].bVk : a2v(*def++);\r
1483         return keybind;\r
1484 }\r
1485 \r
1486 BOOL CXkeymacsDll::IsShift(TCHAR nAscii)\r
1487 {\r
1488         switch (nAscii) {\r
1489         case _T(' '):\r
1490                 return FALSE;\r
1491         case _T('!'):\r
1492         case _T('"'):\r
1493         case _T('#'):\r
1494         case _T('$'):\r
1495         case _T('%'):\r
1496         case _T('&'):\r
1497                 return TRUE;\r
1498         case _T('\''):\r
1499                 return m_Config.b106Keyboard;\r
1500         case _T('('):\r
1501         case _T(')'):\r
1502         case _T('*'):\r
1503         case _T('+'):\r
1504                 return TRUE;\r
1505         case _T(','):\r
1506         case _T('-'):\r
1507         case _T('.'):\r
1508         case _T('/'):\r
1509         case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):\r
1510                 return FALSE;\r
1511         case _T(':'):\r
1512                 return !m_Config.b106Keyboard;\r
1513         case _T(';'):\r
1514                 return FALSE;\r
1515         case _T('<'):\r
1516                 return TRUE;\r
1517         case _T('='):\r
1518                 return m_Config.b106Keyboard;\r
1519         case _T('>'):\r
1520         case _T('?'):\r
1521                 return TRUE;\r
1522         case _T('@'):\r
1523                 return !m_Config.b106Keyboard;\r
1524         case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'): \r
1525         case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'): \r
1526         case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'): \r
1527                 return TRUE;\r
1528         case _T('['):\r
1529         case _T('\\'):\r
1530         case _T(']'):\r
1531                 return FALSE;\r
1532         case _T('^'):\r
1533                 return !m_Config.b106Keyboard;\r
1534         case _T('_'):\r
1535                 return TRUE;\r
1536         case _T('`'):\r
1537                 return m_Config.b106Keyboard;\r
1538         case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'): \r
1539         case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'): \r
1540         case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'): \r
1541                 return FALSE;\r
1542         case _T('{'):\r
1543         case _T('|'):\r
1544         case _T('}'):\r
1545         case _T('~'):\r
1546                 return TRUE;\r
1547         default:\r
1548                 return FALSE;\r
1549         }\r
1550 }\r
1551 \r
1552 BYTE CXkeymacsDll::a2v(TCHAR nAscii)\r
1553 {\r
1554         switch (nAscii) {\r
1555         case _T(' '):\r
1556                 return VK_SPACE;\r
1557         case _T('!'):\r
1558                 return '1';\r
1559         case _T('"'):\r
1560                 return m_Config.b106Keyboard ? '2' : (BYTE) 0xde;       // VK_OEM_7\r
1561         case _T('#'):\r
1562                 return '3';\r
1563         case _T('$'):\r
1564                 return '4';\r
1565         case _T('%'):\r
1566                 return '5';\r
1567         case _T('&'):\r
1568                 return m_Config.b106Keyboard ? '6' : '7';\r
1569         case _T('\''):\r
1570                 return m_Config.b106Keyboard ? '7' : (BYTE) 0xde;       // VK_OEM_7\r
1571         case _T('('):\r
1572                 return m_Config.b106Keyboard ? '8' : '9';\r
1573         case _T(')'):\r
1574                 return m_Config.b106Keyboard ? '9' : '0';\r
1575         case _T('*'):\r
1576                 return m_Config.b106Keyboard ? (BYTE) 0xba : '8';       // VK_OEM_1\r
1577         case _T('+'):\r
1578                 return 0xbb;    // VK_OEM_PLUS\r
1579         case _T(','):\r
1580                 return 0xbc;    // VK_OEM_COMMA\r
1581         case _T('-'):\r
1582                 return 0xbd;    // VK_OEM_MINUS\r
1583         case _T('.'):\r
1584                 return 0xbe;    // VK_OEM_PERIOD\r
1585         case _T('/'):\r
1586                 return 0xbf;    // VK_OEM_2\r
1587         case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):\r
1588                 return nAscii;\r
1589         case _T(':'):\r
1590                 return 0xba;    // VK_OEM_1\r
1591         case _T(';'):\r
1592                 return m_Config.b106Keyboard ? (BYTE) 0xbb : (BYTE) 0xba;       // VK_OEM_PLUS  VK_OEM_1\r
1593         case _T('<'):\r
1594                 return 0xbc;    // VK_OEM_COMMA\r
1595         case _T('='):\r
1596                 return m_Config.b106Keyboard ? (BYTE) 0xbd : (BYTE) 0xbb;       // VK_OEM_MINUS VK_OEM_PLUS\r
1597         case _T('>'):\r
1598                 return 0xbe;    // VK_OEM_PERIOD\r
1599         case _T('?'):\r
1600                 return 0xbf;    // VK_OEM_2\r
1601         case _T('@'):\r
1602                 return m_Config.b106Keyboard ? (BYTE) 0xc0 : '2';\r
1603         case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'): \r
1604         case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'): \r
1605         case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'): \r
1606                 return nAscii;\r
1607         case _T('['):\r
1608                 return 0xdb;    // VK_OEM_4\r
1609         case _T('\\'):\r
1610                 return 0xdc;    // VK_OEM_5\r
1611         case _T(']'):\r
1612                 return 0xdd;    // VK_OEM_6\r
1613         case _T('^'):\r
1614                 return m_Config.b106Keyboard ? (BYTE) 0xde : '6';       // VK_OEM_7\r
1615         case _T('_'):\r
1616                 return m_Config.b106Keyboard ? (BYTE) 0xe2 : (BYTE) 0xbd;       // VK_OEM_102   VK_OEM_MINUS\r
1617         case _T('`'):\r
1618                 return 0xc0;    // VK_OEM_3\r
1619         case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'): \r
1620         case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'): \r
1621         case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'): \r
1622                 return (BYTE) (nAscii - (_T('a') - _T('A')));\r
1623         case _T('{'):\r
1624                 return 0xdb;    // VK_OEM_4\r
1625         case _T('|'):\r
1626                 return 0xdc;    // VK_OEM_5\r
1627         case _T('}'):\r
1628                 return 0xdd;    // VK_OEM_6\r
1629         case _T('~'):\r
1630                 return m_Config.b106Keyboard ? (BYTE) 0xde : (BYTE) 0xc0;       // VK_OEM_7     VK_OEM_3\r
1631         default:\r
1632                 return 0;\r
1633         }\r
1634 }\r
1635 \r
1636 BOOL CXkeymacsDll::IsMatchWindowText(CString szWindowText)\r
1637 {\r
1638         BOOL bIsMatchWindowText = TRUE;\r
1639 \r
1640         TCHAR szCurrentWindowText[WINDOW_TEXT_LENGTH] = {'\0'};\r
1641         GetWindowText(GetForegroundWindow(), szCurrentWindowText, sizeof(szCurrentWindowText));\r
1642 \r
1643         switch (CUtils::GetWindowTextType(szWindowText)) {\r
1644         case IDS_WINDOW_TEXT_MATCH:                                                             // *foo*\r
1645                 szWindowText.Delete(0);                                                         // Delete first '*'\r
1646                 szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
1647                 bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText);\r
1648                 break;\r
1649         case IDS_WINDOW_TEXT_MATCH_FORWARD:                                             // foo*\r
1650                 szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
1651                 bIsMatchWindowText = 0 == CString(szCurrentWindowText).Find(szWindowText);\r
1652                 break;\r
1653         case IDS_WINDOW_TEXT_MATCH_BACKWARD:                                    // *foo\r
1654                 szWindowText.Delete(0);                                                         // Delete first '*'\r
1655                 bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText, CString(szCurrentWindowText).GetLength() - szWindowText.GetLength());\r
1656                 break;\r
1657         case IDS_WINDOW_TEXT_MATCH_FULL:                                                // foo\r
1658                 bIsMatchWindowText = szWindowText == CString(szCurrentWindowText);\r
1659                 break;\r
1660         case IDS_WINDOW_TEXT_IGNORE:                                                    // *\r
1661                 bIsMatchWindowText = TRUE;\r
1662                 break;\r
1663         default:\r
1664                 ASSERT(0);\r
1665                 break;\r
1666         }\r
1667 \r
1668 //      CUtils::Log(_T("IsMatchWindowText: %d, _%s_, _%s_"), bIsMatchWindowText, szCurrentWindowText, szWindowText);\r
1669         return bIsMatchWindowText;\r
1670 }\r
1671 \r
1672 void CXkeymacsDll::SetAccelerate(int nAccelerate)\r
1673 {\r
1674         m_nAccelerate = nAccelerate;\r
1675 }\r
1676 \r
1677 int CXkeymacsDll::GetAccelerate()\r
1678 {\r
1679         return m_nAccelerate;\r
1680 }\r
1681 \r
1682 void CXkeymacsDll::SetKeyboardSpeed(int nKeyboardSpeed)\r
1683 {\r
1684         m_nKeyboardSpeed = nKeyboardSpeed;\r
1685 }\r
1686 \r
1687 unsigned int CXkeymacsDll::GetMaxKeyInterval()\r
1688 {\r
1689         // m_nKeyboardSpeed == 0:       slowest repeat rate; approximately  2 characters per second\r
1690         // m_nKeyboardSpeed == 31:      fastest repeat rate; approximately 30 characters per second\r
1691         // 47 ms is max on my machine w/ KeyboardSpeed 31.\r
1692         // 1000 /  2 + 50 = 550\r
1693         // 1000 / 30 + 50 = 83\r
1694         return (unsigned int) (1000.0 / (2.0 + m_nKeyboardSpeed % 32 * 28.0 / 31.0) + 50.0);\r
1695 }\r
1696 \r
1697 void CXkeymacsDll::SetCursorData(HCURSOR hEnable, HCURSOR hDisableTMP, HCURSOR hDisableWOCQ, HICON hDisable, BOOL bEnable)\r
1698 {\r
1699         m_hCursor[STATUS_ENABLE] = hEnable;\r
1700         m_hCursor[STATUS_DISABLE_TMP] = hDisableTMP;\r
1701         m_hCursor[STATUS_DISABLE_WOCQ] = hDisableWOCQ;\r
1702         m_hCursor[STATUS_DISABLE] = hDisable;\r
1703         m_bCursor = bEnable;\r
1704 }\r
1705 \r
1706 void CXkeymacsDll::DoSetCursor()\r
1707 {\r
1708         if (m_bCursor && m_hCurrentCursor) {\r
1709                 ::SetCursor(m_hCurrentCursor);\r
1710         }\r
1711 }\r
1712 \r
1713 void CXkeymacsDll::Set326Compatible(int nApplicationID, BOOL b326Compatible)\r
1714 {\r
1715         m_Config.b326Compatible[nApplicationID] = b326Compatible;\r
1716 }\r
1717 \r
1718 BOOL CXkeymacsDll::Get326Compatible()\r
1719 {\r
1720         return m_Config.b326Compatible[m_nApplicationID];\r
1721 }\r
1722 \r
1723 void CXkeymacsDll::InvokeM_x(const TCHAR *const szPath)\r
1724 {\r
1725 //      CUtils::Log("M-x: szPath=_%s_", szPath);\r
1726         int (*fCommand)() = NULL;\r
1727 \r
1728         for (int i = 0; i < MAX_COMMAND; ++i) {\r
1729                 if (_tcsicmp(szPath, Commands[i].szCommandName) == 0) {\r
1730                         fCommand = Commands[i].fCommand;\r
1731                         break;\r
1732                 }\r
1733         }\r
1734 \r
1735         if (fCommand) {\r
1736 //              CUtils::Log("M-x: Command: _%s_", Commands[i].szCommandName);\r
1737                 fCommand();\r
1738         } else {\r
1739 //              CUtils::Log("M-x: Path: _%s_", szPath);\r
1740                 ShellExecute(NULL, NULL, szPath, NULL, NULL, SW_SHOWNORMAL);\r
1741         }\r
1742 }\r
1743 \r
1744 void CXkeymacsDll::SetM_xTip(const TCHAR *const szPath)\r
1745 {\r
1746         _tcscpy_s(m_M_xTip, "M-x LED");\r
1747         if (szPath && _tcslen(szPath) < 128 - 5)\r
1748                 _stprintf_s(m_M_xTip, "M-x %s", szPath);\r
1749 }\r
1750 \r
1751 BOOL CXkeymacsDll::SendIconMessage(ICONMSG *pMsg, DWORD num)\r
1752 {\r
1753         DWORD ack, read;\r
1754         return CallNamedPipe(ICON_PIPE, pMsg, sizeof(ICONMSG) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT);\r
1755 }\r