diff --git a/src/Fl_cocoa.mm b/src/Fl_cocoa.mm index 68fbdd060..01f118a6c 100644 --- a/src/Fl_cocoa.mm +++ b/src/Fl_cocoa.mm @@ -1737,6 +1737,27 @@ void Fl_Darwin_System_Driver::open_callback(void (*cb)(const char *)) { } */ +static void foreground_and_activate_if_needed() { + if ([NSApp isActive]) return; + NSBundle *bundle = [NSBundle mainBundle]; + if (bundle) { + NSString *exe = [[bundle executablePath] stringByStandardizingPath]; + NSString *bpath = [bundle bundlePath]; + NSString *exe_dir = [exe stringByDeletingLastPathComponent]; + if ([bpath isEqualToString:exe] || [bpath isEqualToString:exe_dir]) bundle = nil; + } + if ( !bundle ) { // only transform the application type for unbundled apps + ProcessSerialNumber cur_psn = { 0, kCurrentProcess }; + TransformProcessType(&cur_psn, kProcessTransformToForegroundApplication); // needs Mac OS 10.3 + /* support of Mac OS 10.2 or earlier used this undocumented call instead + err = CPSEnableForegroundOperation(&cur_psn, 0x03, 0x3C, 0x2C, 0x1103); + */ + } + [NSApp activateIgnoringOtherApps:YES]; +} + +// this form tested OK on MacOS 10.3 10.6 10.9 and 10.13 + void Fl_Cocoa_Screen_Driver::open_display_platform() { static char beenHereDoneThat = 0; if ( !beenHereDoneThat ) { @@ -1750,7 +1771,7 @@ void Fl_Cocoa_Screen_Driver::open_display_platform() { [(NSApplication*)NSApp setDelegate:[delegate init]]; if (need_new_nsapp) { if (fl_mac_os_version >= 101300 ) { - [NSApp activateIgnoringOtherApps:YES]; // necessary to run app from command line + foreground_and_activate_if_needed(); in_nsapp_run = true; [NSApp run]; in_nsapp_run = false; @@ -1766,45 +1787,7 @@ void Fl_Cocoa_Screen_Driver::open_display_platform() { dequeue:YES]; while (ign_event); - // bring the application into foreground without a 'CARB' resource - bool i_am_in_front; - ProcessSerialNumber cur_psn = { 0, kCurrentProcess }; -#if MAC_OS_X_VERSION_MAX_ALLOWED >= MAC_OS_X_VERSION_10_6 - if (fl_mac_os_version >= 100600) { - i_am_in_front = [[NSRunningApplication currentApplication] isActive]; - } - else -#endif - { - Boolean same_psn; - ProcessSerialNumber front_psn; - //avoid compilation warnings triggered by GetFrontProcess() and SameProcess() - void* h = dlopen(NULL, RTLD_LAZY); - typedef OSErr (*GetFrontProcess_type)(ProcessSerialNumber*); - GetFrontProcess_type GetFrontProcess_ = (GetFrontProcess_type)dlsym(h, "GetFrontProcess"); - typedef OSErr (*SameProcess_type)(ProcessSerialNumber*, ProcessSerialNumber*, Boolean*); - SameProcess_type SameProcess_ = (SameProcess_type)dlsym(h, "SameProcess"); - i_am_in_front = (!GetFrontProcess_( &front_psn ) && - !SameProcess_( &front_psn, &cur_psn, &same_psn ) && same_psn ); - } - if (!i_am_in_front) { - // only transform the application type for unbundled apps - NSBundle *bundle = [NSBundle mainBundle]; - if (bundle) { - NSString *exe = [[bundle executablePath] stringByStandardizingPath]; - NSString *bpath = [bundle bundlePath]; - NSString *exe_dir = [exe stringByDeletingLastPathComponent]; - if ([bpath isEqualToString:exe] || [bpath isEqualToString:exe_dir]) bundle = nil; - } - - if ( !bundle ) { - TransformProcessType(&cur_psn, kProcessTransformToForegroundApplication); // needs Mac OS 10.3 - /* support of Mac OS 10.2 or earlier used this undocumented call instead - err = CPSEnableForegroundOperation(&cur_psn, 0x03, 0x3C, 0x2C, 0x1103); - */ - } - [NSApp activateIgnoringOtherApps:YES]; - } + foreground_and_activate_if_needed(); if (![NSApp servicesMenu]) createAppleMenu(); main_screen_height = [[[NSScreen screens] objectAtIndex:0] frame].size.height; [[NSNotificationCenter defaultCenter] addObserver:[FLWindowDelegate singleInstance]