[vlc-devel] [PATCH 1/1] osx_notifications: Use active state notifications for compatibility with OS X <10.10

epirat07 at gmail.com epirat07 at gmail.com
Fri Oct 23 12:13:23 CEST 2015


From: Marvin Scholz <epirat07 at gmail.com>

This restores the code that used notifications to determine the app state.
Additionally the inital state is not active, as we get a notification if the
app switches to active after opening.
---
 modules/notify/osx_notifications.m | 26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/modules/notify/osx_notifications.m b/modules/notify/osx_notifications.m
index 3871d7a..710ace0 100644
--- a/modules/notify/osx_notifications.m
+++ b/modules/notify/osx_notifications.m
@@ -74,6 +74,7 @@
     NSString *notificationType;
     NSMutableDictionary *registrationDictionary;
     id lastNotification;
+    bool isInForeground;
     intf_thread_t *interfaceThread;
 }
 
@@ -272,6 +273,21 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
     if( !( self = [super init] ) )
         return nil;
 
+    @autoreleasepool {
+        // Subscribe to notifications to determine if VLC is in foreground or not
+        [[NSNotificationCenter defaultCenter] addObserver:self
+                                                 selector:@selector(applicationActiveChange:)
+                                                     name:NSApplicationDidBecomeActiveNotification
+                                                   object:nil];
+
+        [[NSNotificationCenter defaultCenter] addObserver:self
+                                                 selector:@selector(applicationActiveChange:)
+                                                     name:NSApplicationDidResignActiveNotification
+                                                   object:nil];
+    }
+    // Start in background
+    isInForeground = NO;
+
     applicationName = nil;
     notificationType = nil;
     registrationDictionary = nil;
@@ -290,6 +306,7 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
              removeDeliveredNotification:(NSUserNotification *)lastNotification];
             [lastNotification release];
         }
+        [[NSNotificationCenter defaultCenter] removeObserver:self];
     }
 #endif
 
@@ -329,7 +346,7 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
 {
     @autoreleasepool {
         // Do not notify if in foreground
-        if ([NSApplication sharedApplication].active)
+        if (isInForeground)
             return;
 
         // Init Cover
@@ -425,6 +442,13 @@ static int ItemChange( vlc_object_t *p_this, const char *psz_var,
     return applicationName;
 }
 
+- (void)applicationActiveChange:(NSNotification *)n {
+    if (n.name == NSApplicationDidBecomeActiveNotification)
+        isInForeground = YES;
+    else if (n.name == NSApplicationDidResignActiveNotification)
+        isInForeground = NO;
+}
+
 #if __MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
 - (void)userNotificationCenter:(NSUserNotificationCenter *)center
        didActivateNotification:(NSUserNotification *)notification
-- 
2.2.1



More information about the vlc-devel mailing list