summaryrefslogtreecommitdiffstats
path: root/osdep/macosx_finder_args.m
blob: 56d314c100776eb650e9191c742d6a09cbe4ff93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * This file is part of mplayer2.
 *
 * mplayer2 is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * mplayer2 is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with mplayer2; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#import <Cocoa/Cocoa.h>
#import <ApplicationServices/ApplicationServices.h>
#include <stdio.h>
#include "talloc.h"
#include "playlist.h"
#include "macosx_finder_args.h"

static struct playlist *files = NULL;

void macosx_wait_fileopen_events(void);
void macosx_redirect_output_to_logfile(const char *filename);
bool psn_matches_current_process(char *psn_arg_to_check);

@interface FileOpenDelegate : NSObject
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames;
@end

@implementation FileOpenDelegate
- (void)application:(NSApplication *)sender openFiles:(NSArray *)filenames
{
    NSArray *sorted_filenames = [filenames
        sortedArrayUsingSelector:@selector(compare:)];
    files = talloc_zero(NULL, struct playlist);
    for (NSString *filename in sorted_filenames)
        playlist_add_file(files, [filename UTF8String]);
    [NSApp stop:nil]; // stop the runloop (give back control to mplayer2 code)
}
@end

void macosx_wait_fileopen_events()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSApp = [NSApplication sharedApplication];
    [NSApp setDelegate: [[[FileOpenDelegate alloc] init] autorelease]];
    [NSApp run]; // block until we recive the fileopen events
    [pool release];
}

void macosx_redirect_output_to_logfile(const char *filename)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *log_path = [NSHomeDirectory() stringByAppendingPathComponent:
        [@"Library/Logs/" stringByAppendingFormat:@"%s.log", filename]];
    freopen([log_path fileSystemRepresentation], "a", stdout);
    freopen([log_path fileSystemRepresentation], "a", stderr);
    [pool release];
}

bool psn_matches_current_process(char *psn_arg_to_check)
{
    ProcessSerialNumber psn;
    char psn_arg[5+10+1+10+1];

    GetCurrentProcess(&psn);
    snprintf(psn_arg, 5+10+1+10+1, "-psn_%u_%u",
             psn.highLongOfPSN, psn.lowLongOfPSN);
    psn_arg[5+10+1+10]=0;

    return strcmp(psn_arg, psn_arg_to_check) == 0;
}

bool macosx_finder_args(m_config_t *config, struct playlist *pl_files,
                         int argc, char **argv)
{
    if (argc==1 && psn_matches_current_process(argv[0])) {
        macosx_redirect_output_to_logfile("mplayer2");
        m_config_set_option0(config, "quiet", NULL);
        macosx_wait_fileopen_events();
    }

    if (files) {
        playlist_transfer_entries(pl_files, files);
        talloc_free(files);
        files = NULL;
        return true;
    } else {
        return false;
    }
}