Debian Codegraph @

client/common/common.c

raw
1 #include "internal.h"
2
3 #include "common.h"
4 #include <stdlib.h>
5 #include <string.h>
6 #include <signal.h>
7 #include <stdio.h>
8 #include <unistd.h>
9 #include <getopt.h>
10 #include <assert.h>
11 #include <fcntl.h>
12 #include <sys/file.h>
13
14 static void
15 disco_trap(int sig)
16 {
17 (void)sig;
18 fprintf(stderr, "\x1B[?25h");
19 fflush(stderr);
20 exit(EXIT_FAILURE);
21 }
22
23 static void
24 disco(void)
25 {
26 struct sigaction action;
27 memset(&action, 0, sizeof(struct sigaction));
28 action.sa_handler = disco_trap;
29 sigaction(SIGABRT, &action, NULL);
30 sigaction(SIGSEGV, &action, NULL);
31 sigaction(SIGTRAP, &action, NULL);
32 sigaction(SIGINT, &action, NULL);
33
34 uint32_t cc, c = 80;
35 fprintf(stderr, "\x1B[?25l");
36 while (1) {
37 for (uint32_t i = 1; i < c - 1; ++i) {
38 fprintf(stderr, "\r %*s%s %s %s ", (i > c / 2 ? c - i : i), " ", ((i % 2) ? "<o/" : "\\o>"), ((i % 4) ? "DISCO" : " "), ((i %2) ? "\\o>" : "<o/"));
39 for (cc = 0; cc < (i < c / 2 ? c / 2 - i : i - c / 2); ++cc) fprintf(stderr, ((i % 2) ? "^" : "'"));
40 fprintf(stderr, "%s %s \r %s %s", ((i % 2) ? "*" : "•"), ((i % 3) ? "\\o" : "<o"), ((i % 3) ? "o/" : "o>"), ((i % 2) ? "*" : "•"));
41 for (cc = 2; cc < (i > c / 2 ? c - i : i); ++cc) fprintf(stderr, ((i % 2) ? "^" : "'"));
42 fflush(stderr);
43 usleep(140 * 1000);
44 }
45 }
46 fprintf(stderr, "\x1B[?25h");
47 exit(EXIT_SUCCESS);
48 }
49
50 #define WHITESPACE " \t\n\r"
51
52 static const char*
53 tokenize(const char *cstr, size_t *out_len, const char *separator, bool skip_whitespace, const char **state)
54 {
55 assert(out_len && separator && state);
56 const char *current = (state && *state ? *state : cstr);
57
58 if (!current || !*current || !cstr || !*cstr)
59 return NULL;
60
61 current += strspn(current, separator);
62
63 if (skip_whitespace)
64 current += strspn(current, WHITESPACE);
65
66 *out_len = strcspn(current, separator);
67 *state = current + *out_len;
68
69 if (skip_whitespace) {
70 const size_t ws = strcspn(current, WHITESPACE);
71 *out_len -= (ws < *out_len ? *out_len - ws : 0);
72 }
73
74 return current;
75 }
76
77 static const char*
78 tokenize_quoted(const char *cstr, size_t *out_len, const char *separator, const char *quotes, const char **state)
79 {
80 assert(out_len && separator && quotes && state);
81 const char *e, *current = tokenize(cstr, out_len, separator, true, state);
82
83 if (!current)
84 return NULL;
85
86 for (const char *q = quotes; *q; ++q) {
87 if (*current != *q)
88 continue;
89
90 bool escaped = false;
91 for (e = ++current; *e; ++e) {
92 if (escaped)
93 escaped = false;
94 else if (*e == '\\')
95 escaped = true;
96 else if (*e == *q)
97 break;
98 }
99
100 *out_len = e - current;
101 e = (!*e ? e : e + 1);
102
103 if (*e) {
104 size_t tmp;
105 const char *state2 = NULL;
106 *state = tokenize(e, &tmp, separator, true, &state2);
107 } else {
108 *state = e;
109 }
110
111 break;
112 }
113
114 return current;
115 }
116
117 char*
118 cstrcopy(const char *str, size_t size)
119 {
120 char *cpy = calloc(1, size + 1);
121 return (cpy ? memcpy(cpy, str, size) : NULL);
122 }
123
124 char**
125 tokenize_quoted_to_argv(const char *str, char *argv0, int *out_argc)
126 {
127 if (out_argc) *out_argc = 0;
128
129 size_t count = !!argv0;
130 {
131 size_t len;
132 const char *state = NULL;
133 while (tokenize_quoted(str, &len, " ", "\"'", &state))
134 ++count;
135 }
136
137 char **tokens;
138 if (!count || !(tokens = calloc(count + 1, sizeof(char*))))
139 return NULL;
140
141 {
142 tokens[0] = argv0;
143 size_t i = !!argv0, len;
144 const char *t, *state = NULL;
145 while (i < count && (t = tokenize_quoted(str, &len, " ", "\"'", &state))) {
146 if (!(tokens[i++] = cstrcopy(t, len)))
147 return NULL;
148 }
149 }
150
151 if (out_argc) *out_argc = count;
152 return tokens;
153 }
154
155 static void
156 version(const char *name)
157 {
158 assert(name);
159 char *base = strrchr(name, '/');
160 printf("%s v%s\n", (base ? base + 1 : name), bm_version());
161 exit(EXIT_SUCCESS);
162 }
163
164 static void
165 usage(FILE *out, const char *name)
166 {
167 assert(out && name);
168
169 char *base = strrchr(name, '/');
170 fprintf(out, "usage: %s [options]\n", (base ? base + 1 : name));
171 fputs("Options\n"
172 " -h, --help display this help and exit.\n"
173 " -v, --version display version.\n"
174 " -i, --ignorecase match items case insensitively.\n"
175 " -F, --filter filter entries for a given string before showing the menu.\n"
176 " -w, --wrap wraps cursor selection.\n"
177 " -l, --list list items vertically down or up with the given number of lines(number of lines down/up). (down (default), up)\n"
178 " -p, --prompt defines the prompt text to be displayed.\n"
179 " -P, --prefix text to show before highlighted item.\n"
180 " -I, --index select item at index automatically.\n"
181 " -x, --password display/hide/replace input. (none (default), hide, indicator)\n"
182 " -s, --no-spacing disable the title spacing on entries.\n"
183 " -C, --no-cursor ignore cursor events.\n"
184 " -T, --no-touch ignore touch events.\n"
185 " -K, --no-keyboard ignore keyboard events.\n"
186 " --binding use alternative key bindings. Available options: vim\n"
187 " --fixed-height prevent the display from changing height on filter.\n"
188 " --scrollbar display scrollbar. (none (default), always, autohide)\n"
189 " --counter display a matched/total items counter. (none (default), always)\n"
190 " -e, --vim-esc-exits exit bemenu when pressing escape in normal mode for vim bindings\n"
191 " -N, --vim-normal-mode start in normal mode for vim bindings\n"
192 " --accept-single immediately return if there is only one item.\n"
193 " --ifne only display menu if there are items.\n"
194 " --single-instance force a single menu instance.\n"
195 " --fork always fork. (bemenu-run)\n"
196 " --no-exec do not execute command. (bemenu-run)\n"
197 " --auto-select when one entry is left, automatically select it\n\n"
198
199 "Use BEMENU_BACKEND env variable to force backend:\n"
200 " curses ncurses based terminal backend\n"
201 " wayland wayland backend\n"
202 " x11 x11 backend\n\n", out);
203
204 fputs("If BEMENU_BACKEND is empty, one of the GUI backends is selected automatically.\n\n"
205
206 "Backend specific options\n"
207 " c = ncurses, w == wayland, x == x11\n"
208 " (...) At end of help indicates the backend support for option.\n\n"
209
210 " -b, --bottom appears at the bottom of the screen. (wx)\n"
211 " -c, --center appears at the center of the screen. (wx)\n"
212 " -f, --grab show the menu before reading stdin. (wx)\n"
213 " -n, --no-overlap adjust geometry to not overlap with panels. (w)\n"
214 " -m, --monitor index of monitor where menu will appear. (wx)\n"
215 " -H, --line-height defines the height to make each menu line (0 = default height). (wx)\n"
216 " -M, --margin defines the empty space on either side of the menu. (wx)\n"
217 " -W, --width-factor defines the relative width factor of the menu (from 0 to 1). (wx)\n"
218 " -B, --border defines the width of the border in pixels around the menu. (wx)\n"
219 " -R --border-radius defines the radius of the border around the menu (0 = no curved borders).\n"
220 " --ch defines the height of the cursor (0 = scales with line height). (wx)\n"
221 " --cw defines the width of the cursor. (wx)\n"
222 " --hp defines the horizontal padding for the entries in single line mode. (wx)\n"
223 " --fn defines the font to be used ('name [size]'). (wx)\n"
224 " --tb defines the title background color. (wx)\n"
225 " --tf defines the title foreground color. (wx)\n"
226 " --fb defines the filter background color. (wx)\n"
227 " --ff defines the filter foreground color. (wx)\n"
228 " --cb defines the cursor background color. (wx)\n"
229 " --cf defines the cursor foreground color. (wx)\n"
230 " --nb defines the normal background color. (wx)\n"
231 " --nf defines the normal foreground color. (wx)\n"
232 " --hb defines the highlighted background color. (wx)\n"
233 " --hf defines the highlighted foreground color. (wx)\n"
234 " --fbb defines the feedback background color. (wx)\n"
235 " --fbf defines the feedback foreground color. (wx)\n"
236 " --sb defines the selected background color. (wx)\n"
237 " --sf defines the selected foreground color. (wx)\n"
238 " --ab defines the alternating background color. (wx)\n"
239 " --af defines the alternating foreground color. (wx)\n"
240 " --scb defines the scrollbar background color. (wx)\n"
241 " --scf defines the scrollbar foreground color. (wx)\n"
242 " --bdr defines the border color. (wx)\n", out);
243
244 exit((out == stderr ? EXIT_FAILURE : EXIT_SUCCESS));
245 }
246
247 static void
248 set_monitor(struct client *client, char *arg)
249 {
250 char *endptr = NULL;
251 long num = strtol(arg, &endptr, 10);
252 if (arg == endptr) { // No digits found
253 if (!strcmp(arg, "focused")) {
254 client->monitor = -1;
255 } else if (!strcmp(arg, "all")) {
256 client->monitor = -2;
257 } else {
258 client->monitor_name = arg;
259 }
260 } else {
261 client->monitor = num;
262 }
263 }
264
265 static void
266 do_getopt(struct client *client, int *argc, char **argv[])
267 {
268 assert(client && argc && argv);
269
270 static const struct option opts[] = {
271 { "help", no_argument, 0, 'h' },
272 { "version", no_argument, 0, 'v' },
273
274 { "ignorecase", no_argument, 0, 'i' },
275 { "filter", required_argument, 0, 'F' },
276 { "wrap", no_argument, 0, 'w' },
277 { "list", required_argument, 0, 'l' },
278 { "center", no_argument, 0, 'c' },
279 { "prompt", required_argument, 0, 'p' },
280 { "index", required_argument, 0, 'I' },
281 { "prefix", required_argument, 0, 'P' },
282 { "password", required_argument, 0, 'x' },
283 { "fixed-height", no_argument, 0, 0x090 },
284 { "scrollbar", required_argument, 0, 0x100 },
285 { "counter", required_argument, 0, 0x10a },
286 { "vim-esc-exits",no_argument, 0, 'e' },
287 { "vim-normal-mode",no_argument, 0, 'N'},
288 { "accept-single",no_argument, 0, 0x11a },
289 { "auto-select", no_argument, 0, 0x11b },
290 { "ifne", no_argument, 0, 0x117 },
291 { "single-instance",no_argument, 0, 0x11c},
292 { "fork", no_argument, 0, 0x118 },
293 { "no-exec", no_argument, 0, 0x119 },
294 { "bottom", no_argument, 0, 'b' },
295 { "grab", no_argument, 0, 'f' },
296 { "no-overlap", no_argument, 0, 'n' },
297 { "no-spacing", no_argument, 0, 's' },
298 { "no-cursor", no_argument, 0, 'C' },
299 { "no-touch", no_argument, 0, 'T' },
300 { "no-keyboard", no_argument, 0, 'K' },
301 { "monitor", required_argument, 0, 'm' },
302 { "line-height", required_argument, 0, 'H' },
303 { "margin", required_argument, 0, 'M' },
304 { "width-factor", required_argument, 0, 'W' },
305 { "border", required_argument, 0, 'B' },
306 { "border-radius",required_argument, 0, 'R' },
307 { "ch", required_argument, 0, 0x120 },
308 { "cw", required_argument, 0, 0x125 },
309 { "hp", required_argument, 0, 0x122 },
310 { "fn", required_argument, 0, 0x101 },
311 { "tb", required_argument, 0, 0x102 },
312 { "tf", required_argument, 0, 0x103 },
313 { "fb", required_argument, 0, 0x104 },
314 { "ff", required_argument, 0, 0x105 },
315 { "cb", required_argument, 0, 0x126 },
316 { "cf", required_argument, 0, 0x127 },
317 { "nb", required_argument, 0, 0x106 },
318 { "nf", required_argument, 0, 0x107 },
319 { "hb", required_argument, 0, 0x108 },
320 { "hf", required_argument, 0, 0x109 },
321 { "fbb", required_argument, 0, 0x110 },
322 { "fbf", required_argument, 0, 0x111 },
323 { "sb", required_argument, 0, 0x112 },
324 { "sf", required_argument, 0, 0x113 },
325 { "ab", required_argument, 0, 0x123 },
326 { "af", required_argument, 0, 0x124 },
327 { "scb", required_argument, 0, 0x114 },
328 { "scf", required_argument, 0, 0x115 },
329 { "bdr", required_argument, 0, 0x121 },
330 { "binding", required_argument, 0, 0x128 },
331
332 { "disco", no_argument, 0, 0x116 },
333 { 0, 0, 0, 0 }
334 };
335
336 /* TODO: getopt does not support -sf, -sb etc..
337 * Either break the interface and make them --sf, --sb (like they are now),
338 * or parse them before running getopt.. */
339
340 for (optind = 0;;) {
341 int32_t opt;
342
343 if ((opt = getopt_long(*argc, *argv, "hviwcl:I:p:P:I:x:bfF:m:H:M:W:B:R:nsCTK", opts, NULL)) < 0)
344 break;
345
346 switch (opt) {
347 case 'h':
348 usage(stdout, *argv[0]);
349 break;
350 case 'v':
351 version(*argv[0]);
352 break;
353 case 'i':
354 client->filter_mode = BM_FILTER_MODE_DMENU_CASE_INSENSITIVE;
355 break;
356 case 'F':
357 client->initial_filter = optarg;
358 break;
359 case 'w':
360 client->wrap = true;
361 break;
362 case 'l':
363 {
364 char *ptr;
365 client->lines = strtol(optarg, &ptr, 10);
366 client->lines_mode = (*ptr && !strcmp(ptr + 1, "up") ? BM_LINES_UP : BM_LINES_DOWN);
367 break;
368 }
369 case 'c':
370 client->center = true;
371 break;
372 case 'p':
373 client->title = optarg;
374 break;
375 case 'P':
376 client->prefix = optarg;
377 break;
378 case 'I':
379 client->selected = strtol(optarg, NULL, 10);
380 break;
381 case 0x090:
382 client->fixed_height = true;
383 break;
384 case 0x100:
385 client->scrollbar = (!strcmp(optarg, "none") ? BM_SCROLLBAR_NONE : (!strcmp(optarg, "always") ? BM_SCROLLBAR_ALWAYS : (!strcmp(optarg, "autohide") ? BM_SCROLLBAR_AUTOHIDE : BM_SCROLLBAR_NONE)));
386 break;
387 case 0x10a:
388 client->counter = (!strcmp(optarg, "always"));
389 break;
390 case 'e':
391 client->vim_esc_exits = true;
392 break;
393 case 'N':
394 client->vim_init_mode_normal = true;
395 break;
396 case 0x11a:
397 client->accept_single = true;
398 break;
399 case 0x11b:
400 client->auto_select = true;
401 break;
402 case 0x117:
403 client->ifne = true;
404 break;
405 case 0x11c:
406 client->single_instance = true;
407 break;
408 case 0x118:
409 client->force_fork = true;
410 break;
411 case 0x119:
412 client->no_exec = true;
413 break;
414 case 'x':
415 client->password = (!strcmp(optarg, "none") ? BM_PASSWORD_NONE : (!strcmp(optarg, "hide") ? BM_PASSWORD_HIDE : (!strcmp(optarg, "indicator") ? BM_PASSWORD_INDICATOR : BM_PASSWORD_NONE)));
416 break;
417 case 'b':
418 client->bottom = true;
419 break;
420 case 'f':
421 client->grab = true;
422 break;
423 case 'm':
424 set_monitor(client, optarg);
425 break;
426 case 'n':
427 client->no_overlap = true;
428 break;
429 case 's':
430 client->no_spacing = true;
431 break;
432 case 'C':
433 client->no_cursor = true;
434 break;
435 case 'T':
436 client->no_touch = true;
437 break;
438 case 'K':
439 client->no_keyboard = true;
440 break;
441 case 'H':
442 client->line_height = strtol(optarg, NULL, 10);
443 break;
444 case 'M':
445 client->hmargin_size = strtol(optarg, NULL, 10);
446 break;
447 case 'W':
448 client->width_factor = strtof(optarg, NULL);
449 break;
450 case 'B':
451 client->border_size = strtod(optarg, NULL);
452 break;
453 case 'R':
454 client->border_radius = strtod(optarg, NULL);
455 break;
456 case 0x120:
457 client->cursor_height = strtol(optarg, NULL, 10);
458 break;
459 case 0x125:
460 client->cursor_width = strtol(optarg, NULL, 10);
461 break;
462 case 0x122:
463 client->hpadding = strtol(optarg, NULL, 10);
464 break;
465 case 0x101:
466 client->font = optarg;
467 break;
468 case 0x102:
469 client->colors[BM_COLOR_TITLE_BG] = optarg;
470 break;
471 case 0x103:
472 client->colors[BM_COLOR_TITLE_FG] = optarg;
473 break;
474 case 0x104:
475 client->colors[BM_COLOR_FILTER_BG] = optarg;
476 break;
477 case 0x105:
478 client->colors[BM_COLOR_FILTER_FG] = optarg;
479 break;
480 case 0x126:
481 client->colors[BM_COLOR_CURSOR_BG] = optarg;
482 break;
483 case 0x127:
484 client->colors[BM_COLOR_CURSOR_FG] = optarg;
485 break;
486 case 0x106:
487 client->colors[BM_COLOR_ITEM_BG] = optarg;
488 break;
489 case 0x107:
490 client->colors[BM_COLOR_ITEM_FG] = optarg;
491 break;
492 case 0x108:
493 client->colors[BM_COLOR_HIGHLIGHTED_BG] = optarg;
494 break;
495 case 0x109:
496 client->colors[BM_COLOR_HIGHLIGHTED_FG] = optarg;
497 break;
498 case 0x110:
499 client->colors[BM_COLOR_FEEDBACK_BG] = optarg;
500 break;
501 case 0x111:
502 client->colors[BM_COLOR_FEEDBACK_FG] = optarg;
503 break;
504 case 0x112:
505 client->colors[BM_COLOR_SELECTED_BG] = optarg;
506 break;
507 case 0x113:
508 client->colors[BM_COLOR_SELECTED_FG] = optarg;
509 break;
510 case 0x123:
511 client->colors[BM_COLOR_ALTERNATE_BG] = optarg;
512 break;
513 case 0x124:
514 client->colors[BM_COLOR_ALTERNATE_FG] = optarg;
515 break;
516 case 0x114:
517 client->colors[BM_COLOR_SCROLLBAR_BG] = optarg;
518 break;
519 case 0x115:
520 client->colors[BM_COLOR_SCROLLBAR_FG] = optarg;
521 break;
522 case 0x121:
523 client->colors[BM_COLOR_BORDER] = optarg;
524 break;
525 case 0x128:
526 if(strcmp(optarg, "vim") == 0){
527 client->key_binding = BM_KEY_BINDING_VIM;
528 } else {
529 client->key_binding = BM_KEY_BINDING_DEFAULT;
530 }
531 break;
532
533 case 0x116:
534 disco();
535 break;
536
537 case ':':
538 case '?':
539 fputs("\n", stderr);
540 usage(stderr, *argv[0]);
541 break;
542 }
543 }
544
545 *argc -= optind;
546 *argv += optind;
547 }
548
549 void
550 parse_args(struct client *client, int *argc, char **argv[])
551 {
552 int num_opts;
553 char **opts;
554 const char *env;
555 if ((env = getenv("BEMENU_OPTS")) && (opts = tokenize_quoted_to_argv(env, (*argv)[0], &num_opts)))
556 do_getopt(client, &num_opts, &opts);
557 do_getopt(client, argc, argv);
558 }
559
560 struct bm_menu*
561 menu_with_options(struct client *client)
562 {
563 struct bm_menu *menu;
564 if (!(menu = bm_menu_new(NULL)))
565 return NULL;
566
567 if (client->vim_init_mode_normal) {
568 menu->vim_mode = 'n';
569 }
570
571 client->fork = (client->force_fork || (bm_renderer_get_priorty(bm_menu_get_renderer(menu)) != BM_PRIO_TERMINAL));
572
573 bm_menu_set_font(menu, client->font);
574 bm_menu_set_line_height(menu, client->line_height);
575 bm_menu_set_cursor_height(menu, client->cursor_height);
576 bm_menu_set_cursor_width(menu, client->cursor_width);
577 bm_menu_set_hpadding(menu, client->hpadding);
578 bm_menu_set_title(menu, client->title);
579 bm_menu_set_prefix(menu, client->prefix);
580 bm_menu_set_filter_mode(menu, client->filter_mode);
581 bm_menu_set_lines(menu, client->lines);
582 bm_menu_set_lines_mode(menu, client->lines_mode);
583 bm_menu_set_wrap(menu, client->wrap);
584 bm_menu_set_monitor(menu, client->monitor);
585 bm_menu_set_monitor_name(menu, client->monitor_name);
586 bm_menu_set_fixed_height(menu, client->fixed_height);
587 bm_menu_set_scrollbar(menu, client->scrollbar);
588 bm_menu_set_counter(menu, client->counter);
589 bm_menu_set_vim_esc_exits(menu, client->vim_esc_exits);
590 bm_menu_set_panel_overlap(menu, !client->no_overlap);
591 bm_menu_set_spacing(menu, !client->no_spacing);
592 bm_menu_set_password(menu, client->password);
593 bm_menu_set_width(menu, client->hmargin_size, client->width_factor);
594 bm_menu_set_border_size(menu, client->border_size);
595 bm_menu_set_border_radius(menu, client->border_radius);
596 bm_menu_set_key_binding(menu, client->key_binding);
597
598 if (client->center) {
599 bm_menu_set_align(menu, BM_ALIGN_CENTER);
600 } else if (client->bottom) {
601 bm_menu_set_align(menu, BM_ALIGN_BOTTOM);
602 } else {
603 bm_menu_set_align(menu, BM_ALIGN_TOP);
604 }
605
606 for (uint32_t i = 0; i < BM_COLOR_LAST; ++i)
607 bm_menu_set_color(menu, i, client->colors[i]);
608
609 if (client->grab) {
610 bm_menu_set_filter(menu, "Loading...");
611 // bm_menu_grab_keyboard(menu, true);
612 bm_menu_render(menu);
613 bm_menu_set_filter(menu, NULL);
614 }
615
616 return menu;
617 }
618
619 enum bm_run_result
620 run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(const struct client *client, struct bm_item *item))
621 {
622 if (client->single_instance) {
623 char *xdg_runtime_dir = getenv("XDG_RUNTIME_DIR");
624
625 char buffer[1024];
626 char *bemenu_lock_location = (xdg_runtime_dir == NULL ? "/tmp" : xdg_runtime_dir);
627 snprintf(buffer, sizeof(buffer), "%s/bemenu.lock", bemenu_lock_location);
628
629 int menu_lock = open(buffer, O_CREAT | O_RDWR | O_CLOEXEC, 0666); // Create a read-write lock file(if not already exists), closes on exec. Creates in tmp due to permissions.
630 if (flock(menu_lock, LOCK_EX | LOCK_NB) == -1) { // If we cannot set the lock/the lock is already present.
631 fprintf(stderr, "bemenu instance is already running");
632 exit(EXIT_FAILURE);
633 }
634 }
635
636 bm_menu_set_highlighted_index(menu, client->selected);
637 bm_menu_grab_keyboard(menu, true);
638 bm_menu_set_filter(menu, client->initial_filter);
639 bm_menu_filter(menu);
640
641 {
642 uint32_t item_count;
643 struct bm_item **items = bm_menu_get_filtered_items(menu, &item_count);
644
645 if (client->ifne && item_count == 0) {
646 return BM_RUN_RESULT_CANCEL;
647 }
648
649 if ((client->accept_single || client->auto_select) && item_count == 1) {
650 item_cb(client, *items);
651 return BM_RUN_RESULT_SELECTED;
652 }
653
654 }
655
656 uint32_t unicode;
657 enum bm_key key = BM_KEY_NONE;
658 struct bm_pointer pointer = {0};
659 struct bm_touch touch = {0};
660 enum bm_run_result status = BM_RUN_RESULT_RUNNING;
661 do {
662 if(client->auto_select) {
663 uint32_t item_count;
664 bm_menu_get_filtered_items(menu, &item_count);
665 if(item_count == 1) {
666 struct bm_item *highlighted = bm_menu_get_highlighted_item(menu);
667 if (highlighted) {
668 item_cb(client, highlighted);
669 return BM_RUN_RESULT_SELECTED;
670 }
671 }
672 }
673
674 if (!bm_menu_render(menu)) {
675 status = BM_RUN_RESULT_CANCEL;
676 break;
677 }
678 if (!client->no_keyboard) {
679 key = bm_menu_poll_key(menu, &unicode);
680 }
681 if (!client->no_cursor) {
682 pointer = bm_menu_poll_pointer(menu);
683 }
684 if (!client->no_touch) {
685 touch = bm_menu_poll_touch(menu);
686 }
687 } while ((status = bm_menu_run_with_events(menu, key, pointer, touch, unicode)) == BM_RUN_RESULT_RUNNING);
688
689 switch (status) {
690 case BM_RUN_RESULT_SELECTED:
691 case BM_RUN_RESULT_CUSTOM_1:
692 case BM_RUN_RESULT_CUSTOM_2:
693 case BM_RUN_RESULT_CUSTOM_3:
694 case BM_RUN_RESULT_CUSTOM_4:
695 case BM_RUN_RESULT_CUSTOM_5:
696 case BM_RUN_RESULT_CUSTOM_6:
697 case BM_RUN_RESULT_CUSTOM_7:
698 case BM_RUN_RESULT_CUSTOM_8:
699 case BM_RUN_RESULT_CUSTOM_9:
700 case BM_RUN_RESULT_CUSTOM_10:
701 {
702 uint32_t i, count;
703 struct bm_item **items = bm_menu_get_selected_items(menu, &count);
704 for (i = 0; i < count; ++i) item_cb(client, items[i]);
705 }
706 break;
707 default: break;
708 }
709
710 return status;
711 }
712
713 /* vim: set ts=8 sw=4 tw=0 :*/