OSDN Git Service

Stop setting an invalid value to the keyboard state to push the control key.
[xkeymacs/xkeymacs.git] / xkeymacsdll / xkeymacsdll.cpp
index 17e9b79..fecf659 100644 (file)
@@ -4,6 +4,7 @@
 #include "xkeymacsdll.h"\r
 #include "Utils.h"\r
 #include "Commands.h"\r
+#include "CmdTable.h"\r
 #include "../xkeymacs/resource.h"\r
 #include <math.h>\r
 #include <Imm.h>\r
@@ -33,7 +34,10 @@ static const Modifier Modifiers[] = {
 };\r
 static const int MAX_MODIFIER = _countof(Modifiers);\r
 \r
-static const KeyName KeyNames[] = {\r
+static const struct {\r
+       BYTE bVk;\r
+       LPCTSTR name;\r
+} KeyNames[] = {\r
 //     { VK_LBUTTON,           _T("mouse-1") },                                // does not work well\r
 //     { VK_RBUTTON,           _T("mouse-3") },                                // does not work well\r
        { VK_CANCEL,            _T("break") },\r
@@ -203,7 +207,7 @@ DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
 //////////////////////////////////////////////////////////////////////\r
 \r
 #pragma data_seg(".xkmcs")\r
-CONFIG CXkeymacsDll::m_Config = {0};\r
+Config CXkeymacsDll::m_Config = {0};\r
 bool CXkeymacsDll::m_bEnableKeyboardHook = false;\r
 BOOL CXkeymacsDll::m_bHook = TRUE;\r
 DWORD CXkeymacsDll::m_nHookAltRelease = 0;\r
@@ -219,16 +223,14 @@ BOOL CXkeymacsDll::m_bCursor = FALSE;
 HCURSOR CXkeymacsDll::m_hCursor[MAX_STATUS] = {'\0'};\r
 #pragma data_seg()\r
 \r
+AppConfig* CXkeymacsDll::m_CurrentConfig = NULL;\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
-int CXkeymacsDll::m_nAppID = 0;\r
 CList<CClipboardSnap *, CClipboardSnap *> CXkeymacsDll::m_oKillRing;\r
 int CXkeymacsDll::m_nKillRing = 0;\r
-BOOL CXkeymacsDll::m_bRecordingMacro = FALSE;\r
-BOOL CXkeymacsDll::m_bDown[MAX_KEY] = {0};\r
-std::list<KbdMacro> CXkeymacsDll::m_Macro;\r
+KbdMacro* CXkeymacsDll::m_kbdMacro = NULL;\r
 \r
 BOOL CXkeymacsDll::SaveConfig()\r
 {\r
@@ -262,7 +264,7 @@ BOOL CXkeymacsDll::LoadConfig()
        return res;\r
 }\r
 \r
-void CXkeymacsDll::SetConfig(const CONFIG& config)\r
+void CXkeymacsDll::SetConfig(const Config& config)\r
 {\r
        m_Config = config;\r
 }\r
@@ -276,7 +278,7 @@ void CXkeymacsDll::SetHooks()
        m_bEnableKeyboardHook = true;\r
 }\r
 \r
-void CXkeymacsDll::SetKeyboardHook()\r
+void CXkeymacsDll::SetKeyboardHook(DWORD threadId)\r
 {\r
        LPVOID lpData = TlsGetValue(g_TlsIndex);\r
        if (!lpData) {\r
@@ -288,7 +290,7 @@ void CXkeymacsDll::SetKeyboardHook()
        }\r
        HHOOK *phHook = reinterpret_cast<HHOOK *>(lpData);\r
        if (!*phHook)\r
-               *phHook = SetWindowsHookEx(WH_KEYBOARD, (HOOKPROC)KeyboardProc, g_hDllInst, GetCurrentThreadId());\r
+               *phHook = SetWindowsHookEx(WH_KEYBOARD, KeyboardProc, g_hDllInst, threadId ? threadId : GetCurrentThreadId());\r
 }\r
 \r
 inline void unhook(HHOOK &hh)\r
@@ -333,7 +335,7 @@ BOOL CXkeymacsDll::IsKeyboardHook()
 \r
 void CXkeymacsDll::ShowKeyboardHookState()\r
 {\r
-       ICONMSG msg = {MAIN_ICON,};\r
+       IconMsg msg = {MAIN_ICON,};\r
        if (m_bHook) {\r
                if (CCommands::IsTemporarilyDisableXKeymacs()) {\r
                        msg.nState = STATUS_DISABLE_TMP;\r
@@ -345,8 +347,8 @@ void CXkeymacsDll::ShowKeyboardHookState()
        } else {\r
                msg.nState = STATUS_DISABLE_WOCQ;\r
        }\r
-       if (m_Config.nSettingStyle[m_nAppID] == SETTING_DISABLE\r
-        || (!_tcsicmp(m_Config.szSpecialApp[m_nAppID], _T("Default"))\r
+       if (m_CurrentConfig->SettingStyle == SETTING_DISABLE\r
+        || (!_tcsicmp(m_CurrentConfig->AppName, _T("Default"))\r
          && CUtils::IsDefaultIgnoreApplication())) {\r
                msg.nState = STATUS_DISABLE;\r
                m_hCurrentCursor = m_hCursor[STATUS_DISABLE];\r
@@ -362,20 +364,20 @@ LRESULT CALLBACK CXkeymacsDll::CallWndProc(int nCode, WPARAM wParam, LPARAM lPar
                const CWPSTRUCT *cwps = reinterpret_cast<CWPSTRUCT *>(lParam);\r
                switch (cwps->message) {\r
                case WM_IME_STARTCOMPOSITION:\r
-                       InitKeyboardProc(TRUE);\r
+                       InitKeyboardProc(true);\r
                        break;\r
                case WM_IME_ENDCOMPOSITION:\r
-                       InitKeyboardProc(FALSE);\r
+                       InitKeyboardProc(false);\r
                        break;\r
                case WM_SETFOCUS:\r
                        if (cwps->hwnd == GetForegroundWindow()) {\r
-                               InitKeyboardProc(FALSE);\r
+                               InitKeyboardProc(false);\r
                                ShowKeyboardHookState();\r
                        }\r
                        break;\r
                case WM_NCACTIVATE:\r
                        if (cwps->wParam && cwps->hwnd == GetForegroundWindow()) {\r
-                               InitKeyboardProc(FALSE);\r
+                               InitKeyboardProc(false);\r
                                ShowKeyboardHookState();\r
                        }\r
                        break;\r
@@ -392,7 +394,7 @@ LRESULT CALLBACK CXkeymacsDll::CallWndRetProc(int nCode, WPARAM wParam, LPARAM l
                switch (cwprets->message) {\r
                case WM_SETTEXT:\r
                        if (cwprets->hwnd == GetForegroundWindow())\r
-                               InitKeyboardProc(FALSE);\r
+                               InitKeyboardProc(false);\r
                        break;\r
                case WM_SETCURSOR:\r
                        DoSetCursor();\r
@@ -409,10 +411,10 @@ LRESULT CALLBACK CXkeymacsDll::GetMsgProc(int nCode, WPARAM wParam, LPARAM lPara
                const MSG *msg = reinterpret_cast<MSG *>(lParam);\r
                switch (msg->message) {\r
                case WM_IME_STARTCOMPOSITION:\r
-                       InitKeyboardProc(TRUE);\r
+                       InitKeyboardProc(true);\r
                        break;\r
                case WM_IME_ENDCOMPOSITION:\r
-                       InitKeyboardProc(FALSE);\r
+                       InitKeyboardProc(false);\r
                        break;\r
                }\r
        }\r
@@ -422,45 +424,51 @@ LRESULT CALLBACK CXkeymacsDll::GetMsgProc(int nCode, WPARAM wParam, LPARAM lPara
 LRESULT CALLBACK CXkeymacsDll::ShellProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
        if (nCode == HSHELL_WINDOWACTIVATED) {\r
-               TCHAR className[256];\r
-               GetClassName((HWND)wParam, className, 255);\r
+               SetKeyboardHook(GetWindowThreadProcessId(reinterpret_cast<HWND>(wParam), NULL));\r
+               TCHAR className[CLASS_NAME_LENGTH];\r
+               GetClassName(reinterpret_cast<HWND>(wParam), className, CLASS_NAME_LENGTH);\r
                if (!_tcsicmp(className, _T("ConsoleWindowClass"))) {\r
-                       InitKeyboardProc(FALSE);\r
+                       InitKeyboardProc(false);\r
                        ShowKeyboardHookState();\r
                }\r
        }\r
        return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 }\r
 \r
-void CXkeymacsDll::InitKeyboardProc(BOOL bImeComposition)\r
+void CXkeymacsDll::InitKeyboardProc(bool imeState)\r
 {\r
-       CUtils::SetApplicationName(bImeComposition);\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
+       AppName::Init();\r
+       AppName::SetIMEState(imeState);\r
+\r
+       if (m_CurrentConfig == NULL ||\r
+                       _tcsnicmp(m_CurrentConfig->AppName, AppName::GetAppName(), 0xF) ||      // PROCESSENTRY32 has only 0xF bytes of Name\r
+                       !CUtils::IsMatchWindowText(m_CurrentConfig->WindowText)) {\r
+               m_CurrentConfig = NULL;\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
+                       AppConfig* appConfig = m_Config.AppConfig + nAppID;\r
+                       if (_tcsnicmp(appConfig->AppName, AppName::GetAppName(), 0xF) || !CUtils::IsMatchWindowText(appConfig->WindowText))\r
                                continue;\r
-                       if (m_nAppID < 0)\r
-                               m_nAppID = nAppID;\r
+                       if (m_CurrentConfig == NULL)\r
+                               m_CurrentConfig = appConfig;\r
                        else {\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
+                               LPCTSTR curText = m_CurrentConfig->WindowText;\r
+                               LPCTSTR newText = appConfig->WindowText;\r
+                               int curType = CUtils::GetWindowTextType(curText);\r
+                               int newType = CUtils::GetWindowTextType(newText);\r
                                if (curType < newType || curType == newType && _tcscmp(curText, newText) <= 0)\r
-                                       m_nAppID = nAppID;\r
+                                       m_CurrentConfig = appConfig;\r
                        }\r
                }\r
-               if (m_nAppID < 0)\r
-                       m_nAppID = GetAppID(_T("Default"), 0);\r
+               if (m_CurrentConfig == NULL)\r
+                       m_CurrentConfig = GetAppConfig(_T("Default"), m_Config.AppConfig);\r
        }\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
+       if (m_CurrentConfig->SettingStyle != SETTING_DISABLE &&\r
+                       (_tcsicmp(m_CurrentConfig->AppName, _T("Default")) || !CUtils::IsDefaultIgnoreApplication()) &&\r
+                       !imeState && CUtils::IsDialog() && m_CurrentConfig->UseDialogSetting)\r
                // Use Dialog Setting\r
-               m_nAppID = GetAppID(_T("Dialog"), m_nAppID);\r
+               m_CurrentConfig = GetAppConfig(_T("Dialog"), m_CurrentConfig);\r
 \r
-       ICONMSG msg[3] = {\r
+       IconMsg msg[3] = {\r
                {CX_ICON, OFF_ICON, ""},\r
                {MX_ICON, OFF_ICON, ""},\r
                {META_ICON, OFF_ICON, ""}\r
@@ -471,19 +479,20 @@ void CXkeymacsDll::InitKeyboardProc(BOOL bImeComposition)
        CCommands::Reset();\r
 }\r
 \r
-int CXkeymacsDll::GetAppID(LPCSTR szName, int fallback)\r
+AppConfig* CXkeymacsDll::GetAppConfig(LPCTSTR name, AppConfig* fallback)\r
 {\r
        for (int i = 0; i < MAX_APP; ++i)\r
-               if (!_tcsicmp(m_Config.szSpecialApp[i], szName))\r
-                       return i;\r
+               if (!_tcsicmp(m_Config.AppConfig[i].AppName, name))\r
+                       return m_Config.AppConfig + i;\r
        return fallback;\r
 }\r
 \r
 LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)\r
 {\r
-       const BYTE nOrigKey = static_cast<BYTE>(wParam);\r
-       const bool bRelease = (HIWORD(lParam) & KF_UP) != 0;\r
-       const bool bExtended = (HIWORD(lParam) & KF_EXTENDED) != 0;\r
+       BYTE nOrigKey = static_cast<BYTE>(wParam);\r
+       bool bRelease = (HIWORD(lParam) & KF_UP) != 0;\r
+       bool bExtended = (HIWORD(lParam) & KF_EXTENDED) != 0;\r
+       BYTE nKey = nOrigKey;\r
 \r
        static BOOL bLocked = FALSE;\r
        static const BYTE RECURSIVE_KEY = 0x07;\r
@@ -493,7 +502,7 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
 \r
 //     CUtils::Log(_T("nCode = %#x, nKey = %#x, lParam = %#x"), nCode, nOrigKey, lParam);\r
 \r
-       if (!m_bEnableKeyboardHook || CUtils::IsXkeymacs() ||\r
+       if (!m_bEnableKeyboardHook || m_CurrentConfig == NULL || CUtils::IsXkeymacs() ||\r
                        nCode < 0 || nCode == HC_NOREMOVE)\r
                return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 \r
@@ -510,7 +519,6 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
 \r
        CancelMarkWithShift(nOrigKey, bRelease);\r
 \r
-       BYTE nKey = nOrigKey;\r
        switch (nKey) {\r
        case VK_CONTROL:\r
                nKey = bExtended ? VK_RCONTROL : VK_LCONTROL;\r
@@ -523,8 +531,8 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                break;\r
        }\r
 \r
-#define fCommand(type) (Commands[m_Config.nCommandID[m_nAppID][(type)][nKey]].fCommand)\r
-#define nFunctionID (m_Config.nFunctionID[m_nAppID][nType][nKey])\r
+#define fCommand(nType) (CmdTable::Command(m_CurrentConfig->CmdID[(nType)][nKey]))\r
+#define FuncID (m_CurrentConfig->FuncID[nType][nKey])\r
 \r
        if (bRelease) {\r
                switch (nOrigKey) {\r
@@ -541,7 +549,7 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                case VK_RWIN:\r
                case VK_APPS:\r
                        for (int i = 0; i < MAX_COMMAND_TYPE; ++i) {\r
-                               int (*const fCommand)() = fCommand(i);\r
+                               int (*fCommand)() = fCommand(i);\r
                                if (fCommand && !(nOrigKey == VK_MENU && fCommand == CCommands::MetaAlt))\r
                                        goto HOOK;\r
                        }\r
@@ -557,11 +565,11 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                goto DO_NOTHING;\r
        }\r
 \r
-       if (m_Config.nSettingStyle[m_nAppID] == SETTING_DISABLE)\r
+       if (m_CurrentConfig->SettingStyle == 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_nAppID], _T("Default")) && CUtils::IsDefaultIgnoreApplication())\r
+       if (!_tcsicmp(m_CurrentConfig->AppName, _T("Default")) && CUtils::IsDefaultIgnoreApplication())\r
                goto DO_NOTHING;\r
 \r
        switch (IsPassThrough(nKey)) {\r
@@ -576,8 +584,8 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        // set command type\r
        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_nAppID]) {\r
+       if (nType & CONTROLX && fCommand(nType) == NULL && FuncID < 0) {\r
+               if (m_CurrentConfig->IgnoreUndefC_x) {\r
                        CCommands::Reset(GOTO_HOOK);\r
                        goto HOOK;\r
                }\r
@@ -585,8 +593,8 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        }\r
        // Ignore undefined Meta Ctrl+?\r
        if (CCommands::bM_() && nType & CONTROL) {\r
-               if (fCommand(nType) == NULL && nFunctionID < 0) {\r
-                       if (m_Config.bIgnoreUndefinedMetaCtrl[m_nAppID]) {\r
+               if (fCommand(nType) == NULL && FuncID < 0) {\r
+                       if (m_CurrentConfig->IgnoreUndefMetaCtrl) {\r
                                if (CheckOriginal(CONTROL, nKey))\r
                                        goto DO_NOTHING;\r
                                CCommands::Reset(GOTO_HOOK);\r
@@ -604,7 +612,7 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        if (CheckOriginal(nVirtualType, nOrigKey))\r
                goto DO_NOTHING;\r
 \r
-       int (*const fCommand)() = fCommand(nType);\r
+       int (*fCommand)() = fCommand(nType);\r
        if (fCommand == CCommands::EnableOrDisableXKeymacs) {\r
                ToggleKeyboardHookState();\r
                goto HOOK;\r
@@ -664,19 +672,15 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
                        memset(szPath, 0, sizeof(szPath));\r
                        goto HOOK;\r
                } else if (nKey && index < MAX_PATH - 1) {\r
-                       const BOOL bIsShiftDown = IsDown(VK_SHIFT, FALSE);\r
-                       TCHAR nAscii = 0;\r
-                       do { // 1-127\r
-                               if (a2v(++nAscii) == nKey && bIsShiftDown == IsShift(nAscii)) {\r
-//                                     CUtils::Log("M-x: %#X (%c), %#X (%c)", nKey, nKey, nAscii, nAscii);\r
-                                       if (index < _tcslen(szPath))\r
-                                               memmove(szPath + index + 1, szPath + index, MAX_PATH - index - 1);\r
-                                       szPath[index++] = nAscii;\r
-//                                     CUtils::Log("M-x: %c(%#04x)", nAscii, nAscii);\r
-                                       SetM_xTip(szPath);\r
-                                       goto HOOKX;\r
-                               }\r
-                       } while (nAscii != 127);\r
+                       if (SHORT ascii = ConvVkey(nKey | (static_cast<BYTE>(IsDown(VK_SHIFT, FALSE)) << 8), 1)) {\r
+//                             CUtils::Log("M-x: %#X (%c), %#X (%c)", nKey, nKey, ascii, ascii);\r
+                               if (index < _tcslen(szPath))\r
+                                       memmove(szPath + index + 1, szPath + index, MAX_PATH - index - 1);\r
+                               szPath[index++] = static_cast<TCHAR>(ascii);\r
+//                             CUtils::Log("M-x: %c(%#04x)", ascii, ascii);\r
+                               SetM_xTip(szPath);\r
+                               goto HOOKX;\r
+                       }\r
                }\r
        }\r
 \r
@@ -715,13 +719,13 @@ LRESULT CALLBACK CXkeymacsDll::KeyboardProc(int nCode, WPARAM wParam, LPARAM lPa
        if (i == MAX_KEY)\r
                bCherryOneShotModifier = FALSE;\r
 \r
-       if (0 <= nFunctionID && nFunctionID < MAX_FUNCTION && m_Config.szFunctionDefinition[nFunctionID][0]) {\r
-               CallFunction(nFunctionID);\r
+       if (0 <= FuncID && FuncID < MAX_FUNCTION && m_Config.FuncDef[FuncID][0]) {\r
+               CallFunction(FuncID);\r
                CCommands::Reset(GOTO_HOOK);\r
                goto HOOK;\r
        }\r
 #undef fCommand\r
-#undef nFunctionID\r
+#undef FuncID\r
 \r
        if (!fCommand) {\r
                if (nOrigKey == VK_CONTROL || nOrigKey == VK_MENU || nOrigKey == VK_SHIFT)\r
@@ -776,11 +780,8 @@ RECURSIVE_COMMAND:
 \r
 DO_NOTHING:\r
        SetModifierIcons();\r
-       if (m_bRecordingMacro && (!bRelease || m_bDown[wParam])) {\r
-               KbdMacro m = { nCode, wParam, lParam, TRUE };\r
-               m_Macro.push_back(m);\r
-               m_bDown[wParam] |= !bRelease;\r
-       }\r
+       if (m_kbdMacro)\r
+               m_kbdMacro->Record(nKey, bRelease);\r
        return CallNextHookEx(NULL, nCode, wParam, lParam);\r
 \r
 RECURSIVE:\r
@@ -821,9 +822,9 @@ exit:
 int CXkeymacsDll::IsPassThrough(BYTE nKey)\r
 {\r
        BYTE bVk = 0;\r
-       const BYTE *pnID = m_Config.nCommandID[m_nAppID][NONE]; \r
+       const BYTE *pnID = m_CurrentConfig->CmdID[NONE]; \r
        do {\r
-               if (IsDown(bVk) && Commands[pnID[bVk]].fCommand == CCommands::PassThrough) {\r
+               if (IsDown(bVk) && CmdTable::Command(pnID[bVk]) == CCommands::PassThrough) {\r
                        if (bVk == nKey)\r
                                return GOTO_HOOK;\r
                        return GOTO_DO_NOTHING;\r
@@ -837,8 +838,8 @@ void CXkeymacsDll::InvokeM_x(LPCTSTR szPath)
 //     CUtils::Log("M-x: szPath=_%s_", szPath);\r
        int (*fCommand)() = NULL;\r
        for (int i = 0; i < MAX_COMMAND; ++i)\r
-               if (_tcsicmp(szPath, Commands[i].szCommandName) == 0) {\r
-                       fCommand = Commands[i].fCommand;\r
+               if (_tcsicmp(szPath, CmdTable::Name(i)) == 0) {\r
+                       fCommand = CmdTable::Command(i);\r
                        break;\r
                }\r
        if (fCommand) {\r
@@ -852,7 +853,7 @@ void CXkeymacsDll::InvokeM_x(LPCTSTR szPath)
 \r
 void CXkeymacsDll::SetModifierIcons()\r
 {\r
-       ICONMSG msg[6] = {\r
+       IconMsg msg[6] = {\r
                {MX_ICON, CCommands::bM_x(), ""},\r
                {CX_ICON, CCommands::bC_x(), ""},\r
                {META_ICON, CCommands::bM_(), ""},\r
@@ -871,10 +872,10 @@ void CXkeymacsDll::SetM_xTip(LPCTSTR szPath)
                _stprintf_s(m_M_xTip, "M-x %s", szPath);\r
 }\r
 \r
-BOOL CXkeymacsDll::SendIconMessage(ICONMSG *pMsg, DWORD num)\r
+BOOL CXkeymacsDll::SendIconMessage(IconMsg *pMsg, DWORD num)\r
 {\r
        DWORD ack, read;\r
-       return CallNamedPipe(ICON_PIPE, pMsg, sizeof(ICONMSG) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT) && read == sizeof(DWORD);\r
+       return CallNamedPipe(ICON_PIPE, pMsg, sizeof(IconMsg) * num, &ack, sizeof(DWORD), &read, NMPWAIT_NOWAIT) && read == sizeof(DWORD);\r
 }\r
 \r
 void CXkeymacsDll::Kdu(BYTE bVk, DWORD n, BOOL bOriginal)\r
@@ -974,15 +975,14 @@ void CXkeymacsDll::SetModifierState(UINT after, UINT before)
        else if (!(after & SHIFT) && before & SHIFT)\r
                ReleaseKey(VK_SHIFT);\r
 \r
-       if (after & CONTROL && !(before & CONTROL)) {\r
-               UpdateKeyboardState(VK_CONTROL, 1);\r
+       if (after & CONTROL && !(before & CONTROL))\r
                DepressKey(VK_CONTROL);\r
-       else if (!(after & CONTROL) && before & CONTROL) {\r
+       else if (!(after & CONTROL) && before & CONTROL) {\r
                ReleaseKey(VK_CONTROL);\r
                UpdateKeyboardState(VK_CONTROL, 0);\r
        }\r
 \r
-       const BOOL bHookApp =\r
+       BOOL bHookApp =\r
                CUtils::IsVisualCpp() ||  CUtils::IsVisualStudio() ||\r
                CUtils::IsInternetExplorer() || CUtils::IsFirefox() || CUtils::IsChrome();\r
        if (after & META && !(before & META)) {\r
@@ -1018,7 +1018,7 @@ BOOL CXkeymacsDll::IsMeta()
 BOOL CXkeymacsDll::IsDepressedModifier(int (__cdecl *Modifier)(void), BOOL bPhysicalKey)\r
 {\r
        BYTE bVk = 0;\r
-       const BYTE *pnID = m_Config.nCommandID[m_nAppID][NONE];\r
+       const BYTE *pnID = m_CurrentConfig->CmdID[NONE];\r
        do {\r
                switch (bVk) {\r
                case VK_SHIFT:\r
@@ -1027,7 +1027,7 @@ BOOL CXkeymacsDll::IsDepressedModifier(int (__cdecl *Modifier)(void), BOOL bPhys
                case 0xf0: // Eisu key. GetAsyncKeyState returns the wrong state of Eisu key.\r
                        continue;\r
                }\r
-               if (IsDown(bVk, bPhysicalKey) && Commands[pnID[bVk]].fCommand == Modifier)\r
+               if (IsDown(bVk, bPhysicalKey) && CmdTable::Command(pnID[bVk]) == Modifier)\r
                        return TRUE;\r
        } while (++bVk);\r
        return FALSE;\r
@@ -1040,7 +1040,7 @@ BOOL CXkeymacsDll::IsDown(BYTE bVk, BOOL bPhysicalKey)
 \r
 void CXkeymacsDll::AddKillRing(BOOL bNewData)\r
 {\r
-       if (m_Config.nKillRingMax[m_nAppID] == 0) {\r
+       if (m_CurrentConfig->KillRingMax == 0) {\r
                return;\r
        }\r
 \r
@@ -1071,7 +1071,7 @@ void CXkeymacsDll::AddKillRing(BOOL bNewData)
 \r
        m_nKillRing = 0;\r
 \r
-       if (m_Config.nKillRingMax[m_nAppID] < m_oKillRing.GetCount()) {\r
+       if (m_CurrentConfig->KillRingMax < m_oKillRing.GetCount()) {\r
                CClipboardSnap *pSnap = m_oKillRing.GetTail();\r
                delete pSnap;\r
                pSnap = NULL;\r
@@ -1083,7 +1083,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_nAppID] == 0) {\r
+       if (m_CurrentConfig->KillRingMax == 0) {\r
                return NULL;\r
        }\r
 \r
@@ -1122,65 +1122,36 @@ void CXkeymacsDll::IncreaseKillRingIndex(int nKillRing)
        m_nKillRing += nKillRing;\r
 }\r
 \r
-BOOL CXkeymacsDll::GetEnableCUA()\r
-{\r
-       return m_Config.bEnableCUA[m_nAppID];\r
-}\r
-\r
-BOOL CXkeymacsDll::Get326Compatible()\r
+bool CXkeymacsDll::GetEnableCUA()\r
 {\r
-       return m_Config.b326Compatible[m_nAppID];\r
+       return m_CurrentConfig->EnableCUA;\r
 }\r
 \r
-BOOL CXkeymacsDll::Is106Keyboard()\r
+bool CXkeymacsDll::Get326Compatible()\r
 {\r
-       return m_Config.b106Keyboard;\r
+       return m_CurrentConfig->Is326Compatible;\r
 }\r
 \r
-void CXkeymacsDll::StartRecordMacro()\r
+bool CXkeymacsDll::Is106Keyboard()\r
 {\r
-       if (CCommands::bC_u())\r
-               CallMacro();\r
-       m_bRecordingMacro = TRUE;\r
-       m_Macro.erase(m_Macro.begin(), m_Macro.end());\r
-       ZeroMemory(m_bDown, MAX_KEY);\r
+       return m_Config.Is106Keyboard;\r
 }\r
 \r
-void CXkeymacsDll::EndRecordMacro()\r
+void CXkeymacsDll::SetKbMacro(KbdMacro* kbdMacro)\r
 {\r
-       m_bRecordingMacro = FALSE;\r
-       while (!m_Macro.empty()) { // remove not released push\r
-               const KbdMacro& m = m_Macro.back();\r
-               if (HIWORD(m.lParam) & KF_UP)\r
-                       break;\r
-               m_Macro.pop_back();\r
-       }\r
-}\r
-\r
-void CXkeymacsDll::CallMacro()\r
-{\r
-       if (m_bRecordingMacro)\r
-               m_bRecordingMacro = FALSE;\r
-       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 (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
+       m_kbdMacro = kbdMacro;\r
 }\r
 \r
 // call an original command which is defined in dot.xkeymacs\r
-void CXkeymacsDll::CallFunction(int nFuncID)\r
+void CXkeymacsDll::CallFunction(int FuncID)\r
 {\r
-       if (nFuncID < 0 || nFuncID >= MAX_FUNCTION)\r
+       if (FuncID < 0 || FuncID >= MAX_FUNCTION)\r
                return;\r
-       LPCTSTR def = m_Config.szFunctionDefinition[nFuncID];\r
+       LPCTSTR def = m_Config.FuncDef[FuncID];\r
        if (!def[0])\r
                return;\r
        std::vector<KeyBind> keybinds;\r
-       const LPCTSTR last = def + _tcslen(def) - 1;\r
+       LPCTSTR last = def + _tcslen(def) - 1;\r
        if (*def == _T('"') && *last == _T('"')) {\r
                ++def; // skip '"'\r
                while (def < last)\r
@@ -1212,9 +1183,9 @@ void CXkeymacsDll::CallFunction(int nFuncID)
        UINT before = GetModifierState(FALSE);\r
 \r
        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_nAppID][nType][bVk]].fCommand : NULL;\r
+               int nType = p->nType;\r
+               BYTE bVk = p->bVk;\r
+               int (*fCommand)() = nType < MAX_COMMAND_TYPE ? CmdTable::Command(m_CurrentConfig->CmdID[nType][bVk]) : NULL;\r
                if (fCommand) {\r
                        if (fCommand == CCommands::ExecuteExtendedCommand)\r
                                bM_x = TRUE;\r
@@ -1222,7 +1193,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_nAppID][nType][bVk]].szCommandName);\r
+//                     CUtils::Log("CallFunction: Command Name: %s", Commands[m_CurrentConfig->CmdID[nType][bVk]].szCommandName);\r
                        while (fCommand() == GOTO_RECURSIVE)\r
                                ;\r
                        continue;\r
@@ -1230,16 +1201,8 @@ void CXkeymacsDll::CallFunction(int nFuncID)
                if (bM_x) {\r
                        if (bVk == VK_RETURN)\r
                                InvokeM_x(szPath);\r
-                       else if (bVk != 0) {\r
-                               TCHAR nAscii = 0;\r
-                               do { // 1-127\r
-                                       if (a2v(++nAscii) == bVk && ((nType & SHIFT) != 0) == IsShift(nAscii)) {\r
-//                                             CUtils::Log("M-x: %#X (%c), %#X (%c)", bVk, bVk, nAscii, nAscii);\r
-                                               szPath[index++] = nAscii;\r
-                                               break;\r
-                                       }\r
-                               } while (nAscii != 127);\r
-                       }\r
+                       else if (bVk != 0)\r
+                               szPath[index++] = static_cast<TCHAR>(ConvVkey((bVk | (nType << 8)) & 0x7ff /* drop CONTROLX */, 1));\r
                        continue;\r
                }\r
                if (!bInitialized) {\r
@@ -1255,7 +1218,7 @@ void CXkeymacsDll::CallFunction(int nFuncID)
                if (nType & SHIFT)\r
                        DepressKey(VK_SHIFT);\r
                Kdu(bVk);\r
-               const int nNextType = (p + 1) != keybinds.end() ? (p + 1)->nType : 0;\r
+               int nNextType = (p + 1) != keybinds.end() ? (p + 1)->nType : 0;\r
                if (nType & SHIFT && !(nNextType & SHIFT))\r
                        ReleaseKey(VK_SHIFT);\r
                if (nType & WIN_ALT && !(nNextType & WIN_ALT))\r
@@ -1287,8 +1250,6 @@ KeyBind CXkeymacsDll::ParseKey(LPCTSTR& def)
                        }\r
                }\r
        }\r
-       if (IsShift(*def) && !(keybind.nType & (WIN_CTRL | WIN_ALT | WIN_WIN)))\r
-               keybind.nType |= SHIFT;\r
        int i = 0;\r
        for (; i < MAX_KEYNAME; ++i) {\r
                size_t len = _tcslen(KeyNames[i].name);\r
@@ -1297,190 +1258,44 @@ KeyBind CXkeymacsDll::ParseKey(LPCTSTR& def)
                        break;\r
                }\r
        }\r
-       keybind.bVk = i < MAX_KEYNAME ? KeyNames[i].bVk : a2v(*def++);\r
+       if (i < MAX_KEYNAME) {\r
+               keybind.bVk = KeyNames[i].bVk;\r
+               return keybind;\r
+       }\r
+       SHORT r = ConvVkey(*def++, 0);\r
+       if (r & (1 << 8) && !(keybind.nType & (WIN_CTRL | WIN_ALT | WIN_WIN)))\r
+               keybind.nType |= SHIFT;\r
+       keybind.bVk = static_cast<BYTE>(r);\r
        return keybind;\r
 }\r
 \r
-BOOL CXkeymacsDll::IsShift(TCHAR nAscii)\r
+SHORT CXkeymacsDll::ConvVkey(SHORT in, int mode)\r
 {\r
-       switch (nAscii) {\r
-       case _T(' '):\r
-               return FALSE;\r
-       case _T('!'):\r
-       case _T('"'):\r
-       case _T('#'):\r
-       case _T('$'):\r
-       case _T('%'):\r
-       case _T('&'):\r
-               return TRUE;\r
-       case _T('\''):\r
-               return m_Config.b106Keyboard;\r
-       case _T('('):\r
-       case _T(')'):\r
-       case _T('*'):\r
-       case _T('+'):\r
-               return TRUE;\r
-       case _T(','):\r
-       case _T('-'):\r
-       case _T('.'):\r
-       case _T('/'):\r
-       case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):\r
-               return FALSE;\r
-       case _T(':'):\r
-               return !m_Config.b106Keyboard;\r
-       case _T(';'):\r
-               return FALSE;\r
-       case _T('<'):\r
-               return TRUE;\r
-       case _T('='):\r
-               return m_Config.b106Keyboard;\r
-       case _T('>'):\r
-       case _T('?'):\r
-               return TRUE;\r
-       case _T('@'):\r
-               return !m_Config.b106Keyboard;\r
-       case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'): \r
-       case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'): \r
-       case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'): \r
-               return TRUE;\r
-       case _T('['):\r
-       case _T('\\'):\r
-       case _T(']'):\r
-               return FALSE;\r
-       case _T('^'):\r
-               return !m_Config.b106Keyboard;\r
-       case _T('_'):\r
-               return TRUE;\r
-       case _T('`'):\r
-               return m_Config.b106Keyboard;\r
-       case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'): \r
-       case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'): \r
-       case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'): \r
-               return FALSE;\r
-       case _T('{'):\r
-       case _T('|'):\r
-       case _T('}'):\r
-       case _T('~'):\r
-               return TRUE;\r
-       default:\r
-               return FALSE;\r
+       HKL h = GetKeyboardLayout(0);\r
+       if (mode == 0) { // ASCII to VKey and state\r
+               SHORT r = VkKeyScanEx(static_cast<TCHAR>(in), h);\r
+               if (r < 0) // no key correcpont to the char\r
+                       return 0;\r
+               return r & 0x7ff; // drop state flags of Hankaku and others\r
        }\r
-}\r
-\r
-BYTE CXkeymacsDll::a2v(TCHAR nAscii)\r
-{\r
-       switch (nAscii) {\r
-       case _T(' '):\r
-               return VK_SPACE;\r
-       case _T('!'):\r
-               return '1';\r
-       case _T('"'):\r
-               return m_Config.b106Keyboard ? '2' : (BYTE) 0xde;       // VK_OEM_7\r
-       case _T('#'):\r
-               return '3';\r
-       case _T('$'):\r
-               return '4';\r
-       case _T('%'):\r
-               return '5';\r
-       case _T('&'):\r
-               return m_Config.b106Keyboard ? '6' : '7';\r
-       case _T('\''):\r
-               return m_Config.b106Keyboard ? '7' : (BYTE) 0xde;       // VK_OEM_7\r
-       case _T('('):\r
-               return m_Config.b106Keyboard ? '8' : '9';\r
-       case _T(')'):\r
-               return m_Config.b106Keyboard ? '9' : '0';\r
-       case _T('*'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xba : '8';       // VK_OEM_1\r
-       case _T('+'):\r
-               return 0xbb;    // VK_OEM_PLUS\r
-       case _T(','):\r
-               return 0xbc;    // VK_OEM_COMMA\r
-       case _T('-'):\r
-               return 0xbd;    // VK_OEM_MINUS\r
-       case _T('.'):\r
-               return 0xbe;    // VK_OEM_PERIOD\r
-       case _T('/'):\r
-               return 0xbf;    // VK_OEM_2\r
-       case _T('0'): case _T('1'): case _T('2'): case _T('3'): case _T('4'): case _T('5'): case _T('6'): case _T('7'): case _T('8'): case _T('9'):\r
-               return nAscii;\r
-       case _T(':'):\r
-               return 0xba;    // VK_OEM_1\r
-       case _T(';'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xbb : (BYTE) 0xba;       // VK_OEM_PLUS  VK_OEM_1\r
-       case _T('<'):\r
-               return 0xbc;    // VK_OEM_COMMA\r
-       case _T('='):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xbd : (BYTE) 0xbb;       // VK_OEM_MINUS VK_OEM_PLUS\r
-       case _T('>'):\r
-               return 0xbe;    // VK_OEM_PERIOD\r
-       case _T('?'):\r
-               return 0xbf;    // VK_OEM_2\r
-       case _T('@'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xc0 : '2';\r
-       case _T('A'): case _T('B'): case _T('C'): case _T('D'): case _T('E'): case _T('F'): case _T('G'): case _T('H'): case _T('I'): case _T('J'): \r
-       case _T('K'): case _T('L'): case _T('M'): case _T('N'): case _T('O'): case _T('P'): case _T('Q'): case _T('R'): case _T('S'): case _T('T'): \r
-       case _T('U'): case _T('V'): case _T('W'): case _T('X'): case _T('Y'): case _T('Z'): \r
-               return nAscii;\r
-       case _T('['):\r
-               return 0xdb;    // VK_OEM_4\r
-       case _T('\\'):\r
-               return 0xdc;    // VK_OEM_5\r
-       case _T(']'):\r
-               return 0xdd;    // VK_OEM_6\r
-       case _T('^'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xde : '6';       // VK_OEM_7\r
-       case _T('_'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xe2 : (BYTE) 0xbd;       // VK_OEM_102   VK_OEM_MINUS\r
-       case _T('`'):\r
-               return 0xc0;    // VK_OEM_3\r
-       case _T('a'): case _T('b'): case _T('c'): case _T('d'): case _T('e'): case _T('f'): case _T('g'): case _T('h'): case _T('i'): case _T('j'): \r
-       case _T('k'): case _T('l'): case _T('m'): case _T('n'): case _T('o'): case _T('p'): case _T('q'): case _T('r'): case _T('s'): case _T('t'): \r
-       case _T('u'): case _T('v'): case _T('w'): case _T('x'): case _T('y'): case _T('z'): \r
-               return (BYTE) (nAscii - (_T('a') - _T('A')));\r
-       case _T('{'):\r
-               return 0xdb;    // VK_OEM_4\r
-       case _T('|'):\r
-               return 0xdc;    // VK_OEM_5\r
-       case _T('}'):\r
-               return 0xdd;    // VK_OEM_6\r
-       case _T('~'):\r
-               return m_Config.b106Keyboard ? (BYTE) 0xde : (BYTE) 0xc0;       // VK_OEM_7     VK_OEM_3\r
-       default:\r
+       // VKey and state to ASCII\r
+       const BYTE down = 0x80;\r
+       BYTE state[256] = {0};\r
+       if (in & (1 << 8))\r
+               state[VK_SHIFT] = down;\r
+       if (in & (2 << 8))\r
+               state[VK_CONTROL] = down;\r
+       if (in & (4 << 8))\r
+               state[VK_MENU] = down;\r
+       UINT vkey = in & 0xff;\r
+       state[vkey] = down;\r
+       WORD word = 0;\r
+       int r = ToAsciiEx(vkey, MapVirtualKeyEx(vkey, MAPVK_VK_TO_VSC, h), state, &word, 0, h);\r
+       if (r == 0)\r
                return 0;\r
-       }\r
-}\r
-\r
-BOOL CXkeymacsDll::IsMatchWindowText(CString szWindowText)\r
-{\r
-       BOOL bIsMatchWindowText = TRUE;\r
-\r
-       TCHAR szCurrentWindowText[WINDOW_TEXT_LENGTH] = {'\0'};\r
-       GetWindowText(GetForegroundWindow(), szCurrentWindowText, sizeof(szCurrentWindowText));\r
-\r
-       switch (CUtils::GetWindowTextType(szWindowText)) {\r
-       case IDS_WINDOW_TEXT_MATCH:                                                             // *foo*\r
-               szWindowText.Delete(0);                                                         // Delete first '*'\r
-               szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
-               bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_FORWARD:                                             // foo*\r
-               szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
-               bIsMatchWindowText = 0 == CString(szCurrentWindowText).Find(szWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_BACKWARD:                                    // *foo\r
-               szWindowText.Delete(0);                                                         // Delete first '*'\r
-               bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText, CString(szCurrentWindowText).GetLength() - szWindowText.GetLength());\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_FULL:                                                // foo\r
-               bIsMatchWindowText = szWindowText == CString(szCurrentWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_IGNORE:                                                    // *\r
-               bIsMatchWindowText = TRUE;\r
-               break;\r
-       }\r
-//     CUtils::Log(_T("IsMatchWindowText: %d, _%s_, _%s_"), bIsMatchWindowText, szCurrentWindowText, szWindowText);\r
-       return bIsMatchWindowText;\r
+       if (r == 1)\r
+               return static_cast<SHORT>(word);\r
+       return static_cast<SHORT>(word >> 8); // drop a dead key\r
 }\r
 \r
 void CXkeymacsDll::SetAccelerate(int nAccelerate)\r