From b8a7b6a60507f0b99be9d7d534937013945ad708 Mon Sep 17 00:00:00 2001
From: patrick96
Date: Sun, 20 Mar 2022 19:11:03 +0100
Subject: [PATCH] Cleanup cursor code
---
include/x11/cursor.hpp | 18 ++++++++++--------
src/components/bar.cpp | 2 +-
src/x11/cursor.cpp | 28 ++++++++++++++++++++--------
3 files changed, 31 insertions(+), 17 deletions(-)
diff --git a/include/x11/cursor.hpp b/include/x11/cursor.hpp
index 84c33257..be15c878 100644
--- a/include/x11/cursor.hpp
+++ b/include/x11/cursor.hpp
@@ -9,19 +9,21 @@
#include
#include "common.hpp"
-#include "x11/connection.hpp"
#include "utils/string.hpp"
+#include "x11/connection.hpp"
POLYBAR_NS
namespace cursor_util {
static const std::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);
-}
+ {"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
diff --git a/src/components/bar.cpp b/src/components/bar.cpp
index 72ee640b..9709ef9d 100644
--- a/src/components/bar.cpp
+++ b/src/components/bar.cpp
@@ -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();
}
diff --git a/src/x11/cursor.cpp b/src/x11/cursor.cpp
index 858b8e67..fb72ed91 100644
--- a/src/x11/cursor.cpp
+++ b/src/x11/cursor.cpp
@@ -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