upload tizen2.0 source
[framework/uifw/xorg/lib/libx11.git] / src / WinEvent.c
1 /*
2
3 Copyright 1985, 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 extern long const _Xevent_to_mask[];
33 #define AllPointers (PointerMotionMask|PointerMotionHintMask|ButtonMotionMask)
34 #define AllButtons (Button1MotionMask|Button2MotionMask|Button3MotionMask|\
35                     Button4MotionMask|Button5MotionMask)
36
37 /*
38  * Return the next event in the queue
39  * for the given window matching one of the events in the mask.
40  * Events earlier in the queue are not discarded.
41  * If none found, flush, and then wait until an event arrives which
42  * matches.
43  */
44
45 int
46 XWindowEvent (
47         register Display *dpy,
48         Window w,               /* Selected window. */
49         long mask,              /* Selected event mask. */
50         register XEvent *event) /* XEvent to be filled in. */
51 {
52         register _XQEvent *prev, *qelt;
53         unsigned long qe_serial = 0;
54
55         LockDisplay(dpy);
56
57         /* Delete unclaimed cookies */
58         _XFreeEventCookies(dpy);
59
60         prev = NULL;
61         while (1) {
62             for (qelt = prev ? prev->next : dpy->head;
63                  qelt;
64                  prev = qelt, qelt = qelt->next) {
65                 if ((qelt->event.xany.window == w) &&
66                     (qelt->event.type < GenericEvent) &&
67                     (_Xevent_to_mask[qelt->event.type] & mask) &&
68                     ((qelt->event.type != MotionNotify) ||
69                      (mask & AllPointers) ||
70                      (mask & AllButtons & qelt->event.xmotion.state))) {
71                     *event = qelt->event;
72                     _XDeq(dpy, prev, qelt);
73                     UnlockDisplay(dpy);
74                     return 0;
75                 }
76             }
77             if (prev)
78                 qe_serial = prev->qserial_num;
79             _XReadEvents(dpy);
80             if (prev && prev->qserial_num != qe_serial)
81                 /* another thread has snatched this event */
82                 prev = NULL;
83         }
84 }