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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
|
/*
* tkMacOSXNotify.c --
*
* This file contains the implementation of a tcl event source
* for the AppKit event loop.
*
* Copyright © 1995-1997 Sun Microsystems, Inc.
* Copyright © 2001-2009, Apple Inc.
* Copyright © 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 "tkMacOSXInt.h"
#include "tkMacOSXConstants.h"
#if TCL_MAJOR_VERSION < 9
#undef Tcl_MacOSXNotifierAddRunLoopMode
#ifdef USE_TCL_STUBS
#ifdef __cplusplus
extern "C" {
#endif
/* Little hack to eliminate the need for "tclInt.h" here:
Just copy a small portion of TclIntPlatStubs, just
enough to make it work. See [600b72bfbc] */
typedef struct TclIntPlatStubs {
int magic;
void *hooks;
void (*dummy[19]) (void); /* dummy entries 0-18, not used */
void (*tclMacOSXNotifierAddRunLoopMode) (const void *runLoopMode); /* 19 */
} TclIntPlatStubs;
extern const TclIntPlatStubs *tclIntPlatStubsPtr;
#ifdef __cplusplus
}
#endif
#define Tcl_MacOSXNotifierAddRunLoopMode \
(tclIntPlatStubsPtr->tclMacOSXNotifierAddRunLoopMode) /* 19 */
#elif TCL_MINOR_VERSION < 7
extern void TclMacOSXNotifierAddRunLoopMode(const void *runLoopMode);
# define Tcl_MacOSXNotifierAddRunLoopMode TclMacOSXNotifierAddRunLoopMode
#else
extern void Tcl_MacOSXNotifierAddRunLoopMode(const void *runLoopMode);
#endif
#endif
#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 = (ThreadSpecificData *) \
Tcl_GetThreadData(&dataKey, sizeof(ThreadSpecificData))
static void TkMacOSXNotifyExitHandler(void *clientData);
static void TkMacOSXEventsSetupProc(void *clientData, int flags);
static void TkMacOSXEventsCheckProc(void *clientData, int flags);
#ifdef TK_MAC_DEBUG_EVENTS
static const 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(
void *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)
{
void *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)
@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.
*/
/*
* Since the contentView is the first responder for a Tk Window, it is
* responsible for sending events up the responder chain. We also check the
* pasteboard here.
*/
- (void) sendEvent: (NSEvent *) theEvent
{
/*
* Workaround for an Apple bug. When an accented character is selected
* from an NSTextInputClient popup character viewer with the mouse, Apple
* sends an event of type NSAppKitDefined and subtype 21. If that event is
* sent up the responder chain it causes Apple to print a warning to the
* console log and, extremely obnoxiously, also to stderr, which says
* "Window move completed without beginning." Apparently they are sending
* the "move completed" event without having sent the "move began" event of
* subtype 20, and then announcing their error on our stderr. Also, of
* course, no movement is occurring. The popup is not movable and is just
* being closed. The bug has been reported to Apple. If they ever fix it,
* this block should be removed.
*/
#if MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
if ([theEvent type] == NSAppKitDefined) {
static Bool aWindowIsMoving = NO;
switch([theEvent subtype]) {
case 20:
aWindowIsMoving = YES;
break;
case 21:
if (aWindowIsMoving) {
aWindowIsMoving = NO;
break;
} else {
// printf("Bug!!!!\n");
return;
}
default:
break;
}
}
#endif
[super sendEvent:theEvent];
[NSApp tkCheckPasteboard];
#ifdef TK_MAC_DEBUG_EVENTS
fprintf(stderr, "Sending event of type %d\n", (int)[theEvent type]);
DebugPrintQueue();
#endif
}
- (void) _runBackgroundLoop
{
while(Tcl_DoOneEvent(TCL_IDLE_EVENTS|TCL_TIMER_EVENTS|TCL_DONT_WAIT)){
TkMacOSXDrawAllViews(NULL);
}
}
@end
#pragma mark -
/*
*----------------------------------------------------------------------
*
* GetRunLoopMode --
*
* Results:
* RunLoop mode that should be passed to -nextEventMatchingMask:
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static NSString *
GetRunLoopMode(NSModalSession modalSession)
{
NSString *runLoopMode = nil;
if (modalSession) {
runLoopMode = NSModalPanelRunLoopMode;
}
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_MacOSXNotifierAddRunLoopMode(NSEventTrackingRunLoopMode);
Tcl_MacOSXNotifierAddRunLoopMode(NSModalPanelRunLoopMode);
}
}
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXNotifyExitHandler --
*
* This function is called during finalization to clean up the
* TkMacOSXNotify module.
*
* Results:
* None.
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
static void
TkMacOSXNotifyExitHandler(
TCL_UNUSED(void *)) /* Not used. */
{
TSD_INIT();
Tcl_DeleteEventSource(TkMacOSXEventsSetupProc,
TkMacOSXEventsCheckProc, NULL);
tsdPtr->initialized = 0;
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXDrawAllViews --
*
* This static function is meant to be run as an idle task. It attempts
* to redraw all views which have the tkNeedsDisplay property set to YES.
* This relies on a feature of [NSApp nextEventMatchingMask: ...] which
* is undocumented, namely that it sometimes blocks and calls drawRect
* for all views that need display before it returns. We call it with
* deQueue=NO so that it will not change anything on the AppKit event
* queue, because we only want the side effect that it runs drawRect. The
* only times when any NSViews have the needsDisplay property set to YES
* are during execution of this function or in the addTkDirtyRect method
* of TKContentView.
*
* The reason for running this function as an idle task is to try to
* arrange that all widgets will be fully configured before they are
* drawn. Any idle tasks that might reconfigure them should be higher on
* the idle queue, so they should be run before the display procs are run
* by drawRect.
*
* If this function is called directly with non-NULL clientData parameter
* then the int which it references will be set to the number of windows
* that need display, but the needsDisplay property of those windows will
* not be changed.
*
* Results:
* None.
*
* Side effects:
* Parts of windows may get redrawn.
*
*----------------------------------------------------------------------
*/
void
TkMacOSXDrawAllViews(
void *clientData)
{
int count = 0, *dirtyCount = (int *)clientData;
for (NSWindow *window in [NSApp windows]) {
if ([[window contentView] isMemberOfClass:[TKContentView class]]) {
TKContentView *view = [window contentView];
if ([view tkNeedsDisplay]) {
count++;
if (dirtyCount) {
continue;
}
[[view layer] setNeedsDisplayInRect:[view tkDirtyRect]];
[view setNeedsDisplay:YES];
}
} else {
[window displayIfNeeded];
}
}
if (dirtyCount) {
*dirtyCount = count;
}
[NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:GetRunLoopMode(TkMacOSXGetModalSession())
dequeue:NO];
for (NSWindow *window in [NSApp windows]) {
if ([[window contentView] isMemberOfClass:[TKContentView class]]) {
TKContentView *view = [window contentView];
/*
* If we did not run drawRect, we set needsDisplay back to NO.
* Note that if drawRect did run it may have added to Tk's dirty
* rect, due to attempts to draw outside of drawRect's dirty rect.
*/
if ([view needsDisplay]) {
[view setNeedsDisplay: NO];
}
}
}
[NSApp setNeedsToDraw:NO];
}
/*
*----------------------------------------------------------------------
*
* TkMacOSXEventsSetupProc --
*
* This procedure implements the setup part of the MacOSX event source. It
* is invoked by Tcl_DoOneEvent before calling TkMacOSXEventsCheckProc 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, or if there is any drawing that needs to be
* done, then the maximum block time will be set to 0 to ensure that
* Tcl_WaitForEvent returns immediately.
*
*----------------------------------------------------------------------
*/
#define TICK 200
static Tcl_TimerToken ticker = NULL;
static void
Heartbeat(
TCL_UNUSED(void *))
{
if (ticker) {
ticker = Tcl_CreateTimerHandler(TICK, Heartbeat, NULL);
}
}
static const Tcl_Time zeroBlockTime = { 0, 0 };
static void
TkMacOSXEventsSetupProc(
TCL_UNUSED(void *),
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) {
[NSApp _resetAutoreleasePool];
/*
* After calling this setup proc, Tcl_DoOneEvent will call
* Tcl_WaitForEvent. Then it will call check proc to collect the
* events and translate them into XEvents.
*
* If we have any events waiting or if there is any drawing to be done
* we want Tcl_WaitForEvent to return immediately. So we set the block
* time to 0 and stop the heartbeat.
*/
NSEvent *currentEvent =
[NSApp nextEventMatchingMask:NSAnyEventMask
untilDate:[NSDate distantPast]
inMode:GetRunLoopMode(TkMacOSXGetModalSession())
dequeue:NO];
if ((currentEvent) || [NSApp needsToDraw] ) {
Tcl_SetMaxBlockTime(&zeroBlockTime);
Tcl_DeleteTimerHandler(ticker);
ticker = NULL;
} else if (ticker == NULL) {
/*
* When the user is not generating events we schedule a "heartbeat"
* TimerHandler to fire every 200 milliseconds. The handler does
* nothing, but when its timer fires it causes Tcl_WaitForEvent to
* return. This helps avoid hangs when calling vwait during the
* non-regression tests.
*/
ticker = Tcl_CreateTimerHandler(TICK, Heartbeat, NULL);
}
}
}
/*
*----------------------------------------------------------------------
*
* 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(
TCL_UNUSED(void *),
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.
*/
NSEvent *processedEvent = [NSApp tkProcessEvent:currentEvent];
if (processedEvent) {
#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];
/*
* Add an idle task to the end of the idle queue which will redisplay
* all of our dirty windows. We want this to happen after all other
* idle tasks have run so that all widgets will be configured before
* they are displayed. The drawRect method "borrows" the idle queue
* while drawing views. That is, it sends expose events which cause
* display procs to be posted as idle tasks and then runs an inner
* event loop to processes those idle tasks. We are trying to arrange
* for the idle queue to be empty when it starts that process and empty
* when it finishes.
*/
int dirtyCount = 0;
TkMacOSXDrawAllViews(&dirtyCount);
if (dirtyCount > 0) {
Tcl_CancelIdleCall(TkMacOSXDrawAllViews, NULL);
Tcl_DoWhenIdle(TkMacOSXDrawAllViews, NULL);
}
}
}
/*
* Local Variables:
* mode: objc
* c-basic-offset: 4
* fill-column: 79
* coding: utf-8
* End:
*/
|