wl_window.c (54213B)
1 //======================================================================== 2 // GLFW 3.4 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 _GNU_SOURCE 30 31 #include "internal.h" 32 33 #include <stdio.h> 34 #include <stdlib.h> 35 #include <errno.h> 36 #include <unistd.h> 37 #include <string.h> 38 #include <fcntl.h> 39 #include <sys/mman.h> 40 #include <sys/timerfd.h> 41 #include <poll.h> 42 43 44 static int createTmpfileCloexec(char* tmpname) 45 { 46 int fd; 47 48 fd = mkostemp(tmpname, O_CLOEXEC); 49 if (fd >= 0) 50 unlink(tmpname); 51 52 return fd; 53 } 54 55 /* 56 * Create a new, unique, anonymous file of the given size, and 57 * return the file descriptor for it. The file descriptor is set 58 * CLOEXEC. The file is immediately suitable for mmap()'ing 59 * the given size at offset zero. 60 * 61 * The file should not have a permanent backing store like a disk, 62 * but may have if XDG_RUNTIME_DIR is not properly implemented in OS. 63 * 64 * The file name is deleted from the file system. 65 * 66 * The file is suitable for buffer sharing between processes by 67 * transmitting the file descriptor over Unix sockets using the 68 * SCM_RIGHTS methods. 69 * 70 * posix_fallocate() is used to guarantee that disk space is available 71 * for the file at the given size. If disk space is insufficient, errno 72 * is set to ENOSPC. If posix_fallocate() is not supported, program may 73 * receive SIGBUS on accessing mmap()'ed file contents instead. 74 */ 75 static int createAnonymousFile(off_t size) 76 { 77 static const char template[] = "/glfw-shared-XXXXXX"; 78 const char* path; 79 char* name; 80 int fd; 81 int ret; 82 83 #ifdef HAVE_MEMFD_CREATE 84 fd = memfd_create("glfw-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING); 85 if (fd >= 0) 86 { 87 // We can add this seal before calling posix_fallocate(), as the file 88 // is currently zero-sized anyway. 89 // 90 // There is also no need to check for the return value, we couldn’t do 91 // anything with it anyway. 92 fcntl(fd, F_ADD_SEALS, F_SEAL_SHRINK | F_SEAL_SEAL); 93 } 94 else 95 #elif defined(SHM_ANON) 96 fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600); 97 if (fd < 0) 98 #endif 99 { 100 path = getenv("XDG_RUNTIME_DIR"); 101 if (!path) 102 { 103 errno = ENOENT; 104 return -1; 105 } 106 107 name = calloc(strlen(path) + sizeof(template), 1); 108 strcpy(name, path); 109 strcat(name, template); 110 111 fd = createTmpfileCloexec(name); 112 free(name); 113 if (fd < 0) 114 return -1; 115 } 116 117 #if defined(SHM_ANON) 118 // posix_fallocate does not work on SHM descriptors 119 ret = ftruncate(fd, size); 120 #else 121 ret = posix_fallocate(fd, 0, size); 122 #endif 123 if (ret != 0) 124 { 125 close(fd); 126 errno = ret; 127 return -1; 128 } 129 return fd; 130 } 131 132 static struct wl_buffer* createShmBuffer(const GLFWimage* image) 133 { 134 struct wl_shm_pool* pool; 135 struct wl_buffer* buffer; 136 int stride = image->width * 4; 137 int length = image->width * image->height * 4; 138 void* data; 139 int fd, i; 140 141 fd = createAnonymousFile(length); 142 if (fd < 0) 143 { 144 _glfwInputError(GLFW_PLATFORM_ERROR, 145 "Wayland: Creating a buffer file for %d B failed: %m", 146 length); 147 return NULL; 148 } 149 150 data = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); 151 if (data == MAP_FAILED) 152 { 153 _glfwInputError(GLFW_PLATFORM_ERROR, 154 "Wayland: mmap failed: %m"); 155 close(fd); 156 return NULL; 157 } 158 159 pool = wl_shm_create_pool(_glfw.wl.shm, fd, length); 160 161 close(fd); 162 unsigned char* source = (unsigned char*) image->pixels; 163 unsigned char* target = data; 164 for (i = 0; i < image->width * image->height; i++, source += 4) 165 { 166 unsigned int alpha = source[3]; 167 168 *target++ = (unsigned char) ((source[2] * alpha) / 255); 169 *target++ = (unsigned char) ((source[1] * alpha) / 255); 170 *target++ = (unsigned char) ((source[0] * alpha) / 255); 171 *target++ = (unsigned char) alpha; 172 } 173 174 buffer = 175 wl_shm_pool_create_buffer(pool, 0, 176 image->width, 177 image->height, 178 stride, WL_SHM_FORMAT_ARGB8888); 179 munmap(data, length); 180 wl_shm_pool_destroy(pool); 181 182 return buffer; 183 } 184 185 static void createDecoration(_GLFWdecorationWayland* decoration, 186 struct wl_surface* parent, 187 struct wl_buffer* buffer, GLFWbool opaque, 188 int x, int y, 189 int width, int height) 190 { 191 struct wl_region* region; 192 193 decoration->surface = wl_compositor_create_surface(_glfw.wl.compositor); 194 decoration->subsurface = 195 wl_subcompositor_get_subsurface(_glfw.wl.subcompositor, 196 decoration->surface, parent); 197 wl_subsurface_set_position(decoration->subsurface, x, y); 198 decoration->viewport = wp_viewporter_get_viewport(_glfw.wl.viewporter, 199 decoration->surface); 200 wp_viewport_set_destination(decoration->viewport, width, height); 201 wl_surface_attach(decoration->surface, buffer, 0, 0); 202 203 if (opaque) 204 { 205 region = wl_compositor_create_region(_glfw.wl.compositor); 206 wl_region_add(region, 0, 0, width, height); 207 wl_surface_set_opaque_region(decoration->surface, region); 208 wl_surface_commit(decoration->surface); 209 wl_region_destroy(region); 210 } 211 else 212 wl_surface_commit(decoration->surface); 213 } 214 215 static void createDecorations(_GLFWwindow* window) 216 { 217 unsigned char data[] = { 224, 224, 224, 255 }; 218 const GLFWimage image = { 1, 1, data }; 219 GLFWbool opaque = (data[3] == 255); 220 221 if (!_glfw.wl.viewporter || !window->decorated || window->wl.decorations.serverSide) 222 return; 223 224 if (!window->wl.decorations.buffer) 225 window->wl.decorations.buffer = createShmBuffer(&image); 226 if (!window->wl.decorations.buffer) 227 return; 228 229 createDecoration(&window->wl.decorations.top, window->wl.surface, 230 window->wl.decorations.buffer, opaque, 231 0, -_GLFW_DECORATION_TOP, 232 window->wl.width, _GLFW_DECORATION_TOP); 233 createDecoration(&window->wl.decorations.left, window->wl.surface, 234 window->wl.decorations.buffer, opaque, 235 -_GLFW_DECORATION_WIDTH, -_GLFW_DECORATION_TOP, 236 _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP); 237 createDecoration(&window->wl.decorations.right, window->wl.surface, 238 window->wl.decorations.buffer, opaque, 239 window->wl.width, -_GLFW_DECORATION_TOP, 240 _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP); 241 createDecoration(&window->wl.decorations.bottom, window->wl.surface, 242 window->wl.decorations.buffer, opaque, 243 -_GLFW_DECORATION_WIDTH, window->wl.height, 244 window->wl.width + _GLFW_DECORATION_HORIZONTAL, _GLFW_DECORATION_WIDTH); 245 } 246 247 static void destroyDecoration(_GLFWdecorationWayland* decoration) 248 { 249 if (decoration->surface) 250 wl_surface_destroy(decoration->surface); 251 if (decoration->subsurface) 252 wl_subsurface_destroy(decoration->subsurface); 253 if (decoration->viewport) 254 wp_viewport_destroy(decoration->viewport); 255 decoration->surface = NULL; 256 decoration->subsurface = NULL; 257 decoration->viewport = NULL; 258 } 259 260 static void destroyDecorations(_GLFWwindow* window) 261 { 262 destroyDecoration(&window->wl.decorations.top); 263 destroyDecoration(&window->wl.decorations.left); 264 destroyDecoration(&window->wl.decorations.right); 265 destroyDecoration(&window->wl.decorations.bottom); 266 } 267 268 static void xdgDecorationHandleConfigure(void* data, 269 struct zxdg_toplevel_decoration_v1* decoration, 270 uint32_t mode) 271 { 272 _GLFWwindow* window = data; 273 274 window->wl.decorations.serverSide = (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); 275 276 if (!window->wl.decorations.serverSide) 277 createDecorations(window); 278 } 279 280 static const struct zxdg_toplevel_decoration_v1_listener xdgDecorationListener = { 281 xdgDecorationHandleConfigure, 282 }; 283 284 // Makes the surface considered as XRGB instead of ARGB. 285 static void setOpaqueRegion(_GLFWwindow* window) 286 { 287 struct wl_region* region; 288 289 region = wl_compositor_create_region(_glfw.wl.compositor); 290 if (!region) 291 return; 292 293 wl_region_add(region, 0, 0, window->wl.width, window->wl.height); 294 wl_surface_set_opaque_region(window->wl.surface, region); 295 wl_surface_commit(window->wl.surface); 296 wl_region_destroy(region); 297 } 298 299 300 static void resizeWindow(_GLFWwindow* window) 301 { 302 int scale = window->wl.scale; 303 int scaledWidth = window->wl.width * scale; 304 int scaledHeight = window->wl.height * scale; 305 wl_egl_window_resize(window->wl.native, scaledWidth, scaledHeight, 0, 0); 306 if (!window->wl.transparent) 307 setOpaqueRegion(window); 308 _glfwInputFramebufferSize(window, scaledWidth, scaledHeight); 309 _glfwInputWindowContentScale(window, scale, scale); 310 311 if (!window->wl.decorations.top.surface) 312 return; 313 314 // Top decoration. 315 wp_viewport_set_destination(window->wl.decorations.top.viewport, 316 window->wl.width, _GLFW_DECORATION_TOP); 317 wl_surface_commit(window->wl.decorations.top.surface); 318 319 // Left decoration. 320 wp_viewport_set_destination(window->wl.decorations.left.viewport, 321 _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP); 322 wl_surface_commit(window->wl.decorations.left.surface); 323 324 // Right decoration. 325 wl_subsurface_set_position(window->wl.decorations.right.subsurface, 326 window->wl.width, -_GLFW_DECORATION_TOP); 327 wp_viewport_set_destination(window->wl.decorations.right.viewport, 328 _GLFW_DECORATION_WIDTH, window->wl.height + _GLFW_DECORATION_TOP); 329 wl_surface_commit(window->wl.decorations.right.surface); 330 331 // Bottom decoration. 332 wl_subsurface_set_position(window->wl.decorations.bottom.subsurface, 333 -_GLFW_DECORATION_WIDTH, window->wl.height); 334 wp_viewport_set_destination(window->wl.decorations.bottom.viewport, 335 window->wl.width + _GLFW_DECORATION_HORIZONTAL, _GLFW_DECORATION_WIDTH); 336 wl_surface_commit(window->wl.decorations.bottom.surface); 337 } 338 339 static void checkScaleChange(_GLFWwindow* window) 340 { 341 int scale = 1; 342 int i; 343 int monitorScale; 344 345 // Check if we will be able to set the buffer scale or not. 346 if (_glfw.wl.compositorVersion < 3) 347 return; 348 349 // Get the scale factor from the highest scale monitor. 350 for (i = 0; i < window->wl.monitorsCount; ++i) 351 { 352 monitorScale = window->wl.monitors[i]->wl.scale; 353 if (scale < monitorScale) 354 scale = monitorScale; 355 } 356 357 // Only change the framebuffer size if the scale changed. 358 if (scale != window->wl.scale) 359 { 360 window->wl.scale = scale; 361 wl_surface_set_buffer_scale(window->wl.surface, scale); 362 resizeWindow(window); 363 } 364 } 365 366 static void surfaceHandleEnter(void *data, 367 struct wl_surface *surface, 368 struct wl_output *output) 369 { 370 _GLFWwindow* window = data; 371 _GLFWmonitor* monitor = wl_output_get_user_data(output); 372 373 if (window->wl.monitorsCount + 1 > window->wl.monitorsSize) 374 { 375 ++window->wl.monitorsSize; 376 window->wl.monitors = 377 realloc(window->wl.monitors, 378 window->wl.monitorsSize * sizeof(_GLFWmonitor*)); 379 } 380 381 window->wl.monitors[window->wl.monitorsCount++] = monitor; 382 383 checkScaleChange(window); 384 } 385 386 static void surfaceHandleLeave(void *data, 387 struct wl_surface *surface, 388 struct wl_output *output) 389 { 390 _GLFWwindow* window = data; 391 _GLFWmonitor* monitor = wl_output_get_user_data(output); 392 GLFWbool found; 393 int i; 394 395 for (i = 0, found = GLFW_FALSE; i < window->wl.monitorsCount - 1; ++i) 396 { 397 if (monitor == window->wl.monitors[i]) 398 found = GLFW_TRUE; 399 if (found) 400 window->wl.monitors[i] = window->wl.monitors[i + 1]; 401 } 402 window->wl.monitors[--window->wl.monitorsCount] = NULL; 403 404 checkScaleChange(window); 405 } 406 407 static const struct wl_surface_listener surfaceListener = { 408 surfaceHandleEnter, 409 surfaceHandleLeave 410 }; 411 412 static void setIdleInhibitor(_GLFWwindow* window, GLFWbool enable) 413 { 414 if (enable && !window->wl.idleInhibitor && _glfw.wl.idleInhibitManager) 415 { 416 window->wl.idleInhibitor = 417 zwp_idle_inhibit_manager_v1_create_inhibitor( 418 _glfw.wl.idleInhibitManager, window->wl.surface); 419 if (!window->wl.idleInhibitor) 420 _glfwInputError(GLFW_PLATFORM_ERROR, 421 "Wayland: Idle inhibitor creation failed"); 422 } 423 else if (!enable && window->wl.idleInhibitor) 424 { 425 zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor); 426 window->wl.idleInhibitor = NULL; 427 } 428 } 429 430 static GLFWbool createSurface(_GLFWwindow* window, 431 const _GLFWwndconfig* wndconfig) 432 { 433 window->wl.surface = wl_compositor_create_surface(_glfw.wl.compositor); 434 if (!window->wl.surface) 435 return GLFW_FALSE; 436 437 wl_surface_add_listener(window->wl.surface, 438 &surfaceListener, 439 window); 440 441 wl_surface_set_user_data(window->wl.surface, window); 442 443 window->wl.native = wl_egl_window_create(window->wl.surface, 444 wndconfig->width, 445 wndconfig->height); 446 if (!window->wl.native) 447 return GLFW_FALSE; 448 449 window->wl.width = wndconfig->width; 450 window->wl.height = wndconfig->height; 451 window->wl.scale = 1; 452 453 if (!window->wl.transparent) 454 setOpaqueRegion(window); 455 456 return GLFW_TRUE; 457 } 458 459 static void setFullscreen(_GLFWwindow* window, _GLFWmonitor* monitor, 460 int refreshRate) 461 { 462 if (window->wl.xdg.toplevel) 463 { 464 xdg_toplevel_set_fullscreen( 465 window->wl.xdg.toplevel, 466 monitor->wl.output); 467 } 468 setIdleInhibitor(window, GLFW_TRUE); 469 if (!window->wl.decorations.serverSide) 470 destroyDecorations(window); 471 } 472 473 static void xdgToplevelHandleConfigure(void* data, 474 struct xdg_toplevel* toplevel, 475 int32_t width, 476 int32_t height, 477 struct wl_array* states) 478 { 479 _GLFWwindow* window = data; 480 float aspectRatio; 481 float targetRatio; 482 uint32_t* state; 483 GLFWbool maximized = GLFW_FALSE; 484 GLFWbool fullscreen = GLFW_FALSE; 485 GLFWbool activated = GLFW_FALSE; 486 487 wl_array_for_each(state, states) 488 { 489 switch (*state) 490 { 491 case XDG_TOPLEVEL_STATE_MAXIMIZED: 492 maximized = GLFW_TRUE; 493 break; 494 case XDG_TOPLEVEL_STATE_FULLSCREEN: 495 fullscreen = GLFW_TRUE; 496 break; 497 case XDG_TOPLEVEL_STATE_RESIZING: 498 break; 499 case XDG_TOPLEVEL_STATE_ACTIVATED: 500 activated = GLFW_TRUE; 501 break; 502 } 503 } 504 505 if (width != 0 && height != 0) 506 { 507 if (!maximized && !fullscreen) 508 { 509 if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE) 510 { 511 aspectRatio = (float)width / (float)height; 512 targetRatio = (float)window->numer / (float)window->denom; 513 if (aspectRatio < targetRatio) 514 height = width / targetRatio; 515 else if (aspectRatio > targetRatio) 516 width = height * targetRatio; 517 } 518 } 519 520 _glfwInputWindowSize(window, width, height); 521 _glfwPlatformSetWindowSize(window, width, height); 522 _glfwInputWindowDamage(window); 523 } 524 525 if (window->wl.wasFullscreen && window->autoIconify) 526 { 527 if (!activated || !fullscreen) 528 { 529 _glfwPlatformIconifyWindow(window); 530 window->wl.wasFullscreen = GLFW_FALSE; 531 } 532 } 533 if (fullscreen && activated) 534 window->wl.wasFullscreen = GLFW_TRUE; 535 _glfwInputWindowFocus(window, activated); 536 } 537 538 static void xdgToplevelHandleClose(void* data, 539 struct xdg_toplevel* toplevel) 540 { 541 _GLFWwindow* window = data; 542 _glfwInputWindowCloseRequest(window); 543 } 544 545 static const struct xdg_toplevel_listener xdgToplevelListener = { 546 xdgToplevelHandleConfigure, 547 xdgToplevelHandleClose 548 }; 549 550 static void xdgSurfaceHandleConfigure(void* data, 551 struct xdg_surface* surface, 552 uint32_t serial) 553 { 554 xdg_surface_ack_configure(surface, serial); 555 } 556 557 static const struct xdg_surface_listener xdgSurfaceListener = { 558 xdgSurfaceHandleConfigure 559 }; 560 561 static void setXdgDecorations(_GLFWwindow* window) 562 { 563 if (_glfw.wl.decorationManager) 564 { 565 window->wl.xdg.decoration = 566 zxdg_decoration_manager_v1_get_toplevel_decoration( 567 _glfw.wl.decorationManager, window->wl.xdg.toplevel); 568 zxdg_toplevel_decoration_v1_add_listener(window->wl.xdg.decoration, 569 &xdgDecorationListener, 570 window); 571 zxdg_toplevel_decoration_v1_set_mode( 572 window->wl.xdg.decoration, 573 ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); 574 } 575 else 576 { 577 window->wl.decorations.serverSide = GLFW_FALSE; 578 createDecorations(window); 579 } 580 } 581 582 static GLFWbool createXdgSurface(_GLFWwindow* window) 583 { 584 window->wl.xdg.surface = xdg_wm_base_get_xdg_surface(_glfw.wl.wmBase, 585 window->wl.surface); 586 if (!window->wl.xdg.surface) 587 { 588 _glfwInputError(GLFW_PLATFORM_ERROR, 589 "Wayland: xdg-surface creation failed"); 590 return GLFW_FALSE; 591 } 592 593 xdg_surface_add_listener(window->wl.xdg.surface, 594 &xdgSurfaceListener, 595 window); 596 597 window->wl.xdg.toplevel = xdg_surface_get_toplevel(window->wl.xdg.surface); 598 if (!window->wl.xdg.toplevel) 599 { 600 _glfwInputError(GLFW_PLATFORM_ERROR, 601 "Wayland: xdg-toplevel creation failed"); 602 return GLFW_FALSE; 603 } 604 605 xdg_toplevel_add_listener(window->wl.xdg.toplevel, 606 &xdgToplevelListener, 607 window); 608 609 if (window->wl.title) 610 xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title); 611 612 if (window->minwidth != GLFW_DONT_CARE && window->minheight != GLFW_DONT_CARE) 613 xdg_toplevel_set_min_size(window->wl.xdg.toplevel, 614 window->minwidth, window->minheight); 615 if (window->maxwidth != GLFW_DONT_CARE && window->maxheight != GLFW_DONT_CARE) 616 xdg_toplevel_set_max_size(window->wl.xdg.toplevel, 617 window->maxwidth, window->maxheight); 618 619 if (window->monitor) 620 { 621 xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel, 622 window->monitor->wl.output); 623 setIdleInhibitor(window, GLFW_TRUE); 624 } 625 else if (window->wl.maximized) 626 { 627 xdg_toplevel_set_maximized(window->wl.xdg.toplevel); 628 setIdleInhibitor(window, GLFW_FALSE); 629 setXdgDecorations(window); 630 } 631 else 632 { 633 setIdleInhibitor(window, GLFW_FALSE); 634 setXdgDecorations(window); 635 } 636 637 wl_surface_commit(window->wl.surface); 638 wl_display_roundtrip(_glfw.wl.display); 639 640 return GLFW_TRUE; 641 } 642 643 static void setCursorImage(_GLFWwindow* window, 644 _GLFWcursorWayland* cursorWayland) 645 { 646 struct itimerspec timer = {}; 647 struct wl_cursor* wlCursor = cursorWayland->cursor; 648 struct wl_cursor_image* image; 649 struct wl_buffer* buffer; 650 struct wl_surface* surface = _glfw.wl.cursorSurface; 651 int scale = 1; 652 653 if (!wlCursor) 654 buffer = cursorWayland->buffer; 655 else 656 { 657 if (window->wl.scale > 1 && cursorWayland->cursorHiDPI) 658 { 659 wlCursor = cursorWayland->cursorHiDPI; 660 scale = 2; 661 } 662 663 image = wlCursor->images[cursorWayland->currentImage]; 664 buffer = wl_cursor_image_get_buffer(image); 665 if (!buffer) 666 return; 667 668 timer.it_value.tv_sec = image->delay / 1000; 669 timer.it_value.tv_nsec = (image->delay % 1000) * 1000000; 670 timerfd_settime(_glfw.wl.cursorTimerfd, 0, &timer, NULL); 671 672 cursorWayland->width = image->width; 673 cursorWayland->height = image->height; 674 cursorWayland->xhot = image->hotspot_x; 675 cursorWayland->yhot = image->hotspot_y; 676 } 677 678 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial, 679 surface, 680 cursorWayland->xhot / scale, 681 cursorWayland->yhot / scale); 682 wl_surface_set_buffer_scale(surface, scale); 683 wl_surface_attach(surface, buffer, 0, 0); 684 wl_surface_damage(surface, 0, 0, 685 cursorWayland->width, cursorWayland->height); 686 wl_surface_commit(surface); 687 } 688 689 static void incrementCursorImage(_GLFWwindow* window) 690 { 691 _GLFWcursor* cursor; 692 693 if (!window || window->wl.decorations.focus != mainWindow) 694 return; 695 696 cursor = window->wl.currentCursor; 697 if (cursor && cursor->wl.cursor) 698 { 699 cursor->wl.currentImage += 1; 700 cursor->wl.currentImage %= cursor->wl.cursor->image_count; 701 setCursorImage(window, &cursor->wl); 702 } 703 } 704 705 static void handleEvents(int timeout) 706 { 707 struct wl_display* display = _glfw.wl.display; 708 struct pollfd fds[] = { 709 { wl_display_get_fd(display), POLLIN }, 710 { _glfw.wl.timerfd, POLLIN }, 711 { _glfw.wl.cursorTimerfd, POLLIN }, 712 }; 713 ssize_t read_ret; 714 uint64_t repeats, i; 715 716 while (wl_display_prepare_read(display) != 0) 717 wl_display_dispatch_pending(display); 718 719 // If an error different from EAGAIN happens, we have likely been 720 // disconnected from the Wayland session, try to handle that the best we 721 // can. 722 if (wl_display_flush(display) < 0 && errno != EAGAIN) 723 { 724 _GLFWwindow* window = _glfw.windowListHead; 725 while (window) 726 { 727 _glfwInputWindowCloseRequest(window); 728 window = window->next; 729 } 730 wl_display_cancel_read(display); 731 return; 732 } 733 734 if (poll(fds, 3, timeout) > 0) 735 { 736 if (fds[0].revents & POLLIN) 737 { 738 wl_display_read_events(display); 739 wl_display_dispatch_pending(display); 740 } 741 else 742 { 743 wl_display_cancel_read(display); 744 } 745 746 if (fds[1].revents & POLLIN) 747 { 748 read_ret = read(_glfw.wl.timerfd, &repeats, sizeof(repeats)); 749 if (read_ret != 8) 750 return; 751 752 for (i = 0; i < repeats; ++i) 753 _glfwInputKey(_glfw.wl.keyboardFocus, _glfw.wl.keyboardLastKey, 754 _glfw.wl.keyboardLastScancode, GLFW_REPEAT, 755 _glfw.wl.xkb.modifiers); 756 } 757 758 if (fds[2].revents & POLLIN) 759 { 760 read_ret = read(_glfw.wl.cursorTimerfd, &repeats, sizeof(repeats)); 761 if (read_ret != 8) 762 return; 763 764 incrementCursorImage(_glfw.wl.pointerFocus); 765 } 766 } 767 else 768 { 769 wl_display_cancel_read(display); 770 } 771 } 772 773 ////////////////////////////////////////////////////////////////////////// 774 ////// GLFW platform API ////// 775 ////////////////////////////////////////////////////////////////////////// 776 777 int _glfwPlatformCreateWindow(_GLFWwindow* window, 778 const _GLFWwndconfig* wndconfig, 779 const _GLFWctxconfig* ctxconfig, 780 const _GLFWfbconfig* fbconfig) 781 { 782 window->wl.transparent = fbconfig->transparent; 783 784 if (!createSurface(window, wndconfig)) 785 return GLFW_FALSE; 786 787 if (ctxconfig->client != GLFW_NO_API) 788 { 789 if (ctxconfig->source == GLFW_EGL_CONTEXT_API || 790 ctxconfig->source == GLFW_NATIVE_CONTEXT_API) 791 { 792 if (!_glfwInitEGL()) 793 return GLFW_FALSE; 794 if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig)) 795 return GLFW_FALSE; 796 } 797 else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API) 798 { 799 if (!_glfwInitOSMesa()) 800 return GLFW_FALSE; 801 if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig)) 802 return GLFW_FALSE; 803 } 804 } 805 806 if (wndconfig->title) 807 window->wl.title = _glfw_strdup(wndconfig->title); 808 809 if (wndconfig->visible) 810 { 811 if (!createXdgSurface(window)) 812 return GLFW_FALSE; 813 814 window->wl.visible = GLFW_TRUE; 815 } 816 else 817 { 818 window->wl.xdg.surface = NULL; 819 window->wl.xdg.toplevel = NULL; 820 window->wl.visible = GLFW_FALSE; 821 } 822 823 window->wl.currentCursor = NULL; 824 825 window->wl.monitors = calloc(1, sizeof(_GLFWmonitor*)); 826 window->wl.monitorsCount = 0; 827 window->wl.monitorsSize = 1; 828 829 return GLFW_TRUE; 830 } 831 832 void _glfwPlatformDestroyWindow(_GLFWwindow* window) 833 { 834 if (window == _glfw.wl.pointerFocus) 835 { 836 _glfw.wl.pointerFocus = NULL; 837 _glfwInputCursorEnter(window, GLFW_FALSE); 838 } 839 if (window == _glfw.wl.keyboardFocus) 840 { 841 _glfw.wl.keyboardFocus = NULL; 842 _glfwInputWindowFocus(window, GLFW_FALSE); 843 } 844 845 if (window->wl.idleInhibitor) 846 zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor); 847 848 if (window->context.destroy) 849 window->context.destroy(window); 850 851 destroyDecorations(window); 852 if (window->wl.xdg.decoration) 853 zxdg_toplevel_decoration_v1_destroy(window->wl.xdg.decoration); 854 855 if (window->wl.decorations.buffer) 856 wl_buffer_destroy(window->wl.decorations.buffer); 857 858 if (window->wl.native) 859 wl_egl_window_destroy(window->wl.native); 860 861 if (window->wl.xdg.toplevel) 862 xdg_toplevel_destroy(window->wl.xdg.toplevel); 863 864 if (window->wl.xdg.surface) 865 xdg_surface_destroy(window->wl.xdg.surface); 866 867 if (window->wl.surface) 868 wl_surface_destroy(window->wl.surface); 869 870 free(window->wl.title); 871 free(window->wl.monitors); 872 } 873 874 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title) 875 { 876 if (window->wl.title) 877 free(window->wl.title); 878 window->wl.title = _glfw_strdup(title); 879 if (window->wl.xdg.toplevel) 880 xdg_toplevel_set_title(window->wl.xdg.toplevel, title); 881 } 882 883 void _glfwPlatformSetWindowIcon(_GLFWwindow* window, 884 int count, const GLFWimage* images) 885 { 886 _glfwInputError(GLFW_PLATFORM_ERROR, 887 "Wayland: Setting window icon not supported"); 888 } 889 890 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos) 891 { 892 // A Wayland client is not aware of its position, so just warn and leave it 893 // as (0, 0) 894 895 _glfwInputError(GLFW_PLATFORM_ERROR, 896 "Wayland: Window position retrieval not supported"); 897 } 898 899 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos) 900 { 901 // A Wayland client can not set its position, so just warn 902 903 _glfwInputError(GLFW_PLATFORM_ERROR, 904 "Wayland: Window position setting not supported"); 905 } 906 907 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height) 908 { 909 if (width) 910 *width = window->wl.width; 911 if (height) 912 *height = window->wl.height; 913 } 914 915 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height) 916 { 917 window->wl.width = width; 918 window->wl.height = height; 919 resizeWindow(window); 920 } 921 922 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window, 923 int minwidth, int minheight, 924 int maxwidth, int maxheight) 925 { 926 if (window->wl.xdg.toplevel) 927 { 928 if (minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE) 929 minwidth = minheight = 0; 930 if (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE) 931 maxwidth = maxheight = 0; 932 xdg_toplevel_set_min_size(window->wl.xdg.toplevel, minwidth, minheight); 933 xdg_toplevel_set_max_size(window->wl.xdg.toplevel, maxwidth, maxheight); 934 wl_surface_commit(window->wl.surface); 935 } 936 } 937 938 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window, 939 int numer, int denom) 940 { 941 // TODO: find out how to trigger a resize. 942 // The actual limits are checked in the xdg_toplevel::configure handler. 943 } 944 945 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window, 946 int* width, int* height) 947 { 948 _glfwPlatformGetWindowSize(window, width, height); 949 *width *= window->wl.scale; 950 *height *= window->wl.scale; 951 } 952 953 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window, 954 int* left, int* top, 955 int* right, int* bottom) 956 { 957 if (window->decorated && !window->monitor && !window->wl.decorations.serverSide) 958 { 959 if (top) 960 *top = _GLFW_DECORATION_TOP; 961 if (left) 962 *left = _GLFW_DECORATION_WIDTH; 963 if (right) 964 *right = _GLFW_DECORATION_WIDTH; 965 if (bottom) 966 *bottom = _GLFW_DECORATION_WIDTH; 967 } 968 } 969 970 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window, 971 float* xscale, float* yscale) 972 { 973 if (xscale) 974 *xscale = (float) window->wl.scale; 975 if (yscale) 976 *yscale = (float) window->wl.scale; 977 } 978 979 void _glfwPlatformIconifyWindow(_GLFWwindow* window) 980 { 981 if (window->wl.xdg.toplevel) 982 xdg_toplevel_set_minimized(window->wl.xdg.toplevel); 983 } 984 985 void _glfwPlatformRestoreWindow(_GLFWwindow* window) 986 { 987 if (window->wl.xdg.toplevel) 988 { 989 if (window->monitor) 990 xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel); 991 if (window->wl.maximized) 992 xdg_toplevel_unset_maximized(window->wl.xdg.toplevel); 993 // There is no way to unset minimized, or even to know if we are 994 // minimized, so there is nothing to do in this case. 995 } 996 _glfwInputWindowMonitor(window, NULL); 997 window->wl.maximized = GLFW_FALSE; 998 } 999 1000 void _glfwPlatformMaximizeWindow(_GLFWwindow* window) 1001 { 1002 if (window->wl.xdg.toplevel) 1003 { 1004 xdg_toplevel_set_maximized(window->wl.xdg.toplevel); 1005 } 1006 window->wl.maximized = GLFW_TRUE; 1007 } 1008 1009 void _glfwPlatformShowWindow(_GLFWwindow* window) 1010 { 1011 if (!window->wl.visible) 1012 { 1013 createXdgSurface(window); 1014 window->wl.visible = GLFW_TRUE; 1015 } 1016 } 1017 1018 void _glfwPlatformHideWindow(_GLFWwindow* window) 1019 { 1020 if (window->wl.xdg.toplevel) 1021 { 1022 xdg_toplevel_destroy(window->wl.xdg.toplevel); 1023 xdg_surface_destroy(window->wl.xdg.surface); 1024 window->wl.xdg.toplevel = NULL; 1025 window->wl.xdg.surface = NULL; 1026 } 1027 window->wl.visible = GLFW_FALSE; 1028 } 1029 1030 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window) 1031 { 1032 // TODO 1033 _glfwInputError(GLFW_PLATFORM_ERROR, 1034 "Wayland: Window attention request not implemented yet"); 1035 } 1036 1037 void _glfwPlatformFocusWindow(_GLFWwindow* window) 1038 { 1039 _glfwInputError(GLFW_PLATFORM_ERROR, 1040 "Wayland: Focusing a window requires user interaction"); 1041 } 1042 1043 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window, 1044 _GLFWmonitor* monitor, 1045 int xpos, int ypos, 1046 int width, int height, 1047 int refreshRate) 1048 { 1049 if (monitor) 1050 { 1051 setFullscreen(window, monitor, refreshRate); 1052 } 1053 else 1054 { 1055 if (window->wl.xdg.toplevel) 1056 xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel); 1057 setIdleInhibitor(window, GLFW_FALSE); 1058 if (!_glfw.wl.decorationManager) 1059 createDecorations(window); 1060 } 1061 _glfwInputWindowMonitor(window, monitor); 1062 } 1063 1064 int _glfwPlatformWindowFocused(_GLFWwindow* window) 1065 { 1066 return _glfw.wl.keyboardFocus == window; 1067 } 1068 1069 int _glfwPlatformWindowIconified(_GLFWwindow* window) 1070 { 1071 // xdg-shell doesn’t give any way to request whether a surface is 1072 // iconified. 1073 return GLFW_FALSE; 1074 } 1075 1076 int _glfwPlatformWindowVisible(_GLFWwindow* window) 1077 { 1078 return window->wl.visible; 1079 } 1080 1081 int _glfwPlatformWindowMaximized(_GLFWwindow* window) 1082 { 1083 return window->wl.maximized; 1084 } 1085 1086 int _glfwPlatformWindowHovered(_GLFWwindow* window) 1087 { 1088 return window->wl.hovered; 1089 } 1090 1091 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window) 1092 { 1093 return window->wl.transparent; 1094 } 1095 1096 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled) 1097 { 1098 // TODO 1099 _glfwInputError(GLFW_PLATFORM_ERROR, 1100 "Wayland: Window attribute setting not implemented yet"); 1101 } 1102 1103 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled) 1104 { 1105 if (!window->monitor) 1106 { 1107 if (enabled) 1108 createDecorations(window); 1109 else 1110 destroyDecorations(window); 1111 } 1112 } 1113 1114 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled) 1115 { 1116 // TODO 1117 _glfwInputError(GLFW_PLATFORM_ERROR, 1118 "Wayland: Window attribute setting not implemented yet"); 1119 } 1120 1121 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window) 1122 { 1123 return 1.f; 1124 } 1125 1126 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity) 1127 { 1128 } 1129 1130 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled) 1131 { 1132 // This is handled in relativePointerHandleRelativeMotion 1133 } 1134 1135 GLFWbool _glfwPlatformRawMouseMotionSupported(void) 1136 { 1137 return GLFW_TRUE; 1138 } 1139 1140 void _glfwPlatformPollEvents(void) 1141 { 1142 handleEvents(0); 1143 } 1144 1145 void _glfwPlatformWaitEvents(void) 1146 { 1147 handleEvents(-1); 1148 } 1149 1150 void _glfwPlatformWaitEventsTimeout(double timeout) 1151 { 1152 handleEvents((int) (timeout * 1e3)); 1153 } 1154 1155 void _glfwPlatformPostEmptyEvent(void) 1156 { 1157 wl_display_sync(_glfw.wl.display); 1158 } 1159 1160 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos) 1161 { 1162 if (xpos) 1163 *xpos = window->wl.cursorPosX; 1164 if (ypos) 1165 *ypos = window->wl.cursorPosY; 1166 } 1167 1168 static GLFWbool isPointerLocked(_GLFWwindow* window); 1169 1170 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y) 1171 { 1172 if (isPointerLocked(window)) 1173 { 1174 zwp_locked_pointer_v1_set_cursor_position_hint( 1175 window->wl.pointerLock.lockedPointer, 1176 wl_fixed_from_double(x), wl_fixed_from_double(y)); 1177 wl_surface_commit(window->wl.surface); 1178 } 1179 } 1180 1181 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode) 1182 { 1183 _glfwPlatformSetCursor(window, window->wl.currentCursor); 1184 } 1185 1186 const char* _glfwPlatformGetScancodeName(int scancode) 1187 { 1188 // TODO 1189 return NULL; 1190 } 1191 1192 int _glfwPlatformGetKeyScancode(int key) 1193 { 1194 return _glfw.wl.scancodes[key]; 1195 } 1196 1197 int _glfwPlatformCreateCursor(_GLFWcursor* cursor, 1198 const GLFWimage* image, 1199 int xhot, int yhot) 1200 { 1201 cursor->wl.buffer = createShmBuffer(image); 1202 if (!cursor->wl.buffer) 1203 return GLFW_FALSE; 1204 1205 cursor->wl.width = image->width; 1206 cursor->wl.height = image->height; 1207 cursor->wl.xhot = xhot; 1208 cursor->wl.yhot = yhot; 1209 return GLFW_TRUE; 1210 } 1211 1212 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape) 1213 { 1214 const char* name = NULL; 1215 1216 // Try the XDG names first 1217 if (shape == GLFW_ARROW_CURSOR) 1218 name = "default"; 1219 else if (shape == GLFW_IBEAM_CURSOR) 1220 name = "text"; 1221 else if (shape == GLFW_CROSSHAIR_CURSOR) 1222 name = "crosshair"; 1223 else if (shape == GLFW_POINTING_HAND_CURSOR) 1224 name = "pointer"; 1225 else if (shape == GLFW_RESIZE_EW_CURSOR) 1226 name = "ew-resize"; 1227 else if (shape == GLFW_RESIZE_NS_CURSOR) 1228 name = "ns-resize"; 1229 else if (shape == GLFW_RESIZE_NWSE_CURSOR) 1230 name = "nwse-resize"; 1231 else if (shape == GLFW_RESIZE_NESW_CURSOR) 1232 name = "nesw-resize"; 1233 else if (shape == GLFW_RESIZE_ALL_CURSOR) 1234 name = "all-scroll"; 1235 else if (shape == GLFW_NOT_ALLOWED_CURSOR) 1236 name = "not-allowed"; 1237 1238 cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name); 1239 1240 if (_glfw.wl.cursorThemeHiDPI) 1241 { 1242 cursor->wl.cursorHiDPI = 1243 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name); 1244 } 1245 1246 if (!cursor->wl.cursor) 1247 { 1248 // Fall back to the core X11 names 1249 if (shape == GLFW_ARROW_CURSOR) 1250 name = "left_ptr"; 1251 else if (shape == GLFW_IBEAM_CURSOR) 1252 name = "xterm"; 1253 else if (shape == GLFW_CROSSHAIR_CURSOR) 1254 name = "crosshair"; 1255 else if (shape == GLFW_POINTING_HAND_CURSOR) 1256 name = "hand2"; 1257 else if (shape == GLFW_RESIZE_EW_CURSOR) 1258 name = "sb_h_double_arrow"; 1259 else if (shape == GLFW_RESIZE_NS_CURSOR) 1260 name = "sb_v_double_arrow"; 1261 else if (shape == GLFW_RESIZE_ALL_CURSOR) 1262 name = "fleur"; 1263 else 1264 { 1265 _glfwInputError(GLFW_CURSOR_UNAVAILABLE, 1266 "Wayland: Standard cursor shape unavailable"); 1267 return GLFW_FALSE; 1268 } 1269 1270 cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name); 1271 if (!cursor->wl.cursor) 1272 { 1273 _glfwInputError(GLFW_PLATFORM_ERROR, 1274 "Wayland: Failed to create standard cursor \"%s\"", 1275 name); 1276 return GLFW_FALSE; 1277 } 1278 1279 if (_glfw.wl.cursorThemeHiDPI) 1280 { 1281 if (!cursor->wl.cursorHiDPI) 1282 { 1283 cursor->wl.cursorHiDPI = 1284 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name); 1285 } 1286 } 1287 } 1288 1289 return GLFW_TRUE; 1290 } 1291 1292 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor) 1293 { 1294 // If it's a standard cursor we don't need to do anything here 1295 if (cursor->wl.cursor) 1296 return; 1297 1298 if (cursor->wl.buffer) 1299 wl_buffer_destroy(cursor->wl.buffer); 1300 } 1301 1302 static void relativePointerHandleRelativeMotion(void* data, 1303 struct zwp_relative_pointer_v1* pointer, 1304 uint32_t timeHi, 1305 uint32_t timeLo, 1306 wl_fixed_t dx, 1307 wl_fixed_t dy, 1308 wl_fixed_t dxUnaccel, 1309 wl_fixed_t dyUnaccel) 1310 { 1311 _GLFWwindow* window = data; 1312 double xpos = window->virtualCursorPosX; 1313 double ypos = window->virtualCursorPosY; 1314 1315 if (window->cursorMode != GLFW_CURSOR_DISABLED) 1316 return; 1317 1318 if (window->rawMouseMotion) 1319 { 1320 xpos += wl_fixed_to_double(dxUnaccel); 1321 ypos += wl_fixed_to_double(dyUnaccel); 1322 } 1323 else 1324 { 1325 xpos += wl_fixed_to_double(dx); 1326 ypos += wl_fixed_to_double(dy); 1327 } 1328 1329 _glfwInputCursorPos(window, xpos, ypos); 1330 } 1331 1332 static const struct zwp_relative_pointer_v1_listener relativePointerListener = { 1333 relativePointerHandleRelativeMotion 1334 }; 1335 1336 static void lockedPointerHandleLocked(void* data, 1337 struct zwp_locked_pointer_v1* lockedPointer) 1338 { 1339 } 1340 1341 static void unlockPointer(_GLFWwindow* window) 1342 { 1343 struct zwp_relative_pointer_v1* relativePointer = 1344 window->wl.pointerLock.relativePointer; 1345 struct zwp_locked_pointer_v1* lockedPointer = 1346 window->wl.pointerLock.lockedPointer; 1347 1348 zwp_relative_pointer_v1_destroy(relativePointer); 1349 zwp_locked_pointer_v1_destroy(lockedPointer); 1350 1351 window->wl.pointerLock.relativePointer = NULL; 1352 window->wl.pointerLock.lockedPointer = NULL; 1353 } 1354 1355 static void lockPointer(_GLFWwindow* window); 1356 1357 static void lockedPointerHandleUnlocked(void* data, 1358 struct zwp_locked_pointer_v1* lockedPointer) 1359 { 1360 } 1361 1362 static const struct zwp_locked_pointer_v1_listener lockedPointerListener = { 1363 lockedPointerHandleLocked, 1364 lockedPointerHandleUnlocked 1365 }; 1366 1367 static void lockPointer(_GLFWwindow* window) 1368 { 1369 struct zwp_relative_pointer_v1* relativePointer; 1370 struct zwp_locked_pointer_v1* lockedPointer; 1371 1372 if (!_glfw.wl.relativePointerManager) 1373 { 1374 _glfwInputError(GLFW_PLATFORM_ERROR, 1375 "Wayland: no relative pointer manager"); 1376 return; 1377 } 1378 1379 relativePointer = 1380 zwp_relative_pointer_manager_v1_get_relative_pointer( 1381 _glfw.wl.relativePointerManager, 1382 _glfw.wl.pointer); 1383 zwp_relative_pointer_v1_add_listener(relativePointer, 1384 &relativePointerListener, 1385 window); 1386 1387 lockedPointer = 1388 zwp_pointer_constraints_v1_lock_pointer( 1389 _glfw.wl.pointerConstraints, 1390 window->wl.surface, 1391 _glfw.wl.pointer, 1392 NULL, 1393 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT); 1394 zwp_locked_pointer_v1_add_listener(lockedPointer, 1395 &lockedPointerListener, 1396 window); 1397 1398 window->wl.pointerLock.relativePointer = relativePointer; 1399 window->wl.pointerLock.lockedPointer = lockedPointer; 1400 1401 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial, 1402 NULL, 0, 0); 1403 } 1404 1405 static GLFWbool isPointerLocked(_GLFWwindow* window) 1406 { 1407 return window->wl.pointerLock.lockedPointer != NULL; 1408 } 1409 1410 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor) 1411 { 1412 struct wl_cursor* defaultCursor; 1413 struct wl_cursor* defaultCursorHiDPI = NULL; 1414 1415 if (!_glfw.wl.pointer) 1416 return; 1417 1418 window->wl.currentCursor = cursor; 1419 1420 // If we're not in the correct window just save the cursor 1421 // the next time the pointer enters the window the cursor will change 1422 if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != mainWindow) 1423 return; 1424 1425 // Unlock possible pointer lock if no longer disabled. 1426 if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window)) 1427 unlockPointer(window); 1428 1429 if (window->cursorMode == GLFW_CURSOR_NORMAL) 1430 { 1431 if (cursor) 1432 setCursorImage(window, &cursor->wl); 1433 else 1434 { 1435 defaultCursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, 1436 "left_ptr"); 1437 if (!defaultCursor) 1438 { 1439 _glfwInputError(GLFW_PLATFORM_ERROR, 1440 "Wayland: Standard cursor not found"); 1441 return; 1442 } 1443 if (_glfw.wl.cursorThemeHiDPI) 1444 defaultCursorHiDPI = 1445 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, 1446 "left_ptr"); 1447 _GLFWcursorWayland cursorWayland = { 1448 defaultCursor, 1449 defaultCursorHiDPI, 1450 NULL, 1451 0, 0, 1452 0, 0, 1453 0 1454 }; 1455 setCursorImage(window, &cursorWayland); 1456 } 1457 } 1458 else if (window->cursorMode == GLFW_CURSOR_DISABLED) 1459 { 1460 if (!isPointerLocked(window)) 1461 lockPointer(window); 1462 } 1463 else if (window->cursorMode == GLFW_CURSOR_HIDDEN) 1464 { 1465 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial, NULL, 0, 0); 1466 } 1467 } 1468 1469 static void dataSourceHandleTarget(void* data, 1470 struct wl_data_source* dataSource, 1471 const char* mimeType) 1472 { 1473 if (_glfw.wl.dataSource != dataSource) 1474 { 1475 _glfwInputError(GLFW_PLATFORM_ERROR, 1476 "Wayland: Unknown clipboard data source"); 1477 return; 1478 } 1479 } 1480 1481 static void dataSourceHandleSend(void* data, 1482 struct wl_data_source* dataSource, 1483 const char* mimeType, 1484 int fd) 1485 { 1486 const char* string = _glfw.wl.clipboardSendString; 1487 size_t len = _glfw.wl.clipboardSendSize; 1488 int ret; 1489 1490 if (_glfw.wl.dataSource != dataSource) 1491 { 1492 _glfwInputError(GLFW_PLATFORM_ERROR, 1493 "Wayland: Unknown clipboard data source"); 1494 return; 1495 } 1496 1497 if (!string) 1498 { 1499 _glfwInputError(GLFW_PLATFORM_ERROR, 1500 "Wayland: Copy requested from an invalid string"); 1501 return; 1502 } 1503 1504 if (strcmp(mimeType, "text/plain;charset=utf-8") != 0) 1505 { 1506 _glfwInputError(GLFW_PLATFORM_ERROR, 1507 "Wayland: Wrong MIME type asked from clipboard"); 1508 close(fd); 1509 return; 1510 } 1511 1512 while (len > 0) 1513 { 1514 ret = write(fd, string, len); 1515 if (ret == -1 && errno == EINTR) 1516 continue; 1517 if (ret == -1) 1518 { 1519 // TODO: also report errno maybe. 1520 _glfwInputError(GLFW_PLATFORM_ERROR, 1521 "Wayland: Error while writing the clipboard"); 1522 close(fd); 1523 return; 1524 } 1525 len -= ret; 1526 } 1527 close(fd); 1528 } 1529 1530 static void dataSourceHandleCancelled(void* data, 1531 struct wl_data_source* dataSource) 1532 { 1533 wl_data_source_destroy(dataSource); 1534 1535 if (_glfw.wl.dataSource != dataSource) 1536 { 1537 _glfwInputError(GLFW_PLATFORM_ERROR, 1538 "Wayland: Unknown clipboard data source"); 1539 return; 1540 } 1541 1542 _glfw.wl.dataSource = NULL; 1543 } 1544 1545 static const struct wl_data_source_listener dataSourceListener = { 1546 dataSourceHandleTarget, 1547 dataSourceHandleSend, 1548 dataSourceHandleCancelled, 1549 }; 1550 1551 void _glfwPlatformSetClipboardString(const char* string) 1552 { 1553 if (_glfw.wl.dataSource) 1554 { 1555 wl_data_source_destroy(_glfw.wl.dataSource); 1556 _glfw.wl.dataSource = NULL; 1557 } 1558 1559 if (_glfw.wl.clipboardSendString) 1560 { 1561 free(_glfw.wl.clipboardSendString); 1562 _glfw.wl.clipboardSendString = NULL; 1563 } 1564 1565 _glfw.wl.clipboardSendString = strdup(string); 1566 if (!_glfw.wl.clipboardSendString) 1567 { 1568 _glfwInputError(GLFW_PLATFORM_ERROR, 1569 "Wayland: Impossible to allocate clipboard string"); 1570 return; 1571 } 1572 _glfw.wl.clipboardSendSize = strlen(string); 1573 _glfw.wl.dataSource = 1574 wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager); 1575 if (!_glfw.wl.dataSource) 1576 { 1577 _glfwInputError(GLFW_PLATFORM_ERROR, 1578 "Wayland: Impossible to create clipboard source"); 1579 free(_glfw.wl.clipboardSendString); 1580 return; 1581 } 1582 wl_data_source_add_listener(_glfw.wl.dataSource, 1583 &dataSourceListener, 1584 NULL); 1585 wl_data_source_offer(_glfw.wl.dataSource, "text/plain;charset=utf-8"); 1586 wl_data_device_set_selection(_glfw.wl.dataDevice, 1587 _glfw.wl.dataSource, 1588 _glfw.wl.serial); 1589 } 1590 1591 static GLFWbool growClipboardString(void) 1592 { 1593 char* clipboard = _glfw.wl.clipboardString; 1594 1595 clipboard = realloc(clipboard, _glfw.wl.clipboardSize * 2); 1596 if (!clipboard) 1597 { 1598 _glfwInputError(GLFW_PLATFORM_ERROR, 1599 "Wayland: Impossible to grow clipboard string"); 1600 return GLFW_FALSE; 1601 } 1602 _glfw.wl.clipboardString = clipboard; 1603 _glfw.wl.clipboardSize = _glfw.wl.clipboardSize * 2; 1604 return GLFW_TRUE; 1605 } 1606 1607 const char* _glfwPlatformGetClipboardString(void) 1608 { 1609 int fds[2]; 1610 int ret; 1611 size_t len = 0; 1612 1613 if (!_glfw.wl.dataOffer) 1614 { 1615 _glfwInputError(GLFW_FORMAT_UNAVAILABLE, 1616 "No clipboard data has been sent yet"); 1617 return NULL; 1618 } 1619 1620 ret = pipe2(fds, O_CLOEXEC); 1621 if (ret < 0) 1622 { 1623 // TODO: also report errno maybe? 1624 _glfwInputError(GLFW_PLATFORM_ERROR, 1625 "Wayland: Impossible to create clipboard pipe fds"); 1626 return NULL; 1627 } 1628 1629 wl_data_offer_receive(_glfw.wl.dataOffer, "text/plain;charset=utf-8", fds[1]); 1630 close(fds[1]); 1631 1632 // XXX: this is a huge hack, this function shouldn’t be synchronous! 1633 handleEvents(-1); 1634 1635 while (1) 1636 { 1637 // Grow the clipboard if we need to paste something bigger, there is no 1638 // shrink operation yet. 1639 if (len + 4096 > _glfw.wl.clipboardSize) 1640 { 1641 if (!growClipboardString()) 1642 { 1643 close(fds[0]); 1644 return NULL; 1645 } 1646 } 1647 1648 // Then read from the fd to the clipboard, handling all known errors. 1649 ret = read(fds[0], _glfw.wl.clipboardString + len, 4096); 1650 if (ret == 0) 1651 break; 1652 if (ret == -1 && errno == EINTR) 1653 continue; 1654 if (ret == -1) 1655 { 1656 // TODO: also report errno maybe. 1657 _glfwInputError(GLFW_PLATFORM_ERROR, 1658 "Wayland: Impossible to read from clipboard fd"); 1659 close(fds[0]); 1660 return NULL; 1661 } 1662 len += ret; 1663 } 1664 close(fds[0]); 1665 if (len + 1 > _glfw.wl.clipboardSize) 1666 { 1667 if (!growClipboardString()) 1668 return NULL; 1669 } 1670 _glfw.wl.clipboardString[len] = '\0'; 1671 return _glfw.wl.clipboardString; 1672 } 1673 1674 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions) 1675 { 1676 if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_wayland_surface) 1677 return; 1678 1679 extensions[0] = "VK_KHR_surface"; 1680 extensions[1] = "VK_KHR_wayland_surface"; 1681 } 1682 1683 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance, 1684 VkPhysicalDevice device, 1685 uint32_t queuefamily) 1686 { 1687 PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR 1688 vkGetPhysicalDeviceWaylandPresentationSupportKHR = 1689 (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR) 1690 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR"); 1691 if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR) 1692 { 1693 _glfwInputError(GLFW_API_UNAVAILABLE, 1694 "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension"); 1695 return VK_NULL_HANDLE; 1696 } 1697 1698 return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device, 1699 queuefamily, 1700 _glfw.wl.display); 1701 } 1702 1703 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance, 1704 _GLFWwindow* window, 1705 const VkAllocationCallbacks* allocator, 1706 VkSurfaceKHR* surface) 1707 { 1708 VkResult err; 1709 VkWaylandSurfaceCreateInfoKHR sci; 1710 PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR; 1711 1712 vkCreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR) 1713 vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR"); 1714 if (!vkCreateWaylandSurfaceKHR) 1715 { 1716 _glfwInputError(GLFW_API_UNAVAILABLE, 1717 "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension"); 1718 return VK_ERROR_EXTENSION_NOT_PRESENT; 1719 } 1720 1721 memset(&sci, 0, sizeof(sci)); 1722 sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR; 1723 sci.display = _glfw.wl.display; 1724 sci.surface = window->wl.surface; 1725 1726 err = vkCreateWaylandSurfaceKHR(instance, &sci, allocator, surface); 1727 if (err) 1728 { 1729 _glfwInputError(GLFW_PLATFORM_ERROR, 1730 "Wayland: Failed to create Vulkan surface: %s", 1731 _glfwGetVulkanResultString(err)); 1732 } 1733 1734 return err; 1735 } 1736 1737 1738 ////////////////////////////////////////////////////////////////////////// 1739 ////// GLFW native API ////// 1740 ////////////////////////////////////////////////////////////////////////// 1741 1742 GLFWAPI struct wl_display* glfwGetWaylandDisplay(void) 1743 { 1744 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1745 return _glfw.wl.display; 1746 } 1747 1748 GLFWAPI struct wl_surface* glfwGetWaylandWindow(GLFWwindow* handle) 1749 { 1750 _GLFWwindow* window = (_GLFWwindow*) handle; 1751 _GLFW_REQUIRE_INIT_OR_RETURN(NULL); 1752 return window->wl.surface; 1753 } 1754