blob: ac4e2868f8245f5c2cbe1ccf827872ae8454b688 [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
Guido van Rossumdbfb2821995-02-19 15:51:30 +000090/* Check that there aren't any args remaining in the event */
91
92static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +000093get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000094{
95 DescType theType;
96 Size actualSize;
97 OSErr err;
98
99 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
100 &theType, nil, 0, &actualSize);
101 if (err == errAEDescNotFound)
102 return noErr;
103 else
104 return errAEEventNotHandled;
105}
106
107static int got_one; /* Flag that we can stop getting events */
108
109/* Handle the Print or Quit events (by failing) */
110
111static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000112handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000113{
114 #pragma unused (reply, refCon)
115 got_one = 1;
116 return errAEEventNotHandled;
117}
118
119/* Handle the Open Application event (by ignoring it) */
120
121static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000122handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000123{
124 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000125#if 0
126 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000127 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000128#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000129 return get_missing_params(theAppleEvent);
130}
131
132/* Handle the Open Document event, by adding an argument */
133
134static pascal OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000135handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, unsigned long refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000136{
137 #pragma unused (reply, refCon)
138 OSErr err;
139 AEDescList doclist;
140 AEKeyword keywd;
141 DescType rttype;
142 long i, ndocs, size;
143 FSSpec fss;
144 char path[256];
145
146 got_one = 1;
147 if (err = AEGetParamDesc(theAppleEvent,
148 keyDirectObject, typeAEList, &doclist))
149 return err;
150 if (err = get_missing_params(theAppleEvent))
151 return err;
152 if (err = AECountItems(&doclist, &ndocs))
153 return err;
154 for(i = 1; i <= ndocs; i++) {
155 err = AEGetNthPtr(&doclist, i, typeFSS,
156 &keywd, &rttype, &fss, sizeof(fss), &size);
157 if (err)
158 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000159 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000160 arg_vector[arg_count++] = strdup(path);
161 }
162 return err;
163}
164
165/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000166AEEventHandlerUPP open_doc_upp;
167AEEventHandlerUPP open_app_upp;
168AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000169
170static void
171set_ae_handlers()
172{
Jack Jansencc456fb1995-07-29 13:50:59 +0000173 open_doc_upp = NewAEEventHandlerProc(handle_open_doc);
174 open_app_upp = NewAEEventHandlerProc(handle_open_app);
175 not_upp = NewAEEventHandlerProc(handle_not);
176
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000177 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000178 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000179 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000180 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000181 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000182 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000183 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000184 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000185}
186
187/* Uninstall standard core event handlers */
188
189static void
190reset_ae_handlers()
191{
192 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000193 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000194 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000195 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000196 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000197 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000198 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000199 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000200}
201
202/* Wait for events until a core event has been handled */
203
204static void
205event_loop()
206{
207 EventRecord event;
208 int n;
209 int ok;
210
211 got_one = 0;
212 for (n = 0; n < 100 && !got_one; n++) {
Jack Jansen74a1e632000-07-14 22:37:27 +0000213#if !TARGET_API_MAC_CARBON
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000214 SystemTask();
Jack Jansenca23d912000-06-20 07:40:34 +0000215#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000216 ok = GetNextEvent(everyEvent, &event);
217 if (ok && event.what == kHighLevelEvent) {
218 AEProcessAppleEvent(&event);
219 }
220 }
221}
222
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000223/* Get the argv vector, return argc */
224
225int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000226PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000227 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000228 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000229{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000230
231 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000232 (void)PyMac_init_process_location();
233 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000234
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000235 if( !noevents ) {
236 set_ae_handlers();
237 event_loop();
238 reset_ae_handlers();
239 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000240
241 arg_vector[arg_count] = NULL;
242
243 *pargv = arg_vector;
244 return arg_count;
245}