blob: f301bab0746da9b76059d79b63509b682bcc501c [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
Jack Jansendff77702001-09-05 22:07:52 +000030#ifdef WITHOUT_FRAMEWORKS
Guido van Rossumdbfb2821995-02-19 15:51:30 +000031#include <Types.h>
32#include <Files.h>
33#include <Events.h>
34#include <Memory.h>
35#include <Processes.h>
36#include <Errors.h>
37#include <AppleEvents.h>
38#include <AEObjects.h>
Guido van Rossumdbfb2821995-02-19 15:51:30 +000039#include <Fonts.h>
Guido van Rossum6fc5aec1995-02-19 23:32:59 +000040#include <TextEdit.h>
41#include <Menus.h>
42#include <Dialogs.h>
43#include <Windows.h>
Jack Jansendff77702001-09-05 22:07:52 +000044#else
45#include <Carbon/Carbon.h>
46#endif /* WITHOUT_FRAMEWORKS */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000047
Jack Jansen5daef312001-06-20 20:50:19 +000048typedef long refcontype;
Jack Jansen5daef312001-06-20 20:50:19 +000049
Jack Jansen26ee1261996-11-09 18:45:18 +000050#include "Python.h"
51#include "macglue.h"
52
Jack Jansen697842f2001-09-10 22:00:39 +000053#define PATHNAMELEN 256
Jack Jansen697842f2001-09-10 22:00:39 +000054
Guido van Rossumdbfb2821995-02-19 15:51:30 +000055static int arg_count;
56static char *arg_vector[256];
Jack Jansen26ee1261996-11-09 18:45:18 +000057FSSpec PyMac_ApplicationFSSpec;
Jack Jansen697842f2001-09-10 22:00:39 +000058char PyMac_ApplicationPath[PATHNAMELEN];
Guido van Rossumdbfb2821995-02-19 15:51:30 +000059
Jack Jansen178652b1995-10-12 10:22:57 +000060/* Duplicate a string to the heap. We also export this since it isn't standard
61** and others use it
62*/
Jack Jansen9ae898b2000-07-11 21:16:03 +000063#ifndef HAVE_STRDUP
Jack Jansen178652b1995-10-12 10:22:57 +000064char *
Jack Jansen9ae898b2000-07-11 21:16:03 +000065strdup(const char *src)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000066{
67 char *dst = malloc(strlen(src) + 1);
68 if (dst)
69 strcpy(dst, src);
70 return dst;
71}
Jack Jansen9ae898b2000-07-11 21:16:03 +000072#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +000073
Jack Jansen26ee1261996-11-09 18:45:18 +000074/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000075
Jack Jansen41fa7ea1995-08-31 13:59:36 +000076OSErr
Jack Jansendff77702001-09-05 22:07:52 +000077PyMac_init_process_location(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000078{
79 ProcessSerialNumber currentPSN;
80 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000081 OSErr err;
Jack Jansendff77702001-09-05 22:07:52 +000082 static int applocation_inited;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000083
Jack Jansen26ee1261996-11-09 18:45:18 +000084 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000085 currentPSN.highLongOfPSN = 0;
86 currentPSN.lowLongOfPSN = kCurrentProcess;
87 info.processInfoLength = sizeof(ProcessInfoRec);
88 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000089 info.processAppSpec = &PyMac_ApplicationFSSpec;
90 if ( err=GetProcessInformation(&currentPSN, &info))
91 return err;
Jack Jansen697842f2001-09-10 22:00:39 +000092 if ( err=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath, PATHNAMELEN) )
Jack Jansen26ee1261996-11-09 18:45:18 +000093 return err;
94 applocation_inited = 1;
95 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000096}
97
Guido van Rossumdbfb2821995-02-19 15:51:30 +000098/* Check that there aren't any args remaining in the event */
99
100static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000101get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000102{
103 DescType theType;
104 Size actualSize;
105 OSErr err;
106
107 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
108 &theType, nil, 0, &actualSize);
109 if (err == errAEDescNotFound)
110 return noErr;
111 else
112 return errAEEventNotHandled;
113}
114
115static int got_one; /* Flag that we can stop getting events */
116
117/* Handle the Print or Quit events (by failing) */
118
119static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000120handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000121{
122 #pragma unused (reply, refCon)
123 got_one = 1;
124 return errAEEventNotHandled;
125}
126
127/* Handle the Open Application event (by ignoring it) */
128
129static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000130handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000131{
132 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000133#if 0
134 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000135 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000136#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000137 return get_missing_params(theAppleEvent);
138}
139
140/* Handle the Open Document event, by adding an argument */
141
142static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000143handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000144{
145 #pragma unused (reply, refCon)
146 OSErr err;
147 AEDescList doclist;
148 AEKeyword keywd;
149 DescType rttype;
150 long i, ndocs, size;
151 FSSpec fss;
Jack Jansen697842f2001-09-10 22:00:39 +0000152 char path[PATHNAMELEN];
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000153
154 got_one = 1;
Jack Jansendff77702001-09-05 22:07:52 +0000155 if ((err = AEGetParamDesc(theAppleEvent,
156 keyDirectObject, typeAEList, &doclist)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000157 return err;
Jack Jansendff77702001-09-05 22:07:52 +0000158 if ((err = get_missing_params(theAppleEvent)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000159 return err;
Jack Jansendff77702001-09-05 22:07:52 +0000160 if ((err = AECountItems(&doclist, &ndocs)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000161 return err;
162 for(i = 1; i <= ndocs; i++) {
163 err = AEGetNthPtr(&doclist, i, typeFSS,
164 &keywd, &rttype, &fss, sizeof(fss), &size);
165 if (err)
166 break;
Jack Jansen697842f2001-09-10 22:00:39 +0000167 PyMac_GetFullPathname(&fss, path, PATHNAMELEN);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000168 arg_vector[arg_count++] = strdup(path);
169 }
170 return err;
171}
172
173/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000174AEEventHandlerUPP open_doc_upp;
175AEEventHandlerUPP open_app_upp;
176AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000177
178static void
Jack Jansendff77702001-09-05 22:07:52 +0000179set_ae_handlers(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000180{
Jack Jansen5daef312001-06-20 20:50:19 +0000181 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
182 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
183 not_upp = NewAEEventHandlerUPP(&handle_not);
Jack Jansencc456fb1995-07-29 13:50:59 +0000184
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000185 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000186 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000187 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000188 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000189 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000190 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000191 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000192 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000193}
194
195/* Uninstall standard core event handlers */
196
197static void
Jack Jansendff77702001-09-05 22:07:52 +0000198reset_ae_handlers(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000199{
200 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000201 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000202 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000203 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000204 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000205 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000207 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000208}
209
210/* Wait for events until a core event has been handled */
211
212static void
Jack Jansendff77702001-09-05 22:07:52 +0000213event_loop(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000214{
215 EventRecord event;
216 int n;
217 int ok;
218
219 got_one = 0;
220 for (n = 0; n < 100 && !got_one; n++) {
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000221 ok = GetNextEvent(everyEvent, &event);
222 if (ok && event.what == kHighLevelEvent) {
223 AEProcessAppleEvent(&event);
224 }
225 }
226}
227
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000228/* Get the argv vector, return argc */
229
230int
Jack Jansendff77702001-09-05 22:07:52 +0000231PyMac_GetArgv(char ***pargv, int noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000232{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000233 arg_count = 0;
Jack Jansen26ee1261996-11-09 18:45:18 +0000234 (void)PyMac_init_process_location();
235 arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000236
Jack Jansen7d5f9e81996-09-07 17:09:31 +0000237 if( !noevents ) {
238 set_ae_handlers();
239 event_loop();
240 reset_ae_handlers();
241 }
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000242
243 arg_vector[arg_count] = NULL;
244
245 *pargv = arg_vector;
246 return arg_count;
247}