winex11: Use pthread for keyboard lock.

Signed-off-by: Jacek Caban <jacek@codeweavers.com>
Signed-off-by: Huw Davies <huw@codeweavers.com>
Signed-off-by: Alexandre Julliard <julliard@winehq.org>
This commit is contained in:
Jacek Caban 2022-04-18 14:40:30 +02:00 committed by Alexandre Julliard
parent e395c161ed
commit b2bc410b7c
1 changed files with 20 additions and 27 deletions

View File

@ -67,14 +67,7 @@ static WORD keyc2vkey[256], keyc2scan[256];
static int NumLockMask, ScrollLockMask, AltGrMask; /* mask in the XKeyEvent state */ static int NumLockMask, ScrollLockMask, AltGrMask; /* mask in the XKeyEvent state */
static CRITICAL_SECTION kbd_section; static pthread_mutex_t kbd_mutex = PTHREAD_MUTEX_INITIALIZER;
static CRITICAL_SECTION_DEBUG critsect_debug =
{
0, 0, &kbd_section,
{ &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
0, 0, { (DWORD_PTR)(__FILE__ ": kbd_section") }
};
static CRITICAL_SECTION kbd_section = { &critsect_debug, -1, 0, 0, 0, 0 };
static char KEYBOARD_MapDeadKeysym(KeySym keysym); static char KEYBOARD_MapDeadKeysym(KeySym keysym);
@ -1218,7 +1211,7 @@ BOOL X11DRV_KeymapNotify( HWND hwnd, XEvent *event )
memset(keys, 0, sizeof(keys)); memset(keys, 0, sizeof(keys));
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
/* the minimum keycode is always greater or equal to 8, so we can /* the minimum keycode is always greater or equal to 8, so we can
* skip the first 8 values, hence start at 1 * skip the first 8 values, hence start at 1
@ -1248,7 +1241,7 @@ BOOL X11DRV_KeymapNotify( HWND hwnd, XEvent *event )
} }
} }
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
if (!changed) return FALSE; if (!changed) return FALSE;
update_key_state( keystate, VK_CONTROL, (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80 ); update_key_state( keystate, VK_CONTROL, (keystate[VK_LCONTROL] | keystate[VK_RCONTROL]) & 0x80 );
@ -1367,7 +1360,7 @@ BOOL X11DRV_KeyEvent( HWND hwnd, XEvent *xev )
return TRUE; return TRUE;
} }
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
/* If XKB extensions are used, the state mask for AltGr will use the group /* If XKB extensions are used, the state mask for AltGr will use the group
index instead of the modifier mask. The group index is set in bits index instead of the modifier mask. The group index is set in bits
@ -1402,7 +1395,7 @@ BOOL X11DRV_KeyEvent( HWND hwnd, XEvent *xev )
TRACE_(key)("keycode %u converted to vkey 0x%X scan %02x\n", TRACE_(key)("keycode %u converted to vkey 0x%X scan %02x\n",
event->keycode, vkey, bScan); event->keycode, vkey, bScan);
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
if (!vkey) return FALSE; if (!vkey) return FALSE;
@ -1597,7 +1590,7 @@ void X11DRV_InitKeyboard( Display *display )
}; };
int vkey_range; int vkey_range;
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
XDisplayKeycodes(display, &min_keycode, &max_keycode); XDisplayKeycodes(display, &min_keycode, &max_keycode);
if (key_mapping) XFree( key_mapping ); if (key_mapping) XFree( key_mapping );
key_mapping = XGetKeyboardMapping(display, min_keycode, key_mapping = XGetKeyboardMapping(display, min_keycode,
@ -1834,7 +1827,7 @@ void X11DRV_InitKeyboard( Display *display )
keyc2scan[keyc]=scan++; keyc2scan[keyc]=scan++;
} }
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
} }
static BOOL match_x11_keyboard_layout(HKL hkl) static BOOL match_x11_keyboard_layout(HKL hkl)
@ -1939,13 +1932,13 @@ SHORT X11DRV_VkKeyScanEx( WCHAR wChar, HKL hkl )
TRACE("'%c'(%lx): got keycode %u\n", cChar, keysym, keycode); TRACE("'%c'(%lx): got keycode %u\n", cChar, keysym, keycode);
if (!keycode) return -1; if (!keycode) return -1;
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
/* keycode -> (keyc2vkey) vkey */ /* keycode -> (keyc2vkey) vkey */
ret = keyc2vkey[keycode]; ret = keyc2vkey[keycode];
if (!ret) if (!ret)
{ {
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
TRACE("keycode for '%c' not found, returning -1\n", cChar); TRACE("keycode for '%c' not found, returning -1\n", cChar);
return -1; return -1;
} }
@ -1953,7 +1946,7 @@ SHORT X11DRV_VkKeyScanEx( WCHAR wChar, HKL hkl )
for (index = 0; index < 4; index++) /* find shift state */ for (index = 0; index < 4; index++) /* find shift state */
if (keycode_to_keysym(display, keycode, index) == keysym) break; if (keycode_to_keysym(display, keycode, index) == keysym) break;
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
switch (index) switch (index)
{ {
@ -1990,7 +1983,7 @@ UINT X11DRV_MapVirtualKeyEx( UINT wCode, UINT wMapType, HKL hkl )
if (!match_x11_keyboard_layout(hkl)) if (!match_x11_keyboard_layout(hkl))
FIXME("keyboard layout %p is not supported\n", hkl); FIXME("keyboard layout %p is not supported\n", hkl);
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
switch(wMapType) switch(wMapType)
{ {
@ -2116,7 +2109,7 @@ UINT X11DRV_MapVirtualKeyEx( UINT wCode, UINT wMapType, HKL hkl )
break; break;
} }
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
TRACE( "returning 0x%x.\n", ret ); TRACE( "returning 0x%x.\n", ret );
return ret; return ret;
} }
@ -2193,7 +2186,7 @@ INT X11DRV_GetKeyNameText( LONG lParam, LPWSTR lpBuffer, INT nSize )
/* let's do scancode -> keycode -> keysym -> String */ /* let's do scancode -> keycode -> keysym -> String */
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
for (keyi=min_keycode; keyi<=max_keycode; keyi++) for (keyi=min_keycode; keyi<=max_keycode; keyi++)
if ((keyc2scan[keyi]) == scanCode) if ((keyc2scan[keyi]) == scanCode)
@ -2211,7 +2204,7 @@ INT X11DRV_GetKeyNameText( LONG lParam, LPWSTR lpBuffer, INT nSize )
char* idx = strrchr(name, '_'); char* idx = strrchr(name, '_');
if (idx && (_strnicmp(idx, "_r", -1) == 0 || _strnicmp(idx, "_l", -1) == 0)) if (idx && (_strnicmp(idx, "_r", -1) == 0 || _strnicmp(idx, "_l", -1) == 0))
{ {
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
TRACE("found scan=%04x keyc=%u keysym=%lx modified_string=%s\n", TRACE("found scan=%04x keyc=%u keysym=%lx modified_string=%s\n",
scanCode, keyc, keys, debugstr_an(name,idx-name)); scanCode, keyc, keys, debugstr_an(name,idx-name));
rc = MultiByteToWideChar(CP_UNIXCP, 0, name, idx-name+1, lpBuffer, nSize); rc = MultiByteToWideChar(CP_UNIXCP, 0, name, idx-name+1, lpBuffer, nSize);
@ -2223,7 +2216,7 @@ INT X11DRV_GetKeyNameText( LONG lParam, LPWSTR lpBuffer, INT nSize )
if (name) if (name)
{ {
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
TRACE("found scan=%04x keyc=%u keysym=%04x vkey=%04x string=%s\n", TRACE("found scan=%04x keyc=%u keysym=%04x vkey=%04x string=%s\n",
scanCode, keyc, (int)keys, vkey, debugstr_a(name)); scanCode, keyc, (int)keys, vkey, debugstr_a(name));
rc = MultiByteToWideChar(CP_UNIXCP, 0, name, -1, lpBuffer, nSize); rc = MultiByteToWideChar(CP_UNIXCP, 0, name, -1, lpBuffer, nSize);
@ -2235,7 +2228,7 @@ INT X11DRV_GetKeyNameText( LONG lParam, LPWSTR lpBuffer, INT nSize )
/* Finally issue WARN for unknown keys */ /* Finally issue WARN for unknown keys */
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
WARN("(%08x,%p,%d): unsupported key, vkey=%04X, ansi=%04x\n",lParam,lpBuffer,nSize,vkey,ansi); WARN("(%08x,%p,%d): unsupported key, vkey=%04X, ansi=%04x\n",lParam,lpBuffer,nSize,vkey,ansi);
*lpBuffer = 0; *lpBuffer = 0;
return 0; return 0;
@ -2381,7 +2374,7 @@ INT X11DRV_ToUnicodeEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
e.window = X11DRV_get_whole_window( focus ); e.window = X11DRV_get_whole_window( focus );
xic = X11DRV_get_ic( focus ); xic = X11DRV_get_ic( focus );
EnterCriticalSection( &kbd_section ); pthread_mutex_lock( &kbd_mutex );
if (lpKeyState[VK_SHIFT] & 0x80) if (lpKeyState[VK_SHIFT] & 0x80)
{ {
@ -2452,7 +2445,7 @@ INT X11DRV_ToUnicodeEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
if (!e.keycode && virtKey != VK_NONAME) if (!e.keycode && virtKey != VK_NONAME)
{ {
WARN_(key)("Unknown virtual key %X !!!\n", virtKey); WARN_(key)("Unknown virtual key %X !!!\n", virtKey);
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
return 0; return 0;
} }
else TRACE_(key)("Found keycode %u\n",e.keycode); else TRACE_(key)("Found keycode %u\n",e.keycode);
@ -2473,7 +2466,7 @@ INT X11DRV_ToUnicodeEx( UINT virtKey, UINT scanCode, const BYTE *lpKeyState,
if (lpChar == NULL) if (lpChar == NULL)
{ {
ERR_(key)("Failed to allocate memory!\n"); ERR_(key)("Failed to allocate memory!\n");
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
return 0; return 0;
} }
ret = XmbLookupString(xic, &e, lpChar, ret, &keysym, &status); ret = XmbLookupString(xic, &e, lpChar, ret, &keysym, &status);
@ -2625,7 +2618,7 @@ found:
if (buf != lpChar) if (buf != lpChar)
HeapFree(GetProcessHeap(), 0, lpChar); HeapFree(GetProcessHeap(), 0, lpChar);
LeaveCriticalSection( &kbd_section ); pthread_mutex_unlock( &kbd_mutex );
/* Null-terminate the buffer, if there's room. MSDN clearly states that the /* Null-terminate the buffer, if there's room. MSDN clearly states that the
caller must not assume this is done, but some programs (e.g. Audiosurf) do. */ caller must not assume this is done, but some programs (e.g. Audiosurf) do. */