macosx_finder_args: add SDL specific code path
[mplayer.git] / osdep / macosx_finder_args.m
blobda8396b8a88a1685bb9257d61cae88814f6e1b6b
1 /*
2  * This file is part of mplayer2.
3  *
4  * mplayer2 is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * mplayer2 is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with mplayer2; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
19 #import <Cocoa/Cocoa.h>
20 #import <ApplicationServices/ApplicationServices.h>
21 #include <stdio.h>
22 #include "config.h"
23 #include "macosx_finder_args.h"
25 static play_tree_t *files = NULL;
27 play_tree_t *play_tree_from_filenames(NSArray *filenames);
28 void macosx_wait_fileopen_events(void);
29 void macosx_redirect_output_to_logfile(const char *filename);
30 bool psn_matches_current_process(const char *psn_arg_to_check);
31 NSArray *argv_to_filenames(int argc, char **argv);
33 @interface FileOpenDelegate : NSObject
34 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames;
35 @end
37 @implementation FileOpenDelegate
38 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
40     files = play_tree_from_filenames(filenames);
41     [NSApp stop:nil]; // stop the runloop (give back control to mplayer2 code)
43 @end
45 play_tree_t *play_tree_from_filenames(NSArray *filenames) {
46     NSArray *sorted_filenames = [filenames
47         sortedArrayUsingSelector:@selector(compare:)];
48     play_tree_t *new_tree = play_tree_new();
49     play_tree_t *last_entry = nil;
50     for (NSString *filename in sorted_filenames) {
51         play_tree_t *entry = play_tree_new();
52         play_tree_add_file(entry, [filename UTF8String]);
54         if (last_entry)
55           play_tree_append_entry(new_tree, entry);
56         else
57           play_tree_set_child(new_tree, entry);
59         last_entry = entry;
60     }
61     return new_tree;
64 void macosx_wait_fileopen_events()
66     NSApp = [NSApplication sharedApplication];
67     [NSApp setDelegate: [[[FileOpenDelegate alloc] init] autorelease]];
68     [NSApp run]; // block until we recive the fileopen events
71 void macosx_redirect_output_to_logfile(const char *filename)
73     NSString *log_path = [NSHomeDirectory() stringByAppendingPathComponent:
74         [@"Library/Logs/" stringByAppendingFormat:@"%s.log", filename]];
75     freopen([log_path fileSystemRepresentation], "a", stdout);
76     freopen([log_path fileSystemRepresentation], "a", stderr);
79 bool psn_matches_current_process(const char *psn_arg_to_check)
81     ProcessSerialNumber psn;
82     char psn_arg[5+10+1+10+1];
84     GetCurrentProcess(&psn);
85     snprintf(psn_arg, 5+10+1+10+1, "-psn_%u_%u",
86              psn.highLongOfPSN, psn.lowLongOfPSN);
87     psn_arg[5+10+1+10]=0;
89     return strcmp(psn_arg, psn_arg_to_check) == 0;
92 NSArray *argv_to_filenames(int argc, char **argv)
94     NSMutableArray *filenames = [NSMutableArray arrayWithCapacity:argc-1];
95     for (int i=1; i < argc; i++) {
96         NSString *filename = [NSString stringWithUTF8String: argv[i]];
97         [filenames addObject: filename];
98     }
99     return filenames;
102 play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv)
104     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
105 #ifndef CONFIG_SDL
106     if (argc == 2 && psn_matches_current_process(argv[1])) {
107         macosx_redirect_output_to_logfile("mplayer2");
108         m_config_set_option0(config, "quiet", NULL, false);
109         macosx_wait_fileopen_events();
110     }
112     // SDL's bootstrap code (include SDL.h in mplayer.c) renames main() to
113     // SDL_main() with a macro and defines it's own main(). There it starts a
114     // minimal Cocoa Application to wait for file open events.
115     // After the events come in, SDL will call the original main() with argv
116     // set to the list of files to open.
118     // NSProcessInfo still holds the original arguments of the process, so we
119     // can use that to figure out if we got called from the Finder.
120 #else
121     NSArray *args = [[NSProcessInfo processInfo] arguments];
122     if ([args count] == 2) {
123         const char *psn = [[args objectAtIndex:1] UTF8String];
125         if (psn_matches_current_process(psn)) {
126             macosx_redirect_output_to_logfile("mplayer2");
127             m_config_set_option0(config, "quiet", NULL, false);
128             files = play_tree_from_filenames(argv_to_filenames(argc, argv));
129         }
130     }
131 #endif
132     [pool release];
133     return files;