~sircmpwn/xrgears

ref: fb9d492b99b8bb61f3c60bd3c976bccb800df57d xrgears/vkc/vkcWindowWayland.hpp -rw-r--r-- 9.6 KiB
fb9d492b — Lubosz Sarnecki move surface and format to swapchain. 4 years ago
                                                                                
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#pragma once

#include <string.h>
#include <stdio.h>


#include <poll.h>

#include <vulkan/vulkan.h>

#include <xdg-shell-unstable-v6-client-protocol.h>
#include <wayland-client.h>
#include <linux/input.h>

#include "vkcWindow.hpp"
#include "vkcApplication.hpp"
#include "vkcRenderer.hpp"

#include "../vks/vksLog.hpp"

namespace vkc {
class WindowWayland : public Window {

  struct wl_display *display;
  struct wl_compositor *compositor;
  struct zxdg_shell_v6 *shell;
  struct wl_keyboard *keyboard;
  struct wl_seat *seat;
  struct wl_surface *surface;
  struct zxdg_surface_v6 *xdg_surface;
  struct zxdg_toplevel_v6 *xdg_toplevel;
  bool wait_for_configure;

  int hmd_refresh = 0;
  wl_output *hmd_output = nullptr;

public:
  WindowWayland() {
    name = "wayland-xdg";
    seat = NULL;
    keyboard = NULL;
    shell = NULL;
  }

  ~WindowWayland() {}

  static void
  handle_xdg_surface_configure(void *data, struct zxdg_surface_v6 *surface,
                               uint32_t serial)
  {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);

    zxdg_surface_v6_ack_configure(surface, serial);

    if (self->wait_for_configure) {
      // redraw
      self->wait_for_configure = false;
    }
  }

  const struct zxdg_surface_v6_listener xdg_surface_listener = {
    handle_xdg_surface_configure,
  };

  static void
  handle_xdg_toplevel_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
                                int32_t width, int32_t height,
                                struct wl_array *states)
  {
  }

  static void
  handle_xdg_toplevel_close(void *data, struct zxdg_toplevel_v6 *toplevel)
  {
  }

  const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
    handle_xdg_toplevel_configure,
    handle_xdg_toplevel_close,
  };

  static void
  handle_xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
  {
    zxdg_shell_v6_pong(shell, serial);
  }

  const struct zxdg_shell_v6_listener xdg_shell_listener = {
    handle_xdg_shell_ping,
  };

  static void
  handle_wl_keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard,
                            uint32_t format, int32_t fd, uint32_t size)
  {
  }

  static void
  handle_wl_keyboard_enter(void *data, struct wl_keyboard *wl_keyboard,
                           uint32_t serial, struct wl_surface *surface,
                           struct wl_array *keys)
  {
  }

  static void
  handle_wl_keyboard_leave(void *data, struct wl_keyboard *wl_keyboard,
                           uint32_t serial, struct wl_surface *surface)
  {
  }

  static void
  handle_wl_keyboard_key(void *data, struct wl_keyboard *wl_keyboard,
                         uint32_t serial, uint32_t time, uint32_t key,
                         uint32_t state)
  {
    if (key == KEY_ESC && state == WL_KEYBOARD_KEY_STATE_PRESSED)
      exit(0);
  }

  static void
  handle_wl_keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard,
                               uint32_t serial, uint32_t mods_depressed,
                               uint32_t mods_latched, uint32_t mods_locked,
                               uint32_t group)
  {
  }

  static void
  handle_wl_keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
                                 int32_t rate, int32_t delay)
  {
  }

  const struct wl_keyboard_listener wl_keyboard_listener = {
    .keymap = handle_wl_keyboard_keymap,
    .enter = handle_wl_keyboard_enter,
    .leave = handle_wl_keyboard_leave,
    .key = handle_wl_keyboard_key,
    .modifiers = handle_wl_keyboard_modifiers,
    .repeat_info = handle_wl_keyboard_repeat_info,
  };

  static void
  handle_wl_seat_capabilities(void *data, struct wl_seat *wl_seat,
                              uint32_t capabilities)
  {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);

    if ((capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && (!self->keyboard)) {
      self->keyboard = wl_seat_get_keyboard(wl_seat);
      wl_keyboard_add_listener(self->keyboard, &self->wl_keyboard_listener, self);
    } else if (!(capabilities & WL_SEAT_CAPABILITY_KEYBOARD) && self->keyboard) {
      wl_keyboard_destroy(self->keyboard);
      self->keyboard = NULL;
    }
  }

  const struct wl_seat_listener wl_seat_listener = {
    handle_wl_seat_capabilities,
  };

  static void
  registry_handle_global(void *data, struct wl_registry *registry,
                         uint32_t name, const char *interface, uint32_t version)
  {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);

    if (strcmp(interface, "wl_compositor") == 0) {
      self->compositor = ( wl_compositor *) wl_registry_bind(registry, name,
                                                             &wl_compositor_interface, 1);
    } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
      self->shell = (zxdg_shell_v6 *)  wl_registry_bind(registry, name, &zxdg_shell_v6_interface, 1);
      zxdg_shell_v6_add_listener(self->shell, &self->xdg_shell_listener, self);
    } else if (strcmp(interface, "wl_seat") == 0) {
      self->seat = (wl_seat *) wl_registry_bind(registry, name, &wl_seat_interface, 1);
      wl_seat_add_listener(self->seat, &self->wl_seat_listener, self);
    } else if (strcmp(interface, "wl_output") == 0) {
      wl_output* the_output = (wl_output*) wl_registry_bind(registry, name, &wl_output_interface, 2);
      static const struct wl_output_listener _output_listener = {
        outputGeometryCb,
            outputModeCb,
            outputDoneCb,
            outputScaleCb
      };
      wl_output_add_listener(the_output, &_output_listener, self);
    }
  }

  static void
  registry_handle_global_remove(void *data, struct wl_registry *registry,
                                uint32_t name)
  {
  }

  const struct wl_registry_listener registry_listener = {
    registry_handle_global,
    registry_handle_global_remove
  };

  static void outputGeometryCb(void *data, struct wl_output *wl_output, int x,
                               int y, int w, int h, int subpixel, const char *make, const char *model,
                               int transform) {
    //VikWindowWayland *self = reinterpret_cast<VikWindowWayland *>(data);
    vik_log_i("%s: %s [%d, %d] %dx%d", make, model, x, y, w, h);
  }

  static void outputModeCb(void *data, struct wl_output *wl_output,
                           unsigned int flags, int w, int h, int refresh) {
    vik_log_i("outputModeCb: %dx%d@%d", w, h, refresh);

    if (w == 2560 && h == 1440) {
    //if (w == 1920 && h == 1200) {
      WindowWayland *self = reinterpret_cast<WindowWayland *>(data);
      vik_log_d("setting wl_output to %p", wl_output);
      self->hmd_output = wl_output;
      self->hmd_refresh = refresh;
      zxdg_toplevel_v6_set_fullscreen(self->xdg_toplevel, self->hmd_output);
      wl_surface_commit(self->surface);
    } else {
      vik_log_d("ignoring wl_output %p", wl_output);
    }
  }

  static void
  outputDoneCb(void *data, struct wl_output *output) {
    vik_log_d("output done %p", output);
  }

  static void
  outputScaleCb(void *data, struct wl_output *output, int scale) {
    vik_log_d("output scale: %d", scale);
  }

  // Return -1 on failure.
  int init(Renderer* r) {
    display = wl_display_connect(NULL);
    if (!display)
      return -1;

    struct wl_registry *registry = wl_display_get_registry(display);
    wl_registry_add_listener(registry, &registry_listener, this);

    /* Round-trip to get globals */
    wl_display_roundtrip(display);

    /* We don't need this anymore */
    wl_registry_destroy(registry);

    surface = wl_compositor_create_surface(compositor);

    vik_log_f_if(!shell, "Compositor is missing unstable zxdg_shell_v6 protocol support");

    xdg_surface = zxdg_shell_v6_get_xdg_surface(shell, surface);

    zxdg_surface_v6_add_listener(xdg_surface, &xdg_surface_listener, this);

    xdg_toplevel = zxdg_surface_v6_get_toplevel(xdg_surface);

    //zxdg_surface_v6_get_popup()

    //zxdg_positioner_v6_set_size();

    vik_log_d("the hmd output is %p", hmd_output);

    zxdg_toplevel_v6_add_listener(xdg_toplevel, &xdg_toplevel_listener, this);

    zxdg_toplevel_v6_set_title(xdg_toplevel, "vkcube");

    //zxdg_surface_v6_set_window_geometry(xdg_surface, 2560, 0, 1920, 1200);

    //zxdg_toplevel_v6_set_maximized(xdg_toplevel);

    wait_for_configure = true;
    wl_surface_commit(surface);

    r->init_vk(VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME);

    if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR(r->physical_device, 0, display))
      vik_log_f("Vulkan not supported on given Wayland surface");

    VkWaylandSurfaceCreateInfoKHR waylandSurfaceInfo = {};
    waylandSurfaceInfo.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
    waylandSurfaceInfo.display = display;
    waylandSurfaceInfo.surface = surface;

    r->swap_chain = new SwapChainVK();
    SwapChainVK *sc = (SwapChainVK*) r->swap_chain;
    vik_log_f_if(sc == NULL, "no swapchain!");
    vkCreateWaylandSurfaceKHR(r->instance, &waylandSurfaceInfo, NULL, &sc->surface);

    sc->choose_surface_format(r->physical_device);
    init_cb();


    sc->create(r->device, r->physical_device, r->width, r->height);
    sc->update_images(r->device);
    r->create_frame_buffers();


    return 0;
  }

  void flush() {
    while (wl_display_prepare_read(display) != 0)
      wl_display_dispatch_pending(display);
    if (wl_display_flush(display) < 0 && errno != EAGAIN) {
      wl_display_cancel_read(display);
      return;
    }

    pollfd fds[] = {{ wl_display_get_fd(display), POLLIN },};
    if (poll(fds, 1, 0) > 0) {
      wl_display_read_events(display);
      wl_display_dispatch_pending(display);
    } else {
      wl_display_cancel_read(display);
    }
  }

  void iterate(Renderer *r) {
    flush();
    update_cb();
    r->render_swapchain_vk();
  }
};
}