| 1 |
# Copyright (c) 1990-1994 The Regents of the University of California. |
| 2 |
# Copyright (c) 1994 Sun Microsystems, Inc. |
| 3 |
# See the file "license.terms" for information on usage and redistribution |
| 4 |
# of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 5 |
# |
| 6 |
# @(#) FileHndlr.3 1.12 95/05/06 15:29:24 |
| 7 |
# |
| 8 |
|
| 9 |
=head1 NAME |
| 10 |
|
| 11 |
Tk_CreateFileHandler, Tk_CreateFileHandler2, Tk_DeleteFileHandler - associate procedure callbacks with files or devices |
| 12 |
|
| 13 |
=for category C Programming |
| 14 |
|
| 15 |
=head1 SYNOPSIS |
| 16 |
|
| 17 |
B<#include E<lt>tk.hE<gt>> |
| 18 |
|
| 19 |
B<Tk_CreateFileHandler>(I<id, mask, proc, clientData>) |
| 20 |
|
| 21 |
B<Tk_CreateFileHandler2>(I<id, proc2, clientData>) |
| 22 |
|
| 23 |
B<Tk_DeleteFileHandler>(I<id>) |
| 24 |
|
| 25 |
=head1 ARGUMENTS |
| 26 |
|
| 27 |
=over 4 |
| 28 |
|
| 29 |
=item int id (in) |
| 30 |
|
| 31 |
Integer identifier for an open file or device (such as returned by |
| 32 |
B<open> system call). |
| 33 |
|
| 34 |
=item int mask (in) |
| 35 |
|
| 36 |
Conditions under which I<proc> should be called: |
| 37 |
OR-ed combination of B<TK_READABLE>, B<TK_WRITABLE>, |
| 38 |
and B<TK_EXCEPTION>. |
| 39 |
|
| 40 |
=item Tk_FileProc *proc (in) |
| 41 |
|
| 42 |
Procedure to invoke whenever the file or device indicated |
| 43 |
by I<id> meets the conditions specified by I<mask>. |
| 44 |
|
| 45 |
=item Tk_FileProc2 *proc2 (in) |
| 46 |
|
| 47 |
Procedure to invoke from event loop to check whether I<fd> |
| 48 |
is ready and, if so, handle it. |
| 49 |
|
| 50 |
=item ClientData clientData (in) |
| 51 |
|
| 52 |
Arbitrary one-word value to pass to I<proc>. |
| 53 |
|
| 54 |
=back |
| 55 |
|
| 56 |
=head1 DESCRIPTION |
| 57 |
|
| 58 |
B<Tk_CreateFileHandler> arranges for I<proc> to be |
| 59 |
invoked in the future whenever I/O becomes possible on a file |
| 60 |
or an exceptional condition exists for the file. The file |
| 61 |
is indicated by I<id>, and the conditions of interest |
| 62 |
are indicated by I<mask>. For example, if I<mask> |
| 63 |
is B<TK_READABLE>, I<proc> will be called when |
| 64 |
the file is readable. |
| 65 |
The callback to I<proc> is made by B<Tk_DoOneEvent>, so |
| 66 |
B<Tk_CreateFileHandler> is only useful |
| 67 |
in programs that dispatch events |
| 68 |
through B<Tk_DoOneEvent> or through other Tk procedures that |
| 69 |
call B<Tk_DoOneEvent>, such as B<Tk_MainLoop>. |
| 70 |
|
| 71 |
I<Proc> should have arguments and result that match the |
| 72 |
type B<Tk_FileProc>: |
| 73 |
|
| 74 |
=over 4 |
| 75 |
|
| 76 |
typedef void Tk_FileProc( |
| 77 |
|
| 78 |
=over 4 |
| 79 |
|
| 80 |
ClientData I<clientData>, |
| 81 |
int I<mask>); |
| 82 |
|
| 83 |
=back |
| 84 |
|
| 85 |
=back |
| 86 |
|
| 87 |
The I<clientData> parameter to I<proc> is a copy |
| 88 |
of the I<clientData> |
| 89 |
argument given to B<Tk_CreateFileHandler> when the callback |
| 90 |
was created. Typically, I<clientData> points to a data |
| 91 |
structure containing application-specific information about |
| 92 |
the file. I<Mask> is an integer mask indicating which |
| 93 |
of the requested conditions actually exists for the file; it |
| 94 |
will contain a subset of the bits in the I<mask> argument |
| 95 |
to B<Tk_CreateFileHandler>. |
| 96 |
|
| 97 |
B<Tk_CreateFileHandler2> also creates a file handler, |
| 98 |
but it provides a lower-level and more flexible interface. |
| 99 |
The callback procedure I<proc2> must have arguments and result |
| 100 |
that match the following prototype: |
| 101 |
|
| 102 |
=over 4 |
| 103 |
|
| 104 |
typedef int Tk_FileProc2( |
| 105 |
|
| 106 |
=over 4 |
| 107 |
|
| 108 |
ClientData I<clientData>, |
| 109 |
int I<mask>, |
| 110 |
int I<flags>); |
| 111 |
|
| 112 |
=back |
| 113 |
|
| 114 |
=back |
| 115 |
|
| 116 |
Whereas a file handler created by B<Tk_CreateFileHandler> is |
| 117 |
only invoked when the file is known to be ``ready'', a file handler |
| 118 |
created by B<Tk_CreateFileHandler2> is invoked on every pass |
| 119 |
through the the event loop (B<Tk_DoWhenIdle>); it gets to |
| 120 |
determine whether the file is ``ready'' or not. |
| 121 |
The I<mask> argument contains an OR'ed combination of the |
| 122 |
bits B<TK_READABLE>, B<TK_WRITABLE>, and B<TK_EXCEPTION>, |
| 123 |
which indicate whether the file is known to be readable, writable, |
| 124 |
or to have an exceptional condition present (this is the case if |
| 125 |
B<select> has been invoked since the previous call to I<proc2>, |
| 126 |
and if it indicated that the specified conditions were present). |
| 127 |
I<proc2> may use this information along with additional information |
| 128 |
of its own, such as knowledge about buffered data, to decide whether |
| 129 |
the file is really ``ready''. |
| 130 |
The I<flags> argument is a copy of the flags passed to |
| 131 |
B<Tk_DoOneEvent>, which may be used by I<proc2> to ignore |
| 132 |
the file if the appropriate bit, such as B<TK_FILE_EVENTS>, |
| 133 |
is not present. |
| 134 |
|
| 135 |
I<proc2> must return an integer value that is either B<TK_FILE_HANDLED> |
| 136 |
or an OR-ed combination of B<TK_READABLE>, B<TK_WRITABLE>, and |
| 137 |
B<TK_EXCEPTION>. |
| 138 |
If the return value is B<TK_FILE_HANDLED> it means that the file |
| 139 |
was ``ready'' and that I<proc2> handled the ready condition; |
| 140 |
B<Tk_DoOneEvent> will return immediately. |
| 141 |
If the return value is not B<TK_FILE_HANDLED>, then it indicates |
| 142 |
the set of conditions that should be checked for the file if the |
| 143 |
current invocation of B<Tk_DoWhenIdle> invokes B<select>. |
| 144 |
Typically the return value reflects all of the conditions that |
| 145 |
I<proc2> cares about. |
| 146 |
A zero return value means that the file should be ignored if |
| 147 |
B<Tk_DoWhenIdle> calls B<select> (this could happen, for |
| 148 |
example, if the I<flags> |
| 149 |
argument specified that this file's events should be ignored). |
| 150 |
The value returned by I<proc2> only affects a B<select> call |
| 151 |
from the current invocation of B<Tk_DoOneEvent>; the next |
| 152 |
invocation of B<Tk_DoOneEvent> will call I<proc2> afresh |
| 153 |
to get new information. |
| 154 |
|
| 155 |
There may exist only one handler for a given file at a given |
| 156 |
time. If B<Tk_CreateFileHandler> or B<Tk_CreateFileHandler2> |
| 157 |
is called when a handler already exists for I<id>, then the |
| 158 |
new callback replaces the information that was |
| 159 |
previously recorded. |
| 160 |
|
| 161 |
B<Tk_DeleteFileHandler> may be called to delete the |
| 162 |
file handler for I<id>; if no handler exists for the |
| 163 |
file given by I<id> then the procedure has no effect. |
| 164 |
|
| 165 |
The purpose of file handlers is to enable an application to |
| 166 |
respond to X events and other events while waiting for files |
| 167 |
to become ready for I/O. For this to work correctly, the |
| 168 |
application may need to use non-blocking I/O operations on the |
| 169 |
files for which handlers are declared. Otherwise the application |
| 170 |
may be put to sleep if it reads or writes too much data; |
| 171 |
while waiting for the I/O to complete the |
| 172 |
application won't be able to service other events. In BSD-based |
| 173 |
UNIX systems, non-blocking I/O can be specified for a file using |
| 174 |
the B<fcntl> kernel call with the B<FNDELAY> flag. |
| 175 |
|
| 176 |
=head1 KEYWORDS |
| 177 |
|
| 178 |
callback, file, handler |