4 * This file contains Macintosh-specific procedures for the notifier,
5 * which is the lowest-level part of the Tcl event loop. This file
6 * works together with ../generic/tclNotify.c.
8 * Copyright (c) 1995-1996 Sun Microsystems, Inc.
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
13 * SCCS: @(#) tclMacNotify.c 1.36 97/05/07 19:09:29
19 #include "tclMacInt.h"
23 #include <Processes.h>
28 * This is necessary to work around a bug in Apple's Universal header files
29 * for the CFM68K libraries.
34 extern pascal QHdrPtr
GetEventQueue(void)
35 THREEWORDINLINE(0x2EBC, 0x0000, 0x014A);
36 #pragma import list GetEventQueue
37 #define GetEvQHdr() GetEventQueue()
41 * The follwing static indicates whether this module has been initialized.
44 static int initialized
= 0;
47 * The following structure contains the state information for the
52 int timerActive
; /* 1 if timer is running. */
53 Tcl_Time timer
; /* Time when next timer event is expected. */
54 int flags
; /* OR'ed set of flags defined below. */
55 Point lastMousePosition
; /* Last known mouse location. */
56 RgnHandle utilityRgn
; /* Region used as the mouse region for
57 * WaitNextEvent and the update region when
58 * checking for events. */
59 Tcl_MacConvertEventPtr eventProcPtr
;
60 /* This pointer holds the address of the
61 * function that will handle all incoming
62 * Macintosh events. */
66 * The following defines are used in the flags field of the notifier struct.
69 #define NOTIFY_IDLE (1<<1) /* Tcl_ServiceIdle should be called. */
70 #define NOTIFY_TIMER (1<<2) /* Tcl_ServiceTimer should be called. */
73 * Prototypes for procedures that are referenced only in this file:
76 static int HandleMacEvents
_ANSI_ARGS_((void));
77 static void InitNotifier
_ANSI_ARGS_((void));
78 static void NotifierExitHandler
_ANSI_ARGS_((
79 ClientData clientData
));
82 *----------------------------------------------------------------------
86 * Initializes the notifier structure.
92 * Creates a new exit handler.
94 *----------------------------------------------------------------------
101 memset(¬ifier
, 0, sizeof(notifier
));
102 Tcl_CreateExitHandler(NotifierExitHandler
, NULL
);
106 *----------------------------------------------------------------------
108 * NotifierExitHandler --
110 * This function is called to cleanup the notifier state before
119 *----------------------------------------------------------------------
124 ClientData clientData
) /* Not used. */
130 *----------------------------------------------------------------------
134 * This function checks for events from the Macintosh event queue.
137 * Returns 1 if event found, 0 otherwise.
140 * Pulls events off of the Mac event queue and then calls
143 *----------------------------------------------------------------------
147 HandleMacEvents(void)
149 EventRecord theEvent
;
150 int eventFound
= 0, needsUpdate
= 0;
156 * Check for mouse moved events. These events aren't placed on the
157 * system event queue unless we call WaitNextEvent.
160 GetGlobalMouse(¤tMouse
);
161 if ((notifier
.eventProcPtr
!= NULL
) &&
162 !EqualPt(currentMouse
, notifier
.lastMousePosition
)) {
163 notifier
.lastMousePosition
= currentMouse
;
164 theEvent
.what
= nullEvent
;
165 if ((*notifier
.eventProcPtr
)(&theEvent
) == true) {
171 * Check for update events. Since update events aren't generated
172 * until we call GetNextEvent, we may need to force a call to
173 * GetNextEvent, even if the queue is empty.
176 for (windowRef
= FrontWindow(); windowRef
!= NULL
;
177 windowRef
= GetNextWindow(windowRef
)) {
178 GetWindowUpdateRgn(windowRef
, notifier
.utilityRgn
);
179 if (!EmptyRgn(notifier
.utilityRgn
)) {
186 * Process events from the OS event queue.
189 while (needsUpdate
|| (GetEvQHdr()->qHead
!= NULL
)) {
190 GetGlobalMouse(¤tMouse
);
191 SetRect(&mouseRect
, currentMouse
.h
, currentMouse
.v
,
192 currentMouse
.h
+ 1, currentMouse
.v
+ 1);
193 RectRgn(notifier
.utilityRgn
, &mouseRect
);
195 WaitNextEvent(everyEvent
, &theEvent
, 5, notifier
.utilityRgn
);
197 if ((notifier
.eventProcPtr
!= NULL
)
198 && ((*notifier
.eventProcPtr
)(&theEvent
) == true)) {
207 *----------------------------------------------------------------------
211 * This procedure sets the current notifier timer value. The
212 * notifier will ensure that Tcl_ServiceAll() is called after
213 * the specified interval, even if no events have occurred.
219 * Replaces any previous timer.
221 *----------------------------------------------------------------------
226 Tcl_Time
*timePtr
) /* New value for interval timer. */
229 notifier
.timerActive
= 0;
232 * Compute when the timer should fire.
235 TclpGetTime(¬ifier
.timer
);
236 notifier
.timer
.sec
+= timePtr
->sec
;
237 notifier
.timer
.usec
+= timePtr
->usec
;
238 if (notifier
.timer
.usec
>= 1000000) {
239 notifier
.timer
.usec
-= 1000000;
240 notifier
.timer
.sec
+= 1;
242 notifier
.timerActive
= 1;
247 *----------------------------------------------------------------------
249 * Tcl_WaitForEvent --
251 * This function is called by Tcl_DoOneEvent to wait for new
252 * events on the message queue. If the block time is 0, then
253 * Tcl_WaitForEvent just polls the event queue without blocking.
261 *----------------------------------------------------------------------
266 Tcl_Time
*timePtr
) /* Maximum block time. */
269 EventRecord macEvent
;
277 * Compute the next timeout value.
283 ms
= (timePtr
->sec
* 1000) + (timePtr
->usec
/ 1000);
285 timerToken
= TclMacStartTimer((long) ms
);
288 * Poll the Mac event sources. This loop repeats until something
289 * happens: a timeout, a socket event, mouse motion, or some other
290 * window event. Note that we don't call WaitNextEvent if another
291 * event is found to avoid context switches. This effectively gives
292 * events coming in via WaitNextEvent a slightly lower priority.
296 if (notifier
.utilityRgn
== NULL
) {
297 notifier
.utilityRgn
= NewRgn();
302 * Check for generated and queued events.
305 if (HandleMacEvents()) {
310 * Check for time out.
313 if (!found
&& TclMacTimerExpired(timerToken
)) {
318 * Mod by Jack: poll for select() events. Code is in TclSelectNotify.c
321 int Tcl_PollSelectEvent(void);
322 if (!found
&& Tcl_PollSelectEvent())
327 * Check for window events. We may receive a NULL event for
328 * various reasons. 1) the timer has expired, 2) a mouse moved
329 * event is occuring or 3) the os is giving us time for idle
330 * events. Note that we aren't sharing the processor very
331 * well here. We really ought to do a better job of calling
332 * WaitNextEvent for time slicing purposes.
337 * Set up mouse region so we will wake if the mouse is moved.
338 * We do this by defining the smallest possible region around
339 * the current mouse position.
342 GetGlobalMouse(¤tMouse
);
343 SetRect(&mouseRect
, currentMouse
.h
, currentMouse
.v
,
344 currentMouse
.h
+ 1, currentMouse
.v
+ 1);
345 RectRgn(notifier
.utilityRgn
, &mouseRect
);
347 WaitNextEvent(everyEvent
, &macEvent
, sleepTime
,
348 notifier
.utilityRgn
);
350 if (notifier
.eventProcPtr
!= NULL
) {
351 if ((*notifier
.eventProcPtr
)(&macEvent
) == true) {
357 TclMacRemoveTimer(timerToken
);
362 *----------------------------------------------------------------------
366 * Delay execution for the specified number of milliseconds. This
367 * is not a very good call to make. It will block the system -
368 * you will not even be able to switch applications.
376 *----------------------------------------------------------------------
381 int ms
) /* Number of milliseconds to sleep. */
390 timerToken
= TclMacStartTimer((long) ms
);
392 WaitNextEvent(0, &dummy
, (ms
/ 16.66) + 1, NULL
);
394 if (TclMacTimerExpired(timerToken
)) {
398 TclMacRemoveTimer(timerToken
);
402 *----------------------------------------------------------------------
404 * Tcl_MacSetEventProc --
406 * This function sets the event handling procedure for the
407 * application. This function will be passed all incoming Mac
408 * events. This function usually controls the console or some
409 * other entity like Tk.
415 * Changes the event handling function.
417 *----------------------------------------------------------------------
422 Tcl_MacConvertEventPtr procPtr
)
424 notifier
.eventProcPtr
= procPtr
;