1 //========================================================================
2 // GLFW 3.4 Wayland - www.glfw.org
3 //------------------------------------------------------------------------
4 // Copyright (c) 2014 Jonas Ådahl <jadahl@gmail.com>
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.
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:
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.
19 // 2. Altered source versions must be plainly marked as such, and must not
20 // be misrepresented as being the original software.
22 // 3. This notice may not be removed or altered from any source
25 //========================================================================
26 // It is fine to use C99 in this file because it will not be built with VS
27 //========================================================================
40 #include <sys/timerfd.h>
44 static int createTmpfileCloexec(char* tmpname)
48 fd = mkostemp(tmpname, O_CLOEXEC);
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.
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.
64 * The file name is deleted from the file system.
66 * The file is suitable for buffer sharing between processes by
67 * transmitting the file descriptor over Unix sockets using the
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.
75 static int createAnonymousFile(off_t size)
77 static const char template[] = "/glfw-shared-XXXXXX";
83 #ifdef HAVE_MEMFD_CREATE
84 fd = memfd_create("glfw-shared", MFD_CLOEXEC | MFD_ALLOW_SEALING);
87 // We can add this seal before calling posix_fallocate(), as the file
88 // is currently zero-sized anyway.
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);
95 #elif defined(SHM_ANON)
96 fd = shm_open(SHM_ANON, O_RDWR | O_CLOEXEC, 0600);
100 path = getenv("XDG_RUNTIME_DIR");
107 name = calloc(strlen(path) + sizeof(template), 1);
109 strcat(name, template);
111 fd = createTmpfileCloexec(name);
117 #if defined(SHM_ANON)
118 // posix_fallocate does not work on SHM descriptors
119 ret = ftruncate(fd, size);
121 ret = posix_fallocate(fd, 0, size);
132 static struct wl_buffer* createShmBuffer(const GLFWimage* image)
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;
141 fd = createAnonymousFile(length);
144 _glfwInputError(GLFW_PLATFORM_ERROR,
145 "Wayland: Creating a buffer file for %d B failed: %s",
146 length, strerror(errno));
150 data = mmap(NULL, length, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
151 if (data == MAP_FAILED)
153 _glfwInputError(GLFW_PLATFORM_ERROR,
154 "Wayland: mmap failed: %s", strerror(errno));
159 pool = wl_shm_create_pool(_glfw.wl.shm, fd, length);
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)
166 unsigned int alpha = source[3];
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;
175 wl_shm_pool_create_buffer(pool, 0,
178 stride, WL_SHM_FORMAT_ARGB8888);
179 munmap(data, length);
180 wl_shm_pool_destroy(pool);
185 static void createDecoration(_GLFWdecorationWayland* decoration,
186 struct wl_surface* parent,
187 struct wl_buffer* buffer, GLFWbool opaque,
189 int width, int height)
191 struct wl_region* region;
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);
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);
212 wl_surface_commit(decoration->surface);
215 static void createDecorations(_GLFWwindow* window)
217 unsigned char data[] = { 224, 224, 224, 255 };
218 const GLFWimage image = { 1, 1, data };
219 GLFWbool opaque = (data[3] == 255);
221 if (!_glfw.wl.viewporter || !window->decorated || window->wl.decorations.serverSide)
224 if (!window->wl.decorations.buffer)
225 window->wl.decorations.buffer = createShmBuffer(&image);
226 if (!window->wl.decorations.buffer)
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);
247 static void destroyDecoration(_GLFWdecorationWayland* decoration)
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;
260 static void destroyDecorations(_GLFWwindow* window)
262 destroyDecoration(&window->wl.decorations.top);
263 destroyDecoration(&window->wl.decorations.left);
264 destroyDecoration(&window->wl.decorations.right);
265 destroyDecoration(&window->wl.decorations.bottom);
268 static void xdgDecorationHandleConfigure(void* data,
269 struct zxdg_toplevel_decoration_v1* decoration,
272 _GLFWwindow* window = data;
274 window->wl.decorations.serverSide = (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
276 if (!window->wl.decorations.serverSide)
277 createDecorations(window);
280 static const struct zxdg_toplevel_decoration_v1_listener xdgDecorationListener = {
281 xdgDecorationHandleConfigure,
284 // Makes the surface considered as XRGB instead of ARGB.
285 static void setOpaqueRegion(_GLFWwindow* window)
287 struct wl_region* region;
289 region = wl_compositor_create_region(_glfw.wl.compositor);
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);
300 static void resizeWindow(_GLFWwindow* window)
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);
311 if (!window->wl.decorations.top.surface)
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);
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);
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);
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);
339 static void checkScaleChange(_GLFWwindow* window)
345 // Check if we will be able to set the buffer scale or not.
346 if (_glfw.wl.compositorVersion < 3)
349 // Get the scale factor from the highest scale monitor.
350 for (i = 0; i < window->wl.monitorsCount; ++i)
352 monitorScale = window->wl.monitors[i]->wl.scale;
353 if (scale < monitorScale)
354 scale = monitorScale;
357 // Only change the framebuffer size if the scale changed.
358 if (scale != window->wl.scale)
360 window->wl.scale = scale;
361 wl_surface_set_buffer_scale(window->wl.surface, scale);
362 resizeWindow(window);
366 static void surfaceHandleEnter(void *data,
367 struct wl_surface *surface,
368 struct wl_output *output)
370 _GLFWwindow* window = data;
371 _GLFWmonitor* monitor = wl_output_get_user_data(output);
373 if (window->wl.monitorsCount + 1 > window->wl.monitorsSize)
375 ++window->wl.monitorsSize;
376 window->wl.monitors =
377 realloc(window->wl.monitors,
378 window->wl.monitorsSize * sizeof(_GLFWmonitor*));
381 window->wl.monitors[window->wl.monitorsCount++] = monitor;
383 checkScaleChange(window);
386 static void surfaceHandleLeave(void *data,
387 struct wl_surface *surface,
388 struct wl_output *output)
390 _GLFWwindow* window = data;
391 _GLFWmonitor* monitor = wl_output_get_user_data(output);
395 for (i = 0, found = GLFW_FALSE; i < window->wl.monitorsCount - 1; ++i)
397 if (monitor == window->wl.monitors[i])
400 window->wl.monitors[i] = window->wl.monitors[i + 1];
402 window->wl.monitors[--window->wl.monitorsCount] = NULL;
404 checkScaleChange(window);
407 static const struct wl_surface_listener surfaceListener = {
412 static void setIdleInhibitor(_GLFWwindow* window, GLFWbool enable)
414 if (enable && !window->wl.idleInhibitor && _glfw.wl.idleInhibitManager)
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");
423 else if (!enable && window->wl.idleInhibitor)
425 zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor);
426 window->wl.idleInhibitor = NULL;
430 static GLFWbool createSurface(_GLFWwindow* window,
431 const _GLFWwndconfig* wndconfig)
433 window->wl.surface = wl_compositor_create_surface(_glfw.wl.compositor);
434 if (!window->wl.surface)
437 wl_surface_add_listener(window->wl.surface,
441 wl_surface_set_user_data(window->wl.surface, window);
443 window->wl.native = wl_egl_window_create(window->wl.surface,
446 if (!window->wl.native)
449 window->wl.width = wndconfig->width;
450 window->wl.height = wndconfig->height;
451 window->wl.scale = 1;
453 if (!window->wl.transparent)
454 setOpaqueRegion(window);
459 static void setFullscreen(_GLFWwindow* window, _GLFWmonitor* monitor,
462 if (window->wl.xdg.toplevel)
464 xdg_toplevel_set_fullscreen(
465 window->wl.xdg.toplevel,
468 setIdleInhibitor(window, GLFW_TRUE);
469 if (!window->wl.decorations.serverSide)
470 destroyDecorations(window);
473 static void xdgToplevelHandleConfigure(void* data,
474 struct xdg_toplevel* toplevel,
477 struct wl_array* states)
479 _GLFWwindow* window = data;
483 GLFWbool maximized = GLFW_FALSE;
484 GLFWbool fullscreen = GLFW_FALSE;
485 GLFWbool activated = GLFW_FALSE;
487 wl_array_for_each(state, states)
491 case XDG_TOPLEVEL_STATE_MAXIMIZED:
492 maximized = GLFW_TRUE;
494 case XDG_TOPLEVEL_STATE_FULLSCREEN:
495 fullscreen = GLFW_TRUE;
497 case XDG_TOPLEVEL_STATE_RESIZING:
499 case XDG_TOPLEVEL_STATE_ACTIVATED:
500 activated = GLFW_TRUE;
505 if (width != 0 && height != 0)
507 if (!maximized && !fullscreen)
509 if (window->numer != GLFW_DONT_CARE && window->denom != GLFW_DONT_CARE)
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;
520 _glfwInputWindowSize(window, width, height);
521 _glfwPlatformSetWindowSize(window, width, height);
522 _glfwInputWindowDamage(window);
525 if (window->wl.wasFullscreen && window->autoIconify)
527 if (!activated || !fullscreen)
529 _glfwPlatformIconifyWindow(window);
530 window->wl.wasFullscreen = GLFW_FALSE;
533 if (fullscreen && activated)
534 window->wl.wasFullscreen = GLFW_TRUE;
535 _glfwInputWindowFocus(window, activated);
538 static void xdgToplevelHandleClose(void* data,
539 struct xdg_toplevel* toplevel)
541 _GLFWwindow* window = data;
542 _glfwInputWindowCloseRequest(window);
545 static const struct xdg_toplevel_listener xdgToplevelListener = {
546 xdgToplevelHandleConfigure,
547 xdgToplevelHandleClose
550 static void xdgSurfaceHandleConfigure(void* data,
551 struct xdg_surface* surface,
554 xdg_surface_ack_configure(surface, serial);
557 static const struct xdg_surface_listener xdgSurfaceListener = {
558 xdgSurfaceHandleConfigure
561 static void setXdgDecorations(_GLFWwindow* window)
563 if (_glfw.wl.decorationManager)
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,
571 zxdg_toplevel_decoration_v1_set_mode(
572 window->wl.xdg.decoration,
573 ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE);
577 window->wl.decorations.serverSide = GLFW_FALSE;
578 createDecorations(window);
582 static GLFWbool createXdgSurface(_GLFWwindow* window)
584 window->wl.xdg.surface = xdg_wm_base_get_xdg_surface(_glfw.wl.wmBase,
586 if (!window->wl.xdg.surface)
588 _glfwInputError(GLFW_PLATFORM_ERROR,
589 "Wayland: xdg-surface creation failed");
593 xdg_surface_add_listener(window->wl.xdg.surface,
597 window->wl.xdg.toplevel = xdg_surface_get_toplevel(window->wl.xdg.surface);
598 if (!window->wl.xdg.toplevel)
600 _glfwInputError(GLFW_PLATFORM_ERROR,
601 "Wayland: xdg-toplevel creation failed");
605 xdg_toplevel_add_listener(window->wl.xdg.toplevel,
606 &xdgToplevelListener,
609 if (window->wl.title)
610 xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title);
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);
621 xdg_toplevel_set_fullscreen(window->wl.xdg.toplevel,
622 window->monitor->wl.output);
623 setIdleInhibitor(window, GLFW_TRUE);
625 else if (window->wl.maximized)
627 xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
628 setIdleInhibitor(window, GLFW_FALSE);
629 setXdgDecorations(window);
633 setIdleInhibitor(window, GLFW_FALSE);
634 setXdgDecorations(window);
637 wl_surface_commit(window->wl.surface);
638 wl_display_roundtrip(_glfw.wl.display);
643 static void setCursorImage(_GLFWwindow* window,
644 _GLFWcursorWayland* cursorWayland)
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;
654 buffer = cursorWayland->buffer;
657 if (window->wl.scale > 1 && cursorWayland->cursorHiDPI)
659 wlCursor = cursorWayland->cursorHiDPI;
663 image = wlCursor->images[cursorWayland->currentImage];
664 buffer = wl_cursor_image_get_buffer(image);
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);
672 cursorWayland->width = image->width;
673 cursorWayland->height = image->height;
674 cursorWayland->xhot = image->hotspot_x;
675 cursorWayland->yhot = image->hotspot_y;
678 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial,
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);
689 static void incrementCursorImage(_GLFWwindow* window)
693 if (!window || window->wl.decorations.focus != mainWindow)
696 cursor = window->wl.currentCursor;
697 if (cursor && cursor->wl.cursor)
699 cursor->wl.currentImage += 1;
700 cursor->wl.currentImage %= cursor->wl.cursor->image_count;
701 setCursorImage(window, &cursor->wl);
705 static void handleEvents(int timeout)
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 },
716 while (wl_display_prepare_read(display) != 0)
717 wl_display_dispatch_pending(display);
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
722 if (wl_display_flush(display) < 0 && errno != EAGAIN)
724 _GLFWwindow* window = _glfw.windowListHead;
727 _glfwInputWindowCloseRequest(window);
728 window = window->next;
730 wl_display_cancel_read(display);
734 if (poll(fds, 3, timeout) > 0)
736 if (fds[0].revents & POLLIN)
738 wl_display_read_events(display);
739 wl_display_dispatch_pending(display);
743 wl_display_cancel_read(display);
746 if (fds[1].revents & POLLIN)
748 read_ret = read(_glfw.wl.timerfd, &repeats, sizeof(repeats));
752 if (_glfw.wl.keyboardFocus)
754 for (i = 0; i < repeats; ++i)
756 _glfwInputKey(_glfw.wl.keyboardFocus,
757 _glfw.wl.keyboardLastKey,
758 _glfw.wl.keyboardLastScancode,
760 _glfw.wl.xkb.modifiers);
765 if (fds[2].revents & POLLIN)
767 read_ret = read(_glfw.wl.cursorTimerfd, &repeats, sizeof(repeats));
771 incrementCursorImage(_glfw.wl.pointerFocus);
776 wl_display_cancel_read(display);
780 //////////////////////////////////////////////////////////////////////////
781 ////// GLFW platform API //////
782 //////////////////////////////////////////////////////////////////////////
784 int _glfwPlatformCreateWindow(_GLFWwindow* window,
785 const _GLFWwndconfig* wndconfig,
786 const _GLFWctxconfig* ctxconfig,
787 const _GLFWfbconfig* fbconfig)
789 window->wl.transparent = fbconfig->transparent;
791 if (!createSurface(window, wndconfig))
794 if (ctxconfig->client != GLFW_NO_API)
796 if (ctxconfig->source == GLFW_EGL_CONTEXT_API ||
797 ctxconfig->source == GLFW_NATIVE_CONTEXT_API)
801 if (!_glfwCreateContextEGL(window, ctxconfig, fbconfig))
804 else if (ctxconfig->source == GLFW_OSMESA_CONTEXT_API)
806 if (!_glfwInitOSMesa())
808 if (!_glfwCreateContextOSMesa(window, ctxconfig, fbconfig))
813 if (wndconfig->title)
814 window->wl.title = _glfw_strdup(wndconfig->title);
816 if (wndconfig->visible)
818 if (!createXdgSurface(window))
821 window->wl.visible = GLFW_TRUE;
825 window->wl.xdg.surface = NULL;
826 window->wl.xdg.toplevel = NULL;
827 window->wl.visible = GLFW_FALSE;
830 window->wl.currentCursor = NULL;
832 window->wl.monitors = calloc(1, sizeof(_GLFWmonitor*));
833 window->wl.monitorsCount = 0;
834 window->wl.monitorsSize = 1;
839 void _glfwPlatformDestroyWindow(_GLFWwindow* window)
841 if (window == _glfw.wl.pointerFocus)
843 _glfw.wl.pointerFocus = NULL;
844 _glfwInputCursorEnter(window, GLFW_FALSE);
846 if (window == _glfw.wl.keyboardFocus)
848 _glfw.wl.keyboardFocus = NULL;
849 _glfwInputWindowFocus(window, GLFW_FALSE);
852 if (window->wl.idleInhibitor)
853 zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor);
855 if (window->context.destroy)
856 window->context.destroy(window);
858 destroyDecorations(window);
859 if (window->wl.xdg.decoration)
860 zxdg_toplevel_decoration_v1_destroy(window->wl.xdg.decoration);
862 if (window->wl.decorations.buffer)
863 wl_buffer_destroy(window->wl.decorations.buffer);
865 if (window->wl.native)
866 wl_egl_window_destroy(window->wl.native);
868 if (window->wl.xdg.toplevel)
869 xdg_toplevel_destroy(window->wl.xdg.toplevel);
871 if (window->wl.xdg.surface)
872 xdg_surface_destroy(window->wl.xdg.surface);
874 if (window->wl.surface)
875 wl_surface_destroy(window->wl.surface);
877 free(window->wl.title);
878 free(window->wl.monitors);
881 void _glfwPlatformSetWindowTitle(_GLFWwindow* window, const char* title)
883 if (window->wl.title)
884 free(window->wl.title);
885 window->wl.title = _glfw_strdup(title);
886 if (window->wl.xdg.toplevel)
887 xdg_toplevel_set_title(window->wl.xdg.toplevel, title);
890 void _glfwPlatformSetWindowIcon(_GLFWwindow* window,
891 int count, const GLFWimage* images)
893 _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
894 "Wayland: The platform does not support setting the window icon");
897 void _glfwPlatformGetWindowPos(_GLFWwindow* window, int* xpos, int* ypos)
899 // A Wayland client is not aware of its position, so just warn and leave it
902 _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
903 "Wayland: The platform does not provide the window position");
906 void _glfwPlatformSetWindowPos(_GLFWwindow* window, int xpos, int ypos)
908 // A Wayland client can not set its position, so just warn
910 _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
911 "Wayland: The platform does not support setting the window position");
914 void _glfwPlatformGetWindowSize(_GLFWwindow* window, int* width, int* height)
917 *width = window->wl.width;
919 *height = window->wl.height;
922 void _glfwPlatformSetWindowSize(_GLFWwindow* window, int width, int height)
924 window->wl.width = width;
925 window->wl.height = height;
926 resizeWindow(window);
929 void _glfwPlatformSetWindowSizeLimits(_GLFWwindow* window,
930 int minwidth, int minheight,
931 int maxwidth, int maxheight)
933 if (window->wl.xdg.toplevel)
935 if (minwidth == GLFW_DONT_CARE || minheight == GLFW_DONT_CARE)
936 minwidth = minheight = 0;
937 if (maxwidth == GLFW_DONT_CARE || maxheight == GLFW_DONT_CARE)
938 maxwidth = maxheight = 0;
939 xdg_toplevel_set_min_size(window->wl.xdg.toplevel, minwidth, minheight);
940 xdg_toplevel_set_max_size(window->wl.xdg.toplevel, maxwidth, maxheight);
941 wl_surface_commit(window->wl.surface);
945 void _glfwPlatformSetWindowAspectRatio(_GLFWwindow* window,
946 int numer, int denom)
948 // TODO: find out how to trigger a resize.
949 // The actual limits are checked in the xdg_toplevel::configure handler.
950 _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
951 "Wayland: Window aspect ratio not yet implemented");
954 void _glfwPlatformGetFramebufferSize(_GLFWwindow* window,
955 int* width, int* height)
957 _glfwPlatformGetWindowSize(window, width, height);
959 *width *= window->wl.scale;
961 *height *= window->wl.scale;
964 void _glfwPlatformGetWindowFrameSize(_GLFWwindow* window,
966 int* right, int* bottom)
968 if (window->decorated && !window->monitor && !window->wl.decorations.serverSide)
971 *top = _GLFW_DECORATION_TOP;
973 *left = _GLFW_DECORATION_WIDTH;
975 *right = _GLFW_DECORATION_WIDTH;
977 *bottom = _GLFW_DECORATION_WIDTH;
981 void _glfwPlatformGetWindowContentScale(_GLFWwindow* window,
982 float* xscale, float* yscale)
985 *xscale = (float) window->wl.scale;
987 *yscale = (float) window->wl.scale;
990 void _glfwPlatformIconifyWindow(_GLFWwindow* window)
992 if (window->wl.xdg.toplevel)
993 xdg_toplevel_set_minimized(window->wl.xdg.toplevel);
996 void _glfwPlatformRestoreWindow(_GLFWwindow* window)
998 if (window->wl.xdg.toplevel)
1000 if (window->monitor)
1001 xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
1002 if (window->wl.maximized)
1003 xdg_toplevel_unset_maximized(window->wl.xdg.toplevel);
1004 // There is no way to unset minimized, or even to know if we are
1005 // minimized, so there is nothing to do in this case.
1007 _glfwInputWindowMonitor(window, NULL);
1008 window->wl.maximized = GLFW_FALSE;
1011 void _glfwPlatformMaximizeWindow(_GLFWwindow* window)
1013 if (window->wl.xdg.toplevel)
1015 xdg_toplevel_set_maximized(window->wl.xdg.toplevel);
1017 window->wl.maximized = GLFW_TRUE;
1020 void _glfwPlatformShowWindow(_GLFWwindow* window)
1022 if (!window->wl.visible)
1024 createXdgSurface(window);
1025 window->wl.visible = GLFW_TRUE;
1029 void _glfwPlatformHideWindow(_GLFWwindow* window)
1031 if (window->wl.xdg.toplevel)
1033 xdg_toplevel_destroy(window->wl.xdg.toplevel);
1034 xdg_surface_destroy(window->wl.xdg.surface);
1035 window->wl.xdg.toplevel = NULL;
1036 window->wl.xdg.surface = NULL;
1038 window->wl.visible = GLFW_FALSE;
1041 void _glfwPlatformRequestWindowAttention(_GLFWwindow* window)
1044 _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
1045 "Wayland: Window attention request not implemented yet");
1048 void _glfwPlatformFocusWindow(_GLFWwindow* window)
1050 _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
1051 "Wayland: The platform does not support setting the input focus");
1054 void _glfwPlatformSetWindowMonitor(_GLFWwindow* window,
1055 _GLFWmonitor* monitor,
1057 int width, int height,
1062 setFullscreen(window, monitor, refreshRate);
1066 if (window->wl.xdg.toplevel)
1067 xdg_toplevel_unset_fullscreen(window->wl.xdg.toplevel);
1068 setIdleInhibitor(window, GLFW_FALSE);
1069 if (!_glfw.wl.decorationManager)
1070 createDecorations(window);
1072 _glfwInputWindowMonitor(window, monitor);
1075 int _glfwPlatformWindowFocused(_GLFWwindow* window)
1077 return _glfw.wl.keyboardFocus == window;
1080 int _glfwPlatformWindowIconified(_GLFWwindow* window)
1082 // xdg-shell doesn’t give any way to request whether a surface is
1087 int _glfwPlatformWindowVisible(_GLFWwindow* window)
1089 return window->wl.visible;
1092 int _glfwPlatformWindowMaximized(_GLFWwindow* window)
1094 return window->wl.maximized;
1097 int _glfwPlatformWindowHovered(_GLFWwindow* window)
1099 return window->wl.hovered;
1102 int _glfwPlatformFramebufferTransparent(_GLFWwindow* window)
1104 return window->wl.transparent;
1107 void _glfwPlatformSetWindowResizable(_GLFWwindow* window, GLFWbool enabled)
1110 _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
1111 "Wayland: Window attribute setting not implemented yet");
1114 void _glfwPlatformSetWindowDecorated(_GLFWwindow* window, GLFWbool enabled)
1116 if (!window->monitor)
1119 createDecorations(window);
1121 destroyDecorations(window);
1125 void _glfwPlatformSetWindowFloating(_GLFWwindow* window, GLFWbool enabled)
1128 _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
1129 "Wayland: Window attribute setting not implemented yet");
1132 void _glfwPlatformSetWindowMousePassthrough(_GLFWwindow* window, GLFWbool enabled)
1136 struct wl_region* region = wl_compositor_create_region(_glfw.wl.compositor);
1137 wl_surface_set_input_region(window->wl.surface, region);
1138 wl_region_destroy(region);
1141 wl_surface_set_input_region(window->wl.surface, 0);
1142 wl_surface_commit(window->wl.surface);
1145 float _glfwPlatformGetWindowOpacity(_GLFWwindow* window)
1150 void _glfwPlatformSetWindowOpacity(_GLFWwindow* window, float opacity)
1152 _glfwInputError(GLFW_FEATURE_UNAVAILABLE,
1153 "Wayland: The platform does not support setting the window opacity");
1156 void _glfwPlatformSetRawMouseMotion(_GLFWwindow *window, GLFWbool enabled)
1158 // This is handled in relativePointerHandleRelativeMotion
1161 GLFWbool _glfwPlatformRawMouseMotionSupported(void)
1166 void _glfwPlatformPollEvents(void)
1171 void _glfwPlatformWaitEvents(void)
1176 void _glfwPlatformWaitEventsTimeout(double timeout)
1178 handleEvents((int) (timeout * 1e3));
1181 void _glfwPlatformPostEmptyEvent(void)
1183 wl_display_sync(_glfw.wl.display);
1186 void _glfwPlatformGetCursorPos(_GLFWwindow* window, double* xpos, double* ypos)
1189 *xpos = window->wl.cursorPosX;
1191 *ypos = window->wl.cursorPosY;
1194 static GLFWbool isPointerLocked(_GLFWwindow* window);
1196 void _glfwPlatformSetCursorPos(_GLFWwindow* window, double x, double y)
1198 if (isPointerLocked(window))
1200 zwp_locked_pointer_v1_set_cursor_position_hint(
1201 window->wl.pointerLock.lockedPointer,
1202 wl_fixed_from_double(x), wl_fixed_from_double(y));
1203 wl_surface_commit(window->wl.surface);
1207 void _glfwPlatformSetCursorMode(_GLFWwindow* window, int mode)
1209 _glfwPlatformSetCursor(window, window->wl.currentCursor);
1212 const char* _glfwPlatformGetScancodeName(int scancode)
1215 _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED,
1216 "Wayland: Key names not yet implemented");
1220 int _glfwPlatformGetKeyScancode(int key)
1222 return _glfw.wl.scancodes[key];
1225 int _glfwPlatformCreateCursor(_GLFWcursor* cursor,
1226 const GLFWimage* image,
1229 cursor->wl.buffer = createShmBuffer(image);
1230 if (!cursor->wl.buffer)
1233 cursor->wl.width = image->width;
1234 cursor->wl.height = image->height;
1235 cursor->wl.xhot = xhot;
1236 cursor->wl.yhot = yhot;
1240 int _glfwPlatformCreateStandardCursor(_GLFWcursor* cursor, int shape)
1242 const char* name = NULL;
1244 // Try the XDG names first
1245 if (shape == GLFW_ARROW_CURSOR)
1247 else if (shape == GLFW_IBEAM_CURSOR)
1249 else if (shape == GLFW_CROSSHAIR_CURSOR)
1251 else if (shape == GLFW_POINTING_HAND_CURSOR)
1253 else if (shape == GLFW_RESIZE_EW_CURSOR)
1255 else if (shape == GLFW_RESIZE_NS_CURSOR)
1257 else if (shape == GLFW_RESIZE_NWSE_CURSOR)
1258 name = "nwse-resize";
1259 else if (shape == GLFW_RESIZE_NESW_CURSOR)
1260 name = "nesw-resize";
1261 else if (shape == GLFW_RESIZE_ALL_CURSOR)
1262 name = "all-scroll";
1263 else if (shape == GLFW_NOT_ALLOWED_CURSOR)
1264 name = "not-allowed";
1266 cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name);
1268 if (_glfw.wl.cursorThemeHiDPI)
1270 cursor->wl.cursorHiDPI =
1271 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name);
1274 if (!cursor->wl.cursor)
1276 // Fall back to the core X11 names
1277 if (shape == GLFW_ARROW_CURSOR)
1279 else if (shape == GLFW_IBEAM_CURSOR)
1281 else if (shape == GLFW_CROSSHAIR_CURSOR)
1283 else if (shape == GLFW_POINTING_HAND_CURSOR)
1285 else if (shape == GLFW_RESIZE_EW_CURSOR)
1286 name = "sb_h_double_arrow";
1287 else if (shape == GLFW_RESIZE_NS_CURSOR)
1288 name = "sb_v_double_arrow";
1289 else if (shape == GLFW_RESIZE_ALL_CURSOR)
1293 _glfwInputError(GLFW_CURSOR_UNAVAILABLE,
1294 "Wayland: Standard cursor shape unavailable");
1298 cursor->wl.cursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, name);
1299 if (!cursor->wl.cursor)
1301 _glfwInputError(GLFW_PLATFORM_ERROR,
1302 "Wayland: Failed to create standard cursor \"%s\"",
1307 if (_glfw.wl.cursorThemeHiDPI)
1309 if (!cursor->wl.cursorHiDPI)
1311 cursor->wl.cursorHiDPI =
1312 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, name);
1320 void _glfwPlatformDestroyCursor(_GLFWcursor* cursor)
1322 // If it's a standard cursor we don't need to do anything here
1323 if (cursor->wl.cursor)
1326 if (cursor->wl.buffer)
1327 wl_buffer_destroy(cursor->wl.buffer);
1330 static void relativePointerHandleRelativeMotion(void* data,
1331 struct zwp_relative_pointer_v1* pointer,
1336 wl_fixed_t dxUnaccel,
1337 wl_fixed_t dyUnaccel)
1339 _GLFWwindow* window = data;
1340 double xpos = window->virtualCursorPosX;
1341 double ypos = window->virtualCursorPosY;
1343 if (window->cursorMode != GLFW_CURSOR_DISABLED)
1346 if (window->rawMouseMotion)
1348 xpos += wl_fixed_to_double(dxUnaccel);
1349 ypos += wl_fixed_to_double(dyUnaccel);
1353 xpos += wl_fixed_to_double(dx);
1354 ypos += wl_fixed_to_double(dy);
1357 _glfwInputCursorPos(window, xpos, ypos);
1360 static const struct zwp_relative_pointer_v1_listener relativePointerListener = {
1361 relativePointerHandleRelativeMotion
1364 static void lockedPointerHandleLocked(void* data,
1365 struct zwp_locked_pointer_v1* lockedPointer)
1369 static void unlockPointer(_GLFWwindow* window)
1371 struct zwp_relative_pointer_v1* relativePointer =
1372 window->wl.pointerLock.relativePointer;
1373 struct zwp_locked_pointer_v1* lockedPointer =
1374 window->wl.pointerLock.lockedPointer;
1376 zwp_relative_pointer_v1_destroy(relativePointer);
1377 zwp_locked_pointer_v1_destroy(lockedPointer);
1379 window->wl.pointerLock.relativePointer = NULL;
1380 window->wl.pointerLock.lockedPointer = NULL;
1383 static void lockPointer(_GLFWwindow* window);
1385 static void lockedPointerHandleUnlocked(void* data,
1386 struct zwp_locked_pointer_v1* lockedPointer)
1390 static const struct zwp_locked_pointer_v1_listener lockedPointerListener = {
1391 lockedPointerHandleLocked,
1392 lockedPointerHandleUnlocked
1395 static void lockPointer(_GLFWwindow* window)
1397 struct zwp_relative_pointer_v1* relativePointer;
1398 struct zwp_locked_pointer_v1* lockedPointer;
1400 if (!_glfw.wl.relativePointerManager)
1402 _glfwInputError(GLFW_PLATFORM_ERROR,
1403 "Wayland: no relative pointer manager");
1408 zwp_relative_pointer_manager_v1_get_relative_pointer(
1409 _glfw.wl.relativePointerManager,
1411 zwp_relative_pointer_v1_add_listener(relativePointer,
1412 &relativePointerListener,
1416 zwp_pointer_constraints_v1_lock_pointer(
1417 _glfw.wl.pointerConstraints,
1421 ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT);
1422 zwp_locked_pointer_v1_add_listener(lockedPointer,
1423 &lockedPointerListener,
1426 window->wl.pointerLock.relativePointer = relativePointer;
1427 window->wl.pointerLock.lockedPointer = lockedPointer;
1429 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial,
1433 static GLFWbool isPointerLocked(_GLFWwindow* window)
1435 return window->wl.pointerLock.lockedPointer != NULL;
1438 void _glfwPlatformSetCursor(_GLFWwindow* window, _GLFWcursor* cursor)
1440 struct wl_cursor* defaultCursor;
1441 struct wl_cursor* defaultCursorHiDPI = NULL;
1443 if (!_glfw.wl.pointer)
1446 window->wl.currentCursor = cursor;
1448 // If we're not in the correct window just save the cursor
1449 // the next time the pointer enters the window the cursor will change
1450 if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != mainWindow)
1453 // Unlock possible pointer lock if no longer disabled.
1454 if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window))
1455 unlockPointer(window);
1457 if (window->cursorMode == GLFW_CURSOR_NORMAL)
1460 setCursorImage(window, &cursor->wl);
1463 defaultCursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme,
1467 _glfwInputError(GLFW_PLATFORM_ERROR,
1468 "Wayland: Standard cursor not found");
1471 if (_glfw.wl.cursorThemeHiDPI)
1472 defaultCursorHiDPI =
1473 wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI,
1475 _GLFWcursorWayland cursorWayland = {
1483 setCursorImage(window, &cursorWayland);
1486 else if (window->cursorMode == GLFW_CURSOR_DISABLED)
1488 if (!isPointerLocked(window))
1489 lockPointer(window);
1491 else if (window->cursorMode == GLFW_CURSOR_HIDDEN)
1493 wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.serial, NULL, 0, 0);
1497 static void dataSourceHandleTarget(void* data,
1498 struct wl_data_source* dataSource,
1499 const char* mimeType)
1501 if (_glfw.wl.dataSource != dataSource)
1503 _glfwInputError(GLFW_PLATFORM_ERROR,
1504 "Wayland: Unknown clipboard data source");
1509 static void dataSourceHandleSend(void* data,
1510 struct wl_data_source* dataSource,
1511 const char* mimeType,
1514 const char* string = _glfw.wl.clipboardSendString;
1515 size_t len = _glfw.wl.clipboardSendSize;
1518 if (_glfw.wl.dataSource != dataSource)
1520 _glfwInputError(GLFW_PLATFORM_ERROR,
1521 "Wayland: Unknown clipboard data source");
1527 _glfwInputError(GLFW_PLATFORM_ERROR,
1528 "Wayland: Copy requested from an invalid string");
1532 if (strcmp(mimeType, "text/plain;charset=utf-8") != 0)
1534 _glfwInputError(GLFW_PLATFORM_ERROR,
1535 "Wayland: Wrong MIME type asked from clipboard");
1542 ret = write(fd, string, len);
1543 if (ret == -1 && errno == EINTR)
1547 // TODO: also report errno maybe.
1548 _glfwInputError(GLFW_PLATFORM_ERROR,
1549 "Wayland: Error while writing the clipboard");
1558 static void dataSourceHandleCancelled(void* data,
1559 struct wl_data_source* dataSource)
1561 wl_data_source_destroy(dataSource);
1563 if (_glfw.wl.dataSource != dataSource)
1565 _glfwInputError(GLFW_PLATFORM_ERROR,
1566 "Wayland: Unknown clipboard data source");
1570 _glfw.wl.dataSource = NULL;
1573 static const struct wl_data_source_listener dataSourceListener = {
1574 dataSourceHandleTarget,
1575 dataSourceHandleSend,
1576 dataSourceHandleCancelled,
1579 void _glfwPlatformSetClipboardString(const char* string)
1581 if (_glfw.wl.dataSource)
1583 wl_data_source_destroy(_glfw.wl.dataSource);
1584 _glfw.wl.dataSource = NULL;
1587 if (_glfw.wl.clipboardSendString)
1589 free(_glfw.wl.clipboardSendString);
1590 _glfw.wl.clipboardSendString = NULL;
1593 _glfw.wl.clipboardSendString = strdup(string);
1594 if (!_glfw.wl.clipboardSendString)
1596 _glfwInputError(GLFW_PLATFORM_ERROR,
1597 "Wayland: Impossible to allocate clipboard string");
1600 _glfw.wl.clipboardSendSize = strlen(string);
1601 _glfw.wl.dataSource =
1602 wl_data_device_manager_create_data_source(_glfw.wl.dataDeviceManager);
1603 if (!_glfw.wl.dataSource)
1605 _glfwInputError(GLFW_PLATFORM_ERROR,
1606 "Wayland: Impossible to create clipboard source");
1607 free(_glfw.wl.clipboardSendString);
1610 wl_data_source_add_listener(_glfw.wl.dataSource,
1611 &dataSourceListener,
1613 wl_data_source_offer(_glfw.wl.dataSource, "text/plain;charset=utf-8");
1614 wl_data_device_set_selection(_glfw.wl.dataDevice,
1615 _glfw.wl.dataSource,
1619 static GLFWbool growClipboardString(void)
1621 char* clipboard = _glfw.wl.clipboardString;
1623 clipboard = realloc(clipboard, _glfw.wl.clipboardSize * 2);
1626 _glfwInputError(GLFW_PLATFORM_ERROR,
1627 "Wayland: Impossible to grow clipboard string");
1630 _glfw.wl.clipboardString = clipboard;
1631 _glfw.wl.clipboardSize = _glfw.wl.clipboardSize * 2;
1635 const char* _glfwPlatformGetClipboardString(void)
1641 if (!_glfw.wl.dataOffer)
1643 _glfwInputError(GLFW_FORMAT_UNAVAILABLE,
1644 "No clipboard data has been sent yet");
1648 ret = pipe2(fds, O_CLOEXEC);
1651 // TODO: also report errno maybe?
1652 _glfwInputError(GLFW_PLATFORM_ERROR,
1653 "Wayland: Impossible to create clipboard pipe fds");
1657 wl_data_offer_receive(_glfw.wl.dataOffer, "text/plain;charset=utf-8", fds[1]);
1660 // XXX: this is a huge hack, this function shouldn’t be synchronous!
1665 // Grow the clipboard if we need to paste something bigger, there is no
1666 // shrink operation yet.
1667 if (len + 4096 > _glfw.wl.clipboardSize)
1669 if (!growClipboardString())
1676 // Then read from the fd to the clipboard, handling all known errors.
1677 ret = read(fds[0], _glfw.wl.clipboardString + len, 4096);
1680 if (ret == -1 && errno == EINTR)
1684 // TODO: also report errno maybe.
1685 _glfwInputError(GLFW_PLATFORM_ERROR,
1686 "Wayland: Impossible to read from clipboard fd");
1693 if (len + 1 > _glfw.wl.clipboardSize)
1695 if (!growClipboardString())
1698 _glfw.wl.clipboardString[len] = '\0';
1699 return _glfw.wl.clipboardString;
1702 EGLenum _glfwPlatformGetEGLPlatform(EGLint** attribs)
1704 if (_glfw.egl.EXT_platform_base && _glfw.egl.EXT_platform_wayland)
1705 return EGL_PLATFORM_WAYLAND_EXT;
1710 EGLNativeDisplayType _glfwPlatformGetEGLNativeDisplay(void)
1712 return _glfw.wl.display;
1715 EGLNativeWindowType _glfwPlatformGetEGLNativeWindow(_GLFWwindow* window)
1717 return window->wl.native;
1720 void _glfwPlatformGetRequiredInstanceExtensions(char** extensions)
1722 if (!_glfw.vk.KHR_surface || !_glfw.vk.KHR_wayland_surface)
1725 extensions[0] = "VK_KHR_surface";
1726 extensions[1] = "VK_KHR_wayland_surface";
1729 int _glfwPlatformGetPhysicalDevicePresentationSupport(VkInstance instance,
1730 VkPhysicalDevice device,
1731 uint32_t queuefamily)
1733 PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR
1734 vkGetPhysicalDeviceWaylandPresentationSupportKHR =
1735 (PFN_vkGetPhysicalDeviceWaylandPresentationSupportKHR)
1736 vkGetInstanceProcAddr(instance, "vkGetPhysicalDeviceWaylandPresentationSupportKHR");
1737 if (!vkGetPhysicalDeviceWaylandPresentationSupportKHR)
1739 _glfwInputError(GLFW_API_UNAVAILABLE,
1740 "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
1741 return VK_NULL_HANDLE;
1744 return vkGetPhysicalDeviceWaylandPresentationSupportKHR(device,
1749 VkResult _glfwPlatformCreateWindowSurface(VkInstance instance,
1750 _GLFWwindow* window,
1751 const VkAllocationCallbacks* allocator,
1752 VkSurfaceKHR* surface)
1755 VkWaylandSurfaceCreateInfoKHR sci;
1756 PFN_vkCreateWaylandSurfaceKHR vkCreateWaylandSurfaceKHR;
1758 vkCreateWaylandSurfaceKHR = (PFN_vkCreateWaylandSurfaceKHR)
1759 vkGetInstanceProcAddr(instance, "vkCreateWaylandSurfaceKHR");
1760 if (!vkCreateWaylandSurfaceKHR)
1762 _glfwInputError(GLFW_API_UNAVAILABLE,
1763 "Wayland: Vulkan instance missing VK_KHR_wayland_surface extension");
1764 return VK_ERROR_EXTENSION_NOT_PRESENT;
1767 memset(&sci, 0, sizeof(sci));
1768 sci.sType = VK_STRUCTURE_TYPE_WAYLAND_SURFACE_CREATE_INFO_KHR;
1769 sci.display = _glfw.wl.display;
1770 sci.surface = window->wl.surface;
1772 err = vkCreateWaylandSurfaceKHR(instance, &sci, allocator, surface);
1775 _glfwInputError(GLFW_PLATFORM_ERROR,
1776 "Wayland: Failed to create Vulkan surface: %s",
1777 _glfwGetVulkanResultString(err));
1784 //////////////////////////////////////////////////////////////////////////
1785 ////// GLFW native API //////
1786 //////////////////////////////////////////////////////////////////////////
1788 GLFWAPI struct wl_display* glfwGetWaylandDisplay(void)
1790 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
1791 return _glfw.wl.display;
1794 GLFWAPI struct wl_surface* glfwGetWaylandWindow(GLFWwindow* handle)
1796 _GLFWwindow* window = (_GLFWwindow*) handle;
1797 _GLFW_REQUIRE_INIT_OR_RETURN(NULL);
1798 return window->wl.surface;