[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Event handling; suggestions please?

Shadow Mint wrote:

> Currently, I have an event module which updates the physics of the game
> periodically (short time; frame length ~ 25 ms or less, depending on
> machine). I'm interested in suggestions on how to handle keyboard and
> mouse events. The std. xlib X event loop is an idea; but I've already
> got an event loop; and it doesn't include that... I could I guess, do
> one of two things; either check at the end of each frame for buffered
> cursor and keyboard events, and update as appropriate (I'm thinking this
> may be the way to go at the moment), or a I can try to fork the process,
> allocate some shared memory and use the other process as dedicated event
> handler.

I think that forking a process to handle events is not a good idea
(context switching on blitting requests is one thing, but doing so for
each keyboard events is another!).

Why don't you check for X events in the event loop you already have? You
simply check if there are events using XPending, handling the events if
the number is non-zero.

I'd suggest only handling the number of events returned by the first
call to XPending, not checking for events as they are handled, so that
if X events are piling up quickly, the rest of your event loop gets
handled too. Like this:

int events;

for(events = XPending(dpy); events > 0; events--) {
  XEvent ev;

  XNextEvent(dpy, &ev);

  /* handle event */

Maybe use XEventsQueued with QueuedAfterReading would be preferable over
XPending, as it wouldn't flush the message queue, but maybe you do want
to flush (it is safer).

Pierre Phaneuf