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 typedef long refcontype
;
53 #define PATHNAMELEN 256
56 static char *arg_vector
[256];
57 FSSpec PyMac_ApplicationFSSpec
;
58 char PyMac_ApplicationPath
[PATHNAMELEN
];
60 /* Duplicate a string to the heap. We also export this since it isn't standard
65 strdup(const char *src
)
67 char *dst
= malloc(strlen(src
) + 1);
74 /* Initialize FSSpec and full name of current application */
77 PyMac_init_process_location(void)
79 ProcessSerialNumber currentPSN
;
82 static int applocation_inited
;
84 if ( applocation_inited
) return 0;
85 currentPSN
.highLongOfPSN
= 0;
86 currentPSN
.lowLongOfPSN
= kCurrentProcess
;
87 info
.processInfoLength
= sizeof(ProcessInfoRec
);
88 info
.processName
= NULL
;
89 info
.processAppSpec
= &PyMac_ApplicationFSSpec
;
90 if ( err
=GetProcessInformation(¤tPSN
, &info
))
92 if ( err
=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec
, PyMac_ApplicationPath
, PATHNAMELEN
) )
94 applocation_inited
= 1;
98 /* Check that there aren't any args remaining in the event */
101 get_missing_params(const AppleEvent
*theAppleEvent
)
107 err
= AEGetAttributePtr(theAppleEvent
, keyMissedKeywordAttr
, typeWildCard
,
108 &theType
, nil
, 0, &actualSize
);
109 if (err
== errAEDescNotFound
)
112 return errAEEventNotHandled
;
115 static int got_one
; /* Flag that we can stop getting events */
117 /* Handle the Print or Quit events (by failing) */
120 handle_not(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
122 #pragma unused (reply, refCon)
124 return errAEEventNotHandled
;
127 /* Handle the Open Application event (by ignoring it) */
130 handle_open_app(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
132 #pragma unused (reply, refCon)
134 /* Test by Jack: would removing this facilitate debugging? */
137 return get_missing_params(theAppleEvent
);
140 /* Handle the Open Document event, by adding an argument */
143 handle_open_doc(const AppleEvent
*theAppleEvent
, AppleEvent
*reply
, refcontype refCon
)
145 #pragma unused (reply, refCon)
152 char path
[PATHNAMELEN
];
155 if ((err
= AEGetParamDesc(theAppleEvent
,
156 keyDirectObject
, typeAEList
, &doclist
)))
158 if ((err
= get_missing_params(theAppleEvent
)))
160 if ((err
= AECountItems(&doclist
, &ndocs
)))
162 for(i
= 1; i
<= ndocs
; i
++) {
163 err
= AEGetNthPtr(&doclist
, i
, typeFSS
,
164 &keywd
, &rttype
, &fss
, sizeof(fss
), &size
);
167 PyMac_GetFullPathname(&fss
, path
, PATHNAMELEN
);
168 arg_vector
[arg_count
++] = strdup(path
);
173 /* Install standard core event handlers */
174 AEEventHandlerUPP open_doc_upp
;
175 AEEventHandlerUPP open_app_upp
;
176 AEEventHandlerUPP not_upp
;
179 set_ae_handlers(void)
181 open_doc_upp
= NewAEEventHandlerUPP(&handle_open_doc
);
182 open_app_upp
= NewAEEventHandlerUPP(&handle_open_app
);
183 not_upp
= NewAEEventHandlerUPP(&handle_not
);
185 AEInstallEventHandler(kCoreEventClass
, kAEOpenApplication
,
186 open_app_upp
, 0L, false);
187 AEInstallEventHandler(kCoreEventClass
, kAEOpenDocuments
,
188 open_doc_upp
, 0L, false);
189 AEInstallEventHandler(kCoreEventClass
, kAEPrintDocuments
,
191 AEInstallEventHandler(kCoreEventClass
, kAEQuitApplication
,
195 /* Uninstall standard core event handlers */
198 reset_ae_handlers(void)
200 AERemoveEventHandler(kCoreEventClass
, kAEOpenApplication
,
201 open_app_upp
, false);
202 AERemoveEventHandler(kCoreEventClass
, kAEOpenDocuments
,
203 open_doc_upp
, false);
204 AERemoveEventHandler(kCoreEventClass
, kAEPrintDocuments
,
206 AERemoveEventHandler(kCoreEventClass
, kAEQuitApplication
,
210 /* Wait for events until a core event has been handled */
220 for (n
= 0; n
< 100 && !got_one
; n
++) {
221 ok
= GetNextEvent(everyEvent
, &event
);
222 if (ok
&& event
.what
== kHighLevelEvent
) {
223 AEProcessAppleEvent(&event
);
228 /* Get the argv vector, return argc */
231 PyMac_GetArgv(char ***pargv
, int noevents
)
234 (void)PyMac_init_process_location();
235 arg_vector
[arg_count
++] = strdup(PyMac_ApplicationPath
);
243 arg_vector
[arg_count
] = NULL
;