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 */
34 #include <Processes.h>
36 #include <AppleEvents.h>
37 #include <AEObjects.h>
44 #if UNIVERSAL_INTERFACES_VERSION >= 0x0340
45 typedef long refcontype
;
47 typedef unsigned long refcontype
;
54 static char *arg_vector
[256];
55 FSSpec PyMac_ApplicationFSSpec
;
56 char PyMac_ApplicationPath
[256];
57 static int applocation_inited
;
59 /* Duplicate a string to the heap. We also export this since it isn't standard
64 strdup(const char *src
)
66 char *dst
= malloc(strlen(src
) + 1);
73 /* Initialize FSSpec and full name of current application */
76 PyMac_init_process_location()
78 ProcessSerialNumber currentPSN
;
82 if ( applocation_inited
) return 0;
83 currentPSN
.highLongOfPSN
= 0;
84 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
85 info
.processInfoLength
= sizeof(ProcessInfoRec
);
86 info
.processName
= NULL
;
87 info
.processAppSpec
= &PyMac_ApplicationFSSpec
;
88 if ( err
=GetProcessInformation(¤tPSN
, &info
))
90 if ( err
=PyMac_GetFullPath(&PyMac_ApplicationFSSpec
, PyMac_ApplicationPath
) )
92 applocation_inited
= 1;
96 /* Check that there aren't any args remaining in the event */
99 get_missing_params(const AppleEvent
*theAppleEvent
)
105 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
106 &theType
, nil
, 0, &actualSize
);
107 if (err
== errAEDescNotFound
)
110 return errAEEventNotHandled
;
113 static int got_one
; /* Flag that we can stop getting events */
115 /* Handle the Print or Quit events (by failing) */
118 handle_not(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
120 #pragma unused (reply, refCon)
122 return errAEEventNotHandled
;
125 /* Handle the Open Application event (by ignoring it) */
128 handle_open_app(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
130 #pragma unused (reply, refCon)
132 /* Test by Jack: would removing this facilitate debugging? */
135 return get_missing_params(theAppleEvent
);
138 /* Handle the Open Document event, by adding an argument */
141 handle_open_doc(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
143 #pragma unused (reply, refCon)
153 if (err
= AEGetParamDesc(theAppleEvent
,
154 keyDirectObject
, typeAEList
, &doclist
))
156 if (err
= get_missing_params(theAppleEvent
))
158 if (err
= AECountItems(&doclist
, &ndocs
))
160 for(i
= 1; i
<= ndocs
; i
++) {
161 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
162 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
165 PyMac_GetFullPath(&fss
, path
);
166 arg_vector
[arg_count
++] = strdup(path
);
171 /* Install standard core event handlers */
172 AEEventHandlerUPP open_doc_upp
;
173 AEEventHandlerUPP open_app_upp
;
174 AEEventHandlerUPP not_upp
;
179 open_doc_upp
= NewAEEventHandlerUPP(&handle_open_doc
);
180 open_app_upp
= NewAEEventHandlerUPP(&handle_open_app
);
181 not_upp
= NewAEEventHandlerUPP(&handle_not
);
183 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
184 open_app_upp
, 0L, false);
185 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
186 open_doc_upp
, 0L, false);
187 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
189 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
193 /* Uninstall standard core event handlers */
198 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
199 open_app_upp
, false);
200 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
201 open_doc_upp
, false);
202 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
204 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
208 /* Wait for events until a core event has been handled */
218 for (n
= 0; n
< 100 && !got_one
; n
++) {
219 #if !TARGET_API_MAC_CARBON
222 ok
= GetNextEvent(everyEvent
, &event
);
223 if (ok
&& event
.what
== kHighLevelEvent
) {
224 AEProcessAppleEvent(&event
);
229 /* Get the argv vector, return argc */
232 PyMac_GetArgv(pargv
, noevents
)
238 (void)PyMac_init_process_location();
239 arg_vector
[arg_count
++] = strdup(PyMac_ApplicationPath
);
247 arg_vector
[arg_count
] = NULL
;