summaryrefslogtreecommitdiff
path: root/lib/XCL/Pending.c
blob: 4e8380fe21fceaa3607c05ddf8021460a8196e8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* Copyright (C) 2001-2002 Bart Massey and Jamey Sharp.
 * All Rights Reserved.
 * Portions Copyright 1986, 1998  The Open Group
 * 
 * See the file COPYING for licensing information. */
#include "xclint.h"
#include <xcb_event.h>

/* Read in pending events if needed and return the number of queued events. */
int XEventsQueued(register Display *dpy, int mode)
{
    int ret = XCBEventQueueLength(XCBConnectionOfDisplay(dpy));
    if(!ret && mode != QueuedAlready)
	ret = _XEventsQueued(dpy, mode);
    return ret;
}

int XPending(Display *dpy)
{
    return XEventsQueued(dpy, QueuedAfterFlush);
}

int _XEventsQueued(register Display *dpy, int mode)
{
    XCBConnection *c = XCBConnectionOfDisplay(dpy);
    if(mode == QueuedAfterFlush)
	XCBFlush(c);

    /* wait for handle to be readable, without blocking. */
    /* FIXME: this select/XCBWait hack breaks encapsulation. */
    if(XCBEventQueueIsEmpty(c))
    {
	fd_set fds;
	struct timeval tv = { 0, 0 };
	FD_ZERO(&fds);
	FD_SET(dpy->fd, &fds);
	if(select(dpy->fd + 1, &fds, 0, 0, &tv) < 1)
	    return 0;
	XCBWait(c->handle, /* should_write */ 0);
    }

    return XCBEventQueueLength(c);
}