コミットメタ情報

リビジョンb4bded9b58307b6db4d095034412198939dbbe4f (tree)
日時2011-05-27 08:35:43
作者Kazuhiro Fujieda <fujieda@user...>
コミッターKazuhiro Fujieda

ログメッセージ

Make the keyboard hook thread local.

Set and release the hook when DLL ataches and detaches to threads or
processes. Disable the keyboard hook when other hooks are released.

変更サマリ

差分

--- a/xkeymacsdll/xkeymacsdll.cpp
+++ b/xkeymacsdll/xkeymacsdll.cpp
@@ -18,7 +18,8 @@ static char THIS_FILE[] = __FILE__;
1818
1919 static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL };
2020
21-static HINSTANCE g_hDllInst = NULL;
21+static __declspec(thread) HINSTANCE g_hDllInst = NULL;
22+static __declspec(thread) HHOOK g_hHookKeyboard = NULL;
2223
2324 extern "C" int APIENTRY
2425 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
@@ -28,7 +29,8 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
2829 // Remove this if you use lpReserved
2930 UNREFERENCED_PARAMETER(lpReserved);
3031
31- if (dwReason == DLL_PROCESS_ATTACH) {
32+ switch (dwReason) {
33+ case DLL_PROCESS_ATTACH:
3234 TRACE0("XKEYMACSDLL.DLL Initializing!\n");
3335
3436 // Extension DLL one-time initialization
@@ -55,10 +57,15 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
5557 e->Delete();
5658 // CUtils::Log("DllMain: 'new' threw an exception");
5759 }
58- } else if (dwReason == DLL_PROCESS_DETACH) {
60+ case DLL_THREAD_ATTACH:
61+ CXkeymacsDll::SetKeyboardHook();
62+ break;
63+ case DLL_PROCESS_DETACH:
5964 TRACE0("XKEYMACSDLL.DLL Terminating!\n");
6065 // Terminate the library before destructors are called
6166 AfxTermExtensionModule(XkeymacsdllDLL);
67+ case DLL_THREAD_DETACH:
68+ CXkeymacsDll::ReleaseKeyboardHook();
6269 }
6370 return 1; // ok
6471 }
@@ -69,7 +76,7 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
6976
7077 #include "xkeymacsDll.h"
7178 #pragma data_seg(".xkmcs")
72- HHOOK CXkeymacsDll::m_hHookKeyboard = NULL;
79+ BOOL CXkeymacsDll::m_bEnableKeyboardHook = FALSE;
7380 HHOOK CXkeymacsDll::m_hHookCallWnd = NULL;
7481 HHOOK CXkeymacsDll::m_hHookCallWndRet = NULL;
7582 HHOOK CXkeymacsDll::m_hHookGetMessage = NULL;
@@ -206,19 +213,23 @@ void CXkeymacsDll::DeleteShell_NotifyIcon(ICON_TYPE icon)
206213 // set hooks
207214 void CXkeymacsDll::SetHooks()
208215 {
209- m_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, 0);
216+ m_bEnableKeyboardHook = TRUE;
210217 m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, g_hDllInst, 0);
211218 m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, g_hDllInst, 0);
212219 m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hDllInst, 0);
213220 m_hHookShell = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDllInst, 0);
214221 }
215222
223+void CXkeymacsDll::SetKeyboardHook()
224+{
225+ if (!g_hHookKeyboard)
226+ SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, GetCurrentThreadId());
227+}
228+
216229 // release hooks
217230 void CXkeymacsDll::ReleaseHooks()
218231 {
219- if (m_hHookKeyboard)
220- UnhookWindowsHookEx(m_hHookKeyboard);
221- m_hHookKeyboard = NULL;
232+ m_bEnableKeyboardHook = FALSE;
222233 if (m_hHookCallWnd)
223234 UnhookWindowsHookEx(m_hHookCallWnd);
224235 m_hHookCallWnd = NULL;
@@ -233,6 +244,13 @@ void CXkeymacsDll::ReleaseHooks()
233244 m_hHookShell = NULL;
234245 }
235246
247+void CXkeymacsDll::ReleaseKeyboardHook()
248+{
249+ if (g_hHookKeyboard)
250+ UnhookWindowsHookEx(g_hHookKeyboard);
251+ g_hHookKeyboard = NULL;
252+}
253+
236254 void CXkeymacsDll::SetKeyboardHookFlag(BOOL bFlag)
237255 {
238256 m_bHook = bFlag;
@@ -679,6 +697,9 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
679697
680698 // CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %#x"), nCode, nKey, lParam);
681699
700+ if (!m_bEnableKeyboardHook)
701+ return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);
702+
682703 if (nCode < 0 || nCode == HC_NOREMOVE) {
683704 goto DO_NOTHING;
684705 }
@@ -1129,7 +1150,7 @@ DO_NOTHING:
11291150 bDefiningMacro = m_bDefiningMacro;
11301151 }
11311152
1132- return CallNextHookEx(m_hHookKeyboard, nCode, wParam, lParam);
1153+ return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);
11331154
11341155 RECURSIVE:
11351156 Kdu(RECURSIVE_KEY, 1, FALSE);
--- a/xkeymacsdll/xkeymacsdll.h
+++ b/xkeymacsdll/xkeymacsdll.h
@@ -194,10 +194,12 @@ public:
194194 void SetAtIbeamCursorOnly(int nApplicationID, int nCommandType, int nKey, BOOL bAtIbeamCursorOnly);
195195 void SetApplicationName(int nApplicationID, CString szApplicationName);
196196 static void ReleaseHooks();
197+ static void ReleaseKeyboardHook();
197198 void SetEnableCUA(int nApplicationID, BOOL bEnableCUA);
198199 void SetIgnoreUndefinedC_x(int nApplicationID, BOOL bIgnoreUndefinedC_x);
199200 void SetIgnoreUndefinedMetaCtrl(int nApplicationID, BOOL bIgnoreUndefinedMetaCtrl);
200201 static void SetHooks();
202+ static void SetKeyboardHook();
201203 void SetSettingStyle(int nApplicationID, int nSettingStyle);
202204 void SetUseDialogSetting(int nApplicationID, BOOL bUseDialogSetting);
203205 static void AddKillRing(BOOL bNewData = TRUE);
@@ -232,7 +234,7 @@ private:
232234 static LRESULT CALLBACK CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam);
233235 static BOOL IsMatchWindowText(CString szWindowText);
234236 static TCHAR m_szWindowText[MAX_APP][WINDOW_TEXT_LENGTH];
235- static HHOOK m_hHookKeyboard;
237+ static BOOL m_bEnableKeyboardHook;
236238 static HHOOK m_hHookCallWnd;
237239 static HHOOK m_hHookGetMessage;
238240 static HHOOK m_hHookShell;
旧リポジトリブラウザで表示