blob: bc5bbd56fe7b418bf5a737b9d2131097401a1535 [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
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000111/* Check that there aren't any args remaining in the event */
112
113static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000114get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000115{
116 DescType theType;
117 Size actualSize;
118 OSErr err;
119
120 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
121 &theType, nil, 0, &actualSize);
122 if (err == errAEDescNotFound)
123 return noErr;
124 else
125 return errAEEventNotHandled;
126}
127
128static int got_one; /* Flag that we can stop getting events */
129
130/* Handle the Print or Quit events (by failing) */
131
132static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000133handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000134{
135 #pragma unused (reply, refCon)
136 got_one = 1;
137 return errAEEventNotHandled;
138}
139
140/* Handle the Open Application event (by ignoring it) */
141
142static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000143handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000144{
145 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000146#if 0
147 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000148 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000149#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000150 return get_missing_params(theAppleEvent);
151}
152
153/* Handle the Open Document event, by adding an argument */
154
155static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000156handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000157{
158 #pragma unused (reply, refCon)
159 OSErr err;
160 AEDescList doclist;
161 AEKeyword keywd;
162 DescType rttype;
163 long i, ndocs, size;
164 FSSpec fss;
165 char path[256];
166
167 got_one = 1;
168 if (err = AEGetParamDesc(theAppleEvent,
169 keyDirectObject, typeAEList, &doclist))
170 return err;
171 if (err = get_missing_params(theAppleEvent))
172 return err;
173 if (err = AECountItems(&doclist, &ndocs))
174 return err;
175 for(i = 1; i <= ndocs; i++) {
176 err = AEGetNthPtr(&doclist, i, typeFSS,
177 &keywd, &rttype, &fss, sizeof(fss), &size);
178 if (err)
179 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000180 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000181 arg_vector[arg_count++] = strdup(path);
182 }
183 return err;
184}
185
186/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000187AEEventHandlerUPP open_doc_upp;
188AEEventHandlerUPP open_app_upp;
189AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000190
191static void
192set_ae_handlers()
193{
Jack Jansencc456fb1995-07-29 13:50:59 +0000194 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
195 open_app_upp = NewAEEventHandlerProc(handle_open_app);
196 not_upp = NewAEEventHandlerProc(handle_not);
197
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000198 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000199 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000200 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000201 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000202 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000203 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000204 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000205 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206}
207
208/* Uninstall standard core event handlers */
209
210static void
211reset_ae_handlers()
212{
213 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000214 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000215 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000216 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000217 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000218 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000219 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000220 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000221}
222
223/* Wait for events until a core event has been handled */
224
225static void
226event_loop()
227{
228 EventRecord event;
229 int n;
230 int ok;
231
232 got_one = 0;
233 for (n = 0; n < 100 && !got_one; n++) {
Jack Jansen74a1e632000-07-14 22:37:27 +0000234#if !TARGET_API_MAC_CARBON
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000235 SystemTask();
Jack Jansenca23d912000-06-20 07:40:34 +0000236#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000237 ok = GetNextEvent(everyEvent, &event);
238 if (ok && event.what == kHighLevelEvent) {
239 AEProcessAppleEvent(&event);
240 }
241 }
242}
243
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000244/* Get the argv vector, return argc */
245
246int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000247PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000248 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000249 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000250{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000251
252 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000253 (void)PyMac_init_process_location();
254 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000255
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000256 if( !noevents ) {
257 set_ae_handlers();
258 event_loop();
259 reset_ae_handlers();
260 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000261
262 arg_vector[arg_count] = NULL;
263
264 *pargv = arg_vector;
265 return arg_count;
266}