Home | History | Annotate | Line # | Download | only in src
      1 /*
      2 
      3 Copyright 1985, 1987, 1998  The Open Group
      4 
      5 Permission to use, copy, modify, distribute, and sell this software and its
      6 documentation for any purpose is hereby granted without fee, provided that
      7 the above copyright notice appear in all copies and that both that
      8 copyright notice and this permission notice appear in supporting
      9 documentation.
     10 
     11 The above copyright notice and this permission notice shall be included in
     12 all copies or substantial portions of the Software.
     13 
     14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
     15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
     16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
     17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
     18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
     19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
     20 
     21 Except as contained in this notice, the name of The Open Group shall not be
     22 used in advertising or otherwise to promote the sale, use or other dealings
     23 in this Software without prior written authorization from The Open Group.
     24 
     25 */
     26 
     27 #ifdef HAVE_CONFIG_H
     28 #include <config.h>
     29 #endif
     30 #include "Xlibint.h"
     31 
     32 /*
     33  * Check existing events in queue to find if any match.  If so, return.
     34  * If not, flush buffer and see if any more events are readable. If one
     35  * matches, return.  If all else fails, tell the user no events found.
     36  */
     37 
     38 Bool XCheckIfEvent (
     39 	register Display *dpy,
     40 	register XEvent *event,		/* XEvent to be filled in. */
     41 	Bool (*predicate)(
     42 			  Display*			/* display */,
     43 			  XEvent*			/* event */,
     44 			  char*				/* arg */
     45 			  ),		/* function to call */
     46 	char *arg)
     47 {
     48 	register _XQEvent *prev, *qelt;
     49 	unsigned long qe_serial = 0;
     50 	int n;			/* time through count */
     51 
     52 	LockDisplay(dpy);
     53 #ifdef XTHREADS
     54 	dpy->ifevent_thread = xthread_self();
     55 #endif
     56 	dpy->in_ifevent++;
     57 	prev = NULL;
     58 	for (n = 3; --n >= 0;) {
     59 	    for (qelt = prev ? prev->next : dpy->head;
     60 		 qelt;
     61 		 prev = qelt, qelt = qelt->next) {
     62 		if(qelt->qserial_num > qe_serial
     63 		   && (*predicate)(dpy, &qelt->event, arg)) {
     64 		    *event = qelt->event;
     65 		    _XDeq(dpy, prev, qelt);
     66 		    _XStoreEventCookie(dpy, event);
     67 		    dpy->in_ifevent--;
     68 		    UnlockDisplay(dpy);
     69 		    return True;
     70 		}
     71 	    }
     72 	    if (prev)
     73 		qe_serial = prev->qserial_num;
     74 	    switch (n) {
     75 	      case 2:
     76 		_XEventsQueued(dpy, QueuedAfterReading);
     77 		break;
     78 	      case 1:
     79 		_XFlush(dpy);
     80 		break;
     81 	    }
     82 	    if (prev && prev->qserial_num != qe_serial)
     83 		/* another thread has snatched this event */
     84 		prev = NULL;
     85 	}
     86 	dpy->in_ifevent--;
     87 	UnlockDisplay(dpy);
     88 	return False;
     89 }
     90