OSDN Git Service

Modify or remove some declarations of constants in xkeymacsdll.h.
[xkeymacs/xkeymacs.git] / xkeymacsdll / xkeymacsdll.cpp
index aba5e7c..1b74239 100644 (file)
@@ -207,10 +207,6 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 #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
@@ -227,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
@@ -270,18 +270,24 @@ void CXkeymacsDll::SetConfig(const CONFIG& config)
 \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
+       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
 void CXkeymacsDll::SetKeyboardHook()\r
 {\r
-       HHOOK *phHook = reinterpret_cast<HHOOK *>(TlsGetValue(g_TlsIndex));\r
-       if (!phHook)\r
-               return;\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
@@ -353,34 +359,35 @@ BOOL CXkeymacsDll::IsKeyboardHook()
 LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
        SetKeyboardHook();\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
+       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
@@ -393,21 +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
-       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
@@ -420,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
@@ -450,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
@@ -600,9 +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 HHOOK *phHook = reinterpret_cast<HHOOK *>(TlsGetValue(g_TlsIndex));\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
@@ -610,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 (!m_bEnableKeyboardHook || CUtils::IsXkeymacs() ||\r
                        nCode < 0 || nCode == HC_NOREMOVE)\r
-               return CallNextHookEx(*phHook, 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
@@ -713,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
@@ -899,7 +910,7 @@ DO_NOTHING:
                m_Macro.push_back(m);\r
                m_bDown[wParam] |= !bRelease;\r
        }\r
-       return CallNextHookEx(*phHook, nCode, wParam, lParam);\r
+       return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 \r
 RECURSIVE:\r
        Kdu(RECURSIVE_KEY, 1, FALSE);\r
@@ -915,14 +926,13 @@ 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
@@ -1120,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
@@ -1133,7 +1143,7 @@ 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