Created
December 4, 2018 12:57
-
-
Save betaboon/5483f73600532c69d2ea5b2713aa162f to your computer and use it in GitHub Desktop.
adapted from https://github.com/jaagr/polybar/pull/576
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/include/modules/xwindow.hpp b/include/modules/xwindow.hpp | |
index 4378ed5..e4b0e5c 100644 | |
--- a/include/modules/xwindow.hpp | |
+++ b/include/modules/xwindow.hpp | |
@@ -18,6 +18,7 @@ namespace modules { | |
bool match(const xcb_window_t win) const; | |
string title() const; | |
+ xcb_window_t get_window() const; | |
private: | |
xcb_connection_t* m_connection{nullptr}; | |
@@ -45,11 +46,14 @@ namespace modules { | |
private: | |
static constexpr const char* TAG_LABEL{"<label>"}; | |
+ bool active_window_on_monitor(xcb_window_t window, monitor_t monitor) const; | |
connection& m_connection; | |
unique_ptr<active_window> m_active; | |
map<state, label_t> m_statelabels; | |
label_t m_label; | |
+ | |
+ bool m_pinoutput{false}; | |
}; | |
} | |
diff --git a/src/modules/xwindow.cpp b/src/modules/xwindow.cpp | |
index fbc23af..a2e2a43 100644 | |
--- a/src/modules/xwindow.cpp | |
+++ b/src/modules/xwindow.cpp | |
@@ -1,6 +1,7 @@ | |
#include "modules/xwindow.hpp" | |
#include "drawtypes/label.hpp" | |
#include "utils/factory.hpp" | |
+#include "utils/bspwm.hpp" | |
#include "x11/atoms.hpp" | |
#include "x11/connection.hpp" | |
@@ -58,11 +59,55 @@ namespace modules { | |
} | |
} | |
+ xcb_window_t active_window::get_window() const { | |
+ return m_window; | |
+ } | |
+ | |
+ // if any xcb request fails, assume true | |
+ bool xwindow_module::active_window_on_monitor(xcb_window_t window, monitor_t mon) const { | |
+ xcb_get_geometry_reply_t* geom = xcb_get_geometry_reply(m_connection, | |
+ xcb_get_geometry(m_connection, window), nullptr); | |
+ if (!geom) { | |
+ return true; | |
+ } | |
+ | |
+ xcb_translate_coordinates_reply_t *trans = xcb_translate_coordinates_reply(m_connection, | |
+ xcb_translate_coordinates(m_connection, window, m_connection.root(), geom->x, geom->y), nullptr); | |
+ | |
+ if (!trans) { | |
+ return true; | |
+ } | |
+ | |
+ int win_x = 0; // top right | |
+ int win_y = 0; // bottom left | |
+ | |
+ win_x = trans->dst_x; | |
+ win_y = trans->dst_y; | |
+ | |
+ // bspwm workaround - translated coordinates are way off (about x2), | |
+ // coordinates relative to parent window in geom are correct, no translation needed | |
+ if (file_util::exists(bspwm_util::get_socket_path())) { | |
+ win_x = geom->x; | |
+ win_y = geom->y; | |
+ } | |
+ | |
+ // if x pos is within monitor width range and y is within height range | |
+ bool in_x = win_x <= mon->x + mon->w - 1 && win_x >= mon->x; | |
+ bool in_y = win_y <= mon->y + mon->h - 1 && win_y >= mon->y; | |
+ | |
+ free(geom); free(trans); | |
+ return in_x && in_y; | |
+ } | |
+ | |
/** | |
* Construct module | |
*/ | |
xwindow_module::xwindow_module(const bar_settings& bar, string name_) | |
: static_module<xwindow_module>(bar, move(name_)), m_connection(connection::make()) { | |
+ | |
+ // read config values | |
+ m_pinoutput = m_conf.get(name(), "pin-output", m_pinoutput); | |
+ | |
// Initialize ewmh atoms | |
if ((ewmh_util::initialize()) == nullptr) { | |
throw module_error("Failed to initialize ewmh atoms"); | |
@@ -122,7 +167,22 @@ namespace modules { | |
if (m_active) { | |
m_label = m_statelabels.at(state::ACTIVE)->clone(); | |
m_label->reset_tokens(); | |
- m_label->replace_token("%title%", m_active->title()); | |
+ | |
+ if (!m_active) { | |
+ m_label->replace_token("%title%", ""); | |
+ return; | |
+ } | |
+ | |
+ if (m_pinoutput) { | |
+ if (active_window_on_monitor(m_active->get_window(), m_bar.monitor)) { | |
+ m_label->replace_token("%title%", m_active->title()); | |
+ return; | |
+ } else { | |
+ m_label->replace_token("%title%", ""); | |
+ } | |
+ } else { | |
+ m_label->replace_token("%title%", m_active->title()); | |
+ } | |
} else { | |
m_label = m_statelabels.at(state::EMPTY)->clone(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment