Many X applications require input from sources other than the X event
queue. The X Toolkit provides a way for registering procedures to be
invoked when there is input coming from a source such as UNIX file.
The call to xtAppAddInput/6
accomplishes that. The first argument in
xtAppAddInput/6
is the application context, the UNIX file descriptor
for the input source is second, the third is the input condition mask and the
next two arguments are the predicate and client data. An Id is returned in
the final argument, which can be used to unregister the procedure.
Another useful facility in the X Toolkit allows applications to
register callback procedures to be invoked when a specified interval
has elapsed. The call to xtAppAddTimeOut/5
accomplishes that. The
time interval is measured in milliseconds and the registered procedure
must be of arity 2, where the first argument is the client data and
the second is the interval id. Following is an extension to the first
example, which sets a timer when the push button is activated. This in
turn activates a procedure some 2 seconds later.
timer(_ClientData,_IntervalId) :- write('Button pressed approximately 2 seconds earlier...'), nl. set_timer(Widget,Interval,_CallData) :- xtWidgetToApplicationContext(Widget, App), xtAppAddTimeOut(App,Interval,timer,_,_Id). time :- xtAppInitialize(App,'Test',[],Shell), xmCreatePushButton(Shell,push_button, [xmNactivateCallback([callback(set_timer,2000)]), xmNwidth(100), xmNheight(100)], Button), xtManageChild(Button), xtRealizeWidget(Shell), xtAppMainLoop(App).
The X Toolkit includes another type of callback mechanism, known as a
WorkProc, that provides a limited form of background processing. It
allows the application to invoke a callback whenever there are no
events pending. The application can register WorkProc procedures
using the call to xtAppAddWorkProc/4
.