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
|
/*
* tkMacOSXNotify.c --
*
* This file contains the implementation of a tcl event source
* for the AppKit event loop.
*
* Copyright (c) 1995-1997 Sun Microsystems, Inc.
* Copyright 2001-2009, Apple Inc.
* Copyright (c) 2005-2009 Daniel A. Steffen <das@users.sourceforge.net>
* Copyright 2015 Marc Culler.
*
* See the file "license.terms" for information on usage and redistribution
* of this file, and for a DISCLAIMER OF ALL WARRANTIES.
*/
#include "tkMacOSXPrivate.h"
#include "tkMacOSXEvent.h"
#include "tkMacOSXConstants.h"
#include <tclInt.h>
#import <objc/objc-auto.h>
/* This is not used for anything at the moment. */
typedef struct ThreadSpecificData {
int initialized;
} ThreadSpecificData;
static Tcl_ThreadDataKey dataKey;
#define TSD_INIT() ThreadSpecificData *tsdPtr = \
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData))
static void TkMacOSXNotifyExitHandler(ClientData clientData);
static void TkMacOSXEventsSetupProc(ClientData clientData, int flags);
static void TkMacOSXEventsCheckProc(ClientData clientData, int flags);
#ifdef TK_MAC_DEBUG_EVENTS
static char* Tk_EventName[39] = {
"",
"",
"KeyPress", /*2*/
"KeyRelease", /*3*/
"ButtonPress", /*4*/
"ButtonRelease", /*5*/
"MotionNotify", /*6*/
"EnterNotify", /*7*/
"LeaveNotify", /*8*/
"FocusIn", /*9*/
"FocusOut", /*10*/
"KeymapNotify", /*11*/
"Expose", /*12*/
"GraphicsExpose", /*13*/
"NoExpose", /*14*/
"VisibilityNotify", /*15*/
"CreateNotify", /*16*/
"DestroyNotify", /*17*/
"UnmapNotify", /*18*/
"MapNotify", /*19*/
"MapRequest", /*20*/
"ReparentNotify", /*21*/
"ConfigureNotify", /*22*/
"ConfigureRequest", /*23*/
"GravityNotify", /*24*/
"ResizeRequest", /*25*/
"CirculateNotify", /*26*/
"CirculateRequest", /*27*/
"PropertyNotify", /*28*/
"SelectionClear", /*29*/
"SelectionRequest", /*30*/
"SelectionNotify", /*31*/
"ColormapNotify", /*32*/
"ClientMessage", /*33*/
"MappingNotify", /*34*/
"VirtualEvent", /*35*/
"ActivateNotify", /*36*/
"DeactivateNotify", /*37*/
"MouseWheelEvent" /*38*/
};
static Tk_RestrictAction
InspectQueueRestrictProc(
ClientData arg,
XEvent *eventPtr)
{
XVirtualEvent* ve = (XVirtualEvent*) eventPtr;
const char *name;
long serial = ve->serial;
long time = eventPtr->xkey.time;
if (eventPtr->type == VirtualEvent) {
name = ve->name;
} else {
name = Tk_EventName[eventPtr->type];
}
fprintf(stderr, " > %s;serial = %lu; time=%lu)\n",
name, serial, time);
return TK_DEFER_EVENT;
}
/*
* Debugging tool which prints the current Tcl queue.
*/
void DebugPrintQueue(void)
{
ClientData oldArg;
Tk_RestrictProc *oldProc;
oldProc = Tk_RestrictEvents(InspectQueueRestrictProc, NULL, &oldArg);
fprintf(stderr, "Current queue:\n");
while (Tcl_DoOneEvent(TCL_ALL_EVENTS|TCL_DONT_WAIT)) {};
Tk_RestrictEvents(oldProc, oldArg, &oldArg);
}
# endif
#pragma mark TKApplication(TKNotify)
@interface NSApplication(TKNotify)
/* We need to declare this hidden method. */
- (void) _modalSession: (NSModalSession) session sendEvent: (NSEvent *) event;
@end
@implementation TKApplication(TKNotify)
/*
* Earlier versions of Tk would override nextEventMatchingMask here, adding a
* call to displayIfNeeded on all windows after calling super. This would cause
* windows to be redisplayed (if necessary) each time that an event was
* received. This was intended to replace Apple's default autoDisplay
* mechanism, which the earlier versions of Tk would disable. When autoDisplay
* is set to the default value of YES, the Apple event loop will call
* displayIfNeeded on all windows at the beginning of each iteration of their
* event loop. Since Tk does not call the Apple event loop, it was thought
* that the autoDisplay behavior needed to be replicated.
*
* However, as of OSX 10.14 (Mojave) the autoDisplay property became
* deprecated. Luckily it turns out that, even though we don't ever start the
* Apple event loop, the Apple window manager still calls displayIfNeeded on
* all windows on a regular basis, perhaps each time the queue is empty. So we
* no longer, and perhaps never did need to set autoDisplay to NO, nor call
* displayIfNeeded on our windows. We can just leave all of that to the window
* manager.
*/
/*
* Call super then check the pasteboard.
*/
- (void) sendEvent: (NSEvent *) theEvent
{
[super sendEvent:theEvent];
[NSApp tkCheckPasteboard];
#ifdef TK_MAC_DEBUG_EVENTS
fprintf(stderr, "Sending event of type %d\n", (int)[theEvent type]);
DebugPrintQueue();
#endif
}
@end
#pragma mark -
/*
*----------------------------------------------------------------------
*
* GetRunLoopMode --
*
* Results:
* RunLoop mode that should be passed to -nextEventMatchingMask:
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
NSString *
GetRunLoopMode(NSModalSession modalSession)
{
NSString *runLoopMode = nil;
if (modalSession) {
runLoopMode = NSModalPanelRunLoopMode;
} else if (TkMacOSXGetCapture()) {
runLoopMode = NSEventTrackingRunLoopMode;
}
if (!runLoopMode) {
runLoopMode = [[NSRunLoop currentRunLoop] currentMode];
}
if (!runLoopMode) {
runLoopMode = NSDefaultRunLoopMode;
}
return runLoopMode;
}
/*
*----------------------------------------------------------------------
*
* Tk_MacOSXSetupTkNotifier --
*
* This procedure is called during Tk initialization to create
* the event source for TkAqua events.
*
* Results:
* None.
*
* Side effects:
* A new event source is created.
*
*----------------------------------------------------------------------
*/
void
Tk_MacOSXSetupTkNotifier(void)
{
TSD_INIT();
if (!tsdPtr->initialized) {
tsdPtr->initialized = 1;
/*
* Install TkAqua event source in main event loop thread.
*/
if (CFRunLoopGetMain() == CFRunLoopGetCurrent()) {
if (![NSThread isMainThread]) {
/*
* Panic if main runloop is not on the main application thread.
*/
Tcl_Panic("Tk_MacOSXSetupTkNotifier: %s",
"first [load] of TkAqua has to occur in the main thread!");
}
Tcl_CreateEventSource(TkMacOSXEventsSetupProc,
TkMacOSXEventsCheckProc,
NULL);
TkCreateExitHandler(TkMacOSXNotifyExitHandler, NULL);
Tcl_SetServiceMode(TCL_SERVICE_ALL);
TclMacOSXNotifierAddRunLoopMode(NSEventTrackingRunLoopMode);
TclMacOSXNotifierAddRunLoopMode(NSModalPanelRunLoopMode);
}
}
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXNotifyExitHandler --
*
* This function is called during finalization to clean up the
* TkMacOSXNotify module.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
TkMacOSXNotifyExitHandler(
ClientData clientData) /* Not used. */
{
TSD_INIT();
Tcl_DeleteEventSource(TkMacOSXEventsSetupProc,
TkMacOSXEventsCheckProc,
NULL);
tsdPtr->initialized = 0;
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXEventsSetupProc --
*
* This procedure implements the setup part of the MacOSX event
* source. It is invoked by Tcl_DoOneEvent before calling
* TkMacOSXEventsProc to process all queued NSEvents. In our
* case, all we need to do is to set the Tcl MaxBlockTime to
* 0 before starting the loop to process all queued NSEvents.
*
* Results:
* None.
*
* Side effects:
*
* If NSEvents are queued, then the maximum block time will be set
* to 0 to ensure that control returns immediately to Tcl.
*
*----------------------------------------------------------------------
*/
static void
TkMacOSXEventsSetupProc(
ClientData clientData,
int flags)
{
NSString *runloopMode = [[NSRunLoop currentRunLoop] currentMode];
/* runloopMode will be nil if we are in a Tcl event loop. */
if (flags & TCL_WINDOW_EVENTS && !runloopMode) {
static const Tcl_Time zeroBlockTime = { 0, 0 };
[NSApp _resetAutoreleasePool];
/* Call this with dequeue=NO -- just checking if the queue is empty. */
NSEvent *currentEvent = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:GetRunLoopMode(TkMacOSXGetModalSession())
dequeue:NO];
if (currentEvent) {
if (currentEvent.type > 0) {
Tcl_SetMaxBlockTime(&zeroBlockTime);
}
}
}
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXEventsCheckProc --
*
* This procedure loops through all NSEvents waiting in the
* TKApplication event queue, generating X events from them.
*
* Results:
* None.
*
* Side effects:
* NSevents are used to generate X events, which are added to the
* Tcl event queue.
*
*----------------------------------------------------------------------
*/
static void
TkMacOSXEventsCheckProc(
ClientData clientData,
int flags)
{
NSString *runloopMode = [[NSRunLoop currentRunLoop] currentMode];
/* runloopMode will be nil if we are in a Tcl event loop. */
if (flags & TCL_WINDOW_EVENTS && !runloopMode) {
NSEvent *currentEvent = nil;
NSEvent *testEvent = nil;
NSModalSession modalSession;
/* It is possible for the SetupProc to be called before this function
* returns. This happens, for example, when we process an event which
* opens a modal window. To prevent premature release of our
* application-wide autorelease pool by a nested call to the SetupProc,
* we must lock it here.
*/
[NSApp _lockAutoreleasePool];
do {
modalSession = TkMacOSXGetModalSession();
testEvent = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:GetRunLoopMode(modalSession)
dequeue:NO];
/* We must not steal any events during LiveResize. */
if (testEvent && [[testEvent window] inLiveResize]) {
break;
}
currentEvent = [NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:GetRunLoopMode(modalSession)
dequeue:YES];
if (currentEvent) {
/* Generate Xevents. */
int oldServiceMode = Tcl_SetServiceMode(TCL_SERVICE_ALL);
NSEvent *processedEvent = [NSApp tkProcessEvent:currentEvent];
Tcl_SetServiceMode(oldServiceMode);
if (processedEvent) { /* Should always be non-NULL. */
#ifdef TK_MAC_DEBUG_EVENTS
TKLog(@" event: %@", currentEvent);
#endif
if (modalSession) {
[NSApp _modalSession:modalSession sendEvent:currentEvent];
} else {
[NSApp sendEvent:currentEvent];
}
}
} else {
break;
}
} while (1);
/* Now we can unlock the pool. */
[NSApp _unlockAutoreleasePool];
}
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|