2 * This file is part of mplayer2.
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.
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.
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.
19 #import <Cocoa/Cocoa.h>
20 #import <ApplicationServices/ApplicationServices.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;
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)
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]);
55 play_tree_append_entry(new_tree, entry);
57 play_tree_set_child(new_tree, entry);
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);
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];
102 play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv)
104 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
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();
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.
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));