IfEvent.c revision 1ab64890
1/* $Xorg: IfEvent.c,v 1.4 2001/02/09 02:03:34 xorgcvs Exp $ */ 2/* 3 4Copyright 1986, 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: xc/lib/X11/IfEvent.c,v 1.4 2001/12/14 19:54:01 dawes Exp $ */ 28 29#define NEED_EVENTS 30#ifdef HAVE_CONFIG_H 31#include <config.h> 32#endif 33#include "Xlibint.h" 34 35/* 36 * Flush output and (wait for and) return the next event matching the 37 * predicate in the queue. 38 */ 39 40int 41XIfEvent ( 42 register Display *dpy, 43 register XEvent *event, 44 Bool (*predicate)( 45 Display* /* display */, 46 XEvent* /* event */, 47 char* /* arg */ 48 ), /* function to call */ 49 char *arg) 50{ 51 register _XQEvent *qelt, *prev; 52 unsigned long qe_serial = 0; 53 54 LockDisplay(dpy); 55 prev = NULL; 56 while (1) { 57 for (qelt = prev ? prev->next : dpy->head; 58 qelt; 59 prev = qelt, qelt = qelt->next) { 60 if(qelt->qserial_num > qe_serial 61 && (*predicate)(dpy, &qelt->event, arg)) { 62 *event = qelt->event; 63 _XDeq(dpy, prev, qelt); 64 UnlockDisplay(dpy); 65 return 0; 66 } 67 } 68 if (prev) 69 qe_serial = prev->qserial_num; 70 _XReadEvents(dpy); 71 if (prev && prev->qserial_num != qe_serial) 72 /* another thread has snatched this event */ 73 prev = NULL; 74 } 75} 76