refactor(cursor): use map for cursor list

This commit is contained in:
NBonaparte 2017-09-08 16:45:16 -07:00
parent b7f1960a0a
commit e612fe1624
3 changed files with 24 additions and 14 deletions

View File

@ -15,9 +15,12 @@
POLYBAR_NS
namespace cursor_util {
static const vector<string> pointer_names {"pointing_hand", "pointer", "hand", "hand1", "hand2", "e29285e634086352946a0e7090d73106", "9d800788f1b08800ae810202380a0822"};
static const vector<string> default_names {"left_ptr", "arrow", "dnd-none", "op_left_arrow"};
static const vector<string> ns_resize_names {"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"};
static const 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);
}

View File

@ -132,6 +132,17 @@ bar::bar(connection& conn, signal_emitter& emitter, const config& config, const
m_opts.cursor_click = m_conf.get(bs, "cursor-click", ""s);
m_opts.cursor_scroll = m_conf.get(bs, "cursor-scroll", ""s);
#if WITH_XCURSOR
if (!m_opts.cursor_click.empty() && !cursor_util::valid(m_opts.cursor_click)) {
m_log.warn("Ignoring unsupported cursor-click option '%s'", m_opts.cursor_click);
m_opts.cursor_click.clear();
}
if (!m_opts.cursor_scroll.empty() && !cursor_util::valid(m_opts.cursor_scroll)) {
m_log.warn("Ignoring unsupported cursor-scroll option '%s'", m_opts.cursor_scroll);
m_opts.cursor_scroll.clear();
}
#endif
// Build WM_NAME
m_opts.wmname = m_conf.get(bs, "wm-name", "polybar-" + bs.substr(4) + "_" + m_opts.monitor->name);
m_opts.wmname = string_util::replace(m_opts.wmname, " ", "-");

View File

@ -3,6 +3,12 @@
POLYBAR_NS
namespace cursor_util {
bool valid(string name) {
if (cursors.find(name) != cursors.end())
return true;
return false;
}
bool set_cursor(xcb_connection_t *c, xcb_screen_t *screen, xcb_window_t w, string name) {
xcb_cursor_t cursor = XCB_CURSOR_NONE;
xcb_cursor_context_t *ctx;
@ -10,17 +16,7 @@ namespace cursor_util {
if (xcb_cursor_context_new(c, screen, &ctx) < 0) {
return false;
}
const vector<string> *name_list;
if (string_util::compare("pointer", name)) {
name_list = &pointer_names;
} else if (string_util::compare("ns-resize", name)) {
name_list = &ns_resize_names;
} else {
name_list = &default_names;
}
for (auto&& cursor_name : *name_list) {
for (auto&& cursor_name : cursors.at(name)) {
cursor = xcb_cursor_load_cursor(ctx, cursor_name.c_str());
if (cursor != XCB_CURSOR_NONE)
break;