1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
|
/*
* tkMacHLEvents.c --
*
* Implements high level event support for the Macintosh. Currently,
* the only event that really does anything is the Quit event.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*
* SCCS: @(#) tkMacHLEvents.c 1.22 97/11/07 21:20:50
*/
#include "tcl.h"
#include "tclMacInt.h"
#include "tkMacInt.h"
#include <Aliases.h>
#include <AppleEvents.h>
#include <SegLoad.h>
#include <ToolUtils.h>
/*
* This is a Tcl_Event structure that the Quit AppleEvent handler
* uses to schedule the tkReallyKillMe function.
*/
typedef struct KillEvent {
Tcl_Event header; /* Information that is standard for
* all events. */
Tcl_Interp *interp; /* Interp that was passed to the
* Quit AppleEvent */
} KillEvent;
/*
* Static functions used only in this file.
*/
static pascal OSErr QuitHandler _ANSI_ARGS_((AppleEvent* event,
AppleEvent* reply, long refcon));
static pascal OSErr OappHandler _ANSI_ARGS_((AppleEvent* event,
AppleEvent* reply, long refcon));
static pascal OSErr OdocHandler _ANSI_ARGS_((AppleEvent* event,
AppleEvent* reply, long refcon));
static pascal OSErr PrintHandler _ANSI_ARGS_((AppleEvent* event,
AppleEvent* reply, long refcon));
static pascal OSErr ScriptHandler _ANSI_ARGS_((AppleEvent* event,
AppleEvent* reply, long refcon));
static int MissedAnyParameters _ANSI_ARGS_((AppleEvent *theEvent));
static int ReallyKillMe _ANSI_ARGS_((Tcl_Event *eventPtr, int flags));
/*
*----------------------------------------------------------------------
*
* TkMacInitAppleEvents --
*
* Initilize the Apple Events on the Macintosh. This registers the
* core event handlers.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
void
TkMacInitAppleEvents(
Tcl_Interp *interp) /* Interp to handle basic events. */
{
OSErr err;
AEEventHandlerUPP OappHandlerUPP, OdocHandlerUPP,
PrintHandlerUPP, QuitHandlerUPP, ScriptHandlerUPP;
/*
* Install event handlers for the core apple events.
*/
QuitHandlerUPP = NewAEEventHandlerProc(QuitHandler);
err = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
QuitHandlerUPP, (long) interp, false);
OappHandlerUPP = NewAEEventHandlerProc(OappHandler);
err = AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
OappHandlerUPP, (long) interp, false);
OdocHandlerUPP = NewAEEventHandlerProc(OdocHandler);
err = AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
OdocHandlerUPP, (long) interp, false);
PrintHandlerUPP = NewAEEventHandlerProc(PrintHandler);
err = AEInstallEventHandler(kCoreEventClass, kAEPrintDocuments,
PrintHandlerUPP, (long) interp, false);
if (interp != NULL) {
ScriptHandlerUPP = NewAEEventHandlerProc(ScriptHandler);
err = AEInstallEventHandler('misc', 'dosc',
ScriptHandlerUPP, (long) interp, false);
}
}
/*
*----------------------------------------------------------------------
*
* TkMacDoHLEvent --
*
* Dispatch incomming highlevel events.
*
* Results:
* None.
*
* Side effects:
* Depends on the incoming event.
*
*----------------------------------------------------------------------
*/
void
TkMacDoHLEvent(
EventRecord *theEvent)
{
AEProcessAppleEvent(theEvent);
return;
}
/*
*----------------------------------------------------------------------
*
* QuitHandler, OappHandler, etc. --
*
* These are the core Apple event handlers. Only the Quit event does
* anything interesting.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static pascal OSErr
QuitHandler(
AppleEvent *theAppleEvent,
AppleEvent *reply,
long handlerRefcon)
{
Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon;
KillEvent *eventPtr;
/*
* Call the exit command from the event loop, since you are not supposed
* to call ExitToShell in an Apple Event Handler. We put this at the head
* of Tcl's event queue because this message usually comes when the Mac is
* shutting down, and we want to kill the shell as quickly as possible.
*/
eventPtr = (KillEvent *) ckalloc(sizeof(KillEvent));
eventPtr->header.proc = ReallyKillMe;
eventPtr->interp = interp;
Tcl_QueueEvent((Tcl_Event *) eventPtr, TCL_QUEUE_HEAD);
return noErr;
}
static pascal OSErr
OappHandler(
AppleEvent *theAppleEvent,
AppleEvent *reply,
long handlerRefcon)
{
return noErr;
}
static pascal OSErr
OdocHandler(
AppleEvent *theAppleEvent,
AppleEvent *reply,
long handlerRefcon)
{
Tcl_Interp *interp = (Tcl_Interp *) handlerRefcon;
AEDescList fileSpecList;
FSSpec file;
OSErr err;
DescType type;
Size actual;
long count;
AEKeyword keyword;
long index;
Tcl_DString command;
Tcl_DString pathName;
Tcl_CmdInfo dummy;
/*
* Don't bother if we don't have an interp or
* the open document procedure doesn't exist.
*/
if ((interp == NULL) ||
(Tcl_GetCommandInfo(interp, "tkOpenDocument", &dummy)) == 0) {
return noErr;
}
/*
* If we get any errors wil retrieving our parameters
* we just return with no error.
*/
err = AEGetParamDesc(theAppleEvent, keyDirectObject,
typeAEList, &fileSpecList);
if (err != noErr) {
return noErr;
}
err = MissedAnyParameters(theAppleEvent);
if (err != noErr) {
return noErr;
}
err = AECountItems(&fileSpecList, &count);
if (err != noErr) {
return noErr;
}
Tcl_DStringInit(&command);
Tcl_DStringInit(&pathName);
Tcl_DStringAppend(&command, "tkOpenDocument", -1);
for (index = 1; index <= count; index++) {
int length;
Handle fullPath;
Tcl_DStringSetLength(&pathName, 0);
err = AEGetNthPtr(&fileSpecList, index, typeFSS,
&keyword, &type, (Ptr) &file, sizeof(FSSpec), &actual);
if ( err != noErr ) {
continue;
}
err = FSpPathFromLocation(&file, &length, &fullPath);
HLock(fullPath);
Tcl_DStringAppend(&pathName, *fullPath, length);
HUnlock(fullPath);
DisposeHandle(fullPath);
Tcl_DStringAppendElement(&command, pathName.string);
}
Tcl_GlobalEval(interp, command.string);
Tcl_DStringFree(&command);
Tcl_DStringFree(&pathName);
return noErr;
}
static pascal OSErr
PrintHandler(
AppleEvent *theAppleEvent,
AppleEvent *reply,
long handlerRefcon)
{
return noErr;
}
/*
*----------------------------------------------------------------------
*
* DoScriptHandler --
*
* This handler process the do script event.
*
* Results:
* Scedules the given event to be processed.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static pascal OSErr
ScriptHandler(
AppleEvent *theAppleEvent,
AppleEvent *reply,
long handlerRefcon)
{
OSErr theErr;
AEDescList theDesc;
int tclErr = -1;
Tcl_Interp *interp;
char errString[128];
interp = (Tcl_Interp *) handlerRefcon;
/*
* The do script event receives one parameter that should be data or a file.
*/
theErr = AEGetParamDesc(theAppleEvent, keyDirectObject, typeWildCard,
&theDesc);
if (theErr != noErr) {
sprintf(errString, "AEDoScriptHandler: GetParamDesc error %d", theErr);
theErr = AEPutParamPtr(reply, keyErrorString, typeChar, errString,
strlen(errString));
} else if (MissedAnyParameters(theAppleEvent)) {
sprintf(errString, "AEDoScriptHandler: extra parameters");
AEPutParamPtr(reply, keyErrorString, typeChar, errString,
strlen(errString));
theErr = -1771;
} else {
if (theDesc.descriptorType == (DescType)'TEXT') {
short length, i;
length = GetHandleSize(theDesc.dataHandle);
SetHandleSize(theDesc.dataHandle, length + 1);
*(*theDesc.dataHandle + length) = '\0';
for (i=0; i<length; i++) {
if ((*theDesc.dataHandle)[i] == '\r') {
(*theDesc.dataHandle)[i] = '\n';
}
}
HLock(theDesc.dataHandle);
tclErr = Tcl_GlobalEval(interp, *theDesc.dataHandle);
HUnlock(theDesc.dataHandle);
} else if (theDesc.descriptorType == (DescType)'alis') {
Boolean dummy;
FSSpec theFSS;
Handle fullPath;
int length;
theErr = ResolveAlias(NULL, (AliasHandle)theDesc.dataHandle,
&theFSS, &dummy);
if (theErr == noErr) {
FSpPathFromLocation(&theFSS, &length, &fullPath);
HLock(fullPath);
Tcl_EvalFile(interp, *fullPath);
HUnlock(fullPath);
DisposeHandle(fullPath);
} else {
sprintf(errString, "AEDoScriptHandler: file not found");
AEPutParamPtr(reply, keyErrorString, typeChar,
errString, strlen(errString));
}
} else {
sprintf(errString,
"AEDoScriptHandler: invalid script type '%-4.4s', must be 'alis' or 'TEXT'",
&theDesc.descriptorType);
AEPutParamPtr(reply, keyErrorString, typeChar,
errString, strlen(errString));
theErr = -1770;
}
}
/*
* If we actually go to run Tcl code - put the result in the reply.
*/
if (tclErr >= 0) {
if (tclErr == TCL_OK) {
AEPutParamPtr(reply, keyDirectObject, typeChar,
Tcl_GetStringResult(interp),
strlen(Tcl_GetStringResult(interp)));
} else {
AEPutParamPtr(reply, keyErrorString, typeChar,
Tcl_GetStringResult(interp),
strlen(Tcl_GetStringResult(interp)));
AEPutParamPtr(reply, keyErrorNumber, typeInteger,
(Ptr) &tclErr, sizeof(int));
}
}
AEDisposeDesc(&theDesc);
return theErr;
}
/*
*----------------------------------------------------------------------
*
* ReallyKillMe --
*
* This proc tries to kill the shell by running exit, and if that
* has not succeeded (e.g. because someone has renamed the exit
* command), calls Tcl_Exit to really kill the shell. Called from
* an event scheduled by the "Quit" AppleEvent handler.
*
* Results:
* Kills the shell.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
int
ReallyKillMe(Tcl_Event *eventPtr, int flags)
{
Tcl_Interp *interp = ((KillEvent *) eventPtr)->interp;
if (interp != NULL) {
Tcl_GlobalEval(interp, "exit");
}
Tcl_Exit(0);
return 1;
}
/*
*----------------------------------------------------------------------
*
* MissedAnyParameters --
*
* Checks to see if parameters are still left in the event.
*
* Results:
* True or false.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static int
MissedAnyParameters(
AppleEvent *theEvent)
{
DescType returnedType;
Size actualSize;
OSErr err;
err = AEGetAttributePtr(theEvent, keyMissedKeywordAttr, typeWildCard,
&returnedType, NULL, 0, &actualSize);
return (err != errAEDescNotFound);
}
|