From 8e5b5258e2c8133f7e61712b828d9390fd5947a2 Mon Sep 17 00:00:00 2001 From: Mihir Lad Date: Sat, 18 Jul 2020 01:46:07 -0400 Subject: [PATCH] Use focused_title_change_event to update titles This fixes issue mihirlad55/polybar-dwm-module#4 where the title would not update if the title of the currently focused window changed. The focused_title_change_event is raised exactly when the focused window's title changes, so this should automatically update the title using the callback when necessary. --- include/modules/dwm.hpp | 1 + src/modules/dwm.cpp | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/include/modules/dwm.hpp b/include/modules/dwm.hpp index d081d3ec..ff108d26 100644 --- a/include/modules/dwm.hpp +++ b/include/modules/dwm.hpp @@ -67,6 +67,7 @@ namespace modules { shared_ptr> m_monitors; unsigned int m_active_mon_num = 0; unsigned int m_bar_mon = 0; + unsigned int m_focused_client_id = 0; std::unordered_map m_state_labels; vector m_tags; diff --git a/src/modules/dwm.cpp b/src/modules/dwm.cpp index ea80bba4..4cdb7652 100644 --- a/src/modules/dwm.cpp +++ b/src/modules/dwm.cpp @@ -111,19 +111,28 @@ namespace modules { return; } + m_focused_client_id = ev.new_win_id; m_title_label->reset_tokens(); - if (ev.new_win_id != 0) { - auto focused_client = m_ipc->get_client(ev.new_win_id); + if (m_focused_client_id != 0) { + auto focused_client = m_ipc->get_client(m_focused_client_id); m_title_label->replace_token("%title%", focused_client->name); } else { m_title_label->replace_token("%title%", ""); } }; + m_ipc->on_focused_title_change = [this](const dwmipc::FocusedTitleChangeEvent& ev) { + if (ev.monitor_num == m_bar_mon && ev.client_window_id == m_focused_client_id) { + m_title_label->reset_tokens(); + m_title_label->replace_token("%title%", ev.new_name); + } + }; + m_ipc->subscribe(dwmipc::Event::LAYOUT_CHANGE); m_ipc->subscribe(dwmipc::Event::CLIENT_FOCUS_CHANGE); m_ipc->subscribe(dwmipc::Event::TAG_CHANGE); m_ipc->subscribe(dwmipc::Event::MONITOR_FOCUS_CHANGE); + m_ipc->subscribe(dwmipc::Event::FOCUSED_TITLE_CHANGE); } catch (const dwmipc::IPCError& err) { throw module_error(err.what()); }