diff options
Diffstat (limited to 'src/main.cpp')
-rw-r--r-- | src/main.cpp | 282 |
1 files changed, 132 insertions, 150 deletions
diff --git a/src/main.cpp b/src/main.cpp index 7aeb0cc..1f9fb89 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,10 +1,13 @@ + +#include "../include/gui/WidgetContainer.hpp" #include "../include/gui/Button.hpp" -#include "../include/window_texture.h" +#include "../include/gui/ComboBox.hpp" #include "../include/Process.hpp" #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <time.h> #include <errno.h> #include <libgen.h> #include <signal.h> @@ -12,6 +15,7 @@ #include <X11/Xlib.h> #include <X11/cursorfont.h> +#include <X11/Xatom.h> #include <mglpp/mglpp.hpp> #include <mglpp/graphics/Font.hpp> @@ -22,6 +26,7 @@ #include <mglpp/window/Window.hpp> #include <mglpp/window/Event.hpp> #include <mglpp/system/MemoryMappedFile.hpp> +#include <mglpp/system/Clock.hpp> #include <vector> @@ -29,17 +34,8 @@ extern "C" { #include <mgl/mgl.h> } -struct Config { - float scale = 1.0f; -}; - -static Config& get_config() { - static Config config; - return config; -} - static void usage() { - fprintf(stderr, "usage: window-overlay <window>\n"); + fprintf(stderr, "usage: window-overlay\n"); exit(1); } @@ -48,42 +44,11 @@ static void startup_error(const char *msg) { exit(1); } -static bool string_to_i64(const char *str, int64_t *result) { - errno = 0; - char *endptr = nullptr; - int64_t res = strtoll(str, &endptr, 0); - if(endptr == str || errno != 0) - return false; - - *result = res; - return true; -} - -static void window_texture_get_size_or(WindowTexture *window_texture, int *width, int *height, int fallback_width, int fallback_height) { - Window root_window; - int x, y; - unsigned int w = 0, h = 0; - unsigned int border_width, depth; - if(!XGetGeometry(window_texture->display, window_texture->pixmap, &root_window, &x, &y, &w, &h, &border_width, &depth) || w == 0 || h == 0) { - *width = fallback_width; - *height = fallback_height; - } else { - *width = w; - *height = h; - } -} - #define _NET_WM_STATE_REMOVE 0 #define _NET_WM_STATE_ADD 1 #define _NET_WM_STATE_TOGGLE 2 -static Bool make_window_always_on_top(Display* display, Window window) { - Atom net_wm_state_above_atom = XInternAtom(display, "_NET_WM_STATE_ABOVE", True); - if(!net_wm_state_above_atom) { - fprintf(stderr, "Error: failed to find atom _NET_WM_STATE_ABOVE\n"); - return False; - } - +static Bool set_window_wm_state(Display *display, Window window, Atom atom) { Atom net_wm_state_atom = XInternAtom(display, "_NET_WM_STATE", True); if(!net_wm_state_atom) { fprintf(stderr, "Error: failed to find atom _NET_WM_STATE\n"); @@ -98,7 +63,7 @@ static Bool make_window_always_on_top(Display* display, Window window) { xclient.message_type = net_wm_state_atom; xclient.format = 32; xclient.data.l[0] = _NET_WM_STATE_ADD; - xclient.data.l[1] = net_wm_state_above_atom; + xclient.data.l[1] = atom; xclient.data.l[2] = 0; xclient.data.l[3] = 0; xclient.data.l[4] = 0; @@ -108,8 +73,28 @@ static Bool make_window_always_on_top(Display* display, Window window) { return True; } +static Bool make_window_always_on_top(Display* display, Window window) { + Atom net_wm_state_above_atom = XInternAtom(display, "_NET_WM_STATE_ABOVE", True); + if(!net_wm_state_above_atom) { + fprintf(stderr, "Error: failed to find atom _NET_WM_STATE_ABOVE\n"); + return False; + } + + return set_window_wm_state(display, window, net_wm_state_above_atom); +} + +static Bool make_window_sticky(Display* display, Window window) { + Atom net_wm_state_sticky_atom = XInternAtom(display, "_NET_WM_STATE_STICKY", True); + if(!net_wm_state_sticky_atom) { + fprintf(stderr, "Error: failed to find atom _NET_WM_STATE_STICKY\n"); + return False; + } + + return set_window_wm_state(display, window, net_wm_state_sticky_atom); +} + int main(int argc, char **argv) { - if(argc != 2) + if(argc != 1) usage(); std::string program_root_dir = dirname(argv[0]); @@ -117,36 +102,33 @@ int main(int argc, char **argv) { program_root_dir += '/'; program_root_dir += "../../../"; - int64_t target_window; - if(!string_to_i64(argv[1], &target_window)) { - fprintf(stderr, "Error: invalid number '%s' was specific for window argument\n", argv[1]); - return 1; - } - mgl::Init init; Display *display = (Display*)mgl_get_context()->connection; - XWindowAttributes target_win_attr; - if(!XGetWindowAttributes(display, target_window, &target_win_attr)) { - fprintf(stderr, "Error: window argument %s is not a valid window\n", argv[1]); - return 1; - } + // TODO: Put window on the focused monitor right side and update when monitor changes resolution or other modes. + // Use monitor size instead of screen size - XSelectInput(display, target_window, StructureNotifyMask | VisibilityChangeMask); + mgl::vec2i target_monitor_pos = { 0, 0 }; + mgl::vec2i target_monitor_size = { WidthOfScreen(DefaultScreenOfDisplay(display)), HeightOfScreen(DefaultScreenOfDisplay(display)) }; - mgl::vec2i target_window_size = { target_win_attr.width, target_win_attr.height }; - get_config().scale = std::min(1.0, (double)target_win_attr.width / 1920.0); + const mgl::vec2i window_size = { target_monitor_size.x, target_monitor_size.y }; + const mgl::vec2i window_target_pos = { target_monitor_pos.x, target_monitor_pos.y }; + const mgl::vec2i window_start_pos = { window_target_pos.x, window_target_pos.y }; mgl::Window::CreateParams window_create_params; - window_create_params.position = { target_win_attr.x, target_win_attr.y }; - window_create_params.size = target_window_size; + window_create_params.size = window_size; + window_create_params.position = window_start_pos; window_create_params.hidden = true; window_create_params.override_redirect = true; mgl::Window window; - if(!window.create("mglpp", window_create_params)) + if(!window.create("gsr overlay", window_create_params)) startup_error("failed to create window"); + Atom type = XInternAtom(display, "_NET_WM_WINDOW_TYPE", False); + Atom value = XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False); + XChangeProperty(display, DefaultRootWindow(display), type, XA_ATOM, 32, PropModeReplace, reinterpret_cast<unsigned char*>(&value), 1); + mgl::MemoryMappedFile title_font_file; if(!title_font_file.load((program_root_dir + "fonts/Orbitron-Bold.ttf").c_str(), mgl::MemoryMappedFile::LoadOptions{true, false})) startup_error("failed to load file: fonts/Orbitron-Bold.ttf"); @@ -155,12 +137,16 @@ int main(int argc, char **argv) { if(!font_file.load((program_root_dir + "fonts/Orbitron-Regular.ttf").c_str(), mgl::MemoryMappedFile::LoadOptions{true, false})) startup_error("failed to load file: fonts/Orbitron-Regular.ttf"); + mgl::Font top_bar_font; + if(!top_bar_font.load_from_file(title_font_file, window_create_params.size.y * 0.03f)) + startup_error("failed to load font: fonts/Orbitron-Bold.ttf"); + mgl::Font title_font; - if(!title_font.load_from_file(title_font_file, 32 * get_config().scale)) + if(!title_font.load_from_file(title_font_file, window_create_params.size.y * 0.012f)) startup_error("failed to load font: fonts/Orbitron-Bold.ttf"); mgl::Font font; - if(!font.load_from_file(font_file, 20 * get_config().scale)) + if(!font.load_from_file(font_file, window_create_params.size.y * 0.008f)) startup_error("failed to load font: fonts/Orbitron-Regular.ttf"); mgl::Texture replay_button_texture; @@ -179,7 +165,7 @@ int main(int argc, char **argv) { mgl::Text title; mgl::Text description; mgl::Sprite icon; - gsr::Button button; + std::unique_ptr<gsr::Button> button; gsr::GsrMode mode; }; @@ -214,11 +200,14 @@ int main(int argc, char **argv) { title.set_color(mgl::Color(255, 255, 255)); mgl::Text description(descriptions_off[i], {0.0f, 0.0f}, font); - description.set_color(mgl::Color(255, 255, 255, 150)); + description.set_color(mgl::Color(150, 150, 150)); + + const int button_height = window_create_params.size.y * 0.125f; + const int button_width = button_height * 1.125f; mgl::Sprite sprite(textures[i]); - sprite.set_scale(get_config().scale); - gsr::Button button(mgl::vec2f(425 * get_config().scale, 300 * get_config().scale).floor()); + sprite.set_scale(window_create_params.size.y / 2500.0f); + auto button = std::make_unique<gsr::Button>(mgl::vec2f(button_width, button_height)); MainButton main_button = { std::move(title), @@ -232,7 +221,7 @@ int main(int argc, char **argv) { } // Replay - main_buttons[0].button.on_click = [&]() { + main_buttons[0].button->on_click = [&]() { /* char window_to_record_str[32]; snprintf(window_to_record_str, sizeof(window_to_record_str), "%ld", target_window); @@ -252,7 +241,8 @@ int main(int argc, char **argv) { // TODO: Monitor /tmp/gpu-screen-recorder and update ui to match state // Record - main_buttons[1].button.on_click = [&]() { + main_buttons[1].button->on_click = [&]() { + #if 0 int gpu_screen_recorder_process = -1; gsr::GsrMode gsr_mode = gsr::GsrMode::Unknown; if(gsr::is_gpu_screen_recorder_running(gpu_screen_recorder_process, gsr_mode) && gpu_screen_recorder_process > 0) { @@ -277,20 +267,21 @@ int main(int argc, char **argv) { }; gsr::exec_program_daemonized(args); exit(0); + #endif }; main_buttons[1].mode = gsr::GsrMode::Record; main_buttons[2].mode = gsr::GsrMode::Stream; auto update_overlay_shape = [&]() { - const int main_button_margin = 20 * get_config().scale; - const int spacing = 10 * get_config().scale; + const int main_button_margin = 20;// * get_config().scale; + const int spacing = 0;// * get_config().scale; const int combined_spacing = spacing * std::max(0, (int)main_buttons.size() - 1); - const int per_button_width = 425 * get_config().scale; - const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size() + combined_spacing, 300 * get_config().scale); + const int per_button_width = main_buttons[0].button->get_size().x;// * get_config().scale; + const mgl::vec2i overlay_desired_size(per_button_width * (int)main_buttons.size() + combined_spacing, main_buttons[0].button->get_size().y); - const mgl::vec2i main_buttons_start_pos = target_window_size/2 - overlay_desired_size/2; + const mgl::vec2i main_buttons_start_pos = mgl::vec2i(window_create_params.size.x*0.5f, window_create_params.size.y*0.25f) - overlay_desired_size/2; mgl::vec2i main_button_pos = main_buttons_start_pos; int gpu_screen_recorder_process = -1; @@ -323,36 +314,16 @@ int main(int argc, char **argv) { main_button_pos.x + per_button_width * 0.5f - main_buttons[i].icon.get_texture()->get_size().x * main_buttons[i].icon.get_scale().x * 0.5f, main_button_pos.y + overlay_desired_size.y * 0.5f - main_buttons[i].icon.get_texture()->get_size().y * main_buttons[i].icon.get_scale().y * 0.5f).floor()); - main_buttons[i].button.set_position(main_button_pos.to_vec2f()); + main_buttons[i].button->set_position(main_button_pos.to_vec2f()); main_button_pos.x += per_button_width + combined_spacing; } }; update_overlay_shape(); - WindowTexture target_window_texture; - if(window_texture_init(&target_window_texture, display, target_window) != 0) { - fprintf(stderr, "Error: failed to capture window\n"); - return 1; - } - - int target_window_texture_width = 0; - int target_window_texture_height = 0; - window_texture_get_size_or(&target_window_texture, &target_window_texture_width, &target_window_texture_height, target_window_size.x, target_window_size.y); - - mgl_texture window_texture_ref = { - window_texture_get_opengl_texture_id(&target_window_texture), - target_window_texture_width, - target_window_texture_height, - MGL_TEXTURE_FORMAT_RGB, - 32768, - 32768, - true - }; - - window.set_size({ target_window_texture_width, target_window_texture_height }); window.set_visible(true); make_window_always_on_top(display, window.get_system_handle()); + make_window_sticky(display, window.get_system_handle()); Cursor default_cursor = XCreateFontCursor(display, XC_arrow); @@ -365,62 +336,82 @@ int main(int argc, char **argv) { GrabModeAsync, GrabModeAsync, None, default_cursor, CurrentTime); XGrabKeyboard(display, window.get_system_handle(), True, GrabModeAsync, GrabModeAsync, CurrentTime); + XSetInputFocus(display, window.get_system_handle(), RevertToParent, CurrentTime); + XFlush(display); + //XGrabServer(display); - mgl::Texture window_texture = mgl::Texture::reference(window_texture_ref); - mgl::Sprite window_texture_sprite(&window_texture); + mgl::Rectangle top_bar_background(mgl::vec2f(window.get_size().x, window.get_size().y*0.05f).floor()); + top_bar_background.set_color(mgl::Color(0, 0, 0, 250)); - XEvent xev; - mgl::Event event; + mgl::Text top_bar_text("GPU Screen Recorder", top_bar_font); + top_bar_text.set_color(mgl::Color(118, 185, 0)); + 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()); - event.type = mgl::Event::MouseMoved; - event.mouse_move.x = window.get_mouse_position().x; - event.mouse_move.y = window.get_mouse_position().y; - for(auto &main_button : main_buttons) { - main_button.button.on_event(event, window); - } + gsr::ComboBox record_area_box(&title_font); + record_area_box.set_position(mgl::vec2f(300.0f, 300.0f)); + record_area_box.add_item("Window", "window"); + record_area_box.add_item("Focused window", "focused"); + record_area_box.add_item("All monitors (NvFBC)", "all"); + record_area_box.add_item("All monitors, direct mode (NvFBC, VRR workaround)", "all-direct"); + record_area_box.add_item("Monitor DP-0 (3840x2160, NvFBC)", "DP-0"); - mgl::Rectangle background_overlay(mgl::vec2f(target_window_texture_width, target_window_texture_height)); - background_overlay.set_color(mgl::Color(0, 0, 0, 150)); + mgl::Text record_area_title("Record area", title_font); + record_area_title.set_position(mgl::vec2f(record_area_box.get_position().x, record_area_box.get_position().y - title_font.get_character_size() - 10.0f)); - while(window.is_open()) { - if(XCheckTypedWindowEvent(display, target_window, DestroyNotify, &xev)) { - window.close(); - break; - } + gsr::ComboBox audio_input_box(&title_font); + audio_input_box.set_position(mgl::vec2f(record_area_box.get_position().x, record_area_box.get_position().y + record_area_box.get_size().y + title_font.get_character_size()*2.0f + title_font.get_character_size() + 10.0f)); + audio_input_box.add_item("Monitor of Starship/Matissee HD Audio Controller Analog Stereo", "starship.ablalba.monitor"); + audio_input_box.add_item("Monitor of GP104 High Definition Audio Controller Digital Stereo (HDMI 2)", "starship.ablalba.monitor"); - if(XCheckTypedWindowEvent(display, target_window, VisibilityNotify, &xev)) { - if(xev.xvisibility.state) { - window_texture_on_resize(&target_window_texture); - window_texture_ref.id = window_texture_get_opengl_texture_id(&target_window_texture); - window_texture_get_size_or(&target_window_texture, &window_texture_ref.width, &window_texture_ref.height, target_window_size.x, target_window_size.y); - window_texture = mgl::Texture::reference(window_texture_ref); - } - } + mgl::Text audio_input_title("Audio input", title_font); + audio_input_title.set_position(mgl::vec2f(audio_input_box.get_position().x, audio_input_box.get_position().y - title_font.get_character_size() - 10.0f)); - if(XCheckTypedWindowEvent(display, target_window, ConfigureNotify, &xev)) { - while(XCheckTypedWindowEvent(display, target_window, ConfigureNotify, &xev)) {} - if(xev.xconfigure.width != target_window_size.x || xev.xconfigure.height != target_window_size.y) { - target_window_size.x = xev.xconfigure.width; - target_window_size.y = xev.xconfigure.height; - window.set_size(target_window_size); - update_overlay_shape(); + gsr::ComboBox video_quality_box(&title_font); + video_quality_box.set_position(mgl::vec2f(audio_input_box.get_position().x, audio_input_box.get_position().y + audio_input_box.get_size().y + title_font.get_character_size()*2.0f + title_font.get_character_size() + 10.0f)); + video_quality_box.add_item("High", "starship.ablalba.monitor"); + video_quality_box.add_item("Ultra", "starship.ablalba.monitor"); + video_quality_box.add_item("Placebo", "starship.ablalba.monitor"); - window_texture_on_resize(&target_window_texture); - window_texture_ref.id = window_texture_get_opengl_texture_id(&target_window_texture); - window_texture_get_size_or(&target_window_texture, &window_texture_ref.width, &window_texture_ref.height, target_window_size.x, target_window_size.y); - window_texture = mgl::Texture::reference(window_texture_ref); + mgl::Text video_quality_title("Video quality", title_font); + video_quality_title.set_position(mgl::vec2f(video_quality_box.get_position().x, video_quality_box.get_position().y - title_font.get_character_size() - 10.0f)); - background_overlay.set_size(mgl::vec2f(target_window_texture_width, target_window_texture_height)); - } - window.set_position({ xev.xconfigure.x, xev.xconfigure.y }); + gsr::ComboBox framerate_box(&title_font); + framerate_box.set_position(mgl::vec2f(video_quality_box.get_position().x, video_quality_box.get_position().y + video_quality_box.get_size().y + title_font.get_character_size()*2.0f + title_font.get_character_size() + 10.0f)); + framerate_box.add_item("60", "starship.ablalba.monitor"); + + mgl::Text framerate_title("Frame rate", title_font); + framerate_title.set_position(mgl::vec2f(framerate_box.get_position().x, framerate_box.get_position().y - title_font.get_character_size() - 10.0f)); + + gsr::WidgetContainer &widget_container = gsr::WidgetContainer::get_instance(); + + 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; + widget_container.on_event(event, window); + + auto render = [&] { + window.clear(mgl::Color(0, 0, 0, 175)); + window.draw(record_area_title); + window.draw(audio_input_title); + window.draw(video_quality_title); + window.draw(framerate_title); + widget_container.draw(window); + for(auto &main_button : main_buttons) { + window.draw(main_button.icon); + window.draw(main_button.title); + window.draw(main_button.description); } + window.draw(top_bar_background); + window.draw(top_bar_text); + window.display(); + }; + while(window.is_open()) { if(window.poll_event(event)) { - for(auto &main_button : main_buttons) { - main_button.button.on_event(event, window); - } - + widget_container.on_event(event, window); if(event.type == mgl::Event::KeyReleased) { if(event.key.code == mgl::Keyboard::Escape) { window.close(); @@ -429,16 +420,7 @@ int main(int argc, char **argv) { } } - window.clear(mgl::Color(37, 43, 47)); - window.draw(window_texture_sprite); - window.draw(background_overlay); - for(auto &main_button : main_buttons) { - main_button.button.draw(window); - window.draw(main_button.icon); - window.draw(main_button.title); - window.draw(main_button.description); - } - window.display(); + render(); } return 0; |