xruler

a screen ruler for X11
Log | Files | Refs | LICENSE

commit 03bb532f65248cda454c924618de941f6509857d
Author: Matthias Balk <mbalk@mbalk.de>
Date:   Mon, 30 Sep 2019 19:45:27 +0200

initial commit

Diffstat:
AMakefile | 5+++++
Axruler.c | 109+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 114 insertions(+), 0 deletions(-)

diff --git a/Makefile b/Makefile @@ -0,0 +1,5 @@ +CC = gcc +LINKER_FLAGS = `pkg-config --libs xcb` + +xruler: xruler.c + gcc xruler.c $(LINKER_FLAGS) -o xruler diff --git a/xruler.c b/xruler.c @@ -0,0 +1,109 @@ +#include <stdint.h> +#include <stdio.h> +#include <stdlib.h> + +#include <xcb/xcb.h> + + +static uint32_t width = 1500; +static uint32_t height = 50; + +int main (int argc, char** argv) +{ + /* Open the connection to the X server */ + xcb_connection_t* connection = xcb_connect(NULL, NULL); + + + /* Get the first screen */ + xcb_screen_t* screen = + xcb_setup_roots_iterator(xcb_get_setup (connection)).data; + + + /* Create black (foreground) graphic context */ + xcb_drawable_t window = screen->root; + xcb_gcontext_t foreground = xcb_generate_id (connection); + uint32_t mask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; + uint32_t values[2] = {screen->black_pixel, 0}; + + xcb_create_gc(connection, foreground, window, mask, values); + + + /* Create a window */ + window = xcb_generate_id(connection); + + mask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; + values[0] = screen->white_pixel; + values[1] = XCB_EVENT_MASK_EXPOSURE | + XCB_EVENT_MASK_KEY_PRESS | + XCB_EVENT_MASK_POINTER_MOTION; + + xcb_create_window(connection, /* connection */ + XCB_COPY_FROM_PARENT, /* depth */ + window, /* window Id */ + screen->root, /* parent window */ + 0, 0, /* x, y */ + width, height, /* width, height */ + 0, /* border_width */ + XCB_WINDOW_CLASS_INPUT_OUTPUT, /* class */ + screen->root_visual, /* visual */ + mask, values); /* masks */ + + + /* Map the window on the screen and flush*/ + xcb_map_window (connection, window); + xcb_flush (connection); + + + /* draw primitives */ + xcb_generic_event_t* event; + while (event = xcb_wait_for_event(connection)) { + switch (event->response_type & ~0x80) { + case XCB_EXPOSE: + { + for (int i = 1; i < 1000; i++) + { + int y = 15; + if (i % 50 == 0) y = 45; + else if (i % 10 == 0) y = 35; + else if (i % 5 == 0) y = 25; + + xcb_point_t polyline[] = { {5*i, 0}, {0, y} }; + xcb_poly_line ( + connection, XCB_COORD_MODE_PREVIOUS, window, foreground, + 2, polyline); + } + + /* flush the request */ + xcb_flush (connection); + + break; + } + + case XCB_KEY_PRESS: + { + xcb_key_press_event_t* kp = (xcb_key_press_event_t*) event; + + if (kp->detail == 24) /* q */ + { + free(event); + xcb_disconnect(connection); + return 0; + } + } + + 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 ); + break; + } + + default: + /* Unknown event type, ignore it */ + break; + } + + free (event); + } +}