| 1 |
'\" |
| 2 |
'\" Copyright (c) 1998-1999 Scriptics Corporation |
| 3 |
'\" Copyright (c) 1995-1997 Sun Microsystems, Inc. |
| 4 |
'\" |
| 5 |
'\" See the file "license.terms" for information on usage and redistribution |
| 6 |
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 |
'\" |
| 8 |
.TH Notifier 3 8.1 Tcl "Tcl Library Procedures" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
.SH NAME |
| 12 |
Tcl_CreateEventSource, Tcl_DeleteEventSource, Tcl_SetMaxBlockTime, Tcl_QueueEvent, Tcl_ThreadQueueEvent, Tcl_ThreadAlert, Tcl_GetCurrentThread, Tcl_DeleteEvents, Tcl_InitNotifier, Tcl_FinalizeNotifier, Tcl_WaitForEvent, Tcl_AlertNotifier, Tcl_SetTimer, Tcl_ServiceAll, Tcl_ServiceEvent, Tcl_GetServiceMode, Tcl_SetServiceMode, Tcl_ServiceModeHook, Tcl_SetNotifier \- the event queue and notifier interfaces |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
\fB#include <tcl.h>\fR |
| 16 |
.sp |
| 17 |
void |
| 18 |
\fBTcl_CreateEventSource\fR(\fIsetupProc, checkProc, clientData\fR) |
| 19 |
.sp |
| 20 |
void |
| 21 |
\fBTcl_DeleteEventSource\fR(\fIsetupProc, checkProc, clientData\fR) |
| 22 |
.sp |
| 23 |
void |
| 24 |
\fBTcl_SetMaxBlockTime\fR(\fItimePtr\fR) |
| 25 |
.sp |
| 26 |
void |
| 27 |
\fBTcl_QueueEvent\fR(\fIevPtr, position\fR) |
| 28 |
.sp |
| 29 |
void |
| 30 |
\fBTcl_ThreadQueueEvent\fR(\fIthreadId, evPtr, position\fR) |
| 31 |
.sp |
| 32 |
void |
| 33 |
\fBTcl_ThreadAlert\fR(\fIthreadId\fR) |
| 34 |
.sp |
| 35 |
Tcl_ThreadId |
| 36 |
\fBTcl_GetCurrentThread\fR() |
| 37 |
.sp |
| 38 |
void |
| 39 |
\fBTcl_DeleteEvents\fR(\fIdeleteProc, clientData\fR) |
| 40 |
.sp |
| 41 |
ClientData |
| 42 |
\fBTcl_InitNotifier\fR() |
| 43 |
.sp |
| 44 |
void |
| 45 |
\fBTcl_FinalizeNotifier\fR(\fIclientData\fR) |
| 46 |
.sp |
| 47 |
int |
| 48 |
\fBTcl_WaitForEvent\fR(\fItimePtr\fR) |
| 49 |
.sp |
| 50 |
void |
| 51 |
\fBTcl_AlertNotifier\fR(\fIclientData\fR) |
| 52 |
.sp |
| 53 |
void |
| 54 |
\fBTcl_SetTimer\fR(\fItimePtr\fR) |
| 55 |
.sp |
| 56 |
int |
| 57 |
\fBTcl_ServiceAll\fR() |
| 58 |
.sp |
| 59 |
int |
| 60 |
\fBTcl_ServiceEvent\fR(\fIflags\fR) |
| 61 |
.sp |
| 62 |
int |
| 63 |
\fBTcl_GetServiceMode\fR() |
| 64 |
.sp |
| 65 |
int |
| 66 |
\fBTcl_SetServiceMode\fR(\fImode\fR) |
| 67 |
.sp |
| 68 |
void |
| 69 |
\fBTcl_ServiceModeHook\fR(\fImode\fR) |
| 70 |
.sp |
| 71 |
void |
| 72 |
\fBTcl_SetNotifier\fR(\fInotifierProcPtr\fR) |
| 73 |
.SH ARGUMENTS |
| 74 |
.AS Tcl_EventDeleteProc *notifierProcPtr |
| 75 |
.AP Tcl_EventSetupProc *setupProc in |
| 76 |
Procedure to invoke to prepare for event wait in \fBTcl_DoOneEvent\fR. |
| 77 |
.AP Tcl_EventCheckProc *checkProc in |
| 78 |
Procedure for \fBTcl_DoOneEvent\fR to invoke after waiting for |
| 79 |
events. Checks to see if any events have occurred and, if so, |
| 80 |
queues them. |
| 81 |
.AP ClientData clientData in |
| 82 |
Arbitrary one-word value to pass to \fIsetupProc\fR, \fIcheckProc\fR, or |
| 83 |
\fIdeleteProc\fR. |
| 84 |
.AP "const Tcl_Time" *timePtr in |
| 85 |
Indicates the maximum amount of time to wait for an event. This |
| 86 |
is specified as an interval (how long to wait), not an absolute |
| 87 |
time (when to wakeup). If the pointer passed to \fBTcl_WaitForEvent\fR |
| 88 |
is NULL, it means there is no maximum wait time: wait forever if |
| 89 |
necessary. |
| 90 |
.AP Tcl_Event *evPtr in |
| 91 |
An event to add to the event queue. The storage for the event must |
| 92 |
have been allocated by the caller using \fBTcl_Alloc\fR or \fBckalloc\fR. |
| 93 |
.AP Tcl_QueuePosition position in |
| 94 |
Where to add the new event in the queue: \fBTCL_QUEUE_TAIL\fR, |
| 95 |
\fBTCL_QUEUE_HEAD\fR, or \fBTCL_QUEUE_MARK\fR. |
| 96 |
.AP Tcl_ThreadId threadId in |
| 97 |
A unique identifier for a thread. |
| 98 |
.AP Tcl_EventDeleteProc *deleteProc in |
| 99 |
Procedure to invoke for each queued event in \fBTcl_DeleteEvents\fR. |
| 100 |
.AP int flags in |
| 101 |
What types of events to service. These flags are the same as those |
| 102 |
passed to \fBTcl_DoOneEvent\fR. |
| 103 |
.AP int mode in |
| 104 |
Indicates whether events should be serviced by \fBTcl_ServiceAll\fR. |
| 105 |
Must be one of \fBTCL_SERVICE_NONE\fR or \fBTCL_SERVICE_ALL\fR. |
| 106 |
.AP Tcl_NotifierProcs* notifierProcPtr in |
| 107 |
Structure of function pointers describing notifier procedures that are |
| 108 |
to replace the ones installed in the executable. See |
| 109 |
\fBREPLACING THE NOTIFIER\fR for details. |
| 110 |
.BE |
| 111 |
.SH INTRODUCTION |
| 112 |
.PP |
| 113 |
The interfaces described here are used to customize the Tcl event |
| 114 |
loop. The two most common customizations are to add new sources of |
| 115 |
events and to merge Tcl's event loop with some other event loop, such |
| 116 |
as one provided by an application in which Tcl is embedded. Each of |
| 117 |
these tasks is described in a separate section below. |
| 118 |
.PP |
| 119 |
The procedures in this manual entry are the building blocks out of which |
| 120 |
the Tcl event notifier is constructed. The event notifier is the lowest |
| 121 |
layer in the Tcl event mechanism. It consists of three things: |
| 122 |
.IP [1] |
| 123 |
Event sources: these represent the ways in which events can be |
| 124 |
generated. For example, there is a timer event source that implements |
| 125 |
the \fBTcl_CreateTimerHandler\fR procedure and the \fBafter\fR |
| 126 |
command, and there is a file event source that implements the |
| 127 |
\fBTcl_CreateFileHandler\fR procedure on Unix systems. An event |
| 128 |
source must work with the notifier to detect events at the right |
| 129 |
times, record them on the event queue, and eventually notify |
| 130 |
higher-level software that they have occurred. The procedures |
| 131 |
\fBTcl_CreateEventSource\fR, \fBTcl_DeleteEventSource\fR, |
| 132 |
and \fBTcl_SetMaxBlockTime\fR, \fBTcl_QueueEvent\fR, and |
| 133 |
\fBTcl_DeleteEvents\fR are used primarily by event sources. |
| 134 |
.IP [2] |
| 135 |
The event queue: for non-threaded applications, |
| 136 |
there is a single queue for the whole application, |
| 137 |
containing events that have been detected but not yet serviced. Event |
| 138 |
sources place events onto the queue so that they may be processed in |
| 139 |
order at appropriate times during the event loop. The event queue |
| 140 |
guarantees a fair discipline of event handling, so that no event |
| 141 |
source can starve the others. It also allows events to be saved for |
| 142 |
servicing at a future time. Threaded applications work in a |
| 143 |
similar manner, except that there is a separate event queue for |
| 144 |
each thread containing a Tcl interpreter. |
| 145 |
\fBTcl_QueueEvent\fR is used (primarily |
| 146 |
by event sources) to add events to the event queue and |
| 147 |
\fBTcl_DeleteEvents\fR is used to remove events from the queue without |
| 148 |
processing them. In a threaded application, \fBTcl_QueueEvent\fR adds |
| 149 |
an event to the current thread's queue, and \fBTcl_ThreadQueueEvent\fR |
| 150 |
adds an event to a queue in a specific thread. |
| 151 |
.IP [3] |
| 152 |
The event loop: in order to detect and process events, the application |
| 153 |
enters a loop that waits for events to occur, places them on the event |
| 154 |
queue, and then processes them. Most applications will do this by |
| 155 |
calling the procedure \fBTcl_DoOneEvent\fR, which is described in a |
| 156 |
separate manual entry. |
| 157 |
.PP |
| 158 |
Most Tcl applications need not worry about any of the internals of |
| 159 |
the Tcl notifier. However, the notifier now has enough flexibility |
| 160 |
to be retargeted either for a new platform or to use an external event |
| 161 |
loop (such as the Motif event loop, when Tcl is embedded in a Motif |
| 162 |
application). The procedures \fBTcl_WaitForEvent\fR and |
| 163 |
\fBTcl_SetTimer\fR are normally implemented by Tcl, but may be |
| 164 |
replaced with new versions to retarget the notifier (the |
| 165 |
\fBTcl_InitNotifier\fR, \fBTcl_AlertNotifier\fR, |
| 166 |
\fBTcl_FinalizeNotifier\fR, \fBTcl_Sleep\fR, |
| 167 |
\fBTcl_CreateFileHandler\fR, and \fBTcl_DeleteFileHandler\fR must |
| 168 |
also be replaced; see CREATING A NEW NOTIFIER below for details). |
| 169 |
The procedures \fBTcl_ServiceAll\fR, \fBTcl_ServiceEvent\fR, |
| 170 |
\fBTcl_GetServiceMode\fR, and \fBTcl_SetServiceMode\fR are provided |
| 171 |
to help connect Tcl's event loop to an external event loop such as |
| 172 |
Motif's. |
| 173 |
.SH "NOTIFIER BASICS" |
| 174 |
.PP |
| 175 |
The easiest way to understand how the notifier works is to consider |
| 176 |
what happens when \fBTcl_DoOneEvent\fR is called. |
| 177 |
\fBTcl_DoOneEvent\fR is passed a \fIflags\fR argument that indicates |
| 178 |
what sort of events it is OK to process and also whether or not to |
| 179 |
block if no events are ready. \fBTcl_DoOneEvent\fR does the following |
| 180 |
things: |
| 181 |
.IP [1] |
| 182 |
Check the event queue to see if it contains any events that can |
| 183 |
be serviced. If so, service the first possible event, remove it |
| 184 |
from the queue, and return. It does this by calling |
| 185 |
\fBTcl_ServiceEvent\fR and passing in the \fIflags\fR argument. |
| 186 |
.IP [2] |
| 187 |
Prepare to block for an event. To do this, \fBTcl_DoOneEvent\fR |
| 188 |
invokes a \fIsetup procedure\fR in each event source. |
| 189 |
The event source will perform event-source specific initialization and |
| 190 |
possibly call \fBTcl_SetMaxBlockTime\fR to limit how long |
| 191 |
\fBTcl_WaitForEvent\fR will block if no new events occur. |
| 192 |
.IP [3] |
| 193 |
Call \fBTcl_WaitForEvent\fR. This procedure is implemented differently |
| 194 |
on different platforms; it waits for an event to occur, based on the |
| 195 |
information provided by the event sources. |
| 196 |
It may cause the application to block if \fItimePtr\fR specifies |
| 197 |
an interval other than 0. |
| 198 |
\fBTcl_WaitForEvent\fR returns when something has happened, |
| 199 |
such as a file becoming readable or the interval given by \fItimePtr\fR |
| 200 |
expiring. If there are no events for \fBTcl_WaitForEvent\fR to |
| 201 |
wait for, so that it would block forever, then it returns immediately |
| 202 |
and \fBTcl_DoOneEvent\fR returns 0. |
| 203 |
.IP [4] |
| 204 |
Call a \fIcheck procedure\fR in each event source. The check |
| 205 |
procedure determines whether any events of interest to this source |
| 206 |
occurred. If so, the events are added to the event queue. |
| 207 |
.IP [5] |
| 208 |
Check the event queue to see if it contains any events that can |
| 209 |
be serviced. If so, service the first possible event, remove it |
| 210 |
from the queue, and return. |
| 211 |
.IP [6] |
| 212 |
See if there are idle callbacks pending. If so, invoke all of them and |
| 213 |
return. |
| 214 |
.IP [7] |
| 215 |
Either return 0 to indicate that no events were ready, or go back to |
| 216 |
step [2] if blocking was requested by the caller. |
| 217 |
.SH "CREATING A NEW EVENT SOURCE" |
| 218 |
.PP |
| 219 |
An event source consists of three procedures invoked by the notifier, |
| 220 |
plus additional C procedures that are invoked by higher-level code |
| 221 |
to arrange for event-driven callbacks. The three procedures called |
| 222 |
by the notifier consist of the setup and check procedures described |
| 223 |
above, plus an additional procedure that is invoked when an event |
| 224 |
is removed from the event queue for servicing. |
| 225 |
.PP |
| 226 |
The procedure \fBTcl_CreateEventSource\fR creates a new event source. |
| 227 |
Its arguments specify the setup procedure and check procedure for |
| 228 |
the event source. |
| 229 |
\fISetupProc\fR should match the following prototype: |
| 230 |
.PP |
| 231 |
.CS |
| 232 |
typedef void \fBTcl_EventSetupProc\fR( |
| 233 |
ClientData \fIclientData\fR, |
| 234 |
int \fIflags\fR); |
| 235 |
.CE |
| 236 |
.PP |
| 237 |
The \fIclientData\fR argument will be the same as the \fIclientData\fR |
| 238 |
argument to \fBTcl_CreateEventSource\fR; it is typically used to |
| 239 |
point to private information managed by the event source. |
| 240 |
The \fIflags\fR argument will be the same as the \fIflags\fR |
| 241 |
argument passed to \fBTcl_DoOneEvent\fR except that it will never |
| 242 |
be 0 (\fBTcl_DoOneEvent\fR replaces 0 with \fBTCL_ALL_EVENTS\fR). |
| 243 |
\fIFlags\fR indicates what kinds of events should be considered; |
| 244 |
if the bit corresponding to this event source is not set, the event |
| 245 |
source should return immediately without doing anything. For |
| 246 |
example, the file event source checks for the \fBTCL_FILE_EVENTS\fR |
| 247 |
bit. |
| 248 |
.PP |
| 249 |
\fISetupProc\fR's job is to make sure that the application wakes up |
| 250 |
when events of the desired type occur. This is typically done in a |
| 251 |
platform-dependent fashion. For example, under Unix an event source |
| 252 |
might call \fBTcl_CreateFileHandler\fR; under Windows it might |
| 253 |
request notification with a Windows event. For timer-driven event |
| 254 |
sources such as timer events or any polled event, the event source |
| 255 |
can call \fBTcl_SetMaxBlockTime\fR to force the application to wake |
| 256 |
up after a specified time even if no events have occurred. |
| 257 |
If no event source calls \fBTcl_SetMaxBlockTime\fR |
| 258 |
then \fBTcl_WaitForEvent\fR will wait as long as necessary for an |
| 259 |
event to occur; otherwise, it will only wait as long as the shortest |
| 260 |
interval passed to \fBTcl_SetMaxBlockTime\fR by one of the event |
| 261 |
sources. If an event source knows that it already has events ready to |
| 262 |
report, it can request a zero maximum block time. For example, the |
| 263 |
setup procedure for the X event source looks to see if there are |
| 264 |
events already queued. If there are, it calls |
| 265 |
\fBTcl_SetMaxBlockTime\fR with a 0 block time so that |
| 266 |
\fBTcl_WaitForEvent\fR does not block if there is no new data on the X |
| 267 |
connection. |
| 268 |
The \fItimePtr\fR argument to \fBTcl_WaitForEvent\fR points to |
| 269 |
a structure that describes a time interval in seconds and |
| 270 |
microseconds: |
| 271 |
.PP |
| 272 |
.CS |
| 273 |
typedef struct Tcl_Time { |
| 274 |
long \fIsec\fR; |
| 275 |
long \fIusec\fR; |
| 276 |
} \fBTcl_Time\fR; |
| 277 |
.CE |
| 278 |
.PP |
| 279 |
The \fIusec\fR field should be less than 1000000. |
| 280 |
.PP |
| 281 |
Information provided to \fBTcl_SetMaxBlockTime\fR |
| 282 |
is only used for the next call to \fBTcl_WaitForEvent\fR; it is |
| 283 |
discarded after \fBTcl_WaitForEvent\fR returns. |
| 284 |
The next time an event wait is done each of the event sources' |
| 285 |
setup procedures will be called again, and they can specify new |
| 286 |
information for that event wait. |
| 287 |
.PP |
| 288 |
If the application uses an external event loop rather than |
| 289 |
\fBTcl_DoOneEvent\fR, the event sources may need to call |
| 290 |
\fBTcl_SetMaxBlockTime\fR at other times. For example, if a new event |
| 291 |
handler is registered that needs to poll for events, the event source |
| 292 |
may call \fBTcl_SetMaxBlockTime\fR to set the block time to zero to |
| 293 |
force the external event loop to call Tcl. In this case, |
| 294 |
\fBTcl_SetMaxBlockTime\fR invokes \fBTcl_SetTimer\fR with the shortest |
| 295 |
interval seen since the last call to \fBTcl_DoOneEvent\fR or |
| 296 |
\fBTcl_ServiceAll\fR. |
| 297 |
.PP |
| 298 |
In addition to the generic procedure \fBTcl_SetMaxBlockTime\fR, other |
| 299 |
platform-specific procedures may also be available for |
| 300 |
\fIsetupProc\fR, if there is additional information needed by |
| 301 |
\fBTcl_WaitForEvent\fR on that platform. For example, on Unix systems |
| 302 |
the \fBTcl_CreateFileHandler\fR interface can be used to wait for file events. |
| 303 |
.PP |
| 304 |
The second procedure provided by each event source is its check |
| 305 |
procedure, indicated by the \fIcheckProc\fR argument to |
| 306 |
\fBTcl_CreateEventSource\fR. \fICheckProc\fR must match the |
| 307 |
following prototype: |
| 308 |
.PP |
| 309 |
.CS |
| 310 |
typedef void \fBTcl_EventCheckProc\fR( |
| 311 |
ClientData \fIclientData\fR, |
| 312 |
int \fIflags\fR); |
| 313 |
.CE |
| 314 |
.PP |
| 315 |
The arguments to this procedure are the same as those for \fIsetupProc\fR. |
| 316 |
\fBCheckProc\fR is invoked by \fBTcl_DoOneEvent\fR after it has waited |
| 317 |
for events. Presumably at least one event source is now prepared to |
| 318 |
queue an event. \fBTcl_DoOneEvent\fR calls each of the event sources |
| 319 |
in turn, so they all have a chance to queue any events that are ready. |
| 320 |
The check procedure does two things. First, it must see if any events |
| 321 |
have triggered. Different event sources do this in different ways. |
| 322 |
.PP |
| 323 |
If an event source's check procedure detects an interesting event, it |
| 324 |
must add the event to Tcl's event queue. To do this, the event source |
| 325 |
calls \fBTcl_QueueEvent\fR. The \fIevPtr\fR argument is a pointer to |
| 326 |
a dynamically allocated structure containing the event (see below for |
| 327 |
more information on memory management issues). Each event source can |
| 328 |
define its own event structure with whatever information is relevant |
| 329 |
to that event source. However, the first element of the structure |
| 330 |
must be a structure of type \fBTcl_Event\fR, and the address of this |
| 331 |
structure is used when communicating between the event source and the |
| 332 |
rest of the notifier. A \fBTcl_Event\fR has the following definition: |
| 333 |
.PP |
| 334 |
.CS |
| 335 |
typedef struct { |
| 336 |
Tcl_EventProc *\fIproc\fR; |
| 337 |
struct Tcl_Event *\fInextPtr\fR; |
| 338 |
} \fBTcl_Event\fR; |
| 339 |
.CE |
| 340 |
.PP |
| 341 |
The event source must fill in the \fIproc\fR field of |
| 342 |
the event before calling \fBTcl_QueueEvent\fR. |
| 343 |
The \fInextPtr\fR is used to link together the events in the queue |
| 344 |
and should not be modified by the event source. |
| 345 |
.PP |
| 346 |
An event may be added to the queue at any of three positions, depending |
| 347 |
on the \fIposition\fR argument to \fBTcl_QueueEvent\fR: |
| 348 |
.IP \fBTCL_QUEUE_TAIL\fR 24 |
| 349 |
Add the event at the back of the queue, so that all other pending |
| 350 |
events will be serviced first. This is almost always the right |
| 351 |
place for new events. |
| 352 |
.IP \fBTCL_QUEUE_HEAD\fR 24 |
| 353 |
Add the event at the front of the queue, so that it will be serviced |
| 354 |
before all other queued events. |
| 355 |
.IP \fBTCL_QUEUE_MARK\fR 24 |
| 356 |
Add the event at the front of the queue, unless there are other |
| 357 |
events at the front whose position is \fBTCL_QUEUE_MARK\fR; if so, |
| 358 |
add the new event just after all other \fBTCL_QUEUE_MARK\fR events. |
| 359 |
This value of \fIposition\fR is used to insert an ordered sequence of |
| 360 |
events at the front of the queue, such as a series of |
| 361 |
Enter and Leave events synthesized during a grab or ungrab operation |
| 362 |
in Tk. |
| 363 |
.PP |
| 364 |
When it is time to handle an event from the queue (steps 1 and 4 |
| 365 |
above) \fBTcl_ServiceEvent\fR will invoke the \fIproc\fR specified |
| 366 |
in the first queued \fBTcl_Event\fR structure. |
| 367 |
\fIProc\fR must match the following prototype: |
| 368 |
.PP |
| 369 |
.CS |
| 370 |
typedef int \fBTcl_EventProc\fR( |
| 371 |
Tcl_Event *\fIevPtr\fR, |
| 372 |
int \fIflags\fR); |
| 373 |
.CE |
| 374 |
.PP |
| 375 |
The first argument to \fIproc\fR is a pointer to the event, which will |
| 376 |
be the same as the first argument to the \fBTcl_QueueEvent\fR call that |
| 377 |
added the event to the queue. |
| 378 |
The second argument to \fIproc\fR is the \fIflags\fR argument for the |
| 379 |
current call to \fBTcl_ServiceEvent\fR; this is used by the event source |
| 380 |
to return immediately if its events are not relevant. |
| 381 |
.PP |
| 382 |
It is up to \fIproc\fR to handle the event, typically by invoking |
| 383 |
one or more Tcl commands or C-level callbacks. |
| 384 |
Once the event source has finished handling the event it returns 1 |
| 385 |
to indicate that the event can be removed from the queue. |
| 386 |
If for some reason the event source decides that the event cannot |
| 387 |
be handled at this time, it may return 0 to indicate that the event |
| 388 |
should be deferred for processing later; in this case \fBTcl_ServiceEvent\fR |
| 389 |
will go on to the next event in the queue and attempt to service it. |
| 390 |
There are several reasons why an event source might defer an event. |
| 391 |
One possibility is that events of this type are excluded by the |
| 392 |
\fIflags\fR argument. |
| 393 |
For example, the file event source will always return 0 if the |
| 394 |
\fBTCL_FILE_EVENTS\fR bit is not set in \fIflags\fR. |
| 395 |
Another example of deferring events happens in Tk if |
| 396 |
\fBTk_RestrictEvents\fR has been invoked to defer certain kinds |
| 397 |
of window events. |
| 398 |
.PP |
| 399 |
When \fIproc\fR returns 1, \fBTcl_ServiceEvent\fR will remove the |
| 400 |
event from the event queue and free its storage. |
| 401 |
Note that the storage for an event must be allocated by |
| 402 |
the event source (using \fBTcl_Alloc\fR or the Tcl macro \fBckalloc\fR) |
| 403 |
before calling \fBTcl_QueueEvent\fR, but it |
| 404 |
will be freed by \fBTcl_ServiceEvent\fR, not by the event source. |
| 405 |
.PP |
| 406 |
Threaded applications work in a |
| 407 |
similar manner, except that there is a separate event queue for |
| 408 |
each thread containing a Tcl interpreter. |
| 409 |
Calling \fBTcl_QueueEvent\fR in a multithreaded application adds |
| 410 |
an event to the current thread's queue. |
| 411 |
To add an event to another thread's queue, use \fBTcl_ThreadQueueEvent\fR. |
| 412 |
\fBTcl_ThreadQueueEvent\fR accepts as an argument a Tcl_ThreadId argument, |
| 413 |
which uniquely identifies a thread in a Tcl application. To obtain the |
| 414 |
Tcl_ThreadId for the current thread, use the \fBTcl_GetCurrentThread\fR |
| 415 |
procedure. (A thread would then need to pass this identifier to other |
| 416 |
threads for those threads to be able to add events to its queue.) |
| 417 |
After adding an event to another thread's queue, you then typically |
| 418 |
need to call \fBTcl_ThreadAlert\fR to |
| 419 |
.QW "wake up" |
| 420 |
that thread's notifier to alert it to the new event. |
| 421 |
.PP |
| 422 |
\fBTcl_DeleteEvents\fR can be used to explicitly remove one or more |
| 423 |
events from the event queue. \fBTcl_DeleteEvents\fR calls \fIproc\fR |
| 424 |
for each event in the queue, deleting those for with the procedure |
| 425 |
returns 1. Events for which the procedure returns 0 are left in the |
| 426 |
queue. \fIProc\fR should match the following prototype: |
| 427 |
.PP |
| 428 |
.CS |
| 429 |
typedef int \fBTcl_EventDeleteProc\fR( |
| 430 |
Tcl_Event *\fIevPtr\fR, |
| 431 |
ClientData \fIclientData\fR); |
| 432 |
.CE |
| 433 |
.PP |
| 434 |
The \fIclientData\fR argument will be the same as the \fIclientData\fR |
| 435 |
argument to \fBTcl_DeleteEvents\fR; it is typically used to point to |
| 436 |
private information managed by the event source. The \fIevPtr\fR will |
| 437 |
point to the next event in the queue. |
| 438 |
.PP |
| 439 |
\fBTcl_DeleteEventSource\fR deletes an event source. The \fIsetupProc\fR, |
| 440 |
\fIcheckProc\fR, and \fIclientData\fR arguments must exactly match those |
| 441 |
provided to the \fBTcl_CreateEventSource\fR for the event source to be deleted. |
| 442 |
If no such source exists, \fBTcl_DeleteEventSource\fR has no effect. |
| 443 |
.SH "CREATING A NEW NOTIFIER" |
| 444 |
.PP |
| 445 |
The notifier consists of all the procedures described in this manual |
| 446 |
entry, plus \fBTcl_DoOneEvent\fR and \fBTcl_Sleep\fR, which are |
| 447 |
available on all platforms, and \fBTcl_CreateFileHandler\fR and |
| 448 |
\fBTcl_DeleteFileHandler\fR, which are Unix-specific. Most of these |
| 449 |
procedures are generic, in that they are the same for all notifiers. |
| 450 |
However, none of the procedures are notifier-dependent: |
| 451 |
\fBTcl_InitNotifier\fR, \fBTcl_AlertNotifier\fR, |
| 452 |
\fBTcl_FinalizeNotifier\fR, \fBTcl_SetTimer\fR, \fBTcl_Sleep\fR, |
| 453 |
\fBTcl_WaitForEvent\fR, \fBTcl_CreateFileHandler\fR, |
| 454 |
\fBTcl_DeleteFileHandler\fR and \fBTcl_ServiceModeHook\fR. To support a |
| 455 |
new platform or to integrate Tcl with an application-specific event loop, |
| 456 |
you must write new versions of these procedures. |
| 457 |
.PP |
| 458 |
\fBTcl_InitNotifier\fR initializes the notifier state and returns |
| 459 |
a handle to the notifier state. Tcl calls this |
| 460 |
procedure when initializing a Tcl interpreter. Similarly, |
| 461 |
\fBTcl_FinalizeNotifier\fR shuts down the notifier, and is |
| 462 |
called by \fBTcl_Finalize\fR when shutting down a Tcl interpreter. |
| 463 |
.PP |
| 464 |
\fBTcl_WaitForEvent\fR is the lowest-level procedure in the notifier; |
| 465 |
it is responsible for waiting for an |
| 466 |
.QW interesting |
| 467 |
event to occur or |
| 468 |
for a given time to elapse. Before \fBTcl_WaitForEvent\fR is invoked, |
| 469 |
each of the event sources' setup procedure will have been invoked. |
| 470 |
The \fItimePtr\fR argument to |
| 471 |
\fBTcl_WaitForEvent\fR gives the maximum time to block for an event, |
| 472 |
based on calls to \fBTcl_SetMaxBlockTime\fR made by setup procedures |
| 473 |
and on other information (such as the \fBTCL_DONT_WAIT\fR bit in |
| 474 |
\fIflags\fR). |
| 475 |
.PP |
| 476 |
Ideally, \fBTcl_WaitForEvent\fR should only wait for an event |
| 477 |
to occur; it should not actually process the event in any way. |
| 478 |
Later on, the |
| 479 |
event sources will process the raw events and create Tcl_Events on |
| 480 |
the event queue in their \fIcheckProc\fR procedures. |
| 481 |
However, on some platforms (such as Windows) this is not possible; |
| 482 |
events may be processed in \fBTcl_WaitForEvent\fR, including queuing |
| 483 |
Tcl_Events and more (for example, callbacks for native widgets may be |
| 484 |
invoked). The return value from \fBTcl_WaitForEvent\fR must be either |
| 485 |
0, 1, or \-1. On platforms such as Windows where events get processed in |
| 486 |
\fBTcl_WaitForEvent\fR, a return value of 1 means that there may be more |
| 487 |
events still pending that have not been processed. This is a sign to the |
| 488 |
caller that it must call \fBTcl_WaitForEvent\fR again if it wants all |
| 489 |
pending events to be processed. A 0 return value means that calling |
| 490 |
\fBTcl_WaitForEvent\fR again will not have any effect: either this is a |
| 491 |
platform where \fBTcl_WaitForEvent\fR only waits without doing any event |
| 492 |
processing, or \fBTcl_WaitForEvent\fR knows for sure that there are no |
| 493 |
additional events to process (e.g. it returned because the time |
| 494 |
elapsed). Finally, a return value of \-1 means that the event loop is |
| 495 |
no longer operational and the application should probably unwind and |
| 496 |
terminate. Under Windows this happens when a WM_QUIT message is received; |
| 497 |
under Unix it happens when \fBTcl_WaitForEvent\fR would have waited |
| 498 |
forever because there were no active event sources and the timeout was |
| 499 |
infinite. |
| 500 |
.PP |
| 501 |
\fBTcl_AlertNotifier\fR is used in multithreaded applications to allow |
| 502 |
any thread to |
| 503 |
.QW "wake up" |
| 504 |
the notifier to alert it to new events on its |
| 505 |
queue. \fBTcl_AlertNotifier\fR requires as an argument the notifier |
| 506 |
handle returned by \fBTcl_InitNotifier\fR. |
| 507 |
.PP |
| 508 |
If the notifier will be used with an external event loop, then it must |
| 509 |
also support the \fBTcl_SetTimer\fR interface. \fBTcl_SetTimer\fR is |
| 510 |
invoked by \fBTcl_SetMaxBlockTime\fR whenever the maximum blocking |
| 511 |
time has been reduced. \fBTcl_SetTimer\fR should arrange for the |
| 512 |
external event loop to invoke \fBTcl_ServiceAll\fR after the specified |
| 513 |
interval even if no events have occurred. This interface is needed |
| 514 |
because \fBTcl_WaitForEvent\fR is not invoked when there is an external |
| 515 |
event loop. If the |
| 516 |
notifier will only be used from \fBTcl_DoOneEvent\fR, then |
| 517 |
\fBTcl_SetTimer\fR need not do anything. |
| 518 |
.PP |
| 519 |
\fBTcl_ServiceModeHook\fR is called by the platform-independent portion |
| 520 |
of the notifier when client code makes a call to |
| 521 |
\fBTcl_SetServiceMode\fR. This hook is provided to support operating |
| 522 |
systems that require special event handling when the application is in |
| 523 |
a modal loop (the Windows notifier, for instance, uses this hook to |
| 524 |
create a communication window). |
| 525 |
.PP |
| 526 |
On Unix systems, the file event source also needs support from the |
| 527 |
notifier. The file event source consists of the |
| 528 |
\fBTcl_CreateFileHandler\fR and \fBTcl_DeleteFileHandler\fR |
| 529 |
procedures, which are described in the \fBTcl_CreateFileHandler\fR |
| 530 |
manual page. |
| 531 |
.PP |
| 532 |
The \fBTcl_Sleep\fR and \fBTcl_DoOneEvent\fR interfaces are described |
| 533 |
in their respective manual pages. |
| 534 |
.PP |
| 535 |
The easiest way to create a new notifier is to look at the code |
| 536 |
for an existing notifier, such as the files \fBunix/tclUnixNotfy.c\fR |
| 537 |
or \fBwin/tclWinNotify.c\fR in the Tcl source distribution. |
| 538 |
.SH "REPLACING THE NOTIFIER" |
| 539 |
.PP |
| 540 |
A notifier that has been written according to the conventions above |
| 541 |
can also be installed in a running process in place of the standard |
| 542 |
notifier. This mechanism is used so that a single executable can be |
| 543 |
used (with the standard notifier) as a stand-alone program and reused |
| 544 |
(with a replacement notifier in a loadable extension) as an extension |
| 545 |
to another program, such as a Web browser plugin. |
| 546 |
.PP |
| 547 |
To do this, the extension makes a call to \fBTcl_SetNotifier\fR |
| 548 |
passing a pointer to a \fBTcl_NotifierProcs\fR data structure. The |
| 549 |
structure has the following layout: |
| 550 |
.PP |
| 551 |
.CS |
| 552 |
typedef struct Tcl_NotifierProcs { |
| 553 |
Tcl_SetTimerProc *\fIsetTimerProc\fR; |
| 554 |
Tcl_WaitForEventProc *\fIwaitForEventProc\fR; |
| 555 |
Tcl_CreateFileHandlerProc *\fIcreateFileHandlerProc\fR; |
| 556 |
Tcl_DeleteFileHandlerProc *\fIdeleteFileHandlerProc\fR; |
| 557 |
Tcl_InitNotifierProc *\fIinitNotifierProc\fR; |
| 558 |
Tcl_FinalizeNotifierProc *\fIfinalizeNotifierProc\fR; |
| 559 |
Tcl_AlertNotifierProc *\fIalertNotifierProc\fR; |
| 560 |
Tcl_ServiceModeHookProc *\fIserviceModeHookProc\fR; |
| 561 |
} \fBTcl_NotifierProcs\fR; |
| 562 |
.CE |
| 563 |
.PP |
| 564 |
Following the call to \fBTcl_SetNotifier\fR, the pointers given in |
| 565 |
the \fBTcl_NotifierProcs\fR structure replace whatever notifier had |
| 566 |
been installed in the process. |
| 567 |
.PP |
| 568 |
It is extraordinarily unwise to replace a running notifier. Normally, |
| 569 |
\fBTcl_SetNotifier\fR should be called at process initialization time |
| 570 |
before the first call to \fBTcl_InitNotifier\fR. |
| 571 |
.SH "EXTERNAL EVENT LOOPS" |
| 572 |
.PP |
| 573 |
The notifier interfaces are designed so that Tcl can be embedded into |
| 574 |
applications that have their own private event loops. In this case, |
| 575 |
the application does not call \fBTcl_DoOneEvent\fR except in the case |
| 576 |
of recursive event loops such as calls to the Tcl commands \fBupdate\fR |
| 577 |
or \fBvwait\fR. Most of the time is spent in the external event loop |
| 578 |
of the application. In this case the notifier must arrange for the |
| 579 |
external event loop to call back into Tcl when something |
| 580 |
happens on the various Tcl event sources. These callbacks should |
| 581 |
arrange for appropriate Tcl events to be placed on the Tcl event queue. |
| 582 |
.PP |
| 583 |
Because the external event loop is not calling \fBTcl_DoOneEvent\fR on |
| 584 |
a regular basis, it is up to the notifier to arrange for |
| 585 |
\fBTcl_ServiceEvent\fR to be called whenever events are pending on the |
| 586 |
Tcl event queue. The easiest way to do this is to invoke |
| 587 |
\fBTcl_ServiceAll\fR at the end of each callback from the external |
| 588 |
event loop. This will ensure that all of the event sources are |
| 589 |
polled, any queued events are serviced, and any pending idle handlers |
| 590 |
are processed before returning control to the application. In |
| 591 |
addition, event sources that need to poll for events can call |
| 592 |
\fBTcl_SetMaxBlockTime\fR to force the external event loop to call |
| 593 |
Tcl even if no events are available on the system event queue. |
| 594 |
.PP |
| 595 |
As a side effect of processing events detected in the main external |
| 596 |
event loop, Tcl may invoke \fBTcl_DoOneEvent\fR to start a recursive event |
| 597 |
loop in commands like \fBvwait\fR. \fBTcl_DoOneEvent\fR will invoke |
| 598 |
the external event loop, which will result in callbacks as described |
| 599 |
in the preceding paragraph, which will result in calls to |
| 600 |
\fBTcl_ServiceAll\fR. However, in these cases it is undesirable to |
| 601 |
service events in \fBTcl_ServiceAll\fR. Servicing events there is |
| 602 |
unnecessary because control will immediately return to the |
| 603 |
external event loop and hence to \fBTcl_DoOneEvent\fR, which can |
| 604 |
service the events itself. Furthermore, \fBTcl_DoOneEvent\fR is |
| 605 |
supposed to service only a single event, whereas \fBTcl_ServiceAll\fR |
| 606 |
normally services all pending events. To handle this situation, |
| 607 |
\fBTcl_DoOneEvent\fR sets a flag for \fBTcl_ServiceAll\fR |
| 608 |
that causes it to return without servicing any events. |
| 609 |
This flag is called the \fIservice mode\fR; |
| 610 |
\fBTcl_DoOneEvent\fR restores it to its previous value before it returns. |
| 611 |
.PP |
| 612 |
In some cases, however, it may be necessary for \fBTcl_ServiceAll\fR |
| 613 |
to service events |
| 614 |
even when it has been invoked from \fBTcl_DoOneEvent\fR. This happens |
| 615 |
when there is yet another recursive event loop invoked via an |
| 616 |
event handler called by \fBTcl_DoOneEvent\fR (such as one that is |
| 617 |
part of a native widget). In this case, \fBTcl_DoOneEvent\fR may not |
| 618 |
have a chance to service events so \fBTcl_ServiceAll\fR must service |
| 619 |
them all. Any recursive event loop that calls an external event |
| 620 |
loop rather than \fBTcl_DoOneEvent\fR must reset the service mode so |
| 621 |
that all events get processed in \fBTcl_ServiceAll\fR. This is done |
| 622 |
by invoking the \fBTcl_SetServiceMode\fR procedure. If |
| 623 |
\fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_NONE\fR, then calls |
| 624 |
to \fBTcl_ServiceAll\fR will return immediately without processing any |
| 625 |
events. If \fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_ALL\fR, |
| 626 |
then calls to \fBTcl_ServiceAll\fR will behave normally. |
| 627 |
\fBTcl_SetServiceMode\fR returns the previous value of the service |
| 628 |
mode, which should be restored when the recursive loop exits. |
| 629 |
\fBTcl_GetServiceMode\fR returns the current value of the service |
| 630 |
mode. |
| 631 |
.SH "SEE ALSO" |
| 632 |
Tcl_CreateFileHandler(3), Tcl_DeleteFileHandler(3), Tcl_Sleep(3), |
| 633 |
Tcl_DoOneEvent(3), Thread(3) |
| 634 |
.SH KEYWORDS |
| 635 |
event, notifier, event queue, event sources, file events, timer, idle, service mode, threads |