Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / amd64 / man / man3 / Tcl_InitNotifier.3
CommitLineData
920dae64
AT
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'\" RCS: @(#) $Id: Notifier.3,v 1.9.2.1 2004/11/25 15:28:37 vasiljevic Exp $
9'\"
10'\" The definitions below are for supplemental macros used in Tcl/Tk
11'\" manual entries.
12'\"
13'\" .AP type name in/out ?indent?
14'\" Start paragraph describing an argument to a library procedure.
15'\" type is type of argument (int, etc.), in/out is either "in", "out",
16'\" or "in/out" to describe whether procedure reads or modifies arg,
17'\" and indent is equivalent to second arg of .IP (shouldn't ever be
18'\" needed; use .AS below instead)
19'\"
20'\" .AS ?type? ?name?
21'\" Give maximum sizes of arguments for setting tab stops. Type and
22'\" name are examples of largest possible arguments that will be passed
23'\" to .AP later. If args are omitted, default tab stops are used.
24'\"
25'\" .BS
26'\" Start box enclosure. From here until next .BE, everything will be
27'\" enclosed in one large box.
28'\"
29'\" .BE
30'\" End of box enclosure.
31'\"
32'\" .CS
33'\" Begin code excerpt.
34'\"
35'\" .CE
36'\" End code excerpt.
37'\"
38'\" .VS ?version? ?br?
39'\" Begin vertical sidebar, for use in marking newly-changed parts
40'\" of man pages. The first argument is ignored and used for recording
41'\" the version when the .VS was added, so that the sidebars can be
42'\" found and removed when they reach a certain age. If another argument
43'\" is present, then a line break is forced before starting the sidebar.
44'\"
45'\" .VE
46'\" End of vertical sidebar.
47'\"
48'\" .DS
49'\" Begin an indented unfilled display.
50'\"
51'\" .DE
52'\" End of indented unfilled display.
53'\"
54'\" .SO
55'\" Start of list of standard options for a Tk widget. The
56'\" options follow on successive lines, in four columns separated
57'\" by tabs.
58'\"
59'\" .SE
60'\" End of list of standard options for a Tk widget.
61'\"
62'\" .OP cmdName dbName dbClass
63'\" Start of description of a specific option. cmdName gives the
64'\" option's name as specified in the class command, dbName gives
65'\" the option's name in the option database, and dbClass gives
66'\" the option's class in the option database.
67'\"
68'\" .UL arg1 arg2
69'\" Print arg1 underlined, then print arg2 normally.
70'\"
71'\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $
72'\"
73'\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages.
74.if t .wh -1.3i ^B
75.nr ^l \n(.l
76.ad b
77'\" # Start an argument description
78.de AP
79.ie !"\\$4"" .TP \\$4
80.el \{\
81. ie !"\\$2"" .TP \\n()Cu
82. el .TP 15
83.\}
84.ta \\n()Au \\n()Bu
85.ie !"\\$3"" \{\
86\&\\$1 \\fI\\$2\\fP (\\$3)
87.\".b
88.\}
89.el \{\
90.br
91.ie !"\\$2"" \{\
92\&\\$1 \\fI\\$2\\fP
93.\}
94.el \{\
95\&\\fI\\$1\\fP
96.\}
97.\}
98..
99'\" # define tabbing values for .AP
100.de AS
101.nr )A 10n
102.if !"\\$1"" .nr )A \\w'\\$1'u+3n
103.nr )B \\n()Au+15n
104.\"
105.if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n
106.nr )C \\n()Bu+\\w'(in/out)'u+2n
107..
108.AS Tcl_Interp Tcl_CreateInterp in/out
109'\" # BS - start boxed text
110'\" # ^y = starting y location
111'\" # ^b = 1
112.de BS
113.br
114.mk ^y
115.nr ^b 1u
116.if n .nf
117.if n .ti 0
118.if n \l'\\n(.lu\(ul'
119.if n .fi
120..
121'\" # BE - end boxed text (draw box now)
122.de BE
123.nf
124.ti 0
125.mk ^t
126.ie n \l'\\n(^lu\(ul'
127.el \{\
128.\" Draw four-sided box normally, but don't draw top of
129.\" box if the box started on an earlier page.
130.ie !\\n(^b-1 \{\
131\h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
132.\}
133.el \}\
134\h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul'
135.\}
136.\}
137.fi
138.br
139.nr ^b 0
140..
141'\" # VS - start vertical sidebar
142'\" # ^Y = starting y location
143'\" # ^v = 1 (for troff; for nroff this doesn't matter)
144.de VS
145.if !"\\$2"" .br
146.mk ^Y
147.ie n 'mc \s12\(br\s0
148.el .nr ^v 1u
149..
150'\" # VE - end of vertical sidebar
151.de VE
152.ie n 'mc
153.el \{\
154.ev 2
155.nf
156.ti 0
157.mk ^t
158\h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n'
159.sp -1
160.fi
161.ev
162.\}
163.nr ^v 0
164..
165'\" # Special macro to handle page bottom: finish off current
166'\" # box/sidebar if in box/sidebar mode, then invoked standard
167'\" # page bottom macro.
168.de ^B
169.ev 2
170'ti 0
171'nf
172.mk ^t
173.if \\n(^b \{\
174.\" Draw three-sided box if this is the box's first page,
175.\" draw two sides but no top otherwise.
176.ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
177.el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c
178.\}
179.if \\n(^v \{\
180.nr ^x \\n(^tu+1v-\\n(^Yu
181\kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c
182.\}
183.bp
184'fi
185.ev
186.if \\n(^b \{\
187.mk ^y
188.nr ^b 2
189.\}
190.if \\n(^v \{\
191.mk ^Y
192.\}
193..
194'\" # DS - begin display
195.de DS
196.RS
197.nf
198.sp
199..
200'\" # DE - end display
201.de DE
202.fi
203.RE
204.sp
205..
206'\" # SO - start of list of standard options
207.de SO
208.SH "STANDARD OPTIONS"
209.LP
210.nf
211.ta 5.5c 11c
212.ft B
213..
214'\" # SE - end of list of standard options
215.de SE
216.fi
217.ft R
218.LP
219See the \\fBoptions\\fR manual entry for details on the standard options.
220..
221'\" # OP - start of full description for a single option
222.de OP
223.LP
224.nf
225.ta 4c
226Command-Line Name: \\fB\\$1\\fR
227Database Name: \\fB\\$2\\fR
228Database Class: \\fB\\$3\\fR
229.fi
230.IP
231..
232'\" # CS - begin code excerpt
233.de CS
234.RS
235.nf
236.ta .25i .5i .75i 1i
237..
238'\" # CE - end code excerpt
239.de CE
240.fi
241.RE
242..
243.de UL
244\\$1\l'|0\(ul'\\$2
245..
246.TH Notifier 3 8.1 Tcl "Tcl Library Procedures"
247.BS
248.SH NAME
249Tcl_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 \- the event queue and notifier interfaces
250.SH SYNOPSIS
251.nf
252\fB#include <tcl.h>\fR
253.sp
254void
255\fBTcl_CreateEventSource\fR(\fIsetupProc, checkProc, clientData\fR)
256.sp
257void
258\fBTcl_DeleteEventSource\fR(\fIsetupProc, checkProc, clientData\fR)
259.sp
260void
261\fBTcl_SetMaxBlockTime\fR(\fItimePtr\fR)
262.sp
263void
264\fBTcl_QueueEvent\fR(\fIevPtr, position\fR)
265.VS 8.1
266.sp
267void
268\fBTcl_ThreadQueueEvent\fR(\fIthreadId, evPtr, position\fR)
269.sp
270void
271\fBTcl_ThreadAlert\fR(\fIthreadId\fR)
272.sp
273Tcl_ThreadId
274\fBTcl_GetCurrentThread\fR()
275.sp
276void
277\fBTcl_DeleteEvents\fR(\fIdeleteProc, clientData\fR)
278.sp
279ClientData
280\fBTcl_InitNotifier\fR()
281.sp
282void
283\fBTcl_FinalizeNotifier\fR(\fIclientData\fR)
284.sp
285int
286\fBTcl_WaitForEvent\fR(\fItimePtr\fR)
287.sp
288void
289\fBTcl_AlertNotifier\fR(\fIclientData\fR)
290.sp
291void
292\fBTcl_SetTimer\fR(\fItimePtr\fR)
293.sp
294int
295\fBTcl_ServiceAll\fR()
296.sp
297int
298\fBTcl_ServiceEvent\fR(\fIflags\fR)
299.sp
300int
301\fBTcl_GetServiceMode\fR()
302.sp
303int
304\fBTcl_SetServiceMode\fR(\fImode\fR)
305.VE
306
307.SH ARGUMENTS
308.AS Tcl_EventDeleteProc milliseconds
309.AP Tcl_EventSetupProc *setupProc in
310Procedure to invoke to prepare for event wait in \fBTcl_DoOneEvent\fR.
311.AP Tcl_EventCheckProc *checkProc in
312Procedure for \fBTcl_DoOneEvent\fR to invoke after waiting for
313events. Checks to see if any events have occurred and, if so,
314queues them.
315.AP ClientData clientData in
316Arbitrary one-word value to pass to \fIsetupProc\fR, \fIcheckProc\fR, or
317\fIdeleteProc\fR.
318.AP Tcl_Time *timePtr in
319Indicates the maximum amount of time to wait for an event. This
320is specified as an interval (how long to wait), not an absolute
321time (when to wakeup). If the pointer passed to \fBTcl_WaitForEvent\fR
322is NULL, it means there is no maximum wait time: wait forever if
323necessary.
324.AP Tcl_Event *evPtr in
325An event to add to the event queue. The storage for the event must
326have been allocated by the caller using \fBTcl_Alloc\fR or \fBckalloc\fR.
327.AP Tcl_QueuePosition position in
328Where to add the new event in the queue: \fBTCL_QUEUE_TAIL\fR,
329\fBTCL_QUEUE_HEAD\fR, or \fBTCL_QUEUE_MARK\fR.
330.AP Tcl_ThreadId threadId in
331A unique identifier for a thread.
332.AP Tcl_EventDeleteProc *deleteProc in
333Procedure to invoke for each queued event in \fBTcl_DeleteEvents\fR.
334.AP int flags in
335What types of events to service. These flags are the same as those
336passed to \fBTcl_DoOneEvent\fR.
337.VS 8.1
338.AP int mode in
339Indicates whether events should be serviced by \fBTcl_ServiceAll\fR.
340Must be one of \fBTCL_SERVICE_NONE\fR or \fBTCL_SERVICE_ALL\fR.
341.VE
342.BE
343
344.SH INTRODUCTION
345.PP
346The interfaces described here are used to customize the Tcl event
347loop. The two most common customizations are to add new sources of
348events and to merge Tcl's event loop with some other event loop, such
349as one provided by an application in which Tcl is embedded. Each of
350these tasks is described in a separate section below.
351.PP
352The procedures in this manual entry are the building blocks out of which
353the Tcl event notifier is constructed. The event notifier is the lowest
354layer in the Tcl event mechanism. It consists of three things:
355.IP [1]
356Event sources: these represent the ways in which events can be
357generated. For example, there is a timer event source that implements
358the \fBTcl_CreateTimerHandler\fR procedure and the \fBafter\fR
359command, and there is a file event source that implements the
360\fBTcl_CreateFileHandler\fR procedure on Unix systems. An event
361source must work with the notifier to detect events at the right
362times, record them on the event queue, and eventually notify
363higher-level software that they have occurred. The procedures
364\fBTcl_CreateEventSource\fR, \fBTcl_DeleteEventSource\fR,
365and \fBTcl_SetMaxBlockTime\fR, \fBTcl_QueueEvent\fR, and
366\fBTcl_DeleteEvents\fR are used primarily by event sources.
367.IP [2]
368The event queue: for non-threaded applications,
369there is a single queue for the whole application,
370containing events that have been detected but not yet serviced. Event
371sources place events onto the queue so that they may be processed in
372order at appropriate times during the event loop. The event queue
373guarantees a fair discipline of event handling, so that no event
374source can starve the others. It also allows events to be saved for
375servicing at a future time.
376.VS 8.1
377Threaded applications work in a
378similar manner, except that there is a separate event queue for
379each thread containing a Tcl interpreter.
380\fBTcl_QueueEvent\fR is used (primarily
381by event sources) to add events to the event queue and
382\fBTcl_DeleteEvents\fR is used to remove events from the queue without
383processing them. In a threaded application, \fBTcl_QueueEvent\fR adds
384an event to the current thread's queue, and \fBTcl_ThreadQueueEvent\fR
385adds an event to a queue in a specific thread.
386.IP [3]
387The event loop: in order to detect and process events, the application
388enters a loop that waits for events to occur, places them on the event
389queue, and then processes them. Most applications will do this by
390calling the procedure \fBTcl_DoOneEvent\fR, which is described in a
391separate manual entry.
392.PP
393Most Tcl applications need not worry about any of the internals of
394the Tcl notifier. However, the notifier now has enough flexibility
395to be retargeted either for a new platform or to use an external event
396loop (such as the Motif event loop, when Tcl is embedded in a Motif
397application). The procedures \fBTcl_WaitForEvent\fR and
398\fBTcl_SetTimer\fR are normally implemented by Tcl, but may be
399replaced with new versions to retarget the notifier (the
400\fBTcl_InitNotifier\fR, \fBTcl_AlertNotifier\fR,
401\fBTcl_FinalizeNotifier\fR, \fBTcl_Sleep\fR,
402\fBTcl_CreateFileHandler\fR, and \fBTcl_DeleteFileHandler\fR must
403also be replaced; see CREATING A NEW NOTIFIER below for details).
404The procedures \fBTcl_ServiceAll\fR, \fBTcl_ServiceEvent\fR,
405\fBTcl_GetServiceMode\fR, and \fBTcl_SetServiceMode\fR are provided
406to help connect Tcl's event loop to an external event loop such as
407Motif's.
408.SH "NOTIFIER BASICS"
409.VE
410.PP
411The easiest way to understand how the notifier works is to consider
412what happens when \fBTcl_DoOneEvent\fR is called.
413\fBTcl_DoOneEvent\fR is passed a \fIflags\fR argument that indicates
414what sort of events it is OK to process and also whether or not to
415block if no events are ready. \fBTcl_DoOneEvent\fR does the following
416things:
417.IP [1]
418Check the event queue to see if it contains any events that can
419be serviced. If so, service the first possible event, remove it
420.VS 8.1
421from the queue, and return. It does this by calling
422\fBTcl_ServiceEvent\fR and passing in the \fIflags\fR argument.
423.VE
424.IP [2]
425Prepare to block for an event. To do this, \fBTcl_DoOneEvent\fR
426invokes a \fIsetup procedure\fR in each event source.
427The event source will perform event-source specific initialization and
428.VS 8.1
429possibly call \fBTcl_SetMaxBlockTime\fR to limit how long
430.VE
431\fBTcl_WaitForEvent\fR will block if no new events occur.
432.IP [3]
433Call \fBTcl_WaitForEvent\fR. This procedure is implemented differently
434on different platforms; it waits for an event to occur, based on the
435information provided by the event sources.
436It may cause the application to block if \fItimePtr\fR specifies
437an interval other than 0.
438\fBTcl_WaitForEvent\fR returns when something has happened,
439such as a file becoming readable or the interval given by \fItimePtr\fR
440expiring. If there are no events for \fBTcl_WaitForEvent\fR to
441wait for, so that it would block forever, then it returns immediately
442and \fBTcl_DoOneEvent\fR returns 0.
443.IP [4]
444Call a \fIcheck procedure\fR in each event source. The check
445procedure determines whether any events of interest to this source
446occurred. If so, the events are added to the event queue.
447.IP [5]
448Check the event queue to see if it contains any events that can
449be serviced. If so, service the first possible event, remove it
450from the queue, and return.
451.IP [6]
452See if there are idle callbacks pending. If so, invoke all of them and
453return.
454.IP [7]
455Either return 0 to indicate that no events were ready, or go back to
456step [2] if blocking was requested by the caller.
457
458.SH "CREATING A NEW EVENT SOURCE"
459.PP
460An event source consists of three procedures invoked by the notifier,
461plus additional C procedures that are invoked by higher-level code
462to arrange for event-driven callbacks. The three procedures called
463by the notifier consist of the setup and check procedures described
464above, plus an additional procedure that is invoked when an event
465is removed from the event queue for servicing.
466.PP
467The procedure \fBTcl_CreateEventSource\fR creates a new event source.
468Its arguments specify the setup procedure and check procedure for
469the event source.
470\fISetupProc\fR should match the following prototype:
471.CS
472typedef void Tcl_EventSetupProc(
473 ClientData \fIclientData\fR,
474 int \fIflags\fR);
475.CE
476The \fIclientData\fR argument will be the same as the \fIclientData\fR
477argument to \fBTcl_CreateEventSource\fR; it is typically used to
478point to private information managed by the event source.
479The \fIflags\fR argument will be the same as the \fIflags\fR
480argument passed to \fBTcl_DoOneEvent\fR except that it will never
481be 0 (\fBTcl_DoOneEvent\fR replaces 0 with \fBTCL_ALL_EVENTS\fR).
482\fIFlags\fR indicates what kinds of events should be considered;
483if the bit corresponding to this event source isn't set, the event
484source should return immediately without doing anything. For
485example, the file event source checks for the \fBTCL_FILE_EVENTS\fR
486bit.
487.PP
488\fISetupProc\fR's job is to make sure that the application wakes up
489when events of the desired type occur. This is typically done in a
490platform-dependent fashion. For example, under Unix an event source
491might call \fBTcl_CreateFileHandler\fR; under Windows it might
492request notification with a Windows event. For timer-driven event
493sources such as timer events or any polled event, the event source
494can call \fBTcl_SetMaxBlockTime\fR to force the application to wake
495up after a specified time even if no events have occurred.
496.VS 8.1
497If no event source calls \fBTcl_SetMaxBlockTime\fR
498then \fBTcl_WaitForEvent\fR will wait as long as necessary for an
499event to occur; otherwise, it will only wait as long as the shortest
500interval passed to \fBTcl_SetMaxBlockTime\fR by one of the event
501sources. If an event source knows that it already has events ready to
502report, it can request a zero maximum block time. For example, the
503setup procedure for the X event source looks to see if there are
504events already queued. If there are, it calls
505\fBTcl_SetMaxBlockTime\fR with a 0 block time so that
506\fBTcl_WaitForEvent\fR does not block if there is no new data on the X
507connection.
508.VE
509The \fItimePtr\fR argument to \fBTcl_WaitForEvent\fR points to
510a structure that describes a time interval in seconds and
511microseconds:
512.CS
513typedef struct Tcl_Time {
514 long \fIsec\fR;
515 long \fIusec\fR;
516} Tcl_Time;
517.CE
518The \fIusec\fR field should be less than 1000000.
519.PP
520.VS 8.1
521Information provided to \fBTcl_SetMaxBlockTime\fR
522is only used for the next call to \fBTcl_WaitForEvent\fR; it is
523discarded after \fBTcl_WaitForEvent\fR returns.
524.VE
525The next time an event wait is done each of the event sources'
526setup procedures will be called again, and they can specify new
527information for that event wait.
528.PP
529.VS 8.1
530If the application uses an external event loop rather than
531\fBTcl_DoOneEvent\fR, the event sources may need to call
532\fBTcl_SetMaxBlockTime\fR at other times. For example, if a new event
533handler is registered that needs to poll for events, the event source
534may call \fBTcl_SetMaxBlockTime\fR to set the block time to zero to
535force the external event loop to call Tcl. In this case,
536\fBTcl_SetMaxBlockTime\fR invokes \fBTcl_SetTimer\fR with the shortest
537interval seen since the last call to \fBTcl_DoOneEvent\fR or
538\fBTcl_ServiceAll\fR.
539.PP
540In addition to the generic procedure \fBTcl_SetMaxBlockTime\fR, other
541platform-specific procedures may also be available for
542\fIsetupProc\fR, if there is additional information needed by
543\fBTcl_WaitForEvent\fR on that platform. For example, on Unix systems
544the \fBTcl_CreateFileHandler\fR interface can be used to wait for file events.
545.VE
546.PP
547The second procedure provided by each event source is its check
548procedure, indicated by the \fIcheckProc\fR argument to
549\fBTcl_CreateEventSource\fR. \fICheckProc\fR must match the
550following prototype:
551.CS
552typedef void Tcl_EventCheckProc(
553 ClientData \fIclientData\fR,
554 int \fIflags\fR);
555.CE
556The arguments to this procedure are the same as those for \fIsetupProc\fR.
557\fBCheckProc\fR is invoked by \fBTcl_DoOneEvent\fR after it has waited
558for events. Presumably at least one event source is now prepared to
559queue an event. \fBTcl_DoOneEvent\fR calls each of the event sources
560in turn, so they all have a chance to queue any events that are ready.
561The check procedure does two things. First, it must see if any events
562have triggered. Different event sources do this in different ways.
563.PP
564If an event source's check procedure detects an interesting event, it
565must add the event to Tcl's event queue. To do this, the event source
566calls \fBTcl_QueueEvent\fR. The \fIevPtr\fR argument is a pointer to
567a dynamically allocated structure containing the event (see below for
568more information on memory management issues). Each event source can
569define its own event structure with whatever information is relevant
570to that event source. However, the first element of the structure
571must be a structure of type \fBTcl_Event\fR, and the address of this
572structure is used when communicating between the event source and the
573rest of the notifier. A \fBTcl_Event\fR has the following definition:
574.CS
575typedef struct {
576 Tcl_EventProc *\fIproc\fR;
577 struct Tcl_Event *\fInextPtr\fR;
578} Tcl_Event;
579.CE
580The event source must fill in the \fIproc\fR field of
581the event before calling \fBTcl_QueueEvent\fR.
582The \fInextPtr\fR is used to link together the events in the queue
583and should not be modified by the event source.
584.PP
585An event may be added to the queue at any of three positions, depending
586on the \fIposition\fR argument to \fBTcl_QueueEvent\fR:
587.IP \fBTCL_QUEUE_TAIL\fR 24
588Add the event at the back of the queue, so that all other pending
589events will be serviced first. This is almost always the right
590place for new events.
591.IP \fBTCL_QUEUE_HEAD\fR 24
592Add the event at the front of the queue, so that it will be serviced
593before all other queued events.
594.IP \fBTCL_QUEUE_MARK\fR 24
595Add the event at the front of the queue, unless there are other
596events at the front whose position is \fBTCL_QUEUE_MARK\fR; if so,
597add the new event just after all other \fBTCL_QUEUE_MARK\fR events.
598This value of \fIposition\fR is used to insert an ordered sequence of
599events at the front of the queue, such as a series of
600Enter and Leave events synthesized during a grab or ungrab operation
601in Tk.
602.PP
603.VS 8.1
604When it is time to handle an event from the queue (steps 1 and 4
605above) \fBTcl_ServiceEvent\fR will invoke the \fIproc\fR specified
606.VE
607in the first queued \fBTcl_Event\fR structure.
608\fIProc\fR must match the following prototype:
609.CS
610typedef int Tcl_EventProc(
611 Tcl_Event *\fIevPtr\fR,
612 int \fIflags\fR);
613.CE
614The first argument to \fIproc\fR is a pointer to the event, which will
615be the same as the first argument to the \fBTcl_QueueEvent\fR call that
616added the event to the queue.
617The second argument to \fIproc\fR is the \fIflags\fR argument for the
618.VS 8.1
619current call to \fBTcl_ServiceEvent\fR; this is used by the event source
620.VE
621to return immediately if its events are not relevant.
622.PP
623It is up to \fIproc\fR to handle the event, typically by invoking
624one or more Tcl commands or C-level callbacks.
625Once the event source has finished handling the event it returns 1
626to indicate that the event can be removed from the queue.
627If for some reason the event source decides that the event cannot
628be handled at this time, it may return 0 to indicate that the event
629.VS 8.1
630should be deferred for processing later; in this case \fBTcl_ServiceEvent\fR
631.VE
632will go on to the next event in the queue and attempt to service it.
633There are several reasons why an event source might defer an event.
634One possibility is that events of this type are excluded by the
635\fIflags\fR argument.
636For example, the file event source will always return 0 if the
637\fBTCL_FILE_EVENTS\fR bit isn't set in \fIflags\fR.
638Another example of deferring events happens in Tk if
639\fBTk_RestrictEvents\fR has been invoked to defer certain kinds
640of window events.
641.PP
642.VS 8.1
643When \fIproc\fR returns 1, \fBTcl_ServiceEvent\fR will remove the
644event from the event queue and free its storage.
645Note that the storage for an event must be allocated by
646the event source (using \fBTcl_Alloc\fR or the Tcl macro \fBckalloc\fR)
647before calling \fBTcl_QueueEvent\fR, but it
648will be freed by \fBTcl_ServiceEvent\fR, not by the event source.
649.PP
650Threaded applications work in a
651similar manner, except that there is a separate event queue for
652each thread containing a Tcl interpreter.
653Calling \fBTcl_QueueEvent\fR in a multithreaded application adds
654an event to the current thread's queue.
655To add an event to another thread's queue, use \fBTcl_ThreadQueueEvent\fR.
656\fBTcl_ThreadQueueEvent\fR accepts as an argument a Tcl_ThreadId argument,
657which uniquely identifies a thread in a Tcl application. To obtain the
658Tcl_ThreadID for the current thread, use the \fBTcl_GetCurrentThread\fR
659procedure. (A thread would then need to pass this identifier to other
660threads for those threads to be able to add events to its queue.)
661After adding an event to another thread's queue, you then typically
662need to call \fBTcl_ThreadAlert\fR to "wake up" that thread's notifier to
663alert it to the new event.
664.PP
665\fBTcl_DeleteEvents\fR can be used to explicitly remove one or more
666events from the event queue. \fBTcl_DeleteEvents\fR calls \fIproc\fR
667for each event in the queue, deleting those for with the procedure
668returns 1. Events for which the procedure returns 0 are left in the
669queue. \fIProc\fR should match the following prototype:
670.CS
671typedef int Tcl_EventDeleteProc(
672 Tcl_Event *\fIevPtr\fR,
673 ClientData \fIclientData\fR);
674.CE
675The \fIclientData\fR argument will be the same as the \fIclientData\fR
676argument to \fBTcl_DeleteEvents\fR; it is typically used to point to
677private information managed by the event source. The \fIevPtr\fR will
678point to the next event in the queue.
679.PP
680\fBTcl_DeleteEventSource\fR deletes an event source. The \fIsetupProc\fR,
681\fIcheckProc\fR, and \fIclientData\fR arguments must exactly match those
682provided to the \fBTcl_CreateEventSource\fR for the event source to be deleted.
683If no such source exists, \fBTcl_DeleteEventSource\fR has no effect.
684.VE
685
686.SH "CREATING A NEW NOTIFIER"
687.PP
688The notifier consists of all the procedures described in this manual
689entry, plus \fBTcl_DoOneEvent\fR and \fBTcl_Sleep\fR, which are
690.VS 8.1
691available on all platforms, and \fBTcl_CreateFileHandler\fR and
692\fBTcl_DeleteFileHandler\fR, which are Unix-specific. Most of these
693procedures are generic, in that they are the same for all notifiers.
694However, eight of the procedures are notifier-dependent:
695\fBTcl_InitNotifier\fR, \fBTcl_AlertNotifier\fR, \fBTcl_FinalizeNotifier\fR,
696\fBTcl_SetTimer\fR, \fBTcl_Sleep\fR, \fBTcl_WaitForEvent\fR,
697\fBTcl_CreateFileHandler\fR and \fBTcl_DeleteFileHandler\fR. To
698support a new platform or to integrate Tcl with an
699application-specific event loop, you must write new versions of these
700procedures.
701.PP
702\fBTcl_InitNotifier\fR initializes the notifier state and returns
703a handle to the notifier state. Tcl calls this
704procedure when initializing a Tcl interpreter. Similarly,
705\fBTcl_FinalizeNotifier\fR shuts down the notifier, and is
706called by \fBTcl_Finalize\fR when shutting down a Tcl interpreter.
707.PP
708\fBTcl_WaitForEvent\fR is the lowest-level procedure in the notifier;
709it is responsible for waiting for an ``interesting'' event to occur or
710for a given time to elapse. Before \fBTcl_WaitForEvent\fR is invoked,
711each of the event sources' setup procedure will have been invoked.
712The \fItimePtr\fR argument to
713\fBTcl_WaitForEvent\fR gives the maximum time to block for an event,
714based on calls to \fBTcl_SetMaxBlockTime\fR made by setup procedures
715and on other information (such as the \fBTCL_DONT_WAIT\fR bit in
716\fIflags\fR).
717.PP
718Ideally, \fBTcl_WaitForEvent\fR should only wait for an event
719to occur; it should not actually process the event in any way.
720Later on, the
721event sources will process the raw events and create Tcl_Events on
722the event queue in their \fIcheckProc\fR procedures.
723However, on some platforms (such as Windows) this isn't possible;
724events may be processed in \fBTcl_WaitForEvent\fR, including queuing
725Tcl_Events and more (for example, callbacks for native widgets may be
726invoked). The return value from \fBTcl_WaitForEvent\fR must be either
7270, 1, or \-1. On platforms such as Windows where events get processed in
728\fBTcl_WaitForEvent\fR, a return value of 1 means that there may be more
729events still pending that haven't been processed. This is a sign to the
730caller that it must call \fBTcl_WaitForEvent\fR again if it wants all
731pending events to be processed. A 0 return value means that calling
732\fBTcl_WaitForEvent\fR again will not have any effect: either this is a
733platform where \fBTcl_WaitForEvent\fR only waits without doing any event
734processing, or \fBTcl_WaitForEvent\fR knows for sure that there are no
735additional events to process (e.g. it returned because the time
736elapsed). Finally, a return value of \-1 means that the event loop is
737no longer operational and the application should probably unwind and
738terminate. Under Windows this happens when a WM_QUIT message is received;
739under Unix it happens when \fBTcl_WaitForEvent\fR would have waited
740forever because there were no active event sources and the timeout was
741infinite.
742.PP
743\fBTcl_AlertNotifier\fR is used in multithreaded applications to allow
744any thread to "wake up" the notifier to alert it to new events on its
745queue. \fBTcl_AlertNotifier\fR requires as an argument the notifier
746handle returned by \fBTcl_InitNotifier\fR.
747.PP
748If the notifier will be used with an external event loop, then it must
749also support the \fBTcl_SetTimer\fR interface. \fBTcl_SetTimer\fR is
750invoked by \fBTcl_SetMaxBlockTime\fR whenever the maximum blocking
751time has been reduced. \fBTcl_SetTimer\fR should arrange for the
752external event loop to invoke \fBTcl_ServiceAll\fR after the specified
753interval even if no events have occurred. This interface is needed
754because \fBTcl_WaitForEvent\fR isn't invoked when there is an external
755event loop. If the
756notifier will only be used from \fBTcl_DoOneEvent\fR, then
757\fBTcl_SetTimer\fR need not do anything.
758.PP
759On Unix systems, the file event source also needs support from the
760notifier. The file event source consists of the
761\fBTcl_CreateFileHandler\fR and \fBTcl_DeleteFileHandler\fR
762procedures, which are described in the \fBTcl_CreateFileHandler\fR
763manual page.
764.PP
765The \fBTcl_Sleep\fR and \fBTcl_DoOneEvent\fR interfaces are described
766in their respective manual pages.
767.PP
768The easiest way to create a new notifier is to look at the code
769for an existing notifier, such as the files \fBunix/tclUnixNotfy.c\fR
770or \fBwin/tclWinNotify.c\fR in the Tcl source distribution.
771
772.SH "EXTERNAL EVENT LOOPS"
773.PP
774The notifier interfaces are designed so that Tcl can be embedded into
775applications that have their own private event loops. In this case,
776the application does not call \fBTcl_DoOneEvent\fR except in the case
777of recursive event loops such as calls to the Tcl commands \fBupdate\fR
778or \fBvwait\fR. Most of the time is spent in the external event loop
779of the application. In this case the notifier must arrange for the
780external event loop to call back into Tcl when something
781happens on the various Tcl event sources. These callbacks should
782arrange for appropriate Tcl events to be placed on the Tcl event queue.
783.PP
784Because the external event loop is not calling \fBTcl_DoOneEvent\fR on
785a regular basis, it is up to the notifier to arrange for
786\fBTcl_ServiceEvent\fR to be called whenever events are pending on the
787Tcl event queue. The easiest way to do this is to invoke
788\fBTcl_ServiceAll\fR at the end of each callback from the external
789event loop. This will ensure that all of the event sources are
790polled, any queued events are serviced, and any pending idle handlers
791are processed before returning control to the application. In
792addition, event sources that need to poll for events can call
793\fBTcl_SetMaxBlockTime\fR to force the external event loop to call
794Tcl even if no events are available on the system event queue.
795.PP
796As a side effect of processing events detected in the main external
797event loop, Tcl may invoke \fBTcl_DoOneEvent\fR to start a recursive event
798loop in commands like \fBvwait\fR. \fBTcl_DoOneEvent\fR will invoke
799the external event loop, which will result in callbacks as described
800in the preceding paragraph, which will result in calls to
801\fBTcl_ServiceAll\fR. However, in these cases it is undesirable to
802service events in \fBTcl_ServiceAll\fR. Servicing events there is
803unnecessary because control will immediately return to the
804external event loop and hence to \fBTcl_DoOneEvent\fR, which can
805service the events itself. Furthermore, \fBTcl_DoOneEvent\fR is
806supposed to service only a single event, whereas \fBTcl_ServiceAll\fR
807normally services all pending events. To handle this situation,
808\fBTcl_DoOneEvent\fR sets a flag for \fBTcl_ServiceAll\fR
809that causes it to return without servicing any events.
810This flag is called the \fIservice mode\fR;
811\fBTcl_DoOneEvent\fR restores it to its previous value before it returns.
812.PP
813In some cases, however, it may be necessary for \fBTcl_ServiceAll\fR
814to service events
815even when it has been invoked from \fBTcl_DoOneEvent\fR. This happens
816when there is yet another recursive event loop invoked via an
817event handler called by \fBTcl_DoOneEvent\fR (such as one that is
818part of a native widget). In this case, \fBTcl_DoOneEvent\fR may not
819have a chance to service events so \fBTcl_ServiceAll\fR must service
820them all. Any recursive event loop that calls an external event
821loop rather than \fBTcl_DoOneEvent\fR must reset the service mode so
822that all events get processed in \fBTcl_ServiceAll\fR. This is done
823by invoking the \fBTcl_SetServiceMode\fR procedure. If
824\fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_NONE\fR, then calls
825to \fBTcl_ServiceAll\fR will return immediately without processing any
826events. If \fBTcl_SetServiceMode\fR is passed \fBTCL_SERVICE_ALL\fR,
827then calls to \fBTcl_ServiceAll\fR will behave normally.
828\fBTcl_SetServiceMode\fR returns the previous value of the service
829mode, which should be restored when the recursive loop exits.
830\fBTcl_GetServiceMode\fR returns the current value of the service
831mode.
832.VE
833.SH "SEE ALSO"
834\fBTcl_CreateFileHandler\fR, \fBTcl_DeleteFileHandler\fR, \fBTcl_Sleep\fR,
835\fBTcl_DoOneEvent\fR, \fBThread(3)\fR
836.SH KEYWORDS
837event, notifier, event queue, event sources, file events, timer, idle, service mode, threads