]> git.sesse.net Git - vlc/blobdiff - modules/gui/macosx/intf.m
macosx: use CoreInteraction code for the media key triggers
[vlc] / modules / gui / macosx / intf.m
index b005ebcf0dc126bb5e2dc55e4b58dc80b932f532..f7e36d751283fbe11ef23fb0713b6fef958929c3 100644 (file)
@@ -97,6 +97,7 @@ int OpenIntf ( vlc_object_t *p_this )
 {
     NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
     [VLCApplication sharedApplication];
+
     intf_thread_t *p_intf = (intf_thread_t*) p_this;
 
     p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
@@ -106,9 +107,9 @@ int OpenIntf ( vlc_object_t *p_this )
     memset( p_intf->p_sys, 0, sizeof( *p_intf->p_sys ) );
 
     /* subscribe to LibVLCCore's messages */
-    p_intf->p_sys->p_sub = vlc_Subscribe( MsgCallback, NULL );
-    p_intf->pf_run = Run;
-    p_intf->b_should_run_on_first_thread = true;
+    vlc_Subscribe( &p_intf->p_sys->sub, MsgCallback, NULL );
+
+    Run( p_intf );
 
     [o_pool release];
     return VLC_SUCCESS;
@@ -169,7 +170,8 @@ static int WindowControl( vout_window_t *p_wnd, int i_query, va_list args )
     else if( i_query == VOUT_WINDOW_SET_FULLSCREEN )
     {
         NSAutoreleasePool *o_pool = [[NSAutoreleasePool alloc] init];
-        [[VLCMain sharedInstance] fullscreenChanged];
+        // we already have our playlist "fullscreen" callback, do not repeat the same call here
+        //[[VLCMain sharedInstance] performSelectorOnMainThread:@selector(fullscreenChanged) withObject: nil waitUntilDone: NO];
         [o_pool release];
     }
     else
@@ -204,6 +206,8 @@ static void Run( intf_thread_t *p_intf )
     [[VLCMain sharedInstance] applicationWillTerminate:nil];
     [o_appLock release];
     [o_pool release];
+
+    raise(SIGTERM);
 }
 
 #pragma mark -
@@ -625,6 +629,21 @@ static VLCMain *_o_sharedMainInstance = nil;
     if (i_value > 0)
         var_SetInteger( pl_Get( VLCIntf ), "audio-device", i_value );
 
+    if (config_GetInt( VLCIntf, "macosx-icon-change"))
+    {
+        /* After day 354 of the year, the usual VLC cone is replaced by another cone
+         * wearing a Father Xmas hat.
+         * Note: this icon doesn't represent an endorsement of The Coca-Cola Company.
+         */
+        NSCalendar *gregorian =
+        [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
+        NSUInteger dayOfYear = [gregorian ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:[NSDate date]];
+        [gregorian release];
+
+        if (dayOfYear >= 354)
+            [[VLCApplication sharedApplication] setApplicationIconImage: [NSImage imageNamed:@"vlc-xmas"]];
+    }
+
     nib_main_loaded = TRUE;
 }
 
@@ -632,6 +651,8 @@ static VLCMain *_o_sharedMainInstance = nil;
 {
     if( !p_intf ) return;
 
+    [self updateCurrentlyUsedHotkeys];
+
     [o_mainwindow updateWindow];
     [o_mainwindow updateTimeSlider];
     [o_mainwindow updateVolumeSlider];
@@ -775,7 +796,7 @@ static VLCMain *_o_sharedMainInstance = nil;
     [o_eyetv release];
 
     /* unsubscribe from libvlc's debug messages */
-    vlc_Unsubscribe( p_intf->p_sys->p_sub );
+    vlc_Unsubscribe( &p_intf->p_sys->sub );
 
     [o_msg_arr removeAllObjects];
     [o_msg_arr release];
@@ -824,15 +845,15 @@ static VLCMain *_o_sharedMainInstance = nil;
         int keyRepeat = (keyFlags & 0x1);
 
         if( keyCode == NX_KEYTYPE_PLAY && keyState == 0 )
-            var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_PLAY_PAUSE );
+            [[VLCCoreInteraction sharedInstance] play];
 
         if( (keyCode == NX_KEYTYPE_FAST || keyCode == NX_KEYTYPE_NEXT) && !b_mediakeyJustJumped )
         {
             if( keyState == 0 && keyRepeat == 0 )
-                var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_NEXT );
+                [[VLCCoreInteraction sharedInstance] next];
             else if( keyRepeat == 1 )
             {
-                var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_FORWARD_SHORT );
+                [[VLCCoreInteraction sharedInstance] forwardShort];
                 b_mediakeyJustJumped = YES;
                 [self performSelector:@selector(resetMediaKeyJump)
                            withObject: NULL
@@ -843,10 +864,10 @@ static VLCMain *_o_sharedMainInstance = nil;
         if( (keyCode == NX_KEYTYPE_REWIND || keyCode == NX_KEYTYPE_PREVIOUS) && !b_mediakeyJustJumped )
         {
             if( keyState == 0 && keyRepeat == 0 )
-                var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_PREV );
+                [[VLCCoreInteraction sharedInstance] previous];
             else if( keyRepeat == 1 )
             {
-                var_SetInteger( p_intf->p_libvlc, "key-action", ACTIONID_JUMP_BACKWARD_SHORT );
+                [[VLCCoreInteraction sharedInstance] backwardShort];
                 b_mediakeyJustJumped = YES;
                 [self performSelector:@selector(resetMediaKeyJump)
                            withObject: NULL
@@ -1276,8 +1297,8 @@ unsigned int CocoaKeyToVLC( unichar i_key )
     unsigned int i_pressed_modifiers = 0;
     const struct hotkey *p_hotkeys;
     int i;
-    NSMutableString *tempString = [[[NSMutableString alloc] init] autorelease];
-    NSMutableString *tempStringPlus = [[[NSMutableString alloc] init] autorelease];
+    NSMutableString *tempString = [[NSMutableString alloc] init];
+    NSMutableString *tempStringPlus = [[NSMutableString alloc] init];
 
     val.i_int = 0;
     p_hotkeys = p_intf->p_libvlc->p_hotkeys;
@@ -1314,6 +1335,8 @@ unsigned int CocoaKeyToVLC( unichar i_key )
     if( key == 'f' && i_pressed_modifiers & NSControlKeyMask && i_pressed_modifiers & NSCommandKeyMask )
     {
         [[VLCCoreInteraction sharedInstance] toggleFullscreen];
+        [tempString release];
+        [tempStringPlus release];
         return YES;
     }
 
@@ -1329,12 +1352,16 @@ unsigned int CocoaKeyToVLC( unichar i_key )
         case NSLeftArrowFunctionKey:
         case NSEnterCharacter:
         case NSCarriageReturnCharacter:
+            [tempString release];
+            [tempStringPlus release];
             return NO;
     }
 
     if( key == 0x0020 ) // space key
     {
         [[VLCCoreInteraction sharedInstance] play];
+        [tempString release];
+        [tempStringPlus release];
         return YES;
     }
 
@@ -1343,9 +1370,13 @@ unsigned int CocoaKeyToVLC( unichar i_key )
     if( [o_usedHotkeys indexOfObject: tempString] != NSNotFound || [o_usedHotkeys indexOfObject: tempStringPlus] != NSNotFound )
     {
         var_SetInteger( p_intf->p_libvlc, "key-pressed", val.i_int );
+        [tempString release];
+        [tempStringPlus release];
         return YES;
     }
 
+    [tempString release];
+    [tempStringPlus release];
     return NO;
 }
 
@@ -1373,7 +1404,8 @@ unsigned int CocoaKeyToVLC( unichar i_key )
         }
     }
     module_config_free (p_config);
-    o_usedHotkeys = [[NSArray alloc] initWithArray: o_usedHotkeys copyItems: YES];
+    o_usedHotkeys = [[NSArray alloc] initWithArray: o_tempArray copyItems: YES];
+    [o_tempArray release];
 }
 
 #pragma mark -
@@ -1385,7 +1417,11 @@ unsigned int CocoaKeyToVLC( unichar i_key )
 
     if (b_nativeFullscreenMode)
     {
-        [o_mainwindow toggleFullScreen: self];
+        // this is called twice in certain situations, so only toogle if we really need to
+        if( (  b_fullscreen && !([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen) ) || 
+            ( !b_fullscreen &&  ([NSApp currentSystemPresentationOptions] & NSApplicationPresentationFullScreen) ) )
+            [o_mainwindow toggleFullScreen: self];
+
         if(b_fullscreen)
             [NSApp setPresentationOptions:(NSApplicationPresentationFullScreen | NSApplicationPresentationAutoHideDock | NSApplicationPresentationAutoHideMenuBar)];
         else
@@ -1718,7 +1754,7 @@ unsigned int CocoaKeyToVLC( unichar i_key )
     b_active_videoplayback = b_value;
     if( o_mainwindow )
     {
-        [o_mainwindow performSelectorOnMainThread:@selector(setVideoplayEnabled) withObject:nil waitUntilDone:NO];
+        [o_mainwindow performSelectorOnMainThread:@selector(setVideoplayEnabled) withObject:nil waitUntilDone:YES];
         [o_mainwindow performSelectorOnMainThread:@selector(togglePlaylist:) withObject:nil waitUntilDone:NO];
     }
 }
@@ -1876,38 +1912,10 @@ unsigned int CocoaKeyToVLC( unichar i_key )
 
     if( version == 1 )
     {
-        NSMutableString * o_workString;
-        NSRange returnedRange;
-        NSRange fullRange;
-        BOOL b_needsRestart = NO;
-
-        #define fixpref( pref ) \
-        o_workString = [[NSMutableString alloc] initWithFormat:@"%s", config_GetPsz( VLCIntf, pref )]; \
-        if ([o_workString length] > 0) \
-        { \
-            returnedRange = [o_workString rangeOfString:@"macosx" options: NSCaseInsensitiveSearch]; \
-            if (returnedRange.location != NSNotFound) \
-            { \
-                fullRange = NSMakeRange( 0, [o_workString length] ); \
-                [o_workString replaceOccurrencesOfString:@":macosx" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
-                fullRange = NSMakeRange( 0, [o_workString length] ); \
-                [o_workString replaceOccurrencesOfString:@"macosx:" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
-                fullRange = NSMakeRange( 0, [o_workString length] ); \
-                [o_workString replaceOccurrencesOfString:@"macosx" withString:@"" options: NSCaseInsensitiveSearch range: fullRange]; \
-                config_PutPsz( VLCIntf, pref, [o_workString UTF8String] ); \
-                b_needsRestart = YES; \
-            } \
-        } \
-        [o_workString release]
-
-        fixpref( "control" );
-        fixpref( "extraintf" );
-        #undef fixpref
-
         [[NSUserDefaults standardUserDefaults] setInteger:kCurrentPreferencesVersion forKey:kVLCPreferencesVersion];
         [[NSUserDefaults standardUserDefaults] synchronize];
 
-        if (!b_needsRestart)
+        if (![[VLCCoreInteraction sharedInstance] fixPreferences])
             return;
         else
             config_SaveConfigFile( VLCIntf ); // we need to do manually, since we won't quit libvlc cleanly
@@ -1936,7 +1944,7 @@ unsigned int CocoaKeyToVLC( unichar i_key )
             return;
         }
 
-        NSArray * ourPreferences = [NSArray arrayWithObjects:@"org.videolan.vlc.plist", @"VLC", nil];
+        NSArray * ourPreferences = [NSArray arrayWithObjects:@"org.videolan.vlc.plist", @"VLC", @"org.videolan.vlc", nil];
 
         /* Move the file to trash so that user can find them later */
         [[NSWorkspace sharedWorkspace] performFileOperation:NSWorkspaceRecycleOperation source:preferences destination:nil files:ourPreferences tag:0];
@@ -2106,30 +2114,14 @@ unsigned int CocoaKeyToVLC( unichar i_key )
 - (void)coreChangedMediaKeySupportSetting: (NSNotification *)o_notification
 {
     b_mediaKeySupport = config_GetInt( VLCIntf, "macosx-mediakeys" );
-    if (b_mediaKeySupport) {
+    if (b_mediaKeySupport)
+    {
         if (!o_mediaKeyController)
             o_mediaKeyController = [[SPMediaKeyTap alloc] initWithDelegate:self];
         [o_mediaKeyController startWatchingMediaKeys];
     }
     else if (!b_mediaKeySupport && o_mediaKeyController)
-    {
-        int returnedValue = NSRunInformationalAlertPanel(_NS("Relaunch required"),
-                                               _NS("To make sure that VLC no longer listens to your media key events, it needs to be restarted."),
-                                               _NS("Relaunch VLC"), _NS("Ignore"), nil, nil);
-        if( returnedValue == NSOKButton )
-        {
-            /* Relaunch now */
-            const char * path = [[[NSBundle mainBundle] executablePath] UTF8String];
-
-            /* For some reason we need to fork(), not just execl(), which reports a ENOTSUP then. */
-            if(fork() != 0)
-            {
-                exit(0);
-                return;
-            }
-            execl(path, path, NULL);
-        }
-    }
+        [o_mediaKeyController stopWatchingMediaKeys];
 }
 
 @end
@@ -2142,7 +2134,7 @@ unsigned int CocoaKeyToVLC( unichar i_key )
 // when user selects the quit menu from dock it sends a terminate:
 // but we need to send a stop: to properly exits libvlc.
 // However, we are not able to change the action-method sent by this standard menu item.
-// thus we override terminat: to send a stop:
+// thus we override terminate: to send a stop:
 // see [af97f24d528acab89969d6541d83f17ce1ecd580] that introduced the removal of setjmp() and longjmp()
 - (void)terminate:(id)sender
 {