Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Add glfwDragWindow function to latest platform structure. glfw#2152 (#16
Browse files Browse the repository at this point in the history
)

* Added glfwDragWindow function to latest platform structure

Based on work by Felipe da Silva (felselva) glfw#987 ported to v3.4

Add a function glfwDragWindow that starts a drag operation for the
specified window. The implementation is done for X11, Windows, and
requires tests on Wayland. Patch is related with the issue glfw#923.

* Fix X11+Wayland build issues

* Fix contributors and doc.

---------

Co-authored-by: Naveen K <[email protected]>
Co-authored-by: Naveen Karuthedath <[email protected]>
Co-authored-by: Björn DM <[email protected]>
  • Loading branch information
4 people authored Mar 2, 2023
1 parent fbb4c7e commit 55d6d12
Show file tree
Hide file tree
Showing 21 changed files with 92 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Acknowledgements

GLFW exists because people around the world donated their time and lent their
GLFWFF exists because people around the world donated their time and lent their
skills. This list only includes contributions to the main repository and
excludes other invaluable contributions like language bindings and text and
video tutorials.
Expand Down Expand Up @@ -262,6 +262,7 @@ video tutorials.
- Lasse Öörni
- Leonard König
- Daijiro Fukuda
- envyen
- All the unmentioned and anonymous contributors in the GLFW community, for bug
reports, patches, feedback, testing and encouragement

1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ information on what to include when reporting a bug.

## Changelog

- Added `glfwDragWindow` function for starting a drag operation on a window
- Added `GLFW_PLATFORM` init hint for runtime platform selection (#1958)
- Added `GLFW_ANY_PLATFORM`, `GLFW_PLATFORM_WIN32`, `GLFW_PLATFORM_COCOA`,
`GLFW_PLATFORM_WAYLAND`, `GLFW_PLATFORM_X11` and `GLFW_PLATFORM_NULL` symbols to
Expand Down
7 changes: 7 additions & 0 deletions examples/windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@
#include <stdio.h>
#include <stdlib.h>

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
glfwDragWindow(window);
}
}

int main(int argc, char** argv)
{
int xpos, ypos, height;
Expand Down Expand Up @@ -78,6 +84,7 @@ int main(int argc, char** argv)
}

glfwSetInputMode(windows[i], GLFW_STICKY_KEYS, GLFW_TRUE);
glfwSetMouseButtonCallback(windows[i], mouse_button_callback);

glfwMakeContextCurrent(windows[i]);
gladLoadGL(glfwGetProcAddress);
Expand Down
17 changes: 17 additions & 0 deletions include/GLFW/glfw3.h
Original file line number Diff line number Diff line change
Expand Up @@ -3939,6 +3939,23 @@ GLFWAPI void glfwHideWindow(GLFWwindow* window);
*/
GLFWAPI void glfwFocusWindow(GLFWwindow* window);

/*! @brief Starts drag operation to the specified window.
*
* This function starts the drag operation of the specified window.
*
* @param[in] window The window to start the dragging operation.
*
* @errors Possible errors include @ref GLFW_NOT_INITIALIZED and @ref
* GLFW_PLATFORM_ERROR.
*
* @thread_safety This function must only be called from the main thread.
*
* @since Added in glfwff version 3.4.
*
* @ingroup window
*/
GLFWAPI void glfwDragWindow(GLFWwindow* handle);

/*! @brief Requests user attention to the specified window.
*
* This function requests user attention to the specified window. On
Expand Down
1 change: 1 addition & 0 deletions src/cocoa_init.m
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@ GLFWbool _glfwConnectCocoa(int platformID, _GLFWplatform* platform)
_glfwHideWindowCocoa,
_glfwRequestWindowAttentionCocoa,
_glfwFocusWindowCocoa,
_glfwDragWindowCocoa,
_glfwSetWindowMonitorCocoa,
_glfwWindowFocusedCocoa,
_glfwWindowIconifiedCocoa,
Expand Down
1 change: 1 addition & 0 deletions src/cocoa_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ void _glfwShowWindowCocoa(_GLFWwindow* window);
void _glfwHideWindowCocoa(_GLFWwindow* window);
void _glfwRequestWindowAttentionCocoa(_GLFWwindow* window);
void _glfwFocusWindowCocoa(_GLFWwindow* window);
void _glfwDragWindowCocoa(_GLFWwindow* window);
void _glfwSetWindowMonitorCocoa(_GLFWwindow* window, _GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
GLFWbool _glfwWindowFocusedCocoa(_GLFWwindow* window);
GLFWbool _glfwWindowIconifiedCocoa(_GLFWwindow* window);
Expand Down
5 changes: 5 additions & 0 deletions src/cocoa_window.m
Original file line number Diff line number Diff line change
Expand Up @@ -1347,6 +1347,11 @@ void _glfwFocusWindowCocoa(_GLFWwindow* window)
} // autoreleasepool
}

void _glfwDragWindowCocoa(_GLFWwindow* window)
{
// TODO
}

void _glfwSetWindowMonitorCocoa(_GLFWwindow* window,
_GLFWmonitor* monitor,
int xpos, int ypos,
Expand Down
1 change: 1 addition & 0 deletions src/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -768,6 +768,7 @@ struct _GLFWplatform
void (*hideWindow)(_GLFWwindow*);
void (*requestWindowAttention)(_GLFWwindow*);
void (*focusWindow)(_GLFWwindow*);
void (*dragWindow)(_GLFWwindow*);
void (*setWindowMonitor)(_GLFWwindow*,_GLFWmonitor*,int,int,int,int,int);
GLFWbool (*windowFocused)(_GLFWwindow*);
GLFWbool (*windowIconified)(_GLFWwindow*);
Expand Down
1 change: 1 addition & 0 deletions src/null_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ GLFWbool _glfwConnectNull(int platformID, _GLFWplatform* platform)
_glfwHideWindowNull,
_glfwRequestWindowAttentionNull,
_glfwFocusWindowNull,
_glfwDragWindowNull,
_glfwSetWindowMonitorNull,
_glfwWindowFocusedNull,
_glfwWindowIconifiedNull,
Expand Down
1 change: 1 addition & 0 deletions src/null_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ void _glfwRequestWindowAttentionNull(_GLFWwindow* window);
void _glfwRequestWindowAttentionNull(_GLFWwindow* window);
void _glfwHideWindowNull(_GLFWwindow* window);
void _glfwFocusWindowNull(_GLFWwindow* window);
void _glfwDragWindowNull(_GLFWwindow* window);
GLFWbool _glfwWindowFocusedNull(_GLFWwindow* window);
GLFWbool _glfwWindowIconifiedNull(_GLFWwindow* window);
GLFWbool _glfwWindowVisibleNull(_GLFWwindow* window);
Expand Down
5 changes: 5 additions & 0 deletions src/null_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,11 @@ void _glfwFocusWindowNull(_GLFWwindow* window)
_glfwInputWindowFocus(window, GLFW_TRUE);
}

void _glfwDragWindowNull(_GLFWwindow* window)
{
// TODO
}

GLFWbool _glfwWindowFocusedNull(_GLFWwindow* window)
{
return _glfw.null.focusedWindow == window;
Expand Down
1 change: 1 addition & 0 deletions src/win32_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,7 @@ GLFWbool _glfwConnectWin32(int platformID, _GLFWplatform* platform)
_glfwHideWindowWin32,
_glfwRequestWindowAttentionWin32,
_glfwFocusWindowWin32,
_glfwDragWindowWin32,
_glfwSetWindowMonitorWin32,
_glfwWindowFocusedWin32,
_glfwWindowIconifiedWin32,
Expand Down
1 change: 1 addition & 0 deletions src/win32_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,7 @@ void _glfwShowWindowWin32(_GLFWwindow* window);
void _glfwHideWindowWin32(_GLFWwindow* window);
void _glfwRequestWindowAttentionWin32(_GLFWwindow* window);
void _glfwFocusWindowWin32(_GLFWwindow* window);
void _glfwDragWindowWin32(_GLFWwindow* window);
void _glfwSetWindowMonitorWin32(_GLFWwindow* window, _GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
GLFWbool _glfwWindowFocusedWin32(_GLFWwindow* window);
GLFWbool _glfwWindowIconifiedWin32(_GLFWwindow* window);
Expand Down
6 changes: 6 additions & 0 deletions src/win32_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -2163,6 +2163,12 @@ void _glfwFocusWindowWin32(_GLFWwindow* window)
SetFocus(window->win32.handle);
}

void _glfwDragWindowWin32(_GLFWwindow* window)
{
ReleaseCapture();
SendMessage(window->win32.handle, WM_NCLBUTTONDOWN, HTCAPTION, 0);
}

void _glfwSetWindowMonitorWin32(_GLFWwindow* window,
_GLFWmonitor* monitor,
int xpos, int ypos,
Expand Down
10 changes: 10 additions & 0 deletions src/window.c
Original file line number Diff line number Diff line change
Expand Up @@ -855,6 +855,16 @@ GLFWAPI void glfwFocusWindow(GLFWwindow* handle)
_glfw.platform.focusWindow(window);
}

GLFWAPI void glfwDragWindow(GLFWwindow* handle)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
assert(window != NULL);

_GLFW_REQUIRE_INIT();

_glfw.platform.dragWindow(window);
}

GLFWAPI int glfwGetWindowAttrib(GLFWwindow* handle, int attrib)
{
_GLFWwindow* window = (_GLFWwindow*) handle;
Expand Down
1 change: 1 addition & 0 deletions src/wl_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -463,6 +463,7 @@ GLFWbool _glfwConnectWayland(int platformID, _GLFWplatform* platform)
_glfwHideWindowWayland,
_glfwRequestWindowAttentionWayland,
_glfwFocusWindowWayland,
_glfwDragWindowWayland,
_glfwSetWindowMonitorWayland,
_glfwWindowFocusedWayland,
_glfwWindowIconifiedWayland,
Expand Down
1 change: 1 addition & 0 deletions src/wl_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ void _glfwShowWindowWayland(_GLFWwindow* window);
void _glfwHideWindowWayland(_GLFWwindow* window);
void _glfwRequestWindowAttentionWayland(_GLFWwindow* window);
void _glfwFocusWindowWayland(_GLFWwindow* window);
void _glfwDragWindowWayland(_GLFWwindow* window);
void _glfwSetWindowMonitorWayland(_GLFWwindow* window, _GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
GLFWbool _glfwWindowFocusedWayland(_GLFWwindow* window);
GLFWbool _glfwWindowIconifiedWayland(_GLFWwindow* window);
Expand Down
5 changes: 5 additions & 0 deletions src/wl_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,11 @@ void _glfwFocusWindowWayland(_GLFWwindow* window)
"Wayland: The platform does not support setting the input focus");
}

void _glfwDragWindowWayland(_GLFWwindow* window)
{
xdg_toplevel_move(window->wl.xdg.toplevel, _glfw.wl.seat, _glfw.wl.pointerEnterSerial);
}

void _glfwSetWindowMonitorWayland(_GLFWwindow* window,
_GLFWmonitor* monitor,
int xpos, int ypos,
Expand Down
1 change: 1 addition & 0 deletions src/x11_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -1234,6 +1234,7 @@ GLFWbool _glfwConnectX11(int platformID, _GLFWplatform* platform)
_glfwHideWindowX11,
_glfwRequestWindowAttentionX11,
_glfwFocusWindowX11,
_glfwDragWindowX11,
_glfwSetWindowMonitorX11,
_glfwWindowFocusedX11,
_glfwWindowIconifiedX11,
Expand Down
1 change: 1 addition & 0 deletions src/x11_platform.h
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ void _glfwShowWindowX11(_GLFWwindow* window);
void _glfwHideWindowX11(_GLFWwindow* window);
void _glfwRequestWindowAttentionX11(_GLFWwindow* window);
void _glfwFocusWindowX11(_GLFWwindow* window);
void _glfwDragWindowX11(_GLFWwindow* window);
void _glfwSetWindowMonitorX11(_GLFWwindow* window, _GLFWmonitor* monitor, int xpos, int ypos, int width, int height, int refreshRate);
GLFWbool _glfwWindowFocusedX11(_GLFWwindow* window);
GLFWbool _glfwWindowIconifiedX11(_GLFWwindow* window);
Expand Down
23 changes: 23 additions & 0 deletions src/x11_window.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
#define _NET_WM_STATE_REMOVE 0
#define _NET_WM_STATE_ADD 1
#define _NET_WM_STATE_TOGGLE 2
#define _NET_WM_MOVERESIZE_MOVE 8

// Additional mouse button names for XButtonEvent
#define Button6 6
Expand Down Expand Up @@ -2711,6 +2712,28 @@ void _glfwFocusWindowX11(_GLFWwindow* window)
XFlush(_glfw.x11.display);
}

void _glfwDragWindowX11(_GLFWwindow* window)
{
int winXpos, winYpos;
double curXpos, curYpos;
XClientMessageEvent xclient;
memset(&xclient, 0, sizeof(XClientMessageEvent));
XUngrabPointer(_glfw.x11.display, 0);
XFlush(_glfw.x11.display);
_glfwGetCursorPosX11(window, &curXpos, &curYpos);
_glfwGetWindowPosX11(window, &winXpos, &winYpos);
xclient.type = ClientMessage;
xclient.window = window->x11.handle;
xclient.message_type = XInternAtom(_glfw.x11.display, "_NET_WM_MOVERESIZE", False);
xclient.format = 32;
xclient.data.l[0] = winXpos + curXpos;
xclient.data.l[1] = winYpos + curYpos;
xclient.data.l[2] = _NET_WM_MOVERESIZE_MOVE;
xclient.data.l[3] = 0;
xclient.data.l[4] = 0;
XSendEvent(_glfw.x11.display, _glfw.x11.root, False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&xclient);
}

void _glfwSetWindowMonitorX11(_GLFWwindow* window,
_GLFWmonitor* monitor,
int xpos, int ypos,
Expand Down

0 comments on commit 55d6d12

Please sign in to comment.