blob: b8457816920f080c1f768557c145236afa85094d [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*/
Jack Jansen9ae898b2000-07-11 21:16:03 +000056#ifndef HAVE_STRDUP
Jack Jansen178652b1995-10-12 10:22:57 +000057char *
Jack Jansen9ae898b2000-07-11 21:16:03 +000058strdup(const char *src)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000059{
60 char *dst = malloc(strlen(src) + 1);
61 if (dst)
62 strcpy(dst, src);
63 return dst;
64}
Jack Jansen9ae898b2000-07-11 21:16:03 +000065#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +000066
Jack Jansen26ee1261996-11-09 18:45:18 +000067/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000068
Jack Jansen41fa7ea1995-08-31 13:59:36 +000069OSErr
Jack Jansen26ee1261996-11-09 18:45:18 +000070PyMac_init_process_location()
Guido van Rossumdbfb2821995-02-19 15:51:30 +000071{
72 ProcessSerialNumber currentPSN;
73 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000074 OSErr err;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000075
Jack Jansen26ee1261996-11-09 18:45:18 +000076 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000077 currentPSN.highLongOfPSN = 0;
78 currentPSN.lowLongOfPSN = kCurrentProcess;
79 info.processInfoLength = sizeof(ProcessInfoRec);
80 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000081 info.processAppSpec = &PyMac_ApplicationFSSpec;
82 if ( err=GetProcessInformation(&currentPSN, &info))
83 return err;
84 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
85 return err;
86 applocation_inited = 1;
87 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000088}
89
90/* Given an FSSpec, return the FSSpec of the parent folder */
91
92static OSErr
93get_folder_parent (FSSpec * fss, FSSpec * parent)
94{
95 CInfoPBRec rec;
96 short err;
97
98 * parent = * fss;
99 rec.hFileInfo.ioNamePtr = parent->name;
100 rec.hFileInfo.ioVRefNum = parent->vRefNum;
101 rec.hFileInfo.ioDirID = parent->parID;
102 rec.hFileInfo.ioFDirIndex = -1;
103 rec.hFileInfo.ioFVersNum = 0;
104 if (err = PBGetCatInfoSync (& rec))
105 return err;
106 parent->parID = rec.dirInfo.ioDrParID;
107/* parent->name[0] = 0; */
108 return 0;
109}
110
111/* Given an FSSpec return a full, colon-separated pathname */
112
Jack Jansen26ee1261996-11-09 18:45:18 +0000113OSErr
114PyMac_GetFullPath (FSSpec *fss, char *buf)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000115{
116 short err;
117 FSSpec fss_parent, fss_current;
118 char tmpbuf[256];
119 int plen;
120
121 fss_current = *fss;
122 plen = fss_current.name[0];
123 memcpy(buf, &fss_current.name[1], plen);
124 buf[plen] = 0;
Jack Jansene1d8c9b1996-11-20 14:58:27 +0000125 /* Special case for disk names */
126 if ( fss_current.parID <= 1 ) {
127 buf[plen++] = ':';
128 buf[plen] = 0;
129 return 0;
130 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000131 while (fss_current.parID > 1) {
132 /* Get parent folder name */
133 if (err = get_folder_parent(&fss_current, &fss_parent))
134 return err;
135 fss_current = fss_parent;
136 /* Prepend path component just found to buf */
137 plen = fss_current.name[0];
138 if (strlen(buf) + plen + 1 > 256) {
139 /* Oops... Not enough space (shouldn't happen) */
140 *buf = 0;
141 return -1;
142 }
143 memcpy(tmpbuf, &fss_current.name[1], plen);
144 tmpbuf[plen] = ':';
145 strcpy(&tmpbuf[plen+1], buf);
146 strcpy(buf, tmpbuf);
147 }
148 return 0;
149}
150
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000151/* Check that there aren't any args remaining in the event */
152
153static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000154get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000155{
156 DescType theType;
157 Size actualSize;
158 OSErr err;
159
160 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
161 &theType, nil, 0, &actualSize);
162 if (err == errAEDescNotFound)
163 return noErr;
164 else
165 return errAEEventNotHandled;
166}
167
168static int got_one; /* Flag that we can stop getting events */
169
170/* Handle the Print or Quit events (by failing) */
171
172static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000173handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000174{
175 #pragma unused (reply, refCon)
176 got_one = 1;
177 return errAEEventNotHandled;
178}
179
180/* Handle the Open Application event (by ignoring it) */
181
182static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000183handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000184{
185 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000186#if 0
187 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000188 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000189#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000190 return get_missing_params(theAppleEvent);
191}
192
193/* Handle the Open Document event, by adding an argument */
194
195static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000196handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000197{
198 #pragma unused (reply, refCon)
199 OSErr err;
200 AEDescList doclist;
201 AEKeyword keywd;
202 DescType rttype;
203 long i, ndocs, size;
204 FSSpec fss;
205 char path[256];
206
207 got_one = 1;
208 if (err = AEGetParamDesc(theAppleEvent,
209 keyDirectObject, typeAEList, &doclist))
210 return err;
211 if (err = get_missing_params(theAppleEvent))
212 return err;
213 if (err = AECountItems(&doclist, &ndocs))
214 return err;
215 for(i = 1; i <= ndocs; i++) {
216 err = AEGetNthPtr(&doclist, i, typeFSS,
217 &keywd, &rttype, &fss, sizeof(fss), &size);
218 if (err)
219 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000220 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000221 arg_vector[arg_count++] = strdup(path);
222 }
223 return err;
224}
225
226/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000227AEEventHandlerUPP open_doc_upp;
228AEEventHandlerUPP open_app_upp;
229AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000230
231static void
232set_ae_handlers()
233{
Jack Jansencc456fb1995-07-29 13:50:59 +0000234 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
235 open_app_upp = NewAEEventHandlerProc(handle_open_app);
236 not_upp = NewAEEventHandlerProc(handle_not);
237
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000238 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000239 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000240 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000241 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000242 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000243 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000244 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000245 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000246}
247
248/* Uninstall standard core event handlers */
249
250static void
251reset_ae_handlers()
252{
253 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000254 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000255 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000256 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000257 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000258 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000259 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000260 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000261}
262
263/* Wait for events until a core event has been handled */
264
265static void
266event_loop()
267{
268 EventRecord event;
269 int n;
270 int ok;
271
272 got_one = 0;
273 for (n = 0; n < 100 && !got_one; n++) {
Jack Jansen74a1e632000-07-14 22:37:27 +0000274#if !TARGET_API_MAC_CARBON
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000275 SystemTask();
Jack Jansenca23d912000-06-20 07:40:34 +0000276#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000277 ok = GetNextEvent(everyEvent, &event);
278 if (ok && event.what == kHighLevelEvent) {
279 AEProcessAppleEvent(&event);
280 }
281 }
282}
283
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000284/* Get the argv vector, return argc */
285
286int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000287PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000288 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000289 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000290{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000291
292 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000293 (void)PyMac_init_process_location();
294 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000295
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000296 if( !noevents ) {
297 set_ae_handlers();
298 event_loop();
299 reset_ae_handlers();
300 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000301
302 arg_vector[arg_count] = NULL;
303
304 *pargv = arg_vector;
305 return arg_count;
306}