From 663af2f17eec2cc064f3d1e25e00fb840c441964 Mon Sep 17 00:00:00 2001 From: Jeremy Huddleston Sequoia Date: Sun, 12 Jun 2022 22:06:01 -0700 Subject: [PATCH] X11Application: Ensure TIS operations are done on the main thread Fixes: https://github.com/XQuartz/XQuartz/issues/205 Signed-off-by: Jeremy Huddleston Sequoia (cherry picked from commit b1afcecc61d841f95e786e4f4f84184f91d149f1) --- hw/xquartz/X11Application.m | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/hw/xquartz/X11Application.m b/hw/xquartz/X11Application.m index 3db0b2487..a100f5a6f 100644 --- a/hw/xquartz/X11Application.m +++ b/hw/xquartz/X11Application.m @@ -1701,8 +1701,17 @@ handle_mouse: } if (darwinSyncKeymap) { - TISInputSourceRef key_layout = - TISCopyCurrentKeyboardLayoutInputSource(); + __block TISInputSourceRef key_layout; + dispatch_block_t copyCurrentKeyboardLayoutInputSource = ^{ + key_layout = TISCopyCurrentKeyboardLayoutInputSource(); + }; + /* This is an ugly ant-pattern, but it is more expedient to address the problem right now. */ + if (pthread_main_np()) { + copyCurrentKeyboardLayoutInputSource(); + } else { + dispatch_sync(dispatch_get_main_queue(), copyCurrentKeyboardLayoutInputSource); + } + TISInputSourceRef clear; if (CFEqual(key_layout, last_key_layout)) { CFRelease(key_layout);