Skip to content

Commit

Permalink
Implement window_start_drag on Windows and Linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
bruvzg committed Dec 17, 2024
1 parent 4364ed6 commit 293be04
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion doc/classes/DisplayServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1836,7 +1836,7 @@
<param index="0" name="window_id" type="int" default="0" />
<description>
Starts a drag operation on the window with the given [param window_id], using the current mouse position. Call this method when handling a mouse button being pressed to simulate a pressed event on the window's title bar. Using this method allows the window to participate in space switching, tiling, and other system features.
[b]Note:[/b] This method is implemented only on macOS.
[b]Note:[/b] This method is implemented on Linux(X11/Wayland), macOS, and Windows.
</description>
</method>
</methods>
Expand Down
6 changes: 6 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,6 +986,12 @@ DisplayServer::VSyncMode DisplayServerWayland::window_get_vsync_mode(DisplayServ
return DisplayServer::VSYNC_ENABLED;
}

void DisplayServerWayland::window_start_drag(WindowID p_window) {
MutexLock mutex_lock(wayland_thread.mutex);

wayland_thread.window_start_drag(p_window);
}

void DisplayServerWayland::cursor_set_shape(CursorShape p_shape) {
ERR_FAIL_INDEX(p_shape, CURSOR_MAX);

Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/wayland/display_server_wayland.h
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,8 @@ class DisplayServerWayland : public DisplayServer {
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window_id = MAIN_WINDOW_ID) override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_window_id) const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;

virtual void cursor_set_shape(CursorShape p_shape) override;
virtual CursorShape cursor_get_shape() const override;
virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) override;
Expand Down
16 changes: 16 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3338,6 +3338,22 @@ void WaylandThread::beep() const {
}
}

void WaylandThread::window_start_drag(DisplayServer::WindowID p_window_id) {
// TODO: Use window IDs for multiwindow support.
WindowState &ws = main_window;
SeatState *ss = wl_seat_get_seat_state(wl_seat_current);

if (ss && ws.xdg_toplevel) {
xdg_toplevel_move(ws.xdg_toplevel, ss->wl_seat, ss->pointer_data.button_serial);
}

#ifdef LIBDECOR_ENABLED
if (ws.libdecor_frame) {
libdecor_frame_move(ws.libdecor_frame, ss->wl_seat, ss->pointer_data.button_serial);
}
#endif
}

void WaylandThread::window_set_max_size(DisplayServer::WindowID p_window_id, const Size2i &p_size) {
// TODO: Use window IDs for multiwindow support.
WindowState &ws = main_window;
Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/wayland/wayland_thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,8 @@ class WaylandThread {
// Optional - requires xdg_activation_v1
void window_request_attention(DisplayServer::WindowID p_window_id);

void window_start_drag(DisplayServer::WindowID p_window_id);

// Optional - require idle_inhibit_unstable_v1
void window_set_idle_inhibition(DisplayServer::WindowID p_window_id, bool p_enable);
bool window_get_idle_inhibition(DisplayServer::WindowID p_window_id) const;
Expand Down
36 changes: 36 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
#define _NET_WM_STATE_REMOVE 0L // remove/unset property
#define _NET_WM_STATE_ADD 1L // add/set property

#define _NET_WM_MOVERESIZE_MOVE 8L

// 2.2 is the first release with multitouch
#define XINPUT_CLIENT_VERSION_MAJOR 2
#define XINPUT_CLIENT_VERSION_MINOR 2
Expand Down Expand Up @@ -5422,6 +5424,40 @@ DisplayServer::VSyncMode DisplayServerX11::window_get_vsync_mode(WindowID p_wind
return DisplayServer::VSYNC_ENABLED;
}

void DisplayServerX11::window_start_drag(WindowID p_window) {
_THREAD_SAFE_METHOD_

ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];

XClientMessageEvent m;
memset(&m, 0, sizeof(m));

XUngrabPointer(x11_display, CurrentTime);

Window root_return, child_return;
int root_x, root_y, win_x, win_y;
unsigned int mask_return;

Bool xquerypointer_result = XQueryPointer(x11_display, wd.x11_window, &root_return, &child_return, &root_x, &root_y, &win_x, &win_y, &mask_return);

m.type = ClientMessage;
m.window = wd.x11_window;
m.message_type = XInternAtom(x11_display, "_NET_WM_MOVERESIZE", True);
m.format = 32;
if (xquerypointer_result) {
m.data.l[0] = root_x;
m.data.l[1] = root_y;
m.data.l[3] = Button1;
}
m.data.l[2] = _NET_WM_MOVERESIZE_MOVE;
m.data.l[4] = 1; // Source - normal application.

XSendEvent(x11_display, DefaultRootWindow(x11_display), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent *)&m);

XSync(x11_display, 0);
}

Vector<String> DisplayServerX11::get_rendering_drivers_func() {
Vector<String> drivers;

Expand Down
2 changes: 2 additions & 0 deletions platform/linuxbsd/x11/display_server_x11.h
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,8 @@ class DisplayServerX11 : public DisplayServer {
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;

virtual void cursor_set_shape(CursorShape p_shape) override;
virtual CursorShape cursor_get_shape() const override;
virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape, const Vector2 &p_hotspot) override;
Expand Down
15 changes: 15 additions & 0 deletions platform/windows/display_server_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3803,6 +3803,21 @@ DisplayServer::VSyncMode DisplayServerWindows::window_get_vsync_mode(WindowID p_
return DisplayServer::VSYNC_ENABLED;
}

void DisplayServerWindows::window_start_drag(WindowID p_window) {
_THREAD_SAFE_METHOD_

ERR_FAIL_COND(!windows.has(p_window));
WindowData &wd = windows[p_window];

ReleaseCapture();

POINT coords;
GetCursorPos(&coords);
ScreenToClient(wd.hWnd, &coords);

SendMessage(wd.hWnd, WM_SYSCOMMAND, SC_MOVE | HTCAPTION, MAKELPARAM(coords.x, coords.y));
}

void DisplayServerWindows::set_context(Context p_context) {
}

Expand Down
2 changes: 2 additions & 0 deletions platform/windows/display_server_windows.h
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,8 @@ class DisplayServerWindows : public DisplayServer {
virtual void window_set_vsync_mode(DisplayServer::VSyncMode p_vsync_mode, WindowID p_window = MAIN_WINDOW_ID) override;
virtual DisplayServer::VSyncMode window_get_vsync_mode(WindowID p_vsync_mode) const override;

virtual void window_start_drag(WindowID p_window = MAIN_WINDOW_ID) override;

virtual void cursor_set_shape(CursorShape p_shape) override;
virtual CursorShape cursor_get_shape() const override;
virtual void cursor_set_custom_image(const Ref<Resource> &p_cursor, CursorShape p_shape = CURSOR_ARROW, const Vector2 &p_hotspot = Vector2()) override;
Expand Down

0 comments on commit 293be04

Please sign in to comment.