1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
|
#define _POSIX_C_SOURCE 200809L
#include <ctype.h>
#include <poll.h>
#include <stdbool.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <time.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/timerfd.h>
#include <wayland-client.h>
#include <wayland-client-protocol.h>
#include <xkbcommon/xkbcommon.h>
#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;
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_item(struct item *item) {
free(item->text);
free(item);
}
static void free_items(struct menu *menu) {
struct item *next = menu->items;
while (next) {
struct item *item = next;
next = item->next;
free_item(item);
}
}
// Destroys the menu, freeing memory associated with it.
void menu_destroy(struct menu *menu) {
free_pages(menu);
free_items(menu);
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, bool sort) {
struct item *new = calloc(1, sizeof(struct item));
if (!new) {
return;
}
new->text = text;
if (sort) {
for (struct item **item = &menu->items; *item; item = &(*item)->next) {
int result = strcmp(new->text, (*item)->text);
if (result == 0) {
free_item(new);
return;
}
if (result < 0) {
new->next = *item;
*item = new;
return;
}
}
}
if (menu->lastitem) {
menu->lastitem->next = new;
} else {
menu->items = new;
}
menu->lastitem = new;
}
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 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;
struct item *item;
for (item = menu->items; item; item = item->next) {
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;
}
}
// Render menu items.
void menu_render_items(struct menu *menu) {
render_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);
render_menu(menu);
return;
case XKB_KEY_u:
// Delete left
insert(menu, NULL, 0 - menu->cursor);
match_items(menu);
render_menu(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);
render_menu(menu);
return;
case XKB_KEY_Y:
// Paste clipboard
if (!context_paste(menu->context)) {
return;
}
match_items(menu);
render_menu(menu);
return;
case XKB_KEY_Left:
case XKB_KEY_KP_Left:
movewordedge(menu, -1);
render_menu(menu);
return;
case XKB_KEY_Right:
case XKB_KEY_KP_Right:
movewordedge(menu, +1);
render_menu(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);
render_menu(menu);
return;
case XKB_KEY_f:
movewordedge(menu, +1);
render_menu(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;
render_menu(menu);
} else if (menu->cursor > 0) {
menu->cursor = nextrune(menu, -1);
render_menu(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);
render_menu(menu);
} else if (menu->sel && menu->sel->next_match) {
menu->sel = menu->sel->next_match;
render_menu(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;
render_menu(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;
render_menu(menu);
}
break;
case XKB_KEY_Home:
case XKB_KEY_KP_Home:
if (menu->sel == menu->matches) {
menu->cursor = 0;
render_menu(menu);
} else {
menu->sel = menu->matches;
render_menu(menu);
}
break;
case XKB_KEY_End:
case XKB_KEY_KP_End:
if (menu->cursor < len) {
menu->cursor = len;
render_menu(menu);
} else {
menu->sel = menu->matches_end;
render_menu(menu);
}
break;
case XKB_KEY_BackSpace:
if (menu->cursor > 0) {
insert(menu, NULL, nextrune(menu, -1) - menu->cursor);
match_items(menu);
render_menu(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);
render_menu(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);
render_menu(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);
render_menu(menu);
}
}
}
|