Skip to content

Commit

Permalink
Updating context when window title changes
Browse files Browse the repository at this point in the history
  • Loading branch information
houmain committed Jan 25, 2021
1 parent 09abcaa commit 0b74a01
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
24 changes: 13 additions & 11 deletions src/linux/client/FocusedWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,29 @@ class FocusedWindow {
m_net_active_window_atom = XInternAtom(m_display, "_NET_ACTIVE_WINDOW", False);
m_net_wm_name_atom = XInternAtom(m_display, "_NET_WM_NAME", False);
m_utf8_string_atom = XInternAtom(m_display, "UTF8_STRING", False);
XSetErrorHandler([](Display*, XErrorEvent*) { return 0; });
return true;
}

bool update() {
const auto window = get_focused_window();
if (window == m_focused_window)
auto window_title = get_window_title(window);
if (window == m_focused_window &&
window_title == m_focused_window_title)
return false;

// TODO: is there a better way to prevent races?
const auto handler = XSetErrorHandler(&ignore_errors);
m_focused_window = window;
m_focused_window_class = get_window_class(window);
m_focused_window_title = get_window_title(window);
XSetErrorHandler(handler);
// window handles can become invalid any time
auto window_class = get_window_class(window);
if (window_class.empty() || window_title.empty())
return false;

m_focused_window = window;
m_focused_window_class = std::move(window_class);
m_focused_window_title = std::move(window_title);
return true;
}

private:
static int ignore_errors(Display*, XErrorEvent*) { return 0; }

Window get_focused_window() {
auto type = Atom{ };
auto format = 0;
Expand All @@ -68,7 +70,7 @@ class FocusedWindow {

std::string get_window_class(Window window) {
auto ch = XClassHint{ };
if (m_focused_window &&
if (window &&
XGetClassHint(m_display, window, &ch) != 0) {
const auto result = std::string(ch.res_name);
XFree(ch.res_name);
Expand All @@ -84,7 +86,7 @@ class FocusedWindow {
auto length = 0ul;
auto rest = 0ul;
auto data = std::add_pointer_t<unsigned char>{ };
if (m_focused_window &&
if (window &&
XGetWindowProperty(m_display, window, m_net_wm_name_atom, 0, 1024,
False, m_utf8_string_atom, &type, &format, &length,
&rest, &data) == Success &&
Expand Down
13 changes: 8 additions & 5 deletions src/win32/FocusedWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,29 @@
#include "FocusedWindow.h"
#include "win.h"
#include <array>
#include <cstring>

class FocusedWindow {
public:
HWND current() const { return m_current; }
const std::string& get_class() const { return m_class; }
const std::string& get_title() const { return m_title; }

bool update() {
const auto hwnd = GetForegroundWindow();
if (hwnd == m_current)

auto buffer = std::array<char, 256>();
GetWindowTextA(hwnd, buffer.data(), static_cast<int>(buffer.size()));

if (hwnd == m_current &&
!std::strcmp(buffer.data(), m_title.c_str()))
return false;

m_current = hwnd;
m_title = buffer.data();

auto buffer = std::array<char, 256>();
GetClassNameA(hwnd, buffer.data(), static_cast<int>(buffer.size()));
m_class = buffer.data();

GetWindowTextA(hwnd, buffer.data(), static_cast<int>(buffer.size()));
m_title = buffer.data();
return true;
}

Expand Down

0 comments on commit 0b74a01

Please sign in to comment.