From 709862cc84c52cb386688fc8b28dba248ce2c1ea Mon Sep 17 00:00:00 2001 From: Cadu Date: Wed, 4 Dec 2024 20:39:24 -0300 Subject: [PATCH 1/2] Update title if client is remote --- src/wmtitle.cc | 5 +++++ src/ywindow.cc | 12 ++++++++++++ src/ywindow.h | 1 + src/yxapp.h | 11 +++++++++++ 4 files changed, 29 insertions(+) diff --git a/src/wmtitle.cc b/src/wmtitle.cc index c7d609fce..dfbd36b93 100644 --- a/src/wmtitle.cc +++ b/src/wmtitle.cc @@ -526,6 +526,11 @@ void YFrameTitleBar::paint(Graphics &g, const YRect &/*r*/) { } mstring title = getFrame()->getTitle(); + char *client_machine = getFrame()->client()->fetchClientMachine(); + if (strncmp(client_machine, xapp->getLocalHostname(), strlen(client_machine)) != 0) + title = title + " (on " + client_machine + ")"; + free(client_machine); + int const fontHeight = titleFont ? titleFont->height() : 8; int const fontAscent = titleFont ? titleFont->ascent() : 6; int const yPos(int(height() - fontHeight) / 2 + diff --git a/src/ywindow.cc b/src/ywindow.cc index c02e64198..e05cdc7ac 100644 --- a/src/ywindow.cc +++ b/src/ywindow.cc @@ -196,6 +196,18 @@ bool YWindow::fetchTitle(char** title) { return XFetchName(xapp->display(), handle(), title); } +char* YWindow::fetchClientMachine() { + XTextProperty prop; + int ret = XGetTextProperty(xapp->display(), handle(), &prop, XA_WM_CLIENT_MACHINE); + if (ret) { + char *retval = (char *)malloc(sizeof(char)*prop.nitems + 1); + strncpy(retval, (const char *)prop.value, prop.nitems + 1); + XFree(prop.value); + return retval; + } + return nullptr; +} + void YWindow::setClassHint(char const * rName, char const * rClass) { XClassHint wmclass; wmclass.res_name = const_cast(rName); diff --git a/src/ywindow.h b/src/ywindow.h index 8a8e29559..b955f500e 100644 --- a/src/ywindow.h +++ b/src/ywindow.h @@ -98,6 +98,7 @@ class YWindow : protected YWindowList, private YWindowNode { void setWindowFocus(Time timestamp = CurrentTime); bool fetchTitle(char** title); + char* fetchClientMachine(); void setTitle(char const * title); void setClassHint(char const * rName, char const * rClass); diff --git a/src/yxapp.h b/src/yxapp.h index a7434896d..6fb65a77e 100644 --- a/src/yxapp.h +++ b/src/yxapp.h @@ -5,6 +5,7 @@ #include "ywindow.h" #include "ycursor.h" #include +#include #define KEY_MODMASK(x) ((x) & (xapp->KeyMask)) @@ -102,6 +103,16 @@ class YXApplication: public YApplication { YXApplication(int *argc, char ***argv, const char *displayName = nullptr); virtual ~YXApplication(); + char localHostname[256] = {}; + char * getLocalHostname() { + if (localHostname[0] == 0) { + gethostname(localHostname, sizeof(localHostname)); + } + + return localHostname; + } + + Display * display() const { return fDisplay; } int screen() const { return fScreen; } Window root() const { return fRoot; } From 54dd00eabc2e903b9b6f7513833a5fd273a352be Mon Sep 17 00:00:00 2001 From: Cadu Date: Wed, 4 Dec 2024 21:42:23 -0300 Subject: [PATCH 2/2] Don't compare null pointers :) --- src/wmtitle.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/wmtitle.cc b/src/wmtitle.cc index dfbd36b93..0e7bb769f 100644 --- a/src/wmtitle.cc +++ b/src/wmtitle.cc @@ -527,9 +527,10 @@ void YFrameTitleBar::paint(Graphics &g, const YRect &/*r*/) { mstring title = getFrame()->getTitle(); char *client_machine = getFrame()->client()->fetchClientMachine(); - if (strncmp(client_machine, xapp->getLocalHostname(), strlen(client_machine)) != 0) + if (client_machine != nullptr && strncmp(client_machine, xapp->getLocalHostname(), strlen(client_machine)) != 0) { title = title + " (on " + client_machine + ")"; - free(client_machine); + free(client_machine); + } int const fontHeight = titleFont ? titleFont->height() : 8; int const fontAscent = titleFont ? titleFont->ascent() : 6;