blob: 1bd57ad5606813bcf324819177b41968d6e2be8f [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 Jansen5daef312001-06-20 20:50:19 +000044#if UNIVERSAL_INTERFACES_VERSION >= 0x0340
45typedef long refcontype;
46#else
47typedef unsigned long refcontype;
48#endif
49
Jack Jansen26ee1261996-11-09 18:45:18 +000050#include "Python.h"
51#include "macglue.h"
52
Guido van Rossumdbfb2821995-02-19 15:51:30 +000053static int arg_count;
54static char *arg_vector[256];
Jack Jansen26ee1261996-11-09 18:45:18 +000055FSSpec PyMac_ApplicationFSSpec;
56char PyMac_ApplicationPath[256];
57static int applocation_inited;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000058
Jack Jansen178652b1995-10-12 10:22:57 +000059/* Duplicate a string to the heap. We also export this since it isn't standard
60** and others use it
61*/
Jack Jansen9ae898b2000-07-11 21:16:03 +000062#ifndef HAVE_STRDUP
Jack Jansen178652b1995-10-12 10:22:57 +000063char *
Jack Jansen9ae898b2000-07-11 21:16:03 +000064strdup(const char *src)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000065{
66 char *dst = malloc(strlen(src) + 1);
67 if (dst)
68 strcpy(dst, src);
69 return dst;
70}
Jack Jansen9ae898b2000-07-11 21:16:03 +000071#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +000072
Jack Jansen26ee1261996-11-09 18:45:18 +000073/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000074
Jack Jansen41fa7ea1995-08-31 13:59:36 +000075OSErr
Jack Jansen26ee1261996-11-09 18:45:18 +000076PyMac_init_process_location()
Guido van Rossumdbfb2821995-02-19 15:51:30 +000077{
78 ProcessSerialNumber currentPSN;
79 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000080 OSErr err;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000081
Jack Jansen26ee1261996-11-09 18:45:18 +000082 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000083 currentPSN.highLongOfPSN = 0;
84 currentPSN.lowLongOfPSN = kCurrentProcess;
85 info.processInfoLength = sizeof(ProcessInfoRec);
86 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000087 info.processAppSpec = &PyMac_ApplicationFSSpec;
88 if ( err=GetProcessInformation(&currentPSN, &info))
89 return err;
90 if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
91 return err;
92 applocation_inited = 1;
93 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000094}
95
Guido van Rossumdbfb2821995-02-19 15:51:30 +000096/* Check that there aren't any args remaining in the event */
97
98static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +000099get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000100{
101 DescType theType;
102 Size actualSize;
103 OSErr err;
104
105 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
106 &theType, nil, 0, &actualSize);
107 if (err == errAEDescNotFound)
108 return noErr;
109 else
110 return errAEEventNotHandled;
111}
112
113static int got_one; /* Flag that we can stop getting events */
114
115/* Handle the Print or Quit events (by failing) */
116
117static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000118handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000119{
120 #pragma unused (reply, refCon)
121 got_one = 1;
122 return errAEEventNotHandled;
123}
124
125/* Handle the Open Application event (by ignoring it) */
126
127static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000128handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000129{
130 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000131#if 0
132 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000133 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000134#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000135 return get_missing_params(theAppleEvent);
136}
137
138/* Handle the Open Document event, by adding an argument */
139
140static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000141handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000142{
143 #pragma unused (reply, refCon)
144 OSErr err;
145 AEDescList doclist;
146 AEKeyword keywd;
147 DescType rttype;
148 long i, ndocs, size;
149 FSSpec fss;
150 char path[256];
151
152 got_one = 1;
153 if (err = AEGetParamDesc(theAppleEvent,
154 keyDirectObject, typeAEList, &doclist))
155 return err;
156 if (err = get_missing_params(theAppleEvent))
157 return err;
158 if (err = AECountItems(&doclist, &ndocs))
159 return err;
160 for(i = 1; i <= ndocs; i++) {
161 err = AEGetNthPtr(&doclist, i, typeFSS,
162 &keywd, &rttype, &fss, sizeof(fss), &size);
163 if (err)
164 break;
Jack Jansen26ee1261996-11-09 18:45:18 +0000165 PyMac_GetFullPath(&fss, path);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000166 arg_vector[arg_count++] = strdup(path);
167 }
168 return err;
169}
170
171/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000172AEEventHandlerUPP open_doc_upp;
173AEEventHandlerUPP open_app_upp;
174AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000175
176static void
177set_ae_handlers()
178{
Jack Jansen5daef312001-06-20 20:50:19 +0000179 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
180 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
181 not_upp = NewAEEventHandlerUPP(&handle_not);
Jack Jansencc456fb1995-07-29 13:50:59 +0000182
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000183 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000184 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000185 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000186 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000187 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000188 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000189 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000190 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000191}
192
193/* Uninstall standard core event handlers */
194
195static void
196reset_ae_handlers()
197{
198 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000199 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000200 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000201 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000202 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000203 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000204 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000205 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206}
207
208/* Wait for events until a core event has been handled */
209
210static void
211event_loop()
212{
213 EventRecord event;
214 int n;
215 int ok;
216
217 got_one = 0;
218 for (n = 0; n < 100 && !got_one; n++) {
Jack Jansen74a1e632000-07-14 22:37:27 +0000219#if !TARGET_API_MAC_CARBON
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000220 SystemTask();
Jack Jansenca23d912000-06-20 07:40:34 +0000221#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000222 ok = GetNextEvent(everyEvent, &event);
223 if (ok && event.what == kHighLevelEvent) {
224 AEProcessAppleEvent(&event);
225 }
226 }
227}
228
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000229/* Get the argv vector, return argc */
230
231int
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000232PyMac_GetArgv(pargv, noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000233 char ***pargv;
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000234 int noevents;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000235{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000236
237 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000238 (void)PyMac_init_process_location();
239 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000240
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000241 if( !noevents ) {
242 set_ae_handlers();
243 event_loop();
244 reset_ae_handlers();
245 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000246
247 arg_vector[arg_count] = NULL;
248
249 *pargv = arg_vector;
250 return arg_count;
251}