menu.c - wmenu - [fork] efficient dynamic menu for wayland
(HTM) git clone https://git.drkhsh.at/wmenu.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
menu.c (16403B)
---
1 #define _POSIX_C_SOURCE 200809L
2 #include <assert.h>
3 #include <ctype.h>
4 #include <poll.h>
5 #include <stdbool.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11 #include <time.h>
12 #include <unistd.h>
13 #include <sys/mman.h>
14 #include <sys/timerfd.h>
15 #include <wayland-client.h>
16 #include <wayland-client-protocol.h>
17 #include <xkbcommon/xkbcommon.h>
18
19 #include "menu.h"
20
21 #include "pango.h"
22 #include "render.h"
23 #include "wayland.h"
24
25 // Creates and returns a new menu.
26 struct menu *menu_create(menu_callback callback) {
27 struct menu *menu = calloc(1, sizeof(struct menu));
28 menu->strncmp = strncmp;
29 menu->font = "monospace 10";
30 menu->normalbg = 0x222222ff;
31 menu->normalfg = 0xbbbbbbff;
32 menu->promptbg = 0x005577ff;
33 menu->promptfg = 0xeeeeeeff;
34 menu->selectionbg = 0x005577ff;
35 menu->selectionfg = 0xeeeeeeff;
36 menu->callback = callback;
37 menu->test_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 1, 1);
38 menu->test_cairo = cairo_create(menu->test_surface);
39 return menu;
40 }
41
42 static void free_pages(struct menu *menu) {
43 struct page *next = menu->pages;
44 while (next) {
45 struct page *page = next;
46 next = page->next;
47 free(page);
48 }
49 }
50
51 static void free_items(struct menu *menu) {
52 for (size_t i = 0; i < menu->item_count; i++) {
53 struct item *item = &menu->items[i];
54 free(item->text);
55 }
56 free(menu->items);
57 }
58
59 // Destroys the menu, freeing memory associated with it.
60 void menu_destroy(struct menu *menu) {
61 free_pages(menu);
62 free_items(menu);
63 cairo_destroy(menu->test_cairo);
64 cairo_surface_destroy(menu->test_surface);
65 free(menu);
66 }
67
68 static bool parse_color(const char *color, uint32_t *result) {
69 if (color[0] == '#') {
70 ++color;
71 }
72 size_t len = strlen(color);
73 if ((len != 6 && len != 8) || !isxdigit(color[0]) || !isxdigit(color[1])) {
74 return false;
75 }
76 char *ptr;
77 uint32_t parsed = (uint32_t)strtoul(color, &ptr, 16);
78 if (*ptr != '\0') {
79 return false;
80 }
81 *result = len == 6 ? ((parsed << 8) | 0xFF) : parsed;
82 return true;
83 }
84
85 // Parse menu options from command line arguments.
86 void menu_getopts(struct menu *menu, int argc, char *argv[]) {
87 const char *usage =
88 "Usage: wmenu [-biPv] [-f font] [-l lines] [-w width] [-o output] [-p prompt]\n"
89 "\t[-N color] [-n color] [-M color] [-m color] [-S color] [-s color]\n";
90
91 int opt;
92 while ((opt = getopt(argc, argv, "bhiPvf:l:w:o:p:N:n:M:m:S:s:")) != -1) {
93 switch (opt) {
94 case 'b':
95 menu->bottom = true;
96 break;
97 case 'i':
98 menu->strncmp = strncasecmp;
99 break;
100 case 'P':
101 menu->passwd = true;
102 break;
103 case 'v':
104 puts("wmenu " VERSION);
105 exit(EXIT_SUCCESS);
106 case 'f':
107 menu->font = optarg;
108 break;
109 case 'l':
110 menu->lines = atoi(optarg);
111 break;
112 case 'w':
113 menu->customwidth = atoi(optarg);
114 break;
115 case 'o':
116 menu->output_name = optarg;
117 break;
118 case 'p':
119 menu->prompt = optarg;
120 break;
121 case 'N':
122 if (!parse_color(optarg, &menu->normalbg)) {
123 fprintf(stderr, "Invalid background color: %s", optarg);
124 }
125 break;
126 case 'n':
127 if (!parse_color(optarg, &menu->normalfg)) {
128 fprintf(stderr, "Invalid foreground color: %s", optarg);
129 }
130 break;
131 case 'M':
132 if (!parse_color(optarg, &menu->promptbg)) {
133 fprintf(stderr, "Invalid prompt background color: %s", optarg);
134 }
135 break;
136 case 'm':
137 if (!parse_color(optarg, &menu->promptfg)) {
138 fprintf(stderr, "Invalid prompt foreground color: %s", optarg);
139 }
140 break;
141 case 'S':
142 if (!parse_color(optarg, &menu->selectionbg)) {
143 fprintf(stderr, "Invalid selection background color: %s", optarg);
144 }
145 break;
146 case 's':
147 if (!parse_color(optarg, &menu->selectionfg)) {
148 fprintf(stderr, "Invalid selection foreground color: %s", optarg);
149 }
150 break;
151 default:
152 fprintf(stderr, "%s", usage);
153 exit(EXIT_FAILURE);
154 }
155 }
156
157 if (optind < argc) {
158 fprintf(stderr, "%s", usage);
159 exit(EXIT_FAILURE);
160 }
161
162 int height = get_font_height(menu->font);
163 menu->line_height = height + 2;
164 menu->height = menu->line_height;
165 if (menu->lines > 0) {
166 menu->height += menu->height * menu->lines;
167 }
168 menu->padding = height / 2;
169 }
170
171 // Add an item to the menu.
172 void menu_add_item(struct menu *menu, char *text) {
173 if ((menu->item_count & (menu->item_count - 1)) == 0) {
174 size_t alloc_size = menu->item_count ? 2 * menu->item_count : 1;
175 void *new_array = realloc(menu->items, sizeof(struct item) * alloc_size);
176 if (!new_array) {
177 fprintf(stderr, "could not realloc %zu bytes", sizeof(struct item) * alloc_size);
178 exit(EXIT_FAILURE);
179 }
180 menu->items = new_array;
181 }
182
183 struct item *new = &menu->items[menu->item_count];
184 new->text = text;
185
186 menu->item_count++;
187 }
188
189 static int compare_items(const void *a, const void *b) {
190 const struct item *item_a = a;
191 const struct item *item_b = b;
192 return strcmp(item_a->text, item_b->text);
193 }
194
195 void menu_sort_and_deduplicate(struct menu *menu) {
196 size_t j = 1;
197 size_t i;
198
199 qsort(menu->items, menu->item_count, sizeof(*menu->items), compare_items);
200
201 for (i = 1; i < menu->item_count; i++) {
202 if (strcmp(menu->items[i].text, menu->items[j - 1].text) == 0) {
203 free(menu->items[i].text);
204 } else {
205 menu->items[j] = menu->items[i];
206 j++;
207 }
208 }
209 menu->item_count = j;
210 }
211
212 static void append_page(struct page *page, struct page **first, struct page **last) {
213 if (*last) {
214 (*last)->next = page;
215 } else {
216 *first = page;
217 }
218 page->prev = *last;
219 page->next = NULL;
220 *last = page;
221 }
222
223 static void page_items(struct menu *menu) {
224 // Free existing pages
225 while (menu->pages != NULL) {
226 struct page *page = menu->pages;
227 menu->pages = menu->pages->next;
228 free(page);
229 }
230
231 if (!menu->matches) {
232 return;
233 }
234
235 // Make new pages
236 if (menu->lines > 0) {
237 struct page *pages_end = NULL;
238 struct item *item = menu->matches;
239 while (item) {
240 struct page *page = calloc(1, sizeof(struct page));
241 page->first = item;
242
243 for (int i = 1; item && i <= menu->lines; i++) {
244 item->page = page;
245 page->last = item;
246 item = item->next_match;
247 }
248 append_page(page, &menu->pages, &pages_end);
249 }
250 } else {
251 // Calculate available space
252 int max_width = menu->width - menu->inputw - menu->promptw
253 - menu->left_arrow - menu->right_arrow;
254
255 struct page *pages_end = NULL;
256 struct item *item = menu->matches;
257 while (item) {
258 struct page *page = calloc(1, sizeof(struct page));
259 page->first = item;
260
261 int total_width = 0;
262 int items = 0;
263 while (item) {
264 total_width += item->width + 2 * menu->padding;
265 if (total_width > max_width && items > 0) {
266 break;
267 }
268 items++;
269
270 item->page = page;
271 page->last = item;
272 item = item->next_match;
273 }
274 append_page(page, &menu->pages, &pages_end);
275 }
276 }
277 }
278
279 static const char *fstrstr(struct menu *menu, const char *s, const char *sub) {
280 for (size_t len = strlen(sub); *s; s++) {
281 if (!menu->strncmp(s, sub, len)) {
282 return s;
283 }
284 }
285 return NULL;
286 }
287
288 static void append_match(struct item *item, struct item **first, struct item **last) {
289 if (*last) {
290 (*last)->next_match = item;
291 } else {
292 *first = item;
293 }
294 item->prev_match = *last;
295 item->next_match = NULL;
296 *last = item;
297 }
298
299 static void match_items(struct menu *menu) {
300 struct item *lexact = NULL, *exactend = NULL;
301 struct item *lprefix = NULL, *prefixend = NULL;
302 struct item *lsubstr = NULL, *substrend = NULL;
303 char buf[sizeof menu->input], *tok;
304 char **tokv = NULL;
305 int i, tokc = 0;
306 size_t k;
307 size_t tok_len;
308 menu->matches = NULL;
309 menu->matches_end = NULL;
310 menu->sel = NULL;
311
312 size_t input_len = strlen(menu->input);
313
314 /* tokenize input by space for matching the tokens individually */
315 strcpy(buf, menu->input);
316 tok = strtok(buf, " ");
317 while (tok) {
318 tokv = realloc(tokv, (tokc + 1) * sizeof *tokv);
319 if (!tokv) {
320 fprintf(stderr, "could not realloc %zu bytes",
321 (tokc + 1) * sizeof *tokv);
322 exit(EXIT_FAILURE);
323 }
324 tokv[tokc] = tok;
325 tokc++;
326 tok = strtok(NULL, " ");
327 }
328 tok_len = tokc ? strlen(tokv[0]) : 0;
329
330 for (k = 0; k < menu->item_count; k++) {
331 struct item *item = &menu->items[k];
332 for (i = 0; i < tokc; i++) {
333 if (!fstrstr(menu, item->text, tokv[i])) {
334 /* token does not match */
335 break;
336 }
337 }
338 if (i != tokc) {
339 /* not all tokens match */
340 continue;
341 }
342 if (!tokc || !menu->strncmp(menu->input, item->text, input_len + 1)) {
343 append_match(item, &lexact, &exactend);
344 } else if (!menu->strncmp(tokv[0], item->text, tok_len)) {
345 append_match(item, &lprefix, &prefixend);
346 } else {
347 append_match(item, &lsubstr, &substrend);
348 }
349 }
350
351 free(tokv);
352
353 if (lexact) {
354 menu->matches = lexact;
355 menu->matches_end = exactend;
356 }
357 if (lprefix) {
358 if (menu->matches_end) {
359 menu->matches_end->next_match = lprefix;
360 lprefix->prev_match = menu->matches_end;
361 } else {
362 menu->matches = lprefix;
363 }
364 menu->matches_end = prefixend;
365 }
366 if (lsubstr) {
367 if (menu->matches_end) {
368 menu->matches_end->next_match = lsubstr;
369 lsubstr->prev_match = menu->matches_end;
370 } else {
371 menu->matches = lsubstr;
372 }
373 menu->matches_end = substrend;
374 }
375
376 page_items(menu);
377 if (menu->pages) {
378 menu->sel = menu->pages->first;
379 }
380 }
381
382 // Marks the menu as needing to be rendered again.
383 void menu_invalidate(struct menu *menu) {
384 menu->rendered = false;
385 }
386
387 // Render menu items.
388 void menu_render_items(struct menu *menu) {
389 calc_widths(menu);
390 match_items(menu);
391 render_menu(menu);
392 }
393
394 static void insert(struct menu *menu, const char *text, ssize_t len) {
395 if (strlen(menu->input) + len > sizeof menu->input - 1) {
396 return;
397 }
398 memmove(menu->input + menu->cursor + len, menu->input + menu->cursor,
399 sizeof menu->input - menu->cursor - MAX(len, 0));
400 if (len > 0 && text != NULL) {
401 memcpy(menu->input + menu->cursor, text, len);
402 }
403 menu->cursor += len;
404 }
405
406 // Add pasted text to the menu input.
407 void menu_paste(struct menu *menu, const char *text, ssize_t len) {
408 insert(menu, text, len);
409 }
410
411 static size_t nextrune(struct menu *menu, int incr) {
412 size_t n, len;
413
414 len = strlen(menu->input);
415 for(n = menu->cursor + incr; n < len && (menu->input[n] & 0xc0) == 0x80; n += incr);
416 return n;
417 }
418
419 // Move the cursor to the beginning or end of the word, skipping over any preceding whitespace.
420 static void movewordedge(struct menu *menu, int dir) {
421 if (dir < 0) {
422 // Move to beginning of word
423 while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] == ' ') {
424 menu->cursor = nextrune(menu, -1);
425 }
426 while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] != ' ') {
427 menu->cursor = nextrune(menu, -1);
428 }
429 } else {
430 // Move to end of word
431 size_t len = strlen(menu->input);
432 while (menu->cursor < len && menu->input[menu->cursor] == ' ') {
433 menu->cursor = nextrune(menu, +1);
434 }
435 while (menu->cursor < len && menu->input[menu->cursor] != ' ') {
436 menu->cursor = nextrune(menu, +1);
437 }
438 }
439 }
440
441 // Handle a keypress.
442 void menu_keypress(struct menu *menu, enum wl_keyboard_key_state key_state,
443 xkb_keysym_t sym) {
444 if (key_state != WL_KEYBOARD_KEY_STATE_PRESSED) {
445 return;
446 }
447
448 struct xkb_state *state = context_get_xkb_state(menu->context);
449 bool ctrl = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_CTRL,
450 XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
451 bool meta = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_ALT,
452 XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
453 bool shift = xkb_state_mod_name_is_active(state, XKB_MOD_NAME_SHIFT,
454 XKB_STATE_MODS_DEPRESSED | XKB_STATE_MODS_LATCHED);
455
456 size_t len = strlen(menu->input);
457
458 if (ctrl) {
459 // Emacs-style line editing bindings
460 switch (sym) {
461 case XKB_KEY_a:
462 sym = XKB_KEY_Home;
463 break;
464 case XKB_KEY_b:
465 sym = XKB_KEY_Left;
466 break;
467 case XKB_KEY_c:
468 sym = XKB_KEY_Escape;
469 break;
470 case XKB_KEY_d:
471 sym = XKB_KEY_Delete;
472 break;
473 case XKB_KEY_e:
474 sym = XKB_KEY_End;
475 break;
476 case XKB_KEY_f:
477 sym = XKB_KEY_Right;
478 break;
479 case XKB_KEY_g:
480 sym = XKB_KEY_Escape;
481 break;
482 case XKB_KEY_bracketleft:
483 sym = XKB_KEY_Escape;
484 break;
485 case XKB_KEY_h:
486 sym = XKB_KEY_BackSpace;
487 break;
488 case XKB_KEY_i:
489 sym = XKB_KEY_Tab;
490 break;
491 case XKB_KEY_j:
492 case XKB_KEY_J:
493 case XKB_KEY_m:
494 case XKB_KEY_M:
495 sym = XKB_KEY_Return;
496 ctrl = false;
497 break;
498 case XKB_KEY_n:
499 sym = XKB_KEY_Down;
500 break;
501 case XKB_KEY_p:
502 sym = XKB_KEY_Up;
503 break;
504
505 case XKB_KEY_k:
506 // Delete right
507 menu->input[menu->cursor] = '\0';
508 match_items(menu);
509 menu_invalidate(menu);
510 return;
511 case XKB_KEY_u:
512 // Delete left
513 insert(menu, NULL, 0 - menu->cursor);
514 match_items(menu);
515 menu_invalidate(menu);
516 return;
517 case XKB_KEY_w:
518 // Delete word
519 while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] == ' ') {
520 insert(menu, NULL, nextrune(menu, -1) - menu->cursor);
521 }
522 while (menu->cursor > 0 && menu->input[nextrune(menu, -1)] != ' ') {
523 insert(menu, NULL, nextrune(menu, -1) - menu->cursor);
524 }
525 match_items(menu);
526 menu_invalidate(menu);
527 return;
528 case XKB_KEY_Y:
529 // Paste clipboard
530 if (!context_paste(menu->context)) {
531 return;
532 }
533 match_items(menu);
534 menu_invalidate(menu);
535 return;
536 case XKB_KEY_Left:
537 case XKB_KEY_KP_Left:
538 movewordedge(menu, -1);
539 menu_invalidate(menu);
540 return;
541 case XKB_KEY_Right:
542 case XKB_KEY_KP_Right:
543 movewordedge(menu, +1);
544 menu_invalidate(menu);
545 return;
546
547 case XKB_KEY_Return:
548 case XKB_KEY_KP_Enter:
549 break;
550 default:
551 return;
552 }
553 } else if (meta) {
554 // Emacs-style line editing bindings
555 switch (sym) {
556 case XKB_KEY_b:
557 movewordedge(menu, -1);
558 menu_invalidate(menu);
559 return;
560 case XKB_KEY_f:
561 movewordedge(menu, +1);
562 menu_invalidate(menu);
563 return;
564 case XKB_KEY_g:
565 sym = XKB_KEY_Home;
566 break;
567 case XKB_KEY_G:
568 sym = XKB_KEY_End;
569 break;
570 case XKB_KEY_h:
571 sym = XKB_KEY_Up;
572 break;
573 case XKB_KEY_j:
574 sym = XKB_KEY_Next;
575 break;
576 case XKB_KEY_k:
577 sym = XKB_KEY_Prior;
578 break;
579 case XKB_KEY_l:
580 sym = XKB_KEY_Down;
581 break;
582 default:
583 return;
584 }
585 }
586
587 char buf[8];
588 switch (sym) {
589 case XKB_KEY_Return:
590 case XKB_KEY_KP_Enter:
591 if (shift) {
592 menu->callback(menu, menu->input, true);
593 } else {
594 char *text = menu->sel ? menu->sel->text : menu->input;
595 menu->callback(menu, text, !ctrl);
596 }
597 break;
598 case XKB_KEY_Left:
599 case XKB_KEY_KP_Left:
600 case XKB_KEY_Up:
601 case XKB_KEY_KP_Up:
602 if (menu->sel && menu->sel->prev_match) {
603 menu->sel = menu->sel->prev_match;
604 menu_invalidate(menu);
605 } else if (menu->cursor > 0) {
606 menu->cursor = nextrune(menu, -1);
607 menu_invalidate(menu);
608 }
609 break;
610 case XKB_KEY_Right:
611 case XKB_KEY_KP_Right:
612 case XKB_KEY_Down:
613 case XKB_KEY_KP_Down:
614 if (menu->cursor < len) {
615 menu->cursor = nextrune(menu, +1);
616 menu_invalidate(menu);
617 } else if (menu->sel && menu->sel->next_match) {
618 menu->sel = menu->sel->next_match;
619 menu_invalidate(menu);
620 }
621 break;
622 case XKB_KEY_Prior:
623 case XKB_KEY_KP_Prior:
624 if (menu->sel && menu->sel->page->prev) {
625 menu->sel = menu->sel->page->prev->first;
626 menu_invalidate(menu);
627 }
628 break;
629 case XKB_KEY_Next:
630 case XKB_KEY_KP_Next:
631 if (menu->sel && menu->sel->page->next) {
632 menu->sel = menu->sel->page->next->first;
633 menu_invalidate(menu);
634 }
635 break;
636 case XKB_KEY_Home:
637 case XKB_KEY_KP_Home:
638 if (menu->sel == menu->matches) {
639 menu->cursor = 0;
640 menu_invalidate(menu);
641 } else {
642 menu->sel = menu->matches;
643 menu_invalidate(menu);
644 }
645 break;
646 case XKB_KEY_End:
647 case XKB_KEY_KP_End:
648 if (menu->cursor < len) {
649 menu->cursor = len;
650 menu_invalidate(menu);
651 } else {
652 menu->sel = menu->matches_end;
653 menu_invalidate(menu);
654 }
655 break;
656 case XKB_KEY_BackSpace:
657 if (menu->cursor > 0) {
658 insert(menu, NULL, nextrune(menu, -1) - menu->cursor);
659 match_items(menu);
660 menu_invalidate(menu);
661 }
662 break;
663 case XKB_KEY_Delete:
664 case XKB_KEY_KP_Delete:
665 if (menu->cursor == len) {
666 return;
667 }
668 menu->cursor = nextrune(menu, +1);
669 insert(menu, NULL, nextrune(menu, -1) - menu->cursor);
670 match_items(menu);
671 menu_invalidate(menu);
672 break;
673 case XKB_KEY_Tab:
674 if (!menu->sel) {
675 return;
676 }
677 menu->cursor = strnlen(menu->sel->text, sizeof menu->input - 1);
678 memcpy(menu->input, menu->sel->text, menu->cursor);
679 menu->input[menu->cursor] = '\0';
680 match_items(menu);
681 menu_invalidate(menu);
682 break;
683 case XKB_KEY_Escape:
684 menu->exit = true;
685 menu->failure = true;
686 break;
687 default:
688 if (xkb_keysym_to_utf8(sym, buf, 8)) {
689 insert(menu, buf, strnlen(buf, 8));
690 match_items(menu);
691 menu_invalidate(menu);
692 }
693 }
694 }