From 025ea2fd6accebbc528201d0d6516ec06ec1b614 Mon Sep 17 00:00:00 2001 From: Alexandre Janniaux Date: Sun, 15 Jun 2025 08:26:02 +0200 Subject: [PATCH] macosx: handle termination through libvlc_Quit Note: this doesn't break quitting from the interface but SIGINT now requires two attempts to trigger quitting. This will be fixed in following commit. --- modules/gui/macosx/main/VLCApplication.h | 3 +++ modules/gui/macosx/main/VLCApplication.m | 21 ++++++++------------- modules/gui/macosx/main/VLCMain.m | 20 ++++++++++++++++---- modules/gui/macosx/menus/VLCStatusBarIcon.h | 2 ++ modules/gui/macosx/menus/VLCStatusBarIcon.m | 7 +++++-- 5 files changed, 34 insertions(+), 19 deletions(-) diff --git a/modules/gui/macosx/main/VLCApplication.h b/modules/gui/macosx/main/VLCApplication.h index 9f1e6efbb1..0e4e65d4d6 100644 --- a/modules/gui/macosx/main/VLCApplication.h +++ b/modules/gui/macosx/main/VLCApplication.h @@ -25,6 +25,8 @@ #import +typedef struct intf_thread_t intf_thread_t; + /***************************************************************************** * VLCApplication interface *****************************************************************************/ @@ -45,4 +47,5 @@ @property(strong, readonly) NSImage *vlcAppIconImage; @property(assign, readonly) BOOL winterHolidaysTheming; +- (void)setIntf:(intf_thread_t *)intf; @end diff --git a/modules/gui/macosx/main/VLCApplication.m b/modules/gui/macosx/main/VLCApplication.m index 02deafa0fb..b77b02925f 100644 --- a/modules/gui/macosx/main/VLCApplication.m +++ b/modules/gui/macosx/main/VLCApplication.m @@ -31,6 +31,7 @@ #import "extensions/NSString+Helpers.h" #import +#import /***************************************************************************** * VLCApplication implementation @@ -40,6 +41,7 @@ { NSURL *_appLocationURL; NSImage *_vlcAppIconImage; + intf_thread_t *_intf; } @end @@ -74,6 +76,11 @@ return self; } +- (void)setIntf:(intf_thread_t*)intf +{ + _intf = intf; +} + - (void)dealloc { [NSNotificationCenter.defaultCenter removeObserver:self]; @@ -140,19 +147,7 @@ - (void)terminate:(id)sender { [self activateIgnoringOtherApps:YES]; - [self stop:sender]; - - // Trigger event in loop to force evaluating the stop flag - NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined - location:NSMakePoint(0,0) - modifierFlags:0 - timestamp:0.0 - windowNumber:0 - context:nil - subtype:0 - data1:0 - data2:0]; - [NSApp postEvent:event atStart:YES]; + libvlc_Quit(vlc_object_instance(_intf)); } @end diff --git a/modules/gui/macosx/main/VLCMain.m b/modules/gui/macosx/main/VLCMain.m index 1967a6dfc9..6f0c8b758b 100644 --- a/modules/gui/macosx/main/VLCMain.m +++ b/modules/gui/macosx/main/VLCMain.m @@ -141,6 +141,7 @@ NSString * const kVLCPreferencesVersion = @"VLCPreferencesVersion"; static intf_thread_t *p_interface_thread; static vlc_preparser_t *p_network_preparser; +vlc_sem_t g_wait_quit; intf_thread_t *getIntf() { @@ -178,6 +179,7 @@ int OpenIntf (vlc_object_t *p_this) @try { VLCApplication * const application = VLCApplication.sharedApplication; NSCAssert(application != nil, @"VLCApplication must not be nil"); + [application setIntf:p_intf]; VLCMain * const main = VLCMain.sharedInstance; NSCAssert(main != nil, @"VLCMain must not be nil"); @@ -192,6 +194,7 @@ int OpenIntf (vlc_object_t *p_this) dispatch_semaphore_signal(sem); [NSApp run]; } + vlc_sem_post(&g_wait_quit); }); CFRunLoopWakeUp(CFRunLoopGetMain()); @@ -208,15 +211,24 @@ void CloseIntf (vlc_object_t *p_this) [VLCMain.sharedInstance applicationWillTerminate:nil]; [VLCMain killInstance]; } - p_interface_thread = nil; - vlc_preparser_Delete(p_network_preparser); - p_network_preparser = nil; + [NSApp stop:nil]; + NSEvent* event = [NSEvent otherEventWithType:NSApplicationDefined + location:NSMakePoint(0,0) + modifierFlags:0 + timestamp:0.0 + windowNumber:0 + context:nil + subtype:0 + data1:0 + data2:0]; + [NSApp postEvent:event atStart:YES]; }; if (CFRunLoopGetCurrent() == CFRunLoopGetMain()) release_intf(); else dispatch_sync(dispatch_get_main_queue(), release_intf); + vlc_sem_wait(&g_wait_quit); } /***************************************************************************** @@ -337,7 +349,7 @@ static VLCMain *sharedInstance = nil; [self migrateOldPreferences]; } - _statusBarIcon = [[VLCStatusBarIcon alloc] init]; + _statusBarIcon = [[VLCStatusBarIcon alloc] init:_p_intf]; /* on macOS 11 and later, check whether the user attempts to deploy * the x86_64 binary on ARM-64 - if yes, log it */ diff --git a/modules/gui/macosx/menus/VLCStatusBarIcon.h b/modules/gui/macosx/menus/VLCStatusBarIcon.h index 6fa76c5dea..64b23bd090 100644 --- a/modules/gui/macosx/menus/VLCStatusBarIcon.h +++ b/modules/gui/macosx/menus/VLCStatusBarIcon.h @@ -21,6 +21,7 @@ *****************************************************************************/ #import +#include @interface VLCStatusBarIcon : NSObject @@ -30,6 +31,7 @@ @property (strong) IBOutlet NSView *playbackInfoView; @property (strong) IBOutlet NSView *controlsView; +- (instancetype)init:(intf_thread_t *)intf; - (IBAction)statusBarIconShowMainWindow:(id)sender; - (IBAction)statusBarIconTogglePlayPause:(id)sender; - (IBAction)statusBarIconNext:(id)sender; diff --git a/modules/gui/macosx/menus/VLCStatusBarIcon.m b/modules/gui/macosx/menus/VLCStatusBarIcon.m index 4ccbd27db8..68b131b574 100644 --- a/modules/gui/macosx/menus/VLCStatusBarIcon.m +++ b/modules/gui/macosx/menus/VLCStatusBarIcon.m @@ -34,6 +34,8 @@ @interface VLCStatusBarIcon () { + intf_thread_t *_intf; + NSMenuItem *_vlcStatusBarMenuItem; /* Outlets for Now Playing labels */ @@ -71,10 +73,11 @@ #pragma mark - #pragma mark Init -- (instancetype)init +- (instancetype)init:(intf_thread_t *)intf; { self = [super init]; if (self) { + _intf = intf; [[NSBundle mainBundle] loadNibNamed:@"VLCStatusBarIconMainMenu" owner:self topLevelObjects:nil]; } return self; @@ -489,7 +492,7 @@ // Action: Quit VLC - (IBAction)quitAction:(id)sender { - [NSApplication.sharedApplication terminate:nil]; + libvlc_Quit(vlc_object_instance(_intf)); } - (IBAction)statusBarIconShowMiniAudioPlayer:(id)sender