diff options
Diffstat (limited to 'src/WindowUtils.cpp')
-rw-r--r-- | src/WindowUtils.cpp | 343 |
1 files changed, 280 insertions, 63 deletions
diff --git a/src/WindowUtils.cpp b/src/WindowUtils.cpp index d8e3508..c6b278b 100644 --- a/src/WindowUtils.cpp +++ b/src/WindowUtils.cpp @@ -1,8 +1,12 @@ #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> @@ -16,8 +20,6 @@ extern "C" { #include <string.h> #include <poll.h> -#include <optional> - #define MAX_PROPERTY_VALUE_LEN 4096 namespace gsr { @@ -61,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; @@ -105,8 +107,17 @@ namespace gsr { 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); - if(window) - *window = window_get_target_window_child(dpy, *window); + + 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; } @@ -149,7 +160,7 @@ namespace gsr { 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) { + if(i + 3 <= size && memcmp(str + i, "\xEF\xBB\xBF", 3) == 0) { i += 3; continue; } @@ -163,7 +174,8 @@ namespace gsr { return result; } - static std::optional<std::string> get_window_title(Display *dpy, Window window) { + 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); @@ -175,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 utf8_sanitize(data, num_items); + if(type == utf8_string_atom && format == 8 && data) { + result = utf8_sanitize(data, num_items); + goto done; + } + + if(data) + XFree(data); type = None; format = 0; @@ -185,32 +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 utf8_sanitize(data, num_items); - - return std::nullopt; - } - - static std::string strip(const std::string &str) { - int start_index = 0; - int str_len = str.size(); - - for(int i = 0; i < str_len; ++i) { - if(str[i] != ' ') { - start_index += i; - str_len -= i; - break; - } - } - - for(int i = str_len - 1; i >= 0; --i) { - if(str[i] != ' ') { - str_len = i + 1; - break; - } + if((type == XA_STRING || type == utf8_string_atom) && data) { + result = utf8_sanitize(data, num_items); + goto done; } - return str.substr(start_index, str_len); + done: + if(data) + XFree(data); + return result; } std::string get_focused_window_name(Display *dpy, WindowCaptureType window_capture_type) { @@ -228,14 +228,76 @@ namespace gsr { XClassHint class_hint = {nullptr, nullptr}; XGetClassHint(dpy, focused_window, &class_hint); - if(class_hint.res_class) { + 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; + } + + 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; + + XWindowAttributes attr; + memset(&attr, 0, sizeof(attr)); + XGetWindowAttributes(dpy, children[i], &attr); + if(attr.override_redirect || attr.c_class != InputOutput || attr.map_state != IsViewable) + continue; + + 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; + } } + 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 { unsigned long flags; unsigned long functions; @@ -283,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); @@ -347,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); @@ -456,14 +498,189 @@ namespace gsr { return XGetSelectionOwner(dpy, prop_atom) != None; } - static void get_monitors_callback(const mgl_monitor *monitor, void *userdata) { - std::vector<Monitor> *monitors = (std::vector<Monitor>*)userdata; - monitors->push_back({mgl::vec2i(monitor->pos.x, monitor->pos.y), mgl::vec2i(monitor->size.x, monitor->size.y)}); - } - std::vector<Monitor> get_monitors(Display *dpy) { std::vector<Monitor> monitors; - mgl_for_each_active_monitor_output(dpy, get_monitors_callback, &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 |