summaryrefslogtreecommitdiff
path: root/src/wlstate.cc
blob: 92328437a0280b6be8f15e916ee1aac802357157 (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
#include "wlstate.h"
#include "utils.h"
#include "xdg-shell.h"
#include <string.h>
#include <wayland-client-core.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 wl_state_s *state = (struct wl_state_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_wl_state_init(struct wl_state_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_wl_state_deinit(struct wl_state_s *state) {
  wayc_assert(state != NULL);

  wl_display_disconnect(state->display);
}