From 1b50808da575cc346c01dcde40c51d684103ee2d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Wed, 26 Dec 2018 17:16:54 +0100 Subject: [PATCH] bar: set_cursor(): new interface function, to set the X cursor Use this to explicitly set the initial/default cursor to "left_ptr" --- CMakeLists.txt | 12 ++++++------ bar.c | 32 +++++++++++++++++++++++++++++++- bar.h | 2 ++ 3 files changed, 39 insertions(+), 7 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71789ec..55fb894 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -14,13 +14,13 @@ set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}") find_package(Threads REQUIRED) find_package(PkgConfig REQUIRED) -pkg_check_modules(XCB REQUIRED xcb xcb-randr xcb-render) # Core -pkg_check_modules(CAIRO REQUIRED cairo cairo-xcb) # Core -pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration) +pkg_check_modules(XCB REQUIRED xcb xcb-randr xcb-render xcb-cursor) # Core +pkg_check_modules(CAIRO REQUIRED cairo cairo-xcb) # Core +pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration) -pkg_check_modules(XCB_XKB REQUIRED xcb-xkb) # Module/xkb -pkg_check_modules(JSON REQUIRED json-c) # Module/i3 -pkg_check_modules(UDEV REQUIRED libudev) # Module/battery +pkg_check_modules(XCB_XKB REQUIRED xcb-xkb) # Module/xkb +pkg_check_modules(JSON REQUIRED json-c) # Module/i3 +pkg_check_modules(UDEV REQUIRED libudev) # Module/battery add_executable(f00bar bar.c bar.h diff --git a/bar.c b/bar.c index 8226325..0aa61df 100644 --- a/bar.c +++ b/bar.c @@ -13,9 +13,10 @@ #include #include -#include #include #include +#include +#include #include #include @@ -64,6 +65,8 @@ struct private { xcb_colormap_t colormap; xcb_pixmap_t pixmap; xcb_gc_t gc; + xcb_cursor_context_t *cursor_ctx; + xcb_cursor_t cursor; cairo_t *cairo; }; @@ -172,6 +175,7 @@ expose(const struct bar *_bar) } static void refresh(const struct bar *bar); +static void set_cursor(struct bar *bar, const char *cursor); static int run(struct bar_run_context *run_ctx) @@ -376,6 +380,12 @@ run(struct bar_run_context *run_ctx) bar->cairo = cairo_create(surface); xcb_map_window(bar->conn, bar->win); + + if (xcb_cursor_context_new(bar->conn, screen, &bar->cursor_ctx) < 0) + LOG_WARN("failed to create XCB cursor context"); + else + set_cursor(_bar, "left_ptr"); + xcb_flush(bar->conn); /* Start modules */ @@ -490,6 +500,11 @@ run(struct bar_run_context *run_ctx) cairo_surface_destroy(surface); cairo_debug_reset_static_data(); + if (bar->cursor_ctx != NULL) { + xcb_free_cursor(bar->conn, bar->cursor); + xcb_cursor_context_free(bar->cursor_ctx); + } + xcb_free_gc(bar->conn, bar->gc); xcb_free_pixmap(bar->conn, bar->pixmap); xcb_destroy_window(bar->conn, bar->win); @@ -528,6 +543,18 @@ refresh(const struct bar *bar) free(evt); } +static void +set_cursor(struct bar *bar, const char *cursor) +{ + struct private *b = bar->private; + + if (b->cursor_ctx == NULL) + return; + + b->cursor = xcb_cursor_load_cursor(b->cursor_ctx, cursor); + xcb_change_window_attributes(b->conn, b->win, XCB_CW_CURSOR, &b->cursor); +} + static void destroy(struct bar *bar) { @@ -567,6 +594,8 @@ bar_new(const struct bar_config *config) priv->left.count = config->left.count; priv->center.count = config->center.count; priv->right.count = config->right.count; + priv->cursor_ctx = NULL; + priv->cursor = 0; for (size_t i = 0; i < priv->left.count; i++) priv->left.mods[i] = config->left.mods[i]; @@ -580,6 +609,7 @@ bar_new(const struct bar_config *config) bar->run = &run; bar->destroy = &destroy; bar->refresh = &refresh; + bar->set_cursor = &set_cursor; for (size_t i = 0; i < priv->left.count; i++) priv->left.mods[i]->bar = bar; diff --git a/bar.h b/bar.h index 916621c..c64403e 100644 --- a/bar.h +++ b/bar.h @@ -13,7 +13,9 @@ struct bar { void *private; int (*run)(struct bar_run_context *ctx); void (*destroy)(struct bar *bar); + void (*refresh)(const struct bar *bar); + void (*set_cursor)(struct bar *bar, const char *cursor); }; enum bar_location { BAR_TOP, BAR_BOTTOM };