Added KWin workaround to get current window title

This commit is contained in:
Andrew
2026-01-21 23:58:31 +03:00
committed by dec05eba
parent 23b1526092
commit 00ceaa989d
6 changed files with 401 additions and 0 deletions

76
src/KwinWorkaround.cpp Normal file
View File

@@ -0,0 +1,76 @@
#include "../include/KwinWorkaround.hpp"
#include <cstddef>
#include <iostream>
#include <sys/types.h>
#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 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 = getenv("FLATPAK_ID") != NULL;
if(inside_flatpak) {
std::cerr << "Sorry, KWin workaround isn't available for Flatpak yet. Stay tuned!\n";
return;
}
FILE* pipe = popen("/usr/bin/gsr-kwin-helper", "r");
if (!pipe) {
std::cerr << "Failed to start gsr-kwin-helper process\n";
return;
}
std::cerr << "Started a KWin helper thread\n";
char buffer[4096];
const std::string prefix = "Active window title set to: ";
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
std::string line(buffer);
if (!line.empty() && line.back() == '\n') {
line.pop_back();
}
size_t pos = line.find(prefix);
if (pos != std::string::npos) {
std::string title = line.substr(pos + prefix.length());
active_kwin_window->title = title;
}
}
pclose(pipe);
}
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([&] {
kwin_script_thread();
}).detach();
}
}