aboutsummaryrefslogtreecommitdiff
path: root/src/WindowUtils.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/WindowUtils.cpp')
-rw-r--r--src/WindowUtils.cpp181
1 files changed, 95 insertions, 86 deletions
diff --git a/src/WindowUtils.cpp b/src/WindowUtils.cpp
index 7631e4d..d8e3508 100644
--- a/src/WindowUtils.cpp
+++ b/src/WindowUtils.cpp
@@ -4,11 +4,20 @@
#include <X11/Xatom.h>
#include <X11/Xutil.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>
+#include <optional>
+
#define MAX_PROPERTY_VALUE_LEN 4096
namespace gsr {
@@ -89,15 +98,16 @@ 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);
+ 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);
- return window;
+ *window = window_get_target_window_child(dpy, *window);
+ return root_pos;
}
Window get_focused_window(Display *dpy, WindowCaptureType cap_type) {
@@ -127,14 +137,33 @@ 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;
+ }
+
+ static std::optional<std::string> get_window_title(Display *dpy, Window window) {
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);
@@ -147,7 +176,7 @@ namespace gsr {
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;
+ return utf8_sanitize(data, num_items);
type = None;
format = 0;
@@ -157,16 +186,18 @@ namespace gsr {
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 utf8_sanitize(data, num_items);
- return NULL;
+ return std::nullopt;
}
- static const char* strip(const char *str, int *len) {
- int str_len = strlen(str);
+ 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] != ' ') {
- str += i;
+ start_index += i;
str_len -= i;
break;
}
@@ -179,14 +210,7 @@ namespace gsr {
}
}
- *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);
+ return str.substr(start_index, str_len);
}
std::string get_focused_window_name(Display *dpy, WindowCaptureType window_capture_type) {
@@ -196,52 +220,22 @@ 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);
+ result = strip(class_hint.res_class);
return result;
}
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);
-
- // 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;
-
- 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;
- }
- }
- }
-
- return root_pos;
- }
-
typedef struct {
unsigned long flags;
unsigned long functions;
@@ -312,7 +306,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 +314,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);
@@ -373,7 +370,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 +378,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 +442,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 +452,18 @@ 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;
}
+
+ 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);
+ return monitors;
+ }
} \ No newline at end of file