diff options
author | ben <ben.nagy@arachnida.blog> | 2024-10-17 18:19:25 -0700 |
---|---|---|
committer | ben <ben.nagy@arachnida.blog> | 2024-10-17 18:19:25 -0700 |
commit | 639e8280cc4440f4162c3488f1b5c8ad36dec346 (patch) | |
tree | 429dd4265ac9dc9fd584b4bc073b149278092d0d /src/scapx.c | |
parent | 88be7c1ea7b8a0eb1909b3ad43ebb8c0ee37a052 (diff) |
Key symbols and mouse pointer added
Diffstat (limited to 'src/scapx.c')
-rw-r--r-- | src/scapx.c | 165 |
1 files changed, 95 insertions, 70 deletions
diff --git a/src/scapx.c b/src/scapx.c index 09b597e..50edc7f 100644 --- a/src/scapx.c +++ b/src/scapx.c @@ -1,89 +1,114 @@ // std #include <stdio.h> #include <stdlib.h> -#include <unistd.h> +#include <stdbool.h> +#include <stdarg.h> + +// X +#include <X11/keysymdef.h> +#include <X11/keysym.h> +#include <xcb/xcb_keysyms.h> +#include <X11/cursorfont.h> +//#include <xcb/xcb_atom.h> +// #include <xcb/xcb_util.h> +// #include <X11/Xutil.h> +#include <xcb/xcb_cursor.h> // local +#include "types.h" #include "scapx.h" -#include "defs.h" +#include "x.h" +#include "config.h" -// TODO: move this all to x.c -static Server_context_t *init_XCB_server(); -static xcb_window_t create_win(Server_context_t *info); -static Server_context_t *init_XCB_server() +void dbg_printf(const char *fmt, ...) { - Server_context_t *info; + va_list args; + va_start(args, fmt); + vfprintf(stderr, fmt, args); + va_end(args); +} - if (!(info = malloc(sizeof(Server_context_t)))) { - fprintf(stderr, "memory allocation failed.\n"); - return NULL; - } +int main(int argc, char **argv) +{ + // TODO: parse flags - info->con = xcb_connect(NULL, &info->scr_nbr); - if (xcb_connection_has_error(info->con) > 0) { - fprintf(stderr, "Error opening display.\n"); - free(info); - return NULL; - } + Server_context_t *info; - info->iter = xcb_setup_roots_iterator(xcb_get_setup(info->con)); - for (; info->iter.rem; --info->scr_nbr, xcb_screen_next(&info->iter)) - if (info->scr_nbr == 0) { - info->scr = info->iter.data; - break; + if (!(info = init_XCB_server())) + FATAL_ERROR("Couldn't make a connection to the x server. %s", ""); + + xcb_drawable_t win = create_win(info); + xcb_key_symbols_t *ksymbols = xcb_key_symbols_alloc(info->con); + xcb_generic_event_t *event; + xcb_cursor_t mptr; + + bool done = 0; + while (!done && (event = xcb_wait_for_event(info->con))) { + if (event->response_type == 0 ) + SLOG("Error reported by the x server.\n"); + + switch (event->response_type & ~0x80) { + { // mouse + static bool lmbtn = 0; + static bool rmbtn = 0; + case XCB_BUTTON_PRESS: + if (((xcb_button_press_event_t *)event)->detail == M_BTN_1) { + lmbtn = 1; + printf("left clicked.\n"); + } + + if (((xcb_button_press_event_t *)event)->detail == M_BTN_2) { + printf("right clicked.\n"); + rmbtn = 1; + } + break; + case XCB_BUTTON_RELEASE: + cursor_die(info->con, win, mptr); + if (((xcb_button_press_event_t *)event)->detail == M_BTN_1) { + lmbtn = 0; + } + if (((xcb_button_press_event_t *)event)->detail == M_BTN_2) { + rmbtn = 0; + } + break; + case XCB_MOTION_NOTIFY: + if (lmbtn || rmbtn) { + mptr = cursor_set(info->con, info->scr, win, XC_cross); + printf("%s drag.\n", lmbtn ? "left" : "right"); + } else + printf("moving.\n"); + break; + } + + case XCB_KEY_PRESS: + printf("\nKeyPress event received:\n"); + xcb_key_press_event_t *kevent = (xcb_key_press_event_t *)event; + + // NOTE: field is a mask of the buttons held down during the event + printf("state %#x ", kevent->state); + printf("keycode %u ", kevent->detail); + + + xcb_keysym_t ksym = xcb_key_symbols_get_keysym(ksymbols, kevent->detail, kevent->state); + const char *sym_str = ksym_to_str(&ksym); + + if (ksym == XK_Escape || ksym == XK_q || ksym == XK_Q) + die(ksymbols, info, win); + + + printf("keysym [%#x %s]\n", ksym, sym_str ? sym_str : ""); + break; + case XCB_KEY_RELEASE: + break; } - return info; -} - -static xcb_window_t create_win(Server_context_t *info) -{ - xcb_gcontext_t gc; - xcb_window_t win; - u32 vmask, vlist[2]; - - // create gc - win = info->scr->root; - gc = xcb_generate_id(info->con); - vmask = XCB_GC_FOREGROUND | XCB_GC_GRAPHICS_EXPOSURES; - vlist[0] = info->scr->black_pixel; - vlist[1] = 0; - xcb_create_gc (info->con, gc, win, vmask, vlist); - - // create window - win = xcb_generate_id(info->con); - vmask = XCB_CW_BACK_PIXEL | XCB_CW_EVENT_MASK; - vlist[0] = info->scr->white_pixel; - vlist[1] = XCB_EVENT_MASK_EXPOSURE; - - xcb_create_window(info->con, - XCB_COPY_FROM_PARENT, - win, - info->scr->root, - 0, 0, - 150, 150, - 10, - XCB_WINDOW_CLASS_INPUT_OUTPUT, - info->scr->root_visual, - vmask, vlist - ); - xcb_map_window(info->con, win); - xcb_flush(info->con); - - return win; -} - -int main(int argc, char **argv) -{ - // TODO: Parse flags + free(event); + } - Server_context_t *serv_context = init_XCB_server(); - xcb_drawable_t win = create_win(serv_context); - pause(); + xcb_free_cursor(info->con, mptr); - xcb_disconnect(serv_context->con); - free(serv_context); + die(ksymbols, info, win); return 0; } |