commit 5bee511bdc41bb290500059d6488ee4492b5f51b
parent 4490bedd1d309132e58f37c9fc0d3b4f5c03b520
Author: Matthias Balk <mbalk@mbalk.de>
Date: Tue, 5 Nov 2019 19:01:50 +0100
print mouse coordinates in window (instead of stdout)
Diffstat:
| M | xruler.c | | | 78 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- |
1 file changed, 76 insertions(+), 2 deletions(-)
diff --git a/xruler.c b/xruler.c
@@ -69,6 +69,79 @@ static void set_crosshair_cursor(
}
+static xcb_gc_t get_gc_font(
+ xcb_connection_t *c,
+ xcb_screen_t *screen,
+ xcb_window_t window,
+ const char *font_name)
+{
+ xcb_font_t font = xcb_generate_id(c);
+ xcb_void_cookie_t cookie_font =
+ xcb_open_font_checked(c, font, strlen(font_name), font_name);
+
+ 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);
+ return -1;
+ }
+
+ xcb_gcontext_t gc = xcb_generate_id(c);
+ uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_BACKGROUND | XCB_GC_FONT;
+ uint32_t value_list[] = {screen->black_pixel, screen->white_pixel, font};
+ xcb_void_cookie_t cookie_gc =
+ xcb_create_gc_checked(c, gc, window, mask, value_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);
+ }
+
+ 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);
+ }
+
+ return gc;
+}
+
+
+static void draw_text(
+ xcb_connection_t *c,
+ xcb_screen_t *screen,
+ xcb_window_t window,
+ int16_t x,
+ int16_t y,
+ const char *label)
+{
+ xcb_gcontext_t gc = get_gc_font(c, screen, window, "8x13");
+ xcb_void_cookie_t cookie_text =
+ xcb_image_text_8_checked(c, strlen(label), window, gc, x, y, label);
+
+ xcb_generic_error_t *error;
+ error = xcb_request_check(c, cookie_text);
+ if (error) {
+ fprintf(stderr, "ERROR: can't paste text : %d\n", error->error_code);
+ xcb_disconnect(c);
+ exit(-1);
+ }
+
+ xcb_void_cookie_t cookie_gc = xcb_free_gc(c, gc);
+ error = xcb_request_check(c, cookie_gc);
+ if (error) {
+ fprintf(stderr, "ERROR: can't free gc : %d\n", error->error_code);
+ xcb_disconnect(c);
+ exit(-1);
+ }
+}
+
+
int main (int argc, char** argv)
{
/* Open the connection to the X server */
@@ -156,8 +229,9 @@ int main (int argc, char** argv)
case XCB_MOTION_NOTIFY:
{
xcb_motion_notify_event_t* motion = (xcb_motion_notify_event_t*) event;
- printf("Mouse moved in window %u, at coordinates (%u,%u)\n",
- motion->event, motion->event_x, motion->event_y );
+ char buf[16];
+ snprintf(buf, 16, "%04u,%04u", motion->event_x, motion->event_y);
+ draw_text(connection, screen, window, 5, height - 5, buf);
break;
}