wl_init.c (23736B)
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 // It is fine to use C99 in this file because it will not be built with VS 27 //======================================================================== 28 29 #define _POSIX_C_SOURCE 200809L 30 31 #include "internal.h" 32 33 #include <errno.h> 34 #include <limits.h> 35 #include <linux/input.h> 36 #include <stdio.h> 37 #include <stdlib.h> 38 #include <string.h> 39 #include <sys/mman.h> 40 #include <sys/timerfd.h> 41 #include <unistd.h> 42 #include <wayland-client.h> 43 44 static void wmBaseHandlePing(void* userData, 45 struct xdg_wm_base* wmBase, 46 uint32_t serial) 47 { 48 xdg_wm_base_pong(wmBase, serial); 49 } 50 51 static const struct xdg_wm_base_listener wmBaseListener = 52 { 53 wmBaseHandlePing 54 }; 55 56 static void registryHandleGlobal(void* userData, 57 struct wl_registry* registry, 58 uint32_t name, 59 const char* interface, 60 uint32_t version) 61 { 62 if (strcmp(interface, "wl_compositor") == 0) 63 { 64 _glfw.wl.compositorVersion = _glfw_min(3, version); 65 _glfw.wl.compositor = 66 wl_registry_bind(registry, name, &wl_compositor_interface, 67 _glfw.wl.compositorVersion); 68 } 69 else if (strcmp(interface, "wl_subcompositor") == 0) 70 { 71 _glfw.wl.subcompositor = 72 wl_registry_bind(registry, name, &wl_subcompositor_interface, 1); 73 } 74 else if (strcmp(interface, "wl_shm") == 0) 75 { 76 _glfw.wl.shm = 77 wl_registry_bind(registry, name, &wl_shm_interface, 1); 78 } 79 else if (strcmp(interface, "wl_output") == 0) 80 { 81 _glfwAddOutputWayland(name, version); 82 } 83 else if (strcmp(interface, "wl_seat") == 0) 84 { 85 if (!_glfw.wl.seat) 86 { 87 _glfw.wl.seatVersion = _glfw_min(4, version); 88 _glfw.wl.seat = 89 wl_registry_bind(registry, name, &wl_seat_interface, 90 _glfw.wl.seatVersion); 91 _glfwAddSeatListenerWayland(_glfw.wl.seat); 92 } 93 } 94 else if (strcmp(interface, "wl_data_device_manager") == 0) 95 { 96 if (!_glfw.wl.dataDeviceManager) 97 { 98 _glfw.wl.dataDeviceManager = 99 wl_registry_bind(registry, name, 100 &wl_data_device_manager_interface, 1); 101 } 102 } 103 else if (strcmp(interface, "xdg_wm_base") == 0) 104 { 105 _glfw.wl.wmBase = 106 wl_registry_bind(registry, name, &xdg_wm_base_interface, 1); 107 xdg_wm_base_add_listener(_glfw.wl.wmBase, &wmBaseListener, NULL); 108 } 109 else if (strcmp(interface, "zxdg_decoration_manager_v1") == 0) 110 { 111 _glfw.wl.decorationManager = 112 wl_registry_bind(registry, name, 113 &zxdg_decoration_manager_v1_interface, 114 1); 115 } 116 else if (strcmp(interface, "wp_viewporter") == 0) 117 { 118 _glfw.wl.viewporter = 119 wl_registry_bind(registry, name, &wp_viewporter_interface, 1); 120 } 121 else if (strcmp(interface, "zwp_relative_pointer_manager_v1") == 0) 122 { 123 _glfw.wl.relativePointerManager = 124 wl_registry_bind(registry, name, 125 &zwp_relative_pointer_manager_v1_interface, 126 1); 127 } 128 else if (strcmp(interface, "zwp_pointer_constraints_v1") == 0) 129 { 130 _glfw.wl.pointerConstraints = 131 wl_registry_bind(registry, name, 132 &zwp_pointer_constraints_v1_interface, 133 1); 134 } 135 else if (strcmp(interface, "zwp_idle_inhibit_manager_v1") == 0) 136 { 137 _glfw.wl.idleInhibitManager = 138 wl_registry_bind(registry, name, 139 &zwp_idle_inhibit_manager_v1_interface, 140 1); 141 } 142 } 143 144 static void registryHandleGlobalRemove(void* userData, 145 struct wl_registry* registry, 146 uint32_t name) 147 { 148 int i; 149 150 for (i = 0; i < _glfw.monitorCount; ++i) 151 { 152 _GLFWmonitor* monitor = _glfw.monitors[i]; 153 if (monitor->wl.name == name) 154 { 155 _glfwInputMonitor(monitor, GLFW_DISCONNECTED, 0); 156 return; 157 } 158 } 159 } 160 161 162 static const struct wl_registry_listener registryListener = 163 { 164 registryHandleGlobal, 165 registryHandleGlobalRemove 166 }; 167 168 // Create key code translation tables 169 // 170 static void createKeyTables(void) 171 { 172 int scancode; 173 174 memset(_glfw.wl.keycodes, -1, sizeof(_glfw.wl.keycodes)); 175 memset(_glfw.wl.scancodes, -1, sizeof(_glfw.wl.scancodes)); 176 177 _glfw.wl.keycodes[KEY_GRAVE] = GLFW_KEY_GRAVE_ACCENT; 178 _glfw.wl.keycodes[KEY_1] = GLFW_KEY_1; 179 _glfw.wl.keycodes[KEY_2] = GLFW_KEY_2; 180 _glfw.wl.keycodes[KEY_3] = GLFW_KEY_3; 181 _glfw.wl.keycodes[KEY_4] = GLFW_KEY_4; 182 _glfw.wl.keycodes[KEY_5] = GLFW_KEY_5; 183 _glfw.wl.keycodes[KEY_6] = GLFW_KEY_6; 184 _glfw.wl.keycodes[KEY_7] = GLFW_KEY_7; 185 _glfw.wl.keycodes[KEY_8] = GLFW_KEY_8; 186 _glfw.wl.keycodes[KEY_9] = GLFW_KEY_9; 187 _glfw.wl.keycodes[KEY_0] = GLFW_KEY_0; 188 _glfw.wl.keycodes[KEY_SPACE] = GLFW_KEY_SPACE; 189 _glfw.wl.keycodes[KEY_MINUS] = GLFW_KEY_MINUS; 190 _glfw.wl.keycodes[KEY_EQUAL] = GLFW_KEY_EQUAL; 191 _glfw.wl.keycodes[KEY_Q] = GLFW_KEY_Q; 192 _glfw.wl.keycodes[KEY_W] = GLFW_KEY_W; 193 _glfw.wl.keycodes[KEY_E] = GLFW_KEY_E; 194 _glfw.wl.keycodes[KEY_R] = GLFW_KEY_R; 195 _glfw.wl.keycodes[KEY_T] = GLFW_KEY_T; 196 _glfw.wl.keycodes[KEY_Y] = GLFW_KEY_Y; 197 _glfw.wl.keycodes[KEY_U] = GLFW_KEY_U; 198 _glfw.wl.keycodes[KEY_I] = GLFW_KEY_I; 199 _glfw.wl.keycodes[KEY_O] = GLFW_KEY_O; 200 _glfw.wl.keycodes[KEY_P] = GLFW_KEY_P; 201 _glfw.wl.keycodes[KEY_LEFTBRACE] = GLFW_KEY_LEFT_BRACKET; 202 _glfw.wl.keycodes[KEY_RIGHTBRACE] = GLFW_KEY_RIGHT_BRACKET; 203 _glfw.wl.keycodes[KEY_A] = GLFW_KEY_A; 204 _glfw.wl.keycodes[KEY_S] = GLFW_KEY_S; 205 _glfw.wl.keycodes[KEY_D] = GLFW_KEY_D; 206 _glfw.wl.keycodes[KEY_F] = GLFW_KEY_F; 207 _glfw.wl.keycodes[KEY_G] = GLFW_KEY_G; 208 _glfw.wl.keycodes[KEY_H] = GLFW_KEY_H; 209 _glfw.wl.keycodes[KEY_J] = GLFW_KEY_J; 210 _glfw.wl.keycodes[KEY_K] = GLFW_KEY_K; 211 _glfw.wl.keycodes[KEY_L] = GLFW_KEY_L; 212 _glfw.wl.keycodes[KEY_SEMICOLON] = GLFW_KEY_SEMICOLON; 213 _glfw.wl.keycodes[KEY_APOSTROPHE] = GLFW_KEY_APOSTROPHE; 214 _glfw.wl.keycodes[KEY_Z] = GLFW_KEY_Z; 215 _glfw.wl.keycodes[KEY_X] = GLFW_KEY_X; 216 _glfw.wl.keycodes[KEY_C] = GLFW_KEY_C; 217 _glfw.wl.keycodes[KEY_V] = GLFW_KEY_V; 218 _glfw.wl.keycodes[KEY_B] = GLFW_KEY_B; 219 _glfw.wl.keycodes[KEY_N] = GLFW_KEY_N; 220 _glfw.wl.keycodes[KEY_M] = GLFW_KEY_M; 221 _glfw.wl.keycodes[KEY_COMMA] = GLFW_KEY_COMMA; 222 _glfw.wl.keycodes[KEY_DOT] = GLFW_KEY_PERIOD; 223 _glfw.wl.keycodes[KEY_SLASH] = GLFW_KEY_SLASH; 224 _glfw.wl.keycodes[KEY_BACKSLASH] = GLFW_KEY_BACKSLASH; 225 _glfw.wl.keycodes[KEY_ESC] = GLFW_KEY_ESCAPE; 226 _glfw.wl.keycodes[KEY_TAB] = GLFW_KEY_TAB; 227 _glfw.wl.keycodes[KEY_LEFTSHIFT] = GLFW_KEY_LEFT_SHIFT; 228 _glfw.wl.keycodes[KEY_RIGHTSHIFT] = GLFW_KEY_RIGHT_SHIFT; 229 _glfw.wl.keycodes[KEY_LEFTCTRL] = GLFW_KEY_LEFT_CONTROL; 230 _glfw.wl.keycodes[KEY_RIGHTCTRL] = GLFW_KEY_RIGHT_CONTROL; 231 _glfw.wl.keycodes[KEY_LEFTALT] = GLFW_KEY_LEFT_ALT; 232 _glfw.wl.keycodes[KEY_RIGHTALT] = GLFW_KEY_RIGHT_ALT; 233 _glfw.wl.keycodes[KEY_LEFTMETA] = GLFW_KEY_LEFT_SUPER; 234 _glfw.wl.keycodes[KEY_RIGHTMETA] = GLFW_KEY_RIGHT_SUPER; 235 _glfw.wl.keycodes[KEY_COMPOSE] = GLFW_KEY_MENU; 236 _glfw.wl.keycodes[KEY_NUMLOCK] = GLFW_KEY_NUM_LOCK; 237 _glfw.wl.keycodes[KEY_CAPSLOCK] = GLFW_KEY_CAPS_LOCK; 238 _glfw.wl.keycodes[KEY_PRINT] = GLFW_KEY_PRINT_SCREEN; 239 _glfw.wl.keycodes[KEY_SCROLLLOCK] = GLFW_KEY_SCROLL_LOCK; 240 _glfw.wl.keycodes[KEY_PAUSE] = GLFW_KEY_PAUSE; 241 _glfw.wl.keycodes[KEY_DELETE] = GLFW_KEY_DELETE; 242 _glfw.wl.keycodes[KEY_BACKSPACE] = GLFW_KEY_BACKSPACE; 243 _glfw.wl.keycodes[KEY_ENTER] = GLFW_KEY_ENTER; 244 _glfw.wl.keycodes[KEY_HOME] = GLFW_KEY_HOME; 245 _glfw.wl.keycodes[KEY_END] = GLFW_KEY_END; 246 _glfw.wl.keycodes[KEY_PAGEUP] = GLFW_KEY_PAGE_UP; 247 _glfw.wl.keycodes[KEY_PAGEDOWN] = GLFW_KEY_PAGE_DOWN; 248 _glfw.wl.keycodes[KEY_INSERT] = GLFW_KEY_INSERT; 249 _glfw.wl.keycodes[KEY_LEFT] = GLFW_KEY_LEFT; 250 _glfw.wl.keycodes[KEY_RIGHT] = GLFW_KEY_RIGHT; 251 _glfw.wl.keycodes[KEY_DOWN] = GLFW_KEY_DOWN; 252 _glfw.wl.keycodes[KEY_UP] = GLFW_KEY_UP; 253 _glfw.wl.keycodes[KEY_F1] = GLFW_KEY_F1; 254 _glfw.wl.keycodes[KEY_F2] = GLFW_KEY_F2; 255 _glfw.wl.keycodes[KEY_F3] = GLFW_KEY_F3; 256 _glfw.wl.keycodes[KEY_F4] = GLFW_KEY_F4; 257 _glfw.wl.keycodes[KEY_F5] = GLFW_KEY_F5; 258 _glfw.wl.keycodes[KEY_F6] = GLFW_KEY_F6; 259 _glfw.wl.keycodes[KEY_F7] = GLFW_KEY_F7; 260 _glfw.wl.keycodes[KEY_F8] = GLFW_KEY_F8; 261 _glfw.wl.keycodes[KEY_F9] = GLFW_KEY_F9; 262 _glfw.wl.keycodes[KEY_F10] = GLFW_KEY_F10; 263 _glfw.wl.keycodes[KEY_F11] = GLFW_KEY_F11; 264 _glfw.wl.keycodes[KEY_F12] = GLFW_KEY_F12; 265 _glfw.wl.keycodes[KEY_F13] = GLFW_KEY_F13; 266 _glfw.wl.keycodes[KEY_F14] = GLFW_KEY_F14; 267 _glfw.wl.keycodes[KEY_F15] = GLFW_KEY_F15; 268 _glfw.wl.keycodes[KEY_F16] = GLFW_KEY_F16; 269 _glfw.wl.keycodes[KEY_F17] = GLFW_KEY_F17; 270 _glfw.wl.keycodes[KEY_F18] = GLFW_KEY_F18; 271 _glfw.wl.keycodes[KEY_F19] = GLFW_KEY_F19; 272 _glfw.wl.keycodes[KEY_F20] = GLFW_KEY_F20; 273 _glfw.wl.keycodes[KEY_F21] = GLFW_KEY_F21; 274 _glfw.wl.keycodes[KEY_F22] = GLFW_KEY_F22; 275 _glfw.wl.keycodes[KEY_F23] = GLFW_KEY_F23; 276 _glfw.wl.keycodes[KEY_F24] = GLFW_KEY_F24; 277 _glfw.wl.keycodes[KEY_KPSLASH] = GLFW_KEY_KP_DIVIDE; 278 _glfw.wl.keycodes[KEY_KPASTERISK] = GLFW_KEY_KP_MULTIPLY; 279 _glfw.wl.keycodes[KEY_KPMINUS] = GLFW_KEY_KP_SUBTRACT; 280 _glfw.wl.keycodes[KEY_KPPLUS] = GLFW_KEY_KP_ADD; 281 _glfw.wl.keycodes[KEY_KP0] = GLFW_KEY_KP_0; 282 _glfw.wl.keycodes[KEY_KP1] = GLFW_KEY_KP_1; 283 _glfw.wl.keycodes[KEY_KP2] = GLFW_KEY_KP_2; 284 _glfw.wl.keycodes[KEY_KP3] = GLFW_KEY_KP_3; 285 _glfw.wl.keycodes[KEY_KP4] = GLFW_KEY_KP_4; 286 _glfw.wl.keycodes[KEY_KP5] = GLFW_KEY_KP_5; 287 _glfw.wl.keycodes[KEY_KP6] = GLFW_KEY_KP_6; 288 _glfw.wl.keycodes[KEY_KP7] = GLFW_KEY_KP_7; 289 _glfw.wl.keycodes[KEY_KP8] = GLFW_KEY_KP_8; 290 _glfw.wl.keycodes[KEY_KP9] = GLFW_KEY_KP_9; 291 _glfw.wl.keycodes[KEY_KPDOT] = GLFW_KEY_KP_DECIMAL; 292 _glfw.wl.keycodes[KEY_KPEQUAL] = GLFW_KEY_KP_EQUAL; 293 _glfw.wl.keycodes[KEY_KPENTER] = GLFW_KEY_KP_ENTER; 294 _glfw.wl.keycodes[KEY_102ND] = GLFW_KEY_WORLD_2; 295 296 for (scancode = 0; scancode < 256; scancode++) 297 { 298 if (_glfw.wl.keycodes[scancode] > 0) 299 _glfw.wl.scancodes[_glfw.wl.keycodes[scancode]] = scancode; 300 } 301 } 302 303 304 ////////////////////////////////////////////////////////////////////////// 305 ////// GLFW platform API ////// 306 ////////////////////////////////////////////////////////////////////////// 307 308 int _glfwPlatformInit(void) 309 { 310 const char* cursorTheme; 311 const char* cursorSizeStr; 312 char* cursorSizeEnd; 313 long cursorSizeLong; 314 int cursorSize; 315 316 // These must be set before any failure checks 317 _glfw.wl.timerfd = -1; 318 _glfw.wl.cursorTimerfd = -1; 319 320 _glfw.wl.cursor.handle = _glfw_dlopen("libwayland-cursor.so.0"); 321 if (!_glfw.wl.cursor.handle) 322 { 323 _glfwInputError(GLFW_PLATFORM_ERROR, 324 "Wayland: Failed to load libwayland-cursor"); 325 return GLFW_FALSE; 326 } 327 328 _glfw.wl.cursor.theme_load = (PFN_wl_cursor_theme_load) 329 _glfw_dlsym(_glfw.wl.cursor.handle, "wl_cursor_theme_load"); 330 _glfw.wl.cursor.theme_destroy = (PFN_wl_cursor_theme_destroy) 331 _glfw_dlsym(_glfw.wl.cursor.handle, "wl_cursor_theme_destroy"); 332 _glfw.wl.cursor.theme_get_cursor = (PFN_wl_cursor_theme_get_cursor) 333 _glfw_dlsym(_glfw.wl.cursor.handle, "wl_cursor_theme_get_cursor"); 334 _glfw.wl.cursor.image_get_buffer = (PFN_wl_cursor_image_get_buffer) 335 _glfw_dlsym(_glfw.wl.cursor.handle, "wl_cursor_image_get_buffer"); 336 337 _glfw.wl.egl.handle = _glfw_dlopen("libwayland-egl.so.1"); 338 if (!_glfw.wl.egl.handle) 339 { 340 _glfwInputError(GLFW_PLATFORM_ERROR, 341 "Wayland: Failed to load libwayland-egl"); 342 return GLFW_FALSE; 343 } 344 345 _glfw.wl.egl.window_create = (PFN_wl_egl_window_create) 346 _glfw_dlsym(_glfw.wl.egl.handle, "wl_egl_window_create"); 347 _glfw.wl.egl.window_destroy = (PFN_wl_egl_window_destroy) 348 _glfw_dlsym(_glfw.wl.egl.handle, "wl_egl_window_destroy"); 349 _glfw.wl.egl.window_resize = (PFN_wl_egl_window_resize) 350 _glfw_dlsym(_glfw.wl.egl.handle, "wl_egl_window_resize"); 351 352 _glfw.wl.xkb.handle = _glfw_dlopen("libxkbcommon.so.0"); 353 if (!_glfw.wl.xkb.handle) 354 { 355 _glfwInputError(GLFW_PLATFORM_ERROR, 356 "Wayland: Failed to load libxkbcommon"); 357 return GLFW_FALSE; 358 } 359 360 _glfw.wl.xkb.context_new = (PFN_xkb_context_new) 361 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_context_new"); 362 _glfw.wl.xkb.context_unref = (PFN_xkb_context_unref) 363 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_context_unref"); 364 _glfw.wl.xkb.keymap_new_from_string = (PFN_xkb_keymap_new_from_string) 365 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_keymap_new_from_string"); 366 _glfw.wl.xkb.keymap_unref = (PFN_xkb_keymap_unref) 367 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_keymap_unref"); 368 _glfw.wl.xkb.keymap_mod_get_index = (PFN_xkb_keymap_mod_get_index) 369 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_keymap_mod_get_index"); 370 _glfw.wl.xkb.keymap_key_repeats = (PFN_xkb_keymap_key_repeats) 371 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_keymap_key_repeats"); 372 _glfw.wl.xkb.keymap_key_get_syms_by_level = (PFN_xkb_keymap_key_get_syms_by_level) 373 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_keymap_key_get_syms_by_level"); 374 _glfw.wl.xkb.state_new = (PFN_xkb_state_new) 375 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_new"); 376 _glfw.wl.xkb.state_unref = (PFN_xkb_state_unref) 377 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_unref"); 378 _glfw.wl.xkb.state_key_get_syms = (PFN_xkb_state_key_get_syms) 379 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_key_get_syms"); 380 _glfw.wl.xkb.state_update_mask = (PFN_xkb_state_update_mask) 381 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_update_mask"); 382 _glfw.wl.xkb.state_key_get_layout = (PFN_xkb_state_key_get_layout) 383 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_key_get_layout"); 384 _glfw.wl.xkb.state_mod_index_is_active = (PFN_xkb_state_mod_index_is_active) 385 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_state_mod_index_is_active"); 386 387 _glfw.wl.xkb.compose_table_new_from_locale = (PFN_xkb_compose_table_new_from_locale) 388 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_table_new_from_locale"); 389 _glfw.wl.xkb.compose_table_unref = (PFN_xkb_compose_table_unref) 390 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_table_unref"); 391 _glfw.wl.xkb.compose_state_new = (PFN_xkb_compose_state_new) 392 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_state_new"); 393 _glfw.wl.xkb.compose_state_unref = (PFN_xkb_compose_state_unref) 394 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_state_unref"); 395 _glfw.wl.xkb.compose_state_feed = (PFN_xkb_compose_state_feed) 396 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_state_feed"); 397 _glfw.wl.xkb.compose_state_get_status = (PFN_xkb_compose_state_get_status) 398 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_state_get_status"); 399 _glfw.wl.xkb.compose_state_get_one_sym = (PFN_xkb_compose_state_get_one_sym) 400 _glfw_dlsym(_glfw.wl.xkb.handle, "xkb_compose_state_get_one_sym"); 401 402 _glfw.wl.display = wl_display_connect(NULL); 403 if (!_glfw.wl.display) 404 { 405 _glfwInputError(GLFW_PLATFORM_ERROR, 406 "Wayland: Failed to connect to display"); 407 return GLFW_FALSE; 408 } 409 410 _glfw.wl.registry = wl_display_get_registry(_glfw.wl.display); 411 wl_registry_add_listener(_glfw.wl.registry, ®istryListener, NULL); 412 413 createKeyTables(); 414 415 _glfw.wl.xkb.context = xkb_context_new(0); 416 if (!_glfw.wl.xkb.context) 417 { 418 _glfwInputError(GLFW_PLATFORM_ERROR, 419 "Wayland: Failed to initialize xkb context"); 420 return GLFW_FALSE; 421 } 422 423 // Sync so we got all registry objects 424 wl_display_roundtrip(_glfw.wl.display); 425 426 // Sync so we got all initial output events 427 wl_display_roundtrip(_glfw.wl.display); 428 429 #ifdef __linux__ 430 if (!_glfwInitJoysticksLinux()) 431 return GLFW_FALSE; 432 #endif 433 434 _glfwInitTimerPOSIX(); 435 436 #ifdef WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION 437 if (_glfw.wl.seatVersion >= WL_KEYBOARD_REPEAT_INFO_SINCE_VERSION) 438 _glfw.wl.timerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); 439 #endif 440 441 if (!_glfw.wl.wmBase) 442 { 443 _glfwInputError(GLFW_PLATFORM_ERROR, 444 "Wayland: Failed to find xdg-shell in your compositor"); 445 return GLFW_FALSE; 446 } 447 448 if (_glfw.wl.pointer && _glfw.wl.shm) 449 { 450 cursorTheme = getenv("XCURSOR_THEME"); 451 cursorSizeStr = getenv("XCURSOR_SIZE"); 452 cursorSize = 32; 453 if (cursorSizeStr) 454 { 455 errno = 0; 456 cursorSizeLong = strtol(cursorSizeStr, &cursorSizeEnd, 10); 457 if (!*cursorSizeEnd && !errno && cursorSizeLong > 0 && cursorSizeLong <= INT_MAX) 458 cursorSize = (int)cursorSizeLong; 459 } 460 _glfw.wl.cursorTheme = 461 wl_cursor_theme_load(cursorTheme, cursorSize, _glfw.wl.shm); 462 if (!_glfw.wl.cursorTheme) 463 { 464 _glfwInputError(GLFW_PLATFORM_ERROR, 465 "Wayland: Failed to load default cursor theme"); 466 return GLFW_FALSE; 467 } 468 // If this happens to be NULL, we just fallback to the scale=1 version. 469 _glfw.wl.cursorThemeHiDPI = 470 wl_cursor_theme_load(cursorTheme, 2 * cursorSize, _glfw.wl.shm); 471 _glfw.wl.cursorSurface = 472 wl_compositor_create_surface(_glfw.wl.compositor); 473 _glfw.wl.cursorTimerfd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC | TFD_NONBLOCK); 474 } 475 476 if (_glfw.wl.seat && _glfw.wl.dataDeviceManager) 477 { 478 _glfw.wl.dataDevice = 479 wl_data_device_manager_get_data_device(_glfw.wl.dataDeviceManager, 480 _glfw.wl.seat); 481 _glfwAddDataDeviceListenerWayland(_glfw.wl.dataDevice); 482 } 483 484 return GLFW_TRUE; 485 } 486 487 void _glfwPlatformTerminate(void) 488 { 489 #ifdef __linux__ 490 _glfwTerminateJoysticksLinux(); 491 #endif 492 _glfwTerminateEGL(); 493 _glfwTerminateOSMesa(); 494 495 if (_glfw.wl.egl.handle) 496 { 497 _glfw_dlclose(_glfw.wl.egl.handle); 498 _glfw.wl.egl.handle = NULL; 499 } 500 501 if (_glfw.wl.xkb.composeState) 502 xkb_compose_state_unref(_glfw.wl.xkb.composeState); 503 if (_glfw.wl.xkb.keymap) 504 xkb_keymap_unref(_glfw.wl.xkb.keymap); 505 if (_glfw.wl.xkb.state) 506 xkb_state_unref(_glfw.wl.xkb.state); 507 if (_glfw.wl.xkb.context) 508 xkb_context_unref(_glfw.wl.xkb.context); 509 if (_glfw.wl.xkb.handle) 510 { 511 _glfw_dlclose(_glfw.wl.xkb.handle); 512 _glfw.wl.xkb.handle = NULL; 513 } 514 515 if (_glfw.wl.cursorTheme) 516 wl_cursor_theme_destroy(_glfw.wl.cursorTheme); 517 if (_glfw.wl.cursorThemeHiDPI) 518 wl_cursor_theme_destroy(_glfw.wl.cursorThemeHiDPI); 519 if (_glfw.wl.cursor.handle) 520 { 521 _glfw_dlclose(_glfw.wl.cursor.handle); 522 _glfw.wl.cursor.handle = NULL; 523 } 524 525 for (unsigned int i = 0; i < _glfw.wl.offerCount; i++) 526 wl_data_offer_destroy(_glfw.wl.offers[i].offer); 527 528 free(_glfw.wl.offers); 529 530 if (_glfw.wl.cursorSurface) 531 wl_surface_destroy(_glfw.wl.cursorSurface); 532 if (_glfw.wl.subcompositor) 533 wl_subcompositor_destroy(_glfw.wl.subcompositor); 534 if (_glfw.wl.compositor) 535 wl_compositor_destroy(_glfw.wl.compositor); 536 if (_glfw.wl.shm) 537 wl_shm_destroy(_glfw.wl.shm); 538 if (_glfw.wl.viewporter) 539 wp_viewporter_destroy(_glfw.wl.viewporter); 540 if (_glfw.wl.decorationManager) 541 zxdg_decoration_manager_v1_destroy(_glfw.wl.decorationManager); 542 if (_glfw.wl.wmBase) 543 xdg_wm_base_destroy(_glfw.wl.wmBase); 544 if (_glfw.wl.selectionOffer) 545 wl_data_offer_destroy(_glfw.wl.selectionOffer); 546 if (_glfw.wl.dragOffer) 547 wl_data_offer_destroy(_glfw.wl.dragOffer); 548 if (_glfw.wl.selectionSource) 549 wl_data_source_destroy(_glfw.wl.selectionSource); 550 if (_glfw.wl.dataDevice) 551 wl_data_device_destroy(_glfw.wl.dataDevice); 552 if (_glfw.wl.dataDeviceManager) 553 wl_data_device_manager_destroy(_glfw.wl.dataDeviceManager); 554 if (_glfw.wl.pointer) 555 wl_pointer_destroy(_glfw.wl.pointer); 556 if (_glfw.wl.keyboard) 557 wl_keyboard_destroy(_glfw.wl.keyboard); 558 if (_glfw.wl.seat) 559 wl_seat_destroy(_glfw.wl.seat); 560 if (_glfw.wl.relativePointerManager) 561 zwp_relative_pointer_manager_v1_destroy(_glfw.wl.relativePointerManager); 562 if (_glfw.wl.pointerConstraints) 563 zwp_pointer_constraints_v1_destroy(_glfw.wl.pointerConstraints); 564 if (_glfw.wl.idleInhibitManager) 565 zwp_idle_inhibit_manager_v1_destroy(_glfw.wl.idleInhibitManager); 566 if (_glfw.wl.registry) 567 wl_registry_destroy(_glfw.wl.registry); 568 if (_glfw.wl.display) 569 { 570 wl_display_flush(_glfw.wl.display); 571 wl_display_disconnect(_glfw.wl.display); 572 } 573 574 if (_glfw.wl.timerfd >= 0) 575 close(_glfw.wl.timerfd); 576 if (_glfw.wl.cursorTimerfd >= 0) 577 close(_glfw.wl.cursorTimerfd); 578 579 free(_glfw.wl.clipboardString); 580 } 581 582 const char* _glfwPlatformGetVersionString(void) 583 { 584 return _GLFW_VERSION_NUMBER " Wayland EGL OSMesa" 585 #if defined(_POSIX_TIMERS) && defined(_POSIX_MONOTONIC_CLOCK) 586 " clock_gettime" 587 #else 588 " gettimeofday" 589 #endif 590 " evdev" 591 #if defined(_GLFW_BUILD_DLL) 592 " shared" 593 #endif 594 ; 595 }