index

Xevents

if i set StructureNotifyMask:

SubStructureNotifyMask

Keyboard

MappingNotify ... to ALL windows!!

(Input) Event queue

0.3. Event Queue Management

Xlib maintains an event queue. However, the operating system also may be buffering data in its network connection that is not yet read into the event queue.

To check the number of events in the event queue, use XEventsQueued.

int XEventsQueued(display, mode)
Display *display; int mode;

display Specifies the connection to the X server.

mode Specifies the mode. You can pass QueuedAlready,
QueuedAfterFlush, or QueuedAfterReading.

If mode is QueuedAlready, XEventsQueued returns the number of events already in the event queue (and never performs a system call). If mode is QueuedAfterFlush, XEventsQueued returns the number of events already in the queue if the number is nonzero. If there are no events in the queue, XEventsQueued flushes the output buffer, attempts to read more events out of the application's connection, and returns the number read. If mode is QueuedAfterReading,

XEventsQueued returns the number of events already in the queue if the number is nonzero. If there are no events in the queue, XEventsQueued attempts to read more events out of the application's connection without flushing the output buffer and returns the number read.

XEventsQueued always returns immediately without I/O if there are events already in the queue. XEventsQueued with mode QueuedAfterFlush is identical in behavior to XPending. XEventsQueued with mode QueuedAlready is identical to the XQLength function.

To return the number of events that are pending, use XPending.

int XPending(display)
Display *display;

display Specifies the connection to the X server.

The XPending function returns the number of events that have been received from the X server but have not been removed from the event queue. XPending is identical to XEventsQueued with the mode QueuedAfterFlush specified.

0.4. Manipulating the Event Queue

Xlib provides functions that let you manipulate the event queue. This section discusses how to:

0.4.1. Returning the Next Event

To get the next event and remove it from the queue, use XNextEvent.

XNextEvent(display, event_return)
Display *display;
XEvent *event_return;

display Specifies the connection to the X server.

event_return
Returns the next event in the queue.

The XNextEvent function copies the first event from the event queue into the specified XEvent structure and then removes it from the queue. If the event queue is empty, XNextEvent flushes the output buffer and blocks until an event is received.

To peek at the event queue, use XPeekEvent.

XPeekEvent(display, event_return)
Display *display;
XEvent *event_return;

display Specifies the connection to the X server.

event_return
Returns a copy of the matched event's associated
structure.

The XPeekEvent function returns the first event from the event queue, but it does not remove the event from the queue. If the queue is empty, XPeekEvent flushes the output buffer and blocks until an event is received. It then copies the event into the client-supplied XEvent structure without removing it from the event queue.

0.4.2. Selecting Events Using a Predicate Procedure

Each of the functions discussed in this section requires you to pass a predicate procedure that determines if an event matches what you want. Your predicate procedure must decide if the event is useful without calling any Xlib functions. If the predicate directly or indirectly causes the state of the event queue to change, the result is not defined. If Xlib has been initialized for threads, the predicate is called with the display locked and the result of a call by the predicate to any Xlib function that locks the display is not defined unless the caller has first called XLockDisplay.

The predicate procedure and its associated arguments are:

Bool (*predicate)(display, event, arg)
Display *display;
XEvent *event;
XPointer arg;

display Specifies the connection to the X server.

event Specifies the XEvent structure.

arg Specifies the argument passed in from the
XIfEvent, XCheckIfEvent, or XPeekIfEvent function.

The predicate procedure is called once for each event in the queue until it finds a match. After finding a match, the predicate procedure must return True. If it did not find a match, it must return False.

To check the event queue for a matching event and, if found, remove the event from the queue, use XIfEvent.

XIfEvent(display, event_return, predicate, arg)
Display *display;
XEvent *event_return;
Bool (*predicate)();
XPointer arg;

display Specifies the connection to the X server.

event_return
Returns the matched event's associated structure.

predicate Specifies the procedure that is to be called to
determine if the next event in the queue matches
what you want.

arg Specifies the user-supplied argument that will be
passed to the predicate procedure.


The XIfEvent function completes only when the specified predicate procedure returns True for an event, which
indicates an event in the queue matches. XIfEvent flushes the output buffer if it blocks waiting for additional
events.  XIfEvent removes the matching event from the queue and copies the structure into the client-supplied
XEvent structure.
                                                                                                 
To check the event queue for a matching event without blocking, use XCheckIfEvent.

<example>
Bool XCheckIfEvent(display, event_return, predicate, arg)
Display *display;
XEvent *event_return;
Bool (*predicate)();
XPointer arg;

display Specifies the connection to the X server.

event_return
Returns a copy of the matched event's associated
structure.

predicate Specifies the procedure that is to be called to
determine if the next event in the queue matches
what you want.

arg Specifies the user-supplied argument that will be
passed to the predicate procedure.

When the predicate procedure finds a match, XCheckIfEvent copies the matched event into the client-supplied XEvent structure and returns True. (This event is removed from the queue.) If the predicate procedure finds no match, XCheckIfEvent returns False, and the output buffer will have been flushed. All earlier events stored in the queue are not discarded.

To check the event queue for a matching event without removing the event from the queue, use XPeekIfEvent.

XPeekIfEvent(display, event_return, predicate, arg)
Display *display;
XEvent *event_return;
Bool (*predicate)();
XPointer arg;

display Specifies the connection to the X server.

event_return
Returns a copy of the matched event's associated
structure.

predicate Specifies the procedure that is to be called to
determine if the next event in the queue matches
what you want.

arg Specifies the user-supplied argument that will be
passed to the predicate procedure.

The XPeekIfEvent function returns only when the specified predicate procedure returns True for an event. After the predicate procedure finds a match, XPeekIfEvent copies the matched event into the client-supplied XEvent structure without removing the event from the queue. XPeekIfEvent flushes the output buffer if it blocks waiting for additional events.

0.4.3. Selecting Events Using a Window or Event Mask

The functions discussed in this section let you select events by window or event types, allowing you to process events out of order.

To remove the next event that matches both a window and an event mask, use XWindowEvent.

XWindowEvent(display, w, event_mask, event_return)
Display *display;
Window w;
long event_mask;
XEvent *event_return;

display Specifies the connection to the X server.

w Specifies the window whose events you are inter-
ested in.

event_mask
Specifies the event mask.

event_return
Returns the matched event's associated structure.

The XWindowEvent function searches the event queue for an event that matches both the specified window and event mask. When it finds a match, XWindowEvent removes that event from the queue and copies it into the specified XEvent structure. The other events stored in the queue are not discarded. If a matching event is not in the queue, XWindowEvent flushes the output buffer and blocks until one is received.

To remove the next event that matches both a window and an event mask (if any), use XCheckWindowEvent. This function is similar to XWindowEvent except that it never blocks and it returns a Bool indicating if the event was returned.

Bool XCheckWindowEvent(display, w, event_mask, event_return)
Display *display;
Window w;
long event_mask;
XEvent *event_return;

display Specifies the connection to the X server.

w Specifies the window whose events you are inter-
ested in.

event_mask
Specifies the event mask.

event_return
Returns the matched event's associated structure.

The XCheckWindowEvent function searches the event queue and then the events available on the server connection for the first event that matches the specified window and event mask. If it finds a match, XCheckWindowEvent removes that event, copies it into the specified XEvent structure, and returns True. The other events stored in the queue are not discarded. If the event you requested is not available, XCheckWindowEvent returns False, and the output buffer will have been flushed.

To remove the next event that matches an event mask, use XMaskEvent.

XMaskEvent(display, event_mask, event_return)
Display *display;
long event_mask;
XEvent *event_return;

display Specifies the connection to the X server.

event_mask
Specifies the event mask.

event_return
Returns the matched event's associated structure.

The XMaskEvent function searches the event queue for the events associated with the specified mask. When it finds a match, XMaskEvent removes that event and copies it into the specified XEvent structure. The other events stored in the

queue are not discarded. If the event you requested is not in the queue, XMaskEvent flushes the output buffer and blocks until one is received.

To return and remove the next event that matches an event mask (if any), use XCheckMaskEvent. This function is simi- lar to XMaskEvent except that it never blocks and it returns a Bool indicating if the event was returned.

Bool XCheckMaskEvent(display, event_mask, event_return)
Display *display;
long event_mask;
XEvent *event_return;
display Specifies the connection to the X server.

event_mask
Specifies the event mask.

event_return
Returns the matched event's associated structure.

The XCheckMaskEvent function searches the event queue and then any events available on the server connection for the first event that matches the specified mask. If it finds a match, XCheckMaskEvent removes that event, copies it into the specified XEvent structure, and returns True. The other events stored in the queue are not discarded. If the event you requested is not available, XCheckMaskEvent returns False, and the output buffer will have been flushed.

To return and remove the next event in the queue that matches an event type, use XCheckTypedEvent.

Bool XCheckTypedEvent(display, event_type, event_return)
Display *display;
int event_type;
XEvent *event_return;

display Specifies the connection to the X server.

event_type
Specifies the event type to be compared.

event_return
Returns the matched event's associated structure.

The XCheckTypedEvent function searches the event queue and then any events available on the server connection for the first event that matches the specified type. If it finds a match, XCheckTypedEvent removes that event, copies it into the specified XEvent structure, and returns True. The other events in the queue are not discarded. If the event is not available, XCheckTypedEvent returns False, and the output buffer will have been flushed.

To return and remove the next event in the queue that matches an event type and a window, use XCheckTypedWindowEvent.

Bool XCheckTypedWindowEvent(display, w, event_type, event_return)
Display *display;
Window w;
int event_type;
XEvent *event_return;

display Specifies the connection to the X server.

w Specifies the window.

event_type
Specifies the event type to be compared.

event_return
Returns the matched event's associated structure.

The XCheckTypedWindowEvent function searches the event queue and then any events available on the server connection for the first event that matches the specified type and window.

If it finds a match, XCheckTypedWindowEvent removes the event from the queue, copies it into the specified XEvent structure, and returns True. The other events in the queue are not discarded. If the event is not available, XCheck- TypedWindowEvent returns False, and the output buffer will

0.5. Putting an Event Back into the Queue

To push an event back into the event queue, use XPutBackEvent.

XPutBackEvent(display, event)
Display *display;
XEvent *event;

display Specifies the connection to the X server.

event Specifies the event.

The XPutBackEvent function pushes an event back onto the head of the display's event queue by copying the event into the queue. This can be useful if you read an event and then decide that you would rather deal with it later. There is no limit to the number of times in succession that you can call XPutBackEvent.