1 /***********************************************************
2 Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
7 Permission to use, copy, modify, and distribute this software and its
8 documentation for any purpose and without fee is hereby granted,
9 provided that the above copyright notice appear in all copies and that
10 both that copyright notice and this permission notice appear in
11 supporting documentation, and that the names of Stichting Mathematisch
12 Centrum or CWI not be used in advertising or publicity pertaining to
13 distribution of the software without specific, written prior permission.
15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 ******************************************************************/
25 /* Construct argc and argv for main() by using Apple Events */
26 /* From Jack's implementation for STDWIN */
30 #ifdef WITHOUT_FRAMEWORKS
35 #include <Processes.h>
37 #include <AppleEvents.h>
38 #include <AEObjects.h>
45 #include <Carbon/Carbon.h>
46 #endif /* WITHOUT_FRAMEWORKS */
48 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
49 typedef long refcontype
;
51 typedef unsigned long refcontype
;
57 #ifdef TARGET_API_MAC_OSX
58 #define PATHNAMELEN 1024
60 #define PATHNAMELEN 256
64 static char *arg_vector
[256];
65 FSSpec PyMac_ApplicationFSSpec
;
66 char PyMac_ApplicationPath
[PATHNAMELEN
];
68 /* Duplicate a string to the heap. We also export this since it isn't standard
73 strdup(const char *src
)
75 char *dst
= malloc(strlen(src
) + 1);
83 #if !TARGET_API_MAC_OSX
84 /* Initialize FSSpec and full name of current application */
87 PyMac_init_process_location(void)
89 ProcessSerialNumber currentPSN
;
92 static int applocation_inited
;
94 if ( applocation_inited
) return 0;
95 currentPSN
.highLongOfPSN
= 0;
96 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
97 info
.processInfoLength
= sizeof(ProcessInfoRec
);
98 info
.processName
= NULL
;
99 info
.processAppSpec
= &PyMac_ApplicationFSSpec
;
100 if ( err
=GetProcessInformation(¤tPSN
, &info
))
102 if ( err
=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec
, PyMac_ApplicationPath
, PATHNAMELEN
) )
104 applocation_inited
= 1;
107 #endif /* !TARGET_API_MAC_OSX */
109 /* Check that there aren't any args remaining in the event */
112 get_missing_params(const AppleEvent
*theAppleEvent
)
118 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
119 &theType
, nil
, 0, &actualSize
);
120 if (err
== errAEDescNotFound
)
123 return errAEEventNotHandled
;
126 static int got_one
; /* Flag that we can stop getting events */
128 /* Handle the Print or Quit events (by failing) */
131 handle_not(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
133 #pragma unused (reply, refCon)
135 return errAEEventNotHandled
;
138 /* Handle the Open Application event (by ignoring it) */
141 handle_open_app(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
143 #pragma unused (reply, refCon)
145 /* Test by Jack: would removing this facilitate debugging? */
148 return get_missing_params(theAppleEvent
);
151 /* Handle the Open Document event, by adding an argument */
154 handle_open_doc(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
156 #pragma unused (reply, refCon)
163 char path
[PATHNAMELEN
];
166 if ((err
= AEGetParamDesc(theAppleEvent
,
167 keyDirectObject
, typeAEList
, &doclist
)))
169 if ((err
= get_missing_params(theAppleEvent
)))
171 if ((err
= AECountItems(&doclist
, &ndocs
)))
173 for(i
= 1; i
<= ndocs
; i
++) {
174 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
175 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
178 PyMac_GetFullPathname(&fss
, path
, PATHNAMELEN
);
179 arg_vector
[arg_count
++] = strdup(path
);
184 /* Install standard core event handlers */
185 AEEventHandlerUPP open_doc_upp
;
186 AEEventHandlerUPP open_app_upp
;
187 AEEventHandlerUPP not_upp
;
190 set_ae_handlers(void)
192 open_doc_upp
= NewAEEventHandlerUPP(&handle_open_doc
);
193 open_app_upp
= NewAEEventHandlerUPP(&handle_open_app
);
194 not_upp
= NewAEEventHandlerUPP(&handle_not
);
196 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
197 open_app_upp
, 0L, false);
198 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
199 open_doc_upp
, 0L, false);
200 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
202 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
206 /* Uninstall standard core event handlers */
209 reset_ae_handlers(void)
211 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
212 open_app_upp
, false);
213 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
214 open_doc_upp
, false);
215 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
217 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
221 /* Wait for events until a core event has been handled */
231 for (n
= 0; n
< 100 && !got_one
; n
++) {
232 #if !TARGET_API_MAC_CARBON
235 ok
= GetNextEvent(everyEvent
, &event
);
236 if (ok
&& event
.what
== kHighLevelEvent
) {
237 AEProcessAppleEvent(&event
);
242 /* Get the argv vector, return argc */
245 PyMac_GetArgv(char ***pargv
, int noevents
)
248 #if TARGET_API_MAC_OSX
249 /* In an OSX bundle argv[0] is okay */
252 (void)PyMac_init_process_location();
253 arg_vector
[arg_count
++] = strdup(PyMac_ApplicationPath
);
254 #endif /* TARGET_API_MAC_OSX */
262 arg_vector
[arg_count
] = NULL
;