1995-02-19 11:51:30 -04:00
|
|
|
/***********************************************************
|
1997-01-31 12:15:11 -04:00
|
|
|
Copyright 1991-1997 by Stichting Mathematisch Centrum, Amsterdam,
|
1995-02-19 11:51:30 -04:00
|
|
|
The Netherlands.
|
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
|
|
|
provided that the above copyright notice appear in all copies and that
|
|
|
|
both that copyright notice and this permission notice appear in
|
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
|
|
|
Centrum or CWI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior permission.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
|
|
|
|
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
|
|
|
|
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
|
|
|
/* Construct argc and argv for main() by using Apple Events */
|
|
|
|
/* From Jack's implementation for STDWIN */
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2001-09-05 19:07:52 -03:00
|
|
|
#ifdef WITHOUT_FRAMEWORKS
|
1995-02-19 11:51:30 -04:00
|
|
|
#include <Types.h>
|
|
|
|
#include <Files.h>
|
|
|
|
#include <Events.h>
|
|
|
|
#include <Memory.h>
|
|
|
|
#include <Processes.h>
|
|
|
|
#include <Errors.h>
|
|
|
|
#include <AppleEvents.h>
|
|
|
|
#include <AEObjects.h>
|
|
|
|
#include <Fonts.h>
|
1995-02-19 19:32:59 -04:00
|
|
|
#include <TextEdit.h>
|
|
|
|
#include <Menus.h>
|
|
|
|
#include <Dialogs.h>
|
|
|
|
#include <Windows.h>
|
2001-09-05 19:07:52 -03:00
|
|
|
#else
|
|
|
|
#include <Carbon/Carbon.h>
|
|
|
|
#endif /* WITHOUT_FRAMEWORKS */
|
1995-02-19 11:51:30 -04:00
|
|
|
|
2001-06-20 17:50:19 -03:00
|
|
|
#if UNIVERSAL_INTERFACES_VERSION >= 0x0340
|
|
|
|
typedef long refcontype;
|
|
|
|
#else
|
|
|
|
typedef unsigned long refcontype;
|
|
|
|
#endif
|
|
|
|
|
1996-11-09 14:45:18 -04:00
|
|
|
#include "Python.h"
|
|
|
|
#include "macglue.h"
|
|
|
|
|
1995-02-19 11:51:30 -04:00
|
|
|
static int arg_count;
|
|
|
|
static char *arg_vector[256];
|
1996-11-09 14:45:18 -04:00
|
|
|
FSSpec PyMac_ApplicationFSSpec;
|
|
|
|
char PyMac_ApplicationPath[256];
|
1995-02-19 11:51:30 -04:00
|
|
|
|
1995-10-12 07:22:57 -03:00
|
|
|
/* Duplicate a string to the heap. We also export this since it isn't standard
|
|
|
|
** and others use it
|
|
|
|
*/
|
2000-07-11 18:16:03 -03:00
|
|
|
#ifndef HAVE_STRDUP
|
1995-10-12 07:22:57 -03:00
|
|
|
char *
|
2000-07-11 18:16:03 -03:00
|
|
|
strdup(const char *src)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
char *dst = malloc(strlen(src) + 1);
|
|
|
|
if (dst)
|
|
|
|
strcpy(dst, src);
|
|
|
|
return dst;
|
|
|
|
}
|
2000-07-11 18:16:03 -03:00
|
|
|
#endif
|
1995-02-19 11:51:30 -04:00
|
|
|
|
2001-09-05 19:07:52 -03:00
|
|
|
#if TARGET_API_MAC_OSX
|
|
|
|
OSErr
|
|
|
|
PyMac_GetFullPath(FSSpec *fss, char *path)
|
|
|
|
{
|
|
|
|
FSRef fsr;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
*path = '\0';
|
|
|
|
err = FSpMakeFSRef(fss, &fsr);
|
|
|
|
if ( err ) return err;
|
|
|
|
err = (OSErr)FSRefMakePath(&fsr, path, 1024);
|
|
|
|
if ( err ) return err;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif /* TARGET_API_MAC_OSX */
|
|
|
|
|
|
|
|
|
|
|
|
#if !TARGET_API_MAC_OSX
|
1996-11-09 14:45:18 -04:00
|
|
|
/* Initialize FSSpec and full name of current application */
|
1995-02-19 11:51:30 -04:00
|
|
|
|
1995-08-31 10:59:36 -03:00
|
|
|
OSErr
|
2001-09-05 19:07:52 -03:00
|
|
|
PyMac_init_process_location(void)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
ProcessSerialNumber currentPSN;
|
|
|
|
ProcessInfoRec info;
|
1996-11-09 14:45:18 -04:00
|
|
|
OSErr err;
|
2001-09-05 19:07:52 -03:00
|
|
|
static int applocation_inited;
|
1995-02-19 11:51:30 -04:00
|
|
|
|
1996-11-09 14:45:18 -04:00
|
|
|
if ( applocation_inited ) return 0;
|
1995-02-19 11:51:30 -04:00
|
|
|
currentPSN.highLongOfPSN = 0;
|
|
|
|
currentPSN.lowLongOfPSN = kCurrentProcess;
|
|
|
|
info.processInfoLength = sizeof(ProcessInfoRec);
|
|
|
|
info.processName = NULL;
|
1996-11-09 14:45:18 -04:00
|
|
|
info.processAppSpec = &PyMac_ApplicationFSSpec;
|
|
|
|
if ( err=GetProcessInformation(¤tPSN, &info))
|
|
|
|
return err;
|
|
|
|
if ( err=PyMac_GetFullPath(&PyMac_ApplicationFSSpec, PyMac_ApplicationPath) )
|
|
|
|
return err;
|
|
|
|
applocation_inited = 1;
|
|
|
|
return 0;
|
1995-02-19 11:51:30 -04:00
|
|
|
}
|
2001-09-05 19:07:52 -03:00
|
|
|
#endif /* !TARGET_API_MAC_OSX */
|
1995-02-19 11:51:30 -04:00
|
|
|
|
|
|
|
/* Check that there aren't any args remaining in the event */
|
|
|
|
|
|
|
|
static OSErr
|
2000-06-02 18:23:09 -03:00
|
|
|
get_missing_params(const AppleEvent *theAppleEvent)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
DescType theType;
|
|
|
|
Size actualSize;
|
|
|
|
OSErr err;
|
|
|
|
|
|
|
|
err = AEGetAttributePtr(theAppleEvent, keyMissedKeywordAttr, typeWildCard,
|
|
|
|
&theType, nil, 0, &actualSize);
|
|
|
|
if (err == errAEDescNotFound)
|
|
|
|
return noErr;
|
|
|
|
else
|
|
|
|
return errAEEventNotHandled;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int got_one; /* Flag that we can stop getting events */
|
|
|
|
|
|
|
|
/* Handle the Print or Quit events (by failing) */
|
|
|
|
|
|
|
|
static pascal OSErr
|
2001-06-20 17:50:19 -03:00
|
|
|
handle_not(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
#pragma unused (reply, refCon)
|
|
|
|
got_one = 1;
|
|
|
|
return errAEEventNotHandled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the Open Application event (by ignoring it) */
|
|
|
|
|
|
|
|
static pascal OSErr
|
2001-06-20 17:50:19 -03:00
|
|
|
handle_open_app(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
#pragma unused (reply, refCon)
|
1995-11-10 10:54:16 -04:00
|
|
|
#if 0
|
|
|
|
/* Test by Jack: would removing this facilitate debugging? */
|
1995-02-19 11:51:30 -04:00
|
|
|
got_one = 1;
|
1995-11-10 10:54:16 -04:00
|
|
|
#endif
|
1995-02-19 11:51:30 -04:00
|
|
|
return get_missing_params(theAppleEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Handle the Open Document event, by adding an argument */
|
|
|
|
|
|
|
|
static pascal OSErr
|
2001-06-20 17:50:19 -03:00
|
|
|
handle_open_doc(const AppleEvent *theAppleEvent, AppleEvent *reply, refcontype refCon)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
#pragma unused (reply, refCon)
|
|
|
|
OSErr err;
|
|
|
|
AEDescList doclist;
|
|
|
|
AEKeyword keywd;
|
|
|
|
DescType rttype;
|
|
|
|
long i, ndocs, size;
|
|
|
|
FSSpec fss;
|
2001-09-05 19:07:52 -03:00
|
|
|
char path[1024];
|
1995-02-19 11:51:30 -04:00
|
|
|
|
|
|
|
got_one = 1;
|
2001-09-05 19:07:52 -03:00
|
|
|
if ((err = AEGetParamDesc(theAppleEvent,
|
|
|
|
keyDirectObject, typeAEList, &doclist)))
|
1995-02-19 11:51:30 -04:00
|
|
|
return err;
|
2001-09-05 19:07:52 -03:00
|
|
|
if ((err = get_missing_params(theAppleEvent)))
|
1995-02-19 11:51:30 -04:00
|
|
|
return err;
|
2001-09-05 19:07:52 -03:00
|
|
|
if ((err = AECountItems(&doclist, &ndocs)))
|
1995-02-19 11:51:30 -04:00
|
|
|
return err;
|
|
|
|
for(i = 1; i <= ndocs; i++) {
|
|
|
|
err = AEGetNthPtr(&doclist, i, typeFSS,
|
|
|
|
&keywd, &rttype, &fss, sizeof(fss), &size);
|
|
|
|
if (err)
|
|
|
|
break;
|
1996-11-09 14:45:18 -04:00
|
|
|
PyMac_GetFullPath(&fss, path);
|
1995-02-19 11:51:30 -04:00
|
|
|
arg_vector[arg_count++] = strdup(path);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Install standard core event handlers */
|
1995-07-29 10:50:59 -03:00
|
|
|
AEEventHandlerUPP open_doc_upp;
|
|
|
|
AEEventHandlerUPP open_app_upp;
|
|
|
|
AEEventHandlerUPP not_upp;
|
1995-02-19 11:51:30 -04:00
|
|
|
|
|
|
|
static void
|
2001-09-05 19:07:52 -03:00
|
|
|
set_ae_handlers(void)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
2001-06-20 17:50:19 -03:00
|
|
|
open_doc_upp = NewAEEventHandlerUPP(&handle_open_doc);
|
|
|
|
open_app_upp = NewAEEventHandlerUPP(&handle_open_app);
|
|
|
|
not_upp = NewAEEventHandlerUPP(&handle_not);
|
1995-07-29 10:50:59 -03:00
|
|
|
|
1995-02-19 11:51:30 -04:00
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
|
1995-07-29 10:50:59 -03:00
|
|
|
open_app_upp, 0L, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
|
1995-07-29 10:50:59 -03:00
|
|
|
open_doc_upp, 0L, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
|
1995-07-29 10:50:59 -03:00
|
|
|
not_upp, 0L, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
|
1995-07-29 10:50:59 -03:00
|
|
|
not_upp, 0L, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Uninstall standard core event handlers */
|
|
|
|
|
|
|
|
static void
|
2001-09-05 19:07:52 -03:00
|
|
|
reset_ae_handlers(void)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
|
1995-07-29 10:50:59 -03:00
|
|
|
open_app_upp, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
|
1995-07-29 10:50:59 -03:00
|
|
|
open_doc_upp, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AERemoveEventHandler(kCoreEventClass, kAEPrintDocuments,
|
1995-07-29 10:50:59 -03:00
|
|
|
not_upp, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
AERemoveEventHandler(kCoreEventClass, kAEQuitApplication,
|
1995-07-29 10:50:59 -03:00
|
|
|
not_upp, false);
|
1995-02-19 11:51:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for events until a core event has been handled */
|
|
|
|
|
|
|
|
static void
|
2001-09-05 19:07:52 -03:00
|
|
|
event_loop(void)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
EventRecord event;
|
|
|
|
int n;
|
|
|
|
int ok;
|
|
|
|
|
|
|
|
got_one = 0;
|
|
|
|
for (n = 0; n < 100 && !got_one; n++) {
|
2000-07-14 19:37:27 -03:00
|
|
|
#if !TARGET_API_MAC_CARBON
|
1995-02-19 11:51:30 -04:00
|
|
|
SystemTask();
|
2000-06-20 04:40:34 -03:00
|
|
|
#endif
|
1995-02-19 11:51:30 -04:00
|
|
|
ok = GetNextEvent(everyEvent, &event);
|
|
|
|
if (ok && event.what == kHighLevelEvent) {
|
|
|
|
AEProcessAppleEvent(&event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the argv vector, return argc */
|
|
|
|
|
|
|
|
int
|
2001-09-05 19:07:52 -03:00
|
|
|
PyMac_GetArgv(char ***pargv, int noevents)
|
1995-02-19 11:51:30 -04:00
|
|
|
{
|
|
|
|
arg_count = 0;
|
2001-09-05 19:07:52 -03:00
|
|
|
#if TARGET_API_MAC_OSX
|
|
|
|
/* In an OSX bundle argv[0] is okay */
|
|
|
|
arg_count++;
|
|
|
|
#else
|
1996-11-09 14:45:18 -04:00
|
|
|
(void)PyMac_init_process_location();
|
|
|
|
arg_vector[arg_count++] = strdup(PyMac_ApplicationPath);
|
2001-09-05 19:07:52 -03:00
|
|
|
#endif /* TARGET_API_MAC_OSX */
|
1995-02-19 11:51:30 -04:00
|
|
|
|
1996-09-07 14:09:31 -03:00
|
|
|
if( !noevents ) {
|
|
|
|
set_ae_handlers();
|
|
|
|
event_loop();
|
|
|
|
reset_ae_handlers();
|
|
|
|
}
|
1995-02-19 11:51:30 -04:00
|
|
|
|
|
|
|
arg_vector[arg_count] = NULL;
|
|
|
|
|
|
|
|
*pargv = arg_vector;
|
|
|
|
return arg_count;
|
|
|
|
}
|