aboutsummaryrefslogtreecommitdiff
path: root/src/WindowUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/WindowUtils.cpp')
-rw-r--r--src/WindowUtils.cpp472
1 files changed, 349 insertions, 123 deletions
diff --git a/src/WindowUtils.cpp b/src/WindowUtils.cpp
index 7631e4d..c6b278b 100644
--- a/src/WindowUtils.cpp
+++ b/src/WindowUtils.cpp
@@ -1,10 +1,21 @@
#include "../include/WindowUtils.hpp"
+#include "../include/Utils.hpp"
-#include <X11/Xlib.h>
#include <X11/Xatom.h>
#include <X11/Xutil.h>
+#include <X11/extensions/XInput2.h>
+#include <X11/extensions/Xfixes.h>
+#include <X11/extensions/shapeconst.h>
+#include <X11/extensions/Xrandr.h>
+
+#include <mglpp/system/Utf8.hpp>
+
+extern "C" {
+#include <mgl/window/window.h>
+}
#include <stdbool.h>
+#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include <poll.h>
@@ -52,7 +63,7 @@ namespace gsr {
return window_has_atom(dpy, window, net_wm_state_atom) || window_has_atom(dpy, window, wm_state_atom);
}
- static Window window_get_target_window_child(Display *display, Window window) {
+ Window window_get_target_window_child(Display *display, Window window) {
if(window == None)
return None;
@@ -89,15 +100,25 @@ namespace gsr {
return found_window;
}
- static Window get_window_at_cursor_position(Display *dpy) {
+ mgl::vec2i get_cursor_position(Display *dpy, Window *window) {
Window root_window = None;
- Window window = None;
+ *window = None;
int dummy_i;
unsigned int dummy_u;
- XQueryPointer(dpy, DefaultRootWindow(dpy), &root_window, &window, &dummy_i, &dummy_i, &dummy_i, &dummy_i, &dummy_u);
- if(window)
- window = window_get_target_window_child(dpy, window);
- return window;
+ mgl::vec2i root_pos;
+ XQueryPointer(dpy, DefaultRootWindow(dpy), &root_window, window, &root_pos.x, &root_pos.y, &dummy_i, &dummy_i, &dummy_u);
+
+ const Window direct_window = *window;
+ *window = window_get_target_window_child(dpy, *window);
+ // HACK: Count some other x11 windows as having an x11 window focused. Some games seem to create an Input window and that gets focused.
+ if(!*window) {
+ XWindowAttributes attr;
+ memset(&attr, 0, sizeof(attr));
+ XGetWindowAttributes(dpy, direct_window, &attr);
+ if(attr.c_class == InputOnly && !get_window_title(dpy, direct_window))
+ *window = direct_window;
+ }
+ return root_pos;
}
Window get_focused_window(Display *dpy, WindowCaptureType cap_type) {
@@ -127,14 +148,34 @@ namespace gsr {
return focused_window;
}
- focused_window = get_window_at_cursor_position(dpy);
+ get_cursor_position(dpy, &focused_window);
if(focused_window && focused_window != DefaultRootWindow(dpy))
return focused_window;
return None;
}
- static char* get_window_title(Display *dpy, Window window) {
+ static std::string utf8_sanitize(const uint8_t *str, int size) {
+ const uint32_t zero_width_space_codepoint = 0x200b; // Some games such as the finals has zero-width space characters
+ std::string result;
+ for(int i = 0; i < size;) {
+ // Some games such as the finals has utf8-bom between each character, wtf?
+ if(i + 3 <= size && memcmp(str + i, "\xEF\xBB\xBF", 3) == 0) {
+ i += 3;
+ continue;
+ }
+
+ uint32_t codepoint = 0;
+ size_t codepoint_length = 1;
+ if(mgl::utf8_decode(str + i, size - i, &codepoint, &codepoint_length) && codepoint != zero_width_space_codepoint)
+ result.append((const char*)str + i, codepoint_length);
+ i += codepoint_length;
+ }
+ return result;
+ }
+
+ std::optional<std::string> get_window_title(Display *dpy, Window window) {
+ std::optional<std::string> result;
const Atom net_wm_name_atom = XInternAtom(dpy, "_NET_WM_NAME", False);
const Atom wm_name_atom = XInternAtom(dpy, "WM_NAME", False);
const Atom utf8_string_atom = XInternAtom(dpy, "UTF8_STRING", False);
@@ -146,8 +187,13 @@ namespace gsr {
unsigned char *data = NULL;
XGetWindowProperty(dpy, window, net_wm_name_atom, 0, 1024, False, utf8_string_atom, &type, &format, &num_items, &bytes_left, &data);
- if(type == utf8_string_atom && format == 8 && data)
- return (char*)data;
+ if(type == utf8_string_atom && format == 8 && data) {
+ result = utf8_sanitize(data, num_items);
+ goto done;
+ }
+
+ if(data)
+ XFree(data);
type = None;
format = 0;
@@ -156,37 +202,15 @@ namespace gsr {
data = NULL;
XGetWindowProperty(dpy, window, wm_name_atom, 0, 1024, False, 0, &type, &format, &num_items, &bytes_left, &data);
- if((type == XA_STRING || type == utf8_string_atom) && data)
- return (char*)data;
-
- return NULL;
- }
-
- static const char* strip(const char *str, int *len) {
- int str_len = strlen(str);
- for(int i = 0; i < str_len; ++i) {
- if(str[i] != ' ') {
- str += i;
- str_len -= i;
- break;
- }
+ if((type == XA_STRING || type == utf8_string_atom) && data) {
+ result = utf8_sanitize(data, num_items);
+ goto done;
}
- for(int i = str_len - 1; i >= 0; --i) {
- if(str[i] != ' ') {
- str_len = i + 1;
- break;
- }
- }
-
- *len = str_len;
- return str;
- }
-
- static std::string strip_strip(const char *str) {
- int len = 0;
- str = strip(str, &len);
- return std::string(str, len);
+ done:
+ if(data)
+ XFree(data);
+ return result;
}
std::string get_focused_window_name(Display *dpy, WindowCaptureType window_capture_type) {
@@ -196,50 +220,82 @@ namespace gsr {
return result;
// Window title is not always ideal (for example for a browser), but for games its pretty much required
- char *window_title = get_window_title(dpy, focused_window);
+ const std::optional<std::string> window_title = get_window_title(dpy, focused_window);
if(window_title) {
- result = strip_strip(window_title);
- XFree(window_title);
+ result = strip(window_title.value());
return result;
}
XClassHint class_hint = {nullptr, nullptr};
XGetClassHint(dpy, focused_window, &class_hint);
- if(class_hint.res_class) {
- result = strip_strip(class_hint.res_class);
- return result;
- }
+ if(class_hint.res_class)
+ result = strip(class_hint.res_class);
+
+ if(class_hint.res_name)
+ XFree(class_hint.res_name);
+
+ if(class_hint.res_class)
+ XFree(class_hint.res_class);
return result;
}
- mgl::vec2i get_cursor_position(Display *dpy, Window *window) {
- Window root_window = None;
- *window = None;
- int dummy_i;
- unsigned int dummy_u;
- mgl::vec2i root_pos;
- XQueryPointer(dpy, DefaultRootWindow(dpy), &root_window, window, &root_pos.x, &root_pos.y, &dummy_i, &dummy_i, &dummy_u);
+ std::string get_window_name_at_position(Display *dpy, mgl::vec2i position, Window ignore_window) {
+ std::string result;
+
+ Window root;
+ Window parent;
+ Window *children = nullptr;
+ unsigned int num_children = 0;
+ if(!XQueryTree(dpy, DefaultRootWindow(dpy), &root, &parent, &children, &num_children) || !children)
+ return result;
+
+ for(int i = (int)num_children - 1; i >= 0; --i) {
+ if(children[i] == ignore_window)
+ continue;
- // This dumb shit is done to satisfy gnome wayland. Only set |window| if a valid x11 window is focused
- if(window) {
XWindowAttributes attr;
- if(XGetWindowAttributes(dpy, *window, &attr) && attr.override_redirect)
- *window = None;
+ memset(&attr, 0, sizeof(attr));
+ XGetWindowAttributes(dpy, children[i], &attr);
+ if(attr.override_redirect || attr.c_class != InputOutput || attr.map_state != IsViewable)
+ continue;
- int revert_to = 0;
- Window input_focus_window = None;
- if(XGetInputFocus(dpy, &input_focus_window, &revert_to)) {
- if(input_focus_window) {
- if(XGetWindowAttributes(dpy, input_focus_window, &attr) && attr.override_redirect)
- *window = None;
- } else {
- *window = None;
- }
+ if(position.x >= attr.x && position.x <= attr.x + attr.width && position.y >= attr.y && position.y <= attr.y + attr.height) {
+ const Window real_window = window_get_target_window_child(dpy, children[i]);
+ if(!real_window || real_window == ignore_window)
+ continue;
+
+ const std::optional<std::string> window_title = get_window_title(dpy, real_window);
+ if(window_title)
+ result = strip(window_title.value());
+
+ break;
}
}
- return root_pos;
+ XFree(children);
+ return result;
+ }
+
+ std::string get_window_name_at_cursor_position(Display *dpy, Window ignore_window) {
+ Window cursor_window;
+ const mgl::vec2i cursor_position = get_cursor_position(dpy, &cursor_window);
+ return get_window_name_at_position(dpy, cursor_position, ignore_window);
+ }
+
+ void set_window_size_not_resizable(Display *dpy, Window window, int width, int height) {
+ XSizeHints *size_hints = XAllocSizeHints();
+ if(size_hints) {
+ size_hints->width = width;
+ size_hints->height = height;
+ size_hints->min_width = width;
+ size_hints->min_height = height;
+ size_hints->max_width = width;
+ size_hints->max_height = height;
+ size_hints->flags = PSize | PMinSize | PMaxSize;
+ XSetWMNormalHints(dpy, window, size_hints);
+ XFree(size_hints);
+ }
}
typedef struct {
@@ -289,17 +345,7 @@ namespace gsr {
XChangeProperty(display, window, net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1L);
window_set_decorations_visible(display, window, false);
-
- XSizeHints *size_hints = XAllocSizeHints();
- size_hints->width = size;
- size_hints->height = size;
- size_hints->min_width = size;
- size_hints->min_height = size;
- size_hints->max_width = size;
- size_hints->max_height = size;
- size_hints->flags = PSize | PMinSize | PMaxSize;
- XSetWMNormalHints(display, window, size_hints);
- XFree(size_hints);
+ set_window_size_not_resizable(display, window, size, size);
XMapWindow(display, window);
XFlush(display);
@@ -312,7 +358,7 @@ namespace gsr {
poll_fd.fd = x_fd;
poll_fd.events = POLLIN;
poll_fd.revents = 0;
- const int fds_ready = poll(&poll_fd, 1, 1000);
+ const int fds_ready = poll(&poll_fd, 1, 200);
if(fds_ready == 0) {
fprintf(stderr, "Error: timed out waiting for ConfigureNotify after XCreateWindow\n");
break;
@@ -320,15 +366,18 @@ namespace gsr {
continue;
}
- XNextEvent(display, &xev);
- if(xev.type == ConfigureNotify && xev.xconfigure.window == window) {
- got_data = xev.xconfigure.x > 0 && xev.xconfigure.y > 0;
- position.x = xev.xconfigure.x + xev.xconfigure.width / 2;
- position.y = xev.xconfigure.y + xev.xconfigure.height / 2;
- break;
+ while(XPending(display)) {
+ XNextEvent(display, &xev);
+ if(xev.type == ConfigureNotify && xev.xconfigure.window == window) {
+ got_data = xev.xconfigure.x > 0 && xev.xconfigure.y > 0;
+ position.x = xev.xconfigure.x + xev.xconfigure.width / 2;
+ position.y = xev.xconfigure.y + xev.xconfigure.height / 2;
+ goto done;
+ }
}
}
+ done:
XDestroyWindow(display, window);
XFlush(display);
@@ -350,17 +399,7 @@ namespace gsr {
XChangeProperty(display, window, net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1L);
window_set_decorations_visible(display, window, false);
-
- XSizeHints *size_hints = XAllocSizeHints();
- size_hints->width = size;
- size_hints->height = size;
- size_hints->min_width = size;
- size_hints->min_height = size;
- size_hints->max_width = size;
- size_hints->max_height = size;
- size_hints->flags = PSize | PMinSize | PMaxSize;
- XSetWMNormalHints(display, window, size_hints);
- XFree(size_hints);
+ set_window_size_not_resizable(display, window, size, size);
XMapWindow(display, window);
XFlush(display);
@@ -373,7 +412,7 @@ namespace gsr {
poll_fd.fd = x_fd;
poll_fd.events = POLLIN;
poll_fd.revents = 0;
- const int fds_ready = poll(&poll_fd, 1, 1000);
+ const int fds_ready = poll(&poll_fd, 1, 200);
if(fds_ready == 0) {
fprintf(stderr, "Error: timed out waiting for MapNotify/ConfigureNotify after XCreateWindow\n");
break;
@@ -381,27 +420,30 @@ namespace gsr {
continue;
}
- XNextEvent(display, &xev);
- if(xev.type == MapNotify && xev.xmap.window == window) {
- int x = 0;
- int y = 0;
- Window w = None;
- XTranslateCoordinates(display, window, DefaultRootWindow(display), 0, 0, &x, &y, &w);
-
- got_data = x > 0 && y > 0;
- position.x = x + size / 2;
- position.y = y + size / 2;
- if(got_data)
- break;
- } else if(xev.type == ConfigureNotify && xev.xconfigure.window == window) {
- got_data = xev.xconfigure.x > 0 && xev.xconfigure.y > 0;
- position.x = xev.xconfigure.x + xev.xconfigure.width / 2;
- position.y = xev.xconfigure.y + xev.xconfigure.height / 2;
- if(got_data)
- break;
+ while(XPending(display)) {
+ XNextEvent(display, &xev);
+ if(xev.type == MapNotify && xev.xmap.window == window) {
+ int x = 0;
+ int y = 0;
+ Window w = None;
+ XTranslateCoordinates(display, window, DefaultRootWindow(display), 0, 0, &x, &y, &w);
+
+ got_data = x > 0 && y > 0;
+ position.x = x + size / 2;
+ position.y = y + size / 2;
+ if(got_data)
+ goto done;
+ } else if(xev.type == ConfigureNotify && xev.xconfigure.window == window) {
+ got_data = xev.xconfigure.x > 0 && xev.xconfigure.y > 0;
+ position.x = xev.xconfigure.x + xev.xconfigure.width / 2;
+ position.y = xev.xconfigure.y + xev.xconfigure.height / 2;
+ if(got_data)
+ goto done;
+ }
}
}
+ done:
XDestroyWindow(display, window);
XFlush(display);
@@ -442,11 +484,9 @@ namespace gsr {
if(!window)
return wm_name;
- char *window_title = get_window_title(display, window);
- if(window_title) {
- wm_name = strip_strip(window_title);
- XFree(window_title);
- }
+ const std::optional<std::string> window_title = get_window_title(display, window);
+ if(window_title)
+ wm_name = strip(window_title.value());
return wm_name;
}
@@ -454,7 +494,193 @@ namespace gsr {
bool is_compositor_running(Display *dpy, int screen) {
char prop_name[20];
snprintf(prop_name, sizeof(prop_name), "_NET_WM_CM_S%d", screen);
- Atom prop_atom = XInternAtom(dpy, prop_name, False);
+ const Atom prop_atom = XInternAtom(dpy, prop_name, False);
return XGetSelectionOwner(dpy, prop_atom) != None;
}
+
+ std::vector<Monitor> get_monitors(Display *dpy) {
+ std::vector<Monitor> monitors;
+ int nmonitors = 0;
+ XRRMonitorInfo *monitor_info = XRRGetMonitors(dpy, DefaultRootWindow(dpy), True, &nmonitors);
+ if(monitor_info) {
+ for(int i = 0; i < nmonitors; ++i) {
+ char *monitor_name = XGetAtomName(dpy, monitor_info[i].name);
+ if(!monitor_name)
+ continue;
+
+ monitors.push_back({mgl::vec2i(monitor_info[i].x, monitor_info[i].y), mgl::vec2i(monitor_info[i].width, monitor_info[i].height), std::string(monitor_name)});
+ XFree(monitor_name);
+ }
+ XRRFreeMonitors(monitor_info);
+ }
+ return monitors;
+ }
+
+ static bool device_is_mouse(const XIDeviceInfo *dev) {
+ for(int i = 0; i < dev->num_classes; ++i) {
+ if(dev->classes[i]->type == XIMasterPointer || dev->classes[i]->type == XISlavePointer)
+ return true;
+ }
+ return false;
+ }
+
+ static void xi_grab_all_mouse_devices(Display *dpy, bool grab) {
+ if(!dpy)
+ return;
+
+ int num_devices = 0;
+ XIDeviceInfo *info = XIQueryDevice(dpy, XIAllDevices, &num_devices);
+ if(!info)
+ return;
+
+ unsigned char mask[XIMaskLen(XI_LASTEVENT)];
+ memset(mask, 0, sizeof(mask));
+ XISetMask(mask, XI_Motion);
+ //XISetMask(mask, XI_RawMotion);
+ XISetMask(mask, XI_ButtonPress);
+ XISetMask(mask, XI_ButtonRelease);
+ XISetMask(mask, XI_KeyPress);
+ XISetMask(mask, XI_KeyRelease);
+
+ for (int i = 0; i < num_devices; ++i) {
+ const XIDeviceInfo *dev = &info[i];
+ if(!device_is_mouse(dev))
+ continue;
+
+ XIEventMask xi_masks;
+ xi_masks.deviceid = dev->deviceid;
+ xi_masks.mask_len = sizeof(mask);
+ xi_masks.mask = mask;
+ if(grab)
+ XIGrabDevice(dpy, dev->deviceid, DefaultRootWindow(dpy), CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, XIOwnerEvents, &xi_masks);
+ else
+ XIUngrabDevice(dpy, dev->deviceid, CurrentTime);
+ }
+
+ XFlush(dpy);
+ XIFreeDeviceInfo(info);
+ }
+
+ void xi_grab_all_mouse_devices(Display *dpy) {
+ xi_grab_all_mouse_devices(dpy, true);
+ }
+
+ void xi_ungrab_all_mouse_devices(Display *dpy) {
+ xi_grab_all_mouse_devices(dpy, false);
+ }
+
+ void xi_warp_all_mouse_devices(Display *dpy, mgl::vec2i position) {
+ if(!dpy)
+ return;
+
+ int num_devices = 0;
+ XIDeviceInfo *info = XIQueryDevice(dpy, XIAllDevices, &num_devices);
+ if(!info)
+ return;
+
+ for (int i = 0; i < num_devices; ++i) {
+ const XIDeviceInfo *dev = &info[i];
+ if(!device_is_mouse(dev))
+ continue;
+
+ XIWarpPointer(dpy, dev->deviceid, DefaultRootWindow(dpy), DefaultRootWindow(dpy), 0, 0, 0, 0, position.x, position.y);
+ }
+
+ XFlush(dpy);
+ XIFreeDeviceInfo(info);
+ }
+
+ void window_set_fullscreen(Display *dpy, Window window, bool fullscreen) {
+ const Atom net_wm_state_atom = XInternAtom(dpy, "_NET_WM_STATE", False);
+ const Atom net_wm_state_fullscreen_atom = XInternAtom(dpy, "_NET_WM_STATE_FULLSCREEN", False);
+
+ XEvent xev;
+ xev.type = ClientMessage;
+ xev.xclient.window = window;
+ xev.xclient.message_type = net_wm_state_atom;
+ xev.xclient.format = 32;
+ xev.xclient.data.l[0] = fullscreen ? 1 : 0;
+ xev.xclient.data.l[1] = net_wm_state_fullscreen_atom;
+ xev.xclient.data.l[2] = 0;
+ xev.xclient.data.l[3] = 1;
+ xev.xclient.data.l[4] = 0;
+
+ if(!XSendEvent(dpy, DefaultRootWindow(dpy), 0, SubstructureRedirectMask | SubstructureNotifyMask, &xev)) {
+ fprintf(stderr, "mgl warning: failed to change window fullscreen state\n");
+ return;
+ }
+
+ XFlush(dpy);
+ }
+
+ bool window_is_fullscreen(Display *display, Window window) {
+ const Atom wm_state_atom = XInternAtom(display, "_NET_WM_STATE", False);
+ const Atom wm_state_fullscreen_atom = XInternAtom(display, "_NET_WM_STATE_FULLSCREEN", False);
+
+ Atom type = None;
+ int format = 0;
+ unsigned long num_items = 0;
+ unsigned long bytes_after = 0;
+ unsigned char *properties = nullptr;
+ if(XGetWindowProperty(display, window, wm_state_atom, 0, 1024, False, XA_ATOM, &type, &format, &num_items, &bytes_after, &properties) < Success) {
+ fprintf(stderr, "Failed to get window wm state property\n");
+ return false;
+ }
+
+ if(!properties)
+ return false;
+
+ bool is_fullscreen = false;
+ Atom *atoms = (Atom*)properties;
+ for(unsigned long i = 0; i < num_items; ++i) {
+ if(atoms[i] == wm_state_fullscreen_atom) {
+ is_fullscreen = true;
+ break;
+ }
+ }
+
+ XFree(properties);
+ return is_fullscreen;
+ }
+
+ #define _NET_WM_STATE_REMOVE 0
+ #define _NET_WM_STATE_ADD 1
+ #define _NET_WM_STATE_TOGGLE 2
+
+ bool set_window_wm_state(Display *dpy, Window window, Atom atom) {
+ const Atom net_wm_state_atom = XInternAtom(dpy, "_NET_WM_STATE", False);
+
+ XClientMessageEvent xclient;
+ memset(&xclient, 0, sizeof(xclient));
+
+ xclient.type = ClientMessage;
+ xclient.window = window;
+ xclient.message_type = net_wm_state_atom;
+ xclient.format = 32;
+ xclient.data.l[0] = _NET_WM_STATE_ADD;
+ xclient.data.l[1] = atom;
+ xclient.data.l[2] = 0;
+ xclient.data.l[3] = 0;
+ xclient.data.l[4] = 0;
+
+ XSendEvent(dpy, DefaultRootWindow(dpy), False, SubstructureRedirectMask | SubstructureNotifyMask, (XEvent*)&xclient);
+ XFlush(dpy);
+ return true;
+ }
+
+ void make_window_click_through(Display *display, Window window) {
+ XRectangle rect;
+ memset(&rect, 0, sizeof(rect));
+ XserverRegion region = XFixesCreateRegion(display, &rect, 1);
+ XFixesSetWindowShapeRegion(display, window, ShapeInput, 0, 0, region);
+ XFixesDestroyRegion(display, region);
+ }
+
+ bool make_window_sticky(Display *dpy, Window window) {
+ return set_window_wm_state(dpy, window, XInternAtom(dpy, "_NET_WM_STATE_STICKY", False));
+ }
+
+ bool hide_window_from_taskbar(Display *dpy, Window window) {
+ return set_window_wm_state(dpy, window, XInternAtom(dpy, "_NET_WM_STATE_SKIP_TASKBAR", False));
+ }
} \ No newline at end of file