Skip to content
This repository was archived by the owner on Aug 31, 2021. It is now read-only.

Commit 384238a

Browse files
author
Monte Goulding
authored
Merge pull request #6615 from montegoulding/tistsm
[[ Bug ]] Move TISCopyCurrentKeyboardInputSource to main thread
2 parents b73cbb7 + 155a61c commit 384238a

1 file changed

Lines changed: 66 additions & 40 deletions

File tree

engine/src/mac-abort.mm

Lines changed: 66 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,58 @@ static CGEventRef abort_key_callback(CGEventTapProxy p_proxy, CGEventType p_type
151151
}
152152
#endif
153153

154+
static void update_keyboard_input_source()
155+
{
156+
// Update our period key mapping if the input source has changed / hasn't
157+
// been initialized.
158+
TISInputSourceRef t_input_source;
159+
t_input_source = TISCopyCurrentKeyboardInputSource();
160+
if (t_input_source != s_current_input_source || s_current_input_source == nil)
161+
{
162+
if (s_current_input_source != nil)
163+
CFRelease(s_current_input_source);
164+
165+
s_current_input_source = t_input_source;
166+
s_current_period_keycode = 0xffff;
167+
s_current_period_needs_shift = false;
168+
169+
// If we have a valid keyboard input source then resolve '.'.
170+
if (s_current_input_source != nil)
171+
{
172+
// Loop through all possible keycodes and map with no-shift and shift
173+
// to see if we can find our '.' key.
174+
for(uindex_t i = 0; i < 127; i++)
175+
{
176+
unichar t_char;
177+
t_char = map_keycode_to_char(t_input_source, i, false);
178+
if (t_char == '.')
179+
{
180+
s_current_period_keycode = i;
181+
s_current_period_needs_shift = false;
182+
break;
183+
}
184+
185+
t_char = map_keycode_to_char(t_input_source, i, true);
186+
if (t_char == '.')
187+
{
188+
s_current_period_keycode = i;
189+
s_current_period_needs_shift = true;
190+
break;
191+
}
192+
}
193+
}
194+
}
195+
}
196+
197+
void update_keyboard_input_source_callback(CFNotificationCenterRef p_center,
198+
void *p_observer,
199+
CFStringRef p_name,
200+
const void *p_object,
201+
CFDictionaryRef p_userInfo)
202+
{
203+
update_keyboard_input_source();
204+
}
205+
154206
static void abort_key_timer_callback(CFRunLoopTimerRef p_timer, void *p_info)
155207
{
156208
if (s_abort_key_disabled > 0)
@@ -159,46 +211,6 @@ static void abort_key_timer_callback(CFRunLoopTimerRef p_timer, void *p_info)
159211
}
160212

161213
s_abort_key_checked = false;
162-
163-
// Update our period key mapping if the input source has changed / hasn't
164-
// been initialized.
165-
TISInputSourceRef t_input_source;
166-
t_input_source = TISCopyCurrentKeyboardInputSource();
167-
if (t_input_source != s_current_input_source || s_current_input_source == nil)
168-
{
169-
if (s_current_input_source != nil)
170-
CFRelease(s_current_input_source);
171-
172-
s_current_input_source = t_input_source;
173-
s_current_period_keycode = 0xffff;
174-
s_current_period_needs_shift = false;
175-
176-
// If we have a valid keyboard input source then resolve '.'.
177-
if (s_current_input_source != nil)
178-
{
179-
// Loop through all possible keycodes and map with no-shift and shift
180-
// to see if we can find our '.' key.
181-
for(uindex_t i = 0; i < 127; i++)
182-
{
183-
unichar t_char;
184-
t_char = map_keycode_to_char(t_input_source, i, false);
185-
if (t_char == '.')
186-
{
187-
s_current_period_keycode = i;
188-
s_current_period_needs_shift = false;
189-
break;
190-
}
191-
192-
t_char = map_keycode_to_char(t_input_source, i, true);
193-
if (t_char == '.')
194-
{
195-
s_current_period_keycode = i;
196-
s_current_period_needs_shift = true;
197-
break;
198-
}
199-
}
200-
}
201-
}
202214

203215
// If we successfully found period - we must now check the state of the keys.
204216
if (s_current_period_keycode != 0xffff)
@@ -343,6 +355,15 @@ bool MCPlatformInitializeAbortKey(void)
343355
if (!AXAPIEnabled())
344356
return true;
345357
#endif
358+
359+
update_keyboard_input_source();
360+
361+
CFNotificationCenterAddObserver(CFNotificationCenterGetDistributedCenter(),
362+
nullptr,
363+
update_keyboard_input_source_callback,
364+
kTISNotifySelectedKeyboardInputSourceChanged,
365+
nullptr,
366+
CFNotificationSuspensionBehaviorDeliverImmediately);
346367

347368
s_abort_key_thread = [[MCAbortKeyThread alloc] init];
348369
[s_abort_key_thread start];
@@ -351,6 +372,11 @@ bool MCPlatformInitializeAbortKey(void)
351372

352373
void MCPlatformFinalizeAbortKey(void)
353374
{
375+
CFNotificationCenterRemoveObserver(CFNotificationCenterGetDistributedCenter(),
376+
nullptr,
377+
kTISNotifySelectedKeyboardInputSourceChanged,
378+
nullptr);
379+
354380
[s_abort_key_thread terminate];
355381
[s_abort_key_thread release];
356382
s_abort_key_thread = nil;

0 commit comments

Comments
 (0)