From 497217a3e09b577f650aaf503356588515067ca8 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sun, 25 Sep 2022 17:29:17 +0200 Subject: widgets --- depends/mglpp | 2 +- gpu-screen-recorder-overlay-daemon/main.c | 167 ++++++++++++++++-- include/gui/Button.hpp | 7 +- include/gui/ComboBox.hpp | 36 ++++ include/gui/Widget.hpp | 12 +- include/gui/WidgetContainer.hpp | 30 ++++ include/window_texture.h | 38 ---- src/Process.cpp | 4 +- src/gui/Button.cpp | 7 +- src/gui/ComboBox.cpp | 161 +++++++++++++++++ src/gui/Widget.cpp | 13 ++ src/gui/WidgetContainer.cpp | 56 ++++++ src/main.cpp | 282 ++++++++++++++---------------- src/window_texture.c | 185 -------------------- 14 files changed, 608 insertions(+), 392 deletions(-) create mode 100644 include/gui/ComboBox.hpp create mode 100644 include/gui/WidgetContainer.hpp delete mode 100644 include/window_texture.h create mode 100644 src/gui/ComboBox.cpp create mode 100644 src/gui/WidgetContainer.cpp delete mode 100644 src/window_texture.c diff --git a/depends/mglpp b/depends/mglpp index 4e43f6e..bab6160 160000 --- a/depends/mglpp +++ b/depends/mglpp @@ -1 +1 @@ -Subproject commit 4e43f6ea03c1fe4c0cc92285ba5869e6b1815881 +Subproject commit bab61609dc8af11a3395f7b3a784fe3480bd52d0 diff --git a/gpu-screen-recorder-overlay-daemon/main.c b/gpu-screen-recorder-overlay-daemon/main.c index 7db83bd..76e1980 100644 --- a/gpu-screen-recorder-overlay-daemon/main.c +++ b/gpu-screen-recorder-overlay-daemon/main.c @@ -1,8 +1,32 @@ #include -#include #include +#include +#include #include #include +#include + +bool exec_program(const char **args, pid_t *process_id) { + *process_id = -1; + + /* 1 argument */ + if(args[0] == NULL) + return false; + + pid_t pid = vfork(); + if(pid == -1) { + perror("Failed to vfork"); + return false; + } else if(pid == 0) { /* child */ + execvp(args[0], (char* const*)args); + perror("execvp"); + _exit(127); + } else { /* parent */ + *process_id = pid; + } + + return true; +} static int ignore_xerror(Display *display, XErrorEvent *ee) { (void)display; @@ -39,12 +63,118 @@ static void grab_keys(Display *display) { XSetErrorHandler(prev_error_handler); } +static bool window_has_atom(Display *display, Window window, Atom atom) { + Atom type; + unsigned long len, bytes_left; + int format; + unsigned char *properties = NULL; + if(XGetWindowProperty(display, window, atom, 0, 1024, False, AnyPropertyType, &type, &format, &len, &bytes_left, &properties) < Success) + return false; + + if(properties) + XFree(properties); + + return type != None; +} + +static Window window_get_target_window_child(Display *display, Window window) { + if(window == None) + return None; + + Atom wm_state_atom = XInternAtom(display, "_NET_WM_STATE", False); + if(!wm_state_atom) + return None; + + if(window_has_atom(display, window, wm_state_atom)) + return window; + + Window root; + Window parent; + Window *children = NULL; + unsigned int num_children = 0; + if(!XQueryTree(display, window, &root, &parent, &children, &num_children) || !children) + return None; + + Window found_window = None; + for(int i = num_children - 1; i >= 0; --i) { + if(children[i] && window_has_atom(display, children[i], wm_state_atom)) { + found_window = children[i]; + goto finished; + } + } + + for(int i = num_children - 1; i >= 0; --i) { + if(children[i]) { + Window win = window_get_target_window_child(display, children[i]); + if(win) { + found_window = win; + goto finished; + } + } + } + + finished: + XFree(children); + return found_window; +} + +static Window window_get_target_window_parent(Display *display, Window window) { + fprintf(stderr, "window: %ld\n", window); + + if(window == None || window == DefaultRootWindow(display)) + return None; + + Atom wm_state_atom = XInternAtom(display, "WM_STATE", False); + if(!wm_state_atom) + return None; + + if(window_has_atom(display, window, wm_state_atom)) + return window; + + Window root; + Window parent = None; + Window *children = NULL; + unsigned int num_children = 0; + if(!XQueryTree(display, window, &root, &parent, &children, &num_children)) + return None; + + if(children) + XFree(children); + + if(!parent) + return None; + + return window_get_target_window_parent(display, parent); +} + +static Window get_input_focus(Display *display) { + Window focused_window = None; + + Atom net_active_window_atom = XInternAtom(display, "_NET_ACTIVE_WINDOW", False); + Atom type; + unsigned long len, bytes_left; + int format; + unsigned char *properties = NULL; + if(XGetWindowProperty(display, DefaultRootWindow(display), net_active_window_atom, 0, 1024, False, XA_WINDOW, &type, &format, &len, &bytes_left, &properties) == Success) { + if(properties) { + if(len > 0) + focused_window = *(Window*)properties; + XFree(properties); + } + } + + if(!focused_window) { + int rev; + if(!XGetInputFocus(display, &focused_window, &rev)) + focused_window = None; + } + + return focused_window; +} + static Window get_window_with_input_focus(Display *display) { - Window window = None; - int rev; - if(!XGetInputFocus(display, &window, &rev)) - window = None; - return window; + Window window = get_input_focus(display); + return window_get_target_window_parent(display, window); } int main() { @@ -61,15 +191,30 @@ int main() { /* Killing gpu-screen-recorder with SIGTERM also gives us SIGTERM. We want to ignore that as that has no meaning here */ signal(SIGTERM, sigterm_handler); + pid_t overlay_pid = -1; + XEvent xev; for(;;) { XNextEvent(display, &xev); - if(xev.type == KeyRelease && xev.xkey.keycode == overlay_keycode) { + if(xev.type == KeyPress && xev.xkey.keycode == overlay_keycode) { + if(overlay_pid != -1) { + int status; + if(waitpid(overlay_pid, &status, WNOHANG) == 0) + continue; // GPU Screen Recorder overlay is still running + + overlay_pid = -1; + } + Window window_with_input_focus = get_window_with_input_focus(display); - if(window_with_input_focus && window_with_input_focus != DefaultRootWindow(display)) { - char cmd[1024]; - snprintf(cmd, sizeof(cmd), "/home/dec05eba/git/gpu-screen-recorder-overlay/sibs-build/linux_x86_64/debug/gpu-screen-recorder-overlay %ld", window_with_input_focus); - system(cmd); + fprintf(stderr, "window with focus: %ld\n", window_with_input_focus); + if(window_with_input_focus && window_with_input_focus != DefaultRootWindow(display) && overlay_pid == -1) { + fprintf(stderr, "launch overlay\n"); + // TODO: window_with_input_focus + const char *args[] = { + "/home/dec05eba/git/gpu-screen-recorder-overlay/sibs-build/linux_x86_64/debug/gpu-screen-recorder-overlay", + NULL + }; + exec_program(args, &overlay_pid); } } } diff --git a/include/gui/Button.hpp b/include/gui/Button.hpp index 84098b2..972e5c9 100644 --- a/include/gui/Button.hpp +++ b/include/gui/Button.hpp @@ -8,9 +8,14 @@ namespace gsr { class Button : public Widget { public: Button(mgl::vec2f size); - void on_event(mgl::Event &event, mgl::Window &window) override; + Button(const Button&) = delete; + Button& operator=(const Button&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window) override; void draw(mgl::Window &window) override; + mgl::vec2f get_size() const { return size; } + std::function on_click; private: mgl::vec2f size; diff --git a/include/gui/ComboBox.hpp b/include/gui/ComboBox.hpp new file mode 100644 index 0000000..5c899a7 --- /dev/null +++ b/include/gui/ComboBox.hpp @@ -0,0 +1,36 @@ +#pragma once + +#include "Widget.hpp" +#include +#include +#include + +namespace gsr { + class ComboBox : public Widget { + public: + ComboBox(mgl::Font *font); + ComboBox(const ComboBox&) = delete; + ComboBox& operator=(const ComboBox&) = delete; + + bool on_event(mgl::Event &event, mgl::Window &window) override; + void draw(mgl::Window &window) override; + + void add_item(const std::string &text, const std::string &id); + + mgl::vec2f get_size(); + private: + void update_if_dirty(); + private: + struct Item { + mgl::Text text; + std::string id; + }; + + mgl::vec2f max_size; + mgl::Font *font; + std::vector items; + bool dirty = true; + bool show_dropdown = false; + size_t selected_item = 0; + }; +} \ No newline at end of file diff --git a/include/gui/Widget.hpp b/include/gui/Widget.hpp index cf81d69..81f47b5 100644 --- a/include/gui/Widget.hpp +++ b/include/gui/Widget.hpp @@ -9,13 +9,21 @@ namespace mgl { namespace gsr { class Widget { + friend class WidgetContainer; public: - virtual ~Widget() = default; + Widget(); + Widget(const Widget&) = delete; + Widget& operator=(const Widget&) = delete; + virtual ~Widget(); - virtual void on_event(mgl::Event &event, mgl::Window &window) = 0; + // Return true to allow other widgets to also process the event + virtual bool on_event(mgl::Event &event, mgl::Window &window) = 0; virtual void draw(mgl::Window &window) = 0; virtual void set_position(mgl::vec2f position); + + virtual mgl::vec2f get_position() const; protected: mgl::vec2f position; + bool move_to_top = false; }; } \ No newline at end of file diff --git a/include/gui/WidgetContainer.hpp b/include/gui/WidgetContainer.hpp new file mode 100644 index 0000000..e40ffb9 --- /dev/null +++ b/include/gui/WidgetContainer.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +namespace mgl { + class Event; + class Window; +} + +namespace gsr { + class Widget; + + class WidgetContainer { + public: + static WidgetContainer& get_instance(); + + void add_widget(Widget *widget); + void remove_widget(Widget *widget); + + void on_event(mgl::Event &event, mgl::Window &window); + void draw(mgl::Window &window); + private: + WidgetContainer() = default; + WidgetContainer& operator=(const WidgetContainer&) = delete; + WidgetContainer(const WidgetContainer&) = delete; + private: + std::vector widgets; + }; +} \ No newline at end of file diff --git a/include/window_texture.h b/include/window_texture.h deleted file mode 100644 index ea3ff10..0000000 --- a/include/window_texture.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef WINDOW_TEXTURE_H -#define WINDOW_TEXTURE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#define GLX_GLXEXT_PROTOTYPES -#include -#include -#include - -typedef struct { - Display *display; - Window window; - Pixmap pixmap; - GLXPixmap glx_pixmap; - GLuint texture_id; - int redirected; -} WindowTexture; - -/* Returns 0 on success */ -int window_texture_init(WindowTexture *window_texture, Display *display, Window window); -void window_texture_deinit(WindowTexture *self); - -/* - This should ONLY be called when the target window is resized. - Returns 0 on success. -*/ -int window_texture_on_resize(WindowTexture *self); - -GLuint window_texture_get_opengl_texture_id(WindowTexture *self); - -#ifdef __cplusplus -} -#endif - -#endif /* WINDOW_TEXTURE_H */ diff --git a/src/Process.cpp b/src/Process.cpp index 96f6840..c00d547 100644 --- a/src/Process.cpp +++ b/src/Process.cpp @@ -11,7 +11,7 @@ namespace gsr { bool exec_program_daemonized(const char **args) { /* 1 argument */ if(args[0] == nullptr) - return -1; + return false; pid_t pid = vfork(); if(pid == -1) { @@ -28,11 +28,13 @@ namespace gsr { perror("execvp"); _exit(127); } else if(second_child != -1) { + // TODO: _exit(0); } } else { /* parent */ waitpid(pid, nullptr, 0); } + return true; } diff --git a/src/gui/Button.cpp b/src/gui/Button.cpp index 1436f86..5a71a16 100644 --- a/src/gui/Button.cpp +++ b/src/gui/Button.cpp @@ -9,7 +9,7 @@ namespace gsr { } - void Button::on_event(mgl::Event &event, mgl::Window&) { + bool Button::on_event(mgl::Event &event, mgl::Window&) { if(event.type == mgl::Event::MouseMoved) { const bool inside = mgl::FloatRect(position, size).contains({ (float)event.mouse_move.x, (float)event.mouse_move.y }); if(mouse_inside && !inside) { @@ -25,6 +25,7 @@ namespace gsr { if(clicked_inside && on_click) on_click(); } + return true; } void Button::draw(mgl::Window &window) { @@ -33,7 +34,7 @@ namespace gsr { { mgl::Rectangle rect(size); rect.set_position(position); - rect.set_color(mgl::Color(20, 20, 20, 255)); + rect.set_color(mgl::Color(0, 0, 0, 255)); window.draw(rect); } @@ -75,7 +76,7 @@ namespace gsr { // Background mgl::Rectangle rect(size); rect.set_position(position); - rect.set_color(mgl::Color(20, 20, 20, 225)); + rect.set_color(mgl::Color(0, 0, 0, 250)); window.draw(rect); } } diff --git a/src/gui/ComboBox.cpp b/src/gui/ComboBox.cpp new file mode 100644 index 0000000..b96b3ae --- /dev/null +++ b/src/gui/ComboBox.cpp @@ -0,0 +1,161 @@ +#include "../../include/gui/ComboBox.hpp" +#include +#include +#include +#include +#include + +namespace gsr { + static const float padding_top = 10.0f; + static const float padding_bottom = 10.0f; + static const float padding_left = 10.0f; + static const float padding_right = 10.0f; + + ComboBox::ComboBox(mgl::Font *font) : font(font) { + assert(font); + } + + bool ComboBox::on_event(mgl::Event &event, mgl::Window&) { + if(event.type == mgl::Event::MouseButtonPressed && event.mouse_button.button == mgl::Mouse::Left) { + const mgl::vec2f mouse_pos = { (float)event.mouse_button.x, (float)event.mouse_button.y }; + const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom); + + if(show_dropdown && !items.empty()) { + mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top); + pos.y += items[selected_item].text.get_bounds().size.y + padding_top + padding_bottom; + + for(size_t i = 0; i < items.size(); ++i) { + Item &item = items[i]; + const mgl::FloatRect text_bounds = item.text.get_bounds(); + if(mgl::FloatRect(pos - mgl::vec2f(padding_left, padding_top), item_size).contains(mouse_pos)) { + selected_item = i; + show_dropdown = false; + return false; + } + pos.y += text_bounds.size.y + padding_top + padding_bottom; + } + } + + if(mgl::FloatRect(position, item_size).contains(mouse_pos)) { + show_dropdown = !show_dropdown; + move_to_top = true; + } else { + show_dropdown = false; + } + } + return true; + } + + static void draw_rectangle_outline(mgl::Window &window, mgl::vec2f pos, mgl::vec2f size, mgl::Color color, float border_size) { + // Green line at top + { + mgl::Rectangle rect({ size.x, border_size }); + rect.set_position(pos); + rect.set_color(color); + window.draw(rect); + } + + // Green line at bottom + { + mgl::Rectangle rect({ size.x, border_size }); + rect.set_position(pos + mgl::vec2f(0.0f, size.y - border_size)); + rect.set_color(color); + window.draw(rect); + } + + // Green line at left + { + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(pos + mgl::vec2f(0, border_size)); + rect.set_color(color); + window.draw(rect); + } + + // Green line at right + { + mgl::Rectangle rect({ border_size, size.y - border_size * 2 }); + rect.set_position(pos + mgl::vec2f(size.x - border_size, border_size)); + rect.set_color(color); + window.draw(rect); + } + } + + void ComboBox::draw(mgl::Window &window) { + update_if_dirty(); + + if(items.empty()) + return; + + const mgl::vec2f item_size(max_size.x, font->get_character_size() + padding_top + padding_bottom); + const mgl::vec2i mouse_pos = window.get_mouse_position(); + bool inside = false; + + mgl::Rectangle background(position, mgl::vec2f(max_size.x, item_size.y)); + if(show_dropdown) { + background.set_size(max_size); + background.set_color(mgl::Color(0, 0, 0)); + } else { + background.set_color(mgl::Color(0, 0, 0, 250)); + } + window.draw(background); + + mgl::vec2f pos = position + mgl::vec2f(padding_left, padding_top); + + Item &item = items[selected_item]; + item.text.set_position(pos); + if(show_dropdown) { + const int border_size = 3; + const mgl::Color border_color(118, 185, 0); + draw_rectangle_outline(window, pos - mgl::vec2f(padding_left, padding_top), item_size, border_color, border_size); + } + window.draw(item.text); + pos.y += item.text.get_bounds().size.y + padding_top + padding_bottom; + + for(size_t i = 0; i < items.size(); ++i) { + Item &item = items[i]; + item.text.set_position(pos); + const mgl::FloatRect text_bounds = item.text.get_bounds(); + + if(show_dropdown) { + if(!inside) { + inside = mgl::FloatRect(text_bounds.position - mgl::vec2f(padding_left, padding_top), item_size).contains({ (float)mouse_pos.x, (float)mouse_pos.y }); + if(inside) { + mgl::Rectangle item_background(text_bounds.position - mgl::vec2f(padding_left, padding_top), item_size); + item_background.set_color(mgl::Color(118, 185, 0)); + window.draw(item_background); + } else { + /*const int border_size = 3; + const mgl::Color border_color(150, 150, 150); + draw_rectangle_outline(window, text_bounds.position, item_size, border_color, border_size);*/ + } + } + window.draw(item.text); + } + + pos.y += text_bounds.size.y + padding_top + padding_bottom; + } + } + + void ComboBox::add_item(const std::string &text, const std::string &id) { + items.push_back({mgl::Text(text, *font), id}); + dirty = true; + } + + void ComboBox::update_if_dirty() { + if(!dirty) + return; + + max_size = { 0.0f, font->get_character_size() + padding_top + padding_bottom }; + for(Item &item : items) { + const mgl::vec2f bounds = item.text.get_bounds().size; + max_size.x = std::max(max_size.x, bounds.x + padding_left + padding_right); + max_size.y += bounds.y + padding_top + padding_bottom; + } + dirty = false; + } + + mgl::vec2f ComboBox::get_size() { + update_if_dirty(); + return { max_size.x, font->get_character_size() + padding_top + padding_bottom }; + } +} \ No newline at end of file diff --git a/src/gui/Widget.cpp b/src/gui/Widget.cpp index c30c1d1..718fd8d 100644 --- a/src/gui/Widget.cpp +++ b/src/gui/Widget.cpp @@ -1,7 +1,20 @@ #include "../../include/gui/Widget.hpp" +#include "../../include/gui/WidgetContainer.hpp" namespace gsr { + Widget::Widget() { + WidgetContainer::get_instance().add_widget(this); + } + + Widget::~Widget() { + WidgetContainer::get_instance().remove_widget(this); + } + void Widget::set_position(mgl::vec2f position) { this->position = position; } + + mgl::vec2f Widget::get_position() const { + return position; + } } \ No newline at end of file diff --git a/src/gui/WidgetContainer.cpp b/src/gui/WidgetContainer.cpp new file mode 100644 index 0000000..ab97b5b --- /dev/null +++ b/src/gui/WidgetContainer.cpp @@ -0,0 +1,56 @@ +#include "../../include/gui/WidgetContainer.hpp" +#include "../../include/gui/Widget.hpp" + +namespace gsr { + // static + WidgetContainer& WidgetContainer::get_instance() { + static WidgetContainer instance; + return instance; + } + + void WidgetContainer::add_widget(Widget *widget) { + // TODO: to_be_added, and remove in the draw loop + #ifdef DEBUG + for(Widget *existing_widget : widgets) { + if(existing_widget == widget) + return; + } + #endif + widgets.push_back(widget); + } + + void WidgetContainer::remove_widget(Widget *widget) { + // TODO: to_be_removed, and remove in draw loop + for(auto it = widgets.begin(), end = widgets.end(); it != end; ++it) { + if(*it == widget) { + widgets.erase(it); + return; + } + } + } + + void WidgetContainer::on_event(mgl::Event &event, mgl::Window &window) { + // Process widgets by visibility (backwards) + for(auto it = widgets.rbegin(), end = widgets.rend(); it != end; ++it) { + if(!(*it)->on_event(event, window)) + return; + } + } + + void WidgetContainer::draw(mgl::Window &window) { + for(auto it = widgets.begin(); it != widgets.end(); ++it) { + Widget *widget = *it; + if(widget->move_to_top) { + widget->move_to_top = false; + if(widgets.back() != widget) { + widgets.erase(it); + widgets.push_back(widget); + } + } + } + + for(Widget *widget : widgets) { + widget->draw(window); + } + } +} \ No newline at end of file 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 #include #include +#include #include #include #include @@ -12,6 +15,7 @@ #include #include +#include #include #include @@ -22,6 +26,7 @@ #include #include #include +#include #include @@ -29,17 +34,8 @@ extern "C" { #include } -struct Config { - float scale = 1.0f; -}; - -static Config& get_config() { - static Config config; - return config; -} - static void usage() { - fprintf(stderr, "usage: window-overlay \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(&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 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(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; diff --git a/src/window_texture.c b/src/window_texture.c deleted file mode 100644 index bf687c6..0000000 --- a/src/window_texture.c +++ /dev/null @@ -1,185 +0,0 @@ -#include "../include/window_texture.h" -#include -#include -#include - -static int x11_supports_composite_named_window_pixmap(Display *display) { - int extension_major; - int extension_minor; - if(!XCompositeQueryExtension(display, &extension_major, &extension_minor)) - return 0; - - int major_version; - int minor_version; - return XCompositeQueryVersion(display, &major_version, &minor_version) && (major_version > 0 || minor_version >= 2); -} - -int window_texture_init(WindowTexture *window_texture, Display *display, Window window) { - window_texture->display = display; - window_texture->window = window; - window_texture->pixmap = None; - window_texture->glx_pixmap = None; - window_texture->texture_id = 0; - window_texture->redirected = 0; - - if(!x11_supports_composite_named_window_pixmap(display)) - return 1; - - XCompositeRedirectWindow(display, window, CompositeRedirectAutomatic); - window_texture->redirected = 1; - return window_texture_on_resize(window_texture); -} - -static void window_texture_cleanup(WindowTexture *self, int delete_texture) { - mgl_context *context = mgl_get_context(); - - if(delete_texture && self->texture_id) { - context->gl.glDeleteTextures(1, &self->texture_id); - self->texture_id = 0; - } - - if(self->glx_pixmap) { - glXDestroyPixmap(self->display, self->glx_pixmap); - glXReleaseTexImageEXT(self->display, self->glx_pixmap, GLX_FRONT_EXT); - self->glx_pixmap = None; - } - - if(self->pixmap) { - XFreePixmap(self->display, self->pixmap); - self->pixmap = None; - } -} - -void window_texture_deinit(WindowTexture *self) { - if(self->redirected) { - XCompositeUnredirectWindow(self->display, self->window, CompositeRedirectAutomatic); - self->redirected = 0; - } - window_texture_cleanup(self, 1); -} - -int window_texture_on_resize(WindowTexture *self) { - mgl_context *context = mgl_get_context(); - window_texture_cleanup(self, 0); - - int result = 0; - GLXFBConfig *configs = NULL; - Pixmap pixmap = None; - GLXPixmap glx_pixmap = None; - GLuint texture_id = 0; - int glx_pixmap_bound = 0; - - const int pixmap_config[] = { - GLX_BIND_TO_TEXTURE_RGB_EXT, True, - GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT | GLX_WINDOW_BIT, - GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT, - /*GLX_BIND_TO_MIPMAP_TEXTURE_EXT, True,*/ - GLX_BUFFER_SIZE, 24, - GLX_RED_SIZE, 8, - GLX_GREEN_SIZE, 8, - GLX_BLUE_SIZE, 8, - GLX_ALPHA_SIZE, 0, - None - }; - - const int pixmap_attribs[] = { - GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT, - GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT, - /*GLX_MIPMAP_TEXTURE_EXT, True,*/ - None - }; - - XWindowAttributes attr; - if (!XGetWindowAttributes(self->display, self->window, &attr)) { - fprintf(stderr, "Failed to get window attributes\n"); - return 1; - } - - GLXFBConfig config; - int c; - configs = glXChooseFBConfig(self->display, 0, pixmap_config, &c); - if(!configs) { - fprintf(stderr, "Failed to choose fb config\n"); - return 1; - } - - int found = 0; - for (int i = 0; i < c; i++) { - config = configs[i]; - XVisualInfo *visual = glXGetVisualFromFBConfig(self->display, config); - if (!visual) - continue; - - if (attr.depth != visual->depth) { - XFree(visual); - continue; - } - XFree(visual); - found = 1; - break; - } - - if(!found) { - fprintf(stderr, "No matching fb config found\n"); - result = 1; - goto cleanup; - } - - pixmap = XCompositeNameWindowPixmap(self->display, self->window); - if(!pixmap) { - result = 2; - goto cleanup; - } - - glx_pixmap = glXCreatePixmap(self->display, config, pixmap, pixmap_attribs); - if(!glx_pixmap) { - result = 3; - goto cleanup; - } - - if(self->texture_id == 0) { - context->gl.glGenTextures(1, &texture_id); - if(texture_id == 0) { - result = 4; - goto cleanup; - } - context->gl.glBindTexture(GL_TEXTURE_2D, texture_id); - } else { - context->gl.glBindTexture(GL_TEXTURE_2D, self->texture_id); - } - - glXBindTexImageEXT(self->display, glx_pixmap, GLX_FRONT_EXT, NULL); - glx_pixmap_bound = 1; - - context->gl.glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); - context->gl.glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); - context->gl.glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); - context->gl.glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); - - /* - float fLargest = 0.0f; - glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &fLargest); - glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, fLargest); - */ - - context->gl.glBindTexture(GL_TEXTURE_2D, 0); - - XFree(configs); - self->pixmap = pixmap; - self->glx_pixmap = glx_pixmap; - if(texture_id != 0) - self->texture_id = texture_id; - return 0; - - cleanup: - if(texture_id != 0) context->gl.glDeleteTextures(1, &texture_id); - if(glx_pixmap) glXDestroyPixmap(self->display, glx_pixmap); - if(glx_pixmap_bound) glXReleaseTexImageEXT(self->display, glx_pixmap, GLX_FRONT_EXT); - if(pixmap) XFreePixmap(self->display, pixmap); - if(configs) XFree(configs); - return result; -} - -GLuint window_texture_get_opengl_texture_id(WindowTexture *self) { - return self->texture_id; -} -- cgit v1.2.3