2016-11-18 22:03:18 -05:00
|
|
|
#include "x11/ewmh.hpp"
|
2016-12-03 09:44:54 -05:00
|
|
|
#include "components/types.hpp"
|
2016-11-26 00:14:58 -05:00
|
|
|
#include "x11/xutils.hpp"
|
2016-11-18 22:03:18 -05:00
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS
|
2016-11-18 22:03:18 -05:00
|
|
|
|
|
|
|
namespace ewmh_util {
|
2016-12-21 17:22:02 -05:00
|
|
|
ewmh_connection_t g_connection{nullptr};
|
2016-11-26 00:14:58 -05:00
|
|
|
ewmh_connection_t initialize() {
|
2016-12-21 17:22:02 -05:00
|
|
|
if (!g_connection) {
|
|
|
|
g_connection = memory_util::make_malloc_ptr<xcb_ewmh_connection_t>(
|
|
|
|
[=](xcb_ewmh_connection_t* c) { xcb_ewmh_connection_wipe(c); });
|
|
|
|
xcb_ewmh_init_atoms_replies(
|
|
|
|
&*g_connection, xcb_ewmh_init_atoms(xutils::get_connection(), &*g_connection), nullptr);
|
2016-11-26 00:14:58 -05:00
|
|
|
}
|
2016-12-21 17:22:02 -05:00
|
|
|
return g_connection;
|
2016-11-18 22:03:18 -05:00
|
|
|
}
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
bool supports(xcb_ewmh_connection_t* ewmh, xcb_atom_t atom, int screen) {
|
2016-11-18 22:03:18 -05:00
|
|
|
bool supports{false};
|
|
|
|
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_atoms_reply_t reply{};
|
2016-11-18 22:03:18 -05:00
|
|
|
reply.atoms = nullptr;
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
if (!xcb_ewmh_get_supported_reply(ewmh, xcb_ewmh_get_supported(ewmh, screen), &reply, nullptr)) {
|
2016-11-18 22:03:18 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t n = 0; n < reply.atoms_len; ++n) {
|
|
|
|
if (reply.atoms[n] == atom) {
|
|
|
|
supports = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reply.atoms != nullptr) {
|
|
|
|
xcb_ewmh_get_atoms_reply_wipe(&reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
return supports;
|
|
|
|
}
|
|
|
|
|
2016-11-30 07:38:40 -05:00
|
|
|
string get_wm_name(xcb_ewmh_connection_t* conn, xcb_window_t win) {
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_utf8_strings_reply_t utf8_reply{};
|
2016-11-30 07:38:40 -05:00
|
|
|
if (xcb_ewmh_get_wm_name_reply(conn, xcb_ewmh_get_wm_name(conn, win), &utf8_reply, nullptr)) {
|
|
|
|
return get_reply_string(&utf8_reply);
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
2016-11-18 22:03:18 -05:00
|
|
|
string get_visible_name(xcb_ewmh_connection_t* conn, xcb_window_t win) {
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_utf8_strings_reply_t utf8_reply{};
|
2016-11-30 07:38:40 -05:00
|
|
|
if (xcb_ewmh_get_wm_visible_name_reply(conn, xcb_ewmh_get_wm_visible_name(conn, win), &utf8_reply, nullptr)) {
|
|
|
|
return get_reply_string(&utf8_reply);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-30 07:38:40 -05:00
|
|
|
return "";
|
2016-11-18 22:03:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
string get_icon_name(xcb_ewmh_connection_t* conn, xcb_window_t win) {
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_utf8_strings_reply_t utf8_reply{};
|
2016-11-30 07:38:40 -05:00
|
|
|
if (xcb_ewmh_get_wm_icon_name_reply(conn, xcb_ewmh_get_wm_icon_name(conn, win), &utf8_reply, nullptr)) {
|
|
|
|
return get_reply_string(&utf8_reply);
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-30 07:38:40 -05:00
|
|
|
return "";
|
2016-11-18 22:03:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
string get_reply_string(xcb_ewmh_get_utf8_strings_reply_t* reply) {
|
2016-11-30 07:38:40 -05:00
|
|
|
if (reply == nullptr) {
|
2016-11-18 22:03:18 -05:00
|
|
|
return "";
|
2016-11-25 07:55:15 -05:00
|
|
|
}
|
2016-11-30 07:38:40 -05:00
|
|
|
string str(reply->strings, reply->strings_len);
|
2016-11-18 22:03:18 -05:00
|
|
|
xcb_ewmh_get_utf8_strings_reply_wipe(reply);
|
2016-11-30 07:38:40 -05:00
|
|
|
return str;
|
2016-11-18 22:03:18 -05:00
|
|
|
}
|
2016-11-26 00:14:58 -05:00
|
|
|
|
|
|
|
uint32_t get_current_desktop(xcb_ewmh_connection_t* conn, int screen) {
|
|
|
|
uint32_t desktop{0};
|
|
|
|
if (xcb_ewmh_get_current_desktop_reply(conn, xcb_ewmh_get_current_desktop(conn, screen), &desktop, nullptr)) {
|
|
|
|
return desktop;
|
|
|
|
}
|
|
|
|
return XCB_NONE;
|
|
|
|
}
|
|
|
|
|
2016-11-26 03:38:55 -05:00
|
|
|
vector<position> get_desktop_viewports(xcb_ewmh_connection_t* conn, int screen) {
|
2016-11-26 03:51:06 -05:00
|
|
|
vector<position> viewports;
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_desktop_viewport_reply_t reply{};
|
2016-11-26 03:38:55 -05:00
|
|
|
|
2016-11-26 03:51:06 -05:00
|
|
|
if (!xcb_ewmh_get_desktop_viewport_reply(conn, xcb_ewmh_get_desktop_viewport(conn, screen), &reply, nullptr)) {
|
|
|
|
return viewports;
|
|
|
|
}
|
2016-11-26 03:38:55 -05:00
|
|
|
|
|
|
|
for (size_t n = 0; n < reply.desktop_viewport_len; n++) {
|
2016-12-03 09:44:54 -05:00
|
|
|
viewports.emplace_back(position{
|
|
|
|
static_cast<int16_t>(reply.desktop_viewport[n].x), static_cast<int16_t>(reply.desktop_viewport[n].y)});
|
2016-11-26 03:38:55 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return viewports;
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
vector<string> get_desktop_names(xcb_ewmh_connection_t* conn, int screen) {
|
2016-11-26 03:51:06 -05:00
|
|
|
vector<string> names;
|
2016-12-15 03:29:14 -05:00
|
|
|
xcb_ewmh_get_utf8_strings_reply_t reply{};
|
2016-11-26 00:14:58 -05:00
|
|
|
|
2016-11-26 03:51:06 -05:00
|
|
|
if (!xcb_ewmh_get_desktop_names_reply(conn, xcb_ewmh_get_desktop_names(conn, screen), &reply, nullptr)) {
|
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
char buffer[BUFSIZ];
|
2016-11-26 00:39:37 -05:00
|
|
|
size_t len{0};
|
2016-11-26 00:14:58 -05:00
|
|
|
|
2016-11-26 00:39:37 -05:00
|
|
|
for (size_t n = 0; n < reply.strings_len; n++) {
|
2016-11-26 00:14:58 -05:00
|
|
|
if (reply.strings[n] == '\0') {
|
|
|
|
names.emplace_back(buffer, len);
|
|
|
|
len = 0;
|
|
|
|
} else {
|
|
|
|
buffer[len++] = reply.strings[n];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:39:37 -05:00
|
|
|
if (len) {
|
|
|
|
names.emplace_back(buffer, len);
|
|
|
|
}
|
|
|
|
|
2016-11-26 00:14:58 -05:00
|
|
|
return names;
|
|
|
|
}
|
|
|
|
|
|
|
|
xcb_window_t get_active_window(xcb_ewmh_connection_t* conn, int screen) {
|
2016-11-26 03:51:06 -05:00
|
|
|
xcb_window_t win{0};
|
|
|
|
if (!xcb_ewmh_get_active_window_reply(conn, xcb_ewmh_get_active_window(conn, screen), &win, nullptr)) {
|
|
|
|
return XCB_NONE;
|
|
|
|
}
|
2016-11-26 00:14:58 -05:00
|
|
|
return win;
|
|
|
|
}
|
2016-11-26 04:33:32 -05:00
|
|
|
|
|
|
|
void change_current_desktop(xcb_ewmh_connection_t* conn, uint32_t desktop) {
|
|
|
|
xcb_ewmh_request_change_current_desktop(conn, 0, desktop, XCB_CURRENT_TIME);
|
|
|
|
}
|
2016-11-18 22:03:18 -05:00
|
|
|
}
|
|
|
|
|
2016-11-19 00:22:44 -05:00
|
|
|
POLYBAR_NS_END
|