kwin: add support for determining active window's monitor name

This commit is contained in:
p0358
2026-02-19 23:22:31 +01:00
committed by dec05eba
parent 4b47063406
commit 1951fd7c20
4 changed files with 45 additions and 4 deletions

View File

@@ -6,9 +6,11 @@ namespace gsr {
struct ActiveKwinWindow { struct ActiveKwinWindow {
std::string title = "Game"; std::string title = "Game";
bool fullscreen = false; bool fullscreen = false;
std::string monitorName = "";
}; };
void start_kwin_helper_thread(); void start_kwin_helper_thread();
std::string get_current_kwin_window_title(); std::string get_current_kwin_window_title();
bool get_current_kwin_window_fullscreen(); bool get_current_kwin_window_fullscreen();
std::string get_current_kwin_window_monitor_name();
} }

View File

@@ -27,6 +27,7 @@ namespace gsr {
char buffer[4096]; char buffer[4096];
const std::string prefix_title = "Active window title set to: "; const std::string prefix_title = "Active window title set to: ";
const std::string prefix_fullscreen = "Active window fullscreen state set to: "; const std::string prefix_fullscreen = "Active window fullscreen state set to: ";
const std::string prefix_monitor = "Active window monitor name set to: ";
std::string line; std::string line;
while (fgets(buffer, sizeof(buffer), pipe) != nullptr) { while (fgets(buffer, sizeof(buffer), pipe) != nullptr) {
@@ -43,6 +44,9 @@ namespace gsr {
} else if ((pos = line.find(prefix_fullscreen)) != std::string::npos) { } else if ((pos = line.find(prefix_fullscreen)) != std::string::npos) {
std::string fullscreen = line.substr(pos + prefix_fullscreen.length()); std::string fullscreen = line.substr(pos + prefix_fullscreen.length());
active_kwin_window.fullscreen = fullscreen == "1"; active_kwin_window.fullscreen = fullscreen == "1";
} else if ((pos = line.find(prefix_monitor)) != std::string::npos) {
std::string monitorName = line.substr(pos + prefix_monitor.length());
active_kwin_window.monitorName = std::move(monitorName);
} }
} }
@@ -57,6 +61,10 @@ namespace gsr {
return active_kwin_window.fullscreen; return active_kwin_window.fullscreen;
} }
std::string get_current_kwin_window_monitor_name() {
return active_kwin_window.monitorName;
}
void start_kwin_helper_thread() { void start_kwin_helper_thread() {
if (kwin_helper_thread_started) { if (kwin_helper_thread_started) {
return; return;

View File

@@ -1,10 +1,10 @@
const DAEMON_DBUS_NAME = "com.dec05eba.gpu_screen_recorder"; const DAEMON_DBUS_NAME = "com.dec05eba.gpu_screen_recorder";
function dbusSendUpdateActiveWindow(title, isFullscreen) { function dbusSendUpdateActiveWindow(title, isFullscreen, monitorName) {
callDBus( callDBus(
DAEMON_DBUS_NAME, "/", DAEMON_DBUS_NAME, DAEMON_DBUS_NAME, "/", DAEMON_DBUS_NAME,
"updateActiveWindow", "updateActiveWindow",
title, isFullscreen, title, isFullscreen, monitorName,
); );
} }
@@ -13,15 +13,18 @@ let prevEmitActiveWindowUpdate = null;
let prevCaption = null; let prevCaption = null;
let prevFullScreen = null; let prevFullScreen = null;
let prevMonitorName = null;
function emitActiveWindowUpdate(window) { function emitActiveWindowUpdate(window) {
if (workspace.activeWindow === window) { if (workspace.activeWindow === window) {
let caption = window.caption || ""; let caption = window.caption || "";
let fullScreen = window.fullScreen || false; let fullScreen = window.fullScreen || false;
if (caption !== prevCaption || fullScreen !== prevFullScreen) { let monitorName = window.output?.name || "";
dbusSendUpdateActiveWindow(caption, fullScreen); if (caption !== prevCaption || fullScreen !== prevFullScreen || monitorName !== prevMonitorName) {
dbusSendUpdateActiveWindow(caption, fullScreen, monitorName);
prevCaption = caption; prevCaption = caption;
prevFullScreen = fullScreen; prevFullScreen = fullScreen;
prevMonitorName = monitorName;
} }
} }
} }
@@ -32,10 +35,12 @@ function subscribeToWindow(window) {
if (prevWindow !== null) { if (prevWindow !== null) {
prevWindow.captionChanged.disconnect(prevEmitActiveWindowUpdate); prevWindow.captionChanged.disconnect(prevEmitActiveWindowUpdate);
prevWindow.fullScreenChanged.disconnect(prevEmitActiveWindowUpdate); prevWindow.fullScreenChanged.disconnect(prevEmitActiveWindowUpdate);
prevWindow.outputChanged.disconnect(prevEmitActiveWindowUpdate);
} }
let emitActiveWindowUpdateBound = emitActiveWindowUpdate.bind(null, window); let emitActiveWindowUpdateBound = emitActiveWindowUpdate.bind(null, window);
window.captionChanged.connect(emitActiveWindowUpdateBound); window.captionChanged.connect(emitActiveWindowUpdateBound);
window.fullScreenChanged.connect(emitActiveWindowUpdateBound); window.fullScreenChanged.connect(emitActiveWindowUpdateBound);
window.outputChanged.connect(emitActiveWindowUpdateBound);
prevWindow = window; prevWindow = window;
prevEmitActiveWindowUpdate = emitActiveWindowUpdateBound; prevEmitActiveWindowUpdate = emitActiveWindowUpdateBound;
} }

View File

@@ -11,6 +11,7 @@ static const char* INTROSPECTION_XML =
" <method name='updateActiveWindow'>\n" " <method name='updateActiveWindow'>\n"
" <arg type='s' name='title' direction='in'/>\n" " <arg type='s' name='title' direction='in'/>\n"
" <arg type='b' name='fullscreen' direction='in'/>\n" " <arg type='b' name='fullscreen' direction='in'/>\n"
" <arg type='s' name='monitorName' direction='in'/>\n"
" </method>\n" " </method>\n"
" </interface>\n" " </interface>\n"
" <interface name='org.freedesktop.DBus.Introspectable'>\n" " <interface name='org.freedesktop.DBus.Introspectable'>\n"
@@ -25,6 +26,7 @@ class GsrKwinHelper {
public: public:
std::string active_window_title; std::string active_window_title;
bool active_window_fullscreen; bool active_window_fullscreen;
std::string active_window_monitor_name;
DBusConnection* connection = nullptr; DBusConnection* connection = nullptr;
DBusError err; DBusError err;
@@ -114,6 +116,7 @@ public:
DBusMessageIter args; DBusMessageIter args;
DBusBasicValue title; DBusBasicValue title;
DBusBasicValue fullscreen; DBusBasicValue fullscreen;
DBusBasicValue monitorName;
if (!dbus_message_iter_init(msg, &args)) { if (!dbus_message_iter_init(msg, &args)) {
send_error_reply(msg, "No arguments provided"); send_error_reply(msg, "No arguments provided");
@@ -138,6 +141,18 @@ public:
} }
dbus_message_iter_get_basic(&args, &fullscreen); dbus_message_iter_get_basic(&args, &fullscreen);
if (!dbus_message_iter_next(&args)) {
send_error_reply(msg, "Not enough arguments provided");
return;
}
if (dbus_message_iter_get_arg_type(&args) != DBUS_TYPE_STRING) {
send_error_reply(msg, "Expected string argument");
return;
}
dbus_message_iter_get_basic(&args, &monitorName);
if (title.str) { if (title.str) {
if (active_window_title != title.str) { if (active_window_title != title.str) {
@@ -156,6 +171,17 @@ public:
std::cout.flush(); std::cout.flush();
} }
if (monitorName.str) {
if (active_window_monitor_name != monitorName.str) {
active_window_monitor_name = monitorName.str;
std::cout << "Active window monitor name set to: " << active_window_monitor_name << "\n";
std::cout.flush();
}
} else {
send_error_reply(msg, "Failed to read string");
return;
}
send_success_reply(msg); send_success_reply(msg);
} }