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
105
106
107
108
109
110
111
112
113
114
|
#include "wlstate.h"
#include "utils.h"
#include "xdg-shell.h"
#include <poll.h>
#include <string.h>
#include <wayland-client-protocol.h>
#define WAYC_VERSION 1
static void wayc_xdg_wm_base_pong(void *data, struct xdg_wm_base *wm_base,
u32 serial) {
(void)data;
xdg_wm_base_pong(wm_base, serial);
}
static struct xdg_wm_base_listener WAYC_XDG_WM_BASE_LISTENER = {
wayc_xdg_wm_base_pong,
};
static void wayc_registry_add(void *data, struct wl_registry *registry,
u32 name, const char *interface, u32 version) {
(void)version;
wayc_notnull(data);
wayc_notnull(registry);
wayc_notnull(interface);
struct wlstate_s *state = (struct wlstate_s *)data;
if (!strcmp(interface, wl_compositor_interface.name)) {
wl_compositor_t compositor = (wl_compositor_t)wl_registry_bind(
registry, name, &wl_compositor_interface, WAYC_VERSION);
wayc_notnull(compositor);
state->compositor = compositor;
}
if (!strcmp(interface, xdg_wm_base_interface.name)) {
xdg_wm_base_t wm_base = (xdg_wm_base_t)wl_registry_bind(
registry, name, &xdg_wm_base_interface, WAYC_VERSION);
wayc_notnull(wm_base);
state->wm_base = wm_base;
xdg_wm_base_add_listener(wm_base, &WAYC_XDG_WM_BASE_LISTENER, state);
}
}
static void wayc_registry_remove(void *data, struct wl_registry *registry,
u32 name) {
wayc_assert(data != NULL);
wayc_assert(registry != NULL);
wayc_panic("Registry global removed: name=%u", name);
}
static struct wl_registry_listener WAYC_REGISTRY_LISTENER = {
wayc_registry_add,
wayc_registry_remove,
};
bool wayc_wlstate_init(struct wlstate_s *state) {
wayc_assert(state != NULL);
wl_display_t display = wl_display_connect(NULL);
if (display == NULL)
return false;
i32 eventfd = wl_display_get_fd(display);
if (eventfd == -1) {
wl_display_disconnect(display);
return false;
}
wl_registry_t registry = wl_display_get_registry(display);
if (registry == NULL) {
wl_display_disconnect(display);
return false;
}
wl_registry_add_listener(registry, &WAYC_REGISTRY_LISTENER, state);
wl_display_roundtrip(display);
state->display = display;
state->registry = registry;
state->eventfd = eventfd;
return true;
}
void wayc_wlstate_deinit(struct wlstate_s *state) {
wayc_assert(state != NULL);
wl_display_disconnect(state->display);
}
void wayc_wlstate_update(struct wlstate_s *state) {
wayc_assert(state != NULL);
struct pollfd pfd = {
(i32)state->eventfd,
POLLIN,
0,
};
i32 rc = poll(&pfd, 1, 0);
if (rc == -1)
wayc_panic("Failed to poll Wayland eventfd");
if (!(pfd.revents & POLLIN))
return;
rc = wl_display_dispatch(state->display);
if (rc == -1)
wayc_panic("Failed to dispatch Wayland events");
}
|