summaryrefslogtreecommitdiff
path: root/lib/river/src/wayland-source.c
blob: 875c32c7ce1dcd530528a39e39748d34d244f715 (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104

#include "wayland-source.h"

#include <errno.h>
#include <glib.h>
#include <wayland-client.h>

struct _WLSource {
    GSource source;
    struct wl_display *display;
    gpointer fd;
    int error;
};

static gboolean wl_source_prepare(GSource *source, gint *timeout) {
    WLSource *self = (WLSource *)source;

    *timeout = 0;
    if (wl_display_prepare_read(self->display) != 0)
        return TRUE;
    else if (wl_display_flush(self->display) < 0) {
        self->error = errno;
        return TRUE;
    }
    *timeout = -1;
    return FALSE;
}

static gboolean wl_source_check(GSource *source) {
    WLSource *self = (WLSource *)source;

    if (self->error > 0) return TRUE;

    GIOCondition revents;
    revents = g_source_query_unix_fd(source, self->fd);

    if (revents & G_IO_IN) {
        if (wl_display_read_events(self->display) < 0) self->error = errno;
    } else
        wl_display_cancel_read(self->display);

    return revents > 0;
}

static gboolean wl_source_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) {
    WLSource *self = (WLSource *)source;
    GIOCondition revents;

    revents = g_source_query_unix_fd(source, self->fd);
    if ((self->error > 0) || (revents & (G_IO_ERR | G_IO_HUP))) {
        errno = self->error;
        self->error = 0;
        if (callback != NULL) return callback(user_data);
        return G_SOURCE_REMOVE;
    }

    if (wl_display_dispatch_pending(self->display) < 0) {
        if (callback != NULL) return callback(user_data);
        return G_SOURCE_REMOVE;
    }

    return G_SOURCE_CONTINUE;
}

static void wl_source_finalize(GSource *source) {
    WLSource *self = (WLSource *)source;
    wl_display_disconnect(self->display);
}

static GSourceFuncs wl_source_funcs = {
    .prepare = wl_source_prepare,
    .check = wl_source_check,
    .dispatch = wl_source_dispatch,
    .finalize = wl_source_finalize,
};

WLSource *wl_source_new() {
    struct wl_display *display;
    WLSource *self;
    GSource *source;

    display = wl_display_connect(NULL);
    if (display == NULL) return NULL;

    source = g_source_new(&wl_source_funcs, sizeof(WLSource));
    self = (WLSource *)source;
    self->display = display;

    self->fd = g_source_add_unix_fd(source, wl_display_get_fd(self->display),
                                    G_IO_IN | G_IO_ERR | G_IO_HUP);

    g_source_attach(source, NULL);

    return self;
}

void wl_source_free(WLSource *self) {
    GSource *source = (GSource *)self;
    g_return_if_fail(source != NULL);
    g_source_destroy(source);
    g_source_unref(source);
}

struct wl_display *wl_source_get_display(WLSource *self) { return self->display; }