|
831 | 831 | } |
832 | 832 | } |
833 | 833 |
|
| 834 | +Error OS_MacOS::open_with_program(const String &p_program_path, const List<String> &p_paths) { |
| 835 | + NSURL *app_url = [NSURL fileURLWithPath:@(p_program_path.utf8().get_data())]; |
| 836 | + if (!app_url) { |
| 837 | + return ERR_INVALID_PARAMETER; |
| 838 | + } |
| 839 | + |
| 840 | + NSBundle *bundle = [NSBundle bundleWithURL:app_url]; |
| 841 | + if (!bundle) { |
| 842 | + return OS_Unix::create_process(p_program_path, p_paths); |
| 843 | + } |
| 844 | + |
| 845 | + NSMutableArray *urls_to_open = [[NSMutableArray alloc] init]; |
| 846 | + for (const String &path : p_paths) { |
| 847 | + NSURL *file_url = [NSURL fileURLWithPath:@(path.utf8().get_data())]; |
| 848 | + if (file_url) { |
| 849 | + [urls_to_open addObject:file_url]; |
| 850 | + } |
| 851 | + } |
| 852 | + |
| 853 | + if ([urls_to_open count] == 0) { |
| 854 | + return ERR_INVALID_PARAMETER; |
| 855 | + } |
| 856 | + |
| 857 | +#if defined(__x86_64__) |
| 858 | + if (@available(macOS 10.15, *)) { |
| 859 | +#endif |
| 860 | + NSWorkspaceOpenConfiguration *configuration = [[NSWorkspaceOpenConfiguration alloc] init]; |
| 861 | + [configuration setCreatesNewApplicationInstance:NO]; |
| 862 | + __block dispatch_semaphore_t lock = dispatch_semaphore_create(0); |
| 863 | + __block Error err = ERR_TIMEOUT; |
| 864 | + |
| 865 | + [[NSWorkspace sharedWorkspace] openURLs:urls_to_open |
| 866 | + withApplicationAtURL:app_url |
| 867 | + configuration:configuration |
| 868 | + completionHandler:^(NSRunningApplication *app, NSError *error) { |
| 869 | + if (error) { |
| 870 | + err = ERR_CANT_FORK; |
| 871 | + NSLog(@"Failed to open paths: %@", error.localizedDescription); |
| 872 | + } else { |
| 873 | + err = OK; |
| 874 | + } |
| 875 | + dispatch_semaphore_signal(lock); |
| 876 | + }]; |
| 877 | + dispatch_semaphore_wait(lock, dispatch_time(DISPATCH_TIME_NOW, 20000000000)); // 20 sec timeout, wait for app to launch. |
| 878 | + |
| 879 | + return err; |
| 880 | +#if defined(__x86_64__) |
| 881 | + } else { |
| 882 | + NSError *error = nullptr; |
| 883 | + [[NSWorkspace sharedWorkspace] openURLs:urls_to_open withApplicationAtURL:app_url options:NSWorkspaceLaunchDefault configuration:@{} error:&error]; |
| 884 | + if (error) { |
| 885 | + return ERR_CANT_FORK; |
| 886 | + } |
| 887 | + return OK; |
| 888 | + } |
| 889 | +#endif |
| 890 | +} |
| 891 | + |
834 | 892 | bool OS_MacOS::is_process_running(const ProcessID &p_pid) const { |
835 | 893 | NSRunningApplication *app = [NSRunningApplication runningApplicationWithProcessIdentifier:(pid_t)p_pid]; |
836 | 894 | if (!app) { |
|
0 commit comments