aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authordec05eba <dec05eba@protonmail.com>2022-09-25 17:29:17 +0200
committerdec05eba <dec05eba@protonmail.com>2022-09-25 17:29:17 +0200
commit497217a3e09b577f650aaf503356588515067ca8 (patch)
tree2fd43b747957510c55d66caf81df30732c39c398 /src
parent663a36df4aaea583e7cd56ad35b26d05f6151746 (diff)
widgets
Diffstat (limited to 'src')
-rw-r--r--src/Process.cpp4
-rw-r--r--src/gui/Button.cpp7
-rw-r--r--src/gui/ComboBox.cpp161
-rw-r--r--src/gui/Widget.cpp13
-rw-r--r--src/gui/WidgetContainer.cpp56
-rw-r--r--src/main.cpp282
-rw-r--r--src/window_texture.c185
7 files changed, 369 insertions, 339 deletions
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 <mglpp/graphics/Rectangle.hpp>
+#include <mglpp/graphics/Font.hpp>
+#include <mglpp/window/Window.hpp>
+#include <mglpp/window/Event.hpp>
+#include <assert.h>
+
+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 <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;
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 <X11/extensions/Xcomposite.h>
-#include <mgl/mgl.h>
-#include <stdio.h>
-
-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;
-}