11ab64890Smrg/*
21ab64890Smrg
31ab64890SmrgCopyright 1985, 1987, 1998  The Open Group
41ab64890Smrg
51ab64890SmrgPermission to use, copy, modify, distribute, and sell this software and its
61ab64890Smrgdocumentation for any purpose is hereby granted without fee, provided that
71ab64890Smrgthe above copyright notice appear in all copies and that both that
81ab64890Smrgcopyright notice and this permission notice appear in supporting
91ab64890Smrgdocumentation.
101ab64890Smrg
111ab64890SmrgThe above copyright notice and this permission notice shall be included in
121ab64890Smrgall copies or substantial portions of the Software.
131ab64890Smrg
141ab64890SmrgTHE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
151ab64890SmrgIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161ab64890SmrgFITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
171ab64890SmrgOPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
181ab64890SmrgAN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
191ab64890SmrgCONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
201ab64890Smrg
211ab64890SmrgExcept as contained in this notice, the name of The Open Group shall not be
221ab64890Smrgused in advertising or otherwise to promote the sale, use or other dealings
231ab64890Smrgin this Software without prior written authorization from The Open Group.
241ab64890Smrg
251ab64890Smrg*/
261ab64890Smrg
271ab64890Smrg#ifdef HAVE_CONFIG_H
281ab64890Smrg#include <config.h>
291ab64890Smrg#endif
301ab64890Smrg#include "Xlibint.h"
311ab64890Smrg
3261b2299dSmrg/*
331ab64890Smrg * Check existing events in queue to find if any match.  If so, return.
341ab64890Smrg * If not, flush buffer and see if any more events are readable. If one
351ab64890Smrg * matches, return.  If all else fails, tell the user no events found.
361ab64890Smrg */
371ab64890Smrg
381ab64890SmrgBool XCheckIfEvent (
391ab64890Smrg	register Display *dpy,
401ab64890Smrg	register XEvent *event,		/* XEvent to be filled in. */
411ab64890Smrg	Bool (*predicate)(
421ab64890Smrg			  Display*			/* display */,
431ab64890Smrg			  XEvent*			/* event */,
441ab64890Smrg			  char*				/* arg */
451ab64890Smrg			  ),		/* function to call */
461ab64890Smrg	char *arg)
471ab64890Smrg{
481ab64890Smrg	register _XQEvent *prev, *qelt;
491ab64890Smrg	unsigned long qe_serial = 0;
501ab64890Smrg	int n;			/* time through count */
511ab64890Smrg
520efe039aSmartin	LockDisplay(dpy);
53876003caSmrg#ifdef XTHREADS
54876003caSmrg	dpy->ifevent_thread = xthread_self();
55876003caSmrg#endif
56876003caSmrg	dpy->in_ifevent++;
571ab64890Smrg	prev = NULL;
581ab64890Smrg	for (n = 3; --n >= 0;) {
591ab64890Smrg	    for (qelt = prev ? prev->next : dpy->head;
601ab64890Smrg		 qelt;
611ab64890Smrg		 prev = qelt, qelt = qelt->next) {
621ab64890Smrg		if(qelt->qserial_num > qe_serial
631ab64890Smrg		   && (*predicate)(dpy, &qelt->event, arg)) {
641ab64890Smrg		    *event = qelt->event;
651ab64890Smrg		    _XDeq(dpy, prev, qelt);
66b4ee4795Smrg		    _XStoreEventCookie(dpy, event);
670efe039aSmartin		    dpy->in_ifevent--;
681ab64890Smrg		    UnlockDisplay(dpy);
691ab64890Smrg		    return True;
701ab64890Smrg		}
711ab64890Smrg	    }
721ab64890Smrg	    if (prev)
731ab64890Smrg		qe_serial = prev->qserial_num;
741ab64890Smrg	    switch (n) {
751ab64890Smrg	      case 2:
761ab64890Smrg		_XEventsQueued(dpy, QueuedAfterReading);
771ab64890Smrg		break;
781ab64890Smrg	      case 1:
791ab64890Smrg		_XFlush(dpy);
801ab64890Smrg		break;
811ab64890Smrg	    }
821ab64890Smrg	    if (prev && prev->qserial_num != qe_serial)
831ab64890Smrg		/* another thread has snatched this event */
841ab64890Smrg		prev = NULL;
851ab64890Smrg	}
860efe039aSmartin	dpy->in_ifevent--;
871ab64890Smrg	UnlockDisplay(dpy);
881ab64890Smrg	return False;
891ab64890Smrg}
90