medfall

A super great game engine
Log | Files | Refs

wl_init.cc (25648B)


      1 //========================================================================
      2 // GLFW 3.3 Wayland - www.glfw.org
      3 //------------------------------------------------------------------------
      4 // Copyright (c) 2014 Jonas Ã…dahl <jadahl@gmail.com>
      5 //
      6 // This software is provided 'as-is', without any express or implied
      7 // warranty. In no event will the authors be held liable for any damages
      8 // arising from the use of this software.
      9 //
     10 // Permission is granted to anyone to use this software for any purpose,
     11 // including commercial applications, and to alter it and redistribute it
     12 // freely, subject to the following restrictions:
     13 //
     14 // 1. The origin of this software must not be misrepresented; you must not
     15 //    claim that you wrote the original software. If you use this software
     16 //    in a product, an acknowledgment in the product documentation would
     17 //    be appreciated but is not required.
     18 //
     19 // 2. Altered source versions must be plainly marked as such, and must not
     20 //    be misrepresented as being the original software.
     21 //
     22 // 3. This notice may not be removed or altered from any source
     23 //    distribution.
     24 //
     25 //========================================================================
     26 
     27 #include "internal.h"
     28 
     29 #include <linux/input.h>
     30 #include <stdio.h>
     31 #include <stdlib.h>
     32 #include <string.h>
     33 #include <sys/mman.h>
     34 #include <unistd.h>
     35 #include <wayland-client.h>
     36 #include <wayland-cursor.h>
     37 
     38 
     39 static inline int min(int n1, int n2)
     40 {
     41     return n1 < n2 ? n1 : n2;
     42 }
     43 
     44 static void pointerHandleEnter(void* data,
     45                                struct wl_pointer* pointer,
     46                                uint32_t serial,
     47                                struct wl_surface* surface,
     48                                wl_fixed_t sx,
     49                                wl_fixed_t sy)
     50 {
     51     _GLFWwindow* window = wl_surface_get_user_data(surface);
     52 
     53     _glfw.wl.pointerSerial = serial;
     54     _glfw.wl.pointerFocus = window;
     55 
     56     _glfwPlatformSetCursor(window, window->wl.currentCursor);
     57     _glfwInputCursorEnter(window, GLFW_TRUE);
     58 }
     59 
     60 static void pointerHandleLeave(void* data,
     61                                struct wl_pointer* pointer,
     62                                uint32_t serial,
     63                                struct wl_surface* surface)
     64 {
     65     _GLFWwindow* window = _glfw.wl.pointerFocus;
     66 
     67     if (!window)
     68         return;
     69 
     70     _glfw.wl.pointerSerial = serial;
     71     _glfw.wl.pointerFocus = NULL;
     72     _glfwInputCursorEnter(window, GLFW_FALSE);
     73 }
     74 
     75 static void pointerHandleMotion(void* data,
     76                                 struct wl_pointer* pointer,
     77                                 uint32_t time,
     78                                 wl_fixed_t sx,
     79                                 wl_fixed_t sy)
     80 {
     81     _GLFWwindow* window = _glfw.wl.pointerFocus;
     82 
     83     if (!window)
     84         return;
     85 
     86     if (window->cursorMode == GLFW_CURSOR_DISABLED)
     87         return;
     88     else
     89     {
     90         window->wl.cursorPosX = wl_fixed_to_double(sx);
     91         window->wl.cursorPosY = wl_fixed_to_double(sy);
     92     }
     93 
     94     _glfwInputCursorPos(window,
     95                         wl_fixed_to_double(sx),
     96                         wl_fixed_to_double(sy));
     97 }
     98 
     99 static void pointerHandleButton(void* data,
    100                                 struct wl_pointer* wl_pointer,
    101                                 uint32_t serial,
    102                                 uint32_t time,
    103                                 uint32_t button,
    104                                 uint32_t state)
    105 {
    106     _GLFWwindow* window = _glfw.wl.pointerFocus;
    107     int glfwButton;
    108 
    109     if (!window)
    110         return;
    111 
    112     _glfw.wl.pointerSerial = serial;
    113 
    114     /* Makes left, right and middle 0, 1 and 2. Overall order follows evdev
    115      * codes. */
    116     glfwButton = button - BTN_LEFT;
    117 
    118     _glfwInputMouseClick(window,
    119                          glfwButton,
    120                          state == WL_POINTER_BUTTON_STATE_PRESSED
    121                                 ? GLFW_PRESS
    122                                 : GLFW_RELEASE,
    123                          _glfw.wl.xkb.modifiers);
    124 }
    125 
    126 static void pointerHandleAxis(void* data,
    127                               struct wl_pointer* wl_pointer,
    128                               uint32_t time,
    129                               uint32_t axis,
    130                               wl_fixed_t value)
    131 {
    132     _GLFWwindow* window = _glfw.wl.pointerFocus;
    133     double scrollFactor;
    134     double x, y;
    135 
    136     if (!window)
    137         return;
    138 
    139     /* Wayland scroll events are in pointer motion coordinate space (think
    140      * two finger scroll). The factor 10 is commonly used to convert to
    141      * "scroll step means 1.0. */
    142     scrollFactor = 1.0/10.0;
    143 
    144     switch (axis)
    145     {
    146         case WL_POINTER_AXIS_HORIZONTAL_SCROLL:
    147             x = wl_fixed_to_double(value) * scrollFactor;
    148             y = 0.0;
    149             break;
    150         case WL_POINTER_AXIS_VERTICAL_SCROLL:
    151             x = 0.0;
    152             y = wl_fixed_to_double(value) * scrollFactor;
    153             break;
    154         default:
    155             break;
    156     }
    157 
    158     _glfwInputScroll(window, x, y);
    159 }
    160 
    161 static const struct wl_pointer_listener pointerListener = {
    162     pointerHandleEnter,
    163     pointerHandleLeave,
    164     pointerHandleMotion,
    165     pointerHandleButton,
    166     pointerHandleAxis,
    167 };
    168 
    169 static void keyboardHandleKeymap(void* data,
    170                                  struct wl_keyboard* keyboard,
    171                                  uint32_t format,
    172                                  int fd,
    173                                  uint32_t size)
    174 {
    175     struct xkb_keymap* keymap;
    176     struct xkb_state* state;
    177     struct xkb_compose_table* composeTable;
    178     struct xkb_compose_state* composeState;
    179     char* mapStr;
    180     const char* locale;
    181 
    182     if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1)
    183     {
    184         close(fd);
    185         return;
    186     }
    187 
    188     mapStr = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0);
    189     if (mapStr == MAP_FAILED) {
    190         close(fd);
    191         return;
    192     }
    193 
    194     keymap = xkb_keymap_new_from_string(_glfw.wl.xkb.context,
    195                                         mapStr,
    196                                         XKB_KEYMAP_FORMAT_TEXT_V1,
    197                                         0);
    198     munmap(mapStr, size);
    199     close(fd);
    200 
    201     if (!keymap)
    202     {
    203         _glfwInputError(GLFW_PLATFORM_ERROR,
    204                         "Wayland: Failed to compile keymap");
    205         return;
    206     }
    207 
    208     state = xkb_state_new(keymap);
    209     if (!state)
    210     {
    211         _glfwInputError(GLFW_PLATFORM_ERROR,
    212                         "Wayland: Failed to create XKB state");
    213         xkb_keymap_unref(keymap);
    214         return;
    215     }
    216 
    217     // Look up the preferred locale, falling back to "C" as default.
    218     locale = getenv("LC_ALL");
    219     if (!locale)
    220         locale = getenv("LC_CTYPE");
    221     if (!locale)
    222         locale = getenv("LANG");
    223     if (!locale)
    224         locale = "C";
    225 
    226     composeTable =
    227         xkb_compose_table_new_from_locale(_glfw.wl.xkb.context, locale,
    228                                           XKB_COMPOSE_COMPILE_NO_FLAGS);
    229     if (composeTable)
    230     {
    231         composeState =
    232             xkb_compose_state_new(composeTable, XKB_COMPOSE_STATE_NO_FLAGS);
    233         xkb_compose_table_unref(composeTable);
    234         if (composeState)
    235             _glfw.wl.xkb.composeState = composeState;
    236         else
    237             _glfwInputError(GLFW_PLATFORM_ERROR,
    238                             "Wayland: Failed to create XKB compose state");
    239     }
    240     else
    241     {
    242         _glfwInputError(GLFW_PLATFORM_ERROR,
    243                         "Wayland: Failed to create XKB compose table");
    244     }
    245 
    246     xkb_keymap_unref(_glfw.wl.xkb.keymap);
    247     xkb_state_unref(_glfw.wl.xkb.state);
    248     _glfw.wl.xkb.keymap = keymap;
    249     _glfw.wl.xkb.state = state;
    250 
    251     _glfw.wl.xkb.controlMask =
    252         1 << xkb_keymap_mod_get_index(_glfw.wl.xkb.keymap, "Control");
    253     _glfw.wl.xkb.altMask =
    254         1 << xkb_keymap_mod_get_index(_glfw.wl.xkb.keymap, "Mod1");
    255     _glfw.wl.xkb.shiftMask =
    256         1 << xkb_keymap_mod_get_index(_glfw.wl.xkb.keymap, "Shift");
    257     _glfw.wl.xkb.superMask =
    258         1 << xkb_keymap_mod_get_index(_glfw.wl.xkb.keymap, "Mod4");
    259 }
    260 
    261 static void keyboardHandleEnter(void* data,
    262                                 struct wl_keyboard* keyboard,
    263                                 uint32_t serial,
    264                                 struct wl_surface* surface,
    265                                 struct wl_array* keys)
    266 {
    267     _GLFWwindow* window = wl_surface_get_user_data(surface);
    268 
    269     _glfw.wl.keyboardFocus = window;
    270     _glfwInputWindowFocus(window, GLFW_TRUE);
    271 }
    272 
    273 static void keyboardHandleLeave(void* data,
    274                                 struct wl_keyboard* keyboard,
    275                                 uint32_t serial,
    276                                 struct wl_surface* surface)
    277 {
    278     _GLFWwindow* window = _glfw.wl.keyboardFocus;
    279 
    280     if (!window)
    281         return;
    282 
    283     _glfw.wl.keyboardFocus = NULL;
    284     _glfwInputWindowFocus(window, GLFW_FALSE);
    285 }
    286 
    287 static int toGLFWKeyCode(uint32_t key)
    288 {
    289     if (key < sizeof(_glfw.wl.keycodes) / sizeof(_glfw.wl.keycodes[0]))
    290         return _glfw.wl.keycodes[key];
    291 
    292     return GLFW_KEY_UNKNOWN;
    293 }
    294 
    295 static xkb_keysym_t composeSymbol(xkb_keysym_t sym)
    296 {
    297     if (sym == XKB_KEY_NoSymbol || !_glfw.wl.xkb.composeState)
    298         return sym;
    299     if (xkb_compose_state_feed(_glfw.wl.xkb.composeState, sym)
    300             != XKB_COMPOSE_FEED_ACCEPTED)
    301         return sym;
    302     switch (xkb_compose_state_get_status(_glfw.wl.xkb.composeState))
    303     {
    304         case XKB_COMPOSE_COMPOSED:
    305             return xkb_compose_state_get_one_sym(_glfw.wl.xkb.composeState);
    306         case XKB_COMPOSE_COMPOSING:
    307         case XKB_COMPOSE_CANCELLED:
    308             return XKB_KEY_NoSymbol;
    309         case XKB_COMPOSE_NOTHING:
    310         default:
    311             return sym;
    312     }
    313 }
    314 
    315 static void inputChar(_GLFWwindow* window, uint32_t key)
    316 {
    317     uint32_t code, numSyms;
    318     long cp;
    319     const xkb_keysym_t *syms;
    320     xkb_keysym_t sym;
    321 
    322     code = key + 8;
    323     numSyms = xkb_state_key_get_syms(_glfw.wl.xkb.state, code, &syms);
    324 
    325     if (numSyms == 1)
    326     {
    327         sym = composeSymbol(syms[0]);
    328         cp = _glfwKeySym2Unicode(sym);
    329         if (cp != -1)
    330         {
    331             const int mods = _glfw.wl.xkb.modifiers;
    332             const int plain = !(mods & (GLFW_MOD_CONTROL | GLFW_MOD_ALT));
    333             _glfwInputChar(window, cp, mods, plain);
    334         }
    335     }
    336 }
    337 
    338 static void keyboardHandleKey(void* data,
    339                               struct wl_keyboard* keyboard,
    340                               uint32_t serial,
    341                               uint32_t time,
    342                               uint32_t key,
    343                               uint32_t state)
    344 {
    345     int keyCode;
    346     int action;
    347     _GLFWwindow* window = _glfw.wl.keyboardFocus;
    348 
    349     if (!window)
    350         return;
    351 
    352     keyCode = toGLFWKeyCode(key);
    353     action = state == WL_KEYBOARD_KEY_STATE_PRESSED
    354             ? GLFW_PRESS : GLFW_RELEASE;
    355 
    356     _glfwInputKey(window, keyCode, key, action,
    357                   _glfw.wl.xkb.modifiers);
    358 
    359     if (action == GLFW_PRESS)
    360         inputChar(window, key);
    361 }
    362 
    363 static void keyboardHandleModifiers(void* data,
    364                                     struct wl_keyboard* keyboard,
    365                                     uint32_t serial,
    366                                     uint32_t modsDepressed,
    367                                     uint32_t modsLatched,
    368                                     uint32_t modsLocked,
    369                                     uint32_t group)
    370 {
    371     xkb_mod_mask_t mask;
    372     unsigned int modifiers = 0;
    373 
    374     if (!_glfw.wl.xkb.keymap)
    375         return;
    376 
    377     xkb_state_update_mask(_glfw.wl.xkb.state,
    378                           modsDepressed,
    379                           modsLatched,
    380                           modsLocked,
    381                           0,
    382                           0,
    383                           group);
    384 
    385     mask = xkb_state_serialize_mods(_glfw.wl.xkb.state,
    386                                     XKB_STATE_MODS_DEPRESSED |
    387                                     XKB_STATE_LAYOUT_DEPRESSED |
    388                                     XKB_STATE_MODS_LATCHED |
    389                                     XKB_STATE_LAYOUT_LATCHED);
    390     if (mask & _glfw.wl.xkb.controlMask)
    391         modifiers |= GLFW_MOD_CONTROL;
    392     if (mask & _glfw.wl.xkb.altMask)
    393         modifiers |= GLFW_MOD_ALT;
    394     if (mask & _glfw.wl.xkb.shiftMask)
    395         modifiers |= GLFW_MOD_SHIFT;
    396     if (mask & _glfw.wl.xkb.superMask)
    397         modifiers |= GLFW_MOD_SUPER;
    398     _glfw.wl.xkb.modifiers = modifiers;
    399 }
    400 
    401 static const struct wl_keyboard_listener keyboardListener = {
    402     keyboardHandleKeymap,
    403     keyboardHandleEnter,
    404     keyboardHandleLeave,
    405     keyboardHandleKey,
    406     keyboardHandleModifiers,
    407 };
    408 
    409 static void seatHandleCapabilities(void* data,
    410                                    struct wl_seat* seat,
    411                                    enum wl_seat_capability caps)
    412 {
    413     if ((caps & WL_SEAT_CAPABILITY_POINTER) && !_glfw.wl.pointer)
    414     {
    415         _glfw.wl.pointer = wl_seat_get_pointer(seat);
    416         wl_pointer_add_listener(_glfw.wl.pointer, &pointerListener, NULL);
    417     }
    418     else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && _glfw.wl.pointer)
    419     {
    420         wl_pointer_destroy(_glfw.wl.pointer);
    421         _glfw.wl.pointer = NULL;
    422     }
    423 
    424     if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !_glfw.wl.keyboard)
    425     {
    426         _glfw.wl.keyboard = wl_seat_get_keyboard(seat);
    427         wl_keyboard_add_listener(_glfw.wl.keyboard, &keyboardListener, NULL);
    428     }
    429     else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && _glfw.wl.keyboard)
    430     {
    431         wl_keyboard_destroy(_glfw.wl.keyboard);
    432         _glfw.wl.keyboard = NULL;
    433     }
    434 }
    435 
    436 static const struct wl_seat_listener seatListener = {
    437     seatHandleCapabilities
    438 };
    439 
    440 static void registryHandleGlobal(void* data,
    441                                  struct wl_registry* registry,
    442                                  uint32_t name,
    443                                  const char* interface,
    444                                  uint32_t version)
    445 {
    446     if (strcmp(interface, "wl_compositor") == 0)
    447     {
    448         _glfw.wl.compositorVersion = min(3, version);
    449         _glfw.wl.compositor =
    450             wl_registry_bind(registry, name, &wl_compositor_interface,
    451                              _glfw.wl.compositorVersion);
    452     }
    453     else if (strcmp(interface, "wl_shm") == 0)
    454     {
    455         _glfw.wl.shm =
    456             wl_registry_bind(registry, name, &wl_shm_interface, 1);
    457     }
    458     else if (strcmp(interface, "wl_shell") == 0)
    459     {
    460         _glfw.wl.shell =
    461             wl_registry_bind(registry, name, &wl_shell_interface, 1);
    462     }
    463     else if (strcmp(interface, "wl_output") == 0)
    464     {
    465         _glfwAddOutputWayland(name, version);
    466     }
    467     else if (strcmp(interface, "wl_seat") == 0)
    468     {
    469         if (!_glfw.wl.seat)
    470         {
    471             _glfw.wl.seat =
    472                 wl_registry_bind(registry, name, &wl_seat_interface, 1);
    473             wl_seat_add_listener(_glfw.wl.seat, &seatListener, NULL);
    474         }
    475     }
    476     else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0)
    477     {
    478         _glfw.wl.relativePointerManager =
    479             wl_registry_bind(registry, name,
    480                              &zwp_relative_pointer_manager_v1_interface,
    481                              1);
    482     }
    483     else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0)
    484     {
    485         _glfw.wl.pointerConstraints =
    486             wl_registry_bind(registry, name,
    487                              &zwp_pointer_constraints_v1_interface,
    488                              1);
    489     }
    490 }
    491 
    492 static void registryHandleGlobalRemove(void *data,
    493                                        struct wl_registry *registry,
    494                                        uint32_t name)
    495 {
    496 }
    497 
    498 
    499 static const struct wl_registry_listener registryListener = {
    500     registryHandleGlobal,
    501     registryHandleGlobalRemove
    502 };
    503 
    504 // Create key code translation tables
    505 //
    506 static void createKeyTables(void)
    507 {
    508     int scancode;
    509 
    510     memset(_glfw.wl.keycodes, -1, sizeof(_glfw.wl.keycodes));
    511     memset(_glfw.wl.scancodes, -1, sizeof(_glfw.wl.scancodes));
    512 
    513     _glfw.wl.keycodes[KEY_GRAVE]      = GLFW_KEY_GRAVE_ACCENT;
    514     _glfw.wl.keycodes[KEY_1]          = GLFW_KEY_1;
    515     _glfw.wl.keycodes[KEY_2]          = GLFW_KEY_2;
    516     _glfw.wl.keycodes[KEY_3]          = GLFW_KEY_3;
    517     _glfw.wl.keycodes[KEY_4]          = GLFW_KEY_4;
    518     _glfw.wl.keycodes[KEY_5]          = GLFW_KEY_5;
    519     _glfw.wl.keycodes[KEY_6]          = GLFW_KEY_6;
    520     _glfw.wl.keycodes[KEY_7]          = GLFW_KEY_7;
    521     _glfw.wl.keycodes[KEY_8]          = GLFW_KEY_8;
    522     _glfw.wl.keycodes[KEY_9]          = GLFW_KEY_9;
    523     _glfw.wl.keycodes[KEY_0]          = GLFW_KEY_0;
    524     _glfw.wl.keycodes[KEY_SPACE]      = GLFW_KEY_SPACE;
    525     _glfw.wl.keycodes[KEY_MINUS]      = GLFW_KEY_MINUS;
    526     _glfw.wl.keycodes[KEY_EQUAL]      = GLFW_KEY_EQUAL;
    527     _glfw.wl.keycodes[KEY_Q]          = GLFW_KEY_Q;
    528     _glfw.wl.keycodes[KEY_W]          = GLFW_KEY_W;
    529     _glfw.wl.keycodes[KEY_E]          = GLFW_KEY_E;
    530     _glfw.wl.keycodes[KEY_R]          = GLFW_KEY_R;
    531     _glfw.wl.keycodes[KEY_T]          = GLFW_KEY_T;
    532     _glfw.wl.keycodes[KEY_Y]          = GLFW_KEY_Y;
    533     _glfw.wl.keycodes[KEY_U]          = GLFW_KEY_U;
    534     _glfw.wl.keycodes[KEY_I]          = GLFW_KEY_I;
    535     _glfw.wl.keycodes[KEY_O]          = GLFW_KEY_O;
    536     _glfw.wl.keycodes[KEY_P]          = GLFW_KEY_P;
    537     _glfw.wl.keycodes[KEY_LEFTBRACE]  = GLFW_KEY_LEFT_BRACKET;
    538     _glfw.wl.keycodes[KEY_RIGHTBRACE] = GLFW_KEY_RIGHT_BRACKET;
    539     _glfw.wl.keycodes[KEY_A]          = GLFW_KEY_A;
    540     _glfw.wl.keycodes[KEY_S]          = GLFW_KEY_S;
    541     _glfw.wl.keycodes[KEY_D]          = GLFW_KEY_D;
    542     _glfw.wl.keycodes[KEY_F]          = GLFW_KEY_F;
    543     _glfw.wl.keycodes[KEY_G]          = GLFW_KEY_G;
    544     _glfw.wl.keycodes[KEY_H]          = GLFW_KEY_H;
    545     _glfw.wl.keycodes[KEY_J]          = GLFW_KEY_J;
    546     _glfw.wl.keycodes[KEY_K]          = GLFW_KEY_K;
    547     _glfw.wl.keycodes[KEY_L]          = GLFW_KEY_L;
    548     _glfw.wl.keycodes[KEY_SEMICOLON]  = GLFW_KEY_SEMICOLON;
    549     _glfw.wl.keycodes[KEY_APOSTROPHE] = GLFW_KEY_APOSTROPHE;
    550     _glfw.wl.keycodes[KEY_Z]          = GLFW_KEY_Z;
    551     _glfw.wl.keycodes[KEY_X]          = GLFW_KEY_X;
    552     _glfw.wl.keycodes[KEY_C]          = GLFW_KEY_C;
    553     _glfw.wl.keycodes[KEY_V]          = GLFW_KEY_V;
    554     _glfw.wl.keycodes[KEY_B]          = GLFW_KEY_B;
    555     _glfw.wl.keycodes[KEY_N]          = GLFW_KEY_N;
    556     _glfw.wl.keycodes[KEY_M]          = GLFW_KEY_M;
    557     _glfw.wl.keycodes[KEY_COMMA]      = GLFW_KEY_COMMA;
    558     _glfw.wl.keycodes[KEY_DOT]        = GLFW_KEY_PERIOD;
    559     _glfw.wl.keycodes[KEY_SLASH]      = GLFW_KEY_SLASH;
    560     _glfw.wl.keycodes[KEY_BACKSLASH]  = GLFW_KEY_BACKSLASH;
    561     _glfw.wl.keycodes[KEY_ESC]        = GLFW_KEY_ESCAPE;
    562     _glfw.wl.keycodes[KEY_TAB]        = GLFW_KEY_TAB;
    563     _glfw.wl.keycodes[KEY_LEFTSHIFT]  = GLFW_KEY_LEFT_SHIFT;
    564     _glfw.wl.keycodes[KEY_RIGHTSHIFT] = GLFW_KEY_RIGHT_SHIFT;
    565     _glfw.wl.keycodes[KEY_LEFTCTRL]   = GLFW_KEY_LEFT_CONTROL;
    566     _glfw.wl.keycodes[KEY_RIGHTCTRL]  = GLFW_KEY_RIGHT_CONTROL;
    567     _glfw.wl.keycodes[KEY_LEFTALT]    = GLFW_KEY_LEFT_ALT;
    568     _glfw.wl.keycodes[KEY_RIGHTALT]   = GLFW_KEY_RIGHT_ALT;
    569     _glfw.wl.keycodes[KEY_LEFTMETA]   = GLFW_KEY_LEFT_SUPER;
    570     _glfw.wl.keycodes[KEY_RIGHTMETA]  = GLFW_KEY_RIGHT_SUPER;
    571     _glfw.wl.keycodes[KEY_MENU]       = GLFW_KEY_MENU;
    572     _glfw.wl.keycodes[KEY_NUMLOCK]    = GLFW_KEY_NUM_LOCK;
    573     _glfw.wl.keycodes[KEY_CAPSLOCK]   = GLFW_KEY_CAPS_LOCK;
    574     _glfw.wl.keycodes[KEY_PRINT]      = GLFW_KEY_PRINT_SCREEN;
    575     _glfw.wl.keycodes[KEY_SCROLLLOCK] = GLFW_KEY_SCROLL_LOCK;
    576     _glfw.wl.keycodes[KEY_PAUSE]      = GLFW_KEY_PAUSE;
    577     _glfw.wl.keycodes[KEY_DELETE]     = GLFW_KEY_DELETE;
    578     _glfw.wl.keycodes[KEY_BACKSPACE]  = GLFW_KEY_BACKSPACE;
    579     _glfw.wl.keycodes[KEY_ENTER]      = GLFW_KEY_ENTER;
    580     _glfw.wl.keycodes[KEY_HOME]       = GLFW_KEY_HOME;
    581     _glfw.wl.keycodes[KEY_END]        = GLFW_KEY_END;
    582     _glfw.wl.keycodes[KEY_PAGEUP]     = GLFW_KEY_PAGE_UP;
    583     _glfw.wl.keycodes[KEY_PAGEDOWN]   = GLFW_KEY_PAGE_DOWN;
    584     _glfw.wl.keycodes[KEY_INSERT]     = GLFW_KEY_INSERT;
    585     _glfw.wl.keycodes[KEY_LEFT]       = GLFW_KEY_LEFT;
    586     _glfw.wl.keycodes[KEY_RIGHT]      = GLFW_KEY_RIGHT;
    587     _glfw.wl.keycodes[KEY_DOWN]       = GLFW_KEY_DOWN;
    588     _glfw.wl.keycodes[KEY_UP]         = GLFW_KEY_UP;
    589     _glfw.wl.keycodes[KEY_F1]         = GLFW_KEY_F1;
    590     _glfw.wl.keycodes[KEY_F2]         = GLFW_KEY_F2;
    591     _glfw.wl.keycodes[KEY_F3]         = GLFW_KEY_F3;
    592     _glfw.wl.keycodes[KEY_F4]         = GLFW_KEY_F4;
    593     _glfw.wl.keycodes[KEY_F5]         = GLFW_KEY_F5;
    594     _glfw.wl.keycodes[KEY_F6]         = GLFW_KEY_F6;
    595     _glfw.wl.keycodes[KEY_F7]         = GLFW_KEY_F7;
    596     _glfw.wl.keycodes[KEY_F8]         = GLFW_KEY_F8;
    597     _glfw.wl.keycodes[KEY_F9]         = GLFW_KEY_F9;
    598     _glfw.wl.keycodes[KEY_F10]        = GLFW_KEY_F10;
    599     _glfw.wl.keycodes[KEY_F11]        = GLFW_KEY_F11;
    600     _glfw.wl.keycodes[KEY_F12]        = GLFW_KEY_F12;
    601     _glfw.wl.keycodes[KEY_F13]        = GLFW_KEY_F13;
    602     _glfw.wl.keycodes[KEY_F14]        = GLFW_KEY_F14;
    603     _glfw.wl.keycodes[KEY_F15]        = GLFW_KEY_F15;
    604     _glfw.wl.keycodes[KEY_F16]        = GLFW_KEY_F16;
    605     _glfw.wl.keycodes[KEY_F17]        = GLFW_KEY_F17;
    606     _glfw.wl.keycodes[KEY_F18]        = GLFW_KEY_F18;
    607     _glfw.wl.keycodes[KEY_F19]        = GLFW_KEY_F19;
    608     _glfw.wl.keycodes[KEY_F20]        = GLFW_KEY_F20;
    609     _glfw.wl.keycodes[KEY_F21]        = GLFW_KEY_F21;
    610     _glfw.wl.keycodes[KEY_F22]        = GLFW_KEY_F22;
    611     _glfw.wl.keycodes[KEY_F23]        = GLFW_KEY_F23;
    612     _glfw.wl.keycodes[KEY_F24]        = GLFW_KEY_F24;
    613     _glfw.wl.keycodes[KEY_KPSLASH]    = GLFW_KEY_KP_DIVIDE;
    614     _glfw.wl.keycodes[KEY_KPDOT]      = GLFW_KEY_KP_MULTIPLY;
    615     _glfw.wl.keycodes[KEY_KPMINUS]    = GLFW_KEY_KP_SUBTRACT;
    616     _glfw.wl.keycodes[KEY_KPPLUS]     = GLFW_KEY_KP_ADD;
    617     _glfw.wl.keycodes[KEY_KP0]        = GLFW_KEY_KP_0;
    618     _glfw.wl.keycodes[KEY_KP1]        = GLFW_KEY_KP_1;
    619     _glfw.wl.keycodes[KEY_KP2]        = GLFW_KEY_KP_2;
    620     _glfw.wl.keycodes[KEY_KP3]        = GLFW_KEY_KP_3;
    621     _glfw.wl.keycodes[KEY_KP4]        = GLFW_KEY_KP_4;
    622     _glfw.wl.keycodes[KEY_KP5]        = GLFW_KEY_KP_5;
    623     _glfw.wl.keycodes[KEY_KP6]        = GLFW_KEY_KP_6;
    624     _glfw.wl.keycodes[KEY_KP7]        = GLFW_KEY_KP_7;
    625     _glfw.wl.keycodes[KEY_KP8]        = GLFW_KEY_KP_8;
    626     _glfw.wl.keycodes[KEY_KP9]        = GLFW_KEY_KP_9;
    627     _glfw.wl.keycodes[KEY_KPCOMMA]    = GLFW_KEY_KP_DECIMAL;
    628     _glfw.wl.keycodes[KEY_KPEQUAL]    = GLFW_KEY_KP_EQUAL;
    629     _glfw.wl.keycodes[KEY_KPENTER]    = GLFW_KEY_KP_ENTER;
    630 
    631     for (scancode = 0;  scancode < 256;  scancode++)
    632     {
    633         if (_glfw.wl.keycodes[scancode] > 0)
    634             _glfw.wl.scancodes[_glfw.wl.keycodes[scancode]] = scancode;
    635     }
    636 }
    637 
    638 
    639 //////////////////////////////////////////////////////////////////////////
    640 //////                       GLFW platform API                      //////
    641 //////////////////////////////////////////////////////////////////////////
    642 
    643 int _glfwPlatformInit(void)
    644 {
    645     _glfw.wl.display = wl_display_connect(NULL);
    646     if (!_glfw.wl.display)
    647     {
    648         _glfwInputError(GLFW_PLATFORM_ERROR,
    649                         "Wayland: Failed to connect to display");
    650         return GLFW_FALSE;
    651     }
    652 
    653     _glfw.wl.registry = wl_display_get_registry(_glfw.wl.display);
    654     wl_registry_add_listener(_glfw.wl.registry, &registryListener, NULL);
    655 
    656     createKeyTables();
    657 
    658     _glfw.wl.xkb.context = xkb_context_new(0);
    659     if (!_glfw.wl.xkb.context)
    660     {
    661         _glfwInputError(GLFW_PLATFORM_ERROR,
    662                         "Wayland: Failed to initialize xkb context");
    663         return GLFW_FALSE;
    664     }
    665 
    666     // Sync so we got all registry objects
    667     wl_display_roundtrip(_glfw.wl.display);
    668 
    669     // Sync so we got all initial output events
    670     wl_display_roundtrip(_glfw.wl.display);
    671 
    672     if (!_glfwInitJoysticksLinux())
    673         return GLFW_FALSE;
    674 
    675     _glfwInitTimerPOSIX();
    676 
    677     if (_glfw.wl.pointer && _glfw.wl.shm)
    678     {
    679         _glfw.wl.cursorTheme = wl_cursor_theme_load(NULL, 32, _glfw.wl.shm);
    680         if (!_glfw.wl.cursorTheme)
    681         {
    682             _glfwInputError(GLFW_PLATFORM_ERROR,
    683                             "Wayland: Unable to load default cursor theme\n");
    684             return GLFW_FALSE;
    685         }
    686         _glfw.wl.cursorSurface =
    687             wl_compositor_create_surface(_glfw.wl.compositor);
    688     }
    689 
    690     return GLFW_TRUE;
    691 }
    692 
    693 void _glfwPlatformTerminate(void)
    694 {
    695     _glfwTerminateEGL();
    696     _glfwTerminateJoysticksLinux();
    697 
    698     xkb_compose_state_unref(_glfw.wl.xkb.composeState);
    699     xkb_keymap_unref(_glfw.wl.xkb.keymap);
    700     xkb_state_unref(_glfw.wl.xkb.state);
    701     xkb_context_unref(_glfw.wl.xkb.context);
    702 
    703     if (_glfw.wl.cursorTheme)
    704         wl_cursor_theme_destroy(_glfw.wl.cursorTheme);
    705     if (_glfw.wl.cursorSurface)
    706         wl_surface_destroy(_glfw.wl.cursorSurface);
    707     if (_glfw.wl.compositor)
    708         wl_compositor_destroy(_glfw.wl.compositor);
    709     if (_glfw.wl.shm)
    710         wl_shm_destroy(_glfw.wl.shm);
    711     if (_glfw.wl.shell)
    712         wl_shell_destroy(_glfw.wl.shell);
    713     if (_glfw.wl.pointer)
    714         wl_pointer_destroy(_glfw.wl.pointer);
    715     if (_glfw.wl.keyboard)
    716         wl_keyboard_destroy(_glfw.wl.keyboard);
    717     if (_glfw.wl.seat)
    718         wl_seat_destroy(_glfw.wl.seat);
    719     if (_glfw.wl.relativePointerManager)
    720         zwp_relative_pointer_manager_v1_destroy(_glfw.wl.relativePointerManager);
    721     if (_glfw.wl.pointerConstraints)
    722         zwp_pointer_constraints_v1_destroy(_glfw.wl.pointerConstraints);
    723     if (_glfw.wl.registry)
    724         wl_registry_destroy(_glfw.wl.registry);
    725     if (_glfw.wl.display)
    726     {
    727         wl_display_flush(_glfw.wl.display);
    728         wl_display_disconnect(_glfw.wl.display);
    729     }
    730 }
    731 
    732 const char* _glfwPlatformGetVersionString(void)
    733 {
    734     return _GLFW_VERSION_NUMBER " Wayland EGL"
    735 #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK)
    736         " clock_gettime"
    737 #else
    738         " gettimeofday"
    739 #endif
    740         " evdev"
    741 #if defined(_GLFW_BUILD_DLL)
    742         " shared"
    743 #endif
    744         ;
    745 }
    746