polybar/src/x11/cursor.cpp

44 lines
1.0 KiB
C++
Raw Normal View History

2017-09-03 04:45:45 +00:00
#include "x11/cursor.hpp"
2022-03-20 18:11:03 +00:00
#include "utils/scope.hpp"
2017-09-03 04:45:45 +00:00
POLYBAR_NS
namespace cursor_util {
2022-03-20 18:11:03 +00:00
bool valid(const string& name) {
return (cursors.find(name) != cursors.end());
}
2022-03-20 18:11:03 +00:00
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 + "'");
}
2022-03-20 18:11:03 +00:00
xcb_cursor_context_t* ctx;
2017-09-03 04:45:45 +00:00
if (xcb_cursor_context_new(c, screen, &ctx) < 0) {
return false;
}
2022-03-20 18:11:03 +00:00
2022-04-03 18:08:01 +00:00
scope_util::on_exit handler([&] { xcb_cursor_context_free(ctx); });
2022-03-20 18:11:03 +00:00
xcb_cursor_t cursor = XCB_CURSOR_NONE;
for (const auto& cursor_name : cursors.at(name)) {
2017-09-03 04:45:45 +00:00
cursor = xcb_cursor_load_cursor(ctx, cursor_name.c_str());
2022-03-20 18:11:03 +00:00
if (cursor != XCB_CURSOR_NONE) {
2017-09-03 04:45:45 +00:00
break;
2022-03-20 18:11:03 +00:00
}
2017-09-03 04:45:45 +00:00
}
2022-03-20 18:11:03 +00:00
if (cursor == XCB_CURSOR_NONE) {
return false;
2017-09-03 04:45:45 +00:00
}
2022-03-20 18:11:03 +00:00
2017-09-03 04:45:45 +00:00
xcb_change_window_attributes(c, w, XCB_CW_CURSOR, &cursor);
2022-03-20 18:11:03 +00:00
xcb_free_cursor(c, cursor);
2017-09-03 04:45:45 +00:00
return true;
}
2022-03-20 18:11:03 +00:00
} // namespace cursor_util
2017-09-03 04:45:45 +00:00
POLYBAR_NS_END