diff --git a/LICENSE b/LICENSE index 173bef4..27b06f0 100644 --- a/LICENSE +++ b/LICENSE @@ -11,7 +11,7 @@ MIT/X Consortium License © 2014-2020 Hiltjo Posthuma © 2015-2019 Quentin Rameau © 2018-2019 Henrik Nyman -© 2022 adnano +© 2022 adnano Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), diff --git a/README.md b/README.md index d0e62c4..62a34a7 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ Dependencies: - scdoc (optional) ``` -$ meson build +$ meson setup build $ ninja -C build # ninja -C build install ``` @@ -27,17 +27,6 @@ See wmenu(1) To use wmenu with Sway, you can add the following to your configuration file: ``` -set $menu dmenu_path | wmenu | xargs swaymsg exec -- +set $menu wmenu-run bindsym $mod+d exec $menu ``` - -## Contributing - -Send patches and questions to [~adnano/wmenu-devel](https://lists.sr.ht/~adnano/wmenu-devel). - -Subscribe to release announcements on [~adnano/wmenu-announce](https://lists.sr.ht/~adnano/wmenu-announce). - -## Credits - -This project started as a fork of [dmenu-wl](https://github.com/nyyManni/dmenu-wayland). -However, most of the code was rewritten from scratch. diff --git a/docs/meson.build b/docs/meson.build index 4ccfe49..39498d8 100644 --- a/docs/meson.build +++ b/docs/meson.build @@ -1,4 +1,4 @@ -scdoc_dep = dependency('scdoc', version: '>=1.9.2', native: true) +scdoc_dep = dependency('scdoc', version: '>=1.9.2', native: true, required: false) if scdoc_dep.found() scdoc = find_program( diff --git a/docs/wmenu.1.scd b/docs/wmenu.1.scd index f532b21..4519e8b 100644 --- a/docs/wmenu.1.scd +++ b/docs/wmenu.1.scd @@ -1,4 +1,4 @@ -wmenu(1) +WMENU(1) # NAME @@ -6,7 +6,7 @@ wmenu - dynamic menu for Wayland # SYNOPSIS -*wmenu* [-biv] \ +*wmenu* [-biPv] \ [-f _font_] \ [-l _lines_] \ [-o _output_] \ @@ -15,13 +15,18 @@ wmenu - dynamic menu for Wayland [-M _color_] [-m _color_] \ [-S _color_] [-s _color_] +*wmenu-run* ... + # DESCRIPTION -wmenu is a dynamic menu for Wayland, which reads a list of newline-separated +*wmenu* is a dynamic menu for Wayland, which reads a list of newline-separated items from stdin. When the user selects an item and presses Return, their choice is printed to stdout and wmenu terminates. Entering text will narrow the items to those matching the tokens in the input. +*wmenu-run* is a special invocation of wmenu which lists programs in the user's +$PATH and runs the result. + # OPTIONS *-b* @@ -30,6 +35,10 @@ to those matching the tokens in the input. *-i* wmenu matches menu items case insensitively. +*-P* + wmenu will not directly display the keyboard input, but instead replace it + with asterisks. + *-v* prints version information to stdout, then exits. @@ -92,56 +101,86 @@ arrow keys, page up, page down, home, and end. Move cursor to the end of the current word. |[ *C-a* -:[ Home +:< Home |[ *C-b* -:[ Left +:< Left |[ *C-c* -:[ Escape +:< Escape |[ *C-d* -:[ Delete +:< Delete |[ *C-e* -:[ End +:< End |[ *C-f* -:[ Right +:< Right |[ *C-g* -:[ Escape +:< Escape + +|[ *C-[* +:< Escape |[ *C-h* -:[ Backspace +:< Backspace |[ *C-i* -:[ Tab +:< Tab |[ *C-j* -:[ Return +:< Return |[ *C-J* -:[ Shift-Return +:< Shift-Return |[ *C-k* -:[ Delete line right +:< Delete line right |[ *C-m* -:[ Return +:< Return |[ *C-M* -:[ Shift-Return +:< Shift-Return |[ *C-n* -:[ Down +:< Down |[ *C-p* -:[ Up +:< Up |[ *C-u* -:[ Delete line left +:< Delete line left |[ *C-w* -:[ Delete word left +:< Delete word left + +|[ *C-Y* +:< Paste from Wayland clipboard + +|[ *M-b* +:< Move cursor to the start of the current word + +|[ *M-f* +:< Move cursor to the end of the current word + +|[ *M-g* +:< Home + +|[ *M-G* +:< End + +|[ *M-h* +:< Up + +|[ *M-j* +:< Page down + +|[ *M-k* +:< Page up + +|[ *M-l* +:< Down diff --git a/main.c b/main.c deleted file mode 100644 index 056e0a3..0000000 --- a/main.c +++ /dev/null @@ -1,1261 +0,0 @@ -#define _POSIX_C_SOURCE 200809L -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "pango.h" -#include "pool-buffer.h" -#include "wlr-layer-shell-unstable-v1-client-protocol.h" - -struct menu_item { - char *text; - int width; - struct menu_item *next; // traverses all items - struct menu_item *left, *right; // traverses matching items -}; - -struct output { - struct menu_state *menu; - struct wl_output *output; - int32_t scale; -}; - -struct menu_state { - struct output *output; - char *output_name; - - struct wl_display *display; - struct wl_compositor *compositor; - struct wl_shm *shm; - struct wl_seat *seat; - struct wl_data_device_manager *data_device_manager; - struct zwlr_layer_shell_v1 *layer_shell; - - struct wl_surface *surface; - struct zwlr_layer_surface_v1 *layer_surface; - - struct xkb_context *xkb_context; - struct xkb_keymap *xkb_keymap; - struct xkb_state *xkb_state; - - struct wl_data_offer *offer; - - struct pool_buffer buffers[2]; - struct pool_buffer *current; - - int width; - int height; - int line_height; - int padding; - int inputw; - int promptw; - int left_arrow, right_arrow; - - bool bottom; - int (*fstrncmp)(const char *, const char *, size_t); - char *font; - bool vertical; - int lines; - char *prompt; - uint32_t background, foreground; - uint32_t promptbg, promptfg; - uint32_t selectionbg, selectionfg; - - char text[BUFSIZ]; - size_t cursor; - - int repeat_timer; - int repeat_delay; - int repeat_period; - enum wl_keyboard_key_state repeat_key_state; - xkb_keysym_t repeat_sym; - - bool run; - bool failure; - - struct menu_item *items; - struct menu_item *matches; - struct menu_item *selection; - struct menu_item *leftmost, *rightmost; -}; - -static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { - cairo_set_source_rgba(cairo, - (color >> (3*8) & 0xFF) / 255.0, - (color >> (2*8) & 0xFF) / 255.0, - (color >> (1*8) & 0xFF) / 255.0, - (color >> (0*8) & 0xFF) / 255.0); -} - -static void insert(struct menu_state *state, const char *s, ssize_t n); -static void match(struct menu_state *state); -static size_t nextrune(struct menu_state *state, int incr); - -int render_text(struct menu_state *state, cairo_t *cairo, const char *str, - int x, int y, int width, int height, - uint32_t foreground, uint32_t background, - int left_padding, int right_padding) { - - int text_width, text_height; - get_text_size(cairo, state->font, &text_width, &text_height, NULL, 1, str); - int text_y = (state->line_height / 2.0) - (text_height / 2.0); - - if (background) { - int bg_width = text_width + left_padding + right_padding; - cairo_set_source_u32(cairo, background); - cairo_rectangle(cairo, x, y, bg_width, height); - cairo_fill(cairo); - } - - cairo_move_to(cairo, x + left_padding, y + text_y); - cairo_set_source_u32(cairo, foreground); - pango_printf(cairo, state->font, 1, str); - - return x + text_width + left_padding + right_padding; -} - -int render_horizontal_item(struct menu_state *state, cairo_t *cairo, const char *str, - int x, int y, int width, int height, - uint32_t foreground, uint32_t background, - int left_padding, int right_padding) { - - int text_width, text_height; - get_text_size(cairo, state->font, &text_width, &text_height, NULL, 1, str); - int text_y = (state->line_height / 2.0) - (text_height / 2.0); - - if (x + left_padding + text_width > width) { - return -1; - } else { - if (background) { - int bg_width = text_width + left_padding + right_padding; - cairo_set_source_u32(cairo, background); - cairo_rectangle(cairo, x, y, bg_width, height); - cairo_fill(cairo); - } - - cairo_move_to(cairo, x + left_padding, y + text_y); - cairo_set_source_u32(cairo, foreground); - pango_printf(cairo, state->font, 1, str); - } - - return x + text_width + left_padding + right_padding; -} - -void render_vertical_item(struct menu_state *state, cairo_t *cairo, const char *str, - int x, int y, int width, int height, - uint32_t foreground, uint32_t background, - int left_padding) { - - int text_height; - get_text_size(cairo, state->font, NULL, &text_height, NULL, 1, str); - int text_y = (state->line_height / 2.0) - (text_height / 2.0); - - if (background) { - int bg_width = state->width - x; - cairo_set_source_u32(cairo, background); - cairo_rectangle(cairo, x, y, bg_width, height); - cairo_fill(cairo); - } - - cairo_move_to(cairo, x + left_padding, y + text_y); - cairo_set_source_u32(cairo, foreground); - pango_printf(cairo, state->font, 1, str); -} - -void scroll_matches(struct menu_state *state) { - if (!state->matches) { - return; - } - - if (state->vertical) { - if (state->leftmost == NULL) { - state->leftmost = state->matches; - if (state->rightmost == NULL) { - int offs = 0; - struct menu_item *item; - for (item = state->matches; item->left != state->selection; item = item->right) { - offs += state->line_height; - if (offs >= state->height) { - state->leftmost = item->left; - offs = state->height - offs; - } - } - } else { - int offs = 0; - struct menu_item *item; - for (item = state->rightmost; item; item = item->left) { - offs += state->line_height; - if (offs >= state->height) { - state->leftmost = item->right; - break; - } - } - } - } - if (state->rightmost == NULL) { - state->rightmost = state->matches; - int offs = 0; - struct menu_item *item; - for (item = state->leftmost; item; item = item->right) { - offs += state->line_height; - if (offs >= state->height) { - break; - } - state->rightmost = item; - } - } - } else { - // Calculate available space - int padding = state->padding; - int width = state->width - state->inputw - state->promptw - - state->left_arrow - state->right_arrow; - if (state->leftmost == NULL) { - state->leftmost = state->matches; - if (state->rightmost == NULL) { - int offs = 0; - struct menu_item *item; - for (item = state->matches; item->left != state->selection; item = item->right) { - offs += item->width + 2 * padding; - if (offs >= width) { - state->leftmost = item->left; - offs = width - offs; - } - } - } else { - int offs = 0; - struct menu_item *item; - for (item = state->rightmost; item; item = item->left) { - offs += item->width + 2 * padding; - if (offs >= width) { - state->leftmost = item->right; - break; - } - } - } - } - if (state->rightmost == NULL) { - state->rightmost = state->matches; - int offs = 0; - struct menu_item *item; - for (item = state->leftmost; item; item = item->right) { - offs += item->width + 2 * padding; - if (offs >= width) { - break; - } - state->rightmost = item; - } - } - } -} - -void render_to_cairo(struct menu_state *state, cairo_t *cairo) { - int width = state->width; - int padding = state->padding; - - - cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); - cairo_set_source_u32(cairo, state->background); - cairo_paint(cairo); - - int x = 0; - - // Draw prompt - if (state->prompt) { - state->promptw = render_text(state, cairo, state->prompt, - 0, 0, state->width, state->line_height, - state->promptfg, state->promptbg, - padding, padding/2); - x += state->promptw; - } - - // Draw background - cairo_set_source_u32(cairo, state->background); - cairo_rectangle(cairo, x, 0, 300, state->height); - cairo_fill(cairo); - - // Draw input - render_text(state, cairo, state->text, - x, 0, state->width, state->line_height, - state->foreground, 0, padding, padding); - - // Draw cursor - { - int cursor_width = 2; - int cursor_margin = 2; - int cursor_pos = x + padding - + text_width(cairo, state->font, state->text) - - text_width(cairo, state->font, &state->text[state->cursor]) - - cursor_width / 2; - cairo_rectangle(cairo, cursor_pos, cursor_margin, cursor_width, - state->line_height - 2 * cursor_margin); - cairo_fill(cairo); - } - - if (!state->matches) { - return; - } - - if (state->vertical) { - // Draw matches vertically - int y = state->line_height; - struct menu_item *item; - for (item = state->leftmost; item; item = item->right) { - uint32_t bg_color = state->selection == item ? state->selectionbg : state->background; - uint32_t fg_color = state->selection == item ? state->selectionfg : state->foreground; - render_vertical_item(state, cairo, item->text, - x, y, width, state->line_height, - fg_color, bg_color, padding); - y += state->line_height; - if (y >= state->height) { - break; - } - } - } else { - // Leave room for input - x += state->inputw; - - // Calculate scroll indicator widths - state->left_arrow = text_width(cairo, state->font, "<") + 2 * padding; - state->right_arrow = text_width(cairo, state->font, ">") + 2 * padding; - - // Remember scroll indicator position - int left_arrow_pos = x + padding; - x += state->left_arrow; - - // Draw matches horizontally - bool scroll_right = false; - struct menu_item *item; - for (item = state->leftmost; item; item = item->right) { - uint32_t bg_color = state->selection == item ? state->selectionbg : state->background; - uint32_t fg_color = state->selection == item ? state->selectionfg : state->foreground; - x = render_horizontal_item(state, cairo, item->text, - x, 0, width - state->right_arrow, state->line_height, - fg_color, bg_color, padding, padding); - if (x == -1) { - scroll_right = true; - break; - } - } - - // Draw left scroll indicator if necessary - if (state->leftmost != state->matches) { - cairo_move_to(cairo, left_arrow_pos, 0); - pango_printf(cairo, state->font, 1, "<"); - } - - // Draw right scroll indicator if necessary - if (scroll_right) { - cairo_move_to(cairo, width - state->right_arrow + padding, 0); - pango_printf(cairo, state->font, 1, ">"); - } - } -} - -void render_frame(struct menu_state *state) { - cairo_surface_t *recorder = cairo_recording_surface_create( - CAIRO_CONTENT_COLOR_ALPHA, NULL); - cairo_t *cairo = cairo_create(recorder); - cairo_set_antialias(cairo, CAIRO_ANTIALIAS_BEST); - cairo_font_options_t *fo = cairo_font_options_create(); - cairo_set_font_options(cairo, fo); - cairo_font_options_destroy(fo); - cairo_save(cairo); - cairo_set_operator(cairo, CAIRO_OPERATOR_CLEAR); - cairo_paint(cairo); - cairo_restore(cairo); - - render_to_cairo(state, cairo); - - int scale = state->output ? state->output->scale : 1; - state->current = get_next_buffer(state->shm, - state->buffers, state->width, state->height, scale); - if (!state->current) { - goto cleanup; - } - - cairo_t *shm = state->current->cairo; - cairo_save(shm); - cairo_set_operator(shm, CAIRO_OPERATOR_CLEAR); - cairo_paint(shm); - cairo_restore(shm); - cairo_set_source_surface(shm, recorder, 0, 0); - cairo_paint(shm); - - wl_surface_set_buffer_scale(state->surface, scale); - wl_surface_attach(state->surface, state->current->buffer, 0, 0); - wl_surface_damage(state->surface, 0, 0, state->width, state->height); - wl_surface_commit(state->surface); - -cleanup: - cairo_destroy(cairo); -} - -static void noop() { - // Do nothing -} - -static void surface_enter(void *data, struct wl_surface *surface, - struct wl_output *wl_output) { - struct menu_state *state = data; - state->output = wl_output_get_user_data(wl_output); -} - -static const struct wl_surface_listener surface_listener = { - .enter = surface_enter, - .leave = noop, -}; - -static void layer_surface_configure(void *data, - struct zwlr_layer_surface_v1 *surface, - uint32_t serial, uint32_t width, uint32_t height) { - struct menu_state *state = data; - state->width = width; - state->height = height; - zwlr_layer_surface_v1_ack_configure(surface, serial); -} - -static void layer_surface_closed(void *data, - struct zwlr_layer_surface_v1 *surface) { - struct menu_state *state = data; - state->run = false; -} - -struct zwlr_layer_surface_v1_listener layer_surface_listener = { - .configure = layer_surface_configure, - .closed = layer_surface_closed, -}; - -static void output_scale(void *data, struct wl_output *wl_output, int32_t factor) { - struct output *output = data; - output->scale = factor; -} - -static void output_name(void *data, struct wl_output *wl_output, const char *name) { - struct output *output = data; - struct menu_state *state = output->menu; - char *outname = state->output_name; - if (!state->output && outname && strcmp(outname, name) == 0) { - state->output = output; - } -} - -struct wl_output_listener output_listener = { - .geometry = noop, - .mode = noop, - .done = noop, - .scale = output_scale, - .name = output_name, - .description = noop, -}; - -static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, - uint32_t format, int32_t fd, uint32_t size) { - struct menu_state *state = data; - if (format != WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1) { - close(fd); - state->run = false; - state->failure = true; - return; - } - char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); - if (map_shm == MAP_FAILED) { - close(fd); - state->run = false; - state->failure = true; - return; - } - state->xkb_keymap = xkb_keymap_new_from_string(state->xkb_context, - map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, 0); - munmap(map_shm, size); - close(fd); - state->xkb_state = xkb_state_new(state->xkb_keymap); -} - -void keypress(struct menu_state *state, enum wl_keyboard_key_state key_state, - xkb_keysym_t sym) { - if (key_state != WL_KEYBOARD_KEY_STATE_PRESSED) { - return; - } - - bool ctrl = xkb_state_mod_name_is_active(state->xkb_state, - XKB_MOD_NAME_CTRL, - XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); - bool shift = xkb_state_mod_name_is_active(state->xkb_state, - XKB_MOD_NAME_SHIFT, - XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); - - size_t len = strlen(state->text); - - if (ctrl) { - // Emacs-style line editing bindings - switch (sym) { - case XKB_KEY_a: - sym = XKB_KEY_Home; - break; - case XKB_KEY_b: - sym = XKB_KEY_Left; - break; - case XKB_KEY_c: - sym = XKB_KEY_Escape; - break; - case XKB_KEY_d: - sym = XKB_KEY_Delete; - break; - case XKB_KEY_e: - sym = XKB_KEY_End; - break; - case XKB_KEY_f: - sym = XKB_KEY_Right; - break; - case XKB_KEY_g: - sym = XKB_KEY_Escape; - break; - case XKB_KEY_h: - sym = XKB_KEY_BackSpace; - break; - case XKB_KEY_i: - sym = XKB_KEY_Tab; - break; - case XKB_KEY_j: - case XKB_KEY_J: - case XKB_KEY_m: - case XKB_KEY_M: - sym = XKB_KEY_Return; - ctrl = false; - break; - case XKB_KEY_n: - sym = XKB_KEY_Down; - break; - case XKB_KEY_p: - sym = XKB_KEY_Up; - break; - - case XKB_KEY_k: - // Delete right - state->text[state->cursor] = '\0'; - match(state); - render_frame(state); - return; - case XKB_KEY_u: - // Delete left - insert(state, NULL, 0 - state->cursor); - render_frame(state); - return; - case XKB_KEY_w: - // Delete word - while (state->cursor > 0 && state->text[nextrune(state, -1)] == ' ') { - insert(state, NULL, nextrune(state, -1) - state->cursor); - } - while (state->cursor > 0 && state->text[nextrune(state, -1)] != ' ') { - insert(state, NULL, nextrune(state, -1) - state->cursor); - } - render_frame(state); - return; - case XKB_KEY_Y: - // Paste clipboard - if (!state->offer) { - return; - } - - int fds[2]; - pipe(fds); - wl_data_offer_receive(state->offer, "text/plain", fds[1]); - close(fds[1]); - - wl_display_roundtrip(state->display); - - while (true) { - char buf[1024]; - ssize_t n = read(fds[0], buf, sizeof(buf)); - if (n <= 0) { - break; - } - insert(state, buf, n); - } - close(fds[0]); - - wl_data_offer_destroy(state->offer); - state->offer = NULL; - render_frame(state); - return; - case XKB_KEY_Left: - case XKB_KEY_KP_Left: - // Move to beginning of word - while (state->cursor > 0 && state->text[nextrune(state, -1)] == ' ') { - state->cursor = nextrune(state, -1); - } - while (state->cursor > 0 && state->text[nextrune(state, -1)] != ' ') { - state->cursor = nextrune(state, -1); - } - render_frame(state); - return; - case XKB_KEY_Right: - case XKB_KEY_KP_Right: - // Move to end of word - while (state->cursor < len && state->text[state->cursor] == ' ') { - state->cursor = nextrune(state, +1); - } - while (state->cursor < len && state->text[state->cursor] != ' ') { - state->cursor = nextrune(state, +1); - } - render_frame(state); - return; - } - } - - char buf[8]; - switch (sym) { - case XKB_KEY_Return: - case XKB_KEY_KP_Enter: - if (shift) { - puts(state->text); - fflush(stdout); - state->run = false; - } else { - char *text = state->selection ? state->selection->text - : state->text; - puts(text); - fflush(stdout); - if (!ctrl) { - state->run = false; - } - } - break; - case XKB_KEY_Left: - case XKB_KEY_KP_Left: - if (state->vertical) { - break; - } - if (state->cursor && (!state->selection || !state->selection->left)) { - state->cursor = nextrune(state, -1); - render_frame(state); - } - if (state->selection && state->selection->left) { - if (state->selection == state->leftmost) { - state->rightmost = state->selection->left; - state->leftmost = NULL; - } - state->selection = state->selection->left; - scroll_matches(state); - render_frame(state); - } - break; - case XKB_KEY_Right: - case XKB_KEY_KP_Right: - if (state->vertical) { - break; - } - if (state->cursor < len) { - state->cursor = nextrune(state, +1); - render_frame(state); - } else if (state->cursor == len) { - if (state->selection && state->selection->right) { - if (state->selection == state->rightmost) { - state->leftmost = state->selection->right; - state->rightmost = NULL; - } - state->selection = state->selection->right; - scroll_matches(state); - render_frame(state); - } - } - break; - case XKB_KEY_Up: - case XKB_KEY_KP_Up: - if (!state->vertical) { - break; - } - if (state->cursor && (!state->selection || !state->selection->left)) { - state->cursor = nextrune(state, -1); - render_frame(state); - } - if (state->selection && state->selection->left) { - if (state->selection == state->leftmost) { - state->rightmost = state->selection->left; - state->leftmost = NULL; - } - state->selection = state->selection->left; - scroll_matches(state); - render_frame(state); - } - break; - case XKB_KEY_Down: - case XKB_KEY_KP_Down: - if (!state->vertical) { - break; - } - if (state->cursor < len) { - state->cursor = nextrune(state, +1); - render_frame(state); - } else if (state->cursor == len) { - if (state->selection && state->selection->right) { - if (state->selection == state->rightmost) { - state->leftmost = state->selection->right; - state->rightmost = NULL; - } - state->selection = state->selection->right; - scroll_matches(state); - render_frame(state); - } - } - break; - case XKB_KEY_Page_Up: - case XKB_KEY_KP_Page_Up: - if (state->leftmost && state->leftmost->left) { - state->rightmost = state->leftmost->left; - state->leftmost = NULL; - scroll_matches(state); - state->selection = state->leftmost; - render_frame(state); - } - break; - case XKB_KEY_Page_Down: - case XKB_KEY_KP_Page_Down: - if (state->rightmost && state->rightmost->right) { - state->leftmost = state->rightmost->right; - state->rightmost = NULL; - state->selection = state->leftmost; - scroll_matches(state); - render_frame(state); - } - break; - case XKB_KEY_Home: - case XKB_KEY_KP_Home: - if (state->selection == state->matches) { - if (state->cursor != 0) { - state->cursor = 0; - render_frame(state); - } - } else { - state->selection = state->matches; - state->leftmost = state->matches; - state->rightmost = NULL; - scroll_matches(state); - render_frame(state); - } - break; - case XKB_KEY_End: - case XKB_KEY_KP_End: - if (state->cursor < len) { - state->cursor = len; - render_frame(state); - } else { - if (!state->selection || !state->selection->right) { - return; - } - while (state->selection && state->selection->right) { - state->selection = state->selection->right; - } - state->leftmost = NULL; - state->rightmost = state->selection; - scroll_matches(state); - render_frame(state); - } - break; - case XKB_KEY_BackSpace: - if (state->cursor > 0) { - insert(state, NULL, nextrune(state, -1) - state->cursor); - render_frame(state); - } - break; - case XKB_KEY_Delete: - case XKB_KEY_KP_Delete: - if (state->cursor == len) { - return; - } - state->cursor = nextrune(state, +1); - insert(state, NULL, nextrune(state, -1) - state->cursor); - render_frame(state); - break; - case XKB_KEY_Tab: - if (!state->selection) { - return; - } - state->cursor = strnlen(state->selection->text, sizeof state->text - 1); - memcpy(state->text, state->selection->text, state->cursor); - state->text[state->cursor] = '\0'; - match(state); - render_frame(state); - break; - case XKB_KEY_Escape: - state->failure = true; - state->run = false; - break; - default: - if (xkb_keysym_to_utf8(sym, buf, 8)) { - insert(state, buf, strnlen(buf, 8)); - render_frame(state); - } - } -} - -void keyboard_repeat(struct menu_state *state) { - keypress(state, state->repeat_key_state, state->repeat_sym); - struct itimerspec spec = { 0 }; - spec.it_value.tv_sec = state->repeat_period / 1000; - spec.it_value.tv_nsec = (state->repeat_period % 1000) * 1000000l; - timerfd_settime(state->repeat_timer, 0, &spec, NULL); -} - -static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard, - uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) { - struct menu_state *state = data; - - enum wl_keyboard_key_state key_state = _key_state; - xkb_keysym_t sym = xkb_state_key_get_one_sym(state->xkb_state, key + 8); - keypress(state, key_state, sym); - - if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED && state->repeat_period >= 0) { - state->repeat_key_state = key_state; - state->repeat_sym = sym; - - struct itimerspec spec = { 0 }; - spec.it_value.tv_sec = state->repeat_delay / 1000; - spec.it_value.tv_nsec = (state->repeat_delay % 1000) * 1000000l; - timerfd_settime(state->repeat_timer, 0, &spec, NULL); - } else if (key_state == WL_KEYBOARD_KEY_STATE_RELEASED) { - struct itimerspec spec = { 0 }; - timerfd_settime(state->repeat_timer, 0, &spec, NULL); - } -} - -static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, - int32_t rate, int32_t delay) { - struct menu_state *state = data; - state->repeat_delay = delay; - if (rate > 0) { - state->repeat_period = 1000 / rate; - } else { - state->repeat_period = -1; - } -} - -static void keyboard_modifiers(void *data, struct wl_keyboard *keyboard, - uint32_t serial, uint32_t mods_depressed, - uint32_t mods_latched, uint32_t mods_locked, - uint32_t group) { - struct menu_state *state = data; - xkb_state_update_mask(state->xkb_state, mods_depressed, mods_latched, - mods_locked, 0, 0, group); -} - -static const struct wl_keyboard_listener keyboard_listener = { - .keymap = keyboard_keymap, - .enter = noop, - .leave = noop, - .key = keyboard_key, - .modifiers = keyboard_modifiers, - .repeat_info = keyboard_repeat_info, -}; - -static void seat_capabilities(void *data, struct wl_seat *seat, - enum wl_seat_capability caps) { - struct menu_state *state = data; - if (caps & WL_SEAT_CAPABILITY_KEYBOARD) { - struct wl_keyboard *keyboard = wl_seat_get_keyboard(seat); - wl_keyboard_add_listener(keyboard, &keyboard_listener, state); - } -} - -static const struct wl_seat_listener seat_listener = { - .capabilities = seat_capabilities, - .name = noop, -}; - -static void data_device_selection(void *data, struct wl_data_device *data_device, - struct wl_data_offer *offer) { - struct menu_state *state = data; - state->offer = offer; -} - -static const struct wl_data_device_listener data_device_listener = { - .data_offer = noop, - .enter = noop, - .leave = noop, - .motion = noop, - .drop = noop, - .selection = data_device_selection, -}; - -static void handle_global(void *data, struct wl_registry *registry, - uint32_t name, const char *interface, uint32_t version) { - struct menu_state *state = data; - if (strcmp(interface, wl_compositor_interface.name) == 0) { - state->compositor = wl_registry_bind(registry, name, - &wl_compositor_interface, 4); - } else if (strcmp(interface, wl_shm_interface.name) == 0) { - state->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); - } else if (strcmp(interface, wl_seat_interface.name) == 0) { - state->seat = wl_registry_bind(registry, name, &wl_seat_interface, 4); - wl_seat_add_listener(state->seat, &seat_listener, state); - } else if (strcmp(interface, wl_data_device_manager_interface.name) == 0) { - state->data_device_manager = wl_registry_bind(registry, name, - &wl_data_device_manager_interface, 3); - } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { - state->layer_shell = wl_registry_bind(registry, name, - &zwlr_layer_shell_v1_interface, 1); - } else if (strcmp(interface, wl_output_interface.name) == 0) { - struct output *output = calloc(1, sizeof(struct output)); - output->output = wl_registry_bind(registry, name, - &wl_output_interface, 4); - output->menu = state; - output->scale = 1; - wl_output_set_user_data(output->output, output); - wl_output_add_listener(output->output, &output_listener, output); - } -} - -static const struct wl_registry_listener registry_listener = { - .global = handle_global, - .global_remove = noop, -}; - -void insert(struct menu_state *state, const char *s, ssize_t n) { - if (strlen(state->text) + n > sizeof state->text - 1) { - return; - } - memmove(state->text + state->cursor + n, state->text + state->cursor, - sizeof state->text - state->cursor - MAX(n, 0)); - if (n > 0 && s != NULL) { - memcpy(state->text + state->cursor, s, n); - } - state->cursor += n; - match(state); -} - -char * fstrstr(struct menu_state *state, const char *s, const char *sub) { - size_t len; - - for(len = strlen(sub); *s; s++) - if(!state->fstrncmp(s, sub, len)) - return (char *)s; - return NULL; -} - -void append_item(struct menu_item *item, struct menu_item **list, struct menu_item **last) { - if(!*last) - *list = item; - else - (*last)->right = item; - item->left = *last; - item->right = NULL; - *last = item; -} - -void match(struct menu_state *state) { - struct menu_item *item, *itemend, *lexact, *lprefix, *lsubstr, *exactend, *prefixend, *substrend; - - state->matches = NULL; - state->leftmost = NULL; - size_t len = strlen(state->text); - state->matches = lexact = lprefix = lsubstr = itemend = exactend = prefixend = substrend = NULL; - for (item = state->items; item; item = item->next) { - if (!state->fstrncmp(state->text, item->text, len + 1)) { - append_item(item, &lexact, &exactend); - } else if (!state->fstrncmp(state->text, item->text, len)) { - append_item(item, &lprefix, &prefixend); - } else if (fstrstr(state, item->text, state->text)) { - append_item(item, &lsubstr, &substrend); - } - } - - if (lexact) { - state->matches = lexact; - itemend = exactend; - } - if (lprefix) { - if (itemend) { - itemend->right = lprefix; - lprefix->left = itemend; - } else { - state->matches = lprefix; - } - itemend = prefixend; - } - if (lsubstr) { - if (itemend) { - itemend->right = lsubstr; - lsubstr->left = itemend; - itemend = substrend; - } else { - state->matches = lsubstr; - } - } - state->selection = state->matches; - state->leftmost = state->matches; - state->rightmost = NULL; - scroll_matches(state); -} - -size_t nextrune(struct menu_state *state, int incr) { - size_t n, len; - - len = strlen(state->text); - for(n = state->cursor + incr; n < len && (state->text[n] & 0xc0) == 0x80; n += incr); - return n; -} - -void read_stdin(struct menu_state *state) { - char buf[sizeof state->text], *p; - struct menu_item *item, **end; - - for(end = &state->items; fgets(buf, sizeof buf, stdin); *end = item, end = &item->next) { - if((p = strchr(buf, '\n'))) { - *p = '\0'; - } - item = malloc(sizeof *item); - if (!item) { - return; - } - - item->text = strdup(buf); - item->next = item->left = item->right = NULL; - - cairo_t *cairo = state->current->cairo; - item->width = text_width(cairo, state->font, item->text); - if (item->width > state->inputw) { - state->inputw = item->width; - } - } -} - -static void menu_init(struct menu_state *state) { - int height = get_font_height(state->font); - state->line_height = height + 3; - state->height = state->line_height; - if (state->vertical) { - state->height += state->height * state->lines; - } - state->padding = height / 2; - - state->display = wl_display_connect(NULL); - if (!state->display) { - fprintf(stderr, "wl_display_connect: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - state->xkb_context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); - if (!state->xkb_context) { - fprintf(stderr, "xkb_context_new: %s\n", strerror(errno)); - exit(EXIT_FAILURE); - } - - state->repeat_timer = timerfd_create(CLOCK_MONOTONIC, 0); - assert(state->repeat_timer >= 0); - - struct wl_registry *registry = wl_display_get_registry(state->display); - wl_registry_add_listener(registry, ®istry_listener, state); - wl_display_roundtrip(state->display); - assert(state->compositor != NULL); - assert(state->shm != NULL); - assert(state->seat != NULL); - assert(state->data_device_manager != NULL); - assert(state->layer_shell != NULL); - - // Get data device for seat - struct wl_data_device *data_device = wl_data_device_manager_get_data_device( - state->data_device_manager, state->seat); - wl_data_device_add_listener(data_device, &data_device_listener, state); - - // Second roundtrip for xdg-output - wl_display_roundtrip(state->display); - - if (state->output_name && !state->output) { - fprintf(stderr, "Output %s not found\n", state->output_name); - exit(EXIT_FAILURE); - } -} - -static void menu_create_surface(struct menu_state *state) { - state->surface = wl_compositor_create_surface(state->compositor); - wl_surface_add_listener(state->surface, &surface_listener, state); - state->layer_surface = zwlr_layer_shell_v1_get_layer_surface( - state->layer_shell, - state->surface, - NULL, - ZWLR_LAYER_SHELL_V1_LAYER_TOP, - "menu" - ); - assert(state->layer_surface != NULL); - - uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | - ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; - if (state->bottom) { - anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; - } else { - anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; - } - - zwlr_layer_surface_v1_set_anchor(state->layer_surface, anchor); - zwlr_layer_surface_v1_set_size(state->layer_surface, 0, state->height); - zwlr_layer_surface_v1_set_exclusive_zone(state->layer_surface, -1); - zwlr_layer_surface_v1_set_keyboard_interactivity(state->layer_surface, true); - zwlr_layer_surface_v1_add_listener(state->layer_surface, - &layer_surface_listener, state); - - wl_surface_commit(state->surface); - wl_display_roundtrip(state->display); -} - -bool parse_color(const char *color, uint32_t *result) { - if (color[0] == '#') { - ++color; - } - int len = strlen(color); - if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) { - return false; - } - char *ptr; - uint32_t parsed = strtoul(color, &ptr, 16); - if (*ptr != '\0') { - return false; - } - *result = len == 6 ? ((parsed << 8) | 0xFF) : parsed; - return true; -} - -int main(int argc, char **argv) { - struct menu_state state = { - .fstrncmp = strncmp, - .font = "monospace 10", - .vertical = false, - .background = 0x222222ff, - .foreground = 0xbbbbbbff, - .promptbg = 0x005577ff, - .promptfg = 0xeeeeeeff, - .selectionbg = 0x005577ff, - .selectionfg = 0xeeeeeeff, - .run = true, - }; - - const char *usage = - "Usage: wmenu [-biv] [-f font] [-l lines] [-o output] [-p prompt]\n" - "\t[-N color] [-n color] [-M color] [-m color] [-S color] [-s color]\n"; - - int opt; - while ((opt = getopt(argc, argv, "bhivf:l:o:p:N:n:M:m:S:s:")) != -1) { - switch (opt) { - case 'b': - state.bottom = true; - break; - case 'i': - state.fstrncmp = strncasecmp; - break; - case 'v': - puts("wmenu " VERSION); - exit(EXIT_SUCCESS); - case 'f': - state.font = optarg; - break; - case 'l': - state.vertical = true; - state.lines = atoi(optarg); - break; - case 'o': - state.output_name = optarg; - break; - case 'p': - state.prompt = optarg; - break; - case 'N': - if (!parse_color(optarg, &state.background)) { - fprintf(stderr, "Invalid background color: %s", optarg); - } - break; - case 'n': - if (!parse_color(optarg, &state.foreground)) { - fprintf(stderr, "Invalid foreground color: %s", optarg); - } - break; - case 'M': - if (!parse_color(optarg, &state.promptbg)) { - fprintf(stderr, "Invalid prompt background color: %s", optarg); - } - break; - case 'm': - if (!parse_color(optarg, &state.promptfg)) { - fprintf(stderr, "Invalid prompt foreground color: %s", optarg); - } - break; - case 'S': - if (!parse_color(optarg, &state.selectionbg)) { - fprintf(stderr, "Invalid selection background color: %s", optarg); - } - break; - case 's': - if (!parse_color(optarg, &state.selectionfg)) { - fprintf(stderr, "Invalid selection foreground color: %s", optarg); - } - break; - default: - fprintf(stderr, "%s", usage); - exit(EXIT_FAILURE); - } - } - - if (optind < argc) { - fprintf(stderr, "%s", usage); - exit(EXIT_FAILURE); - } - - menu_init(&state); - menu_create_surface(&state); - render_frame(&state); - - read_stdin(&state); - match(&state); - render_frame(&state); - - struct pollfd fds[] = { - { wl_display_get_fd(state.display), POLLIN }, - { state.repeat_timer, POLLIN }, - }; - const int nfds = sizeof(fds) / sizeof(*fds); - - while (state.run) { - errno = 0; - do { - if (wl_display_flush(state.display) == -1 && errno != EAGAIN) { - fprintf(stderr, "wl_display_flush: %s\n", strerror(errno)); - break; - } - } while (errno == EAGAIN); - - if (poll(fds, nfds, -1) < 0) { - fprintf(stderr, "poll: %s\n", strerror(errno)); - break; - } - - if (fds[0].revents & POLLIN) { - if (wl_display_dispatch(state.display) < 0) { - state.run = false; - } - } - - if (fds[1].revents & POLLIN) { - keyboard_repeat(&state); - } - } - - wl_display_disconnect(state.display); - - if (state.failure) { - return EXIT_FAILURE; - } - return EXIT_SUCCESS; -} diff --git a/menu.c b/menu.c new file mode 100644 index 0000000..207d71c --- /dev/null +++ b/menu.c @@ -0,0 +1,691 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "menu.h" + +#include "pango.h" +#include "render.h" +#include "wayland.h" + +// Creates and returns a new menu. +struct menu *menu_create(menu_callback callback) { + struct menu *menu = calloc(1, sizeof(struct menu)); + menu->strncmp = strncmp; + menu->font = "monospace 10"; + menu->normalbg = 0x222222ff; + menu->normalfg = 0xbbbbbbff; + menu->promptbg = 0x005577ff; + menu->promptfg = 0xeeeeeeff; + menu->selectionbg = 0x005577ff; + menu->selectionfg = 0xeeeeeeff; + menu->callback = callback; + menu->test_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1); + menu->test_cairo = cairo_create(menu->test_surface); + return menu; +} + +static void free_pages(struct menu *menu) { + struct page *next = menu->pages; + while (next) { + struct page *page = next; + next = page->next; + free(page); + } +} + +static void free_items(struct menu *menu) { + for (size_t i = 0; i < menu->item_count; i++) { + struct item *item = &menu->items[i]; + free(item->text); + } + free(menu->items); +} + +// Destroys the menu, freeing memory associated with it. +void menu_destroy(struct menu *menu) { + free_pages(menu); + free_items(menu); + cairo_destroy(menu->test_cairo); + cairo_surface_destroy(menu->test_surface); + free(menu); +} + +static bool parse_color(const char *color, uint32_t *result) { + if (color[0] == '#') { + ++color; + } + size_t len = strlen(color); + if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) { + return false; + } + char *ptr; + uint32_t parsed = (uint32_t)strtoul(color, &ptr, 16); + if (*ptr != '\0') { + return false; + } + *result = len == 6 ? ((parsed << 8) | 0xFF) : parsed; + return true; +} + +// Parse menu options from command line arguments. +void menu_getopts(struct menu *menu, int argc, char *argv[]) { + const char *usage = + "Usage: wmenu [-biPv] [-f font] [-l lines] [-o output] [-p prompt]\n" + "\t[-N color] [-n color] [-M color] [-m color] [-S color] [-s color]\n"; + + int opt; + while ((opt = getopt(argc, argv, "bhiPvf:l:o:p:N:n:M:m:S:s:")) != -1) { + switch (opt) { + case 'b': + menu->bottom = true; + break; + case 'i': + menu->strncmp = strncasecmp; + break; + case 'P': + menu->passwd = true; + break; + case 'v': + puts("wmenu " VERSION); + exit(EXIT_SUCCESS); + case 'f': + menu->font = optarg; + break; + case 'l': + menu->lines = atoi(optarg); + break; + case 'o': + menu->output_name = optarg; + break; + case 'p': + menu->prompt = optarg; + break; + case 'N': + if (!parse_color(optarg, &menu->normalbg)) { + fprintf(stderr, "Invalid background color: %s", optarg); + } + break; + case 'n': + if (!parse_color(optarg, &menu->normalfg)) { + fprintf(stderr, "Invalid foreground color: %s", optarg); + } + break; + case 'M': + if (!parse_color(optarg, &menu->promptbg)) { + fprintf(stderr, "Invalid prompt background color: %s", optarg); + } + break; + case 'm': + if (!parse_color(optarg, &menu->promptfg)) { + fprintf(stderr, "Invalid prompt foreground color: %s", optarg); + } + break; + case 'S': + if (!parse_color(optarg, &menu->selectionbg)) { + fprintf(stderr, "Invalid selection background color: %s", optarg); + } + break; + case 's': + if (!parse_color(optarg, &menu->selectionfg)) { + fprintf(stderr, "Invalid selection foreground color: %s", optarg); + } + break; + default: + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); + } + } + + if (optind < argc) { + fprintf(stderr, "%s", usage); + exit(EXIT_FAILURE); + } + + int height = get_font_height(menu->font); + menu->line_height = height + 2; + menu->height = menu->line_height; + if (menu->lines > 0) { + menu->height += menu->height * menu->lines; + } + menu->padding = height / 2; +} + +// Add an item to the menu. +void menu_add_item(struct menu *menu, char *text) { + if ((menu->item_count & (menu->item_count - 1)) == 0) { + size_t alloc_size = menu->item_count ? 2 * menu->item_count : 1; + void *new_array = realloc(menu->items, sizeof(struct item) * alloc_size); + if (!new_array) { + fprintf(stderr, "could not realloc %zu bytes", sizeof(struct item) * alloc_size); + exit(EXIT_FAILURE); + } + menu->items = new_array; + } + + struct item *new = &menu->items[menu->item_count]; + new->text = text; + + menu->item_count++; +} + +static int compare_items(const void *a, const void *b) { + const struct item *item_a = a; + const struct item *item_b = b; + return strcmp(item_a->text, item_b->text); +} + +void menu_sort_and_deduplicate(struct menu *menu) { + size_t j = 1; + size_t i; + + qsort(menu->items, menu->item_count, sizeof(*menu->items), compare_items); + + for (i = 1; i < menu->item_count; i++) { + if (strcmp(menu->items[i].text, menu->items[j - 1].text) == 0) { + free(menu->items[i].text); + } else { + menu->items[j] = menu->items[i]; + j++; + } + } + menu->item_count = j; +} + +static void append_page(struct page *page, struct page **first, struct page **last) { + if (*last) { + (*last)->next = page; + } else { + *first = page; + } + page->prev = *last; + page->next = NULL; + *last = page; +} + +static void page_items(struct menu *menu) { + // Free existing pages + while (menu->pages != NULL) { + struct page *page = menu->pages; + menu->pages = menu->pages->next; + free(page); + } + + if (!menu->matches) { + return; + } + + // Make new pages + if (menu->lines > 0) { + struct page *pages_end = NULL; + struct item *item = menu->matches; + while (item) { + struct page *page = calloc(1, sizeof(struct page)); + page->first = item; + + for (int i = 1; item && i <= menu->lines; i++) { + item->page = page; + page->last = item; + item = item->next_match; + } + append_page(page, &menu->pages, &pages_end); + } + } else { + // Calculate available space + int max_width = menu->width - menu->inputw - menu->promptw + - menu->left_arrow - menu->right_arrow; + + struct page *pages_end = NULL; + struct item *item = menu->matches; + while (item) { + struct page *page = calloc(1, sizeof(struct page)); + page->first = item; + + int total_width = 0; + int items = 0; + while (item) { + total_width += item->width + 2 * menu->padding; + if (total_width > max_width && items > 0) { + break; + } + items++; + + item->page = page; + page->last = item; + item = item->next_match; + } + append_page(page, &menu->pages, &pages_end); + } + } +} + +static const char *fstrstr(struct menu *menu, const char *s, const char *sub) { + for (size_t len = strlen(sub); *s; s++) { + if (!menu->strncmp(s, sub, len)) { + return s; + } + } + return NULL; +} + +static void append_match(struct item *item, struct item **first, struct item **last) { + if (*last) { + (*last)->next_match = item; + } else { + *first = item; + } + item->prev_match = *last; + item->next_match = NULL; + *last = item; +} + +static void match_items(struct menu *menu) { + struct item *lexact = NULL, *exactend = NULL; + struct item *lprefix = NULL, *prefixend = NULL; + struct item *lsubstr = NULL, *substrend = NULL; + char buf[sizeof menu->input], *tok; + char **tokv = NULL; + int i, tokc = 0; + size_t k; + size_t tok_len; + menu->matches = NULL; + menu->matches_end = NULL; + menu->sel = NULL; + + size_t input_len = strlen(menu->input); + + /* tokenize input by space for matching the tokens individually */ + strcpy(buf, menu->input); + tok = strtok(buf, " "); + while (tok) { + tokv = realloc(tokv, (tokc + 1) * sizeof *tokv); + if (!tokv) { + fprintf(stderr, "could not realloc %zu bytes", + (tokc + 1) * sizeof *tokv); + exit(EXIT_FAILURE); + } + tokv[tokc] = tok; + tokc++; + tok = strtok(NULL, " "); + } + tok_len = tokc ? strlen(tokv[0]) : 0; + + for (k = 0; k < menu->item_count; k++) { + struct item *item = &menu->items[k]; + for (i = 0; i < tokc; i++) { + if (!fstrstr(menu, item->text, tokv[i])) { + /* token does not match */ + break; + } + } + if (i != tokc) { + /* not all tokens match */ + continue; + } + if (!tokc || !menu->strncmp(menu->input, item->text, input_len + 1)) { + append_match(item, &lexact, &exactend); + } else if (!menu->strncmp(tokv[0], item->text, tok_len)) { + append_match(item, &lprefix, &prefixend); + } else { + append_match(item, &lsubstr, &substrend); + } + } + + free(tokv); + + if (lexact) { + menu->matches = lexact; + menu->matches_end = exactend; + } + if (lprefix) { + if (menu->matches_end) { + menu->matches_end->next_match = lprefix; + lprefix->prev_match = menu->matches_end; + } else { + menu->matches = lprefix; + } + menu->matches_end = prefixend; + } + if (lsubstr) { + if (menu->matches_end) { + menu->matches_end->next_match = lsubstr; + lsubstr->prev_match = menu->matches_end; + } else { + menu->matches = lsubstr; + } + menu->matches_end = substrend; + } + + page_items(menu); + if (menu->pages) { + menu->sel = menu->pages->first; + } +} + +// Marks the menu as needing to be rendered again. +void menu_invalidate(struct menu *menu) { + menu->rendered = false; +} + +// Render menu items. +void menu_render_items(struct menu *menu) { + calc_widths(menu); + match_items(menu); + render_menu(menu); +} + +static void insert(struct menu *menu, const char *text, ssize_t len) { + if (strlen(menu->input) + len > sizeof menu->input - 1) { + return; + } + memmove(menu->input + menu->cursor + len, menu->input + menu->cursor, + sizeof menu->input - menu->cursor - MAX(len, 0)); + if (len > 0 && text != NULL) { + memcpy(menu->input + menu->cursor, text, len); + } + menu->cursor += len; +} + +// Add pasted text to the menu input. +void menu_paste(struct menu *menu, const char *text, ssize_t len) { + insert(menu, text, len); +} + +static size_t nextrune(struct menu *menu, int incr) { + size_t n, len; + + len = strlen(menu->input); + for(n = menu->cursor + incr; n < len && (menu->input[n] & 0xc0) == 0x80; n += incr); + return n; +} + +// Move the cursor to the beginning or end of the word, skipping over any preceding whitespace. +static void movewordedge(struct menu *menu, int dir) { + if (dir < 0) { + // Move to beginning of word + while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] == ' ') { + menu->cursor = nextrune(menu, -1); + } + while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] != ' ') { + menu->cursor = nextrune(menu, -1); + } + } else { + // Move to end of word + size_t len = strlen(menu->input); + while (menu->cursor < len && menu->input[menu->cursor] == ' ') { + menu->cursor = nextrune(menu, +1); + } + while (menu->cursor < len && menu->input[menu->cursor] != ' ') { + menu->cursor = nextrune(menu, +1); + } + } +} + +// Handle a keypress. +void menu_keypress(struct menu *menu, enum wl_keyboard_key_state key_state, + xkb_keysym_t sym) { + if (key_state != WL_KEYBOARD_KEY_STATE_PRESSED) { + return; + } + + struct xkb_state *state = context_get_xkb_state(menu->context); + bool ctrl = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL, + XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); + bool meta = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT, + XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); + bool shift = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT, + XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED); + + size_t len = strlen(menu->input); + + if (ctrl) { + // Emacs-style line editing bindings + switch (sym) { + case XKB_KEY_a: + sym = XKB_KEY_Home; + break; + case XKB_KEY_b: + sym = XKB_KEY_Left; + break; + case XKB_KEY_c: + sym = XKB_KEY_Escape; + break; + case XKB_KEY_d: + sym = XKB_KEY_Delete; + break; + case XKB_KEY_e: + sym = XKB_KEY_End; + break; + case XKB_KEY_f: + sym = XKB_KEY_Right; + break; + case XKB_KEY_g: + sym = XKB_KEY_Escape; + break; + case XKB_KEY_bracketleft: + sym = XKB_KEY_Escape; + break; + case XKB_KEY_h: + sym = XKB_KEY_BackSpace; + break; + case XKB_KEY_i: + sym = XKB_KEY_Tab; + break; + case XKB_KEY_j: + case XKB_KEY_J: + case XKB_KEY_m: + case XKB_KEY_M: + sym = XKB_KEY_Return; + ctrl = false; + break; + case XKB_KEY_n: + sym = XKB_KEY_Down; + break; + case XKB_KEY_p: + sym = XKB_KEY_Up; + break; + + case XKB_KEY_k: + // Delete right + menu->input[menu->cursor] = '\0'; + match_items(menu); + menu_invalidate(menu); + return; + case XKB_KEY_u: + // Delete left + insert(menu, NULL, 0 - menu->cursor); + match_items(menu); + menu_invalidate(menu); + return; + case XKB_KEY_w: + // Delete word + while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] == ' ') { + insert(menu, NULL, nextrune(menu, -1) - menu->cursor); + } + while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] != ' ') { + insert(menu, NULL, nextrune(menu, -1) - menu->cursor); + } + match_items(menu); + menu_invalidate(menu); + return; + case XKB_KEY_Y: + // Paste clipboard + if (!context_paste(menu->context)) { + return; + } + match_items(menu); + menu_invalidate(menu); + return; + case XKB_KEY_Left: + case XKB_KEY_KP_Left: + movewordedge(menu, -1); + menu_invalidate(menu); + return; + case XKB_KEY_Right: + case XKB_KEY_KP_Right: + movewordedge(menu, +1); + menu_invalidate(menu); + return; + + case XKB_KEY_Return: + case XKB_KEY_KP_Enter: + break; + default: + return; + } + } else if (meta) { + // Emacs-style line editing bindings + switch (sym) { + case XKB_KEY_b: + movewordedge(menu, -1); + menu_invalidate(menu); + return; + case XKB_KEY_f: + movewordedge(menu, +1); + menu_invalidate(menu); + return; + case XKB_KEY_g: + sym = XKB_KEY_Home; + break; + case XKB_KEY_G: + sym = XKB_KEY_End; + break; + case XKB_KEY_h: + sym = XKB_KEY_Up; + break; + case XKB_KEY_j: + sym = XKB_KEY_Next; + break; + case XKB_KEY_k: + sym = XKB_KEY_Prior; + break; + case XKB_KEY_l: + sym = XKB_KEY_Down; + break; + default: + return; + } + } + + char buf[8]; + switch (sym) { + case XKB_KEY_Return: + case XKB_KEY_KP_Enter: + if (shift) { + menu->callback(menu, menu->input, true); + } else { + char *text = menu->sel ? menu->sel->text : menu->input; + menu->callback(menu, text, !ctrl); + } + break; + case XKB_KEY_Left: + case XKB_KEY_KP_Left: + case XKB_KEY_Up: + case XKB_KEY_KP_Up: + if (menu->sel && menu->sel->prev_match) { + menu->sel = menu->sel->prev_match; + menu_invalidate(menu); + } else if (menu->cursor > 0) { + menu->cursor = nextrune(menu, -1); + menu_invalidate(menu); + } + break; + case XKB_KEY_Right: + case XKB_KEY_KP_Right: + case XKB_KEY_Down: + case XKB_KEY_KP_Down: + if (menu->cursor < len) { + menu->cursor = nextrune(menu, +1); + menu_invalidate(menu); + } else if (menu->sel && menu->sel->next_match) { + menu->sel = menu->sel->next_match; + menu_invalidate(menu); + } + break; + case XKB_KEY_Prior: + case XKB_KEY_KP_Prior: + if (menu->sel && menu->sel->page->prev) { + menu->sel = menu->sel->page->prev->first; + menu_invalidate(menu); + } + break; + case XKB_KEY_Next: + case XKB_KEY_KP_Next: + if (menu->sel && menu->sel->page->next) { + menu->sel = menu->sel->page->next->first; + menu_invalidate(menu); + } + break; + case XKB_KEY_Home: + case XKB_KEY_KP_Home: + if (menu->sel == menu->matches) { + menu->cursor = 0; + menu_invalidate(menu); + } else { + menu->sel = menu->matches; + menu_invalidate(menu); + } + break; + case XKB_KEY_End: + case XKB_KEY_KP_End: + if (menu->cursor < len) { + menu->cursor = len; + menu_invalidate(menu); + } else { + menu->sel = menu->matches_end; + menu_invalidate(menu); + } + break; + case XKB_KEY_BackSpace: + if (menu->cursor > 0) { + insert(menu, NULL, nextrune(menu, -1) - menu->cursor); + match_items(menu); + menu_invalidate(menu); + } + break; + case XKB_KEY_Delete: + case XKB_KEY_KP_Delete: + if (menu->cursor == len) { + return; + } + menu->cursor = nextrune(menu, +1); + insert(menu, NULL, nextrune(menu, -1) - menu->cursor); + match_items(menu); + menu_invalidate(menu); + break; + case XKB_KEY_Tab: + if (!menu->sel) { + return; + } + menu->cursor = strnlen(menu->sel->text, sizeof menu->input - 1); + memcpy(menu->input, menu->sel->text, menu->cursor); + menu->input[menu->cursor] = '\0'; + match_items(menu); + menu_invalidate(menu); + break; + case XKB_KEY_Escape: + menu->exit = true; + menu->failure = true; + break; + default: + if (xkb_keysym_to_utf8(sym, buf, 8)) { + insert(menu, buf, strnlen(buf, 8)); + match_items(menu); + menu_invalidate(menu); + } + } +} diff --git a/menu.h b/menu.h new file mode 100644 index 0000000..fdd9ad2 --- /dev/null +++ b/menu.h @@ -0,0 +1,95 @@ +#ifndef WMENU_MENU_H +#define WMENU_MENU_H + +#include +#include +#include +#include +#include + +struct menu; +typedef void (*menu_callback)(struct menu *menu, char *text, bool exit); + +// A menu item. +struct item { + char *text; + int width; + struct item *prev_match; // previous matching item + struct item *next_match; // next matching item + struct page *page; // the page holding this item +}; + +// A page of menu items. +struct page { + struct item *first; // first item in the page + struct item *last; // last item in the page + struct page *prev; // previous page + struct page *next; // next page +}; + +// Menu state. +struct menu { + // Whether the menu appears at the bottom of the screen + bool bottom; + // The function used to match menu items + int (*strncmp)(const char *, const char *, size_t); + // Whether the input is a password + bool passwd; + // The font used to display the menu + char *font; + // The number of lines to list items vertically + int lines; + // The name of the output to display on + char *output_name; + // The prompt displayed to the left of the input field + char *prompt; + // Normal colors + uint32_t normalbg, normalfg; + // Prompt colors + uint32_t promptbg, promptfg; + // Selection colors + uint32_t selectionbg, selectionfg; + + struct wl_context *context; + + // 1x1 surface used estimate text sizes with pango + cairo_surface_t *test_surface; + cairo_t *test_cairo; + + int width; + int height; + int line_height; + int padding; + int inputw; + int promptw; + int left_arrow; + int right_arrow; + bool rendered; + + char input[BUFSIZ]; + size_t cursor; + + struct item *items; // array of all items + size_t item_count; + struct item *matches; // list of matching items + struct item *matches_end; // last matching item + struct item *sel; // selected item + struct page *pages; // list of pages + + menu_callback callback; + bool exit; + bool failure; +}; + +struct menu *menu_create(menu_callback callback); +void menu_destroy(struct menu *menu); +void menu_getopts(struct menu *menu, int argc, char *argv[]); +void menu_add_item(struct menu *menu, char *text); +void menu_sort_and_deduplicate(struct menu *menu); +void menu_invalidate(struct menu *menu); +void menu_render_items(struct menu *menu); +void menu_paste(struct menu *menu, const char *text, ssize_t len); +void menu_keypress(struct menu *menu, enum wl_keyboard_key_state key_state, + xkb_keysym_t sym); + +#endif diff --git a/meson.build b/meson.build index 5607d42..3a5cb18 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project( 'wmenu', 'c', - version: '0.1.5', + version: '0.2.0', license: 'MIT', default_options: [ 'c_std=c11', @@ -36,9 +36,35 @@ subdir('docs') executable( 'wmenu', files( - 'main.c', + 'menu.c', 'pango.c', 'pool-buffer.c', + 'render.c', + 'wayland.c', + 'wmenu.c', + ), + dependencies: [ + cairo, + client_protos, + pango, + pangocairo, + rt, + wayland_client, + wayland_protos, + xkbcommon, + ], + install: true, +) + +executable( + 'wmenu-run', + files( + 'menu.c', + 'pango.c', + 'pool-buffer.c', + 'render.c', + 'wayland.c', + 'wmenu-run.c', ), dependencies: [ cairo, diff --git a/pango.c b/pango.c index 122b050..07d4c5b 100644 --- a/pango.c +++ b/pango.c @@ -1,24 +1,28 @@ #include #include -#include #include -#include #include #include #include -int get_font_height(char *fontstr) { +#include "pango.h" + +int get_font_height(const char *fontstr) { PangoFontMap *fontmap = pango_cairo_font_map_get_default(); PangoContext *context = pango_font_map_create_context(fontmap); PangoFontDescription *desc = pango_font_description_from_string(fontstr); PangoFont *font = pango_font_map_load_font(fontmap, context, desc); if (font == NULL) { + pango_font_description_free(desc); + g_object_unref(context); return -1; } PangoFontMetrics *metrics = pango_font_get_metrics(font, NULL); int height = pango_font_metrics_get_height(metrics) / PANGO_SCALE; - pango_font_description_free(desc); pango_font_metrics_unref(metrics); + g_object_unref(font); + pango_font_description_free(desc); + g_object_unref(context); return height; } @@ -32,8 +36,8 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, pango_layout_set_font_description(layout, desc); pango_layout_set_single_paragraph_mode(layout, 1); pango_layout_set_attributes(layout, attrs); - pango_attr_list_unref(attrs); pango_font_description_free(desc); + pango_attr_list_unref(attrs); return layout; } diff --git a/pango.h b/pango.h index 563ac84..482d49a 100644 --- a/pango.h +++ b/pango.h @@ -1,8 +1,6 @@ -#ifndef DMENU_PANGO_H -#define DMENU_PANGO_H -#include +#ifndef WMENU_PANGO_H +#define WMENU_PANGO_H #include -#include #include #include diff --git a/pool-buffer.c b/pool-buffer.c index aca95a1..daa84fd 100644 --- a/pool-buffer.c +++ b/pool-buffer.c @@ -11,6 +11,7 @@ #include #include #include + #include "pool-buffer.h" static void randname(char *buf) { @@ -68,19 +69,19 @@ static const struct wl_buffer_listener buffer_listener = { static struct pool_buffer *create_buffer(struct wl_shm *shm, struct pool_buffer *buf, int32_t width, int32_t height, int32_t scale, uint32_t format) { - uint32_t stride = width * scale * 4; - size_t size = stride * height * scale; + int32_t stride = width * scale * 4; + int32_t size = stride * height * scale; int fd = create_shm_file(size); assert(fd != -1); - void *data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + void *data = mmap(NULL, (size_t)size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); struct wl_shm_pool *pool = wl_shm_create_pool(shm, fd, size); buf->buffer = wl_shm_pool_create_buffer(pool, 0, width * scale, height * scale, stride, format); wl_shm_pool_destroy(pool); close(fd); - buf->size = size; + buf->size = (size_t)size; buf->width = width; buf->height = height; buf->scale = scale; diff --git a/pool-buffer.h b/pool-buffer.h index 67a643c..e07802d 100644 --- a/pool-buffer.h +++ b/pool-buffer.h @@ -1,4 +1,7 @@ /* Taken from sway. MIT licensed */ +#ifndef WMENU_POOL_BUFFER_H +#define WMENU_POOL_BUFFER_H + #include #include #include @@ -12,10 +15,12 @@ struct pool_buffer { PangoContext *pango; size_t size; int32_t width, height, scale; - void *data; bool busy; + void *data; }; struct pool_buffer *get_next_buffer(struct wl_shm *shm, struct pool_buffer pool[static 2], int32_t width, int32_t height, int32_t scale); void destroy_buffer(struct pool_buffer *buffer); + +#endif diff --git a/protocols/meson.build b/protocols/meson.build index fb7ce6a..5c9973d 100644 --- a/protocols/meson.build +++ b/protocols/meson.build @@ -12,6 +12,7 @@ endif protocols = [ [wl_protocol_dir, 'stable/xdg-shell/xdg-shell.xml'], + [wl_protocol_dir, 'staging/xdg-activation/xdg-activation-v1.xml'], ['wlr-layer-shell-unstable-v1.xml'], ] diff --git a/render.c b/render.c new file mode 100644 index 0000000..e86d93b --- /dev/null +++ b/render.c @@ -0,0 +1,212 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include + +#include "render.h" + +#include "menu.h" +#include "pango.h" +#include "pool-buffer.h" +#include "wayland.h" + +// Calculate text widths. +void calc_widths(struct menu *menu) { + struct wl_context *context = menu->context; + int scale = context_get_scale(context); + cairo_surface_set_device_scale(menu->test_surface, scale, scale); + cairo_set_antialias(menu->test_cairo, CAIRO_ANTIALIAS_BEST); + cairo_font_options_t *fo = cairo_font_options_create(); + cairo_set_font_options(menu->test_cairo, fo); + cairo_font_options_destroy(fo); + cairo_t *cairo = menu->test_cairo; + + // Calculate prompt width + if (menu->prompt) { + menu->promptw = text_width(cairo, menu->font, menu->prompt) + menu->padding + menu->padding/2; + } else { + menu->promptw = 0; + } + + // Calculate scroll indicator widths + menu->left_arrow = text_width(cairo, menu->font, "<") + 2 * menu->padding; + menu->right_arrow = text_width(cairo, menu->font, ">") + 2 * menu->padding; + + // Calculate item widths and input area width + for (size_t i = 0; i < menu->item_count; i++) { + struct item *item = &menu->items[i]; + item->width = text_width(cairo, menu->font, item->text); + if (item->width > menu->inputw) { + menu->inputw = item->width; + } + } +} + +static void cairo_set_source_u32(cairo_t *cairo, uint32_t color) { + cairo_set_source_rgba(cairo, + (color >> (3*8) & 0xFF) / 255.0, + (color >> (2*8) & 0xFF) / 255.0, + (color >> (1*8) & 0xFF) / 255.0, + (color >> (0*8) & 0xFF) / 255.0); +} + +// Renders text to cairo. +static int render_text(struct menu *menu, cairo_t *cairo, const char *str, + int x, int y, int width, uint32_t bg_color, uint32_t fg_color, + int left_padding, int right_padding) { + + int text_width, text_height; + get_text_size(cairo, menu->font, &text_width, &text_height, NULL, 1, str); + int text_y = (menu->line_height / 2.0) - (text_height / 2.0); + + if (width == 0) { + width = text_width + left_padding + right_padding; + } + if (bg_color) { + cairo_set_source_u32(cairo, bg_color); + cairo_rectangle(cairo, x, y, width, menu->line_height); + cairo_fill(cairo); + } + cairo_move_to(cairo, x + left_padding, y + text_y); + cairo_set_source_u32(cairo, fg_color); + pango_printf(cairo, menu->font, 1, str); + + return width; +} + +// Renders the prompt message. +static void render_prompt(struct menu *menu, cairo_t *cairo) { + if (!menu->prompt) { + return; + } + render_text(menu, cairo, menu->prompt, 0, 0, 0, + menu->promptbg, menu->promptfg, menu->padding, menu->padding/2); +} + +// Renders the input text. +static void render_input(struct menu *menu, cairo_t *cairo) { + char *censort = NULL; + + if (menu->passwd) { + censort = calloc(1, sizeof(menu->input)); + if (!censort) { + return; + } + memset(censort, '*', strlen(menu->input)); + } + + render_text(menu, cairo, menu->passwd ? censort : menu->input, + menu->promptw, 0, 0, 0, menu->normalfg, menu->padding, menu->padding); + + if (censort) { + free(censort); + } +} + +// Renders a cursor for the input field. +static void render_cursor(struct menu *menu, cairo_t *cairo) { + const int cursor_width = 2; + const int cursor_margin = 2; + int cursor_pos = menu->promptw + menu->padding + + text_width(cairo, menu->font, menu->input) + - text_width(cairo, menu->font, &menu->input[menu->cursor]) + - cursor_width / 2; + cairo_rectangle(cairo, cursor_pos, cursor_margin, cursor_width, + menu->line_height - 2 * cursor_margin); + cairo_fill(cairo); +} + +// Renders a single menu item horizontally. +static int render_horizontal_item(struct menu *menu, cairo_t *cairo, struct item *item, int x) { + uint32_t bg_color = menu->sel == item ? menu->selectionbg : menu->normalbg; + uint32_t fg_color = menu->sel == item ? menu->selectionfg : menu->normalfg; + + return render_text(menu, cairo, item->text, x, 0, 0, + bg_color, fg_color, menu->padding, menu->padding); +} + +// Renders a single menu item vertically. +static int render_vertical_item(struct menu *menu, cairo_t *cairo, struct item *item, int x, int y) { + uint32_t bg_color = menu->sel == item ? menu->selectionbg : menu->normalbg; + uint32_t fg_color = menu->sel == item ? menu->selectionfg : menu->normalfg; + + render_text(menu, cairo, item->text, x, y, menu->width - x, + bg_color, fg_color, menu->padding, 0); + return menu->line_height; +} + +// Renders a page of menu items horizontally. +static void render_horizontal_page(struct menu *menu, cairo_t *cairo, struct page *page) { + int x = menu->promptw + menu->inputw + menu->left_arrow; + for (struct item *item = page->first; item != page->last->next_match; item = item->next_match) { + x += render_horizontal_item(menu, cairo, item, x); + } + + // Draw left and right scroll indicators if necessary + if (page->prev) { + cairo_move_to(cairo, menu->promptw + menu->inputw + menu->padding, 0); + pango_printf(cairo, menu->font, 1, "<"); + } + if (page->next) { + cairo_move_to(cairo, menu->width - menu->right_arrow + menu->padding, 0); + pango_printf(cairo, menu->font, 1, ">"); + } +} + +// Renders a page of menu items vertically. +static void render_vertical_page(struct menu *menu, cairo_t *cairo, struct page *page) { + int x = menu->promptw; + int y = menu->line_height; + for (struct item *item = page->first; item != page->last->next_match; item = item->next_match) { + y += render_vertical_item(menu, cairo, item, x, y); + } +} + +// Renders the menu to cairo. +static void render_to_cairo(struct menu *menu, cairo_t *cairo) { + // Render background + cairo_set_operator(cairo, CAIRO_OPERATOR_SOURCE); + cairo_set_source_u32(cairo, menu->normalbg); + cairo_paint(cairo); + + // Render prompt and input + render_prompt(menu, cairo); + render_input(menu, cairo); + render_cursor(menu, cairo); + + // Render selected page + if (!menu->sel) { + return; + } + if (menu->lines > 0) { + render_vertical_page(menu, cairo, menu->sel->page); + } else { + render_horizontal_page(menu, cairo, menu->sel->page); + } +} + +// Renders a single frame of the menu. +void render_menu(struct menu *menu) { + struct wl_context *context = menu->context; + + int scale = context_get_scale(context); + struct pool_buffer *buffer = context_get_next_buffer(context, scale); + if (!buffer) { + return; + } + + cairo_t *shm = buffer->cairo; + cairo_set_antialias(shm, CAIRO_ANTIALIAS_BEST); + cairo_font_options_t *fo = cairo_font_options_create(); + cairo_set_font_options(shm, fo); + cairo_font_options_destroy(fo); + render_to_cairo(menu, shm); + + struct wl_surface *surface = context_get_surface(context); + wl_surface_set_buffer_scale(surface, scale); + wl_surface_attach(surface, buffer->buffer, 0, 0); + wl_surface_damage(surface, 0, 0, menu->width, menu->height); + wl_surface_commit(surface); + + menu->rendered = true; +} diff --git a/render.h b/render.h new file mode 100644 index 0000000..af5fb67 --- /dev/null +++ b/render.h @@ -0,0 +1,9 @@ +#ifndef WMENU_RENDER_H +#define WMENU_RENDER_H + +#include "menu.h" + +void calc_widths(struct menu *menu); +void render_menu(struct menu *menu); + +#endif diff --git a/wayland.c b/wayland.c new file mode 100644 index 0000000..823ced1 --- /dev/null +++ b/wayland.c @@ -0,0 +1,510 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "menu.h" +#include "pool-buffer.h" +#include "render.h" +#include "wayland.h" +#include "xdg-activation-v1-client-protocol.h" +#include "wlr-layer-shell-unstable-v1-client-protocol.h" + +// A Wayland output. +struct output { + struct wl_context *context; + struct wl_output *output; + const char *name; // output name + int32_t scale; // output scale + struct output *next; // next output +}; + +// Creates and returns a new output. +static struct output *output_create(struct wl_context *context, struct wl_output *wl_output) { + struct output *output = calloc(1, sizeof(struct output)); + output->context = context; + output->output = wl_output; + output->scale = 1; + return output; +} + +// Keyboard state. +struct keyboard { + struct menu *menu; + struct wl_keyboard *keyboard; + struct xkb_context *context; + struct xkb_keymap *keymap; + struct xkb_state *state; + + int repeat_timer; + int repeat_delay; + int repeat_period; + enum wl_keyboard_key_state repeat_key_state; + xkb_keysym_t repeat_sym; +}; + +// Creates and returns a new keyboard. +static struct keyboard *keyboard_create(struct menu *menu, struct wl_keyboard *wl_keyboard) { + struct keyboard *keyboard = calloc(1, sizeof(struct keyboard)); + keyboard->menu = menu; + keyboard->keyboard = wl_keyboard; + keyboard->context = xkb_context_new(XKB_CONTEXT_NO_FLAGS); + assert(keyboard->context != NULL); + keyboard->repeat_timer = timerfd_create(CLOCK_MONOTONIC, 0); + assert(keyboard->repeat_timer != -1); + return keyboard; +} + +// Frees the keyboard. +static void free_keyboard(struct keyboard *keyboard) { + wl_keyboard_release(keyboard->keyboard); + xkb_state_unref(keyboard->state); + xkb_keymap_unref(keyboard->keymap); + xkb_context_unref(keyboard->context); + free(keyboard); +} + +// Wayland context. +struct wl_context { + struct menu *menu; + + struct wl_display *display; + struct wl_registry *registry; + struct wl_compositor *compositor; + struct wl_shm *shm; + struct wl_seat *seat; + struct wl_data_device_manager *data_device_manager; + struct zwlr_layer_shell_v1 *layer_shell; + struct output *output_list; + struct xdg_activation_v1 *activation; + + struct keyboard *keyboard; + struct wl_data_device *data_device; + struct wl_surface *surface; + struct zwlr_layer_surface_v1 *layer_surface; + struct wl_data_offer *data_offer; + struct output *output; + + struct pool_buffer buffers[2]; + struct pool_buffer *current; +}; + +// Returns the current output_scale. +int context_get_scale(struct wl_context *context) { + return context->output ? context->output->scale : 1; +} + +// Returns the current buffer from the pool. +struct pool_buffer *context_get_current_buffer(struct wl_context *context) { + return context->current; +} + +// Returns the next buffer from the pool. +struct pool_buffer *context_get_next_buffer(struct wl_context *context, int scale) { + struct menu *menu = context->menu; + context->current = get_next_buffer(context->shm, context->buffers, menu->width, menu->height, scale); + return context->current; +} + +// Returns the Wayland surface for the context. +struct wl_surface *context_get_surface(struct wl_context *context) { + return context->surface; +} + +// Returns the XKB state for the context. +struct xkb_state *context_get_xkb_state(struct wl_context *context) { + return context->keyboard->state; +} + +// Returns the XDG activation object for the context. +struct xdg_activation_v1 *context_get_xdg_activation(struct wl_context *context) { + return context->activation; +} + +// Retrieves pasted text from a Wayland data offer. +bool context_paste(struct wl_context *context) { + if (!context->data_offer) { + return false; + } + + int fds[2]; + if (pipe(fds) == -1) { + // Pipe failed + return false; + } + wl_data_offer_receive(context->data_offer, "text/plain", fds[1]); + close(fds[1]); + + wl_display_roundtrip(context->display); + + while (true) { + char buf[1024]; + ssize_t n = read(fds[0], buf, sizeof(buf)); + if (n <= 0) { + break; + } + menu_paste(context->menu, buf, n); + } + close(fds[0]); + + wl_data_offer_destroy(context->data_offer); + context->data_offer = NULL; + return true; +} + +// Adds an output to the output list. +static void context_add_output(struct wl_context *context, struct output *output) { + output->next = context->output_list; + context->output_list = output; +} + +// Frees the outputs. +static void free_outputs(struct wl_context *context) { + struct output *next = context->output_list; + while (next) { + struct output *output = next; + next = output->next; + wl_output_destroy(output->output); + free(output); + } +} + +// Destroys the Wayland context, freeing memory associated with it. +static void context_destroy(struct wl_context *context) { + wl_registry_destroy(context->registry); + wl_compositor_destroy(context->compositor); + wl_shm_destroy(context->shm); + wl_seat_destroy(context->seat); + wl_data_device_manager_destroy(context->data_device_manager); + zwlr_layer_shell_v1_destroy(context->layer_shell); + free_outputs(context); + + free_keyboard(context->keyboard); + wl_data_device_destroy(context->data_device); + wl_surface_destroy(context->surface); + zwlr_layer_surface_v1_destroy(context->layer_surface); + xdg_activation_v1_destroy(context->activation); + + wl_display_disconnect(context->display); + free(context); +} + +static void noop() { + // Do nothing +} + +static void surface_enter(void *data, struct wl_surface *surface, struct wl_output *wl_output) { + struct wl_context *context = data; + context->output = wl_output_get_user_data(wl_output); + menu_invalidate(context->menu); +} + +static const struct wl_surface_listener surface_listener = { + .enter = surface_enter, + .leave = noop, +}; + +static void layer_surface_configure(void *data, + struct zwlr_layer_surface_v1 *surface, + uint32_t serial, uint32_t width, uint32_t height) { + struct wl_context *context = data; + context->menu->width = width; + context->menu->height = height; + zwlr_layer_surface_v1_ack_configure(surface, serial); +} + +static void layer_surface_closed(void *data, struct zwlr_layer_surface_v1 *surface) { + struct wl_context *context = data; + context->menu->exit = true; +} + +static const struct zwlr_layer_surface_v1_listener layer_surface_listener = { + .configure = layer_surface_configure, + .closed = layer_surface_closed, +}; + +static void output_scale(void *data, struct wl_output *wl_output, int32_t factor) { + struct output *output = data; + output->scale = factor; +} + +static void output_name(void *data, struct wl_output *wl_output, const char *name) { + struct output *output = data; + output->name = name; + + struct wl_context *context = output->context; + if (context->menu->output_name && strcmp(context->menu->output_name, name) == 0) { + context->output = output; + } +} + +static const struct wl_output_listener output_listener = { + .geometry = noop, + .mode = noop, + .done = noop, + .scale = output_scale, + .name = output_name, + .description = noop, +}; + +static void keyboard_keymap(void *data, struct wl_keyboard *wl_keyboard, + uint32_t format, int32_t fd, uint32_t size) { + struct keyboard *keyboard = data; + assert(format == WL_KEYBOARD_KEYMAP_FORMAT_XKB_V1); + + char *map_shm = mmap(NULL, size, PROT_READ, MAP_SHARED, fd, 0); + assert(map_shm != MAP_FAILED); + + keyboard->keymap = xkb_keymap_new_from_string(keyboard->context, + map_shm, XKB_KEYMAP_FORMAT_TEXT_V1, 0); + munmap(map_shm, size); + close(fd); + + keyboard->state = xkb_state_new(keyboard->keymap); +} + +static void keyboard_repeat(struct keyboard *keyboard) { + menu_keypress(keyboard->menu, keyboard->repeat_key_state, keyboard->repeat_sym); + struct itimerspec spec = { 0 }; + spec.it_value.tv_sec = keyboard->repeat_period / 1000; + spec.it_value.tv_nsec = (keyboard->repeat_period % 1000) * 1000000l; + timerfd_settime(keyboard->repeat_timer, 0, &spec, NULL); +} + +static void keyboard_key(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t time, uint32_t key, uint32_t _key_state) { + struct keyboard *keyboard = data; + + enum wl_keyboard_key_state key_state = _key_state; + xkb_keysym_t sym = xkb_state_key_get_one_sym(keyboard->state, key + 8); + menu_keypress(keyboard->menu, key_state, sym); + + if (key_state == WL_KEYBOARD_KEY_STATE_PRESSED && keyboard->repeat_period >= 0) { + keyboard->repeat_key_state = key_state; + keyboard->repeat_sym = sym; + + struct itimerspec spec = { 0 }; + spec.it_value.tv_sec = keyboard->repeat_delay / 1000; + spec.it_value.tv_nsec = (keyboard->repeat_delay % 1000) * 1000000l; + timerfd_settime(keyboard->repeat_timer, 0, &spec, NULL); + } else if (key_state == WL_KEYBOARD_KEY_STATE_RELEASED) { + struct itimerspec spec = { 0 }; + timerfd_settime(keyboard->repeat_timer, 0, &spec, NULL); + } +} + +static void keyboard_repeat_info(void *data, struct wl_keyboard *wl_keyboard, + int32_t rate, int32_t delay) { + struct keyboard *keyboard = data; + keyboard->repeat_delay = delay; + if (rate > 0) { + keyboard->repeat_period = 1000 / rate; + } else { + keyboard->repeat_period = -1; + } +} + +static void keyboard_modifiers(void *data, struct wl_keyboard *wl_keyboard, + uint32_t serial, uint32_t mods_depressed, + uint32_t mods_latched, uint32_t mods_locked, + uint32_t group) { + struct keyboard *keyboard = data; + xkb_state_update_mask(keyboard->state, mods_depressed, mods_latched, + mods_locked, 0, 0, group); +} + +static const struct wl_keyboard_listener keyboard_listener = { + .keymap = keyboard_keymap, + .enter = noop, + .leave = noop, + .key = keyboard_key, + .modifiers = keyboard_modifiers, + .repeat_info = keyboard_repeat_info, +}; + +static void seat_capabilities(void *data, struct wl_seat *seat, + enum wl_seat_capability caps) { + struct wl_context *context = data; + if (caps & WL_SEAT_CAPABILITY_KEYBOARD) { + struct wl_keyboard *wl_keyboard = wl_seat_get_keyboard(seat); + struct keyboard *keyboard = keyboard_create(context->menu, wl_keyboard); + wl_keyboard_add_listener(wl_keyboard, &keyboard_listener, keyboard); + context->keyboard = keyboard; + } +} + +static const struct wl_seat_listener seat_listener = { + .capabilities = seat_capabilities, + .name = noop, +}; + +static void data_device_selection(void *data, struct wl_data_device *data_device, + struct wl_data_offer *data_offer) { + struct wl_context *context = data; + context->data_offer = data_offer; +} + +static const struct wl_data_device_listener data_device_listener = { + .data_offer = noop, + .enter = noop, + .leave = noop, + .motion = noop, + .drop = noop, + .selection = data_device_selection, +}; + +static void handle_global(void *data, struct wl_registry *registry, + uint32_t name, const char *interface, uint32_t version) { + struct wl_context *context = data; + if (strcmp(interface, wl_compositor_interface.name) == 0) { + context->compositor = wl_registry_bind(registry, name, &wl_compositor_interface, 4); + } else if (strcmp(interface, wl_shm_interface.name) == 0) { + context->shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); + } else if (strcmp(interface, wl_seat_interface.name) == 0) { + context->seat = wl_registry_bind(registry, name, &wl_seat_interface, 4); + wl_seat_add_listener(context->seat, &seat_listener, data); + } else if (strcmp(interface, wl_data_device_manager_interface.name) == 0) { + context->data_device_manager = wl_registry_bind(registry, name, &wl_data_device_manager_interface, 3); + } else if (strcmp(interface, zwlr_layer_shell_v1_interface.name) == 0) { + context->layer_shell = wl_registry_bind(registry, name, &zwlr_layer_shell_v1_interface, 1); + } else if (strcmp(interface, wl_output_interface.name) == 0) { + struct wl_output *wl_output = wl_registry_bind(registry, name, &wl_output_interface, 4); + struct output *output = output_create(context, wl_output); + wl_output_set_user_data(wl_output, output); + wl_output_add_listener(wl_output, &output_listener, output); + context_add_output(context, output); + } else if (strcmp(interface, xdg_activation_v1_interface.name) == 0) { + context->activation = wl_registry_bind(registry, name, &xdg_activation_v1_interface, 1); + } +} + +static const struct wl_registry_listener registry_listener = { + .global = handle_global, + .global_remove = noop, +}; + +// Connect to the Wayland display and run the menu. +int menu_run(struct menu *menu) { + struct wl_context *context = calloc(1, sizeof(struct wl_context)); + context->menu = menu; + menu->context = context; + + context->display = wl_display_connect(NULL); + if (!context->display) { + fprintf(stderr, "Failed to connect to display.\n"); + exit(EXIT_FAILURE); + } + + struct wl_registry *registry = wl_display_get_registry(context->display); + wl_registry_add_listener(registry, ®istry_listener, context); + wl_display_roundtrip(context->display); + assert(context->compositor != NULL); + assert(context->shm != NULL); + assert(context->seat != NULL); + assert(context->data_device_manager != NULL); + assert(context->layer_shell != NULL); + assert(context->activation != NULL); + context->registry = registry; + + // Get data device for seat + struct wl_data_device *data_device = wl_data_device_manager_get_data_device( + context->data_device_manager, context->seat); + wl_data_device_add_listener(data_device, &data_device_listener, context); + context->data_device = data_device; + + // Second roundtrip for seat and output listeners + wl_display_roundtrip(context->display); + assert(context->keyboard != NULL); + + if (menu->output_name && !context->output) { + fprintf(stderr, "Output %s not found\n", menu->output_name); + exit(EXIT_FAILURE); + } + + context->surface = wl_compositor_create_surface(context->compositor); + wl_surface_add_listener(context->surface, &surface_listener, context); + + struct zwlr_layer_surface_v1 *layer_surface = zwlr_layer_shell_v1_get_layer_surface( + context->layer_shell, + context->surface, + context->output ? context->output->output : NULL, + ZWLR_LAYER_SHELL_V1_LAYER_OVERLAY, + "menu" + ); + assert(layer_surface != NULL); + context->layer_surface = layer_surface; + + uint32_t anchor = ZWLR_LAYER_SURFACE_V1_ANCHOR_LEFT | + ZWLR_LAYER_SURFACE_V1_ANCHOR_RIGHT; + if (menu->bottom) { + anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_BOTTOM; + } else { + anchor |= ZWLR_LAYER_SURFACE_V1_ANCHOR_TOP; + } + + zwlr_layer_surface_v1_set_anchor(layer_surface, anchor); + zwlr_layer_surface_v1_set_size(layer_surface, 0, menu->height); + zwlr_layer_surface_v1_set_exclusive_zone(layer_surface, -1); + zwlr_layer_surface_v1_set_keyboard_interactivity(layer_surface, true); + zwlr_layer_surface_v1_add_listener(layer_surface, &layer_surface_listener, context); + + wl_surface_commit(context->surface); + wl_display_roundtrip(context->display); + menu_render_items(menu); + + struct pollfd fds[] = { + { wl_display_get_fd(context->display), POLLIN }, + { context->keyboard->repeat_timer, POLLIN }, + }; + const size_t nfds = sizeof(fds) / sizeof(*fds); + + while (!menu->exit) { + errno = 0; + do { + if (wl_display_flush(context->display) == -1 && errno != EAGAIN) { + fprintf(stderr, "wl_display_flush: %s\n", strerror(errno)); + break; + } + } while (errno == EAGAIN); + + if (poll(fds, nfds, -1) < 0) { + fprintf(stderr, "poll: %s\n", strerror(errno)); + break; + } + + if (fds[0].revents & POLLIN) { + if (wl_display_dispatch(context->display) < 0) { + menu->exit = true; + } + } + + if (fds[1].revents & POLLIN) { + keyboard_repeat(context->keyboard); + } + + // Render the menu if necessary + if (!menu->rendered) { + render_menu(menu); + } + } + + context_destroy(context); + menu->context = NULL; + + if (menu->failure) { + return EXIT_FAILURE; + } + return EXIT_SUCCESS; +} diff --git a/wayland.h b/wayland.h new file mode 100644 index 0000000..c5d7ce5 --- /dev/null +++ b/wayland.h @@ -0,0 +1,19 @@ +#ifndef WMENU_WAYLAND_H +#define WMENU_WAYLAND_H + +#include "menu.h" +#include + +struct wl_context; + +int menu_run(struct menu *menu); + +int context_get_scale(struct wl_context *context); +struct pool_buffer *context_get_current_buffer(struct wl_context *context); +struct pool_buffer *context_get_next_buffer(struct wl_context *context, int scale); +struct wl_surface *context_get_surface(struct wl_context *context); +struct xkb_state *context_get_xkb_state(struct wl_context *context); +struct xdg_activation_v1 *context_get_xdg_activation(struct wl_context *context); +bool context_paste(struct wl_context *context); + +#endif diff --git a/wmenu-run.c b/wmenu-run.c new file mode 100644 index 0000000..1b7b8c1 --- /dev/null +++ b/wmenu-run.c @@ -0,0 +1,78 @@ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include + +#include "menu.h" +#include "wayland.h" +#include "xdg-activation-v1-client-protocol.h" + +static void read_items(struct menu *menu) { + char *path = strdup(getenv("PATH")); + for (char *p = strtok(path, ":"); p != NULL; p = strtok(NULL, ":")) { + DIR *dir = opendir(p); + if (dir == NULL) { + continue; + } + for (struct dirent *ent = readdir(dir); ent != NULL; ent = readdir(dir)) { + if (ent->d_name[0] == '.') { + continue; + } + menu_add_item(menu, strdup(ent->d_name)); + } + closedir(dir); + } + menu_sort_and_deduplicate(menu); + free(path); +} + +struct command { + struct menu *menu; + char *text; + bool exit; +}; + +static void activation_token_done(void *data, struct xdg_activation_token_v1 *activation_token, + const char *token) { + struct command *cmd = data; + xdg_activation_token_v1_destroy(activation_token); + + int pid = fork(); + if (pid == 0) { + setenv("XDG_ACTIVATION_TOKEN", token, true); + char *argv[] = {"/bin/sh", "-c", cmd->text, NULL}; + execvp(argv[0], (char**)argv); + } else { + if (cmd->exit) { + cmd->menu->exit = true; + } + } +} + +static const struct xdg_activation_token_v1_listener activation_token_listener = { + .done = activation_token_done, +}; + +static void exec_item(struct menu *menu, char *text, bool exit) { + struct command *cmd = calloc(1, sizeof(struct command)); + cmd->menu = menu; + cmd->text = strdup(text); + cmd->exit = exit; + + struct xdg_activation_v1 *activation = context_get_xdg_activation(menu->context); + struct xdg_activation_token_v1 *activation_token = xdg_activation_v1_get_activation_token(activation); + xdg_activation_token_v1_set_surface(activation_token, context_get_surface(menu->context)); + xdg_activation_token_v1_add_listener(activation_token, &activation_token_listener, cmd); + xdg_activation_token_v1_commit(activation_token); +} + +int main(int argc, char *argv[]) { + struct menu *menu = menu_create(exec_item); + menu_getopts(menu, argc, argv); + read_items(menu); + int status = menu_run(menu); + menu_destroy(menu); + return status; +} diff --git a/wmenu.c b/wmenu.c new file mode 100644 index 0000000..38e78b9 --- /dev/null +++ b/wmenu.c @@ -0,0 +1,35 @@ +#define _POSIX_C_SOURCE 200809L + +#include +#include + +#include "menu.h" +#include "wayland.h" + +static void read_items(struct menu *menu) { + char buf[sizeof menu->input]; + while (fgets(buf, sizeof buf, stdin)) { + char *p = strchr(buf, '\n'); + if (p) { + *p = '\0'; + } + menu_add_item(menu, strdup(buf)); + } +} + +static void print_item(struct menu *menu, char *text, bool exit) { + puts(text); + fflush(stdout); + if (exit) { + menu->exit = true; + } +} + +int main(int argc, char *argv[]) { + struct menu *menu = menu_create(print_item); + menu_getopts(menu, argc, argv); + read_items(menu); + int status = menu_run(menu); + menu_destroy(menu); + return status; +}