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>
22 #include "macosx_finder_args.h"
24 static play_tree_t *files = NULL;
26 void macosx_wait_fileopen_events(void);
27 void macosx_redirect_output_to_logfile(const char *filename);
28 bool psn_matches_current_process(char *psn_arg_to_check);
30 @interface FileOpenDelegate : NSObject
31 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames;
34 @implementation FileOpenDelegate
35 - (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
37 files = play_tree_new();
38 play_tree_t *last_entry = nil;
39 for (NSString *filename in filenames) {
40 play_tree_t *entry = play_tree_new();
41 play_tree_add_file(entry, [filename UTF8String]);
44 play_tree_append_entry(files, entry);
46 play_tree_set_child(files, entry);
50 [NSApp stop:nil]; // stop the runloop (give back control to mplayer2 code)
54 void macosx_wait_fileopen_events()
56 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
57 NSApp = [NSApplication sharedApplication];
58 [NSApp setDelegate: [[[FileOpenDelegate alloc] init] autorelease]];
59 [NSApp run]; // block until we recive the fileopen events
63 void macosx_redirect_output_to_logfile(const char *filename)
65 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
66 NSString *log_path = [NSHomeDirectory() stringByAppendingPathComponent:
67 [@"Library/Logs/" stringByAppendingFormat:@"%s.log", filename]];
68 freopen([log_path fileSystemRepresentation], "a", stdout);
69 freopen([log_path fileSystemRepresentation], "a", stderr);
73 bool psn_matches_current_process(char *psn_arg_to_check)
75 ProcessSerialNumber psn;
76 char psn_arg[5+10+1+10+1];
78 GetCurrentProcess(&psn);
79 snprintf(psn_arg, 5+10+1+10+1, "-psn_%u_%u",
80 psn.highLongOfPSN, psn.lowLongOfPSN);
83 return strcmp(psn_arg, psn_arg_to_check) == 0;
86 play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv)
88 if (argc==2 && psn_matches_current_process(argv[1])) {
89 macosx_redirect_output_to_logfile("mplayer2");
90 m_config_set_option0(config, "quiet", NULL, false);
91 macosx_wait_fileopen_events();