~sircmpwn/xrgears

ref: 04a78fdd25967ed8f104e5c0b1e0589588d6c776 xrgears/vitamin-k/window/vikWindowWayland.hpp -rw-r--r-- 13.4 KiB
04a78fdd — Lubosz Sarnecki overlay: fix validation warnings. 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
 * vitamin-k
 *
 * Copyright 2017-2018 Collabora Ltd.
 *
 * Authors: Lubosz Sarnecki <lubosz.sarnecki@collabora.com>
 * SPDX-License-Identifier: MIT
 */

#pragma once

#include <linux/input.h>
#include <wayland-client.h>

#include <vector>
#include <string>
#include <map>

#include "vikWindow.hpp"

#include "../render/vikSwapChainVKComplex.hpp"

namespace vik {
class WindowWayland : public Window {
 protected:
  wl_display *display = nullptr;
  wl_compositor *compositor = nullptr;
  wl_keyboard *keyboard = nullptr;
  wl_pointer *pointer = nullptr;
  wl_seat *seat = nullptr;
  wl_surface *surface = nullptr;

  SwapChainVK swap_chain;

  explicit WindowWayland(Settings *s) : Window(s) {}

  struct Mode {
    std::pair<int,int> size;
    int refresh;
  };

  struct Display {
    wl_output* output;
    std::string make;
    std::string model;
    std::vector<Mode> modes;
    std::pair<int,int> physical_size_mm;
    std::pair<int,int> position;
  };

  std::vector<Display> displays;


  ~WindowWayland() {
    if (surface)
      wl_surface_destroy(surface);
    if (keyboard)
      wl_keyboard_destroy(keyboard);
    if (pointer)
      wl_pointer_destroy(pointer);
    if (seat)
      wl_seat_destroy(seat);
    if (compositor)
      wl_compositor_destroy(compositor);
    if (display)
      wl_display_disconnect(display);
  }

  void iterate() {
    flush();
    render_frame_cb();
  }

  void init_swap_chain(uint32_t width, uint32_t height) {
    VkResult err = create_surface(swap_chain.instance, &swap_chain.surface);
    vik_log_f_if(err != VK_SUCCESS, "Could not create surface!");
    //swap_chain.set_dimension_cb(dimension_cb);
    //swap_chain.select_queue();
    swap_chain.select_surface_format();
    swap_chain.set_settings(settings);
    swap_chain.create(width, height);
  }

  SwapChain* get_swap_chain() {
    return (SwapChain*) &swap_chain;
  }

  static Input::Key wayland_to_vik_key(uint32_t key) {
    switch (key) {
      case KEY_W:
        return Input::Key::W;
      case KEY_S:
        return Input::Key::S;
      case KEY_A:
        return Input::Key::A;
      case KEY_D:
        return Input::Key::D;
      case KEY_P:
        return Input::Key::P;
      case KEY_F1:
        return Input::Key::F1;
      case KEY_ESC:
        return Input::Key::ESCAPE;
      case KEY_SPACE:
        return Input::Key::SPACE;
      case KEY_KPPLUS:
        return Input::Key::KPPLUS;
      case KEY_KPMINUS:
        return Input::Key::KPMINUS;
      default:
        return Input::Key::UNKNOWN;
    }
  }

  static Input::MouseScrollAxis wayland_to_vik_axis(uint32_t axis) {
    switch (axis) {
      case REL_X:
        return Input::MouseScrollAxis::X;
      case REL_Y:
        return Input::MouseScrollAxis::Y;
      default:
        return Input::MouseScrollAxis::X;
    }
  }

  static Input::MouseButton wayland_to_vik_button(uint32_t button) {
    switch (button) {
      case BTN_LEFT:
        return Input::MouseButton::Left;
      case BTN_MIDDLE:
        return Input::MouseButton::Middle;
      case BTN_RIGHT:
        return Input::MouseButton::Right;
      default:
        return Input::MouseButton::Left;
    }
  }

  const std::vector<const char*> required_extensions() {
    return {VK_KHR_WAYLAND_SURFACE_EXTENSION_NAME };
  }

  // needs initialized vulkan
  VkBool32 check_support(VkPhysicalDevice physical_device) {
    return vkGetPhysicalDeviceWaylandPresentationSupportKHR(
          physical_device, 0, display);
  }

  VkResult create_surface(VkInstance instance, VkSurfaceKHR *vk_surface) {
    VkWaylandSurfaceCreateInfoKHR surface_info = {};
    surface_info.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
    surface_info.display = display;
    surface_info.surface = surface;
    return vkCreateWaylandSurfaceKHR(instance, &surface_info, NULL, vk_surface);
  }

  void seat_capabilities(wl_seat *seat, uint32_t caps) {
    if ((caps & WL_SEAT_CAPABILITY_POINTER) && !pointer) {
      pointer = wl_seat_get_pointer(seat);
      wl_pointer_add_listener(pointer, &pointer_listener, this);
    } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && pointer) {
      wl_pointer_destroy(pointer);
      pointer = nullptr;
    }

    if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !keyboard) {
      keyboard = wl_seat_get_keyboard(seat);
      wl_keyboard_add_listener(keyboard, &keyboard_listener, this);
    } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && keyboard) {
      wl_keyboard_destroy(keyboard);
      keyboard = nullptr;
    }
  }

  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 output_mode(wl_output *output, unsigned int flags,
                   int w, int h, int refresh) {
    Mode m = {};
    m.size = {w, h};
    m.refresh = refresh;

    Display *d = get_display_from_output(output);
    vik_log_f_if(d == nullptr, "Output mode callback before geomentry!");

    d->modes.push_back(m);
  }

  virtual void registry_global(wl_registry *registry, uint32_t name,
                               const char *interface) = 0;

  // listeners
  const wl_registry_listener registry_listener = {
    _registry_global_cb,
    _registry_global_remove_cb
  };

  const wl_seat_listener seat_listener = {
    _seat_capabilities_cb,
  };

  const wl_output_listener output_listener = {
    _output_geometry_cb,
    _output_mode_cb,
    _output_done_cb,
    _output_scale_cb
  };

  const wl_pointer_listener pointer_listener = {
    _pointer_enter_cb,
    _pointer_leave_cb,
    _pointer_motion_cb,
    _pointer_button_cb,
    _pointer_axis_cb,
  };

  const struct wl_keyboard_listener keyboard_listener = {
    .keymap = _keyboard_keymap_cb,
    .enter = _keyboard_enter_cb,
    .leave = _keyboard_leave_cb,
    .key = _keyboard_key_cb,
    .modifiers = _keyboard_modifiers_cb,
    .repeat_info = _keyboard_repeat_cb,
  };

  // callback wrappers
  static void _keyboard_key_cb(void *data, wl_keyboard *keyboard,
                               uint32_t serial, uint32_t time, uint32_t key,
                               uint32_t state) {
    Window *self = reinterpret_cast<Window *>(data);
    self->keyboard_key_cb(wayland_to_vik_key(key), state);
  }

  static void _pointer_motion_cb(void *data, wl_pointer *pointer, uint32_t time,
                                 wl_fixed_t x, wl_fixed_t y) {
    Window *self = reinterpret_cast<Window *>(data);
    self->pointer_motion_cb(wl_fixed_to_double(x), wl_fixed_to_double(y));
  }

  static void _pointer_button_cb(void *data, wl_pointer *pointer,
                                 uint32_t serial, uint32_t time,
                                 uint32_t button, uint32_t state) {
    Window *self = reinterpret_cast<Window*>(data);
    self->pointer_button_cb(wayland_to_vik_button(button), state);
  }

  static void _pointer_axis_cb(void *data, wl_pointer *pointer, uint32_t time,
                               uint32_t axis, wl_fixed_t value) {
    Window *self = reinterpret_cast<Window*>(data);
    self->pointer_axis_cb(wayland_to_vik_axis(axis), wl_fixed_to_double(value));
  }

  static void _registry_global_cb(void *data, wl_registry *registry, uint32_t name,
                                  const char *interface, uint32_t version) {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);
    self->registry_global(registry, name, interface);
  }

  static void _seat_capabilities_cb(void *data, wl_seat *seat, uint32_t caps) {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);
    self->seat_capabilities(seat, caps);
  }

  static void _output_mode_cb(void *data, wl_output *output,
                              unsigned int flags, int w, int h, int refresh) {
    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);
    self->output_mode(output, flags, w, h, refresh);
  }

  static void _output_geometry_cb(void *data, wl_output *output,
                                  int x, int y, int w, int h,
                                  int subpixel,
                                  const char *make, const char *model,
                                  int transform) {
    Display d = {};
    d.output = output;
    d.make = std::string(make);
    d.model = std::string(model);
    d.physical_size_mm = {w, h};
    d.position = {x, y};

    WindowWayland *self = reinterpret_cast<WindowWayland *>(data);
    self->displays.push_back(d);
  }

  Display* get_display_from_output(wl_output* output) {
    for (int i = 0; i < displays.size(); i++) {
      if (displays[i].output == output)
        return &displays[i];
    }
    return nullptr;
  }

  void print_displays() {
    int i_d = 0;
    for (auto d : displays) {
      vik_log_i("%d: %s %s [%d, %d] %dx%dmm (%d Modes)",
                i_d,
                d.make.c_str(),
                d.model.c_str(),
                d.position.first, d.position.second,
                d.physical_size_mm.first, d.physical_size_mm.second,
                d.modes.size());

      int i_m = 0;
      for (auto m : d.modes) {
        vik_log_i("\t%d: %s", i_m, mode_to_string(m).c_str());
        i_m++;
      }
      i_d++;
    }
  }

  Display* current_display() {
    return &displays[settings->display];
  }

  Mode* current_mode() {
    return &current_display()->modes[settings->mode];
  }

  static std::string mode_to_string(const Mode& m) {
    auto size = std::snprintf(nullptr, 0, "%d x %d @ %.2fHz",
                              m.size.first,
                              m.size.second,
                              (float) m.refresh/1000.0);
    std::string output(size + 1, '\0');
    std::sprintf(&output[0], "%d x %d @ %.2fHz", m.size.first,
        m.size.second,
        (float) m.refresh/1000.0);
    return std::string(output);
  }

  virtual void fullscreen() = 0;

  bool is_configured = false;

  void validate_display() {
    Display *d;
    if (settings->display > displays.size()) {
      vik_log_e("Requested display %d, but only %d displays are available.",
                settings->display, displays.size());

      settings->display = 0;
      d = current_display();
      vik_log_e("Selecting '%s %s' instead.",
                d->make.c_str(),
                d->model.c_str());
    }
  }

  void validate_mode() {
    Display* d = current_display();

    if (settings->mode > d->modes.size()) {
      vik_log_e("Requested mode %d, but only %d modes are available on display %d.",
                settings->mode,
                d->modes.size(),
                settings->display);
      settings->mode = 0;
      vik_log_e("Selecting '%s' instead",
                mode_to_string(*current_mode()).c_str());
    }
  }

  bool first_configure = true;
  bool fullscreen_requested = false;

  void configure(int32_t width, int32_t height) {
    if (settings->list_screens_and_exit) {
      print_displays();
      quit_cb();
      return;
    }

    // vik_log_d("configure: %dx%d", width, height);

    if (first_configure) {
      validate_display();
      validate_mode();
      first_configure = false;
    }

    Mode *m = current_mode();
    if (fullscreen_requested &&
        (m->size.first != width || m->size.second != height)) {
      vik_log_w("Received mode %dx%d does not match requested Mode %dx%d. Compositor bug? Requesting again.",
                width, height,
                m->size.first, m->size.second);
      fullscreen_requested = false;
    }

    m = current_mode();
    if (settings->fullscreen && !fullscreen_requested) {
      vik_log_i("Setting full screen on Display %d Mode %s",
                settings->display, mode_to_string(*m).c_str());
      fullscreen();
      fullscreen_requested = true;
      dimension_cb(m->size.first, m->size.second);
    }
  }

  // Unused callbacks

  static void _output_done_cb(void *data, wl_output *output) {}

  static void _output_scale_cb(void *data, wl_output *output, int scale) {}

  static void _registry_global_remove_cb(void *data, wl_registry *registry,
                                         uint32_t name) {}
  static void _keyboard_keymap_cb(void *data, wl_keyboard *keyboard,
                                  uint32_t format, int fd, uint32_t size) {}
  static void _keyboard_modifiers_cb(void *data, wl_keyboard *keyboard,
                                     uint32_t serial, uint32_t mods_depressed,
                                     uint32_t mods_latched, uint32_t mods_locked,
                                     uint32_t group) {}
  static void _keyboard_repeat_cb(void *data, wl_keyboard *keyboard,
                                  int32_t rate, int32_t delay) {}

  static void _keyboard_enter_cb(void *data,
                                 wl_keyboard *keyboard, uint32_t serial,
                                 wl_surface *surface, wl_array *keys) {}
  static void _keyboard_leave_cb(void *data,
                                 wl_keyboard *keyboard, uint32_t serial,
                                 wl_surface *surface) {}
  static void _pointer_enter_cb(void *data, wl_pointer *pointer,
                                uint32_t serial, wl_surface *surface,
                                wl_fixed_t sx, wl_fixed_t sy) {}
  static void _pointer_leave_cb(void *data, wl_pointer *pointer,
                                uint32_t serial, wl_surface *surface) {}
};
}  // namespace vik