From 38011e1e07ba686ba40a6fba323b5f7b3c3be5cf Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Fri, 21 Nov 2025 16:29:27 +0000 Subject: [PATCH] wait-events win32 implementation --- src/init.c | 2 ++ src/input.c | 20 ++++++++++++++++++++ src/internal.h | 2 ++ src/monitor.c | 4 ++++ src/win32_init.c | 3 +++ src/win32_window.c | 15 +++++++++++---- src/window.c | 22 ++++++++++++++++++++++ 7 files changed, 64 insertions(+), 4 deletions(-) diff --git a/src/init.c b/src/init.c index dbd5a900c..d7b6c82c4 100644 --- a/src/init.c +++ b/src/init.c @@ -376,6 +376,8 @@ void _glfwInputError(int code, const char* format, ...) if (_glfwErrorCallback) _glfwErrorCallback(code, description); + + _glfw.newEventsRcvd = GLFW_TRUE; } diff --git a/src/input.c b/src/input.c index c619eefce..d72480b8b 100644 --- a/src/input.c +++ b/src/input.c @@ -301,6 +301,8 @@ void _glfwInputKey(_GLFWwindow* window, int key, int scancode, int action, int m if (window->callbacks.key) window->callbacks.key((GLFWwindow*) window, key, scancode, action, mods); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a Unicode codepoint input event @@ -326,6 +328,8 @@ void _glfwInputChar(_GLFWwindow* window, uint32_t codepoint, int mods, GLFWbool if (window->callbacks.character) window->callbacks.character((GLFWwindow*) window, codepoint); } + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a scroll event @@ -340,6 +344,8 @@ void _glfwInputScroll(_GLFWwindow* window, double xoffset, double yoffset) if (window->callbacks.scroll) window->callbacks.scroll((GLFWwindow*) window, xoffset, yoffset); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a mouse button click event @@ -367,6 +373,8 @@ void _glfwInputMouseClick(_GLFWwindow* window, int button, int action, int mods) if (window->callbacks.mouseButton) window->callbacks.mouseButton((GLFWwindow*) window, button, action, mods); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a cursor motion event @@ -388,6 +396,8 @@ void _glfwInputCursorPos(_GLFWwindow* window, double xpos, double ypos) if (window->callbacks.cursorPos) window->callbacks.cursorPos((GLFWwindow*) window, xpos, ypos); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a cursor enter/leave event @@ -399,6 +409,8 @@ void _glfwInputCursorEnter(_GLFWwindow* window, GLFWbool entered) if (window->callbacks.cursorEnter) window->callbacks.cursorEnter((GLFWwindow*) window, entered); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of files or directories dropped on a window @@ -411,6 +423,8 @@ void _glfwInputDrop(_GLFWwindow* window, int count, const char** paths) if (window->callbacks.drop) window->callbacks.drop((GLFWwindow*) window, count, paths); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of a joystick connection or disconnection @@ -427,6 +441,8 @@ void _glfwInputJoystick(_GLFWjoystick* js, int event) if (_glfw.callbacks.joystick) _glfw.callbacks.joystick((int) (js - _glfw.joysticks), event); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of the new value of a joystick axis @@ -450,6 +466,8 @@ void _glfwInputJoystickButton(_GLFWjoystick* js, int button, char value) assert(value == GLFW_PRESS || value == GLFW_RELEASE); js->buttons[button] = value; + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code of the new value of a joystick hat @@ -476,6 +494,8 @@ void _glfwInputJoystickHat(_GLFWjoystick* js, int hat, char value) js->buttons[base + 3] = (value & 0x08) ? GLFW_PRESS : GLFW_RELEASE; js->hats[hat] = value; + + _glfw.newEventsRcvd = GLFW_TRUE; } diff --git a/src/internal.h b/src/internal.h index de703740f..e90257935 100644 --- a/src/internal.h +++ b/src/internal.h @@ -793,6 +793,8 @@ struct _GLFWlibrary _GLFWtls contextSlot; _GLFWmutex errorLock; + GLFWbool newEventsRcvd; + struct { uint64_t offset; // This is defined in platform.h diff --git a/src/monitor.c b/src/monitor.c index cc95efb64..395410fb1 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -150,6 +150,8 @@ void _glfwInputMonitor(_GLFWmonitor* monitor, int action, int placement) if (action == GLFW_DISCONNECTED) _glfwFreeMonitor(monitor); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a full screen window has acquired or released @@ -159,6 +161,8 @@ void _glfwInputMonitorWindow(_GLFWmonitor* monitor, _GLFWwindow* window) { assert(monitor != NULL); monitor->window = window; + + _glfw.newEventsRcvd = GLFW_TRUE; } diff --git a/src/win32_init.c b/src/win32_init.c index 6b6e9d08e..9c44f8b74 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -357,6 +357,9 @@ static LRESULT CALLBACK helperWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LP break; } + case WM_NULL: + _glfw.newEventsRcvd = GLFW_TRUE; + return 0; } return DefWindowProcW(hWnd, uMsg, wParam, lParam); diff --git a/src/win32_window.c b/src/win32_window.c index 6427a673e..37d8830e5 100644 --- a/src/win32_window.c +++ b/src/win32_window.c @@ -2171,16 +2171,23 @@ void _glfwPollEventsWin32(void) void _glfwWaitEventsWin32(void) { - WaitMessage(); + while (_glfw.newEventsRcvd == GLFW_FALSE) + { + WaitMessage(); - _glfwPollEventsWin32(); + _glfwPollEventsWin32(); + } } void _glfwWaitEventsTimeoutWin32(double timeout) { - MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD) (timeout * 1e3), QS_ALLINPUT); + DWORD ret; + do + { + ret = MsgWaitForMultipleObjects(0, NULL, FALSE, (DWORD) (timeout * 1e3), QS_ALLINPUT); - _glfwPollEventsWin32(); + _glfwPollEventsWin32(); + } while (_glfw.newEventsRcvd == GLFW_FALSE || ret == WAIT_TIMEOUT); } void _glfwPostEmptyEventWin32(void) diff --git a/src/window.c b/src/window.c index 3a3b66dfe..cef6699a8 100644 --- a/src/window.c +++ b/src/window.c @@ -67,6 +67,8 @@ void _glfwInputWindowFocus(_GLFWwindow* window, GLFWbool focused) _glfwInputMouseClick(window, button, GLFW_RELEASE, 0); } } + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window has moved @@ -78,6 +80,8 @@ void _glfwInputWindowPos(_GLFWwindow* window, int x, int y) if (window->callbacks.pos) window->callbacks.pos((GLFWwindow*) window, x, y); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window has been resized @@ -91,6 +95,8 @@ void _glfwInputWindowSize(_GLFWwindow* window, int width, int height) if (window->callbacks.size) window->callbacks.size((GLFWwindow*) window, width, height); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window has been iconified or restored @@ -102,6 +108,8 @@ void _glfwInputWindowIconify(_GLFWwindow* window, GLFWbool iconified) if (window->callbacks.iconify) window->callbacks.iconify((GLFWwindow*) window, iconified); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window has been maximized or restored @@ -113,6 +121,8 @@ void _glfwInputWindowMaximize(_GLFWwindow* window, GLFWbool maximized) if (window->callbacks.maximize) window->callbacks.maximize((GLFWwindow*) window, maximized); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window framebuffer has been resized @@ -126,6 +136,8 @@ void _glfwInputFramebufferSize(_GLFWwindow* window, int width, int height) if (window->callbacks.fbsize) window->callbacks.fbsize((GLFWwindow*) window, width, height); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window content scale has changed @@ -141,6 +153,8 @@ void _glfwInputWindowContentScale(_GLFWwindow* window, float xscale, float yscal if (window->callbacks.scale) window->callbacks.scale((GLFWwindow*) window, xscale, yscale); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that the window contents needs updating @@ -151,6 +165,8 @@ void _glfwInputWindowDamage(_GLFWwindow* window) if (window->callbacks.refresh) window->callbacks.refresh((GLFWwindow*) window); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that the user wishes to close a window @@ -163,6 +179,8 @@ void _glfwInputWindowCloseRequest(_GLFWwindow* window) if (window->callbacks.close) window->callbacks.close((GLFWwindow*) window); + + _glfw.newEventsRcvd = GLFW_TRUE; } // Notifies shared code that a window has changed its desired monitor @@ -171,6 +189,8 @@ void _glfwInputWindowMonitor(_GLFWwindow* window, _GLFWmonitor* monitor) { assert(window != NULL); window->monitor = monitor; + + _glfw.newEventsRcvd = GLFW_TRUE; } ////////////////////////////////////////////////////////////////////////// @@ -1172,6 +1192,7 @@ GLFWAPI void glfwPollEvents(void) GLFWAPI void glfwWaitEvents(void) { _GLFW_REQUIRE_INIT(); + _glfw.newEventsRcvd = GLFW_FALSE; _glfw.platform.waitEvents(); } @@ -1188,6 +1209,7 @@ GLFWAPI void glfwWaitEventsTimeout(double timeout) return; } + _glfw.platform.waitEvents(); _glfw.platform.waitEventsTimeout(timeout); }