polybar/src/utils/i3.cpp

84 lines
2.1 KiB
C++
Raw Normal View History

2016-11-02 19:22:45 +00:00
#include <xcb/xcb.h>
#include <i3ipc++/ipc.hpp>
#include "common.hpp"
2017-01-11 02:07:28 +00:00
#include "settings.hpp"
2016-11-02 19:22:45 +00:00
#include "utils/i3.hpp"
#include "utils/socket.hpp"
#include "utils/string.hpp"
2016-11-26 05:13:20 +00:00
#include "x11/connection.hpp"
#include "x11/ewmh.hpp"
2016-11-30 15:56:48 +00:00
#include "x11/icccm.hpp"
2016-11-02 19:22:45 +00:00
2016-11-19 05:22:44 +00:00
POLYBAR_NS
2016-11-02 19:22:45 +00:00
namespace i3_util {
2016-12-14 03:42:21 +00:00
/**
* Get all workspaces for given output
*/
vector<shared_ptr<workspace_t>> workspaces(const connection_t& conn, const string& output, const bool show_urgent) {
2016-12-14 03:42:21 +00:00
vector<shared_ptr<workspace_t>> result;
for (auto&& ws : conn.get_workspaces()) {
if (output.empty() || ws->output == output || (show_urgent && ws->urgent)) {
2016-12-14 03:42:21 +00:00
result.emplace_back(forward<decltype(ws)>(ws));
}
}
return result;
}
/**
* Get currently focused workspace
*/
shared_ptr<workspace_t> focused_workspace(const connection_t& conn) {
for (auto&& ws : conn.get_workspaces()) {
if (ws->focused) {
return ws;
}
}
return nullptr;
}
2016-11-02 19:22:45 +00:00
/**
* Get main root window
2016-11-02 19:22:45 +00:00
*/
xcb_window_t root_window(connection& conn) {
2016-11-02 19:22:45 +00:00
auto children = conn.query_tree(conn.screen()->root).children();
2017-01-24 07:49:27 +00:00
const auto wm_name = [&](xcb_connection_t* conn, xcb_window_t win) -> string {
string title;
2017-01-24 07:49:27 +00:00
if (!(title = ewmh_util::get_wm_name(win)).empty()) {
return title;
2017-01-24 07:49:27 +00:00
} else if (!(title = icccm_util::get_wm_name(conn, win)).empty()) {
return title;
} else {
return "";
}
};
2016-11-02 19:22:45 +00:00
for (auto it = children.begin(); it != children.end(); it++) {
2017-01-24 07:49:27 +00:00
if (wm_name(conn, *it) == "i3") {
return *it;
2016-11-19 03:06:05 +00:00
}
2016-11-02 19:22:45 +00:00
}
return XCB_NONE;
2016-11-02 19:22:45 +00:00
}
/**
2017-01-01 14:45:18 +00:00
* Restack given window relative to the i3 root window
2016-11-02 19:22:45 +00:00
* defined for the given monitor
*
* Fixes the issue with always-on-top window's
*/
bool restack_to_root(connection& conn, const xcb_window_t win) {
2017-01-19 10:11:28 +00:00
const unsigned int value_mask = XCB_CONFIG_WINDOW_SIBLING | XCB_CONFIG_WINDOW_STACK_MODE;
const unsigned int value_list[2]{root_window(conn), XCB_STACK_MODE_ABOVE};
if (value_list[0] != XCB_NONE) {
2016-11-02 19:22:45 +00:00
conn.configure_window_checked(win, value_mask, value_list);
return true;
}
return false;
}
}
2016-11-19 05:22:44 +00:00
POLYBAR_NS_END