#include "../include/Overlay.hpp" #include "../include/Theme.hpp" #include "../include/Config.hpp" #include "../include/Process.hpp" #include "../include/Utils.hpp" #include "../include/gui/StaticPage.hpp" #include "../include/gui/DropdownButton.hpp" #include "../include/gui/CustomRendererWidget.hpp" #include "../include/gui/SettingsPage.hpp" #include "../include/gui/GlobalSettingsPage.hpp" #include "../include/gui/Utils.hpp" #include "../include/gui/PageStack.hpp" #include "../include/gui/GsrPage.hpp" #include "../include/WindowUtils.hpp" #include "../include/GlobalHotkeys.hpp" #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include extern "C" { #include } namespace gsr { static const mgl::Color bg_color(0, 0, 0, 100); static const double force_window_on_top_timeout_seconds = 1.0; static const double replay_status_update_check_timeout_seconds = 1.0; static bool is_focused_application_wayland(Display *dpy) { return get_focused_window(dpy, WindowCaptureType::FOCUSED) == 0; } static mgl::Texture texture_from_ximage(XImage *img) { uint8_t *texture_data = (uint8_t*)malloc(img->width * img->height * 3); // TODO: for(int y = 0; y < img->height; ++y) { for(int x = 0; x < img->width; ++x) { unsigned long pixel = XGetPixel(img, x, y); unsigned char red = (pixel & img->red_mask) >> 16; unsigned char green = (pixel & img->green_mask) >> 8; unsigned char blue = pixel & img->blue_mask; const size_t texture_data_index = (x + y * img->width) * 3; texture_data[texture_data_index + 0] = red; texture_data[texture_data_index + 1] = green; texture_data[texture_data_index + 2] = blue; } } mgl::Texture texture; // TODO: texture.load_from_memory(texture_data, img->width, img->height, MGL_IMAGE_FORMAT_RGB); free(texture_data); return texture; } static bool texture_from_x11_cursor(XFixesCursorImage *x11_cursor_image, bool *visible, mgl::vec2i *hotspot, mgl::Texture &texture) { uint8_t *cursor_data = NULL; uint8_t *out = NULL; const unsigned long *pixels = NULL; *visible = false; if(!x11_cursor_image) goto err; if(!x11_cursor_image->pixels) goto err; hotspot->x = x11_cursor_image->xhot; hotspot->y = x11_cursor_image->yhot; pixels = x11_cursor_image->pixels; cursor_data = (uint8_t*)malloc((int)x11_cursor_image->width * (int)x11_cursor_image->height * 4); if(!cursor_data) goto err; out = cursor_data; /* Un-premultiply alpha */ for(int y = 0; y < x11_cursor_image->height; ++y) { for(int x = 0; x < x11_cursor_image->width; ++x) { uint32_t pixel = *pixels++; uint8_t *in = (uint8_t*)&pixel; uint8_t alpha = in[3]; if(alpha == 0) { alpha = 1; } else { *visible = true; } out[0] = (float)in[2] * 255.0/(float)alpha; out[1] = (float)in[1] * 255.0/(float)alpha; out[2] = (float)in[0] * 255.0/(float)alpha; out[3] = in[3]; out += 4; in += 4; } } texture.load_from_memory(cursor_data, x11_cursor_image->width, x11_cursor_image->height, MGL_IMAGE_FORMAT_RGBA); free(cursor_data); XFree(x11_cursor_image); return true; err: if(x11_cursor_image) XFree(x11_cursor_image); return false; } static char hex_value_to_str(uint8_t v) { if(v <= 9) return '0' + v; else if(v >= 10 && v <= 15) return 'A' + (v - 10); else return '0'; } // Excludes alpha static std::string color_to_hex_str(mgl::Color color) { std::string result; result.resize(6); result[0] = hex_value_to_str((color.r & 0xF0) >> 4); result[1] = hex_value_to_str(color.r & 0x0F); result[2] = hex_value_to_str((color.g & 0xF0) >> 4); result[3] = hex_value_to_str(color.g & 0x0F); result[4] = hex_value_to_str((color.b & 0xF0) >> 4); result[5] = hex_value_to_str(color.b & 0x0F); return result; } struct DrawableGeometry { int x, y, width, height; }; static bool get_drawable_geometry(Display *display, Drawable drawable, DrawableGeometry *geometry) { geometry->x = 0; geometry->y = 0; geometry->width = 0; geometry->height = 0; Window root_window; unsigned int w, h; unsigned int dummy_border, dummy_depth; Status s = XGetGeometry(display, drawable, &root_window, &geometry->x, &geometry->y, &w, &h, &dummy_border, &dummy_depth); geometry->width = w; geometry->height = h; return s != Success; } static bool diff_int(int a, int b, int difference) { return std::abs(a - b) <= difference; } static bool is_window_fullscreen_on_monitor(Display *display, Window window, const mgl_monitor *monitor) { if(!window) return false; DrawableGeometry geometry; if(!get_drawable_geometry(display, window, &geometry)) return false; const int margin = 2; return diff_int(geometry.x, monitor->pos.x, margin) && diff_int(geometry.y, monitor->pos.y, margin) && diff_int(geometry.width, monitor->size.x, margin) && diff_int(geometry.height, monitor->size.y, margin); } /*static bool is_window_fullscreen_on_monitor(Display *display, Window window, const mgl_monitor *monitors, int num_monitors) { if(!window) return false; DrawableGeometry geometry; if(!get_drawable_geometry(display, window, &geometry)) return false; const int margin = 2; for(int i = 0; i < num_monitors; ++i) { const mgl_monitor *mon = &monitors[i]; if(diff_int(geometry.x, mon->pos.x, margin) && diff_int(geometry.y, mon->pos.y, margin) && diff_int(geometry.width, mon->size.x, margin) && diff_int(geometry.height, mon->size.y, margin)) { return true; } } return false; }*/ static 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; } static void set_focused_window(Display *dpy, Window window) { XSetInputFocus(dpy, window, RevertToPointerRoot, CurrentTime); const Atom net_active_window_atom = XInternAtom(dpy, "_NET_ACTIVE_WINDOW", False); XChangeProperty(dpy, DefaultRootWindow(dpy), net_active_window_atom, XA_WINDOW, 32, PropModeReplace, (const unsigned char*)&window, 1); XFlush(dpy); } #define _NET_WM_STATE_REMOVE 0 #define _NET_WM_STATE_ADD 1 #define _NET_WM_STATE_TOGGLE 2 static 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; } static 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); } static Bool make_window_sticky(Display *dpy, Window window) { return set_window_wm_state(dpy, window, XInternAtom(dpy, "_NET_WM_STATE_STICKY", False)); } static Bool hide_window_from_taskbar(Display *dpy, Window window) { return set_window_wm_state(dpy, window, XInternAtom(dpy, "_NET_WM_STATE_SKIP_TASKBAR", False)); } // Returns the first monitor if not found. Assumes there is at least one monitor connected. static const mgl_monitor* find_monitor_at_position(mgl::Window &window, mgl::vec2i pos) { const mgl_window *win = window.internal_window(); assert(win->num_monitors > 0); for(int i = 0; i < win->num_monitors; ++i) { const mgl_monitor *mon = &win->monitors[i]; if(mgl::IntRect({ mon->pos.x, mon->pos.y }, { mon->size.x, mon->size.y }).contains(pos)) return mon; } return &win->monitors[0]; } static mgl::vec2i create_window_get_center_position(Display *display) { XSetWindowAttributes window_attr; window_attr.event_mask = StructureNotifyMask; window_attr.background_pixel = 0; const Window window = XCreateWindow(display, DefaultRootWindow(display), 0, 0, 16, 16, 0, CopyFromParent, InputOutput, CopyFromParent, CWBackPixel | CWEventMask, &window_attr); if(!window) return {0, 0}; const Atom net_wm_window_type_atom = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False); const Atom net_wm_window_type_notification_atom = XInternAtom(display, "_NET_WM_WINDOW_TYPE_NOTIFICATION", False); const Atom net_wm_window_type_utility = XInternAtom(display, "_NET_WM_WINDOW_TYPE_UTILITY", False); const Atom net_wm_window_opacity = XInternAtom(display, "_NET_WM_WINDOW_OPACITY", False); const Atom window_type_atoms[2] = { net_wm_window_type_notification_atom, net_wm_window_type_utility }; XChangeProperty(display, window, net_wm_window_type_atom, XA_ATOM, 32, PropModeReplace, (const unsigned char*)window_type_atoms, 2L); const double alpha = 0.0; const unsigned long opacity = (unsigned long)(0xFFFFFFFFul * alpha); XChangeProperty(display, window, net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1L); XMapWindow(display, window); XFlush(display); mgl::vec2i window_pos; XEvent xev; while(true) { XNextEvent(display, &xev); if(xev.type == ConfigureNotify) { window_pos.x = xev.xconfigure.x + xev.xconfigure.width / 2; window_pos.y = xev.xconfigure.y + xev.xconfigure.height / 2; break; } } XDestroyWindow(display, window); XFlush(display); return window_pos; } static 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); return XGetSelectionOwner(dpy, prop_atom) != None; } static std::string get_power_supply_online_filepath() { std::string result; const char *paths[] = { "/sys/class/power_supply/ADP0/online", "/sys/class/power_supply/ADP1/online", "/sys/class/power_supply/AC/online", "/sys/class/power_supply/ACAD/online" }; for(const char *power_supply_online_filepath : paths) { if(access(power_supply_online_filepath, F_OK) == 0) { result = power_supply_online_filepath; break; } } return result; } static bool power_supply_is_connected(const char *power_supply_online_filepath) { int fd = open(power_supply_online_filepath, O_RDONLY); if(fd == -1) return false; char buf[1]; const bool is_connected = read(fd, buf, 1) == 1 && buf[0] == '1'; close(fd); return is_connected; } static bool xinput_is_supported(Display *dpy, int *xi_opcode) { *xi_opcode = 0; int query_event = 0; int query_error = 0; if(!XQueryExtension(dpy, "XInputExtension", xi_opcode, &query_event, &query_error)) { fprintf(stderr, "gsr-ui error: X Input extension not available\n"); return false; } int major = 2; int minor = 1; int retval = XIQueryVersion(dpy, &major, &minor); if (retval != Success) { fprintf(stderr, "gsr-ui error: XInput 2.1 is not supported\n"); return false; } return true; } Overlay::Overlay(std::string resources_path, GsrInfo gsr_info, SupportedCaptureOptions capture_options, egl_functions egl_funcs) : resources_path(std::move(resources_path)), gsr_info(std::move(gsr_info)), egl_funcs(egl_funcs), config(capture_options), bg_screenshot_overlay({0.0f, 0.0f}), top_bar_background({0.0f, 0.0f}), close_button_widget({0.0f, 0.0f}) { // TODO: //xi_setup(); key_bindings[0].key_event.code = mgl::Keyboard::Escape; key_bindings[0].key_event.alt = false; key_bindings[0].key_event.control = false; key_bindings[0].key_event.shift = false; key_bindings[0].key_event.system = false; key_bindings[0].callback = [this]() { page_stack.pop(); }; memset(&window_texture, 0, sizeof(window_texture)); std::optional new_config = read_config(capture_options); if(new_config) config = std::move(new_config.value()); init_color_theme(config, this->gsr_info); power_supply_online_filepath = get_power_supply_online_filepath(); if(config.replay_config.turn_on_replay_automatically_mode == "turn_on_at_system_startup") on_press_start_replay(true); } Overlay::~Overlay() { hide(); if(notification_process > 0) { kill(notification_process, SIGKILL); int status; if(waitpid(notification_process, &status, 0) == -1) { perror("waitpid failed"); /* Ignore... */ } notification_process = -1; } close_gpu_screen_recorder_output(); if(gpu_screen_recorder_process > 0) { kill(gpu_screen_recorder_process, SIGINT); int status; if(waitpid(gpu_screen_recorder_process, &status, 0) == -1) { perror("waitpid failed"); /* Ignore... */ } gpu_screen_recorder_process = -1; } free(xi_input_xev); free(xi_output_xev); if(xi_display) XCloseDisplay(xi_display); } void Overlay::xi_setup() { xi_display = XOpenDisplay(nullptr); if(!xi_display) { fprintf(stderr, "gsr-ui error: failed to setup XI connection\n"); return; } if(!xinput_is_supported(xi_display, &xi_opcode)) goto error; xi_input_xev = (XEvent*)calloc(1, sizeof(XEvent)); if(!xi_input_xev) throw std::runtime_error("gsr-ui error: failed to allocate XEvent data"); xi_output_xev = (XEvent*)calloc(1, sizeof(XEvent)); if(!xi_output_xev) throw std::runtime_error("gsr-ui error: failed to allocate XEvent data"); unsigned char mask[XIMaskLen(XI_LASTEVENT)]; memset(mask, 0, sizeof(mask)); XISetMask(mask, XI_Motion); XISetMask(mask, XI_ButtonPress); XISetMask(mask, XI_ButtonRelease); XISetMask(mask, XI_KeyPress); XISetMask(mask, XI_KeyRelease); XIEventMask xi_masks; xi_masks.deviceid = XIAllMasterDevices; xi_masks.mask_len = sizeof(mask); xi_masks.mask = mask; if(XISelectEvents(xi_display, DefaultRootWindow(xi_display), &xi_masks, 1) != Success) { fprintf(stderr, "gsr-ui error: XISelectEvents failed\n"); goto error; } XFlush(xi_display); return; error: free(xi_input_xev); xi_input_xev = nullptr; free(xi_output_xev); xi_output_xev = nullptr; if(xi_display) { XCloseDisplay(xi_display); xi_display = nullptr; } } void Overlay::close_gpu_screen_recorder_output() { if(gpu_screen_recorder_process_output_file) { fclose(gpu_screen_recorder_process_output_file); gpu_screen_recorder_process_output_file = nullptr; } if(gpu_screen_recorder_process_output_fd > 0) { close(gpu_screen_recorder_process_output_fd); gpu_screen_recorder_process_output_fd = -1; } } void Overlay::handle_xi_events() { if(!xi_display) return; mgl_context *context = mgl_get_context(); Display *display = (Display*)context->connection; while(XPending(xi_display)) { XNextEvent(xi_display, xi_input_xev); XGenericEventCookie *cookie = &xi_input_xev->xcookie; if(cookie->type == GenericEvent && cookie->extension == xi_opcode && XGetEventData(xi_display, cookie)) { const XIDeviceEvent *de = (XIDeviceEvent*)cookie->data; if(cookie->evtype == XI_Motion) { memset(xi_output_xev, 0, sizeof(*xi_output_xev)); xi_output_xev->type = MotionNotify; xi_output_xev->xmotion.display = display; xi_output_xev->xmotion.window = window->get_system_handle(); xi_output_xev->xmotion.subwindow = window->get_system_handle(); xi_output_xev->xmotion.x = de->event_x; xi_output_xev->xmotion.y = de->event_y; xi_output_xev->xmotion.x_root = de->root_x; xi_output_xev->xmotion.y_root = de->root_y; //xi_output_xev->xmotion.state = // modifiers // TODO: if(window->inject_x11_event(xi_output_xev, event)) on_event(event); } else if(cookie->evtype == XI_ButtonPress || cookie->evtype == XI_ButtonRelease) { memset(xi_output_xev, 0, sizeof(*xi_output_xev)); xi_output_xev->type = cookie->evtype == XI_ButtonPress ? ButtonPress : ButtonRelease; xi_output_xev->xbutton.display = display; xi_output_xev->xbutton.window = window->get_system_handle(); xi_output_xev->xbutton.subwindow = window->get_system_handle(); xi_output_xev->xbutton.x = de->event_x; xi_output_xev->xbutton.y = de->event_y; xi_output_xev->xbutton.x_root = de->root_x; xi_output_xev->xbutton.y_root = de->root_y; //xi_output_xev->xbutton.state = // modifiers // TODO: xi_output_xev->xbutton.button = de->detail; if(window->inject_x11_event(xi_output_xev, event)) on_event(event); } else if(cookie->evtype == XI_KeyPress || cookie->evtype == XI_KeyRelease) { memset(xi_output_xev, 0, sizeof(*xi_output_xev)); xi_output_xev->type = cookie->evtype == XI_KeyPress ? KeyPress : KeyRelease; xi_output_xev->xkey.display = display; xi_output_xev->xkey.window = window->get_system_handle(); xi_output_xev->xkey.subwindow = window->get_system_handle(); xi_output_xev->xkey.x = de->event_x; xi_output_xev->xkey.y = de->event_y; xi_output_xev->xkey.x_root = de->root_x; xi_output_xev->xkey.y_root = de->root_y; xi_output_xev->xkey.state = de->mods.effective; xi_output_xev->xkey.keycode = de->detail; if(window->inject_x11_event(xi_output_xev, event)) on_event(event); } //fprintf(stderr, "got xi event: %d\n", cookie->evtype); XFreeEventData(xi_display, cookie); } } } static uint32_t key_event_to_bitmask(mgl::Event::KeyEvent key_event) { return ((uint32_t)key_event.alt << (uint32_t)0) | ((uint32_t)key_event.control << (uint32_t)1) | ((uint32_t)key_event.shift << (uint32_t)2) | ((uint32_t)key_event.system << (uint32_t)3); } void Overlay::process_key_bindings(mgl::Event &event) { if(event.type != mgl::Event::KeyReleased) return; const uint32_t event_key_bitmask = key_event_to_bitmask(event.key); for(const KeyBinding &key_binding : key_bindings) { if(event.key.code == key_binding.key_event.code && event_key_bitmask == key_event_to_bitmask(key_binding.key_event)) key_binding.callback(); } } void Overlay::handle_events(gsr::GlobalHotkeys *global_hotkeys) { if(!visible || !window) return; handle_xi_events(); while(window->poll_event(event)) { if(global_hotkeys) { if(!global_hotkeys->on_event(event)) continue; } on_event(event); } } void Overlay::on_event(mgl::Event &event) { if(!visible || !window) return; if(!close_button_widget.on_event(event, *window, mgl::vec2f(0.0f, 0.0f))) return; if(!page_stack.on_event(event, *window, mgl::vec2f(0.0f, 0.0f))) return; process_key_bindings(event); } bool Overlay::draw() { update_notification_process_status(); update_gsr_replay_save(); update_gsr_process_status(); replay_status_update_status(); if(!visible) return false; if(page_stack.empty()) { hide(); return false; } if(!window) return false; grab_mouse_and_keyboard(); //force_window_on_top(); window->clear(bg_color); if(window_texture_sprite.get_texture() && window_texture.texture_id) { window->draw(window_texture_sprite); window->draw(bg_screenshot_overlay); } else if(screenshot_texture.is_valid()) { window->draw(screenshot_sprite); window->draw(bg_screenshot_overlay); } window->draw(top_bar_background); window->draw(top_bar_text); window->draw(logo_sprite); close_button_widget.draw(*window, mgl::vec2f(0.0f, 0.0f)); page_stack.draw(*window, mgl::vec2f(0.0f, 0.0f)); if(cursor_texture.is_valid()) { if(!cursor_drawn) { cursor_drawn = true; XFixesHideCursor(xi_display, DefaultRootWindow(xi_display)); XFlush(xi_display); } cursor_sprite.set_position((window->get_mouse_position() - cursor_hotspot).to_vec2f()); window->draw(cursor_sprite); } window->display(); if(!drawn_first_frame) { drawn_first_frame = true; mgl::Event event; event.type = mgl::Event::MouseMoved; event.mouse_move.x = window->get_mouse_position().x; event.mouse_move.y = window->get_mouse_position().y; on_event(event); } return true; } void Overlay::grab_mouse_and_keyboard() { // TODO: Remove these grabs when debugging with a debugger, or your X11 session will appear frozen. // There should be a debug mode to not use these mgl_context *context = mgl_get_context(); Display *display = (Display*)context->connection; XGrabPointer(display, window->get_system_handle(), True, ButtonPressMask | ButtonReleaseMask | PointerMotionMask | Button1MotionMask | Button2MotionMask | Button3MotionMask | Button4MotionMask | Button5MotionMask | ButtonMotionMask, GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime); // TODO: This breaks global hotkeys (when using x11 global hotkeys) XGrabKeyboard(display, window->get_system_handle(), True, GrabModeAsync, GrabModeAsync, CurrentTime); XFlush(display); } void Overlay::xi_setup_fake_cursor() { if(!xi_display) return; XFixesShowCursor(xi_display, DefaultRootWindow(xi_display)); XFlush(xi_display); bool cursor_visible = false; texture_from_x11_cursor(XFixesGetCursorImage(xi_display), &cursor_visible, &cursor_hotspot, cursor_texture); if(cursor_texture.is_valid()) cursor_sprite.set_texture(&cursor_texture); } void Overlay::xi_grab_all_devices() { if(!xi_display) return; int num_devices = 0; XIDeviceInfo *info = XIQueryDevice(xi_display, XIAllDevices, &num_devices); if(!info) return; for (int i = 0; i < num_devices; ++i) { const XIDeviceInfo *dev = &info[i]; XIEventMask masks[1]; unsigned char mask0[XIMaskLen(XI_LASTEVENT)]; memset(mask0, 0, sizeof(mask0)); XISetMask(mask0, XI_Motion); XISetMask(mask0, XI_ButtonPress); XISetMask(mask0, XI_ButtonRelease); XISetMask(mask0, XI_KeyPress); XISetMask(mask0, XI_KeyRelease); masks[0].deviceid = dev->deviceid; masks[0].mask_len = sizeof(mask0); masks[0].mask = mask0; XIGrabDevice(xi_display, dev->deviceid, window->get_system_handle(), CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, XIOwnerEvents, masks); } XFlush(xi_display); XIFreeDeviceInfo(info); } void Overlay::xi_warp_pointer(mgl::vec2i position) { if(!xi_display) return; int num_devices = 0; XIDeviceInfo *info = XIQueryDevice(xi_display, XIAllDevices, &num_devices); if(!info) return; for (int i = 0; i < num_devices; ++i) { const XIDeviceInfo *dev = &info[i]; XIWarpPointer(xi_display, dev->deviceid, DefaultRootWindow(xi_display), DefaultRootWindow(xi_display), 0, 0, 0, 0, position.x, position.y); } XFlush(xi_display); XIFreeDeviceInfo(info); } void Overlay::show() { if(visible) return; drawn_first_frame = false; window.reset(); window = std::make_unique(); deinit_theme(); mgl_context *context = mgl_get_context(); Display *display = (Display*)context->connection; // Wayland doesn't allow XGrabPointer/XGrabKeyboard when a wayland application is focused. // If the focused window is a wayland application then don't use override redirect and instead create // a fullscreen window for the ui. const bool prevent_game_minimizing = gsr_info.system_info.display_server != DisplayServer::WAYLAND || !is_focused_application_wayland(display); mgl::vec2i window_size = { 1280, 720 }; mgl::vec2i window_pos = { 0, 0 }; mgl::Window::CreateParams window_create_params; window_create_params.size = window_size; window_create_params.min_size = window_size; window_create_params.max_size = window_size; window_create_params.position = window_pos; window_create_params.hidden = true; window_create_params.override_redirect = prevent_game_minimizing; window_create_params.background_color = bg_color; window_create_params.support_alpha = true; window_create_params.window_type = MGL_WINDOW_TYPE_NORMAL; window_create_params.render_api = MGL_RENDER_API_EGL; if(!window->create("gsr ui", window_create_params)) fprintf(stderr, "error: failed to create window\n"); unsigned char data = 2; // Prefer being composed to allow transparency XChangeProperty(display, window->get_system_handle(), XInternAtom(display, "_NET_WM_BYPASS_COMPOSITOR", False), XA_CARDINAL, 32, PropModeReplace, &data, 1); data = 1; XChangeProperty(display, window->get_system_handle(), XInternAtom(display, "GAMESCOPE_EXTERNAL_OVERLAY", False), XA_CARDINAL, 32, PropModeReplace, &data, 1); if(!init_theme(resources_path)) { fprintf(stderr, "Error: failed to load theme\n"); exit(1); } mgl_window *win = window->internal_window(); if(win->num_monitors == 0) { fprintf(stderr, "gsr warning: no monitors found, not showing overlay\n"); return; } // The cursor position is wrong on wayland if an x11 window is not focused. On wayland we instead create a window and get the position where the wayland compositor puts it const mgl::vec2i monitor_position_query_value = gsr_info.system_info.display_server == DisplayServer::WAYLAND ? create_window_get_center_position(display) : mgl::vec2i(win->cursor_position.x, win->cursor_position.y); const mgl_monitor *focused_monitor = find_monitor_at_position(*window, monitor_position_query_value); window_pos = {focused_monitor->pos.x, focused_monitor->pos.y}; window_size = {focused_monitor->size.x, focused_monitor->size.y}; get_theme().set_window_size(window_size); window->set_size(window_size); window->set_size_limits(window_size, window_size); window->set_position(window_pos); update_compositor_texture(focused_monitor); top_bar_text = mgl::Text("GPU Screen Recorder", get_theme().top_bar_font); logo_sprite = mgl::Sprite(&get_theme().logo_texture); bg_screenshot_overlay = mgl::Rectangle(mgl::vec2f(get_theme().window_width, get_theme().window_height)); top_bar_background = mgl::Rectangle(mgl::vec2f(get_theme().window_width, get_theme().window_height*0.06f).floor()); top_bar_text = mgl::Text("GPU Screen Recorder", get_theme().top_bar_font); logo_sprite = mgl::Sprite(&get_theme().logo_texture); close_button_widget.set_size(mgl::vec2f(top_bar_background.get_size().y * 0.35f, top_bar_background.get_size().y * 0.35f).floor()); bg_screenshot_overlay.set_color(bg_color); top_bar_background.set_color(mgl::Color(0, 0, 0, 180)); //top_bar_text.set_color(get_color_theme().tint_color); top_bar_text.set_position((top_bar_background.get_position() + top_bar_background.get_size()*0.5f - top_bar_text.get_bounds().size*0.5f).floor()); logo_sprite.set_height((int)(top_bar_background.get_size().y * 0.65f)); logo_sprite.set_position(mgl::vec2f( (top_bar_background.get_size().y - logo_sprite.get_size().y) * 0.5f, top_bar_background.get_size().y * 0.5f - logo_sprite.get_size().y * 0.5f ).floor()); close_button_widget.set_position(mgl::vec2f(get_theme().window_width - close_button_widget.get_size().x - logo_sprite.get_position().x, top_bar_background.get_size().y * 0.5f - close_button_widget.get_size().y * 0.5f).floor()); while(!page_stack.empty()) { page_stack.pop(); } auto front_page = std::make_unique(window_size.to_vec2f()); StaticPage *front_page_ptr = front_page.get(); page_stack.push(std::move(front_page)); const int button_height = window_size.y / 5.0f; const int button_width = button_height; auto main_buttons_list = std::make_unique(List::Orientation::HORIZONTAL); List * main_buttons_list_ptr = main_buttons_list.get(); main_buttons_list->set_spacing(0.0f); { auto button = std::make_unique(&get_theme().title_font, &get_theme().body_font, "Instant Replay", "Off", &get_theme().replay_button_texture, mgl::vec2f(button_width, button_height)); replay_dropdown_button_ptr = button.get(); button->add_item("Turn on", "start", "Alt+Shift+F10"); button->add_item("Save", "save", "Alt+F10"); button->add_item("Settings", "settings"); button->set_item_icon("start", &get_theme().play_texture); button->set_item_icon("save", &get_theme().save_texture); button->on_click = [this](const std::string &id) { if(id == "settings") { auto replay_settings_page = std::make_unique(SettingsPage::Type::REPLAY, &gsr_info, config, &page_stack); page_stack.push(std::move(replay_settings_page)); } else if(id == "save") { on_press_save_replay(); } else if(id == "start") { on_press_start_replay(false); } }; main_buttons_list->add_widget(std::move(button)); } { auto button = std::make_unique(&get_theme().title_font, &get_theme().body_font, "Record", "Not recording", &get_theme().record_button_texture, mgl::vec2f(button_width, button_height)); record_dropdown_button_ptr = button.get(); button->add_item("Start", "start", "Alt+F9"); button->add_item("Pause", "pause", "Alt+F7"); button->add_item("Settings", "settings"); button->set_item_icon("start", &get_theme().play_texture); button->set_item_icon("pause", &get_theme().pause_texture); button->on_click = [this](const std::string &id) { if(id == "settings") { auto record_settings_page = std::make_unique(SettingsPage::Type::RECORD, &gsr_info, config, &page_stack); page_stack.push(std::move(record_settings_page)); } else if(id == "pause") { toggle_pause(); } else if(id == "start") { on_press_start_record(); } }; main_buttons_list->add_widget(std::move(button)); } { auto button = std::make_unique(&get_theme().title_font, &get_theme().body_font, "Livestream", "Not streaming", &get_theme().stream_button_texture, mgl::vec2f(button_width, button_height)); stream_dropdown_button_ptr = button.get(); button->add_item("Start", "start", "Alt+F8"); button->add_item("Settings", "settings"); button->set_item_icon("start", &get_theme().play_texture); button->on_click = [this](const std::string &id) { if(id == "settings") { auto stream_settings_page = std::make_unique(SettingsPage::Type::STREAM, &gsr_info, config, &page_stack); page_stack.push(std::move(stream_settings_page)); } else if(id == "start") { on_press_start_stream(); } }; main_buttons_list->add_widget(std::move(button)); } const mgl::vec2f main_buttons_list_size = main_buttons_list->get_size(); main_buttons_list->set_position((mgl::vec2f(window_size.x * 0.5f, window_size.y * 0.25f) - main_buttons_list_size * 0.5f).floor()); front_page_ptr->add_widget(std::move(main_buttons_list)); { const mgl::vec2f main_buttons_size = main_buttons_list_ptr->get_size(); const int settings_button_size = main_buttons_size.y * 0.2f; auto button = std::make_unique