blob: 1eb4a54b77070482654a3f9e0de31a099c32066c [file] [log] [blame]
Guido van Rossumdbfb2821995-02-19 15:51:30 +00001/***********************************************************
Jack Jansen42218ce1997-01-31 16:15:11 +00002Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
Guido van Rossumdbfb2821995-02-19 15:51:30 +00003The Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* Construct argc and argv for main() by using Apple Events */
26/* From Jack's implementation for STDWIN */
27
28#include <stdlib.h>
29
Guido van Rossumdbfb2821995-02-19 15:51:30 +000030#include <Types.h>
31#include <Files.h>
32#include <Events.h>
33#include <Memory.h>
34#include <Processes.h>
35#include <Errors.h>
36#include <AppleEvents.h>
37#include <AEObjects.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000038#include <Fonts.h>
Guido van Rossum6fc5aec1995-02-19 23:32:59 +000039#include <TextEdit.h>
40#include <Menus.h>
41#include <Dialogs.h>
42#include <Windows.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000043
Jack Jansen26ee1261996-11-09 18:45:18 +000044#include "Python.h"
45#include "macglue.h"
46
Guido van Rossumdbfb2821995-02-19 15:51:30 +000047static int arg_count;
48static char *arg_vector[256];
Jack Jansen26ee1261996-11-09 18:45:18 +000049FSSpec PyMac_ApplicationFSSpec;
50char PyMac_ApplicationPath[256];
51static int applocation_inited;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000052
Jack Jansen178652b1995-10-12 10:22:57 +000053/* Duplicate a string to the heap. We also export this since it isn't standard
54** and others use it
55*/
Guido van Rossumdbfb2821995-02-19 15:51:30 +000056
Jack Jansen178652b1995-10-12 10:22:57 +000057char *
Guido van Rossumdbfb2821995-02-19 15:51:30 +000058strdup(char *src)
59{
60 char *dst = malloc(strlen(src) + 1);
61 if (dst)
62 strcpy(dst, src);
63 return dst;
64}
65
Jack Jansen26ee1261996-11-09 18:45:18 +000066/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000067
Jack Jansen41fa7ea1995-08-31 13:59:36 +000068OSErr
Jack Jansen26ee1261996-11-09 18:45:18 +000069PyMac_init_process_location()
Guido van Rossumdbfb2821995-02-19 15:51:30 +000070{
71 ProcessSerialNumber currentPSN;
72 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000073 OSErr err;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000074
Jack Jansen26ee1261996-11-09 18:45:18 +000075 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000076 currentPSN.highLongOfPSN = 0;
77 currentPSN.lowLongOfPSN = kCurrentProcess;
78 info.processInfoLength = sizeof(ProcessInfoRec);
79 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000080 info.processAppSpec = &PyMac_ApplicationFSSpec;
81 if ( err=GetProcessInformation(&currentPSN, &info))
82 return err;
83 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
84 return err;
85 applocation_inited = 1;
86 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000087}
88
89/* Given an FSSpec, return the FSSpec of the parent folder */
90
91static OSErr
92get_folder_parent (FSSpec * fss, FSSpec * parent)
93{
94 CInfoPBRec rec;
95 short err;
96
97 * parent = * fss;
98 rec.hFileInfo.ioNamePtr = parent->name;
99 rec.hFileInfo.ioVRefNum = parent->vRefNum;
100 rec.hFileInfo.ioDirID = parent->parID;
101 rec.hFileInfo.ioFDirIndex = -1;
102 rec.hFileInfo.ioFVersNum = 0;
103 if (err = PBGetCatInfoSync (& rec))
104 return err;
105 parent->parID = rec.dirInfo.ioDrParID;
106/* parent->name[0] = 0; */
107 return 0;
108}
109
110/* Given an FSSpec return a full, colon-separated pathname */
111
Jack Jansen26ee1261996-11-09 18:45:18 +0000112OSErr
113PyMac_GetFullPath (FSSpec *fss, char *buf)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000114{
115 short err;
116 FSSpec fss_parent, fss_current;
117 char tmpbuf[256];
118 int plen;
119
120 fss_current = *fss;
121 plen = fss_current.name[0];
122 memcpy(buf, &fss_current.name[1], plen);
123 buf[plen] = 0;
Jack Jansene1d8c9b1996-11-20 14:58:27 +0000124 /* Special case for disk names */
125 if ( fss_current.parID <= 1 ) {
126 buf[plen++] = ':';
127 buf[plen] = 0;
128 return 0;
129 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000130 while (fss_current.parID > 1) {
131 /* Get parent folder name */
132 if (err = get_folder_parent(&fss_current, &fss_parent))
133 return err;
134 fss_current = fss_parent;
135 /* Prepend path component just found to buf */
136 plen = fss_current.name[0];
137 if (strlen(buf) + plen + 1 > 256) {
138 /* Oops... Not enough space (shouldn't happen) */
139 *buf = 0;
140 return -1;
141 }
142 memcpy(tmpbuf, &fss_current.name[1], plen);
143 tmpbuf[plen] = ':';
144 strcpy(&tmpbuf[plen+1], buf);
145 strcpy(buf, tmpbuf);
146 }
147 return 0;
148}
149
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000150/* Check that there aren't any args remaining in the event */
151
152static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000153get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000154{
155 DescType theType;
156 Size actualSize;
157 OSErr err;
158
159 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
160 &theType, nil, 0, &actualSize);
161 if (err == errAEDescNotFound)
162 return noErr;
163 else
164 return errAEEventNotHandled;
165}
166
167static int got_one; /* Flag that we can stop getting events */
168
169/* Handle the Print or Quit events (by failing) */
170
171static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000172handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000173{
174 #pragma unused (reply, refCon)
175 got_one = 1;
176 return errAEEventNotHandled;
177}
178
179/* Handle the Open Application event (by ignoring it) */
180
181static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000182handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000183{
184 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000185#if 0
186 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000187 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000188#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000189 return get_missing_params(theAppleEvent);
190}
191
192/* Handle the Open Document event, by adding an argument */
193
194static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000195handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000196{
197 #pragma unused (reply, refCon)
198 OSErr err;
199 AEDescList doclist;
200 AEKeyword keywd;
201 DescType rttype;
202 long i, ndocs, size;
203 FSSpec fss;
204 char path[256];
205
206 got_one = 1;
207 if (err = AEGetParamDesc(theAppleEvent,
208 keyDirectObject, typeAEList, &doclist))
209 return err;
210 if (err = get_missing_params(theAppleEvent))
211 return err;
212 if (err = AECountItems(&doclist, &ndocs))
213 return err;
214 for(i = 1; i <= ndocs; i++) {
215 err = AEGetNthPtr(&doclist, i, typeFSS,
216 &keywd, &rttype, &fss, sizeof(fss), &size);
217 if (err)
218 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000219 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000220 arg_vector[arg_count++] = strdup(path);
221 }
222 return err;
223}
224
225/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000226AEEventHandlerUPP open_doc_upp;
227AEEventHandlerUPP open_app_upp;
228AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000229
230static void
231set_ae_handlers()
232{
Jack Jansencc456fb1995-07-29 13:50:59 +0000233 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
234 open_app_upp = NewAEEventHandlerProc(handle_open_app);
235 not_upp = NewAEEventHandlerProc(handle_not);
236
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000237 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000238 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000239 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000240 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000241 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000242 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000243 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000244 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000245}
246
247/* Uninstall standard core event handlers */
248
249static void
250reset_ae_handlers()
251{
252 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000253 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000254 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000255 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000256 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000257 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000258 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000259 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000260}
261
262/* Wait for events until a core event has been handled */
263
264static void
265event_loop()
266{
267 EventRecord event;
268 int n;
269 int ok;
270
271 got_one = 0;
272 for (n = 0; n < 100 && !got_one; n++) {
Jack Jansenca23d912000-06-20 07:40:34 +0000273#ifndef TARGET_API_MAC_CARBON
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000274 SystemTask();
Jack Jansenca23d912000-06-20 07:40:34 +0000275#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000276 ok = GetNextEvent(everyEvent, &event);
277 if (ok && event.what == kHighLevelEvent) {
278 AEProcessAppleEvent(&event);
279 }
280 }
281}
282
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000283/* Get the argv vector, return argc */
284
285int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000286PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000287 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000288 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000289{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000290
291 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000292 (void)PyMac_init_process_location();
293 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000294
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000295 if( !noevents ) {
296 set_ae_handlers();
297 event_loop();
298 reset_ae_handlers();
299 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000300
301 arg_vector[arg_count] = NULL;
302
303 *pargv = arg_vector;
304 return arg_count;
305}