diff options
Diffstat (limited to 'src/Overlay.cpp')
-rw-r--r-- | src/Overlay.cpp | 307 |
1 files changed, 130 insertions, 177 deletions
diff --git a/src/Overlay.cpp b/src/Overlay.cpp index 9a795ee..aa14e3b 100644 --- a/src/Overlay.cpp +++ b/src/Overlay.cpp @@ -29,6 +29,7 @@ #include <X11/extensions/Xfixes.h> #include <X11/extensions/XInput2.h> #include <X11/extensions/shape.h> +#include <X11/Xcursor/Xcursor.h> #include <mglpp/system/Rect.hpp> #include <mglpp/window/Event.hpp> @@ -41,10 +42,6 @@ namespace gsr { 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: @@ -70,17 +67,17 @@ namespace gsr { return texture; } - static bool texture_from_x11_cursor(XFixesCursorImage *x11_cursor_image, bool *visible, mgl::vec2i *hotspot, mgl::Texture &texture) { + static bool texture_from_x11_cursor(XcursorImage *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; + const unsigned int *pixels = NULL; *visible = false; if(!x11_cursor_image) - goto err; + return false; if(!x11_cursor_image->pixels) - goto err; + return false; hotspot->x = x11_cursor_image->xhot; hotspot->y = x11_cursor_image->yhot; @@ -88,12 +85,12 @@ namespace gsr { 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; + return false; 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) { + for(uint32_t y = 0; y < x11_cursor_image->height; ++y) { + for(uint32_t x = 0; x < x11_cursor_image->width; ++x) { uint32_t pixel = *pixels++; uint8_t *in = (uint8_t*)&pixel; uint8_t alpha = in[3]; @@ -114,13 +111,7 @@ namespace gsr { 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) { @@ -299,81 +290,6 @@ namespace gsr { return &win->monitors[0]; } - static mgl::vec2i create_window_get_center_position(Display *display) { - const int size = 16; - XSetWindowAttributes window_attr; - window_attr.event_mask = StructureNotifyMask; - window_attr.background_pixel = 0; - const Window window = XCreateWindow(display, DefaultRootWindow(display), 0, 0, size, size, 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); - - XSizeHints *size_hints = XAllocSizeHints(); - size_hints->width = size; - size_hints->min_width = size; - size_hints->max_width = size; - size_hints->height = size; - size_hints->min_height = size; - size_hints->max_height = size; - size_hints->flags = PSize | PMinSize | PMaxSize; - XSetWMNormalHints(display, window, size_hints); - XFree(size_hints); - - XMapWindow(display, window); - XFlush(display); - - const int x_fd = XConnectionNumber(display); - mgl::vec2i window_pos; - XEvent xev; - while(true) { - struct pollfd poll_fd; - poll_fd.fd = x_fd; - poll_fd.events = POLLIN; - poll_fd.revents = 0; - const int fds_ready = poll(&poll_fd, 1, 1000); - if(fds_ready == 0) { - fprintf(stderr, "Error: timed out waiting for ConfigureNotify after XCreateWindow\n"); - break; - } else if(fds_ready == -1 || !(poll_fd.revents & POLLIN)) { - continue; - } - - 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[] = { @@ -431,9 +347,6 @@ namespace gsr { 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; @@ -470,8 +383,6 @@ namespace gsr { notification_process = -1; } - close_gpu_screen_recorder_output(); - if(gpu_screen_recorder_process > 0) { kill(gpu_screen_recorder_process, SIGINT); int status; @@ -482,10 +393,8 @@ namespace gsr { gpu_screen_recorder_process = -1; } - free(xi_input_xev); - free(xi_output_xev); - if(xi_display) - XCloseDisplay(xi_display); + close_gpu_screen_recorder_output(); + deinit_color_theme(); } void Overlay::xi_setup() { @@ -509,6 +418,7 @@ namespace gsr { 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); @@ -567,8 +477,8 @@ namespace gsr { 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 = de->root_x - window_pos.x; + xi_output_xev->xmotion.y = de->root_y - window_pos.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: @@ -580,8 +490,8 @@ namespace gsr { 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 = de->root_x - window_pos.x; + xi_output_xev->xbutton.y = de->root_y - window_pos.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: @@ -594,8 +504,8 @@ namespace gsr { 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 = de->root_x - window_pos.x; + xi_output_xev->xkey.y = de->root_y - window_pos.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; @@ -694,12 +604,6 @@ namespace gsr { 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); } @@ -737,45 +641,35 @@ namespace gsr { if(!xi_display) return; - XFixesShowCursor(xi_display, DefaultRootWindow(xi_display)); + XFixesHideCursor(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); - } + // TODO: XCURSOR_SIZE and XCURSOR_THEME environment variables + const char *cursor_theme = XcursorGetTheme(xi_display); + if(!cursor_theme) { + //fprintf(stderr, "Warning: failed to get cursor theme, using \"default\" theme instead\n"); + cursor_theme = "default"; + } - void Overlay::xi_grab_all_devices() { - if(!xi_display) - return; + int cursor_size = XcursorGetDefaultSize(xi_display); + if(cursor_size <= 1) + cursor_size = 24; - int num_devices = 0; - XIDeviceInfo *info = XIQueryDevice(xi_display, XIAllDevices, &num_devices); - if(!info) + XcursorImage *cursor_image = XcursorShapeLoadImage(XC_left_ptr, cursor_theme, cursor_size); + if(!cursor_image) { + fprintf(stderr, "Error: failed to get cursor\n"); 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); + bool cursor_visible = false; + texture_from_x11_cursor(cursor_image, &cursor_visible, &cursor_hotspot, cursor_texture); + if(cursor_texture.is_valid()) + cursor_sprite.set_texture(&cursor_texture); + + XcursorImageDestroy(cursor_image); } - void Overlay::xi_warp_pointer(mgl::vec2i position) { + void Overlay::xi_grab_all_devices() { if(!xi_display) return; @@ -784,9 +678,22 @@ namespace gsr { 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]; - XIWarpPointer(xi_display, dev->deviceid, DefaultRootWindow(xi_display), DefaultRootWindow(xi_display), 0, 0, 0, 0, position.x, position.y); + XIEventMask xi_masks; + xi_masks.deviceid = dev->deviceid; + xi_masks.mask_len = sizeof(mask); + xi_masks.mask = mask; + XIGrabDevice(xi_display, dev->deviceid, window->get_system_handle(), CurrentTime, None, XIGrabModeAsync, XIGrabModeAsync, XIOwnerEvents, &xi_masks); } XFlush(xi_display); @@ -805,24 +712,37 @@ namespace gsr { mgl_context *context = mgl_get_context(); Display *display = (Display*)context->connection; + const std::string wm_name = get_window_manager_name(display); + const bool is_kwin = wm_name == "KWin"; + + // 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 + Window x11_cursor_window = None; + const mgl::vec2i cursor_position = get_cursor_position(display, &x11_cursor_window); + const mgl::vec2i monitor_position_query_value = (x11_cursor_window || gsr_info.system_info.display_server != DisplayServer::WAYLAND) ? cursor_position : create_window_get_center_position(display); + // 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); + const bool prevent_game_minimizing = gsr_info.system_info.display_server != DisplayServer::WAYLAND || x11_cursor_window; - mgl::vec2i window_size = { 1280, 720 }; - mgl::vec2i window_pos = { 0, 0 }; + window_size = { 32, 32 }; + 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; + if(prevent_game_minimizing) { + 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.hidden = prevent_game_minimizing; 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.hide_decorations = true; + // MGL_WINDOW_TYPE_DIALOG is needed for kde plasma wayland in some cases, otherwise the window will pop up on another activity + // or may not be visible at all + window_create_params.window_type = (is_kwin && gsr_info.system_info.display_server == DisplayServer::WAYLAND) ? MGL_WINDOW_TYPE_DIALOG : MGL_WINDOW_TYPE_NORMAL; window_create_params.render_api = MGL_RENDER_API_EGL; if(!window->create("gsr ui", window_create_params)) @@ -842,19 +762,23 @@ namespace gsr { mgl_window *win = window->internal_window(); if(win->num_monitors == 0) { fprintf(stderr, "gsr warning: no monitors found, not showing overlay\n"); + window.reset(); 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); + if(prevent_game_minimizing) { + window->set_size(window_size); + window->set_size_limits(window_size, window_size); + window->set_position(window_pos); + } + + win->cursor_position.x = cursor_position.x - window_pos.x; + win->cursor_position.y = cursor_position.y - window_pos.y; update_compositor_texture(focused_monitor); @@ -1032,6 +956,11 @@ namespace gsr { return true; }; + // The focused application can be an xwayland application but the cursor can hover over a wayland application. + // This is even the case when hovering over the titlebar of the xwayland application. + if(prevent_game_minimizing) + xi_setup(); + //window->set_fullscreen(true); if(gsr_info.system_info.display_server == DisplayServer::X11) make_window_click_through(display, window->get_system_handle()); @@ -1045,7 +974,7 @@ namespace gsr { XFreeCursor(display, default_cursor); default_cursor = 0; } - default_cursor = XCreateFontCursor(display, XC_arrow); + default_cursor = XCreateFontCursor(display, XC_left_ptr); XFlush(display); grab_mouse_and_keyboard(); @@ -1107,10 +1036,6 @@ namespace gsr { XFlush(display); if(xi_display) { - // TODO: Only show cursor if it wasn't hidden before the ui was shown - cursor_drawn = false; - //XFixesShowCursor(xi_display, DefaultRootWindow(xi_display)); - //XFlush(xi_display); cursor_texture.clear(); cursor_sprite.set_texture(nullptr); } @@ -1123,29 +1048,45 @@ namespace gsr { visible = false; drawn_first_frame = false; - if(window) { - const mgl::vec2i new_cursor_position = mgl::vec2i(window->internal_window()->pos.x, window->internal_window()->pos.y) + window->get_mouse_position(); - window->set_visible(false); - window.reset(); + if(xi_input_xev) { + free(xi_input_xev); + xi_input_xev = nullptr; + } - if(xi_display) { - XFlush(display); + if(xi_output_xev) { + free(xi_output_xev); + xi_output_xev = nullptr; + } + + if(xi_display) { + XCloseDisplay(xi_display); + xi_display = nullptr; + + if(window) { + mgl_context *context = mgl_get_context(); + Display *display = (Display*)context->connection; + + const mgl::vec2i new_cursor_position = mgl::vec2i(window->internal_window()->pos.x, window->internal_window()->pos.y) + window->get_mouse_position(); XWarpPointer(display, DefaultRootWindow(display), DefaultRootWindow(display), 0, 0, 0, 0, new_cursor_position.x, new_cursor_position.y); XFlush(display); - //xi_warp_pointer(new_cursor_position); - XFixesShowCursor(xi_display, DefaultRootWindow(xi_display)); - XFlush(xi_display); + XFixesShowCursor(display, DefaultRootWindow(display)); + XFlush(display); } } + if(window) { + window->set_visible(false); + window.reset(); + } + deinit_theme(); } void Overlay::toggle_show() { if(visible) { //hide(); - // We dont want to hide immediately because hide is called in event callback, in which it destroys the window. + // We dont want to hide immediately because hide is called in mgl event callback, in which it destroys the mgl window. // Instead remove all pages and wait until next iteration to close the UI (which happens when there are no pages to render). while(!page_stack.empty()) { page_stack.pop(); @@ -1211,7 +1152,7 @@ namespace gsr { const char *notification_type_str = notification_type_to_string(notification_type); if(notification_type_str) { notification_args[9] = "--icon"; - notification_args[10] = "record", + notification_args[10] = notification_type_str; notification_args[11] = nullptr; } else { notification_args[9] = nullptr; @@ -1241,6 +1182,10 @@ namespace gsr { do_exit = true; } + const Config& Overlay::get_config() const { + return config; + } + void Overlay::update_notification_process_status() { if(notification_process <= 0) return; @@ -1306,12 +1251,18 @@ namespace gsr { truncate_string(focused_window_name, 20); std::string text; switch(notification_type) { - case NotificationType::RECORD: + case NotificationType::RECORD: { + if(!config.record_config.show_video_saved_notifications) + return; text = "Saved recording to '" + focused_window_name + "/" + video_filename + "'"; break; - case NotificationType::REPLAY: + } + case NotificationType::REPLAY: { + if(!config.replay_config.show_replay_saved_notifications) + return; text = "Saved replay to '" + focused_window_name + "/" + video_filename + "'"; break; + } case NotificationType::NONE: case NotificationType::STREAM: break; @@ -1973,11 +1924,11 @@ namespace gsr { "-fm", framerate_mode.c_str(), "-encoder", encoder, "-f", fps.c_str(), - "-f", fps.c_str(), "-v", "no", "-o", url.c_str() }; + config.streaming_config.record_options.merge_audio_tracks = true; add_common_gpu_screen_recorder_args(args, config.streaming_config.record_options, audio_tracks, video_bitrate, region, audio_tracks_merged); args.push_back(nullptr); @@ -2016,8 +1967,10 @@ namespace gsr { return false; bool window_texture_loaded = false; - const Window focused_window = get_focused_window(display, WindowCaptureType::FOCUSED); - if(is_window_fullscreen_on_monitor(display, focused_window, monitor) && focused_window) + Window focused_window = get_focused_window(display, WindowCaptureType::CURSOR); + if(!focused_window) + focused_window = get_focused_window(display, WindowCaptureType::FOCUSED); + if(focused_window && is_window_fullscreen_on_monitor(display, focused_window, monitor)) window_texture_loaded = window_texture_init(&window_texture, display, mgl_window_get_egl_display(window->internal_window()), focused_window, egl_funcs) == 0; if(window_texture_loaded && window_texture.texture_id) { |