commit f1f8d511bbf84a85e5214a4246c5938a45ff7354
parent 811ca13d0ba503678f63fcf2108fedb836f6d2ee
Author: Matthias Balk <mbalk@mbalk.de>
Date: Sun, 3 Nov 2019 10:02:26 +0100
set mouse cursor to crosshair
inspired by
https://www.x.org/releases/X11R7.5/doc/libxcb/tutorial/index.html#mousecursorexample
Diffstat:
| M | xruler.c | | | 57 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
1 file changed, 57 insertions(+), 0 deletions(-)
diff --git a/xruler.c b/xruler.c
@@ -6,6 +6,7 @@
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
#include <xcb/xcb.h>
@@ -13,6 +14,61 @@
static uint32_t width = 1500;
static uint32_t height = 50;
+
+static void set_crosshair_cursor(
+ xcb_connection_t *c,
+ xcb_screen_t *screen,
+ xcb_window_t window)
+{
+ int cursor_id = 33; /* Crosshair */
+
+ xcb_font_t font = xcb_generate_id(c);
+ xcb_void_cookie_t cookie_font =
+ xcb_open_font_checked(c, font, strlen("cursor"), "cursor");
+
+ xcb_generic_error_t *error;
+ error = xcb_request_check(c, cookie_font);
+ if (error) {
+ fprintf(stderr, "ERROR: can't open font : %d\n", error->error_code);
+ xcb_disconnect(c);
+ exit(-1);
+ }
+
+ xcb_cursor_t cursor = xcb_generate_id(c);
+ xcb_create_glyph_cursor(c, cursor, font, font,
+ cursor_id, cursor_id + 1,
+ 0, 0, 0,
+ 0, 0, 0);
+
+ xcb_gcontext_t gc = xcb_generate_id(c);
+ uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
+ uint32_t values_list[] = {screen->black_pixel, screen->white_pixel, font};
+ xcb_void_cookie_t cookie_gc =
+ xcb_create_gc_checked(c, gc, window, mask, values_list);
+
+ error = xcb_request_check(c, cookie_gc);
+ if (error) {
+ fprintf(stderr, "ERROR: can't create gc : %d\n", error->error_code);
+ xcb_disconnect(c);
+ exit(-1);
+ }
+
+ mask = XCB_CW_CURSOR;
+ uint32_t value_list = cursor;
+ xcb_change_window_attributes(c, window, mask, &value_list);
+
+ xcb_free_cursor(c, cursor);
+
+ cookie_font = xcb_close_font_checked(c, font);
+ error = xcb_request_check(c, cookie_font);
+ if (error) {
+ fprintf(stderr, "ERROR: can't close font : %d\n", error->error_code);
+ xcb_disconnect(c);
+ exit(-1);
+ }
+}
+
+
int main (int argc, char** argv)
{
/* Open the connection to the X server */
@@ -58,6 +114,7 @@ int main (int argc, char** argv)
xcb_map_window (connection, window);
xcb_flush (connection);
+ set_crosshair_cursor(connection, screen, window);
/* draw primitives */
xcb_generic_event_t* event;