コミットメタ情報

リビジョン00805d5e4ce4167751725f51e83fd97bd9bbcd77 (tree)
日時2014-09-11 22:22:29
作者Kazuhiro Fujieda <fujieda@user...>
コミッターKazuhiro Fujieda

ログメッセージ

Add a new class to handle the TLS stuff

変更サマリ

差分

--- /dev/null
+++ b/xkeymacsdll/TLS.cpp
@@ -0,0 +1,54 @@
1+#include "StdAfx.h"
2+#include "TLS.h"
3+
4+DWORD TLS::m_TlsIndex = TLS_OUT_OF_INDEXES;
5+
6+bool TLS::Alloc()
7+{
8+ return (m_TlsIndex = TlsAlloc()) != TLS_OUT_OF_INDEXES;
9+}
10+
11+void TLS::Free()
12+{
13+ FreeLocal();
14+ if (m_TlsIndex != TLS_OUT_OF_INDEXES)
15+ TlsFree(m_TlsIndex);
16+ m_TlsIndex = TLS_OUT_OF_INDEXES;
17+}
18+
19+TLS *TLS::AllocLocal()
20+{
21+ if (m_TlsIndex == TLS_OUT_OF_INDEXES)
22+ return nullptr;
23+ LPVOID data = TlsGetValue(m_TlsIndex);
24+ if (!data) {
25+ data = LocalAlloc(LPTR, sizeof(TLS));
26+ if (data)
27+ TlsSetValue(m_TlsIndex, data);
28+ }
29+ return reinterpret_cast<TLS *>(data);
30+}
31+
32+void TLS::FreeLocal()
33+{
34+ if (m_TlsIndex == TLS_OUT_OF_INDEXES)
35+ return;
36+ LPVOID data = TlsGetValue(m_TlsIndex);
37+ if (!data)
38+ return;
39+ LocalFree(data);
40+ TlsSetValue(m_TlsIndex, nullptr);
41+}
42+
43+HHOOK TLS::GetKeyboardHook()
44+{
45+ TLS *tls = AllocLocal();
46+ return tls ? tls->m_KeyboardHook : nullptr;
47+}
48+
49+void TLS::PutKeyboardHook(HHOOK hook)
50+{
51+ TLS *tls = AllocLocal();
52+ if (tls)
53+ tls->m_KeyboardHook = hook;
54+}
\ No newline at end of file
--- /dev/null
+++ b/xkeymacsdll/TLS.h
@@ -0,0 +1,17 @@
1+#pragma once
2+class TLS
3+{
4+public:
5+ static bool Alloc();
6+ static void Free();
7+ static void FreeLocal();
8+ static HHOOK GetKeyboardHook();
9+ static void PutKeyboardHook(HHOOK hook);
10+
11+private:
12+ static DWORD m_TlsIndex;
13+ static TLS *m_LocalData;
14+ HHOOK m_KeyboardHook;
15+ static TLS *AllocLocal();
16+};
17+
--- a/xkeymacsdll/xkeymacsdll-vc10.vcxproj
+++ b/xkeymacsdll/xkeymacsdll-vc10.vcxproj
@@ -215,6 +215,7 @@
215215 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
216216 <PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
217217 </ClCompile>
218+ <ClCompile Include="TLS.cpp" />
218219 <ClCompile Include="Utils.cpp" />
219220 <ClCompile Include="xkeymacsdll.cpp" />
220221 </ItemGroup>
@@ -234,6 +235,7 @@
234235 <ClInclude Include="KbdMacro.h" />
235236 <ClInclude Include="resource.h" />
236237 <ClInclude Include="StdAfx.h" />
238+ <ClInclude Include="TLS.h" />
237239 <ClInclude Include="tstring.h" />
238240 <ClInclude Include="Utils.h" />
239241 <ClInclude Include="xkeymacsdll.h" />
--- a/xkeymacsdll/xkeymacsdll-vc10.vcxproj.filters
+++ b/xkeymacsdll/xkeymacsdll-vc10.vcxproj.filters
@@ -48,6 +48,9 @@
4848 <ClCompile Include="PipeName.cpp">
4949 <Filter>Source Files</Filter>
5050 </ClCompile>
51+ <ClCompile Include="TLS.cpp">
52+ <Filter>Source Files</Filter>
53+ </ClCompile>
5154 </ItemGroup>
5255 <ItemGroup>
5356 <None Include="xkeymacsdll.def">
@@ -100,6 +103,9 @@
100103 <ClInclude Include="PipeName.h">
101104 <Filter>Header Files</Filter>
102105 </ClInclude>
106+ <ClInclude Include="TLS.h">
107+ <Filter>Header Files</Filter>
108+ </ClInclude>
103109 </ItemGroup>
104110 <ItemGroup>
105111 <ResourceCompile Include="xkeymacsdll.rc">
--- a/xkeymacsdll/xkeymacsdll.cpp
+++ b/xkeymacsdll/xkeymacsdll.cpp
@@ -5,6 +5,7 @@
55 #include "Utils.h"
66 #include "Commands.h"
77 #include "CmdTable.h"
8+#include "TLS.h"
89 #include "../xkeymacs/resource.h"
910 #include <math.h>
1011 #include <Imm.h>
@@ -19,13 +20,11 @@ static char THIS_FILE[] = __FILE__;
1920 static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL };
2021
2122 static HINSTANCE g_hDllInst = NULL;
22-static DWORD g_TlsIndex = 0;
2323
2424 extern "C" int APIENTRY
2525 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
2626 {
2727 g_hDllInst = hInstance;
28- LPVOID lpData;
2928
3029 // Remove this if you use lpReserved
3130 UNREFERENCED_PARAMETER(lpReserved);
@@ -58,27 +57,20 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
5857 e->Delete();
5958 // CUtils::Log("DllMain: 'new' threw an exception");
6059 }
61-
62- if ((g_TlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)
60+ if (!TLS::Alloc())
6361 return FALSE;
64- // fall through
6562 case DLL_THREAD_ATTACH:
66- if ((lpData = LocalAlloc(LPTR, sizeof(HHOOK))) != NULL)
67- TlsSetValue(g_TlsIndex, lpData);
6863 break;
6964 case DLL_PROCESS_DETACH:
7065 TRACE0("XKEYMACSDLL.DLL Terminating!\n");
7166 // Terminate the library before destructors are called
7267 AfxTermExtensionModule(XkeymacsdllDLL);
7368 CXkeymacsDll::ReleaseKeyboardHook();
74- if ((lpData = TlsGetValue(g_TlsIndex)) != NULL)
75- LocalFree(lpData);
76- TlsFree(g_TlsIndex);
69+ TLS::Free();
7770 break;
7871 case DLL_THREAD_DETACH:
7972 CXkeymacsDll::ReleaseKeyboardHook();
80- if ((lpData = TlsGetValue(g_TlsIndex)) != NULL)
81- LocalFree(lpData);
73+ TLS::FreeLocal();
8274 break;
8375 }
8476 return 1; // ok
@@ -164,17 +156,8 @@ void CXkeymacsDll::SetHooks()
164156
165157 void CXkeymacsDll::SetKeyboardHook(DWORD threadId)
166158 {
167- LPVOID lpData = TlsGetValue(g_TlsIndex);
168- if (!lpData) {
169- lpData = LocalAlloc(LPTR, sizeof(HHOOK));
170- if (!lpData)
171- return;
172- if (!TlsSetValue(g_TlsIndex, lpData))
173- return;
174- }
175- HHOOK *phHook = reinterpret_cast<HHOOK *>(lpData);
176- if (!*phHook)
177- *phHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hDllInst, threadId ? threadId : GetCurrentThreadId());
159+ if (!TLS::GetKeyboardHook())
160+ TLS::PutKeyboardHook(SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hDllInst, threadId ? threadId : GetCurrentThreadId()));
178161 }
179162
180163 inline void unhook(HHOOK &hh)
@@ -201,9 +184,10 @@ void CXkeymacsDll::ReleaseHooks()
201184
202185 void CXkeymacsDll::ReleaseKeyboardHook()
203186 {
204- HHOOK *phHook = reinterpret_cast<HHOOK *>(TlsGetValue(g_TlsIndex));
205- if (phHook)
206- unhook(*phHook);
187+ HHOOK hook = TLS::GetKeyboardHook();
188+ if (!hook)
189+ return;
190+ UnhookWindowsHookEx(hook);
207191 }
208192
209193 void CXkeymacsDll::SetHookStateDirect(bool enable)
旧リポジトリブラウザで表示