• R/O
  • HTTP
  • SSH
  • HTTPS

xkeymacs: Commit


Commit MetaInfo

Revisiond735b48c0d0a95080e3c7f303a31b64d350f476c (tree)
Time2012-08-26 15:52:59
AuthorKazuhiro Fujieda <fujieda@user...>
CommiterKazuhiro Fujieda

Log Message

Change the behavior of the reset

The reset restarts the 64bit process. This avoids the reset can't
cause any effect on the 64bit applications.

This commit includes the following changes.
- Make CMainFrame::TerminatePollThread wait until the thread really

terminates.

- Make SendIPC64Message and IsWow64 in CXkeymacsApp static methods.
- Make IsWow64 invoke always IsWow64Process, remove the invocation

from Start64bitProcess, and add Terminate64bitProcess in CXkeymacsApp.

- Delete InitDllData and make SetDllData public in CProfile.
- Remove IPC64_RESET of an IPC message.
- Change CMainFrame::OnReset as above.

Change Summary

Incremental Difference

--- a/xkeymacs/mainfrm.cpp
+++ b/xkeymacs/mainfrm.cpp
@@ -166,6 +166,7 @@ void CMainFrame::TerminatePollThread()
166166 IPC32Message msg;
167167 msg.Type = IPC32_TERMINATE;
168168 CallNamedPipe(XKEYMACS32_PIPE, &msg, sizeof(msg.Type), &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT);
169+ WaitForSingleObject(m_hThread, 5000);
169170 CloseHandle(m_hThread);
170171 }
171172
@@ -224,7 +225,7 @@ DWORD WINAPI CMainFrame::PollMessage(LPVOID)
224225 goto exit;
225226 case IPC32_HOOKSTATE:
226227 CXkeymacsDll::SetHookStateDirect(msg.Enable);
227- static_cast<CXkeymacsApp *>(AfxGetApp())->SendIPC64Message(msg.Enable ? IPC64_ENABLE : IPC64_DISABLE);
228+ CXkeymacsApp::SendIPC64Message(msg.Enable ? IPC64_ENABLE : IPC64_DISABLE);
228229 if (!SendAck(pipe))
229230 goto exit;
230231 continue;
@@ -515,7 +516,7 @@ void CMainFrame::OnQuit()
515516
516517 CXkeymacsDll::ReleaseHooks();
517518 TerminatePollThread();
518- static_cast<CXkeymacsApp *>(AfxGetApp())->SendIPC64Message(IPC64_EXIT);
519+ CXkeymacsApp::Terminate64bitProcess();
519520 DeleteAllShell_NotifyIcon();
520521
521522 PostQuitMessage(0);
@@ -553,7 +554,8 @@ void CMainFrame::PopUpKeyboardDialog(const HKEY_TYPE hkeyType)
553554 void CMainFrame::OnImport()
554555 {
555556 CProfile::ImportProperties();
556- CProfile::InitDllData();
557+ CProfile::LoadData();
558+ CProfile::SetDllData();
557559 }
558560
559561 void CMainFrame::OnExport()
@@ -563,10 +565,12 @@ void CMainFrame::OnExport()
563565
564566 void CMainFrame::OnReset()
565567 {
566- TerminatePollThread();
567568 CXkeymacsDll::ResetHooks();
569+ TerminatePollThread();
568570 StartPollThread();
569- static_cast<CXkeymacsApp *>(AfxGetApp())->SendIPC64Message(IPC64_RESET);
571+ CXkeymacsApp::Terminate64bitProcess();
572+ CXkeymacsApp::Start64bitProcess();
573+ CProfile::SetDllData();
570574 }
571575
572576 void CMainFrame::OnHelpFinder()
--- a/xkeymacs/profile.cpp
+++ b/xkeymacs/profile.cpp
@@ -33,12 +33,6 @@ void CProfile::SaveData()
3333 SetDllData();
3434 }
3535
36-void CProfile::InitDllData()
37-{
38- LoadData();
39- SetDllData();
40-}
41-
4236 void CProfile::DeleteAllRegistryData()
4337 {
4438 HKEY hkey = NULL;
@@ -276,12 +270,11 @@ void CProfile::SetDllData()
276270 }
277271 m_Config.Is106Keyboard = Is106Keyboard();
278272 CXkeymacsDll::SetConfig(m_Config);
279- CXkeymacsApp *pApp = static_cast<CXkeymacsApp *>(AfxGetApp());
280- if (!pApp->IsWow64())
273+ if (!CXkeymacsApp::IsWow64())
281274 return;
282275 if (!CXkeymacsDll::SaveConfig())
283276 return;
284- pApp->SendIPC64Message(IPC64_RELOAD);
277+ CXkeymacsApp::SendIPC64Message(IPC64_RELOAD);
285278 }
286279
287280 void CProfile::SaveKeyBind(LPCTSTR appName, int comID, int type, int key)
--- a/xkeymacs/profile.h
+++ b/xkeymacs/profile.h
@@ -23,7 +23,7 @@ class CProfile
2323 public:
2424 static void LoadData();
2525 static void SaveData();
26- static void InitDllData();
26+ static void SetDllData();
2727 static void StringToKey(LPCTSTR str, int& type, int& key);
2828 static CString KeyToString(int type, int key);
2929 static void GetAppList(TCHAR (&appTitle)[MAX_APP][WINDOW_TEXT_LENGTH], TCHAR (&appName)[MAX_APP][CLASS_NAME_LENGTH]);
@@ -64,7 +64,6 @@ private:
6464 static void AddKeyBind2C_(LPCTSTR szApplicationName, BYTE bVk);
6565 static void LoadRegistry();
6666 static void SaveRegistry();
67- static void SetDllData();
6867 static void SaveKeyBind(LPCTSTR appName, int comID, int type, int key);
6968 static void SaveKeyBind(LPCTSTR appName, LPCTSTR comName, int type, int key);
7069 static BOOL DiableTokenPrivileges();
--- a/xkeymacs/xkeymacs.cpp
+++ b/xkeymacs/xkeymacs.cpp
@@ -4,6 +4,7 @@
44 #include "xkeymacs.h"
55 #include "mainfrm.h"
66 #include "profile.h"
7+#include "../xkeymacsdll/ipc.h"
78 #include "../xkeymacsdll/Utils.h"
89
910 #ifdef _DEBUG
@@ -15,6 +16,8 @@ static char THIS_FILE[] = __FILE__;
1516 /////////////////////////////////////////////////////////////////////////////
1617 // CXkeymacsApp
1718
19+HANDLE CXkeymacsApp::m_64bitProcessHandle = INVALID_HANDLE_VALUE;
20+
1821 BEGIN_MESSAGE_MAP(CXkeymacsApp, CWinApp)
1922 //{{AFX_MSG_MAP(CXkeymacsApp)
2023 // NOTE - the ClassWizard will add and remove mapping macros here.
@@ -28,7 +31,6 @@ END_MESSAGE_MAP()
2831 CXkeymacsApp::CXkeymacsApp()
2932 {
3033 m_hMutex = NULL;
31- m_bIsWow64 = FALSE;
3234 }
3335
3436 /////////////////////////////////////////////////////////////////////////////
@@ -73,54 +75,65 @@ BOOL CXkeymacsApp::InitInstance()
7375 m_pMainWnd->UpdateWindow();
7476 SetClassLongPtr(m_pMainWnd->m_hWnd, GCLP_HICON, (LONG_PTR)LoadIcon(IDR_MAINFRAME));
7577
76- if (!Start64bitProcess())
78+ if (IsWow64() && !Start64bitProcess())
7779 return FALSE;
78- CProfile::InitDllData();
80+ CProfile::LoadData();
81+ CProfile::SetDllData();
7982
8083 return TRUE;
8184 }
8285
83-BOOL CXkeymacsApp::IsWow64()
86+bool CXkeymacsApp::IsWow64()
8487 {
85- return m_bIsWow64;
88+ typedef BOOL (WINAPI *IsWow64ProcessType)(HANDLE, PBOOL);
89+ IsWow64ProcessType proc = reinterpret_cast<IsWow64ProcessType>(GetProcAddress(GetModuleHandle(_T("kernel32")), _T("IsWow64Process")));
90+ if (!proc)
91+ return false; // IsWow64Process not exists
92+ BOOL ret;
93+ if (!proc(GetCurrentProcess(), &ret))
94+ return false; // error
95+ return ret == TRUE;
8696 }
8797
88-BOOL CXkeymacsApp::Start64bitProcess()
98+bool CXkeymacsApp::Start64bitProcess()
8999 {
90- typedef BOOL (WINAPI *PFIsWow64Process)(HANDLE, PBOOL);
91- PFIsWow64Process func = (PFIsWow64Process)GetProcAddress(GetModuleHandle(_T("kernel32")), _T("IsWow64Process"));
92- if (!func)
93- return TRUE; // IsWow64Process not exists
94- if (!func(GetCurrentProcess(), &m_bIsWow64))
95- return FALSE; // error
96- if (!m_bIsWow64)
97- return TRUE; // do nothing
98-
99100 TCHAR buf[MAX_PATH];
100101 if (!GetModuleFileName(NULL, buf, MAX_PATH))
101- return FALSE;
102+ return false;
102103 CString path = buf;
103104 if (!path.Replace(_T("xkeymacs.exe"), _T("xkeymacs64.exe")))
104- return FALSE;
105+ return false;
105106 STARTUPINFO si;
106107 ZeroMemory(&si, sizeof(si));
107108 si.cb = sizeof(si);
108109 PROCESS_INFORMATION pi;
109110 ZeroMemory(&pi, sizeof(pi));
110111 if (!CreateProcess(path, NULL, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
111- return FALSE;
112+ return false;
113+ m_64bitProcessHandle = pi.hProcess;
112114 // wait until the child process starts.
113- if (WaitForInputIdle(pi.hProcess, INFINITE))
114- return FALSE;
115+ if (WaitForInputIdle(m_64bitProcessHandle, INFINITE))
116+ return false;
115117 // close unused handles
116- CloseHandle(pi.hProcess);
117118 CloseHandle(pi.hThread);
118- return TRUE;
119+ return true;
120+}
121+
122+void CXkeymacsApp::Terminate64bitProcess()
123+{
124+ SendIPC64Message(IPC64_EXIT);
125+ if (WaitForSingleObject(m_64bitProcessHandle, 5000) != WAIT_OBJECT_0) {
126+ TerminateProcess(m_64bitProcessHandle, 0);
127+ WaitForSingleObject(m_64bitProcessHandle, INFINITE);
128+ }
129+ CloseHandle(m_64bitProcessHandle);
130+ m_64bitProcessHandle = INVALID_HANDLE_VALUE;
131+ return;
119132 }
120133
121134 void CXkeymacsApp::SendIPC64Message(DWORD msg)
122135 {
123- if (!m_bIsWow64)
136+ if (!IsWow64())
124137 return;
125138 DWORD ack, read;
126139 for (int i = 0; i < 10; Sleep(100), ++i)
--- a/xkeymacs/xkeymacs.h
+++ b/xkeymacs/xkeymacs.h
@@ -37,13 +37,14 @@ public:
3737 // NOTE - the ClassWizard will add and remove member functions here.
3838 // DO NOT EDIT what you see in these blocks of generated code !
3939 //}}AFX_MSG
40- BOOL IsWow64();
41- void SendIPC64Message(DWORD msg);
42- BOOL Start64bitProcess();
40+ static bool IsWow64();
41+ static bool Start64bitProcess();
42+ static void Terminate64bitProcess();
43+ static void SendIPC64Message(DWORD msg);
4344 DECLARE_MESSAGE_MAP()
4445 private:
4546 HANDLE m_hMutex;
46- BOOL m_bIsWow64;
47+ static HANDLE m_64bitProcessHandle;
4748 };
4849
4950 //{{AFX_INSERT_LOCATION}}
--- a/xkeymacs/xkeymacs64.cpp
+++ b/xkeymacs/xkeymacs64.cpp
@@ -94,9 +94,6 @@ UINT PollIPCMessage(LPVOID param)
9494 case IPC64_RELOAD:
9595 CXkeymacsDll::LoadConfig();
9696 break;
97- case IPC64_RESET:
98- CXkeymacsDll::ResetHooks();
99- break;
10097 case IPC64_DISABLE:
10198 CXkeymacsDll::SetHookStateDirect(false);
10299 break;
--- a/xkeymacsdll/ipc.h
+++ b/xkeymacsdll/ipc.h
@@ -28,7 +28,7 @@ struct Config {
2828 };
2929
3030 enum XKEYMACS_IPC32 { IPC32_TERMINATE, IPC32_ICON, IPC32_HOOKSTATE };
31-enum XKEYMACS_IPC64 { IPC64_EXIT, IPC64_RELOAD, IPC64_RESET, IPC64_DISABLE, IPC64_ENABLE };
31+enum XKEYMACS_IPC64 { IPC64_EXIT, IPC64_RELOAD, IPC64_DISABLE, IPC64_ENABLE };
3232
3333 struct IconState {
3434 ICON_TYPE Type;
Show on old repository browser