Subject: Re: XSendEvent()
To: Jochen Schmidt <crashcore@t-online.de>
From: Robert Alexander Baxter <alex@santa.asf.alaska.edu>
List: netbsd-help
Date: 07/28/1998 15:25:16
Jochen Schmidt wrote:
> 
> I have a problem with XLib-programming.
> I want to write a X11-program that can
> react on Keyboard-events and drawing
> simplegraphics like a moving worm to
> the same time.
> When I use XNextEvent, the eventloop blocks
> until I receive a event. It seems that there
> are no timer-events in the Xlib. (I haven`t found something similar in the
> Xlib-tutorials I have)
> So i`ve tried to fork a daemon in mz program,
> that has a while(1) loop which waits some time
> with nanosleep() and after that it sends an ExposeEvent to the parent-process.
> But my Parent-process doesn`t get any Events from the daemon.
> Can someone give me a little sample-script how
> I can do that? (Or explain me a better method!)
>

Let me check that I understand what you want to happen:

   The line (worm) keeps going until a key is pressed
   which changes its direction.  So, you want your program
   to extend the worm another pixel every half a second
   or something like that.

You can use a while() loop and XCheckWindowEvent()
rather than XNextEvent(), because XCheckWindowEvent()
is non-blocking.

Another approach would be to use the Xt Toolkit.  You can
find *many* example programs using the Xt Toolkit in this
file:

   ftp://ftp.oreilly.com/pub/examples/xbook/xt/xtprogs2.tar.Z

Basically what you can do is use the XtAppMainLoop() function
rather than XNextEvent().  This allows you to use the function
XtAppAddTimeOut() which will make the event handler loop call
your function of choice (move the worm!) every N milliseconds.

The Xt Toolkit should be available wherever X Windows is installed,
even though it never was officially part of the X standard.  Look for
files like:

   <your X directory tree>/lib/libXt.a
   <your X directory tree>/include/X11/Intrinsic.h
   <your X directory tree>/include/X11/Xaw/*

The O'Reilly X Window volumes 4 ("X Toolkit Intrinsics Programming
Manual")
and 5 ("X Toolkit Intrinsics Reference Manual") tell all about using
the Xt Toolkit.

The Xt Toolkit is nice because stuff like creating your main window,
dialog windows, and communication with the window manager is simplified.
However you do not give up your programming freedom because you can
still access all the Xlib data structures and functions.  Indeed you
must still do that to draw the worm!

Good luck; please ask for more details if you need them,
 -Alex (:-)
  alex@santa.asf.alaska.edu