blob: 180de33d13c9e6bbb71bb72ecffc5057a67727ba [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 +000048#if UNIVERSAL_INTERFACES_VERSION >= 0x0340
49typedef long refcontype;
50#else
51typedef unsigned long refcontype;
52#endif
53
Jack Jansen26ee1261996-11-09 18:45:18 +000054#include "Python.h"
55#include "macglue.h"
56
Jack Jansen697842f2001-09-10 22:00:39 +000057#define PATHNAMELEN 256
Jack Jansen697842f2001-09-10 22:00:39 +000058
Guido van Rossumdbfb2821995-02-19 15:51:30 +000059static int arg_count;
60static char *arg_vector[256];
Jack Jansen26ee1261996-11-09 18:45:18 +000061FSSpec PyMac_ApplicationFSSpec;
Jack Jansen697842f2001-09-10 22:00:39 +000062char PyMac_ApplicationPath[PATHNAMELEN];
Guido van Rossumdbfb2821995-02-19 15:51:30 +000063
Jack Jansen178652b1995-10-12 10:22:57 +000064/* Duplicate a string to the heap. We also export this since it isn't standard
65** and others use it
66*/
Jack Jansen9ae898b2000-07-11 21:16:03 +000067#ifndef HAVE_STRDUP
Jack Jansen178652b1995-10-12 10:22:57 +000068char *
Jack Jansen9ae898b2000-07-11 21:16:03 +000069strdup(const char *src)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000070{
71 char *dst = malloc(strlen(src) + 1);
72 if (dst)
73 strcpy(dst, src);
74 return dst;
75}
Jack Jansen9ae898b2000-07-11 21:16:03 +000076#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +000077
Jack Jansen26ee1261996-11-09 18:45:18 +000078/* Initialize FSSpec and full name of current application */
Guido van Rossumdbfb2821995-02-19 15:51:30 +000079
Jack Jansen41fa7ea1995-08-31 13:59:36 +000080OSErr
Jack Jansendff77702001-09-05 22:07:52 +000081PyMac_init_process_location(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +000082{
83 ProcessSerialNumber currentPSN;
84 ProcessInfoRec info;
Jack Jansen26ee1261996-11-09 18:45:18 +000085 OSErr err;
Jack Jansendff77702001-09-05 22:07:52 +000086 static int applocation_inited;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000087
Jack Jansen26ee1261996-11-09 18:45:18 +000088 if ( applocation_inited ) return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +000089 currentPSN.highLongOfPSN = 0;
90 currentPSN.lowLongOfPSN = kCurrentProcess;
91 info.processInfoLength = sizeof(ProcessInfoRec);
92 info.processName = NULL;
Jack Jansen26ee1261996-11-09 18:45:18 +000093 info.processAppSpec = &PyMac_ApplicationFSSpec;
94 if ( err=GetProcessInformation(&currentPSN, &info))
95 return err;
Jack Jansen697842f2001-09-10 22:00:39 +000096 if ( err=PyMac_GetFullPathname(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath, PATHNAMELEN) )
Jack Jansen26ee1261996-11-09 18:45:18 +000097 return err;
98 applocation_inited = 1;
99 return 0;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000100}
101
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000102/* Check that there aren't any args remaining in the event */
103
104static OSErr
Jack Jansen14cd7502000-06-02 21:23:09 +0000105get_missing_params(const AppleEvent *theAppleEvent)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000106{
107 DescType theType;
108 Size actualSize;
109 OSErr err;
110
111 err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
112 &theType, nil, 0, &actualSize);
113 if (err == errAEDescNotFound)
114 return noErr;
115 else
116 return errAEEventNotHandled;
117}
118
119static int got_one; /* Flag that we can stop getting events */
120
121/* Handle the Print or Quit events (by failing) */
122
123static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000124handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000125{
126 #pragma unused (reply, refCon)
127 got_one = 1;
128 return errAEEventNotHandled;
129}
130
131/* Handle the Open Application event (by ignoring it) */
132
133static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000134handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000135{
136 #pragma unused (reply, refCon)
Jack Jansendbe75ae1995-11-10 14:54:16 +0000137#if 0
138 /* Test by Jack: would removing this facilitate debugging? */
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000139 got_one = 1;
Jack Jansendbe75ae1995-11-10 14:54:16 +0000140#endif
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000141 return get_missing_params(theAppleEvent);
142}
143
144/* Handle the Open Document event, by adding an argument */
145
146static pascal OSErr
Jack Jansen5daef312001-06-20 20:50:19 +0000147handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000148{
149 #pragma unused (reply, refCon)
150 OSErr err;
151 AEDescList doclist;
152 AEKeyword keywd;
153 DescType rttype;
154 long i, ndocs, size;
155 FSSpec fss;
Jack Jansen697842f2001-09-10 22:00:39 +0000156 char path[PATHNAMELEN];
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000157
158 got_one = 1;
Jack Jansendff77702001-09-05 22:07:52 +0000159 if ((err = AEGetParamDesc(theAppleEvent,
160 keyDirectObject, typeAEList, &doclist)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000161 return err;
Jack Jansendff77702001-09-05 22:07:52 +0000162 if ((err = get_missing_params(theAppleEvent)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000163 return err;
Jack Jansendff77702001-09-05 22:07:52 +0000164 if ((err = AECountItems(&doclist, &ndocs)))
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000165 return err;
166 for(i = 1; i <= ndocs; i++) {
167 err = AEGetNthPtr(&doclist, i, typeFSS,
168 &keywd, &rttype, &fss, sizeof(fss), &size);
169 if (err)
170 break;
Jack Jansen697842f2001-09-10 22:00:39 +0000171 PyMac_GetFullPathname(&fss, path, PATHNAMELEN);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000172 arg_vector[arg_count++] = strdup(path);
173 }
174 return err;
175}
176
177/* Install standard core event handlers */
Jack Jansencc456fb1995-07-29 13:50:59 +0000178AEEventHandlerUPP open_doc_upp;
179AEEventHandlerUPP open_app_upp;
180AEEventHandlerUPP not_upp;
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000181
182static void
Jack Jansendff77702001-09-05 22:07:52 +0000183set_ae_handlers(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000184{
Jack Jansen5daef312001-06-20 20:50:19 +0000185 open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
186 open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
187 not_upp = NewAEEventHandlerUPP(&handle_not);
Jack Jansencc456fb1995-07-29 13:50:59 +0000188
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000189 AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000190 open_app_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000191 AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000192 open_doc_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000193 AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000194 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000195 AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000196 not_upp, 0L, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000197}
198
199/* Uninstall standard core event handlers */
200
201static void
Jack Jansendff77702001-09-05 22:07:52 +0000202reset_ae_handlers(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000203{
204 AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000205 open_app_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000206 AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000207 open_doc_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000208 AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
Jack Jansencc456fb1995-07-29 13:50:59 +0000209 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000210 AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
Jack Jansencc456fb1995-07-29 13:50:59 +0000211 not_upp, false);
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000212}
213
214/* Wait for events until a core event has been handled */
215
216static void
Jack Jansendff77702001-09-05 22:07:52 +0000217event_loop(void)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000218{
219 EventRecord event;
220 int n;
221 int ok;
222
223 got_one = 0;
224 for (n = 0; n < 100 && !got_one; n++) {
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000225 ok = GetNextEvent(everyEvent, &event);
226 if (ok && event.what == kHighLevelEvent) {
227 AEProcessAppleEvent(&event);
228 }
229 }
230}
231
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000232/* Get the argv vector, return argc */
233
234int
Jack Jansendff77702001-09-05 22:07:52 +0000235PyMac_GetArgv(char ***pargv, int noevents)
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000236{
Guido van Rossumdbfb2821995-02-19 15:51:30 +0000237 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}