It looks like that GetKeyState is supposed to catch key presses when called from a background thread even after its thread message queue and thread input structures have been created.
This happens in Bioshock 2 Remaster, and causes the game to stay forever on a "Press space to continue" screen, as the thread checking for space key press calls GetKeyState repeatedly (and PeekMessage), although it's in background.
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=26269 Signed-off-by: Rémi Bernon rbernon@codeweavers.com ---
v2: Fix PATCH 2 todo_wine, skip the test on non-us keyboards.
dlls/user32/tests/input.c | 145 +++++++++++++++++++++++++++++--------- 1 file changed, 111 insertions(+), 34 deletions(-)
diff --git a/dlls/user32/tests/input.c b/dlls/user32/tests/input.c index 1249f8cf5a1..1ca836b9cdd 100644 --- a/dlls/user32/tests/input.c +++ b/dlls/user32/tests/input.c @@ -3715,74 +3715,151 @@ static void test_attach_input(void) DestroyWindow(Wnd2); }
+struct get_key_state_test_desc +{ + BOOL peek_message; + BOOL peek_message_main; +}; + +struct get_key_state_test_desc get_key_state_tests[] = +{ + /* 0: not peeking in thread, no msg queue: GetKeyState misses key press */ + {FALSE, TRUE}, + /* 1: peeking on thread init, not in main: GetKeyState catches key press */ + { TRUE, FALSE}, + /* 2: peeking on thread init, and in main: GetKeyState catches key press */ + { TRUE, TRUE}, +}; + +struct get_key_state_thread_params +{ + HANDLE semaphores[2]; + int index; +}; + static DWORD WINAPI get_key_state_thread(void *arg) { - HANDLE *semaphores = arg; + struct get_key_state_thread_params *params = arg; + struct get_key_state_test_desc* test; + HANDLE *semaphores = params->semaphores; DWORD result; + BOOL has_queue; + MSG msg; + int i = params->index;
+ test = get_key_state_tests + i; + has_queue = test->peek_message; + + if (test->peek_message) + { + while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) + ok(!is_keyboard_message(msg.message), "%d: PeekMessageA got keyboard message.\n", i); + } + + /* initialization */ ReleaseSemaphore(semaphores[0], 1, NULL); result = WaitForSingleObject(semaphores[1], 1000); - ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
- result = GetKeyState('X'); - ok((result & 0x8000) || broken(!(result & 0x8000)), /* > Win 2003 */ - "expected that highest bit is set, got %x\n", result); + /* key pressed */ + ReleaseSemaphore(semaphores[0], 1, NULL); + result = WaitForSingleObject(semaphores[1], 1000); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
- ok((SHORT)(result & 0x007e) == 0, - "expected that undefined bits are unset, got %x\n", result); + result = GetKeyState('X'); + if (!has_queue) todo_wine ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); + else todo_wine ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); + ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
+ /* key released */ ReleaseSemaphore(semaphores[0], 1, NULL); result = WaitForSingleObject(semaphores[1], 1000); - ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
result = GetKeyState('X'); - ok(!(result & 0x8000), "expected that highest bit is unset, got %x\n", result); - - ok((SHORT)(result & 0x007e) == 0, - "expected that undefined bits are unset, got %x\n", result); + ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); + ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
return 0; }
static void test_GetKeyState(void) { - HANDLE semaphores[2]; + struct get_key_state_thread_params params; HANDLE thread; DWORD result; HWND hwnd; + MSG msg; + int i;
- semaphores[0] = CreateSemaphoreA(NULL, 0, 1, NULL); - ok(semaphores[0] != NULL, "CreateSemaphoreA failed %u\n", GetLastError()); - semaphores[1] = CreateSemaphoreA(NULL, 0, 1, NULL); - ok(semaphores[1] != NULL, "CreateSemaphoreA failed %u\n", GetLastError()); + BOOL us_kbd = (GetKeyboardLayout(0) == (HKL)(ULONG_PTR)0x04090409); + if (!us_kbd) + { + skip("skipping test with inconsistent results on non-us keyboard\n"); + return; + } + + params.semaphores[0] = CreateSemaphoreA(NULL, 0, 1, NULL); + ok(params.semaphores[0] != NULL, "CreateSemaphoreA failed %u\n", GetLastError()); + params.semaphores[1] = CreateSemaphoreA(NULL, 0, 1, NULL); + ok(params.semaphores[1] != NULL, "CreateSemaphoreA failed %u\n", GetLastError());
hwnd = CreateWindowA("static", "Title", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 10, 10, 200, 200, NULL, NULL, NULL, NULL); ok(hwnd != NULL, "CreateWindowA failed %u\n", GetLastError()); + empty_message_queue();
- thread = CreateThread(NULL, 0, get_key_state_thread, semaphores, 0, NULL); - ok(thread != NULL, "CreateThread failed %u\n", GetLastError()); - result = WaitForSingleObject(semaphores[0], 1000); - ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); + for (i = 0; i < ARRAY_SIZE(get_key_state_tests); ++i) + { + struct get_key_state_test_desc* test = get_key_state_tests + i;
- SetForegroundWindow(hwnd); - SetFocus(hwnd); - keybd_event('X', 0, 0, 0); + params.index = i; + thread = CreateThread(NULL, 0, get_key_state_thread, ¶ms, 0, NULL); + ok(thread != NULL, "CreateThread failed %u\n", GetLastError());
- ReleaseSemaphore(semaphores[1], 1, NULL); - result = WaitForSingleObject(semaphores[0], 1000); - ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); + /* initialization */ + result = WaitForSingleObject(params.semaphores[0], 1000); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
- keybd_event('X', 0, KEYEVENTF_KEYUP, 0); + SetForegroundWindow(hwnd); + SetFocus(hwnd); + empty_message_queue();
- ReleaseSemaphore(semaphores[1], 1, NULL); - result = WaitForSingleObject(thread, 1000); - ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); - CloseHandle(thread); + ReleaseSemaphore(params.semaphores[1], 1, NULL); + + /* key pressed */ + result = WaitForSingleObject(params.semaphores[0], 1000); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result); + + keybd_event('X', 0, 0, 0); + if (test->peek_message_main) while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessageA(&msg); + + result = GetKeyState('X'); + if (test->peek_message_main) ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); + else ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); + + ReleaseSemaphore(params.semaphores[1], 1, NULL); + + /* key released */ + result = WaitForSingleObject(params.semaphores[0], 1000); + ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result); + + keybd_event('X', 0, KEYEVENTF_KEYUP, 0); + if (test->peek_message_main) while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessageA(&msg); + + result = GetKeyState('X'); + ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); + + ReleaseSemaphore(params.semaphores[1], 1, NULL); + + result = WaitForSingleObject(thread, 1000); + ok(result == WAIT_OBJECT_0, "WaitForSingleObject returned %u\n", result); + CloseHandle(thread); + }
DestroyWindow(hwnd); - CloseHandle(semaphores[0]); - CloseHandle(semaphores[1]); + CloseHandle(params.semaphores[0]); + CloseHandle(params.semaphores[1]); }
static void test_OemKeyScan(void)
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=26269 Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/user32/tests/input.c | 38 +++++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-)
diff --git a/dlls/user32/tests/input.c b/dlls/user32/tests/input.c index 1ca836b9cdd..a17934fa6bc 100644 --- a/dlls/user32/tests/input.c +++ b/dlls/user32/tests/input.c @@ -3723,11 +3723,13 @@ struct get_key_state_test_desc
struct get_key_state_test_desc get_key_state_tests[] = { - /* 0: not peeking in thread, no msg queue: GetKeyState misses key press */ + /* 0: not peeking in thread, no msg queue: GetKeyState / GetKeyboardState miss key press */ {FALSE, TRUE}, - /* 1: peeking on thread init, not in main: GetKeyState catches key press */ + /* 1: peeking on thread init, not in main: GetKeyState / GetKeyboardState catch key press */ + /* - GetKeyboardState misses key press if called before GetKeyState */ + /* - GetKeyboardState catches key press, if called after GetKeyState */ { TRUE, FALSE}, - /* 2: peeking on thread init, and in main: GetKeyState catches key press */ + /* 2: peeking on thread init, and in main: GetKeyState / GetKeyboardState catch key press */ { TRUE, TRUE}, };
@@ -3743,7 +3745,9 @@ static DWORD WINAPI get_key_state_thread(void *arg) struct get_key_state_test_desc* test; HANDLE *semaphores = params->semaphores; DWORD result; + BYTE keystate[256]; BOOL has_queue; + BOOL ret; MSG msg; int i = params->index;
@@ -3766,20 +3770,48 @@ static DWORD WINAPI get_key_state_thread(void *arg) result = WaitForSingleObject(semaphores[1], 1000); ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
+ memset(keystate, 0, sizeof(keystate)); + ret = GetKeyboardState(keystate); + ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); + result = keystate['X']; + todo_wine_if(!has_queue) + ok(!result, "%d: expected that keystate is not set, got %#x\n", i, result); + result = GetKeyState('X'); if (!has_queue) todo_wine ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); else todo_wine ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
+ memset(keystate, 0, sizeof(keystate)); + ret = GetKeyboardState(keystate); + ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); + result = keystate['X']; + if (!has_queue) todo_wine ok(!result, "%d: expected that keystate is unset, got %#x\n", i, result); + else todo_wine ok(result, "%d: expected that keystate is set, got %#x\n", i, result); + /* key released */ ReleaseSemaphore(semaphores[0], 1, NULL); result = WaitForSingleObject(semaphores[1], 1000); ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
+ memset(keystate, 0, sizeof(keystate)); + ret = GetKeyboardState(keystate); + ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); + result = keystate['X']; + if (!has_queue) ok(!result, "%d: expected that keystate is unset, got %#x\n", i, result); + else todo_wine ok(result, "%d: expected that keystate is set, got %#x\n", i, result); + result = GetKeyState('X'); ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
+ memset(keystate, 0, sizeof(keystate)); + ret = GetKeyboardState(keystate); + ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); + result = keystate['X']; + if (!has_queue) ok(!result || broken(result) /* w2008 */, "%d: expected that keystate is unset, got %#x\n", i, result); + else todo_wine ok(result || broken(!result) /* w2008 */, "%d: expected that keystate is set, got %#x\n", i, result); + return 0; }
Hi,
While running your changed tests, I think I found new failures. Being a bot and all I'm not very good at pattern recognition, so I might be wrong, but could you please double-check?
Full results can be found at: https://testbot.winehq.org/JobDetails.pl?Key=85307
Your paranoid android.
=== w1064_2qxl (64 bit report) ===
user32: input.c:1322: Test failed: GetCursorPos: (100,100)
=== w10pro64_ar (64 bit report) ===
user32: input.c:1362: Test failed: Wrong new pos: (103,103) input.c:1292: Test failed: GetCursorPos: (99,100)
Wine-Bug: https://bugs.winehq.org/show_bug.cgi?id=26269 Signed-off-by: Rémi Bernon rbernon@codeweavers.com --- dlls/user32/tests/input.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+)
diff --git a/dlls/user32/tests/input.c b/dlls/user32/tests/input.c index a17934fa6bc..7367c2f8f21 100644 --- a/dlls/user32/tests/input.c +++ b/dlls/user32/tests/input.c @@ -3777,11 +3777,18 @@ static DWORD WINAPI get_key_state_thread(void *arg) todo_wine_if(!has_queue) ok(!result, "%d: expected that keystate is not set, got %#x\n", i, result);
+ result = keystate['C']; + ok(!result, "%d: expected that C keystate is not set, got %#x\n", i, result); + result = GetKeyState('X'); if (!has_queue) todo_wine ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); else todo_wine ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
+ result = GetKeyState('C'); + ok(!(result & 0x8000), "%d: expected that C highest bit is unset, got %#x\n", i, result); + ok(!(result & 0x007e), "%d: expected that C undefined bits are unset, got %#x\n", i, result); + memset(keystate, 0, sizeof(keystate)); ret = GetKeyboardState(keystate); ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); @@ -3789,6 +3796,9 @@ static DWORD WINAPI get_key_state_thread(void *arg) if (!has_queue) todo_wine ok(!result, "%d: expected that keystate is unset, got %#x\n", i, result); else todo_wine ok(result, "%d: expected that keystate is set, got %#x\n", i, result);
+ result = keystate['C']; + ok(!result, "%d: expected that C keystate is not set, got %#x\n", i, result); + /* key released */ ReleaseSemaphore(semaphores[0], 1, NULL); result = WaitForSingleObject(semaphores[1], 1000); @@ -3801,10 +3811,17 @@ static DWORD WINAPI get_key_state_thread(void *arg) if (!has_queue) ok(!result, "%d: expected that keystate is unset, got %#x\n", i, result); else todo_wine ok(result, "%d: expected that keystate is set, got %#x\n", i, result);
+ result = keystate['C']; + ok(!result, "%d: expected that C keystate is not set, got %#x\n", i, result); + result = GetKeyState('X'); ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); ok(!(result & 0x007e), "%d: expected that undefined bits are unset, got %#x\n", i, result);
+ result = GetKeyState('C'); + ok(!(result & 0x8000), "%d: expected that C highest bit is unset, got %#x\n", i, result); + ok(!(result & 0x007e), "%d: expected that C undefined bits are unset, got %#x\n", i, result); + memset(keystate, 0, sizeof(keystate)); ret = GetKeyboardState(keystate); ok(ret, "GetKeyboardState failed, %u\n", GetLastError()); @@ -3812,6 +3829,9 @@ static DWORD WINAPI get_key_state_thread(void *arg) if (!has_queue) ok(!result || broken(result) /* w2008 */, "%d: expected that keystate is unset, got %#x\n", i, result); else todo_wine ok(result || broken(!result) /* w2008 */, "%d: expected that keystate is set, got %#x\n", i, result);
+ result = keystate['C']; + ok(!result, "%d: expected that C keystate is not set, got %#x\n", i, result); + return 0; }
@@ -3820,6 +3840,7 @@ static void test_GetKeyState(void) struct get_key_state_thread_params params; HANDLE thread; DWORD result; + BYTE keystate[256]; HWND hwnd; MSG msg; int i; @@ -3831,6 +3852,7 @@ static void test_GetKeyState(void) return; }
+ memset(keystate, 0, sizeof(keystate)); params.semaphores[0] = CreateSemaphoreA(NULL, 0, 1, NULL); ok(params.semaphores[0] != NULL, "CreateSemaphoreA failed %u\n", GetLastError()); params.semaphores[1] = CreateSemaphoreA(NULL, 0, 1, NULL); @@ -3864,8 +3886,12 @@ static void test_GetKeyState(void) ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
keybd_event('X', 0, 0, 0); + keystate['C'] = 0xff; + SetKeyboardState(keystate); if (test->peek_message_main) while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
+ result = GetKeyState('C'); + ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); result = GetKeyState('X'); if (test->peek_message_main) ok((result & 0x8000), "%d: expected that highest bit is set, got %#x\n", i, result); else ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); @@ -3877,8 +3903,12 @@ static void test_GetKeyState(void) ok(result == WAIT_OBJECT_0, "%d: WaitForSingleObject returned %u\n", i, result);
keybd_event('X', 0, KEYEVENTF_KEYUP, 0); + keystate['C'] = 0x00; + SetKeyboardState(keystate); if (test->peek_message_main) while (PeekMessageA(&msg, NULL, 0, 0, PM_REMOVE)) DispatchMessageA(&msg);
+ result = GetKeyState('C'); + ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result); result = GetKeyState('X'); ok(!(result & 0x8000), "%d: expected that highest bit is unset, got %#x\n", i, result);
Hi,
While running your changed tests, I think I found new failures. Being a bot and all I'm not very good at pattern recognition, so I might be wrong, but could you please double-check?
Full results can be found at: https://testbot.winehq.org/JobDetails.pl?Key=85308
Your paranoid android.
=== w1064 (32 bit report) ===
user32: input.c:1432: Test failed: Wrong new pos: (150,150)
Hi,
While running your changed tests, I think I found new failures. Being a bot and all I'm not very good at pattern recognition, so I might be wrong, but could you please double-check?
Full results can be found at: https://testbot.winehq.org/JobDetails.pl?Key=85306
Your paranoid android.
=== w1064_2qxl (64 bit report) ===
user32: input.c:3268: Test failed: expected WM_NCHITTEST message input.c:3269: Test failed: expected WM_RBUTTONDOWN message input.c:3270: Test failed: expected WM_RBUTTONUP message input.c:3299: Test failed: expected WM_LBUTTONDOWN message input.c:3300: Test failed: expected WM_LBUTTONUP message input.c:3353: Test failed: expected loop with WM_NCHITTEST messages input.c:3406: Test failed: expected WM_LBUTTONDOWN message input.c:3407: Test failed: expected WM_LBUTTONUP message