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