Simplify gsr-hyprland-helper, some cleanups

This commit is contained in:
dec05eba
2026-01-24 00:55:47 +01:00
parent 9ccb4dd541
commit 9c5688f61b
7 changed files with 102 additions and 279 deletions

View File

@@ -1,4 +1,5 @@
#pragma once
#include <string>
namespace gsr {

View File

@@ -1,4 +1,5 @@
#pragma once
#include <string>
namespace gsr {

View File

@@ -1,30 +1,22 @@
#include "../include/HyprlandWorkaround.hpp"
#include "../include/Process.hpp"
#include <cstddef>
#include <iostream>
#include <sys/types.h>
#include <thread>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <thread>
namespace gsr {
static ActiveHyprlandWindow *active_hyprland_window = nullptr;
static ActiveHyprlandWindow active_hyprland_window;
static bool hyprland_listener_thread_started = false;
static void hyprland_listener_thread() {
const bool inside_flatpak = access("/app/manifest.json", F_OK) == 0;
const std::string hyprland_helper_bin = (
const char *hyprland_helper_bin =
inside_flatpak ?
"flatpak-spawn --host -- /var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/bin/gsr-hyprland-helper"
: "/usr/bin/gsr-hyprland-helper"
);
: "gsr-hyprland-helper";
FILE* pipe = popen(hyprland_helper_bin.c_str(), "r");
FILE* pipe = popen(hyprland_helper_bin, "r");
if (!pipe) {
std::cerr << "Failed to start gsr-hyprland-helper process\n";
return;
@@ -35,8 +27,9 @@ namespace gsr {
char buffer[4096];
const std::string prefix = "Window title changed: ";
std::string line;
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
std::string line(buffer);
line = buffer;
if (!line.empty() && line.back() == '\n') {
line.pop_back();
@@ -44,9 +37,7 @@ namespace gsr {
size_t pos = line.find(prefix);
if (pos != std::string::npos) {
std::string title = line.substr(pos + prefix.length());
active_hyprland_window->title = title;
active_hyprland_window.title = line.substr(pos + prefix.length());
}
}
@@ -54,11 +45,7 @@ namespace gsr {
}
std::string get_current_hyprland_window_title() {
if (active_hyprland_window == nullptr) {
return "Game";
}
return active_hyprland_window->title;
return active_hyprland_window.title;
}
void start_hyprland_listener_thread() {
@@ -66,10 +53,6 @@ namespace gsr {
return;
}
if (active_hyprland_window == nullptr) {
active_hyprland_window = new ActiveHyprlandWindow();
}
hyprland_listener_thread_started = true;
std::thread([&] {

View File

@@ -6,29 +6,16 @@
#include <thread>
#include <cstdlib>
#include <cstring>
#include <array>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
namespace gsr {
static ActiveKwinWindow *active_kwin_window = nullptr;
static ActiveKwinWindow active_kwin_window;
static bool kwin_helper_thread_started = false;
std::string get_current_kwin_window_title() {
if (active_kwin_window == nullptr) {
return "Game";
}
return active_kwin_window->title;
}
void kwin_script_thread() {
const bool inside_flatpak = access("/app/manifest.json", F_OK) == 0;
const std::string kwin_helper_bin = inside_flatpak ? "/app/bin/gsr-kwin-helper" : "/usr/bin/gsr-kwin-helper";
FILE* pipe = popen(kwin_helper_bin.c_str(), "r");
FILE* pipe = popen("gsr-kwin-helper", "r");
if (!pipe) {
std::cerr << "Failed to start gsr-kwin-helper process\n";
return;
@@ -39,8 +26,9 @@ namespace gsr {
char buffer[4096];
const std::string prefix = "Active window title set to: ";
std::string line;
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
std::string line(buffer);
line = buffer;
if (!line.empty() && line.back() == '\n') {
line.pop_back();
@@ -50,26 +38,26 @@ namespace gsr {
if (pos != std::string::npos) {
std::string title = line.substr(pos + prefix.length());
if (title == "gsr ui") {
continue; // ignore the overlay
if (title == "gsr ui" || title == "gsr notify") {
continue; // ignore the overlay and notification
}
active_kwin_window->title = title;
active_kwin_window.title = std::move(title);
}
}
pclose(pipe);
}
std::string get_current_kwin_window_title() {
return active_kwin_window.title;
}
void start_kwin_helper_thread() {
if (kwin_helper_thread_started) {
return;
}
if (active_kwin_window == nullptr) {
active_kwin_window = new ActiveKwinWindow();
}
kwin_helper_thread_started = true;
std::thread([&] {

View File

@@ -555,9 +555,7 @@ namespace gsr {
if (wm_name.find("Hyprland") != std::string::npos) {
start_hyprland_listener_thread();
}
if (wm_name == "KWin") {
} else if (wm_name == "KWin") {
start_kwin_helper_thread();
}
}
@@ -1975,25 +1973,27 @@ namespace gsr {
mgl_context *context = mgl_get_context();
Display *display = (Display*)context->connection;
const std::string video_filename = filepath_get_filename(video_filepath.c_str());
const Window gsr_ui_window = window ? (Window)window->get_system_handle() : None;
std::string focused_window_name = get_window_name_at_cursor_position(display, gsr_ui_window);
const std::string wm_name = get_window_manager_name(display);
const bool is_hyprland = wm_name.find("Hyprland") != std::string::npos;
const bool is_kwin = wm_name == "KWin";
std::string focused_window_name;
if (is_hyprland) {
focused_window_name = get_current_hyprland_window_title();
} else if (is_kwin) {
focused_window_name = get_current_kwin_window_title();
} else {
const Window gsr_ui_window = window ? (Window)window->get_system_handle() : None;
std::string focused_window_name = get_window_name_at_cursor_position(display, gsr_ui_window);
if(focused_window_name.empty())
focused_window_name = get_focused_window_name(display, WindowCaptureType::FOCUSED, false);
if(focused_window_name.empty())
focused_window_name = "Game";
}
if(focused_window_name.empty())
focused_window_name = "Game";
string_replace_characters(focused_window_name.data(), "/\\", ' ');
std::string video_directory = filepath_get_directory(video_filepath.c_str()) + "/" + focused_window_name;

View File

@@ -1,243 +1,95 @@
#include "unistd.h"
#include "string.h"
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/un.h>
struct ActiveHyprlandWindow {
char* window_id;
char* title;
};
struct ActiveHyprlandWindow* active_window;
const char* get_hyprland_socket_path() {
static bool get_hyprland_socket_path(char *path, int path_len) {
const char* xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
const char* instance_sig = getenv("HYPRLAND_INSTANCE_SIGNATURE");
if (!xdg_runtime_dir || !instance_sig) {
fprintf(stderr, "Error: Env vars not set.\n");
exit(1);
fprintf(stderr, "Error: gsr-hypland-helper: environment variables not set\n");
return false;
}
// allocate buffer for the full path
size_t path_len = strlen(xdg_runtime_dir) + strlen(instance_sig) + 32;
char* socket_path = malloc(path_len);
if (!socket_path) {
fprintf(stderr, "Error: Memory allocation failed.\n");
exit(1);
if (snprintf(path, path_len, "%s/hypr/%s/.socket2.sock", xdg_runtime_dir, instance_sig) >= path_len) {
fprintf(stderr, "Error: gsr-hypland-helper: path to hyprland socket (%s/hypr/%s/.socket2.sock) is more than %d characters long\n", xdg_runtime_dir, instance_sig, path_len);
return false;
}
snprintf(socket_path, path_len, "%s/hypr/%s/.socket2.sock", xdg_runtime_dir, instance_sig);
return socket_path;
return true;
}
void print_window_title(void) {
if (strlen(active_window->title) == 0) {
static void print_window_title(const char *window_title) {
if (window_title[0] == 0) {
printf("Window title changed: %s\n", "Desktop");
fflush(stdout);
return;
}
printf("Window title changed: %s\n", active_window->title);
printf("Window title changed: %s\n", window_title);
fflush(stdout);
}
void handle_ipc(void) {
int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
static int handle_ipc(void) {
const int sock_fd = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock_fd == -1) {
perror("socket");
return;
perror("Error: gsr-hyprland-helper: socket");
return 1;
}
active_window = malloc(sizeof(struct ActiveHyprlandWindow));
active_window->window_id = NULL;
active_window->title = NULL;
struct sockaddr_un addr;
memset(&addr, 0, sizeof(addr));
addr.sun_family = AF_UNIX;
const char* socket_path = get_hyprland_socket_path();
strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path) - 1);
if (!get_hyprland_socket_path(addr.sun_path, sizeof(addr.sun_path))) {
return 1;
}
if (connect(sock_fd, (struct sockaddr*)&addr, sizeof(addr)) == -1) {
perror("connect");
perror("Error: gsr-hyprland-helper: connect");
close(sock_fd);
free((void*)socket_path);
return;
return 1;
}
fprintf(stderr, "Info: gsr-hyprland-helper: connected to Hyprland socket: %s\n", addr.sun_path);
printf("Connected to Hyprland socket: %s\n", socket_path);
fflush(stdout);
free((void*)socket_path);
char buffer[4096];
char* incomplete_line = NULL;
size_t incomplete_len = 0;
while (1) {
ssize_t bytes_read = read(sock_fd, buffer, sizeof(buffer));
if (bytes_read == 0) {
fprintf(stderr, "Connection closed by Hyprland\n");
break;
}
if (bytes_read < 0) {
perror("read");
break;
}
size_t total_len = incomplete_len + bytes_read;
char *data = malloc(total_len + 1);
if (!data) {
perror("malloc");
break;
}
if (incomplete_line) {
memcpy(data, incomplete_line, incomplete_len);
fflush(stdout);
free(incomplete_line);
incomplete_line = NULL;
}
memcpy(data + incomplete_len, buffer, bytes_read);
data[total_len] = '\0';
char* line_start = data;
char* newline_pos;
while ((newline_pos = strchr(line_start, '\n')) != NULL) {
*newline_pos = '\0';
char *line = line_start;
char *delimiter = strstr(line, ">>");
if (delimiter != NULL) {
*delimiter = '\0';
char *event_name = line;
char *event_data = delimiter + 2;
if (strcmp(event_name, "activewindowv2") == 0) {
fflush(stdout);
if (active_window->window_id) {
free(active_window->window_id);
}
active_window->window_id = strdup(event_data);
}
if (strcmp(event_name, "activewindow") == 0) {
char *title_delimiter = strchr(event_data, ',');
if (title_delimiter != NULL) {
char *window_title = title_delimiter + 1;
if (strcmp(window_title, "gsr ui") == 0 ||
strcmp(window_title, "gsr notify") == 0) {
line_start = newline_pos + 1;
continue;
}
fflush(stdout);
if (active_window->title) {
free(active_window->title);
}
active_window->title = strdup(window_title);
print_window_title();
}
}
if (strcmp(event_name, "windowtitlev2") == 0) {
char *data_delimiter = strchr(event_data, ',');
if (data_delimiter != NULL) {
*data_delimiter = '\0';
char *win_id = event_data;
char *new_window_title = data_delimiter + 1;
if (strcmp(new_window_title, "gsr ui") == 0 ||
strcmp(new_window_title, "gsr notify") == 0) {
line_start = newline_pos + 1;
continue;
}
if (strcmp(active_window->window_id, win_id) != 0) {
line_start = newline_pos + 1;
continue;
}
fflush(stdout);
if (active_window->title) {
free(active_window->title);
}
active_window->title = strdup(new_window_title);
print_window_title();
}
}
}
line_start = newline_pos + 1;
}
size_t remaining = strlen(line_start);
if (remaining > 0) {
incomplete_line = malloc(remaining + 1);
if (incomplete_line) {
memcpy(incomplete_line, line_start, remaining);
incomplete_line[remaining] = '\0';
incomplete_len = remaining;
}
} else {
incomplete_len = 0;
}
fflush(stdout);
free(data);
FILE *sock_file = fdopen(sock_fd, "r");
if (!sock_file) {
perror("Error: gsr-hyprland-helper: fdopen");
close(sock_fd);
return 1;
}
char buffer[1024];
while (fgets(buffer, sizeof(buffer), sock_file)) {
int line_len = strlen(buffer);
if(line_len > 0 && buffer[line_len - 1] == '\n') {
buffer[line_len - 1] = '\0';
line_len -= 1;
}
if (incomplete_line) {
fflush(stdout);
free(incomplete_line);
if(line_len >= 14 && memcmp(buffer, "activewindow>>", 14) == 0) {
char *window_id = buffer + 14;
char *window_title = strchr(buffer + 14, ',');
if(!window_title)
continue;
window_title[0] = '\0';
window_title += 1;
if(strcmp(window_id, "gsr-ui") == 0 || strcmp(window_id, "gsr-notify") == 0)
continue;
print_window_title(window_title);
}
}
if (active_window) {
if (active_window->window_id) {
free(active_window->window_id);
}
if (active_window->title) {
free(active_window->title);
}
free(active_window);
}
close(sock_fd);
}
int main(int argc, char** argv) {
handle_ipc();
fclose(sock_file);
return 0;
}
int main(void) {
return handle_ipc();
}

View File

@@ -1,10 +1,9 @@
#include "dbus/dbus.h"
#include <dbus/dbus.h>
#include <unistd.h>
#include <iostream>
#include <string>
#include <cstring>
const char* INTROSPECTION_XML =
static const char* INTROSPECTION_XML =
"<!DOCTYPE node PUBLIC \"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN\"\n"
"\"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd\">\n"
"<node>\n"
@@ -32,43 +31,42 @@ public:
connection = dbus_bus_get(DBUS_BUS_SESSION, &err);
if (dbus_error_is_set(&err)) {
std::cerr << "Failed to connect to session bus: " << err.message << "\n";
std::cerr << "Error: gsr-kwin-helper: failed to connect to session bus: " << err.message << "\n";
dbus_error_free(&err);
return false;
}
if (!connection) {
std::cerr << "Connection is null\n";
std::cerr << "Error: gsr-kwin-helper: connection is null\n";
return false;
}
int ret = dbus_bus_request_name(connection, "com.dec05eba.gsr_kwin_helper",
DBUS_NAME_FLAG_REPLACE_EXISTING, &err);
if (dbus_error_is_set(&err)) {
std::cerr << "Failed to request name: " << err.message << "\n";
std::cerr << "Error: gsr-kwin-helper: failed to request name: " << err.message << "\n";
dbus_error_free(&err);
return false;
}
if (ret != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
std::cerr << "Not primary owner of the name\n";
std::cerr << "Error: gsr-kwin-helper: not primary owner of the name\n";
return false;
}
std::cout << "DBus server initialized on com.dec05eba.gsr_kwin_helper\n";
std::cerr << "Info: gsr-kwin-helper: DBus server initialized on com.dec05eba.gsr_kwin_helper\n";
const bool inside_flatpak = access("/app/manifest.json", F_OK) == 0;
std::string helper_path = (
const char *helper_path =
!inside_flatpak
? KWIN_HELPER_SCRIPT_PATH
: "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/share/gsr-ui/gsrkwinhelper.js"
);
: "/var/lib/flatpak/app/com.dec05eba.gpu_screen_recorder/current/active/files/share/gsr-ui/gsrkwinhelper.js";
std::cout << "KWin script path: " << helper_path << std::endl;
std::cerr << "Info: gsr-kwin-helper: KWin script path: " << helper_path << std::endl;
if (!load_kwin_script(connection, helper_path.c_str())) {
std::cerr << "Warning: Failed to load KWin script\n";
if (!load_kwin_script(connection, helper_path)) {
std::cerr << "Warning: gsr-kwin-helper: failed to load KWin script\n";
}
return true;
@@ -164,7 +162,7 @@ public:
);
if (!msg) {
std::cerr << "Failed to create message for " << method << "\n";
std::cerr << "Error: gsr-kwin-helper: failed to create message for " << method << "\n";
return false;
}
@@ -184,7 +182,7 @@ public:
dbus_message_unref(msg);
if (dbus_error_is_set(&err)) {
std::cerr << "Error calling " << method << ": " << err.message << "\n";
std::cerr << "Error: gsr-kwin-helper: error calling " << method << ": " << err.message << "\n";
dbus_error_free(&err);
return false;
}
@@ -201,16 +199,16 @@ public:
call_kwin_method(conn, "unloadScript", "gsrkwinhelper");
if (!call_kwin_method(conn, "loadScript", script_path, "gsrkwinhelper")) {
std::cerr << "Failed to load KWin script\n";
std::cerr << "Error: gsr-kwin-helper: failed to load KWin script\n";
return false;
}
if (!call_kwin_method(conn, "start")) {
std::cerr << "Failed to start KWin script\n";
std::cerr << "Error: gsr-kwin-helper: failed to start KWin script\n";
return false;
}
std::cout << "KWin script loaded and started successfully\n";
std::cerr << "Info: gsr-kwin-helper: KWin script loaded and started successfully\n";
return true;
}