typo fixes
[mplayer/greg.git] / osdep / macosx_finder_args.c
blob16d95303c026723cefd955ebdd9a816f14a88418
1 #include <Carbon/Carbon.h>
2 #include <ApplicationServices/ApplicationServices.h>
3 #include "libmpdemux/url.h"
4 #include "mp_msg.h"
5 #include "m_option.h"
6 #include "m_config.h"
7 #include "playtree.h"
9 static play_tree_t *files=NULL;
11 static inline void add_entry(play_tree_t **last_parentp, play_tree_t **last_entryp, play_tree_t *entry) {
13 if(*last_entryp==NULL)
14 play_tree_set_child(*last_parentp, entry);
15 else
16 play_tree_append_entry(*last_entryp, entry);
18 *last_entryp=entry;
21 static pascal OSErr AppleEventHandlerProc(const AppleEvent *theAppleEvent, AppleEvent* reply, SInt32 handlerRefcon) {
22 OSErr err=errAEEventNotHandled, res=noErr;
23 AEDescList docList;
24 long itemsInList;
26 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments, NULL, FALSE);
27 if((res=AEGetParamDesc(theAppleEvent, keyDirectObject, typeAEList, &docList))==noErr) {
28 if((res=AECountItems(&docList, &itemsInList))==noErr) {
29 Size currentSize=0;
30 int valid=0,i;
31 char *parm=NULL;
32 play_tree_t *last_entry=NULL;
34 files=play_tree_new();
35 for(i=1;i<=itemsInList;++i) {
37 for(;;) {
38 OSErr e;
39 Size actualSize=0;
40 AEKeyword keywd;
41 DescType returnedType;
43 if((e=AEGetNthPtr(&docList, i, typeFileURL, &keywd, &returnedType, (Ptr)parm, currentSize, &actualSize))==noErr) {
44 if(actualSize>=currentSize) {
45 currentSize=actualSize+1;
46 parm=realloc(parm, currentSize);
48 else {
49 parm[actualSize]=0;
50 valid=1;
51 break;
54 else {
55 valid=0;
56 break;
60 if(valid) {
61 URL_t *url=url_new(parm);
63 if(url && !strcmp(url->protocol,"file") && !strcmp(url->hostname,"localhost")) {
64 play_tree_t *entry=play_tree_new();
66 url_unescape_string(url->file, url->file);
67 play_tree_add_file(entry, url->file);
68 add_entry(&files, &last_entry, entry);
71 url_free(url);
75 if(parm)
76 free(parm);
78 err=noErr;
80 else
81 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "AECountItems() error %d\n", res);
83 AEDisposeDesc(&docList);
85 else
86 mp_msg(MSGT_CFGPARSER, MSGL_ERR, "AEGetParamDesc() error %d\n", res);
88 QuitApplicationEventLoop();
89 return err;
92 play_tree_t *macosx_finder_args(m_config_t *config, int argc, char **argv) {
93 ProcessSerialNumber myPsn;
94 char myPsnStr[5+10+1+10+1];
96 GetCurrentProcess(&myPsn);
97 snprintf(myPsnStr, 5+10+1+10+1, "-psn_%u_%u", myPsn.highLongOfPSN, myPsn.lowLongOfPSN);
98 myPsnStr[5+10+1+10]=0;
100 if((argc==2) && !strcmp(myPsnStr, argv[1])) {
101 m_config_set_option(config, "quiet", NULL);
102 InitCursor();
103 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments, NewAEEventHandlerUPP(AppleEventHandlerProc), 0, FALSE);
104 RunApplicationEventLoop();
107 return files;