From b9ed25d2e991944acb51dedc63f3fe68e24c02c9 Mon Sep 17 00:00:00 2001 From: Michael Pennington Date: Thu, 26 May 2022 20:33:28 -0400 Subject: [PATCH 01/15] Wayland: Add GLFW_WAYLAND_APP_ID window hint This adds a window hint string for the xdg_toplevel::app_id, which is used by desktop environments to connect windows with application icons and other information. This is similar to the WM_CLASS property on X11. A few very minor fixes were done by @elmindreda during merge. Fixes #2121 Closes #2122 --- CONTRIBUTORS.md | 1 + README.md | 2 ++ docs/news.dox | 6 ++++++ docs/window.dox | 8 ++++++++ include/GLFW/glfw3.h | 6 ++++++ src/internal.h | 3 +++ src/window.c | 4 ++++ src/wl_platform.h | 1 + src/wl_window.c | 5 +++++ 9 files changed, 36 insertions(+) diff --git a/CONTRIBUTORS.md b/CONTRIBUTORS.md index 673db319..9c4964c0 100644 --- a/CONTRIBUTORS.md +++ b/CONTRIBUTORS.md @@ -166,6 +166,7 @@ video tutorials. - Peoro - Braden Pellett - Christopher Pelloux + - Michael Pennington - Arturo J. PĂ©rez - Vladimir Perminov - Olivier Perret diff --git a/README.md b/README.md index 5b56b3eb..66af82e5 100644 --- a/README.md +++ b/README.md @@ -142,6 +142,8 @@ information on what to include when reporting a bug. - Added `GLFW_PLATFORM_UNAVAILABLE` error for platform detection failures (#1958) - Added `GLFW_FEATURE_UNAVAILABLE` error for platform limitations (#1692) - Added `GLFW_FEATURE_UNIMPLEMENTED` error for incomplete backends (#1692) + - Added `GLFW_WAYLAND_APP_ID` window hint string for Wayland app\_id selection + (#2121,#2122) - Added `GLFW_ANGLE_PLATFORM_TYPE` init hint and `GLFW_ANGLE_PLATFORM_TYPE_*` values to select ANGLE backend (#1380) - Added `GLFW_X11_XCB_VULKAN_SURFACE` init hint for selecting X11 Vulkan diff --git a/docs/news.dox b/docs/news.dox index fbf60319..98daa3a2 100644 --- a/docs/news.dox +++ b/docs/news.dox @@ -43,6 +43,12 @@ to whatever window is behind it. This can also be changed after window creation with the matching [window attribute](@ref GLFW_MOUSE_PASSTHROUGH_attrib). +@subsubsection wayland_app_id_34 Wayland app_id specification + +GLFW now supports specifying the app_id for a Wayland window using the +[GLFW_WAYLAND_APP_ID](@ref GLFW_WAYLAND_APP_ID_hint) window hint string. + + @subsubsection features_34_angle_backend Support for ANGLE rendering backend selection GLFW now provides the diff --git a/docs/window.dox b/docs/window.dox index 32271e3a..451aca73 100644 --- a/docs/window.dox +++ b/docs/window.dox @@ -492,6 +492,13 @@ __GLFW_X11_CLASS_NAME__ and __GLFW_X11_INSTANCE_NAME__ specifies the desired ASCII encoded class and instance parts of the ICCCM `WM_CLASS` window property. These are set with @ref glfwWindowHintString. +@subsubsection window_hints_wayland Wayland specific window hints + +@anchor GLFW_WAYLAND_APP_ID_hint +__GLFW_WAYLAND_APP_ID__ specifies the Wayland app_id for a window, used +by window managers to identify types of windows. This is set with +@ref glfwWindowHintString. + @subsubsection window_hints_values Supported and default values @@ -540,6 +547,7 @@ GLFW_COCOA_FRAME_NAME | `""` | A UTF-8 encoded fr GLFW_COCOA_GRAPHICS_SWITCHING | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE` GLFW_X11_CLASS_NAME | `""` | An ASCII encoded `WM_CLASS` class name GLFW_X11_INSTANCE_NAME | `""` | An ASCII encoded `WM_CLASS` instance name +GLFW_WAYLAND_APP_ID | `""` | An ASCII encoded Wayland `app_id` name @section window_events Window event processing diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index aa6ddc33..8f481790 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1105,6 +1105,12 @@ extern "C" { */ #define GLFW_X11_INSTANCE_NAME 0x00024002 #define GLFW_WIN32_KEYBOARD_MENU 0x00025001 +/*! @brief Wayland specific + * [window hint](@ref GLFW_WAYLAND_APP_ID_hint). + * + * Allows specification of the Wayland app_id. + */ +#define GLFW_WAYLAND_APP_ID 0x00026001 /*! @} */ #define GLFW_NO_API 0 diff --git a/src/internal.h b/src/internal.h index 89a18628..8d576d1d 100644 --- a/src/internal.h +++ b/src/internal.h @@ -421,6 +421,9 @@ struct _GLFWwndconfig struct { GLFWbool keymenu; } win32; + struct { + char appId[256]; + } wl; }; // Context configuration diff --git a/src/window.c b/src/window.c index ebbc6dca..f740580a 100644 --- a/src/window.c +++ b/src/window.c @@ -447,6 +447,10 @@ GLFWAPI void glfwWindowHintString(int hint, const char* value) strncpy(_glfw.hints.window.x11.instanceName, value, sizeof(_glfw.hints.window.x11.instanceName) - 1); return; + case GLFW_WAYLAND_APP_ID: + strncpy(_glfw.hints.window.wl.appId, value, + sizeof(_glfw.hints.window.wl.appId) - 1); + return; } _glfwInputError(GLFW_INVALID_ENUM, "Invalid window hint string 0x%08X", hint); diff --git a/src/wl_platform.h b/src/wl_platform.h index f65bcb11..73639de1 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -258,6 +258,7 @@ typedef struct _GLFWwindowWayland double cursorPosX, cursorPosY; char* title; + char* appId; // We need to track the monitors the window spans on to calculate the // optimal scaling factor. diff --git a/src/wl_window.c b/src/wl_window.c index ef935d33..900c877d 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -614,6 +614,9 @@ static GLFWbool createShellObjects(_GLFWwindow* window) xdg_toplevel_add_listener(window->wl.xdg.toplevel, &xdgToplevelListener, window); + if (window->wl.appId) + xdg_toplevel_set_app_id(window->wl.xdg.toplevel, window->wl.appId); + if (window->wl.title) xdg_toplevel_set_title(window->wl.xdg.toplevel, window->wl.title); @@ -728,6 +731,7 @@ static GLFWbool createNativeSurface(_GLFWwindow* window, window->wl.height = wndconfig->height; window->wl.scale = 1; window->wl.title = _glfw_strdup(wndconfig->title); + window->wl.appId = _glfw_strdup(wndconfig->wl.appId); window->wl.maximized = wndconfig->maximized; @@ -1865,6 +1869,7 @@ void _glfwDestroyWindowWayland(_GLFWwindow* window) wl_surface_destroy(window->wl.surface); _glfw_free(window->wl.title); + _glfw_free(window->wl.appId); _glfw_free(window->wl.monitors); } From e85b645b8adbbc565c9e9fccbf474705f453d1c1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Sun, 24 Jul 2022 22:27:22 +0200 Subject: [PATCH 02/15] Wayland: Clean up pointer locking Shorten names and allow C99 declarations. Replace helper function with the NULL check it was hiding. Separate cursor hiding from pointer locking. --- src/wl_platform.h | 6 ++-- src/wl_window.c | 88 +++++++++++++++++++---------------------------- 2 files changed, 37 insertions(+), 57 deletions(-) diff --git a/src/wl_platform.h b/src/wl_platform.h index 73639de1..f2bf8f03 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -267,10 +267,8 @@ typedef struct _GLFWwindowWayland int monitorsCount; int monitorsSize; - struct { - struct zwp_relative_pointer_v1* relativePointer; - struct zwp_locked_pointer_v1* lockedPointer; - } pointerLock; + struct zwp_relative_pointer_v1* relativePointer; + struct zwp_locked_pointer_v1* lockedPointer; struct zwp_idle_inhibitor_v1* idleInhibitor; diff --git a/src/wl_window.c b/src/wl_window.c index 900c877d..c25edf71 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2241,15 +2241,14 @@ void _glfwGetCursorPosWayland(_GLFWwindow* window, double* xpos, double* ypos) *ypos = window->wl.cursorPosY; } -static GLFWbool isPointerLocked(_GLFWwindow* window); - void _glfwSetCursorPosWayland(_GLFWwindow* window, double x, double y) { - if (isPointerLocked(window)) + if (window->wl.lockedPointer) { zwp_locked_pointer_v1_set_cursor_position_hint( - window->wl.pointerLock.lockedPointer, - wl_fixed_from_double(x), wl_fixed_from_double(y)); + window->wl.lockedPointer, + wl_fixed_from_double(x), + wl_fixed_from_double(y)); } } @@ -2486,20 +2485,13 @@ static void lockedPointerHandleLocked(void* userData, static void unlockPointer(_GLFWwindow* window) { - struct zwp_relative_pointer_v1* relativePointer = - window->wl.pointerLock.relativePointer; - struct zwp_locked_pointer_v1* lockedPointer = - window->wl.pointerLock.lockedPointer; + zwp_relative_pointer_v1_destroy(window->wl.relativePointer); + window->wl.relativePointer = NULL; - zwp_relative_pointer_v1_destroy(relativePointer); - zwp_locked_pointer_v1_destroy(lockedPointer); - - window->wl.pointerLock.relativePointer = NULL; - window->wl.pointerLock.lockedPointer = NULL; + zwp_locked_pointer_v1_destroy(window->wl.lockedPointer); + window->wl.lockedPointer = NULL; } -static void lockPointer(_GLFWwindow* window); - static void lockedPointerHandleUnlocked(void* userData, struct zwp_locked_pointer_v1* lockedPointer) { @@ -2513,9 +2505,6 @@ static const struct zwp_locked_pointer_v1_listener lockedPointerListener = static void lockPointer(_GLFWwindow* window) { - struct zwp_relative_pointer_v1* relativePointer; - struct zwp_locked_pointer_v1* lockedPointer; - if (!_glfw.wl.relativePointerManager) { _glfwInputError(GLFW_PLATFORM_ERROR, @@ -2523,42 +2512,28 @@ static void lockPointer(_GLFWwindow* window) return; } - relativePointer = + window->wl.relativePointer = zwp_relative_pointer_manager_v1_get_relative_pointer( _glfw.wl.relativePointerManager, _glfw.wl.pointer); - zwp_relative_pointer_v1_add_listener(relativePointer, + zwp_relative_pointer_v1_add_listener(window->wl.relativePointer, &relativePointerListener, window); - lockedPointer = + window->wl.lockedPointer = zwp_pointer_constraints_v1_lock_pointer( _glfw.wl.pointerConstraints, window->wl.surface, _glfw.wl.pointer, NULL, ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT); - zwp_locked_pointer_v1_add_listener(lockedPointer, + zwp_locked_pointer_v1_add_listener(window->wl.lockedPointer, &lockedPointerListener, window); - - window->wl.pointerLock.relativePointer = relativePointer; - window->wl.pointerLock.lockedPointer = lockedPointer; - - wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.pointerEnterSerial, - NULL, 0, 0); -} - -static GLFWbool isPointerLocked(_GLFWwindow* window) -{ - return window->wl.pointerLock.lockedPointer != NULL; } void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) { - struct wl_cursor* defaultCursor; - struct wl_cursor* defaultCursorHiDPI = NULL; - if (!_glfw.wl.pointer) return; @@ -2569,9 +2544,17 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) if (window != _glfw.wl.pointerFocus || window->wl.decorations.focus != mainWindow) return; - // Unlock possible pointer lock if no longer disabled. - if (window->cursorMode != GLFW_CURSOR_DISABLED && isPointerLocked(window)) - unlockPointer(window); + // Update pointer lock to match cursor mode + if (window->cursorMode == GLFW_CURSOR_DISABLED) + { + if (!window->wl.lockedPointer) + lockPointer(window); + } + else + { + if (window->wl.lockedPointer) + unlockPointer(window); + } if (window->cursorMode == GLFW_CURSOR_NORMAL) { @@ -2579,19 +2562,24 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) setCursorImage(window, &cursor->wl); else { - defaultCursor = wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, - "left_ptr"); + struct wl_cursor* defaultCursor = + wl_cursor_theme_get_cursor(_glfw.wl.cursorTheme, "left_ptr"); if (!defaultCursor) { _glfwInputError(GLFW_PLATFORM_ERROR, "Wayland: Standard cursor not found"); return; } + + struct wl_cursor* defaultCursorHiDPI = NULL; if (_glfw.wl.cursorThemeHiDPI) + { defaultCursorHiDPI = - wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, - "left_ptr"); - _GLFWcursorWayland cursorWayland = { + wl_cursor_theme_get_cursor(_glfw.wl.cursorThemeHiDPI, "left_ptr"); + } + + _GLFWcursorWayland cursorWayland = + { defaultCursor, defaultCursorHiDPI, NULL, @@ -2599,18 +2587,12 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) 0, 0, 0 }; + setCursorImage(window, &cursorWayland); } } - else if (window->cursorMode == GLFW_CURSOR_DISABLED) - { - if (!isPointerLocked(window)) - lockPointer(window); - } - else if (window->cursorMode == GLFW_CURSOR_HIDDEN) - { + else wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.pointerEnterSerial, NULL, 0, 0); - } } static void dataSourceHandleTarget(void* userData, From 09ebf3f0bfe5a5e819fd51f0995ca928b27185bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Sun, 24 Jul 2022 22:28:32 +0200 Subject: [PATCH 03/15] Wayland: Fix error for missing protocol A protocol not being available is not a platform error. The platform is doing fine; we're just sad that it lacks a feature we want. --- src/wl_window.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/wl_window.c b/src/wl_window.c index c25edf71..27203e76 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2507,8 +2507,8 @@ static void lockPointer(_GLFWwindow* window) { if (!_glfw.wl.relativePointerManager) { - _glfwInputError(GLFW_PLATFORM_ERROR, - "Wayland: no relative pointer manager"); + _glfwInputError(GLFW_FEATURE_UNAVAILABLE, + "Wayland: The compositor does not support pointer locking"); return; } From 03af6b3d4c2dbd763fbf32927c44fe6fd517991c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 25 Jul 2022 17:40:22 +0200 Subject: [PATCH 04/15] Wayland: Fix leaks of pointer related objects --- src/wl_window.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/wl_window.c b/src/wl_window.c index 27203e76..ef346655 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1854,6 +1854,12 @@ void _glfwDestroyWindowWayland(_GLFWwindow* window) if (window->wl.idleInhibitor) zwp_idle_inhibitor_v1_destroy(window->wl.idleInhibitor); + if (window->wl.relativePointer) + zwp_relative_pointer_v1_destroy(window->wl.relativePointer); + + if (window->wl.lockedPointer) + zwp_locked_pointer_v1_destroy(window->wl.lockedPointer); + if (window->context.destroy) window->context.destroy(window); From a46f829de8d90d6932a696fd397479042ec1071d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 8 Jul 2019 14:45:31 +0200 Subject: [PATCH 05/15] Refactor cursor mode paths This is the refactoring part of adding GLFW_CURSOR_CAPTURED, separated out to help keep 3.3-stable similar to the main branch. Related to #58. --- src/win32_platform.h | 2 ++ src/win32_window.c | 83 +++++++++++++++++++++++++++++++------------- src/wl_window.c | 8 +++-- src/x11_window.c | 70 ++++++++++++++++++++++++++++--------- 4 files changed, 120 insertions(+), 43 deletions(-) diff --git a/src/win32_platform.h b/src/win32_platform.h index c2158943..82b34bb9 100644 --- a/src/win32_platform.h +++ b/src/win32_platform.h @@ -452,6 +452,8 @@ typedef struct _GLFWlibraryWin32 double restoreCursorPosX, restoreCursorPosY; // The window whose disabled cursor mode is active _GLFWwindow* disabledCursorWindow; + // The window the cursor is captured in + _GLFWwindow* capturedCursorWindow; RAWINPUT* rawInput; int rawInputSize; UINT mouseTrailSize; diff --git a/src/win32_window.c b/src/win32_window.c index 168529e2..4cf25640 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -249,20 +249,24 @@ static void updateCursorImage(_GLFWwindow* window) SetCursor(NULL); } -// Updates the cursor clip rect +// Sets the cursor clip rect to the window content area // -static void updateClipRect(_GLFWwindow* window) +static void captureCursor(_GLFWwindow* window) { - if (window) - { - RECT clipRect; - GetClientRect(window->win32.handle, &clipRect); - ClientToScreen(window->win32.handle, (POINT*) &clipRect.left); - ClientToScreen(window->win32.handle, (POINT*) &clipRect.right); - ClipCursor(&clipRect); - } - else - ClipCursor(NULL); + RECT clipRect; + GetClientRect(window->win32.handle, &clipRect); + ClientToScreen(window->win32.handle, (POINT*) &clipRect.left); + ClientToScreen(window->win32.handle, (POINT*) &clipRect.right); + ClipCursor(&clipRect); + _glfw.win32.capturedCursorWindow = window; +} + +// Disabled clip cursor +// +static void releaseCursor(void) +{ + ClipCursor(NULL); + _glfw.win32.capturedCursorWindow = NULL; } // Enables WM_INPUT messages for the mouse for the specified window @@ -301,7 +305,7 @@ static void disableCursor(_GLFWwindow* window) &_glfw.win32.restoreCursorPosY); updateCursorImage(window); _glfwCenterCursorInContentArea(window); - updateClipRect(window); + captureCursor(window); if (window->rawMouseMotion) enableRawMouseMotion(window); @@ -315,7 +319,7 @@ static void enableCursor(_GLFWwindow* window) disableRawMouseMotion(window); _glfw.win32.disabledCursorWindow = NULL; - updateClipRect(NULL); + releaseCursor(); _glfwSetCursorPosWin32(window, _glfw.win32.restoreCursorPosX, _glfw.win32.restoreCursorPosY); @@ -1004,8 +1008,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l (window->win32.maximized && wParam != SIZE_RESTORED); - if (_glfw.win32.disabledCursorWindow == window) - updateClipRect(window); + if (_glfw.win32.capturedCursorWindow == window) + captureCursor(window); if (window->win32.iconified != iconified) _glfwInputWindowIconify(window, iconified); @@ -1040,8 +1044,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l case WM_MOVE: { - if (_glfw.win32.disabledCursorWindow == window) - updateClipRect(window); + if (_glfw.win32.capturedCursorWindow == window) + captureCursor(window); // NOTE: This cannot use LOWORD/HIWORD recommended by MSDN, as // those macros do not handle negative window positions correctly @@ -1495,7 +1499,10 @@ void _glfwDestroyWindowWin32(_GLFWwindow* window) window->context.destroy(window); if (_glfw.win32.disabledCursorWindow == window) - _glfw.win32.disabledCursorWindow = NULL; + enableCursor(window); + + if (_glfw.win32.capturedCursorWindow == window) + releaseCursor(); if (window->win32.handle) { @@ -2142,14 +2149,40 @@ void _glfwSetCursorPosWin32(_GLFWwindow* window, double xpos, double ypos) void _glfwSetCursorModeWin32(_GLFWwindow* window, int mode) { - if (mode == GLFW_CURSOR_DISABLED) + if (_glfwWindowFocusedWin32(window)) { - if (_glfwWindowFocusedWin32(window)) - disableCursor(window); + if (mode == GLFW_CURSOR_DISABLED) + { + _glfwGetCursorPosWin32(window, + &_glfw.win32.restoreCursorPosX, + &_glfw.win32.restoreCursorPosY); + _glfwCenterCursorInContentArea(window); + if (window->rawMouseMotion) + enableRawMouseMotion(window); + } + else if (_glfw.win32.disabledCursorWindow == window) + { + if (window->rawMouseMotion) + disableRawMouseMotion(window); + } + + if (mode == GLFW_CURSOR_DISABLED) + captureCursor(window); + else + releaseCursor(); + + if (mode == GLFW_CURSOR_DISABLED) + _glfw.win32.disabledCursorWindow = window; + else if (_glfw.win32.disabledCursorWindow == window) + { + _glfw.win32.disabledCursorWindow = NULL; + _glfwSetCursorPosWin32(window, + _glfw.win32.restoreCursorPosX, + _glfw.win32.restoreCursorPosY); + } } - else if (_glfw.win32.disabledCursorWindow == window) - enableCursor(window); - else if (cursorInContentArea(window)) + + if (cursorInContentArea(window)) updateCursorImage(window); } diff --git a/src/wl_window.c b/src/wl_window.c index ef346655..b4b49b94 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2556,7 +2556,8 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) if (!window->wl.lockedPointer) lockPointer(window); } - else + else if (window->cursorMode == GLFW_CURSOR_NORMAL || + window->cursorMode == GLFW_CURSOR_HIDDEN) { if (window->wl.lockedPointer) unlockPointer(window); @@ -2597,8 +2598,11 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) setCursorImage(window, &cursorWayland); } } - else + else if (window->cursorMode == GLFW_CURSOR_HIDDEN || + window->cursorMode == GLFW_CURSOR_DISABLED) + { wl_pointer_set_cursor(_glfw.wl.pointer, _glfw.wl.pointerEnterSerial, NULL, 0, 0); + } } static void dataSourceHandleTarget(void* userData, diff --git a/src/x11_window.c b/src/x11_window.c index 98f990b2..f323db6c 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -470,6 +470,25 @@ static void updateCursorImage(_GLFWwindow* window) } } +// Grabs the cursor and confines it to the window +// +static void captureCursor(_GLFWwindow* window) +{ + XGrabPointer(_glfw.x11.display, window->x11.handle, True, + ButtonPressMask | ButtonReleaseMask | PointerMotionMask, + GrabModeAsync, GrabModeAsync, + window->x11.handle, + None, + CurrentTime); +} + +// Ungrabs the cursor +// +static void releaseCursor(void) +{ + XUngrabPointer(_glfw.x11.display, CurrentTime); +} + // Enable XI2 raw mouse motion events // static void enableRawMouseMotion(_GLFWwindow* window) @@ -512,12 +531,7 @@ static void disableCursor(_GLFWwindow* window) &_glfw.x11.restoreCursorPosY); updateCursorImage(window); _glfwCenterCursorInContentArea(window); - XGrabPointer(_glfw.x11.display, window->x11.handle, True, - ButtonPressMask | ButtonReleaseMask | PointerMotionMask, - GrabModeAsync, GrabModeAsync, - window->x11.handle, - _glfw.x11.hiddenCursorHandle, - CurrentTime); + captureCursor(window); } // Exit disabled cursor mode for the specified window @@ -528,7 +542,7 @@ static void enableCursor(_GLFWwindow* window) disableRawMouseMotion(window); _glfw.x11.disabledCursorWindow = NULL; - XUngrabPointer(_glfw.x11.display, CurrentTime); + releaseCursor(); _glfwSetCursorPosX11(window, _glfw.x11.restoreCursorPosX, _glfw.x11.restoreCursorPosY); @@ -1986,7 +2000,7 @@ GLFWbool _glfwCreateWindowX11(_GLFWwindow* window, void _glfwDestroyWindowX11(_GLFWwindow* window) { if (_glfw.x11.disabledCursorWindow == window) - _glfw.x11.disabledCursorWindow = NULL; + enableCursor(window); if (window->monitor) releaseMonitor(window); @@ -2800,16 +2814,40 @@ void _glfwSetCursorPosX11(_GLFWwindow* window, double x, double y) void _glfwSetCursorModeX11(_GLFWwindow* window, int mode) { - if (mode == GLFW_CURSOR_DISABLED) + if (_glfwWindowFocusedX11(window)) { - if (_glfwWindowFocusedX11(window)) - disableCursor(window); - } - else if (_glfw.x11.disabledCursorWindow == window) - enableCursor(window); - else - updateCursorImage(window); + if (mode == GLFW_CURSOR_DISABLED) + { + _glfwGetCursorPosX11(window, + &_glfw.x11.restoreCursorPosX, + &_glfw.x11.restoreCursorPosY); + _glfwCenterCursorInContentArea(window); + if (window->rawMouseMotion) + enableRawMouseMotion(window); + } + else if (_glfw.x11.disabledCursorWindow == window) + { + if (window->rawMouseMotion) + disableRawMouseMotion(window); + } + if (mode == GLFW_CURSOR_DISABLED) + captureCursor(window); + else + releaseCursor(); + + if (mode == GLFW_CURSOR_DISABLED) + _glfw.x11.disabledCursorWindow = window; + else if (_glfw.x11.disabledCursorWindow == window) + { + _glfw.x11.disabledCursorWindow = NULL; + _glfwSetCursorPosX11(window, + _glfw.x11.restoreCursorPosX, + _glfw.x11.restoreCursorPosY); + } + } + + updateCursorImage(window); XFlush(_glfw.x11.display); } From 488008e0a22d10c337ef6e910b57a52cecb6bb46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 3 Dec 2019 17:58:20 +0100 Subject: [PATCH 06/15] Add cursor mode GLFW_CURSOR_CAPTURED This adds a cursor mode that provides a visible cursor confined to the content area of the window. Fixes #58 --- README.md | 2 ++ docs/input.dox | 12 ++++++++++ docs/news.dox | 9 ++++++++ include/GLFW/glfw3.h | 3 +++ src/cocoa_window.m | 8 +++++++ src/input.c | 3 ++- src/win32_window.c | 15 ++++++++++-- src/wl_platform.h | 1 + src/wl_window.c | 54 +++++++++++++++++++++++++++++++++++++++++++- src/x11_window.c | 12 +++++++--- tests/cursor.c | 8 ++++++- 11 files changed, 119 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 66af82e5..c84f8cb5 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,8 @@ information on what to include when reporting a bug. - Added `GLFW_POINTING_HAND_CURSOR` alias for `GLFW_HAND_CURSOR` (#427) - Added `GLFW_MOUSE_PASSTHROUGH` window hint for letting mouse input pass through the window (#1236,#1568) + - Added `GLFW_CURSOR_CAPTURED` cursor mode to confine the cursor to the window + content area (#58) - Added `GLFW_PLATFORM_UNAVAILABLE` error for platform detection failures (#1958) - Added `GLFW_FEATURE_UNAVAILABLE` error for platform limitations (#1692) - Added `GLFW_FEATURE_UNIMPLEMENTED` error for incomplete backends (#1692) diff --git a/docs/input.dox b/docs/input.dox index 41508e7b..dfb06a43 100644 --- a/docs/input.dox +++ b/docs/input.dox @@ -312,6 +312,16 @@ glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN); This mode puts no limit on the motion of the cursor. +If you wish the cursor to be visible but confined to the content area of the +window, set the cursor mode to `GLFW_CURSOR_CAPTURED`. + +@code +glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED); +@endcode + +The cursor will behave normally inside the content area but will not be able to +leave unless the window loses focus. + To exit out of either of these special modes, restore the `GLFW_CURSOR_NORMAL` cursor mode. @@ -319,6 +329,8 @@ cursor mode. glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_NORMAL); @endcode +If the cursor was disabled, this will move it back to its last visible position. + @anchor GLFW_RAW_MOUSE_MOTION @subsection raw_mouse_motion Raw mouse motion diff --git a/docs/news.dox b/docs/news.dox index 98daa3a2..1ff534b8 100644 --- a/docs/news.dox +++ b/docs/news.dox @@ -58,6 +58,14 @@ requesting a specific rendering backend when using contexts. +@subsubsection captured_cursor_34 Captured cursor mode + +GLFW now supports confining the cursor to the window content area with the @ref +GLFW_CURSOR_CAPTURED cursor mode. + +For more information see @ref cursor_mode. + + @subsubsection features_34_init_allocator Support for custom memory allocator GLFW now supports plugging a custom memory allocator at initialization with @ref @@ -234,6 +242,7 @@ then GLFW will fail to initialize. - @ref GLFW_ANGLE_PLATFORM_TYPE_VULKAN - @ref GLFW_ANGLE_PLATFORM_TYPE_METAL - @ref GLFW_X11_XCB_VULKAN_SURFACE + - @ref GLFW_CURSOR_CAPTURED @section news_archive Release notes for earlier versions diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 8f481790..8a43134c 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -1134,6 +1134,7 @@ extern "C" { #define GLFW_CURSOR_NORMAL 0x00034001 #define GLFW_CURSOR_HIDDEN 0x00034002 #define GLFW_CURSOR_DISABLED 0x00034003 +#define GLFW_CURSOR_CAPTURED 0x00034004 #define GLFW_ANY_RELEASE_BEHAVIOR 0 #define GLFW_RELEASE_BEHAVIOR_FLUSH 0x00035001 @@ -4566,6 +4567,8 @@ GLFWAPI int glfwGetInputMode(GLFWwindow* window, int mode); * - `GLFW_CURSOR_DISABLED` hides and grabs the cursor, providing virtual * and unlimited cursor movement. This is useful for implementing for * example 3D camera controls. + * - `GLFW_CURSOR_CAPTURED` makes the cursor visible and confines it to the + * content area of the window. * * If the mode is `GLFW_STICKY_KEYS`, the value must be either `GLFW_TRUE` to * enable sticky keys, or `GLFW_FALSE` to disable it. If sticky keys are diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 5bb1289b..42782280 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -1619,8 +1619,16 @@ void _glfwSetCursorPosCocoa(_GLFWwindow* window, double x, double y) void _glfwSetCursorModeCocoa(_GLFWwindow* window, int mode) { @autoreleasepool { + + if (mode == GLFW_CURSOR_CAPTURED) + { + _glfwInputError(GLFW_FEATURE_UNIMPLEMENTED, + "Cocoa: Captured cursor mode not yet implemented"); + } + if (_glfwWindowFocusedCocoa(window)) updateCursorMode(window); + } // autoreleasepool } diff --git a/src/input.c b/src/input.c index c2a7a86d..36128e10 100644 --- a/src/input.c +++ b/src/input.c @@ -596,7 +596,8 @@ GLFWAPI void glfwSetInputMode(GLFWwindow* handle, int mode, int value) { if (value != GLFW_CURSOR_NORMAL && value != GLFW_CURSOR_HIDDEN && - value != GLFW_CURSOR_DISABLED) + value != GLFW_CURSOR_DISABLED && + value != GLFW_CURSOR_CAPTURED) { _glfwInputError(GLFW_INVALID_ENUM, "Invalid cursor mode 0x%08X", diff --git a/src/win32_window.c b/src/win32_window.c index 4cf25640..f069c115 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -238,7 +238,8 @@ static void applyAspectRatio(_GLFWwindow* window, int edge, RECT* area) // static void updateCursorImage(_GLFWwindow* window) { - if (window->cursorMode == GLFW_CURSOR_NORMAL) + if (window->cursorMode == GLFW_CURSOR_NORMAL || + window->cursorMode == GLFW_CURSOR_CAPTURED) { if (window->cursor) SetCursor(window->cursor->win32.handle); @@ -586,6 +587,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l { if (window->cursorMode == GLFW_CURSOR_DISABLED) disableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + captureCursor(window); window->win32.frameAction = GLFW_FALSE; } @@ -604,6 +607,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l if (window->cursorMode == GLFW_CURSOR_DISABLED) disableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + captureCursor(window); return 0; } @@ -612,6 +617,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l { if (window->cursorMode == GLFW_CURSOR_DISABLED) enableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + releaseCursor(); if (window->monitor && window->autoIconify) _glfwIconifyWindowWin32(window); @@ -981,6 +988,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l // resizing the window or using the window menu if (window->cursorMode == GLFW_CURSOR_DISABLED) enableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + releaseCursor(); break; } @@ -995,6 +1004,8 @@ static LRESULT CALLBACK windowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM l // resizing the window or using the menu if (window->cursorMode == GLFW_CURSOR_DISABLED) disableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + captureCursor(window); break; } @@ -2166,7 +2177,7 @@ void _glfwSetCursorModeWin32(_GLFWwindow* window, int mode) disableRawMouseMotion(window); } - if (mode == GLFW_CURSOR_DISABLED) + if (mode == GLFW_CURSOR_DISABLED || mode == GLFW_CURSOR_CAPTURED) captureCursor(window); else releaseCursor(); diff --git a/src/wl_platform.h b/src/wl_platform.h index f2bf8f03..238e1ed4 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -269,6 +269,7 @@ typedef struct _GLFWwindowWayland struct zwp_relative_pointer_v1* relativePointer; struct zwp_locked_pointer_v1* lockedPointer; + struct zwp_confined_pointer_v1* confinedPointer; struct zwp_idle_inhibitor_v1* idleInhibitor; diff --git a/src/wl_window.c b/src/wl_window.c index b4b49b94..cd39c140 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -1860,6 +1860,9 @@ void _glfwDestroyWindowWayland(_GLFWwindow* window) if (window->wl.lockedPointer) zwp_locked_pointer_v1_destroy(window->wl.lockedPointer); + if (window->wl.confinedPointer) + zwp_confined_pointer_v1_destroy(window->wl.confinedPointer); + if (window->context.destroy) window->context.destroy(window); @@ -2538,6 +2541,43 @@ static void lockPointer(_GLFWwindow* window) window); } +static void confinedPointerHandleConfined(void* userData, + struct zwp_confined_pointer_v1* confinedPointer) +{ +} + +static void confinedPointerHandleUnconfined(void* userData, + struct zwp_confined_pointer_v1* confinedPointer) +{ +} + +static const struct zwp_confined_pointer_v1_listener confinedPointerListener = +{ + confinedPointerHandleConfined, + confinedPointerHandleUnconfined +}; + +static void confinePointer(_GLFWwindow* window) +{ + window->wl.confinedPointer = + zwp_pointer_constraints_v1_confine_pointer( + _glfw.wl.pointerConstraints, + window->wl.surface, + _glfw.wl.pointer, + NULL, + ZWP_POINTER_CONSTRAINTS_V1_LIFETIME_PERSISTENT); + + zwp_confined_pointer_v1_add_listener(window->wl.confinedPointer, + &confinedPointerListener, + window); +} + +static void unconfinePointer(_GLFWwindow* window) +{ + zwp_confined_pointer_v1_destroy(window->wl.confinedPointer); + window->wl.confinedPointer = NULL; +} + void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) { if (!_glfw.wl.pointer) @@ -2553,17 +2593,29 @@ void _glfwSetCursorWayland(_GLFWwindow* window, _GLFWcursor* cursor) // Update pointer lock to match cursor mode if (window->cursorMode == GLFW_CURSOR_DISABLED) { + if (window->wl.confinedPointer) + unconfinePointer(window); if (!window->wl.lockedPointer) lockPointer(window); } + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + { + if (window->wl.lockedPointer) + unlockPointer(window); + if (!window->wl.confinedPointer) + confinePointer(window); + } else if (window->cursorMode == GLFW_CURSOR_NORMAL || window->cursorMode == GLFW_CURSOR_HIDDEN) { if (window->wl.lockedPointer) unlockPointer(window); + else if (window->wl.confinedPointer) + unconfinePointer(window); } - if (window->cursorMode == GLFW_CURSOR_NORMAL) + if (window->cursorMode == GLFW_CURSOR_NORMAL || + window->cursorMode == GLFW_CURSOR_CAPTURED) { if (cursor) setCursorImage(window, &cursor->wl); diff --git a/src/x11_window.c b/src/x11_window.c index f323db6c..4f605771 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -453,7 +453,8 @@ static char* convertLatin1toUTF8(const char* source) // static void updateCursorImage(_GLFWwindow* window) { - if (window->cursorMode == GLFW_CURSOR_NORMAL) + if (window->cursorMode == GLFW_CURSOR_NORMAL || + window->cursorMode == GLFW_CURSOR_CAPTURED) { if (window->cursor) { @@ -1705,6 +1706,8 @@ static void processEvent(XEvent *event) if (window->cursorMode == GLFW_CURSOR_DISABLED) disableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + captureCursor(window); if (window->x11.ic) XSetICFocus(window->x11.ic); @@ -1725,6 +1728,8 @@ static void processEvent(XEvent *event) if (window->cursorMode == GLFW_CURSOR_DISABLED) enableCursor(window); + else if (window->cursorMode == GLFW_CURSOR_CAPTURED) + releaseCursor(); if (window->x11.ic) XUnsetICFocus(window->x11.ic); @@ -2831,7 +2836,7 @@ void _glfwSetCursorModeX11(_GLFWwindow* window, int mode) disableRawMouseMotion(window); } - if (mode == GLFW_CURSOR_DISABLED) + if (mode == GLFW_CURSOR_DISABLED || mode == GLFW_CURSOR_CAPTURED) captureCursor(window); else releaseCursor(); @@ -3003,7 +3008,8 @@ void _glfwDestroyCursorX11(_GLFWcursor* cursor) void _glfwSetCursorX11(_GLFWwindow* window, _GLFWcursor* cursor) { - if (window->cursorMode == GLFW_CURSOR_NORMAL) + if (window->cursorMode == GLFW_CURSOR_NORMAL || + window->cursorMode == GLFW_CURSOR_CAPTURED) { updateCursorImage(window); XFlush(_glfw.x11.display); diff --git a/tests/cursor.c b/tests/cursor.c index 9be42748..37f3299c 100644 --- a/tests/cursor.c +++ b/tests/cursor.c @@ -172,7 +172,8 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, case GLFW_KEY_ESCAPE: { - if (glfwGetInputMode(window, GLFW_CURSOR) != GLFW_CURSOR_DISABLED) + const int mode = glfwGetInputMode(window, GLFW_CURSOR); + if (mode != GLFW_CURSOR_DISABLED && mode != GLFW_CURSOR_CAPTURED) { glfwSetWindowShouldClose(window, GLFW_TRUE); break; @@ -197,6 +198,11 @@ static void key_callback(GLFWwindow* window, int key, int scancode, int action, printf("(( cursor is hidden ))\n"); break; + case GLFW_KEY_C: + glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_CAPTURED); + printf("(( cursor is captured ))\n"); + break; + case GLFW_KEY_R: if (!glfwRawMouseMotionSupported()) break; From 7be6a2cabec3a50419448cc618792d2949d988fd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 25 Jul 2022 13:04:21 +0200 Subject: [PATCH 07/15] Wayland: Clean up pointer helper function order --- src/wl_window.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/wl_window.c b/src/wl_window.c index cd39c140..b5989086 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2492,15 +2492,6 @@ static void lockedPointerHandleLocked(void* userData, { } -static void unlockPointer(_GLFWwindow* window) -{ - zwp_relative_pointer_v1_destroy(window->wl.relativePointer); - window->wl.relativePointer = NULL; - - zwp_locked_pointer_v1_destroy(window->wl.lockedPointer); - window->wl.lockedPointer = NULL; -} - static void lockedPointerHandleUnlocked(void* userData, struct zwp_locked_pointer_v1* lockedPointer) { @@ -2541,6 +2532,15 @@ static void lockPointer(_GLFWwindow* window) window); } +static void unlockPointer(_GLFWwindow* window) +{ + zwp_relative_pointer_v1_destroy(window->wl.relativePointer); + window->wl.relativePointer = NULL; + + zwp_locked_pointer_v1_destroy(window->wl.lockedPointer); + window->wl.lockedPointer = NULL; +} + static void confinedPointerHandleConfined(void* userData, struct zwp_confined_pointer_v1* confinedPointer) { From 3a5c726d1bd485e63219c2a6c3e377f434d6ee10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 25 Jul 2022 16:28:23 +0200 Subject: [PATCH 08/15] Wayland: Remove unreachable code This platform function is only called if the cursor is enabled. --- src/wl_window.c | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/wl_window.c b/src/wl_window.c index b5989086..fc5e5783 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2252,13 +2252,6 @@ void _glfwGetCursorPosWayland(_GLFWwindow* window, double* xpos, double* ypos) void _glfwSetCursorPosWayland(_GLFWwindow* window, double x, double y) { - if (window->wl.lockedPointer) - { - zwp_locked_pointer_v1_set_cursor_position_hint( - window->wl.lockedPointer, - wl_fixed_from_double(x), - wl_fixed_from_double(y)); - } } void _glfwSetCursorModeWayland(_GLFWwindow* window, int mode) From 3a60992a418aad88717db32353bec22e8bb7dab3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 25 Jul 2022 16:30:11 +0200 Subject: [PATCH 09/15] Wayland: Add error for unsupported feature --- src/wl_window.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/wl_window.c b/src/wl_window.c index fc5e5783..76d5f15b 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -2252,6 +2252,8 @@ void _glfwGetCursorPosWayland(_GLFWwindow* window, double* xpos, double* ypos) void _glfwSetCursorPosWayland(_GLFWwindow* window, double x, double y) { + _glfwInputError(GLFW_FEATURE_UNAVAILABLE, + "Wayland: The platform does not support setting the cursor position"); } void _glfwSetCursorModeWayland(_GLFWwindow* window, int mode) From 2e12ef00bb4b96c7b4c6ea14f594cc8f50ea718f Mon Sep 17 00:00:00 2001 From: TheChocolateOre <50585073+TheChocolateOre@users.noreply.github.com> Date: Thu, 28 Jul 2022 16:56:18 +0300 Subject: [PATCH 10/15] Fix typos and other issues in docs This fixes spelling, grammar and punctuation issues, missing words and stray words across the documentation. A confusing sentence was removed from the tutorial. Closes #2085 --- docs/build.dox | 2 +- docs/compat.dox | 6 +++--- docs/compile.dox | 14 +++++++------- docs/context.dox | 6 +++--- docs/input.dox | 4 ++-- docs/intro.dox | 16 ++++++++-------- docs/monitor.dox | 4 ++-- docs/moving.dox | 6 +++--- docs/news.dox | 8 ++++---- docs/quick.dox | 6 +----- docs/vulkan.dox | 2 +- 11 files changed, 35 insertions(+), 39 deletions(-) diff --git a/docs/build.dox b/docs/build.dox index c7677037..a1625d60 100644 --- a/docs/build.dox +++ b/docs/build.dox @@ -26,7 +26,7 @@ GLFW. @endcode This header defines all the constants and declares all the types and function -prototypes of the GLFW API. By default it also includes the OpenGL header from +prototypes of the GLFW API. By default, it also includes the OpenGL header from your development environment. See [option macros](@ref build_macros) below for how to select OpenGL ES headers and more. diff --git a/docs/compat.dox b/docs/compat.dox index 989c4c19..94372197 100644 --- a/docs/compat.dox +++ b/docs/compat.dox @@ -152,7 +152,7 @@ formats. If GLX 1.3 is not supported, @ref glfwInit will fail. GLFW uses the `GLX_MESA_swap_control,` `GLX_EXT_swap_control` and `GLX_SGI_swap_control` extensions to provide vertical retrace synchronization -(or _vsync_), in that order of preference. Where none of these extension are +(or _vsync_), in that order of preference. When none of these extensions are available, calling @ref glfwSwapInterval will have no effect. GLFW uses the `GLX_ARB_multisample` extension to create contexts with @@ -219,8 +219,8 @@ extension is unavailable, the `GLFW_CONTEXT_RELEASE_BEHAVIOR` hint will have no effect and the context will always be flushed when released. GLFW uses the `WGL_ARB_framebuffer_sRGB` and `WGL_EXT_framebuffer_sRGB` -extensions to provide support for sRGB framebuffers. Where both of these -extension are unavailable, the `GLFW_SRGB_CAPABLE` hint will have no effect. +extensions to provide support for sRGB framebuffers. When both of these +extensions are unavailable, the `GLFW_SRGB_CAPABLE` hint will have no effect. @section compat_osx OpenGL on macOS diff --git a/docs/compile.dox b/docs/compile.dox index 925ab1ab..3490eb15 100644 --- a/docs/compile.dox +++ b/docs/compile.dox @@ -45,7 +45,7 @@ Linux and FreeBSD you will need a few extra packages. To compile GLFW for X11, you need to have the X11 development packages installed. They are not needed to build or run programs that use GLFW. -On Debian and derivates like Ubuntu and Linux Mint the `xorg-dev` meta-package +On Debian and derivatives like Ubuntu and Linux Mint the `xorg-dev` meta-package pulls in the development packages for all of X11. @code{.sh} @@ -83,7 +83,7 @@ development packages installed. They are not needed to build or run programs th GLFW. You will also need to set the @ref GLFW_BUILD_WAYLAND CMake option in the next step when generating build files. -On Debian and derivates like Ubuntu and Linux Mint you will need the `libwayland-dev`, +On Debian and derivatives like Ubuntu and Linux Mint you will need the `libwayland-dev`, `libxkbcommon-dev` and `wayland-protocols` packages and the `xorg-dev` meta-package. These will pull in all other dependencies. @@ -142,7 +142,7 @@ If you wish change any CMake variables in the list, press _Configure_ and then _Generate_ to have the new values take effect. The variable list will be populated after the first configure step. -By default GLFW will use X11 on Linux and other Unix-like systems other than macOS. To +By default, GLFW will use X11 on Linux and other Unix-like systems other than macOS. To include support for Wayland as well, set the @ref GLFW_BUILD_WAYLAND option in the GLFW section of the variable list, then apply the new value as described above. @@ -176,7 +176,7 @@ flag. cmake -S path/to/glfw -B path/to/build -G Xcode @endcode -By default GLFW will use X11 on Linux and other Unix-like systems other +By default, GLFW will use X11 on Linux and other Unix-like systems other than macOS. To also include support for Wayland, set the @ref GLFW_BUILD_WAYLAND CMake option. @@ -263,12 +263,12 @@ build GLFW as a static library, `SHARED` to build it as a shared library @anchor GLFW_BUILD_EXAMPLES __GLFW_BUILD_EXAMPLES__ determines whether the GLFW examples are built along with the library. This is enabled by default unless GLFW is being built -as a sub-project of a larger CMake project. +as a subproject of a larger CMake project. @anchor GLFW_BUILD_TESTS __GLFW_BUILD_TESTS__ determines whether the GLFW test programs are built along with the library. This is enabled by default unless GLFW is being -built as a sub-project of a larger CMake project. +built as a subproject of a larger CMake project. @anchor GLFW_BUILD_DOCS __GLFW_BUILD_DOCS__ determines whether the GLFW documentation is built along @@ -358,7 +358,7 @@ For more details see the @section compile_manual Compiling GLFW manually If you wish to compile GLFW without its CMake build environment then you will have to do -at least some of the platform detection yourself. There are preprocessor macros for +at least some platform-detection yourself. There are preprocessor macros for enabling support for the platforms (window systems) available. There are also optional, platform-specific macros for various features. diff --git a/docs/context.dox b/docs/context.dox index c51e268c..c64a0709 100644 --- a/docs/context.dox +++ b/docs/context.dox @@ -61,7 +61,7 @@ information. The name and number of this chapter unfortunately varies between versions and APIs, but has at times been named _Shared Objects and Multiple Contexts_. -GLFW comes with a barebones object sharing example program called `sharing`. +GLFW comes with a bare-bones object sharing example program called `sharing`. @subsection context_offscreen Offscreen contexts @@ -189,7 +189,7 @@ it suppresses the development environment's OpenGL or OpenGL ES header. #include @endcode -Finally you need to initialize glad once you have a suitable current context. +Finally, you need to initialize glad once you have a suitable current context. @code window = glfwCreateWindow(640, 480, "My Window", NULL, NULL); @@ -205,7 +205,7 @@ gladLoadGLLoader((GLADloadproc) glfwGetProcAddress); Once glad has been loaded, you have access to all OpenGL core and extension functions supported by both the context you created and the glad loader you -generated and you are ready to start rendering. +generated. After that, you are ready to start rendering. You can specify a minimum required OpenGL or OpenGL ES version with [context hints](@ref window_hints_ctx). If your needs are more complex, you can diff --git a/docs/input.dox b/docs/input.dox index dfb06a43..d3904f46 100644 --- a/docs/input.dox +++ b/docs/input.dox @@ -24,7 +24,7 @@ All input callbacks receive a window handle. By using the or objects from your callbacks. To get a better feel for how the various events callbacks behave, run the -`events` test program. It register every callback supported by GLFW and prints +`events` test program. It registers every callback supported by GLFW and prints out all arguments provided for every event, along with time and sequence information. @@ -394,7 +394,7 @@ sequential rows, starting from the top-left corner. @subsubsection cursor_standard Standard cursor creation A cursor with a [standard shape](@ref shapes) from the current system cursor -theme can be can be created with @ref glfwCreateStandardCursor. +theme can be created with @ref glfwCreateStandardCursor. @code GLFWcursor* url_cursor = glfwCreateStandardCursor(GLFW_POINTING_HAND_CURSOR); diff --git a/docs/intro.dox b/docs/intro.dox index 5cbd7eb0..79348323 100644 --- a/docs/intro.dox +++ b/docs/intro.dox @@ -162,7 +162,7 @@ GLFW can be compiled for more than one platform (window system) at once. This l a single library binary support both X11 and Wayland on Linux and other Unix-like systems. You can control platform selection via the @ref GLFW_PLATFORM initialization hint. By -default this is set to @ref GLFW_ANY_PLATFORM, which will look for supported window +default, this is set to @ref GLFW_ANY_PLATFORM, which will look for supported window systems in order of priority and select the first one it finds. It can also be set to any specific platform to have GLFW only look for that one. @@ -269,9 +269,9 @@ This will destroy any remaining window, monitor and cursor objects, restore any modified gamma ramps, re-enable the screensaver if it had been disabled and free any other resources allocated by GLFW. -Once the library is terminated, it is as if it had never been initialized and +Once the library is terminated, it is as if it had never been initialized, therefore you will need to initialize it again before being able to use GLFW. If the -library was not initialized or had already been terminated, it return +library was not initialized or had already been terminated, it returns immediately. @@ -391,14 +391,14 @@ which monitor the window is currently considered to be on. This section describes the conditions under which GLFW can be expected to function, barring bugs in the operating system or drivers. Use of GLFW outside -of these limits may work on some platforms, or on some machines, or some of the +these limits may work on some platforms, or on some machines, or some of the time, or on some versions of GLFW, but it may break at any time and this will not be considered a bug. @subsection lifetime Pointer lifetimes -GLFW will never free any pointer you provide to it and you must never free any +GLFW will never free any pointer you provide to it, and you must never free any pointer it provides to you. Many GLFW functions return pointers to dynamically allocated structures, strings @@ -602,15 +602,15 @@ The format of the string is as follows: - The names of the always supported context creation APIs EGL and OSMesa - Any additional compile-time options, APIs and (on Windows) what compiler was used -For example, GLFW 3.4 compiled as a DLL for Windows with MinGW may have a version string +For example, compiling GLFW 3.4 with MinGW as a DLL for Windows, may result in a version string like this: @code 3.4.0 Win32 WGL Null EGL OSMesa MinGW DLL @endcode -While GLFW compiled as as static library for Linux with both Wayland and X11 enabled may -have a version string like this: +Compiling GLFW as a static library for Linux, with both Wayland and X11 enabled, may +result in a version string like this: @code 3.4.0 Wayland X11 GLX Null EGL OSMesa monotonic diff --git a/docs/monitor.dox b/docs/monitor.dox index 86eb4540..b4099dbf 100644 --- a/docs/monitor.dox +++ b/docs/monitor.dox @@ -138,7 +138,7 @@ glfwGetMonitorPhysicalSize(monitor, &width_mm, &height_mm); @endcode While this can be used to calculate the raw DPI of a monitor, this is often not -useful. Instead use the [monitor content scale](@ref monitor_scale) and +useful. Instead, use the [monitor content scale](@ref monitor_scale) and [window content scale](@ref window_scale) to scale your content. @@ -261,7 +261,7 @@ To experiment with gamma correction via the @ref glfwSetGamma function, run the `gamma` test program. @note The software controlled gamma ramp is applied _in addition_ to the -hardware gamma correction, which today is usually an approximation of sRGB +hardware gamma correction, which today is typically an approximation of sRGB gamma. This means that setting a perfectly linear ramp, or gamma 1.0, will produce the default (usually sRGB-like) behavior. diff --git a/docs/moving.dox b/docs/moving.dox index b80d84a2..705b4fa8 100644 --- a/docs/moving.dox +++ b/docs/moving.dox @@ -243,7 +243,7 @@ while (!glfwWindowShouldClose(window)) @endcode The close callback no longer returns a value. Instead, it is called after the -close flag has been set so it can override its value, if it chooses to, before +close flag has been set, so it can optionally override its value, before event processing completes. You may however not call @ref glfwDestroyWindow from the close callback (or any other window related callback). @@ -350,11 +350,11 @@ from a repeat. Note that @ref glfwGetKey still returns only `GLFW_PRESS` or GLFW 3 key tokens map to physical keys, unlike in GLFW 2 where they mapped to the values generated by the current keyboard layout. The tokens are named -according to the values they would have using the standard US layout, but this +according to the values they would have in the standard US layout, but this is only a convenience, as most programmers are assumed to know that layout. This means that (for example) `GLFW_KEY_LEFT_BRACKET` is always a single key and is the same key in the same place regardless of what keyboard layouts the users -of your program has. +of your program have. The key input facility was never meant for text input, although using it that way worked slightly better in GLFW 2. If you were using it to input text, you diff --git a/docs/news.dox b/docs/news.dox index 1ff534b8..38110b14 100644 --- a/docs/news.dox +++ b/docs/news.dox @@ -21,7 +21,7 @@ support for a given platform is compiled in with @ref glfwPlatformSupported. GLFW now provides the standard cursor shapes @ref GLFW_RESIZE_NWSE_CURSOR and @ref GLFW_RESIZE_NESW_CURSOR for diagonal resizing, @ref GLFW_RESIZE_ALL_CURSOR -for omni-directional resizing and @ref GLFW_NOT_ALLOWED_CURSOR for showing an +for omnidirectional resizing and @ref GLFW_NOT_ALLOWED_CURSOR for showing an action is not allowed. Unlike the original set, these shapes may not be available everywhere and @@ -92,7 +92,7 @@ applications. Because GLFW now supports runtime selection of platform (window system), a library binary may export native access functions for multiple platforms. Starting with version 3.4 you must not assume that GLFW is running on a platform just because it exports native access -functions for it. After initialization you can query the selected platform with @ref +functions for it. After initialization, you can query the selected platform with @ref glfwGetPlatform. @@ -118,7 +118,7 @@ To work around this, call any joystick function before waiting for events, for example by setting a [joystick callback](@ref joystick_event). -@subsubsection standalone_34 Tests and examples are disabled when built as a sub-project +@subsubsection standalone_34 Tests and examples are disabled when built as a subproject GLFW now does not build the tests and examples when it is added as a subdirectory of another CMake project. To enable these, set the @ref @@ -152,7 +152,7 @@ GLFW_TRANSPARENT_FRAMEBUFFER on Windows 7 if DWM transparency is off (the Transparency setting under Personalization > Window Color). -@subsubsection emptyevents_34 Empty events on X11 no longer roundtrip to server +@subsubsection emptyevents_34 Empty events on X11 no longer round-trip to server Events posted with @ref glfwPostEmptyEvent now use a separate unnamed pipe instead of sending an X11 client event to the helper window. diff --git a/docs/quick.dox b/docs/quick.dox index c3f47aa1..8824ff5b 100644 --- a/docs/quick.dox +++ b/docs/quick.dox @@ -149,10 +149,6 @@ if (!window) } @endcode -The window handle is passed to all window related functions and is provided to -along to all window related callbacks, so they can tell which window received -the event. - When a window and context is no longer needed, destroy it. @code @@ -238,7 +234,7 @@ events as described below. @subsection quick_render Rendering with OpenGL Once you have a current OpenGL context, you can use OpenGL normally. In this -tutorial, a multi-colored rotating triangle will be rendered. The framebuffer +tutorial, a multicolored rotating triangle will be rendered. The framebuffer size needs to be retrieved for `glViewport`. @code diff --git a/docs/vulkan.dox b/docs/vulkan.dox index 8e9821ca..5e38c014 100644 --- a/docs/vulkan.dox +++ b/docs/vulkan.dox @@ -142,7 +142,7 @@ PFN_vkGetDeviceProcAddr pfnGetDeviceProcAddr = (PFN_vkGetDeviceProcAddr) glfwGetInstanceProcAddress(instance, "vkGetDeviceProcAddr"); @endcode -Device-specific functions may execute a little bit faster, due to not having to +Device-specific functions may execute a little faster, due to not having to dispatch internally based on the device passed to them. For more information about `vkGetDeviceProcAddr`, see the Vulkan documentation. From 2efc598d70be92267a97c95ac08668565784ab19 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 26 Jul 2022 19:21:07 +0200 Subject: [PATCH 11/15] Fix native access macros being mutually exclusive The documentation was updated with the introduction of run-time platform selection, but the preprocessor logic was not. --- include/GLFW/glfw3native.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/include/GLFW/glfw3native.h b/include/GLFW/glfw3native.h index 36934120..171abe36 100644 --- a/include/GLFW/glfw3native.h +++ b/include/GLFW/glfw3native.h @@ -103,17 +103,23 @@ extern "C" { #undef GLFW_APIENTRY_DEFINED #endif #include - #elif defined(GLFW_EXPOSE_NATIVE_COCOA) || defined(GLFW_EXPOSE_NATIVE_NSGL) + #endif + + #if defined(GLFW_EXPOSE_NATIVE_COCOA) || defined(GLFW_EXPOSE_NATIVE_NSGL) #if defined(__OBJC__) #import #else #include #include #endif - #elif defined(GLFW_EXPOSE_NATIVE_X11) || defined(GLFW_EXPOSE_NATIVE_GLX) + #endif + + #if defined(GLFW_EXPOSE_NATIVE_X11) || defined(GLFW_EXPOSE_NATIVE_GLX) #include #include - #elif defined(GLFW_EXPOSE_NATIVE_WAYLAND) + #endif + + #if defined(GLFW_EXPOSE_NATIVE_WAYLAND) #include #endif From 7d73629e5071b0baea87135f01a4335b10740068 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 26 Oct 2021 20:30:29 +0200 Subject: [PATCH 12/15] X11: Preserve unrelated values in WM_NORMAL_HINTS This stops GLFW overwriting the whole WM_NORMAL_HINTS property every time it updates size-related parts of it. --- src/x11_window.c | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/src/x11_window.c b/src/x11_window.c index 4f605771..b4ce8669 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -270,6 +270,11 @@ static void updateNormalHints(_GLFWwindow* window, int width, int height) { XSizeHints* hints = XAllocSizeHints(); + long supplied; + XGetWMNormalHints(_glfw.x11.display, window->x11.handle, hints, &supplied); + + hints->flags &= ~(PMinSize | PMaxSize | PAspect); + if (!window->monitor) { if (window->resizable) @@ -306,9 +311,6 @@ static void updateNormalHints(_GLFWwindow* window, int width, int height) } } - hints->flags |= PWinGravity; - hints->win_gravity = StaticGravity; - XSetWMNormalHints(_glfw.x11.display, window->x11.handle, hints); XFree(hints); } @@ -696,7 +698,28 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, XFree(hints); } - updateNormalHints(window, width, height); + // Set ICCCM WM_NORMAL_HINTS property + { + XSizeHints* hints = XAllocSizeHints(); + if (!hints) + { + _glfwInputError(GLFW_OUT_OF_MEMORY, "X11: Failed to allocate size hints"); + return GLFW_FALSE; + } + + if (!wndconfig->resizable) + { + hints->flags |= (PMinSize | PMaxSize); + hints->min_width = hints->max_width = width; + hints->min_height = hints->max_height = height; + } + + hints->flags |= PWinGravity; + hints->win_gravity = StaticGravity; + + XSetWMNormalHints(_glfw.x11.display, window->x11.handle, hints); + XFree(hints); + } // Set ICCCM WM_CLASS property { From 0f9a9578f38518317d0b3305dd816ba7b400972d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Tue, 26 Oct 2021 14:25:03 +0200 Subject: [PATCH 13/15] Add window hints for initial position This adds window hints for the initial position, in screen coordinates, of a window. The special value GLFW_ANY_POSITION means the window manager will be allowed to position the window. It is not possible to set window positions on Wayland and GLFW will always behave as if these hints are set to GLFW_ANY_POSITION. Fixes #1603 Fixes #1747 --- README.md | 3 +++ docs/news.dox | 11 +++++++++++ docs/window.dox | 25 ++++++++++++++++++++++++- examples/windows.c | 10 +++------- include/GLFW/glfw3.h | 22 ++++++++++++++++++---- src/cocoa_window.m | 26 +++++++++++++++++++++----- src/internal.h | 2 ++ src/null_window.c | 13 +++++++++++-- src/win32_window.c | 22 ++++++++++++++++------ src/window.c | 8 ++++++++ src/x11_window.c | 19 ++++++++++++++++++- tests/threads.c | 8 +++----- 12 files changed, 138 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index c84f8cb5..db76a6fa 100644 --- a/README.md +++ b/README.md @@ -141,6 +141,9 @@ information on what to include when reporting a bug. through the window (#1236,#1568) - Added `GLFW_CURSOR_CAPTURED` cursor mode to confine the cursor to the window content area (#58) + - Added `GLFW_POSITION_X` and `GLFW_POSITION_Y` window hints for initial position + (#1603,#1747) + - Added `GLFW_ANY_POSITION` hint value for letting the window manager choose (#1603,#1747) - Added `GLFW_PLATFORM_UNAVAILABLE` error for platform detection failures (#1958) - Added `GLFW_FEATURE_UNAVAILABLE` error for platform limitations (#1692) - Added `GLFW_FEATURE_UNIMPLEMENTED` error for incomplete backends (#1692) diff --git a/docs/news.dox b/docs/news.dox index 38110b14..8601d6c3 100644 --- a/docs/news.dox +++ b/docs/news.dox @@ -76,6 +76,14 @@ function pointers corresponding to the standard library functions `malloc`, For more information see @ref init_allocator. +@subsubsection features_34_position_hint Window hints for initial position + +GLFW now provides the @ref GLFW_POSITION_X and @ref GLFW_POSITION_Y window hints for +specifying the initial position of the window. This removes the need to create a hidden +window, move it and then show it. The default value of these hints is +`GLFW_ANY_POSITION`, which selects the previous behavior. + + @subsubsection features_34_win32_keymenu Support for keyboard access to Windows window menu GLFW now provides the @@ -243,6 +251,9 @@ then GLFW will fail to initialize. - @ref GLFW_ANGLE_PLATFORM_TYPE_METAL - @ref GLFW_X11_XCB_VULKAN_SURFACE - @ref GLFW_CURSOR_CAPTURED + - @ref GLFW_POSITION_X + - @ref GLFW_POSITION_Y + - @ref GLFW_ANY_POSITION @section news_archive Release notes for earlier versions diff --git a/docs/window.dox b/docs/window.dox index 451aca73..2852a809 100644 --- a/docs/window.dox +++ b/docs/window.dox @@ -256,6 +256,14 @@ This is only supported for undecorated windows. Decorated windows with this enabled will behave differently between platforms. Possible values are `GLFW_TRUE` and `GLFW_FALSE`. +@anchor GLFW_POSITION_X +@anchor GLFW_POSITION_Y +__GLFW_POSITION_X__ and __GLFW_POSITION_Y__ specify the desired initial position +of the window. The window manager may modify or ignore these coordinates. If +either or both of these hints are set to `GLFW_ANY_POSITION` then the window +manager will position the window where it thinks the user will prefer it. +Possible values are any valid screen coordinates and `GLFW_ANY_POSITION`. + @subsubsection window_hints_fb Framebuffer related hints @@ -516,6 +524,8 @@ GLFW_TRANSPARENT_FRAMEBUFFER | `GLFW_FALSE` | `GLFW_TRUE` or `GL GLFW_FOCUS_ON_SHOW | `GLFW_TRUE` | `GLFW_TRUE` or `GLFW_FALSE` GLFW_SCALE_TO_MONITOR | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE` GLFW_MOUSE_PASSTHROUGH | `GLFW_FALSE` | `GLFW_TRUE` or `GLFW_FALSE` +GLFW_POSITION_X | `GLFW_ANY_POSITION` | Any valid screen x-coordinate or `GLFW_ANY_POSITION` +GLFW_POSITION_Y | `GLFW_ANY_POSITION` | Any valid screen y-coordinate or `GLFW_ANY_POSITION` GLFW_RED_BITS | 8 | 0 to `INT_MAX` or `GLFW_DONT_CARE` GLFW_GREEN_BITS | 8 | 0 to `INT_MAX` or `GLFW_DONT_CARE` GLFW_BLUE_BITS | 8 | 0 to `INT_MAX` or `GLFW_DONT_CARE` @@ -798,7 +808,20 @@ are undefined if they conflict. @subsection window_pos Window position -The position of a windowed-mode window can be changed with @ref +By default, the window manager chooses the position of new windowed mode +windows, based on its size and which monitor the user appears to be working on. +This is most often the right choice. If you need to create a window at +a specific position, you can set the desired position with the @ref +GLFW_POSITION_X and @ref GLFW_POSITION_Y window hints. + +@code +glfwWindowHint(GLFW_POSITION_X, 70); +glfwWindowHint(GLFW_POSITION_Y, 83); +@endcode + +To restore the previous behavior, set these hints to `GLFW_ANY_POSITION`. + +The position of a windowed mode window can be changed with @ref glfwSetWindowPos. This moves the window so that the upper-left corner of its content area has the specified [screen coordinates](@ref coordinate_systems). The window system may put limitations on window placement. diff --git a/examples/windows.c b/examples/windows.c index 598e5218..1589ffbf 100644 --- a/examples/windows.c +++ b/examples/windows.c @@ -44,7 +44,6 @@ int main(int argc, char** argv) exit(EXIT_FAILURE); } - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); glfwWindowHint(GLFW_DECORATED, GLFW_FALSE); glfwGetMonitorWorkarea(glfwGetPrimaryMonitor(), &xpos, &ypos, NULL, &height); @@ -66,6 +65,9 @@ int main(int argc, char** argv) if (i > 0) glfwWindowHint(GLFW_FOCUS_ON_SHOW, GLFW_FALSE); + glfwWindowHint(GLFW_POSITION_X, xpos + size * (1 + (i & 1))); + glfwWindowHint(GLFW_POSITION_Y, ypos + size * (1 + (i >> 1))); + windows[i] = glfwCreateWindow(size, size, "Multi-Window Example", NULL, NULL); if (!windows[i]) { @@ -75,9 +77,6 @@ int main(int argc, char** argv) exit(EXIT_FAILURE); } - glfwSetWindowPos(windows[i], - xpos + size * (1 + (i & 1)), - ypos + size * (1 + (i >> 1))); glfwSetInputMode(windows[i], GLFW_STICKY_KEYS, GLFW_TRUE); glfwMakeContextCurrent(windows[i]); @@ -85,9 +84,6 @@ int main(int argc, char** argv) glClearColor(colors[i].r, colors[i].g, colors[i].b, 1.f); } - for (int i = 0; i < 4; i++) - glfwShowWindow(windows[i]); - for (;;) { for (int i = 0; i < 4; i++) diff --git a/include/GLFW/glfw3.h b/include/GLFW/glfw3.h index 8a43134c..26875465 100644 --- a/include/GLFW/glfw3.h +++ b/include/GLFW/glfw3.h @@ -927,6 +927,18 @@ extern "C" { */ #define GLFW_MOUSE_PASSTHROUGH 0x0002000D +/*! @brief Initial position x-coordinate window hint. + * + * Initial position x-coordinate [window hint](@ref GLFW_POSITION_X). + */ +#define GLFW_POSITION_X 0x0002000E + +/*! @brief Initial position y-coordinate window hint. + * + * Initial position y-coordinate [window hint](@ref GLFW_POSITION_Y). + */ +#define GLFW_POSITION_Y 0x0002000F + /*! @brief Framebuffer bit depth hint. * * Framebuffer bit depth [hint](@ref GLFW_RED_BITS). @@ -1152,6 +1164,8 @@ extern "C" { #define GLFW_ANGLE_PLATFORM_TYPE_VULKAN 0x00037007 #define GLFW_ANGLE_PLATFORM_TYPE_METAL 0x00037008 +#define GLFW_ANY_POSITION 0x80000000 + /*! @defgroup shapes Standard cursor shapes * @brief Standard system cursor shapes. * @@ -3036,10 +3050,10 @@ GLFWAPI void glfwWindowHintString(int hint, const char* value); * OpenGL or OpenGL ES context. * * By default, newly created windows use the placement recommended by the - * window system. To create the window at a specific position, make it - * initially invisible using the [GLFW_VISIBLE](@ref GLFW_VISIBLE_hint) window - * hint, set its [position](@ref window_pos) and then [show](@ref window_hide) - * it. + * window system. To create the window at a specific position, set the @ref + * GLFW_POSITION_X and @ref GLFW_POSITION_Y window hints before creation. To + * restore the default behavior, set either or both hints back to + * `GLFW_ANY_POSITION`. * * As long as at least one full screen window is not iconified, the screensaver * is prohibited from starting. diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 42782280..daac39b3 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -791,7 +791,19 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, contentRect = NSMakeRect(xpos, ypos, mode.width, mode.height); } else - contentRect = NSMakeRect(0, 0, wndconfig->width, wndconfig->height); + { + if (wndconfig->xpos == GLFW_ANY_POSITION || + wndconfig->ypos == GLFW_ANY_POSITION) + { + contentRect = NSMakeRect(0, 0, wndconfig->width, wndconfig->height); + } + else + { + const int xpos = wndconfig->xpos; + const int ypos = _glfwTransformYCocoa(wndconfig->ypos + wndconfig->height - 1); + contentRect = NSMakeRect(xpos, ypos, wndconfig->width, wndconfig->height); + } + } NSUInteger styleMask = NSWindowStyleMaskMiniaturizable; @@ -821,10 +833,14 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, [window->ns.object setLevel:NSMainMenuWindowLevel + 1]; else { - [(NSWindow*) window->ns.object center]; - _glfw.ns.cascadePoint = - NSPointToCGPoint([window->ns.object cascadeTopLeftFromPoint: - NSPointFromCGPoint(_glfw.ns.cascadePoint)]); + if (wndconfig->xpos == GLFW_ANY_POSITION || + wndconfig->ypos == GLFW_ANY_POSITION) + { + [(NSWindow*) window->ns.object center]; + _glfw.ns.cascadePoint = + NSPointToCGPoint([window->ns.object cascadeTopLeftFromPoint: + NSPointFromCGPoint(_glfw.ns.cascadePoint)]); + } if (wndconfig->resizable) { diff --git a/src/internal.h b/src/internal.h index 8d576d1d..5aa22f59 100644 --- a/src/internal.h +++ b/src/internal.h @@ -396,6 +396,8 @@ struct _GLFWinitconfig // struct _GLFWwndconfig { + int xpos; + int ypos; int width; int height; const char* title; diff --git a/src/null_window.c b/src/null_window.c index 8a7cae41..5cdf3e23 100644 --- a/src/null_window.c +++ b/src/null_window.c @@ -82,8 +82,17 @@ static int createNativeWindow(_GLFWwindow* window, fitToMonitor(window); else { - window->null.xpos = 17; - window->null.ypos = 17; + if (wndconfig->xpos == GLFW_ANY_POSITION && wndconfig->ypos == GLFW_ANY_POSITION) + { + window->null.xpos = 17; + window->null.ypos = 17; + } + else + { + window->null.xpos = wndconfig->xpos; + window->null.ypos = wndconfig->ypos; + } + window->null.width = wndconfig->width; window->null.height = wndconfig->height; } diff --git a/src/win32_window.c b/src/win32_window.c index f069c115..69fb6422 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -1316,17 +1316,27 @@ static int createNativeWindow(_GLFWwindow* window, } else { - xpos = CW_USEDEFAULT; - ypos = CW_USEDEFAULT; + RECT rect = { 0, 0, wndconfig->width, wndconfig->height }; window->win32.maximized = wndconfig->maximized; if (wndconfig->maximized) style |= WS_MAXIMIZE; - getFullWindowSize(style, exStyle, - wndconfig->width, wndconfig->height, - &fullWidth, &fullHeight, - USER_DEFAULT_SCREEN_DPI); + AdjustWindowRectEx(&rect, style, FALSE, exStyle); + + if (wndconfig->xpos == GLFW_ANY_POSITION && wndconfig->ypos == GLFW_ANY_POSITION) + { + xpos = CW_USEDEFAULT; + ypos = CW_USEDEFAULT; + } + else + { + xpos = wndconfig->xpos + rect.left; + ypos = wndconfig->ypos + rect.top; + } + + fullWidth = rect.right - rect.left; + fullHeight = rect.bottom - rect.top; } wideTitle = _glfwCreateWideStringFromUTF8Win32(wndconfig->title); diff --git a/src/window.c b/src/window.c index f740580a..1c8519ff 100644 --- a/src/window.c +++ b/src/window.c @@ -274,6 +274,8 @@ void glfwDefaultWindowHints(void) _glfw.hints.window.autoIconify = GLFW_TRUE; _glfw.hints.window.centerCursor = GLFW_TRUE; _glfw.hints.window.focusOnShow = GLFW_TRUE; + _glfw.hints.window.xpos = GLFW_ANY_POSITION; + _glfw.hints.window.ypos = GLFW_ANY_POSITION; // The default is 24 bits of color, 24 bits of depth and 8 bits of stencil, // double buffered @@ -368,6 +370,12 @@ GLFWAPI void glfwWindowHint(int hint, int value) case GLFW_VISIBLE: _glfw.hints.window.visible = value ? GLFW_TRUE : GLFW_FALSE; return; + case GLFW_POSITION_X: + _glfw.hints.window.xpos = value; + return; + case GLFW_POSITION_Y: + _glfw.hints.window.ypos = value; + return; case GLFW_COCOA_RETINA_FRAMEBUFFER: _glfw.hints.window.ns.retina = value ? GLFW_TRUE : GLFW_FALSE; return; diff --git a/src/x11_window.c b/src/x11_window.c index b4ce8669..8a689ed1 100644 --- a/src/x11_window.c +++ b/src/x11_window.c @@ -575,6 +575,14 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, height *= _glfw.x11.contentScaleY; } + int xpos = 0, ypos = 0; + + if (wndconfig->xpos != GLFW_ANY_POSITION && wndconfig->ypos != GLFW_ANY_POSITION) + { + xpos = wndconfig->xpos; + ypos = wndconfig->ypos; + } + // Create a colormap based on the visual used by the current context window->x11.colormap = XCreateColormap(_glfw.x11.display, _glfw.x11.root, @@ -595,7 +603,7 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, window->x11.parent = _glfw.x11.root; window->x11.handle = XCreateWindow(_glfw.x11.display, _glfw.x11.root, - 0, 0, // Position + xpos, ypos, width, height, 0, // Border width depth, // Color depth @@ -714,6 +722,15 @@ static GLFWbool createNativeWindow(_GLFWwindow* window, hints->min_height = hints->max_height = height; } + // HACK: Explicitly setting PPosition to any value causes some WMs, notably + // Compiz and Metacity, to honor the position of unmapped windows + if (wndconfig->xpos != GLFW_ANY_POSITION && wndconfig->ypos != GLFW_ANY_POSITION) + { + hints->flags |= PPosition; + hints->x = 0; + hints->y = 0; + } + hints->flags |= PWinGravity; hints->win_gravity = StaticGravity; diff --git a/tests/threads.c b/tests/threads.c index ec13b001..a2caea56 100644 --- a/tests/threads.c +++ b/tests/threads.c @@ -96,10 +96,11 @@ int main(void) if (!glfwInit()) exit(EXIT_FAILURE); - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); - for (i = 0; i < count; i++) { + glfwWindowHint(GLFW_POSITION_X, 200 + 250 * i); + glfwWindowHint(GLFW_POSITION_Y, 200); + threads[i].window = glfwCreateWindow(200, 200, threads[i].title, NULL, NULL); @@ -110,9 +111,6 @@ int main(void) } glfwSetKeyCallback(threads[i].window, key_callback); - - glfwSetWindowPos(threads[i].window, 200 + 250 * i, 200); - glfwShowWindow(threads[i].window); } glfwMakeContextCurrent(threads[0].window); From 55aad3c37b67f17279378db52da0a3ab81bbf26d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Wed, 27 Jul 2022 15:03:16 +0200 Subject: [PATCH 14/15] EGL: Add support for loading glvnd libOpenGL --- README.md | 1 + src/egl_context.c | 1 + 2 files changed, 2 insertions(+) diff --git a/README.md b/README.md index db76a6fa..15ab9914 100644 --- a/README.md +++ b/README.md @@ -394,6 +394,7 @@ information on what to include when reporting a bug. (#442) - [EGL] Added ANGLE backend selection via `EGL_ANGLE_platform_angle` extension (#1380) + [EGL] Added loading of glvnd `libOpenGL.so.0` where available for OpenGL - [EGL] Bugfix: The `GLFW_DOUBLEBUFFER` context attribute was ignored (#1843) - [GLX] Bugfix: Context creation failed if GLX 1.4 was not exported by GLX library diff --git a/src/egl_context.c b/src/egl_context.c index f8850fa2..490770cd 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -737,6 +737,7 @@ GLFWbool _glfwCreateContextEGL(_GLFWwindow* window, #elif defined(__OpenBSD__) || defined(__NetBSD__) "libGL.so", #else + "libOpenGL.so.0", "libGL.so.1", #endif NULL From c18851f52ec9704eb06464058a600845ec1eada1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Wed, 27 Jul 2022 15:05:23 +0200 Subject: [PATCH 15/15] GLX: Add support for loading glvnd libGLX --- README.md | 1 + src/glx_context.c | 4 ++++ src/x11_platform.h | 1 - 3 files changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 15ab9914..73dbb79a 100644 --- a/README.md +++ b/README.md @@ -396,6 +396,7 @@ information on what to include when reporting a bug. (#1380) [EGL] Added loading of glvnd `libOpenGL.so.0` where available for OpenGL - [EGL] Bugfix: The `GLFW_DOUBLEBUFFER` context attribute was ignored (#1843) + - [GLX] Added loading of glvnd `libGLX.so.0` where available - [GLX] Bugfix: Context creation failed if GLX 1.4 was not exported by GLX library diff --git a/src/glx_context.c b/src/glx_context.c index 872612d1..3c38807f 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -226,7 +226,10 @@ static GLFWglproc getProcAddressGLX(const char* procname) else if (_glfw.glx.GetProcAddressARB) return _glfw.glx.GetProcAddressARB((const GLubyte*) procname); else + { + // NOTE: glvnd provides GLX 1.4, so this can only happen with libGL return _glfwPlatformGetModuleSymbol(_glfw.glx.handle, procname); + } } static void destroyContextGLX(_GLFWwindow* window) @@ -262,6 +265,7 @@ GLFWbool _glfwInitGLX(void) #elif defined(__OpenBSD__) || defined(__NetBSD__) "libGL.so", #else + "libGLX.so.0", "libGL.so.1", "libGL.so", #endif diff --git a/src/x11_platform.h b/src/x11_platform.h index ecaa0fa4..cdea3957 100644 --- a/src/x11_platform.h +++ b/src/x11_platform.h @@ -480,7 +480,6 @@ typedef struct _GLFWlibraryGLX int eventBase; int errorBase; - // dlopen handle for libGL.so.1 void* handle; // GLX 1.3 functions