blob: a0efa17306382ac6cfe4f0e3f8d0ae0e71a2cf04 [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
30#ifndef SystemSevenOrLater
31#define SystemSevenOrLater 1
32#endif
33
34#include <Types.h>
35#include <Files.h>
36#include <Events.h>
37#include <Memory.h>
38#include <Processes.h>
39#include <Errors.h>
40#include <AppleEvents.h>
41#include <AEObjects.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000042#include <Fonts.h>
Guido van Rossum6fc5aec1995-02-19 23:32:59 +000043#include <TextEdit.h>
44#include <Menus.h>
45#include <Dialogs.h>
46#include <Windows.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000047
Jack Jansen26ee1261996-11-09 18:45:18 +000048#include "Python.h"
49#include "macglue.h"
50
Guido van Rossumdbfb2821995-02-19 15:51:30 +000051#ifdef GENERATINGCFM /* Defined to 0 or 1 in Universal headers */
52#define HAVE_UNIVERSAL_HEADERS
53#endif
54
Jack Jansenf74f63a1995-06-27 13:18:14 +000055#ifdef SYMANTEC__CFM68K__
Guido van Rossumdbfb2821995-02-19 15:51:30 +000056#pragma lib_export on
57#endif
58
59#ifndef HAVE_UNIVERSAL_HEADERS
60#define NewAEEventHandlerProc(x) (x)
61#define AEEventHandlerUPP EventHandlerProcPtr
62#endif
63
64static int arg_count;
65static char *arg_vector[256];
Jack Jansen26ee1261996-11-09 18:45:18 +000066FSSpec PyMac_ApplicationFSSpec;
67char PyMac_ApplicationPath[256];
68static int applocation_inited;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000069
Jack Jansen178652b1995-10-12 10:22:57 +000070/* Duplicate a string to the heap. We also export this since it isn't standard
71** and others use it
72*/
Guido van Rossumdbfb2821995-02-19 15:51:30 +000073
Jack Jansen178652b1995-10-12 10:22:57 +000074char *
Guido van Rossumdbfb2821995-02-19 15:51:30 +000075strdup(char *src)
76{
77 char *dst = malloc(strlen(src) + 1);
78 if (dst)
79 strcpy(dst, src);
80 return dst;
81}
82
Jack Jansen26ee1261996-11-09 18:45:18 +000083/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000084
Jack Jansen41fa7ea1995-08-31 13:59:36 +000085OSErr
Jack Jansen26ee1261996-11-09 18:45:18 +000086PyMac_init_process_location()
Guido van Rossumdbfb2821995-02-19 15:51:30 +000087{
88 ProcessSerialNumber currentPSN;
89 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000090 OSErr err;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000091
Jack Jansen26ee1261996-11-09 18:45:18 +000092 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000093 currentPSN.highLongOfPSN = 0;
94 currentPSN.lowLongOfPSN = kCurrentProcess;
95 info.processInfoLength = sizeof(ProcessInfoRec);
96 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000097 info.processAppSpec = &PyMac_ApplicationFSSpec;
98 if ( err=GetProcessInformation(&currentPSN, &info))
99 return err;
100 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
101 return err;
102 applocation_inited = 1;
103 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000104}
105
106/* Given an FSSpec, return the FSSpec of the parent folder */
107
108static OSErr
109get_folder_parent (FSSpec * fss, FSSpec * parent)
110{
111 CInfoPBRec rec;
112 short err;
113
114 * parent = * fss;
115 rec.hFileInfo.ioNamePtr = parent->name;
116 rec.hFileInfo.ioVRefNum = parent->vRefNum;
117 rec.hFileInfo.ioDirID = parent->parID;
118 rec.hFileInfo.ioFDirIndex = -1;
119 rec.hFileInfo.ioFVersNum = 0;
120 if (err = PBGetCatInfoSync (& rec))
121 return err;
122 parent->parID = rec.dirInfo.ioDrParID;
123/* parent->name[0] = 0; */
124 return 0;
125}
126
127/* Given an FSSpec return a full, colon-separated pathname */
128
Jack Jansen26ee1261996-11-09 18:45:18 +0000129OSErr
130PyMac_GetFullPath (FSSpec *fss, char *buf)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000131{
132 short err;
133 FSSpec fss_parent, fss_current;
134 char tmpbuf[256];
135 int plen;
136
137 fss_current = *fss;
138 plen = fss_current.name[0];
139 memcpy(buf, &fss_current.name[1], plen);
140 buf[plen] = 0;
Jack Jansene1d8c9b1996-11-20 14:58:27 +0000141 /* Special case for disk names */
142 if ( fss_current.parID <= 1 ) {
143 buf[plen++] = ':';
144 buf[plen] = 0;
145 return 0;
146 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000147 while (fss_current.parID > 1) {
148 /* Get parent folder name */
149 if (err = get_folder_parent(&fss_current, &fss_parent))
150 return err;
151 fss_current = fss_parent;
152 /* Prepend path component just found to buf */
153 plen = fss_current.name[0];
154 if (strlen(buf) + plen + 1 > 256) {
155 /* Oops... Not enough space (shouldn't happen) */
156 *buf = 0;
157 return -1;
158 }
159 memcpy(tmpbuf, &fss_current.name[1], plen);
160 tmpbuf[plen] = ':';
161 strcpy(&tmpbuf[plen+1], buf);
162 strcpy(buf, tmpbuf);
163 }
164 return 0;
165}
166
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000167/* Check that there aren't any args remaining in the event */
168
169static OSErr
170get_missing_params(AppleEvent *theAppleEvent)
171{
172 DescType theType;
173 Size actualSize;
174 OSErr err;
175
176 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
177 &theType, nil, 0, &actualSize);
178 if (err == errAEDescNotFound)
179 return noErr;
180 else
181 return errAEEventNotHandled;
182}
183
184static int got_one; /* Flag that we can stop getting events */
185
186/* Handle the Print or Quit events (by failing) */
187
188static pascal OSErr
189handle_not(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
190{
191 #pragma unused (reply, refCon)
192 got_one = 1;
193 return errAEEventNotHandled;
194}
195
196/* Handle the Open Application event (by ignoring it) */
197
198static pascal OSErr
199handle_open_app(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
200{
201 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000202#if 0
203 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000204 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000205#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206 return get_missing_params(theAppleEvent);
207}
208
209/* Handle the Open Document event, by adding an argument */
210
211static pascal OSErr
212handle_open_doc(AppleEvent *theAppleEvent, AppleEvent *reply, long refCon)
213{
214 #pragma unused (reply, refCon)
215 OSErr err;
216 AEDescList doclist;
217 AEKeyword keywd;
218 DescType rttype;
219 long i, ndocs, size;
220 FSSpec fss;
221 char path[256];
222
223 got_one = 1;
224 if (err = AEGetParamDesc(theAppleEvent,
225 keyDirectObject, typeAEList, &doclist))
226 return err;
227 if (err = get_missing_params(theAppleEvent))
228 return err;
229 if (err = AECountItems(&doclist, &ndocs))
230 return err;
231 for(i = 1; i <= ndocs; i++) {
232 err = AEGetNthPtr(&doclist, i, typeFSS,
233 &keywd, &rttype, &fss, sizeof(fss), &size);
234 if (err)
235 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000236 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000237 arg_vector[arg_count++] = strdup(path);
238 }
239 return err;
240}
241
242/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000243AEEventHandlerUPP open_doc_upp;
244AEEventHandlerUPP open_app_upp;
245AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000246
247static void
248set_ae_handlers()
249{
Jack Jansencc456fb1995-07-29 13:50:59 +0000250 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
251 open_app_upp = NewAEEventHandlerProc(handle_open_app);
252 not_upp = NewAEEventHandlerProc(handle_not);
253
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000254 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000255 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000256 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000257 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000258 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000259 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000260 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000261 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000262}
263
264/* Uninstall standard core event handlers */
265
266static void
267reset_ae_handlers()
268{
269 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000270 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000271 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000272 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000273 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000274 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000275 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000276 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000277}
278
279/* Wait for events until a core event has been handled */
280
281static void
282event_loop()
283{
284 EventRecord event;
285 int n;
286 int ok;
287
288 got_one = 0;
289 for (n = 0; n < 100 && !got_one; n++) {
290 SystemTask();
291 ok = GetNextEvent(everyEvent, &event);
292 if (ok && event.what == kHighLevelEvent) {
293 AEProcessAppleEvent(&event);
294 }
295 }
296}
297
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000298/* Get the argv vector, return argc */
299
300int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000301PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000302 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000303 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000304{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000305
306 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000307 (void)PyMac_init_process_location();
308 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000309
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000310 if( !noevents ) {
311 set_ae_handlers();
312 event_loop();
313 reset_ae_handlers();
314 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000315
316 arg_vector[arg_count] = NULL;
317
318 *pargv = arg_vector;
319 return arg_count;
320}