summaryrefslogtreecommitdiffstats
path: root/source/x11-event-source.c
blob: fe3544f67714ceb1a871be0b87b8a3ad5685d4be (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <glib.h>
#include <X11/Xlib.h>
#include "x11-event-source.h"

/**
 * Custom X11 Source implementation.
 */
typedef struct _X11EventSource
{
    // Source
    GSource source;
    // Polling field
    GPollFD fd_x11;
    Display *display;
} X11EventSource;

static gboolean x11_event_source_prepare ( GSource * base, gint * timeout )
{
    X11EventSource *xs = (X11EventSource *) base;
    *timeout = -1;
    return XPending ( xs->display );
}

static gboolean x11_event_source_check ( GSource * base )
{
    X11EventSource *xs = (X11EventSource *) base;
    if ( xs->fd_x11.revents ) {
        return TRUE;
    }
    return FALSE;
}

static gboolean x11_event_source_dispatch ( GSource * base, GSourceFunc callback, gpointer data )
{
    X11EventSource *xs = (X11EventSource *) base;
    if ( callback ) {
        if ( xs->fd_x11.revents ) {
            callback ( data );
        }
    }
    return G_SOURCE_CONTINUE;;
}

static GSourceFuncs x11_event_source_funcs = {
    x11_event_source_prepare,
    x11_event_source_check,
    x11_event_source_dispatch,
    NULL
};

GSource * x11_event_source_new ( Display  *display )
{
    int            x11_fd  = ConnectionNumber ( display );
    X11EventSource *source = (X11EventSource *) g_source_new ( &x11_event_source_funcs, sizeof ( X11EventSource ) );
    source->display       = display;
    source->fd_x11.fd     = x11_fd;
    source->fd_x11.events = G_IO_IN | G_IO_ERR;
    g_source_add_poll ( (GSource *) source, &source->fd_x11 );
    return (GSource *) source;
}