comparison mac/dw.m @ 2351:0c8d769df97e

Mac: Added HAVE_AVAILABLE macro when building on a system with @available(). Use HAVE_AVAILABLE whenever we use @availabe() to test for availability instead of the macro for the code itself. Not sure if this will work, but if it does it will allow older systems to build for newer features. Also add ability to return the PID from dw_exec() in certain instances instead of just returning DW_ERROR_NONE when possible.
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Fri, 05 Mar 2021 04:30:20 +0000
parents 5ddc447fb367
children 360f3cb6bd57
comparison
equal deleted inserted replaced
2350:5ddc447fb367 2351:0c8d769df97e
99 #define DWEventMaskLeftMouseDown NSEventMaskLeftMouseDown 99 #define DWEventMaskLeftMouseDown NSEventMaskLeftMouseDown
100 #define DWEventMaskRightMouseUp NSEventMaskRightMouseUp 100 #define DWEventMaskRightMouseUp NSEventMaskRightMouseUp
101 #define DWEventMaskRightMouseDown NSEventMaskRightMouseDown 101 #define DWEventMaskRightMouseDown NSEventMaskRightMouseDown
102 #define DWWindowStyleMaskResizable NSWindowStyleMaskResizable 102 #define DWWindowStyleMaskResizable NSWindowStyleMaskResizable
103 #define BUILDING_FOR_SIERRA 103 #define BUILDING_FOR_SIERRA
104 #define HAVE_AVAILABLE
104 #else 105 #else
105 #define DWButtonTypeSwitch NSSwitchButton 106 #define DWButtonTypeSwitch NSSwitchButton
106 #define DWButtonTypeRadio NSRadioButton 107 #define DWButtonTypeRadio NSRadioButton
107 #define DWButtonTypeMomentaryPushIn NSMomentaryPushInButton 108 #define DWButtonTypeMomentaryPushIn NSMomentaryPushInButton
108 #define DWBezelStyleRegularSquare NSRegularSquareBezelStyle 109 #define DWBezelStyleRegularSquare NSRegularSquareBezelStyle
434 } 435 }
435 436
436 /* Returns TRUE of Mojave or later is in Dark Mode */ 437 /* Returns TRUE of Mojave or later is in Dark Mode */
437 BOOL _is_dark(id object) 438 BOOL _is_dark(id object)
438 { 439 {
439 #ifdef BUILDING_FOR_MOJAVE 440 #ifdef HAVE_AVAILABLE
440 NSAppearance *appearance = [object effectiveAppearance]; 441 NSAppearance *appearance = [object effectiveAppearance];
441 442
442 if(@available(macOS 10.14, *)) 443 if(@available(macOS 10.14, *))
443 { 444 {
444 NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[ 445 NSAppearanceName basicAppearance = [appearance bestMatchFromAppearancesWithNames:@[
1463 return NSTerminateNow; 1464 return NSTerminateNow;
1464 } 1465 }
1465 #if defined(BUILDING_FOR_MOUNTAIN_LION) && !defined(BUILDING_FOR_BIG_SUR) 1466 #if defined(BUILDING_FOR_MOUNTAIN_LION) && !defined(BUILDING_FOR_BIG_SUR)
1466 -(void)applicationDidFinishLaunching:(NSNotification *)aNotification 1467 -(void)applicationDidFinishLaunching:(NSNotification *)aNotification
1467 { 1468 {
1468 #ifdef BUILDING_FOR_MOJAVE 1469 #ifdef HAVE_AVAILABLE
1469 if (@available(macOS 10.14, *)) {} else 1470 if (@available(macOS 10.14, *)) {} else
1470 #endif 1471 #endif
1471 { 1472 {
1472 NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter]; 1473 NSUserNotificationCenter* unc = [NSUserNotificationCenter defaultUserNotificationCenter];
1473 unc.delegate = self; 1474 unc.delegate = self;
11145 va_start(args, description); 11146 va_start(args, description);
11146 vsnprintf(outbuf, 1024, description, args); 11147 vsnprintf(outbuf, 1024, description, args);
11147 va_end(args); 11148 va_end(args);
11148 } 11149 }
11149 11150
11150 #ifdef BUILDING_FOR_MOJAVE 11151 #ifdef HAVE_AVAILABLE
11151 /* Configure the notification's payload. */ 11152 /* Configure the notification's payload. */
11152 if (@available(macOS 10.14, *)) 11153 if (@available(macOS 10.14, *))
11153 { 11154 {
11154 if([[NSBundle mainBundle] bundleIdentifier] != nil) 11155 if([[NSBundle mainBundle] bundleIdentifier] != nil)
11155 { 11156 {
11210 #ifdef BUILDING_FOR_MOUNTAIN_LION 11211 #ifdef BUILDING_FOR_MOUNTAIN_LION
11211 if(notification) 11212 if(notification)
11212 { 11213 {
11213 NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", DW_POINTER_TO_ULONGLONG(notification)]; 11214 NSString *notid = [NSString stringWithFormat:@"dw-notification-%llu", DW_POINTER_TO_ULONGLONG(notification)];
11214 11215
11215 #ifdef BUILDING_FOR_MOJAVE 11216 #ifdef HAVE_AVAILABLE
11216 /* Schedule the notification. */ 11217 /* Schedule the notification. */
11217 if (@available(macOS 10.14, *)) 11218 if (@available(macOS 10.14, *))
11218 { 11219 {
11219 if([[NSBundle mainBundle] bundleIdentifier] != nil) 11220 if([[NSBundle mainBundle] bundleIdentifier] != nil)
11220 { 11221 {
12525 [DWMainMenu retain]; 12526 [DWMainMenu retain];
12526 [DWApp setMainMenu:DWMainMenu]; 12527 [DWApp setMainMenu:DWMainMenu];
12527 DWObj = [[DWObject alloc] init]; 12528 DWObj = [[DWObject alloc] init];
12528 DWDefaultFont = nil; 12529 DWDefaultFont = nil;
12529 DWFontManager = [NSFontManager sharedFontManager]; 12530 DWFontManager = [NSFontManager sharedFontManager];
12530 #ifdef BUILDING_FOR_MOJAVE 12531 #ifdef HAVE_AVAILABLE
12531 if (@available(macOS 10.14, *)) 12532 if (@available(macOS 10.14, *))
12532 { 12533 {
12533 if([[NSBundle mainBundle] bundleIdentifier] != nil) 12534 if([[NSBundle mainBundle] bundleIdentifier] != nil)
12534 { 12535 {
12535 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter]; 12536 UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
12564 if(!_dw_app_id[0]) 12565 if(!_dw_app_id[0])
12565 { 12566 {
12566 /* Generate an Application ID based on the PID if all else fails. */ 12567 /* Generate an Application ID based on the PID if all else fails. */
12567 snprintf(_dw_app_id, _DW_APP_ID_SIZE, "%s.pid.%d", DW_APP_DOMAIN_DEFAULT, getpid()); 12568 snprintf(_dw_app_id, _DW_APP_ID_SIZE, "%s.pid.%d", DW_APP_DOMAIN_DEFAULT, getpid());
12568 } 12569 }
12569 return 0; 12570 return DW_ERROR_NONE;
12570 } 12571 }
12571 12572
12572 /* 12573 /*
12573 * Allocates a shared memory region with a name. 12574 * Allocates a shared memory region with a name.
12574 * Parameters: 12575 * Parameters:
12754 NSString *nsprogram = [NSString stringWithUTF8String:program]; 12755 NSString *nsprogram = [NSString stringWithUTF8String:program];
12755 NSWorkspace *ws = [NSWorkspace sharedWorkspace]; 12756 NSWorkspace *ws = [NSWorkspace sharedWorkspace];
12756 12757
12757 if(params && params[0] && params[1]) 12758 if(params && params[0] && params[1])
12758 { 12759 {
12759 #ifdef BUILDING_FOR_CATALINA 12760 #ifdef HAVE_AVAILABLE
12760 if(@available(macOS 10.15, *)) 12761 if(@available(macOS 10.15, *))
12761 { 12762 {
12762 NSURL *url = _dw_url_from_program(nsprogram, ws); 12763 NSURL *url = _dw_url_from_program(nsprogram, ws);
12763 NSMutableArray *array = [[NSMutableArray alloc] init]; 12764 NSMutableArray *array = [[NSMutableArray alloc] init];
12765 __block DWDialog *dialog = dw_dialog_new(NULL);
12764 int z = 1; 12766 int z = 1;
12765 12767
12766 while(params[z]) 12768 while(params[z])
12767 { 12769 {
12768 NSString *thisfile = [NSString stringWithUTF8String:params[z]]; 12770 NSString *thisfile = [NSString stringWithUTF8String:params[z]];
12773 } 12775 }
12774 12776
12775 [ws openURLs:array withApplicationAtURL:url 12777 [ws openURLs:array withApplicationAtURL:url
12776 configuration:[NSWorkspaceOpenConfiguration configuration] 12778 configuration:[NSWorkspaceOpenConfiguration configuration]
12777 completionHandler:^(NSRunningApplication *app, NSError *error) { 12779 completionHandler:^(NSRunningApplication *app, NSError *error) {
12780 int pid = DW_ERROR_UNKNOWN;
12781
12778 if(error) 12782 if(error)
12779 NSLog(@"openURLs: %@", [error localizedDescription]); 12783 NSLog(@"openURLs: %@", [error localizedDescription]);
12784 else
12785 pid = [app processIdentifier];
12786 dw_dialog_dismiss(dialog, DW_INT_TO_POINTER(pid));
12780 }]; 12787 }];
12788 ret = DW_POINTER_TO_INT(dw_dialog_wait(dialog));
12781 } 12789 }
12782 else 12790 else
12783 #endif 12791 #endif
12784 { 12792 {
12785 SEL sofwa = NSSelectorFromString(@"openFile:withApplication:"); 12793 SEL sofwa = NSSelectorFromString(@"openFile:withApplication:");
12792 { 12800 {
12793 NSString *file = [NSString stringWithUTF8String:params[z]]; 12801 NSString *file = [NSString stringWithUTF8String:params[z]];
12794 DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa]; 12802 DWIMP iofwa = (DWIMP)[ws methodForSelector:sofwa];
12795 12803
12796 iofwa(ws, sofwa, file, nsprogram); 12804 iofwa(ws, sofwa, file, nsprogram);
12805 ret = DW_ERROR_NONE;
12797 z++; 12806 z++;
12798 } 12807 }
12799 } 12808 }
12800 } 12809 }
12801 } 12810 }
12802 else 12811 else
12803 { 12812 {
12804 #ifdef BUILDING_FOR_CATALINA 12813 #ifdef HAVE_AVAILABLE
12805 if(@available(macOS 10.15, *)) 12814 if(@available(macOS 10.15, *))
12806 { 12815 {
12807 NSURL *url = _dw_url_from_program(nsprogram, ws); 12816 NSURL *url = _dw_url_from_program(nsprogram, ws);
12817 __block DWDialog *dialog = dw_dialog_new(NULL);
12808 12818
12809 [ws openApplicationAtURL:url 12819 [ws openApplicationAtURL:url
12810 configuration:[NSWorkspaceOpenConfiguration configuration] 12820 configuration:[NSWorkspaceOpenConfiguration configuration]
12811 completionHandler:^(NSRunningApplication *app, NSError *error) { 12821 completionHandler:^(NSRunningApplication *app, NSError *error) {
12822 int pid = DW_ERROR_UNKNOWN;
12823
12812 if(error) 12824 if(error)
12813 NSLog(@"openApplicationAtURL: %@", [error localizedDescription]); 12825 NSLog(@"openApplicationAtURL: %@", [error localizedDescription]);
12826 else
12827 pid = [app processIdentifier];
12828 dw_dialog_dismiss(dialog, DW_INT_TO_POINTER(pid));
12814 }]; 12829 }];
12830 ret = DW_POINTER_TO_INT(dw_dialog_wait(dialog));
12815 } 12831 }
12816 else 12832 else
12817 #endif 12833 #endif
12818 { 12834 {
12819 SEL sla = NSSelectorFromString(@"launchApplication:"); 12835 SEL sla = NSSelectorFromString(@"launchApplication:");
12820 12836
12821 if([ws respondsToSelector:sla]) 12837 if([ws respondsToSelector:sla])
12822 { 12838 {
12823 DWIMP ila = (DWIMP)[ws methodForSelector:sla]; 12839 DWIMP ila = (DWIMP)[ws methodForSelector:sla];
12824 ila(ws, sla, nsprogram); 12840 ila(ws, sla, nsprogram);
12841 ret = DW_ERROR_NONE;
12825 } 12842 }
12826 } 12843 }
12827 } 12844 }
12828 ret = DW_ERROR_NONE;
12829 } 12845 }
12830 else 12846 else
12831 { 12847 {
12832 /* Use AppleScript to Open Terminal.app or contact an existing copy, 12848 /* Use AppleScript to Open Terminal.app or contact an existing copy,
12833 * and execute the command passed in params[]. 12849 * and execute the command passed in params[].
13130 case DW_FEATURE_HTML_RESULT: 13146 case DW_FEATURE_HTML_RESULT:
13131 case DW_FEATURE_CONTAINER_STRIPE: 13147 case DW_FEATURE_CONTAINER_STRIPE:
13132 case DW_FEATURE_MLE_WORD_WRAP: 13148 case DW_FEATURE_MLE_WORD_WRAP:
13133 case DW_FEATURE_UTF8_UNICODE: 13149 case DW_FEATURE_UTF8_UNICODE:
13134 return DW_FEATURE_ENABLED; 13150 return DW_FEATURE_ENABLED;
13135 #ifdef BUILDING_FOR_MOJAVE 13151 #ifdef HAVE_AVAILABLE
13136 case DW_FEATURE_DARK_MODE: 13152 case DW_FEATURE_DARK_MODE:
13137 { 13153 {
13138 if(@available(macOS 10.14, *)) 13154 if(@available(macOS 10.14, *))
13139 { 13155 {
13140 /* Make sure DWApp is initialized */ 13156 /* Make sure DWApp is initialized */
13193 case DW_FEATURE_CONTAINER_STRIPE: 13209 case DW_FEATURE_CONTAINER_STRIPE:
13194 case DW_FEATURE_MLE_WORD_WRAP: 13210 case DW_FEATURE_MLE_WORD_WRAP:
13195 case DW_FEATURE_UTF8_UNICODE: 13211 case DW_FEATURE_UTF8_UNICODE:
13196 return DW_ERROR_GENERAL; 13212 return DW_ERROR_GENERAL;
13197 /* These features are supported and configurable */ 13213 /* These features are supported and configurable */
13198 #ifdef BUILDING_FOR_MOJAVE 13214 #ifdef HAVE_AVAILABLE
13199 case DW_FEATURE_DARK_MODE: 13215 case DW_FEATURE_DARK_MODE:
13200 { 13216 {
13201 if(@available(macOS 10.14, *)) 13217 if(@available(macOS 10.14, *))
13202 { 13218 {
13203 /* Make sure DWApp is initialized */ 13219 /* Make sure DWApp is initialized */