Bläddra i källkod

libobs: Add local event monitor for hotkey thread

To avoid a mismatch between the state of pressed keys as held by the
hotkey thread and keys pressed while the application is in focus, local
key events need to be monitored as well (even though they are already
handled by Cocoa's main event loop).
PatTheMav 2 år sedan
förälder
incheckning
1b194c8ebd
1 ändrade filer med 17 tillägg och 1 borttagningar
  1. 17 1
      libobs/obs-cocoa.m

+ 17 - 1
libobs/obs-cocoa.m

@@ -160,6 +160,7 @@ static bool dstr_from_cfstring(struct dstr *str, CFStringRef ref)
 struct obs_hotkeys_platform {
 struct obs_hotkeys_platform {
     volatile long refs;
     volatile long refs;
     CFTypeRef monitor;
     CFTypeRef monitor;
+    CFTypeRef local_monitor;
     bool is_key_down[OBS_KEY_LAST_VALUE];
     bool is_key_down[OBS_KEY_LAST_VALUE];
     TISInputSourceRef tis;
     TISInputSourceRef tis;
     CFDataRef layout_data;
     CFDataRef layout_data;
@@ -674,6 +675,16 @@ static bool init_hotkeys_platform(obs_hotkeys_platform_t **plat_)
         [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged
         [NSEvent addGlobalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged
                                                handler:handler];
                                                handler:handler];
 
 
+    NSEvent *_Nullable (^local_handler)(NSEvent *event) = ^NSEvent *_Nullable(NSEvent *event)
+    {
+        handle_monitor_event(plat, event);
+
+        return event;
+    };
+    plat->local_monitor = (__bridge CFTypeRef)
+        [NSEvent addLocalMonitorForEventsMatchingMask:NSEventMaskKeyDown | NSEventMaskKeyUp | NSEventMaskFlagsChanged
+                                              handler:local_handler];
+
     plat->tis = TISCopyCurrentKeyboardLayoutInputSource();
     plat->tis = TISCopyCurrentKeyboardLayoutInputSource();
     plat->layout_data = (CFDataRef) TISGetInputSourceProperty(plat->tis, kTISPropertyUnicodeKeyLayoutData);
     plat->layout_data = (CFDataRef) TISGetInputSourceProperty(plat->tis, kTISPropertyUnicodeKeyLayoutData);
 
 
@@ -699,10 +710,15 @@ static inline void free_hotkeys_platform(obs_hotkeys_platform_t *plat)
         return;
         return;
 
 
     if (plat->monitor) {
     if (plat->monitor) {
-        CFRelease(plat->monitor);
+        [NSEvent removeMonitor:(__bridge id _Nonnull)(plat->monitor)];
         plat->monitor = NULL;
         plat->monitor = NULL;
     }
     }
 
 
+    if (plat->local_monitor) {
+        [NSEvent removeMonitor:(__bridge id _Nonnull)(plat->local_monitor)];
+        plat->local_monitor = NULL;
+    }
+
     if (plat->tis) {
     if (plat->tis) {
         CFRelease(plat->tis);
         CFRelease(plat->tis);
         plat->tis = NULL;
         plat->tis = NULL;