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