sync with en/mplayer.1 rev. 30611
[mplayer/glamo.git] / osdep / macosx_finder_args.c
blob81c1959894dea6aeba70c4139c773e6fa33a073d
1 /*
2 * This file is part of MPlayer.
4 * MPlayer 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 * MPlayer 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 MPlayer; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 #include <Carbon/Carbon.h>
20 #include <ApplicationServices/ApplicationServices.h>
21 #include "stream/url.h"
22 #include "mp_msg.h"
23 #include "m_option.h"
24 #include "m_config.h"
25 #include "playtree.h"
27 static play_tree_t *files=NULL;
29 static inline void add_entry(play_tree_t **last_parentp, play_tree_t **last_entryp, play_tree_t *entry) {
31 if(*last_entryp==NULL)
32 play_tree_set_child(*last_parentp, entry);
33 else
34 play_tree_append_entry(*last_entryp, entry);
36 *last_entryp=entry;
39 static pascal OSErr AppleEventHandlerProc(const AppleEvent *theAppleEvent, AppleEvent* reply, SInt32 handlerRefcon) {
40 OSErr err=errAEEventNotHandled, res=noErr;
41 AEDescList docList;
42 long itemsInList;
44 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments, NULL, FALSE);
45 if((res=AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList))==noErr) {
46 if((res=AECountItems(&docList, &itemsInList))==noErr) {
47 Size currentSize=0;
48 int valid=0,i;
49 char *parm=NULL;
50 play_tree_t *last_entry=NULL;
52 files=play_tree_new();
53 for(i=1;i<=itemsInList;++i) {
55 for(;;) {
56 OSErr e;
57 Size actualSize=0;
58 AEKeyword keywd;
59 DescType returnedType;
61 if((e=AEGetNthPtr(&docList, i, typeFileURL, &keywd, &returnedType, (Ptr)parm, currentSize, &actualSize))==noErr) {
62 if(actualSize>=currentSize) {
63 currentSize=actualSize+1;
64 parm=realloc(parm, currentSize);
66 else {
67 parm[actualSize]=0;
68 valid=1;
69 break;
72 else {
73 valid=0;
74 break;
78 if(valid) {
79 URL_t *url=url_new(parm);
81 if(url && !strcmp(url->protocol,"file") && !strcmp(url->hostname,"localhost")) {
82 play_tree_t *entry=play_tree_new();
84 url_unescape_string(url->file, url->file);
85 play_tree_add_file(entry, url->file);
86 add_entry(&files, &last_entry, entry);
89 url_free(url);
93 if(parm)
94 free(parm);
96 err=noErr;
98 else
99 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "AECountItems() error %d\n", res);
101 AEDisposeDesc(&docList);
103 else
104 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "AEGetParamDesc() error %d\n", res);
106 QuitApplicationEventLoop();
107 return err;
110 play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv) {
111 ProcessSerialNumber myPsn;
112 char myPsnStr[5+10+1+10+1];
114 GetCurrentProcess(&myPsn);
115 snprintf(myPsnStr, 5+10+1+10+1, "-psn_%u_%u", myPsn.highLongOfPSN, myPsn.lowLongOfPSN);
116 myPsnStr[5+10+1+10]=0;
118 if((argc==2) && !strcmp(myPsnStr, argv[1])) {
119 m_config_set_option(config, "quiet", NULL);
120 InitCursor();
121 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(AppleEventHandlerProc), 0, FALSE);
122 RunApplicationEventLoop();
125 return files;