OSDN Git Service

Modify or remove some declarations of constants in xkeymacsdll.h.
[xkeymacs/xkeymacs.git] / xkeymacsdll / xkeymacsdll.cpp
index c3ada2f..1b74239 100644 (file)
@@ -133,51 +133,14 @@ static const int MAX_KEYNAME = _countof(KeyNames);
 \r
 static AFX_EXTENSION_MODULE XkeymacsdllDLL = { NULL, NULL };\r
 \r
-HINSTANCE g_hDllInst = NULL;\r
-UINT g_ImeManipulationMessage = 0;\r
-#pragma data_seg(".xkmcs")\r
-HHOOK g_hHookKeyboard = NULL;\r
-HHOOK g_hHookDummy = NULL;\r
-#pragma data_seg()\r
-\r
-inline bool IsWow64(HANDLE mod)\r
-{\r
-       typedef BOOL (WINAPI *pfnIsWow64Process_t)(HANDLE, PBOOL);\r
-       if (const pfnIsWow64Process_t IsWow64Process = (pfnIsWow64Process_t)GetProcAddress(GetModuleHandle(_T("kernel32")), "IsWow64Process")) {\r
-               BOOL b;\r
-               return IsWow64Process(mod, &b) && b;\r
-       }\r
-       return false;\r
-}\r
-\r
-inline bool Is64System()\r
-{\r
-       SYSTEM_INFO info;\r
-       GetNativeSystemInfo(&info);\r
-       return info.wProcessorArchitecture != PROCESSOR_ARCHITECTURE_INTEL;\r
-}\r
-\r
-inline bool Is64Process(HANDLE mod)\r
-{\r
-       return Is64System() && !IsWow64(mod);\r
-}\r
-\r
-const bool IsDll64 = sizeof(void *) == 8;\r
-\r
-inline bool Is64ProcessHwnd(HWND hwnd)\r
-{\r
-       DWORD pid;\r
-       GetWindowThreadProcessId(hwnd, &pid);\r
-       HANDLE hmod = OpenProcess(PROCESS_QUERY_INFORMATION, 0, pid);\r
-       bool b = Is64Process(hmod);\r
-       CloseHandle(hmod);\r
-       return b;\r
-}\r
+static HINSTANCE g_hDllInst = NULL;\r
+static DWORD g_TlsIndex = NULL;\r
 \r
 extern "C" int APIENTRY\r
 DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)\r
 {\r
        g_hDllInst = hInstance;\r
+       LPVOID lpData;\r
        \r
        // Remove this if you use lpReserved\r
        UNREFERENCED_PARAMETER(lpReserved);\r
@@ -185,7 +148,6 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
        switch (dwReason) {\r
        case DLL_PROCESS_ATTACH:\r
                TRACE0("XKEYMACSDLL.DLL Initializing!\n");\r
-               g_ImeManipulationMessage = RegisterWindowMessage(_T("XkManipulateIME"));\r
 \r
                // Extension DLL one-time initialization\r
                if (!AfxInitExtensionModule(XkeymacsdllDLL, hInstance)) {\r
@@ -211,11 +173,27 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
                        e->Delete();\r
 //                     CUtils::Log("DllMain: 'new' threw an exception");\r
                }\r
+\r
+               if ((g_TlsIndex = TlsAlloc()) == TLS_OUT_OF_INDEXES)\r
+                       return FALSE;\r
+               // fall through\r
+       case DLL_THREAD_ATTACH:\r
+               if ((lpData = LocalAlloc(LPTR, sizeof(HHOOK))) != NULL)\r
+                       TlsSetValue(g_TlsIndex, lpData);\r
                break;\r
        case DLL_PROCESS_DETACH:\r
                TRACE0("XKEYMACSDLL.DLL Terminating!\n");\r
                // Terminate the library before destructors are called\r
                AfxTermExtensionModule(XkeymacsdllDLL);\r
+               CXkeymacsDll::ReleaseKeyboardHook();\r
+               if ((lpData = TlsGetValue(g_TlsIndex)) != NULL)\r
+                       LocalFree(lpData);\r
+               TlsFree(g_TlsIndex);\r
+               break;\r
+       case DLL_THREAD_DETACH:\r
+               CXkeymacsDll::ReleaseKeyboardHook();\r
+               if ((lpData = TlsGetValue(g_TlsIndex)) != NULL)\r
+                       LocalFree(lpData);\r
                break;\r
        }\r
        return 1;   // ok\r
@@ -227,19 +205,16 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 \r
 #include "xkeymacsDll.h"\r
 #pragma data_seg(".xkmcs")\r
+       bool    CXkeymacsDll::m_bEnableKeyboardHook = false;\r
        DWORD   CXkeymacsDll::m_nHookAltRelease = 0;\r
-       HHOOK   CXkeymacsDll::m_hHookCallWnd = NULL;\r
-       HHOOK   CXkeymacsDll::m_hHookCallWndRet = NULL;\r
-       HHOOK   CXkeymacsDll::m_hHookGetMessage = NULL;\r
-       HHOOK   CXkeymacsDll::m_hHookShell = NULL;\r
        BOOL    CXkeymacsDll::m_bRightControl   = FALSE;\r
        BOOL    CXkeymacsDll::m_bRightAlt               = FALSE;\r
        BOOL    CXkeymacsDll::m_bRightShift             = FALSE;\r
        BOOL    CXkeymacsDll::m_bHook                   = TRUE;\r
        CList<CClipboardSnap *, CClipboardSnap *> CXkeymacsDll::m_oKillRing;\r
        int             CXkeymacsDll::m_nKillRing = 0;\r
-       int             CXkeymacsDll::m_nOriginal[MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};\r
-       int             CXkeymacsDll::m_nApplicationID = 0;\r
+       BYTE    CXkeymacsDll::m_nOriginal[MAX_COMMAND_TYPE][MAX_KEY] = {'\0'};\r
+       int             CXkeymacsDll::m_nAppID = 0;\r
        int             CXkeymacsDll::m_nAccelerate = 0;\r
        int             CXkeymacsDll::m_nKeyboardSpeed = 31;\r
        HCURSOR CXkeymacsDll::m_hCursor[MAX_STATUS] = {'\0'};\r
@@ -248,6 +223,10 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
        TCHAR   CXkeymacsDll::m_M_xTip[128] = "";\r
        CONFIG  CXkeymacsDll::m_Config = {0};\r
 #pragma data_seg()\r
+HHOOK CXkeymacsDll::m_hHookCallWnd = NULL;\r
+HHOOK CXkeymacsDll::m_hHookCallWndRet = NULL;\r
+HHOOK CXkeymacsDll::m_hHookGetMessage = NULL;\r
+HHOOK CXkeymacsDll::m_hHookShell = NULL;\r
 BOOL CXkeymacsDll::m_bRecordingMacro = FALSE;\r
 BOOL CXkeymacsDll::m_bDown[MAX_KEY] = {0};\r
 std::list<KbdMacro> CXkeymacsDll::m_Macro;\r
@@ -284,22 +263,37 @@ BOOL CXkeymacsDll::LoadConfig()
        return res;\r
 }\r
 \r
-// set hooks\r
-LRESULT WINAPI DummyProc(int code, WPARAM wp, LPARAM lp) {\r
-       return CallNextHookEx(0, code, wp, lp);\r
+void CXkeymacsDll::SetConfig(const CONFIG& config)\r
+{\r
+       m_Config = config;\r
 }\r
 \r
 void CXkeymacsDll::SetHooks()\r
 {\r
-       m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, (HOOKPROC)CallWndProc, g_hDllInst, 0);\r
-       m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, (HOOKPROC)CallWndRetProc, g_hDllInst, 0);\r
-       m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)GetMsgProc, g_hDllInst, 0);\r
-       m_hHookShell = SetWindowsHookEx(WH_SHELL, (HOOKPROC)ShellProc, g_hDllInst, 0);\r
-       g_hHookDummy = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)DummyProc, g_hDllInst, 0);\r
-       g_hHookKeyboard = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, 0);\r
+       m_hHookCallWnd = SetWindowsHookEx(WH_CALLWNDPROC, CallWndProc, g_hDllInst, 0);\r
+       m_hHookCallWndRet = SetWindowsHookEx(WH_CALLWNDPROCRET, CallWndRetProc, g_hDllInst, 0);\r
+       m_hHookGetMessage = SetWindowsHookEx(WH_GETMESSAGE, GetMsgProc, g_hDllInst, 0);\r
+       m_hHookShell = SetWindowsHookEx(WH_SHELL, ShellProc, g_hDllInst, 0);\r
+       m_bEnableKeyboardHook = true;\r
 }\r
 \r
-inline void unhook(HHOOK &hh) {\r
+void CXkeymacsDll::SetKeyboardHook()\r
+{\r
+       LPVOID lpData = TlsGetValue(g_TlsIndex);\r
+       if (!lpData) {\r
+               lpData = LocalAlloc(LPTR, sizeof(HHOOK));\r
+               if (!lpData)\r
+                       return;\r
+               if (!TlsSetValue(g_TlsIndex, lpData))\r
+                       return;\r
+       }\r
+       HHOOK *phHook = reinterpret_cast<HHOOK *>(lpData);\r
+       if (!*phHook)\r
+               *phHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, GetCurrentThreadId());\r
+}\r
+\r
+inline void unhook(HHOOK &hh)\r
+{\r
        if (hh)\r
                UnhookWindowsHookEx(hh);\r
        hh = NULL;\r
@@ -311,15 +305,20 @@ void CXkeymacsDll::ResetHooks()
        SetHooks();\r
 }\r
 \r
-// release hooks\r
 void CXkeymacsDll::ReleaseHooks()\r
 {\r
        unhook(m_hHookCallWnd);\r
        unhook(m_hHookCallWndRet);\r
        unhook(m_hHookGetMessage);\r
        unhook(m_hHookShell);\r
-       unhook(g_hHookKeyboard);\r
-       unhook(g_hHookDummy);\r
+       m_bEnableKeyboardHook = false;\r
+}\r
+\r
+void CXkeymacsDll::ReleaseKeyboardHook()\r
+{\r
+       HHOOK *phHook = reinterpret_cast<HHOOK *>(TlsGetValue(g_TlsIndex));\r
+       if (phHook)\r
+               unhook(*phHook);\r
 }\r
 \r
 void CXkeymacsDll::ToggleKeyboardHookState()\r
@@ -342,8 +341,8 @@ void CXkeymacsDll::ShowKeyboardHookState()
        } else {\r
                msg.nState = STATUS_DISABLE_WOCQ;\r
        }\r
-       if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE\r
-        || (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default"))\r
+       if (m_Config.nSettingStyle[m_nAppID] == SETTING_DISABLE\r
+        || (!_tcsicmp(m_Config.szSpecialApp[m_nAppID], _T("Default"))\r
          && CUtils::IsDefaultIgnoreApplication())) {\r
                msg.nState = STATUS_DISABLE;\r
                m_hCurrentCursor = m_hCursor[STATUS_DISABLE];\r
@@ -359,34 +358,36 @@ BOOL CXkeymacsDll::IsKeyboardHook()
 \r
 LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
-       if (nCode < 0)\r
-               return CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);\r
-       const CWPSTRUCT *cwps = reinterpret_cast<CWPSTRUCT *>(lParam);\r
-       switch (cwps->message) {\r
-       case WM_IME_STARTCOMPOSITION:\r
-               InitKeyboardProc(TRUE);\r
-               break;\r
-       case WM_IME_ENDCOMPOSITION:\r
-               InitKeyboardProc(FALSE);\r
-               break;\r
-       case WM_SETFOCUS:\r
-               if (cwps->hwnd == GetForegroundWindow()) {\r
-                       InitKeyboardProc(FALSE);\r
-                       ShowKeyboardHookState();\r
-               }\r
-               break;\r
-       case WM_NCACTIVATE:\r
-               if (cwps->wParam && cwps->hwnd == GetForegroundWindow()) {\r
+       SetKeyboardHook();\r
+       if (nCode >= 0) {\r
+               const CWPSTRUCT *cwps = reinterpret_cast<CWPSTRUCT *>(lParam);\r
+               switch (cwps->message) {\r
+               case WM_IME_STARTCOMPOSITION:\r
+                       InitKeyboardProc(TRUE);\r
+                       break;\r
+               case WM_IME_ENDCOMPOSITION:\r
                        InitKeyboardProc(FALSE);\r
-                       ShowKeyboardHookState();\r
+                       break;\r
+               case WM_SETFOCUS:\r
+                       if (cwps->hwnd == GetForegroundWindow()) {\r
+                               InitKeyboardProc(FALSE);\r
+                               ShowKeyboardHookState();\r
+                       }\r
+                       break;\r
+               case WM_NCACTIVATE:\r
+                       if (cwps->wParam && cwps->hwnd == GetForegroundWindow()) {\r
+                               InitKeyboardProc(FALSE);\r
+                               ShowKeyboardHookState();\r
+                       }\r
+                       break;\r
                }\r
-               break;\r
        }\r
-       return CallNextHookEx(m_hHookCallWnd, nCode, wParam, lParam);\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 }\r
 \r
 LRESULT CALLBACK CXkeymacsDll::CallWndRetProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
+       SetKeyboardHook();\r
        if (nCode >= 0) {\r
                const CWPRETSTRUCT *cwprets = reinterpret_cast<CWPRETSTRUCT *>(lParam);\r
                switch (cwprets->message) {\r
@@ -399,26 +400,24 @@ LRESULT CALLBACK CXkeymacsDll::CallWndRetProc(int nCode, WPARAM wParam, LPARAM l
                        break;\r
                }\r
        }\r
-       return CallNextHookEx(m_hHookCallWndRet, nCode, wParam, lParam);\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 }\r
 \r
 LRESULT CALLBACK CXkeymacsDll::GetMsgProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
-       const MSG *msg = reinterpret_cast<MSG *>(lParam);\r
-       if (msg->message == g_ImeManipulationMessage) {\r
-               if (wParam)\r
-                       CCommands::DoSetInputMethodOpenStatus(static_cast<INPUT_METHOD_OPEN_STATUS>(msg->wParam), static_cast<BOOL>(msg->lParam));\r
-               return 1;\r
-       }\r
-       switch (msg->message) {\r
-       case WM_IME_STARTCOMPOSITION:\r
-               InitKeyboardProc(TRUE);\r
-               break;\r
-       case WM_IME_ENDCOMPOSITION:\r
-               InitKeyboardProc(FALSE);\r
-               break;\r
+       SetKeyboardHook();\r
+       if (nCode >= 0) {\r
+               const MSG *msg = reinterpret_cast<MSG *>(lParam);\r
+               switch (msg->message) {\r
+               case WM_IME_STARTCOMPOSITION:\r
+                       InitKeyboardProc(TRUE);\r
+                       break;\r
+               case WM_IME_ENDCOMPOSITION:\r
+                       InitKeyboardProc(FALSE);\r
+                       break;\r
+               }\r
        }\r
-       return CallNextHookEx(m_hHookGetMessage, nCode, wParam, lParam);\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 }\r
 \r
 LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam)\r
@@ -431,7 +430,7 @@ LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam
                        ShowKeyboardHookState();\r
                }\r
        }\r
-       return CallNextHookEx( m_hHookShell, nCode, wParam, lParam );\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 }\r
 \r
 UINT CXkeymacsDll::GetModifierState(BOOL bPhysicalKey)\r
@@ -461,7 +460,9 @@ void CXkeymacsDll::SetModifierState(const UINT after, const UINT before)
                UpdateKeyboardState(VK_CONTROL, 0);\r
        }\r
 \r
-       const BOOL bHookApp = CUtils::IsVisualCpp() || CUtils::IsFirefox() || CUtils::IsVisualStudio() || CUtils::IsInternetExplorer();\r
+       const BOOL bHookApp =\r
+               CUtils::IsVisualCpp() ||  CUtils::IsVisualStudio() ||\r
+               CUtils::IsInternetExplorer() || CUtils::IsFirefox() || CUtils::IsChrome();\r
        if (after & META && !(before & META)) {\r
                if (bHookApp)\r
                        m_nHookAltRelease |= HOOK_ALT_LATER;\r
@@ -564,30 +565,30 @@ void CXkeymacsDll::Kdu(BYTE bVk, DWORD n, BOOL bOriginal)
 void CXkeymacsDll::InitKeyboardProc(BOOL bImeComposition)\r
 {\r
        CUtils::SetApplicationName(bImeComposition);\r
-       if (_tcsnicmp(m_Config.szSpecialApp[m_nApplicationID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[m_nApplicationID])) {     // PROCESSENTRY32 has only 0xF bytes of Name\r
-               m_nApplicationID = -1;\r
+       if (_tcsnicmp(m_Config.szSpecialApp[m_nAppID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[m_nAppID])) {     // PROCESSENTRY32 has only 0xF bytes of Name\r
+               m_nAppID = -1;\r
                for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {\r
                        if (_tcsnicmp(m_Config.szSpecialApp[nAppID], CUtils::GetApplicationName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[nAppID]))\r
                                continue;\r
-                       if (m_nApplicationID < 0)\r
-                               m_nApplicationID = nAppID;\r
+                       if (m_nAppID < 0)\r
+                               m_nAppID = nAppID;\r
                        else {\r
-                               const LPCSTR curText = m_Config.szWindowText[m_nApplicationID];\r
+                               const LPCSTR curText = m_Config.szWindowText[m_nAppID];\r
                                const LPCSTR newText = m_Config.szWindowText[nAppID];\r
                                const int curType = CUtils::GetWindowTextType(curText);\r
                                const int newType = CUtils::GetWindowTextType(newText);\r
                                if (curType < newType || curType == newType && _tcscmp(curText, newText) <= 0)\r
-                                       m_nApplicationID = nAppID;\r
+                                       m_nAppID = nAppID;\r
                        }\r
                }\r
-               if (m_nApplicationID < 0)\r
-                       m_nApplicationID = GetAppID(_T("Default"), 0);\r
+               if (m_nAppID < 0)\r
+                       m_nAppID = GetAppID(_T("Default"), 0);\r
        }\r
-       if (m_Config.nSettingStyle[m_nApplicationID] != SETTING_DISABLE &&\r
-                       (_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default")) || !CUtils::IsDefaultIgnoreApplication()) &&\r
-                       !bImeComposition && CUtils::IsDialog() && m_Config.bUseDialogSetting[m_nApplicationID])\r
+       if (m_Config.nSettingStyle[m_nAppID] != SETTING_DISABLE &&\r
+                       (_tcsicmp(m_Config.szSpecialApp[m_nAppID], _T("Default")) || !CUtils::IsDefaultIgnoreApplication()) &&\r
+                       !bImeComposition && CUtils::IsDialog() && m_Config.bUseDialogSetting[m_nAppID])\r
                // Use Dialog Setting\r
-               m_nApplicationID = GetAppID(_T("Dialog"), m_nApplicationID);\r
+               m_nAppID = GetAppID(_T("Dialog"), m_nAppID);\r
 \r
        ICONMSG msg[3] = {\r
                {CX_ICON, OFF_ICON, ""},\r
@@ -611,8 +612,8 @@ int CXkeymacsDll::GetAppID(const LPCSTR szName, const int fallback)
 LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
        const BYTE nOrigKey = static_cast<BYTE>(wParam);\r
-       const bool bRelease = (lParam & BEING_RELEASED) != 0;\r
-       const bool bExtended = (lParam & EXTENDED_KEY) != 0;\r
+       const bool bRelease = (HIWORD(lParam) & KF_UP) != 0;\r
+       const bool bExtended = (HIWORD(lParam) & KF_EXTENDED) != 0;\r
 \r
        static BOOL bLocked = FALSE;\r
        static const BYTE RECURSIVE_KEY = 0x07;\r
@@ -620,14 +621,14 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        static BYTE nOneShotModifier[MAX_KEY] = {'\0'};\r
        static BOOL bCherryOneShotModifier = FALSE;\r
 \r
-//     CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %p, %d, %d"), nCode, nOrigKey, lParam, IsDll64, Is64ProcessHwnd(GetForegroundWindow()));\r
+//     CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %#x"), nCode, nOrigKey, lParam);\r
 \r
-       if (Is64ProcessHwnd(GetForegroundWindow()) != IsDll64 || CUtils::IsXkeymacs() ||\r
+       if (!m_bEnableKeyboardHook || CUtils::IsXkeymacs() ||\r
                        nCode < 0 || nCode == HC_NOREMOVE)\r
-               return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);\r
+               return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 \r
 //     CUtils::Log(_T("nKey = %#x, ext = %d, rel = %d, pre = %d, %#hx, %#hx"), nOrigKey,\r
-//             (lParam & EXTENDED_KEY) ? 1 : 0, (lParam & BEING_RELEASED) ? 1 : 0, (lParam & REPEATED_KEY) ? 1 : 0,\r
+//             (HIWORD(lParam) & KF_EXTENDED) ? 1 : 0, (HIWORD(lParam) & KF_UP) ? 1 : 0, (HIWORD(lParam) & KF_REPEAT) ? 1 : 0,\r
 //             GetKeyState(nOrigKey), GetAsyncKeyState(nOrigKey));\r
 \r
        if (nOrigKey == RECURSIVE_KEY) {\r
@@ -652,8 +653,8 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                break;\r
        }\r
 \r
-#define fCommand(type) (Commands[m_Config.nCommandID[m_nApplicationID][(type)][nKey]].fCommand)\r
-#define nFunctionID (m_Config.nFunctionID[m_nApplicationID][nType][nKey])\r
+#define fCommand(type) (Commands[m_Config.nCommandID[m_nAppID][(type)][nKey]].fCommand)\r
+#define nFunctionID (m_Config.nFunctionID[m_nAppID][nType][nKey])\r
 \r
        if (bRelease) {\r
                switch (nOrigKey) {\r
@@ -686,11 +687,11 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                goto DO_NOTHING;\r
        }\r
 \r
-       if (m_Config.nSettingStyle[m_nApplicationID] == SETTING_DISABLE)\r
+       if (m_Config.nSettingStyle[m_nAppID] == SETTING_DISABLE)\r
                goto DO_NOTHING;\r
 \r
        // Do Nothing for Meadow, Mule for Win32, ... if those use default setting.\r
-       if (!_tcsicmp(m_Config.szSpecialApp[m_nApplicationID], _T("Default")) && CUtils::IsDefaultIgnoreApplication())\r
+       if (!_tcsicmp(m_Config.szSpecialApp[m_nAppID], _T("Default")) && CUtils::IsDefaultIgnoreApplication())\r
                goto DO_NOTHING;\r
 \r
        switch (IsPassThrough(nKey)) {\r
@@ -706,7 +707,7 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        int nType = IsDown(VK_SHIFT) * SHIFT | IsControl() * CONTROL | IsMeta() * META | CCommands::bC_x() * CONTROLX;\r
        // Ignore undefined C-x ?\r
        if (nType & CONTROLX && fCommand(nType) == NULL && nFunctionID < 0) {\r
-               if (m_Config.bIgnoreUndefinedC_x[m_nApplicationID]) {\r
+               if (m_Config.bIgnoreUndefinedC_x[m_nAppID]) {\r
                        CCommands::Reset(GOTO_HOOK);\r
                        goto HOOK;\r
                }\r
@@ -714,7 +715,7 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        }\r
        // Ignore undefined Meta Ctrl+?\r
        if (CCommands::bM_() && nType & CONTROL && fCommand(nType) == NULL && nFunctionID < 0) {\r
-               if (m_Config.bIgnoreUndefinedMetaCtrl[m_nApplicationID]) {\r
+               if (m_Config.bIgnoreUndefinedMetaCtrl[m_nAppID]) {\r
                        if (CheckOriginal(CONTROL, nKey))\r
                                goto DO_NOTHING;\r
                        CCommands::Reset(GOTO_HOOK);\r
@@ -723,12 +724,12 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                }\r
        }\r
 \r
-       int nVirtualCommandType = GetModifierState(FALSE);\r
+       int nVirtualType = GetModifierState(FALSE);\r
        if (nOrigKey == VK_CONTROL)\r
-               nVirtualCommandType &= ~CONTROL;\r
+               nVirtualType &= ~CONTROL;\r
        if (nOrigKey == VK_MENU)\r
-               nVirtualCommandType &= ~META;\r
-       if (CheckOriginal(nVirtualCommandType, nOrigKey)) {\r
+               nVirtualType &= ~META;\r
+       if (CheckOriginal(nVirtualType, nOrigKey)) {\r
                goto DO_NOTHING;\r
        }\r
 \r
@@ -909,13 +910,13 @@ DO_NOTHING:
                m_Macro.push_back(m);\r
                m_bDown[wParam] |= !bRelease;\r
        }\r
-       return CallNextHookEx(g_hHookKeyboard, nCode, wParam, lParam);\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 \r
 RECURSIVE:\r
        Kdu(RECURSIVE_KEY, 1, FALSE);\r
        goto HOOKX;\r
 HOOK:\r
-       CCommands::SetLastCommand(fCommand);\r
+       CCommands::SetLastCommand(fLastCommand);\r
 HOOK0_9:\r
 HOOKX:\r
        SetModifierIcons();\r
@@ -925,46 +926,18 @@ HOOK_RECURSIVE_KEY:
 \r
 void CXkeymacsDll::SetModifierIcons()\r
 {\r
-#define IconState(x) ((x) ? ON_ICON : OFF_ICON)\r
        ICONMSG msg[6] = {\r
-               {MX_ICON, IconState(CCommands::bM_x()), ""},\r
-               {CX_ICON, IconState(CCommands::bC_x()), ""},\r
-               {META_ICON, IconState(CCommands::bM_()), ""},\r
-               {SHIFT_ICON, IconState(IsDown(VK_SHIFT, FALSE)), ""},\r
-               {CTRL_ICON, IconState(IsControl()), ""},\r
-               {ALT_ICON, IconState(IsDown(VK_MENU, FALSE)), ""}\r
+               {MX_ICON, CCommands::bM_x(), ""},\r
+               {CX_ICON, CCommands::bC_x(), ""},\r
+               {META_ICON, CCommands::bM_(), ""},\r
+               {SHIFT_ICON, IsDown(VK_SHIFT, FALSE), ""},\r
+               {CTRL_ICON, IsControl(), ""},\r
+               {ALT_ICON, IsDown(VK_MENU, FALSE), ""}\r
        };\r
        _tcscpy_s(msg[0].szTip, m_M_xTip);\r
        SendIconMessage(msg, 6);\r
 }\r
 \r
-void CXkeymacsDll::SetApplicationName(int nAppID, CString szApplicationName)\r
-{\r
-       ZeroMemory(m_Config.szSpecialApp[nAppID], CLASS_NAME_LENGTH);\r
-       _tcsncpy_s(m_Config.szSpecialApp[nAppID], szApplicationName, _TRUNCATE);\r
-}\r
-\r
-void CXkeymacsDll::SetWindowText(int nAppID, CString szWindowText)\r
-{\r
-       ZeroMemory(m_Config.szWindowText[nAppID], WINDOW_TEXT_LENGTH);\r
-       _tcsncpy_s(m_Config.szWindowText[nAppID], szWindowText, _TRUNCATE);\r
-}\r
-\r
-void CXkeymacsDll::SetCommandID(int nAppID, int nType, int nKey, int nComID)\r
-{\r
-       m_Config.nCommandID[nAppID][nType][nKey] = nComID;\r
-}\r
-\r
-void CXkeymacsDll::SetKillRingMax(int nAppID, int nKillRingMax)\r
-{\r
-       m_Config.nKillRingMax[nAppID] = nKillRingMax;\r
-}\r
-\r
-void CXkeymacsDll::SetUseDialogSetting(int nAppID, BOOL bUseDialogSetting)\r
-{\r
-       m_Config.bUseDialogSetting[nAppID] = bUseDialogSetting;\r
-}\r
-\r
 // Clear data of nAppID\r
 void CXkeymacsDll::Clear(int nAppID)\r
 {\r
@@ -1002,7 +975,7 @@ BOOL CXkeymacsDll::IsDepressedModifier(int (__cdecl *Modifier)(void), BOOL bPhys
        BYTE bVk = 0;\r
        do {\r
                if (IsValidKey(bVk) && IsDown(bVk, bPhysicalKey) &&\r
-                               Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == Modifier)\r
+                               Commands[m_Config.nCommandID[m_nAppID][NONE][bVk]].fCommand == Modifier)\r
                        return TRUE;\r
        } while (++bVk);\r
        return FALSE;\r
@@ -1043,7 +1016,7 @@ BOOL CXkeymacsDll::IsMeta()
 \r
 void CXkeymacsDll::AddKillRing(BOOL bNewData)\r
 {\r
-       if (m_Config.nKillRingMax[m_nApplicationID] == 0) {\r
+       if (m_Config.nKillRingMax[m_nAppID] == 0) {\r
                return;\r
        }\r
 \r
@@ -1074,7 +1047,7 @@ void CXkeymacsDll::AddKillRing(BOOL bNewData)
 \r
        m_nKillRing = 0;\r
 \r
-       if (m_Config.nKillRingMax[m_nApplicationID] < m_oKillRing.GetCount()) {\r
+       if (m_Config.nKillRingMax[m_nAppID] < m_oKillRing.GetCount()) {\r
                CClipboardSnap *pSnap = m_oKillRing.GetTail();\r
                delete pSnap;\r
                pSnap = NULL;\r
@@ -1086,7 +1059,7 @@ void CXkeymacsDll::AddKillRing(BOOL bNewData)
 // Return FALSE if there is no more data\r
 CClipboardSnap* CXkeymacsDll::GetKillRing(CClipboardSnap* pSnap, BOOL bForce)\r
 {\r
-       if (m_Config.nKillRingMax[m_nApplicationID] == 0) {\r
+       if (m_Config.nKillRingMax[m_nAppID] == 0) {\r
                return NULL;\r
        }\r
 \r
@@ -1138,29 +1111,9 @@ void CXkeymacsDll::IncreaseKillRingIndex(int nKillRing)
        m_nKillRing += nKillRing;\r
 }\r
 \r
-void CXkeymacsDll::SetSettingStyle(int nAppID, int nSettingStyle)\r
-{\r
-       m_Config.nSettingStyle[nAppID] = nSettingStyle;\r
-}\r
-\r
-void CXkeymacsDll::SetIgnoreUndefinedMetaCtrl(int nAppID, BOOL bIgnoreUndefinedMetaCtrl)\r
-{\r
-       m_Config.bIgnoreUndefinedMetaCtrl[nAppID] = bIgnoreUndefinedMetaCtrl;\r
-}\r
-\r
-void CXkeymacsDll::SetIgnoreUndefinedC_x(int nAppID, BOOL bIgnoreUndefinedC_x)\r
-{\r
-       m_Config.bIgnoreUndefinedC_x[nAppID] = bIgnoreUndefinedC_x;\r
-}\r
-\r
-void CXkeymacsDll::SetEnableCUA(int nAppID, BOOL bEnableCUA)\r
-{\r
-       m_Config.bEnableCUA[nAppID] = bEnableCUA;\r
-}\r
-\r
 BOOL CXkeymacsDll::GetEnableCUA()\r
 {\r
-       return m_Config.bEnableCUA[m_nApplicationID];\r
+       return m_Config.bEnableCUA[m_nAppID];\r
 }\r
 \r
 void CXkeymacsDll::StartRecordMacro()\r
@@ -1177,7 +1130,7 @@ void CXkeymacsDll::EndRecordMacro()
        m_bRecordingMacro = FALSE;\r
        while (!m_Macro.empty()) { // remove not released push\r
                const KbdMacro& m = m_Macro.back();\r
-               if (m.lParam & BEING_RELEASED)\r
+               if (HIWORD(m.lParam) & KF_UP)\r
                        break;\r
                m_Macro.pop_back();\r
        }\r
@@ -1190,18 +1143,13 @@ void CXkeymacsDll::CallMacro()
        UINT before = GetModifierState(FALSE);\r
        SetModifierState(0, before);\r
        for (std::list<KbdMacro>::const_iterator m = m_Macro.begin(); m != m_Macro.end(); ++m)\r
-               if (m->lParam & BEING_RELEASED)\r
+               if (HIWORD(m->lParam) & KF_UP)\r
                        ReleaseKey(static_cast<BYTE>(m->wParam));\r
                else\r
                        DepressKey(static_cast<BYTE>(m->wParam), m->bOriginal);\r
        SetModifierState(before, 0);\r
 }\r
 \r
-void CXkeymacsDll::Set106Keyboard(BOOL b106Keyboard)\r
-{\r
-       m_Config.b106Keyboard = b106Keyboard;\r
-}\r
-\r
 BOOL CXkeymacsDll::Is106Keyboard()\r
 {\r
        return m_Config.b106Keyboard;\r
@@ -1212,7 +1160,7 @@ int CXkeymacsDll::IsPassThrough(BYTE nKey)
        BYTE bVk = 0;\r
        do {\r
                if (IsDown(bVk)\r
-                && (Commands[m_Config.nCommandID[m_nApplicationID][NONE][bVk]].fCommand == CCommands::PassThrough)) {\r
+                && (Commands[m_Config.nCommandID[m_nAppID][NONE][bVk]].fCommand == CCommands::PassThrough)) {\r
                        if (bVk == nKey) {\r
                                return GOTO_HOOK;\r
                        }\r
@@ -1223,32 +1171,6 @@ int CXkeymacsDll::IsPassThrough(BYTE nKey)
        return CONTINUE;\r
 }\r
 \r
-void CXkeymacsDll::SetFunctionKey(int nFuncID, int nAppID, int nType, int nKey)\r
-{\r
-       if (nAppID      < 0 || MAX_APP                  <= nAppID\r
-        || nType       < 0 || MAX_COMMAND_TYPE <= nType\r
-        || nKey                        < 0 || MAX_KEY                  <= nKey) {\r
-               return;\r
-       }\r
-\r
-       m_Config.nFunctionID[nAppID][nType][nKey] = nFuncID;\r
-}\r
-\r
-void CXkeymacsDll::ClearFunctionDefinition()\r
-{\r
-       memset(m_Config.nFunctionID, -1, sizeof(m_Config.nFunctionID));\r
-       memset(m_Config.szFunctionDefinition, 0, sizeof(m_Config.szFunctionDefinition));\r
-}\r
-\r
-void CXkeymacsDll::SetFunctionDefinition(int nFuncID, CString szDefinition)\r
-{\r
-       if (nFuncID < 0 || nFuncID >= MAX_FUNCTION)\r
-               return;\r
-       memset(m_Config.szFunctionDefinition[nFuncID], 0, sizeof(m_Config.szFunctionDefinition[nFuncID]));\r
-       _tcscpy_s(m_Config.szFunctionDefinition[nFuncID], szDefinition);\r
-       return;\r
-}\r
-\r
 // call an original command which is defined in dot.xkeymacs\r
 void CXkeymacsDll::CallFunction(int nFuncID)\r
 {\r
@@ -1292,7 +1214,7 @@ void CXkeymacsDll::CallFunction(int nFuncID)
        for (std::vector<KeyBind>::const_iterator p = keybinds.begin(); p != keybinds.end(); ++p) {\r
                const int nType = p->nType;\r
                const BYTE bVk = p->bVk;\r
-               int (*fCommand)() = nType < MAX_COMMAND_TYPE ? Commands[m_Config.nCommandID[m_nApplicationID][nType][bVk]].fCommand : NULL;\r
+               int (*fCommand)() = nType < MAX_COMMAND_TYPE ? Commands[m_Config.nCommandID[m_nAppID][nType][bVk]].fCommand : NULL;\r
                if (fCommand) {\r
                        if (fCommand == CCommands::ExecuteExtendedCommand)\r
                                bM_x = TRUE;\r
@@ -1300,7 +1222,7 @@ void CXkeymacsDll::CallFunction(int nFuncID)
                                SetModifierState(0, before);\r
                                bInitialized = TRUE;\r
                        }\r
-//                     CUtils::Log("CallFunction: Command Name: %s", Commands[m_Config.nCommandID[m_nApplicationID][nType][bVk]].szCommandName);\r
+//                     CUtils::Log("CallFunction: Command Name: %s", Commands[m_Config.nCommandID[m_nAppID][nType][bVk]].szCommandName);\r
                        while (fCommand() == GOTO_RECURSIVE)\r
                                ;\r
                        continue;\r
@@ -1606,14 +1528,9 @@ void CXkeymacsDll::DoSetCursor()
        }\r
 }\r
 \r
-void CXkeymacsDll::Set326Compatible(int nAppID, BOOL b326Compatible)\r
-{\r
-       m_Config.b326Compatible[nAppID] = b326Compatible;\r
-}\r
-\r
 BOOL CXkeymacsDll::Get326Compatible()\r
 {\r
-       return m_Config.b326Compatible[m_nApplicationID];\r
+       return m_Config.b326Compatible[m_nAppID];\r
 }\r
 \r
 void CXkeymacsDll::InvokeM_x(const TCHAR *const szPath)\r