bar: set_cursor(): new interface function, to set the X cursor

Use this to explicitly set the initial/default cursor to "left_ptr"
This commit is contained in:
Daniel Eklöf 2018-12-26 17:16:54 +01:00
parent ce68bdb59d
commit 1b50808da5
3 changed files with 39 additions and 7 deletions

View file

@ -14,13 +14,13 @@ set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")
find_package(Threads REQUIRED) find_package(Threads REQUIRED)
find_package(PkgConfig REQUIRED) find_package(PkgConfig REQUIRED)
pkg_check_modules(XCB REQUIRED xcb xcb-randr xcb-render) # Core 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(CAIRO REQUIRED cairo cairo-xcb) # Core
pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration) pkg_check_modules(YAML REQUIRED yaml-0.1) # Core (configuration)
pkg_check_modules(XCB_XKB REQUIRED xcb-xkb) # Module/xkb pkg_check_modules(XCB_XKB REQUIRED xcb-xkb) # Module/xkb
pkg_check_modules(JSON REQUIRED json-c) # Module/i3 pkg_check_modules(JSON REQUIRED json-c) # Module/i3
pkg_check_modules(UDEV REQUIRED libudev) # Module/battery pkg_check_modules(UDEV REQUIRED libudev) # Module/battery
add_executable(f00bar add_executable(f00bar
bar.c bar.h bar.c bar.h

32
bar.c
View file

@ -13,9 +13,10 @@
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <xcb/xcb.h> #include <xcb/xcb.h>
#include <xcb/xcb_event.h>
#include <xcb/randr.h> #include <xcb/randr.h>
#include <xcb/render.h> #include <xcb/render.h>
#include <xcb/xcb_cursor.h>
#include <xcb/xcb_event.h>
#include <xcb/xcb_ewmh.h> #include <xcb/xcb_ewmh.h>
#include <cairo.h> #include <cairo.h>
@ -64,6 +65,8 @@ struct private {
xcb_colormap_t colormap; xcb_colormap_t colormap;
xcb_pixmap_t pixmap; xcb_pixmap_t pixmap;
xcb_gc_t gc; xcb_gc_t gc;
xcb_cursor_context_t *cursor_ctx;
xcb_cursor_t cursor;
cairo_t *cairo; cairo_t *cairo;
}; };
@ -172,6 +175,7 @@ expose(const struct bar *_bar)
} }
static void refresh(const struct bar *bar); static void refresh(const struct bar *bar);
static void set_cursor(struct bar *bar, const char *cursor);
static int static int
run(struct bar_run_context *run_ctx) run(struct bar_run_context *run_ctx)
@ -376,6 +380,12 @@ run(struct bar_run_context *run_ctx)
bar->cairo = cairo_create(surface); bar->cairo = cairo_create(surface);
xcb_map_window(bar->conn, bar->win); 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); xcb_flush(bar->conn);
/* Start modules */ /* Start modules */
@ -490,6 +500,11 @@ run(struct bar_run_context *run_ctx)
cairo_surface_destroy(surface); cairo_surface_destroy(surface);
cairo_debug_reset_static_data(); 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_gc(bar->conn, bar->gc);
xcb_free_pixmap(bar->conn, bar->pixmap); xcb_free_pixmap(bar->conn, bar->pixmap);
xcb_destroy_window(bar->conn, bar->win); xcb_destroy_window(bar->conn, bar->win);
@ -528,6 +543,18 @@ refresh(const struct bar *bar)
free(evt); 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 static void
destroy(struct bar *bar) destroy(struct bar *bar)
{ {
@ -567,6 +594,8 @@ bar_new(const struct bar_config *config)
priv->left.count = config->left.count; priv->left.count = config->left.count;
priv->center.count = config->center.count; priv->center.count = config->center.count;
priv->right.count = config->right.count; priv->right.count = config->right.count;
priv->cursor_ctx = NULL;
priv->cursor = 0;
for (size_t i = 0; i < priv->left.count; i++) for (size_t i = 0; i < priv->left.count; i++)
priv->left.mods[i] = config->left.mods[i]; priv->left.mods[i] = config->left.mods[i];
@ -580,6 +609,7 @@ bar_new(const struct bar_config *config)
bar->run = &run; bar->run = &run;
bar->destroy = &destroy; bar->destroy = &destroy;
bar->refresh = &refresh; bar->refresh = &refresh;
bar->set_cursor = &set_cursor;
for (size_t i = 0; i < priv->left.count; i++) for (size_t i = 0; i < priv->left.count; i++)
priv->left.mods[i]->bar = bar; priv->left.mods[i]->bar = bar;

2
bar.h
View file

@ -13,7 +13,9 @@ struct bar {
void *private; void *private;
int (*run)(struct bar_run_context *ctx); int (*run)(struct bar_run_context *ctx);
void (*destroy)(struct bar *bar); void (*destroy)(struct bar *bar);
void (*refresh)(const 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 }; enum bar_location { BAR_TOP, BAR_BOTTOM };