Cleanup cursor code

This commit is contained in:
patrick96 2022-03-20 19:11:03 +01:00 committed by Patrick Ziegler
parent 3f89d73374
commit b8a7b6a605
3 changed files with 31 additions and 17 deletions

View File

@ -9,19 +9,21 @@
#include <xcb/xcb_cursor.h>
#include "common.hpp"
#include "x11/connection.hpp"
#include "utils/string.hpp"
#include "x11/connection.hpp"
POLYBAR_NS
namespace cursor_util {
static const std::map<string, vector<string>> cursors = {
{"pointer", {"pointing_hand", "pointer", "hand", "hand1", "hand2", "e29285e634086352946a0e7090d73106", "9d800788f1b08800ae810202380a0822"}},
{"default", {"left_ptr", "arrow", "dnd-none", "op_left_arrow"}},
{"ns-resize", {"size_ver", "sb_v_double_arrow", "v_double_arrow", "n-resize", "s-resize", "col-resize", "top_side", "bottom_side", "base_arrow_up", "base_arrow_down", "based_arrow_down", "based_arrow_up", "00008160000006810000408080010102"}}
};
bool valid(string name);
bool set_cursor(xcb_connection_t *c, xcb_screen_t *screen, xcb_window_t w, string name);
}
{"pointer", {"pointing_hand", "pointer", "hand", "hand1", "hand2", "e29285e634086352946a0e7090d73106",
"9d800788f1b08800ae810202380a0822"}},
{"default", {"left_ptr", "arrow", "dnd-none", "op_left_arrow"}},
{"ns-resize", {"size_ver", "sb_v_double_arrow", "v_double_arrow", "n-resize", "s-resize", "col-resize",
"top_side", "bottom_side", "base_arrow_up", "base_arrow_down", "based_arrow_down",
"based_arrow_up", "00008160000006810000408080010102"}}};
bool valid(const string& name);
bool set_cursor(xcb_connection_t* c, xcb_screen_t* screen, xcb_window_t w, const string& name);
} // namespace cursor_util
POLYBAR_NS_END

View File

@ -924,7 +924,7 @@ void bar::change_cursor(const string& name) {
m_opts.cursor = name;
if (!cursor_util::set_cursor(m_connection, m_connection.screen(), m_opts.window, name)) {
m_log.warn("Failed to create cursor context");
m_log.warn("Failed to create cursor context for cursor name '%s'", name);
}
m_connection.flush();
}

View File

@ -1,31 +1,43 @@
#include "x11/cursor.hpp"
#include "utils/scope.hpp"
POLYBAR_NS
namespace cursor_util {
bool valid(string name) {
bool valid(const string& name) {
return (cursors.find(name) != cursors.end());
}
bool set_cursor(xcb_connection_t *c, xcb_screen_t *screen, xcb_window_t w, string name) {
bool set_cursor(xcb_connection_t* c, xcb_screen_t* screen, xcb_window_t w, const string& name) {
if (!valid(name)) {
throw std::runtime_error("Tried to set cursor to invalid name: '" + name + "'");
}
xcb_cursor_t cursor = XCB_CURSOR_NONE;
xcb_cursor_context_t *ctx;
xcb_cursor_context_t* ctx;
if (xcb_cursor_context_new(c, screen, &ctx) < 0) {
return false;
}
for (auto&& cursor_name : cursors.at(name)) {
scope_util::on_exit<> handler([&] { xcb_cursor_context_free(ctx); });
xcb_cursor_t cursor = XCB_CURSOR_NONE;
for (const auto& cursor_name : cursors.at(name)) {
cursor = xcb_cursor_load_cursor(ctx, cursor_name.c_str());
if (cursor != XCB_CURSOR_NONE)
if (cursor != XCB_CURSOR_NONE) {
break;
}
}
if (cursor == XCB_CURSOR_NONE) {
return false;
}
xcb_change_window_attributes(c, w, XCB_CW_CURSOR, &cursor);
xcb_cursor_context_free(ctx);
xcb_free_cursor(c, cursor);
return true;
}
}
} // namespace cursor_util
POLYBAR_NS_END