Skip to content

Commit 3885311

Browse files
authored
[KBSWITCH] Fix ID_IMEONOFF action (reactos#8562)
Toggling IME open/close from pen icon menu didn't work because the kbswitch app thread is different from the IME thread. JIRA issue: CORE-19268 - Use WM_IME_CONTROL:IMC_GETOPENSTATUS instead of imm32!ImmGetOpenStatus. - Use WM_IME_CONTROL:IMC_GETCONVERSIONMODE instead of imm32!ImmGetConversionStatus. - Use WM_IME_SYSTEM:IMS_SETOPENSTATUS instead of imm32!ImmSetOpenStatus. - Adjustment for timing of SetForegroundWindow call.
1 parent c77e0e5 commit 3885311

1 file changed

Lines changed: 28 additions & 16 deletions

File tree

base/applications/kbswitch/kbswitch.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ HWND g_hTrayNotifyWnd = NULL;
7777
#define LAYOUTF_REMOVE_LEFT_DEF_MENU 0x8
7878
#define LAYOUTF_REMOVE_RIGHT_DEF_MENU 0x10
7979

80+
// ImmGetOpenStatus cannot be used from different thread
81+
static inline BOOL IsImeOpen(HWND hwndIme)
82+
{
83+
return (BOOL)SendMessage(hwndIme, WM_IME_CONTROL, IMC_GETOPENSTATUS, 0);
84+
}
85+
86+
// ImmGetConversionStatus cannot be used from different thread
87+
static inline DWORD GetImeConversionMode(HWND hwndIme)
88+
{
89+
return (DWORD)SendMessage(hwndIme, WM_IME_CONTROL, IMC_GETCONVERSIONMODE, 0);
90+
}
91+
8092
static VOID
8193
UpdateTrayInfo(VOID)
8294
{
@@ -471,18 +483,15 @@ GetImeStatus(HWND hwndTarget)
471483
if (!hIMC)
472484
return IME_STATUS_NO_IME;
473485

474-
DWORD dwImeStatus = (ImmGetOpenStatus(hIMC) ? IME_STATUS_IME_OPEN : IME_STATUS_IME_CLOSED);
486+
DWORD dwImeStatus = (IsImeOpen(hwndIme) ? IME_STATUS_IME_OPEN : IME_STATUS_IME_CLOSED);
475487
if (GetACP() == 949) // Korean
476488
{
477-
DWORD dwConversion = 0, dwSentence = 0;
478-
if (ImmGetConversionStatus(hIMC, &dwConversion, &dwSentence))
479-
{
480-
if (dwConversion & IME_CMODE_NATIVE)
481-
dwImeStatus |= IME_STATUS_IME_NATIVE;
489+
DWORD dwConversion = GetImeConversionMode(hwndIme);
490+
if (dwConversion & IME_CMODE_NATIVE)
491+
dwImeStatus |= IME_STATUS_IME_NATIVE;
482492

483-
if (dwConversion & IME_CMODE_FULLSHAPE)
484-
dwImeStatus |= IME_STATUS_IME_FULLSHAPE;
485-
}
493+
if (dwConversion & IME_CMODE_FULLSHAPE)
494+
dwImeStatus |= IME_STATUS_IME_FULLSHAPE;
486495
}
487496

488497
return dwImeStatus;
@@ -1111,7 +1120,11 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
11111120
return;
11121121
}
11131122

1114-
// Workaround of TrackPopupMenu's bug
1123+
// Is IME open?
1124+
BOOL bImeOn = IsImeOpen(hwndIme);
1125+
1126+
// Workaround of TrackPopupMenu's bug.
1127+
// NOTE: This might change IME status.
11151128
SetForegroundWindow(hwnd);
11161129

11171130
// Create IME menu
@@ -1120,8 +1133,7 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
11201133
HMENU hMenu = MenuFromImeMenu(pImeMenu);
11211134

11221135
HKL hKL = g_ahKLs[g_iKL];
1123-
DWORD dwImeStatus = GetImeStatus(hwndTarget);
1124-
BOOL bImeOn = FALSE, bSoftOn = FALSE, bShowToolbar = FALSE;
1136+
BOOL bSoftOn = FALSE, bShowToolbar = FALSE;
11251137
TCHAR szText[128];
11261138
if (bRightButton)
11271139
{
@@ -1145,7 +1157,6 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
11451157
if (!IS_KOREAN_IME_HKL(hKL)) // Not Korean IME?
11461158
{
11471159
// "IME ON / OFF"
1148-
bImeOn = (dwImeStatus == IME_STATUS_IME_OPEN);
11491160
UINT nId = (bImeOn ? IDS_IME_ON : IDS_IME_OFF);
11501161
LoadString(g_hInst, nId, szText, _countof(szText));
11511162
AppendMenu(hMenu, MF_STRING, ID_IMEONOFF, szText);
@@ -1197,6 +1208,9 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
11971208
// Workaround of TrackPopupMenu's bug
11981209
PostMessage(hwnd, WM_NULL, 0, 0);
11991210

1211+
// Back to target window
1212+
SetForegroundWindow(hwndTarget);
1213+
12001214
if (nID) // Action!
12011215
{
12021216
if (nID >= ID_STARTIMEMENU) // IME internal menu ID?
@@ -1221,7 +1235,7 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
12211235
PostMessage(hwndIme, WM_IME_SYSTEM, IMS_CONFIGURE, (LPARAM)hKL);
12221236
break;
12231237
case ID_IMEONOFF:
1224-
ImmSetOpenStatus(hIMC, !bImeOn);
1238+
PostMessage(hwndIme, WM_IME_SYSTEM, IMS_SETOPENSTATUS, !bImeOn);
12251239
break;
12261240
case ID_SOFTKBDONOFF:
12271241
PostMessage(hwndIme, WM_IME_SYSTEM, IMS_SOFTKBDONOFF, !bSoftOn);
@@ -1241,8 +1255,6 @@ KbSwitch_OnPenIconMsg(HWND hwnd, UINT uMouseMsg)
12411255
// Clean up
12421256
DestroyMenu(hMenu);
12431257
CleanupImeMenus();
1244-
1245-
SetForegroundWindow(hwndTarget);
12461258
}
12471259

12481260
// WM_COMMAND

0 commit comments

Comments
 (0)