From e612fe16240478c3c7dfdd80c337c28898bcf38b Mon Sep 17 00:00:00 2001 From: NBonaparte Date: Fri, 8 Sep 2017 16:45:16 -0700 Subject: [PATCH] refactor(cursor): use map for cursor list --- include/x11/cursor.hpp | 9 ++++++--- src/components/bar.cpp | 11 +++++++++++ src/x11/cursor.cpp | 18 +++++++----------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/include/x11/cursor.hpp b/include/x11/cursor.hpp index 7be50c89..514aa1eb 100644 --- a/include/x11/cursor.hpp +++ b/include/x11/cursor.hpp @@ -15,9 +15,12 @@ POLYBAR_NS namespace cursor_util { - static const vector pointer_names {"pointing_hand", "pointer", "hand", "hand1", "hand2", "e29285e634086352946a0e7090d73106", "9d800788f1b08800ae810202380a0822"}; - static const vector default_names {"left_ptr", "arrow", "dnd-none", "op_left_arrow"}; - static const vector 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> 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); } diff --git a/src/components/bar.cpp b/src/components/bar.cpp index d16cce65..adb2c32b 100644 --- a/src/components/bar.cpp +++ b/src/components/bar.cpp @@ -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, " ", "-"); diff --git a/src/x11/cursor.cpp b/src/x11/cursor.cpp index 01d7d7a9..8ecdcb1d 100644 --- a/src/x11/cursor.cpp +++ b/src/x11/cursor.cpp @@ -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 *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;