forked from RepoMirrors/bemenu
Add custom key support
This commit is contained in:
parent
20661aed78
commit
64c38dde50
@ -81,7 +81,24 @@ main(int argc, char **argv)
|
||||
read_items_to_menu_from_stdin(menu);
|
||||
const enum bm_run_result status = run_menu(&client, menu, item_cb);
|
||||
bm_menu_free(menu);
|
||||
return (status == BM_RUN_RESULT_SELECTED ? EXIT_SUCCESS : EXIT_FAILURE);
|
||||
switch (status) {
|
||||
case BM_RUN_RESULT_SELECTED:
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
case BM_RUN_RESULT_CUSTOM_1: return 10;
|
||||
case BM_RUN_RESULT_CUSTOM_2: return 11;
|
||||
case BM_RUN_RESULT_CUSTOM_3: return 12;
|
||||
case BM_RUN_RESULT_CUSTOM_4: return 13;
|
||||
case BM_RUN_RESULT_CUSTOM_5: return 14;
|
||||
case BM_RUN_RESULT_CUSTOM_6: return 15;
|
||||
case BM_RUN_RESULT_CUSTOM_7: return 16;
|
||||
case BM_RUN_RESULT_CUSTOM_8: return 17;
|
||||
case BM_RUN_RESULT_CUSTOM_9: return 18;
|
||||
case BM_RUN_RESULT_CUSTOM_10: return 19;
|
||||
|
||||
default:
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
/* vim: set ts=8 sw=4 tw=0 :*/
|
||||
|
@ -436,10 +436,25 @@ run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(cons
|
||||
key = bm_menu_poll_key(menu, &unicode);
|
||||
} while ((status = bm_menu_run_with_key(menu, key, unicode)) == BM_RUN_RESULT_RUNNING);
|
||||
|
||||
if (status == BM_RUN_RESULT_SELECTED) {
|
||||
uint32_t i, count;
|
||||
struct bm_item **items = bm_menu_get_selected_items(menu, &count);
|
||||
for (i = 0; i < count; ++i) item_cb(client, items[i]);
|
||||
switch (status) {
|
||||
case BM_RUN_RESULT_SELECTED:
|
||||
case BM_RUN_RESULT_CUSTOM_1:
|
||||
case BM_RUN_RESULT_CUSTOM_2:
|
||||
case BM_RUN_RESULT_CUSTOM_3:
|
||||
case BM_RUN_RESULT_CUSTOM_4:
|
||||
case BM_RUN_RESULT_CUSTOM_5:
|
||||
case BM_RUN_RESULT_CUSTOM_6:
|
||||
case BM_RUN_RESULT_CUSTOM_7:
|
||||
case BM_RUN_RESULT_CUSTOM_8:
|
||||
case BM_RUN_RESULT_CUSTOM_9:
|
||||
case BM_RUN_RESULT_CUSTOM_10:
|
||||
{
|
||||
uint32_t i, count;
|
||||
struct bm_item **items = bm_menu_get_selected_items(menu, &count);
|
||||
for (i = 0; i < count; ++i) item_cb(client, items[i]);
|
||||
}
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
|
||||
return status;
|
||||
|
20
lib/bemenu.h
20
lib/bemenu.h
@ -162,6 +162,16 @@ enum bm_run_result {
|
||||
BM_RUN_RESULT_RUNNING,
|
||||
BM_RUN_RESULT_SELECTED,
|
||||
BM_RUN_RESULT_CANCEL,
|
||||
BM_RUN_RESULT_CUSTOM_1,
|
||||
BM_RUN_RESULT_CUSTOM_2,
|
||||
BM_RUN_RESULT_CUSTOM_3,
|
||||
BM_RUN_RESULT_CUSTOM_4,
|
||||
BM_RUN_RESULT_CUSTOM_5,
|
||||
BM_RUN_RESULT_CUSTOM_6,
|
||||
BM_RUN_RESULT_CUSTOM_7,
|
||||
BM_RUN_RESULT_CUSTOM_8,
|
||||
BM_RUN_RESULT_CUSTOM_9,
|
||||
BM_RUN_RESULT_CUSTOM_10,
|
||||
};
|
||||
|
||||
/**
|
||||
@ -192,6 +202,16 @@ enum bm_key {
|
||||
BM_KEY_RETURN,
|
||||
BM_KEY_SHIFT_RETURN,
|
||||
BM_KEY_CONTROL_RETURN,
|
||||
BM_KEY_CUSTOM_1,
|
||||
BM_KEY_CUSTOM_2,
|
||||
BM_KEY_CUSTOM_3,
|
||||
BM_KEY_CUSTOM_4,
|
||||
BM_KEY_CUSTOM_5,
|
||||
BM_KEY_CUSTOM_6,
|
||||
BM_KEY_CUSTOM_7,
|
||||
BM_KEY_CUSTOM_8,
|
||||
BM_KEY_CUSTOM_9,
|
||||
BM_KEY_CUSTOM_10,
|
||||
BM_KEY_UNICODE,
|
||||
BM_KEY_LAST
|
||||
};
|
||||
|
34
lib/menu.c
34
lib/menu.c
@ -777,6 +777,16 @@ bm_menu_run_with_key(struct bm_menu *menu, enum bm_key key, uint32_t unicode)
|
||||
|
||||
case BM_KEY_CONTROL_RETURN:
|
||||
case BM_KEY_RETURN:
|
||||
case BM_KEY_CUSTOM_1:
|
||||
case BM_KEY_CUSTOM_2:
|
||||
case BM_KEY_CUSTOM_3:
|
||||
case BM_KEY_CUSTOM_4:
|
||||
case BM_KEY_CUSTOM_5:
|
||||
case BM_KEY_CUSTOM_6:
|
||||
case BM_KEY_CUSTOM_7:
|
||||
case BM_KEY_CUSTOM_8:
|
||||
case BM_KEY_CUSTOM_9:
|
||||
case BM_KEY_CUSTOM_10:
|
||||
{
|
||||
struct bm_item *highlighted = bm_menu_get_highlighted_item(menu);
|
||||
if (highlighted && !bm_menu_item_is_selected(menu, highlighted))
|
||||
@ -795,13 +805,37 @@ bm_menu_run_with_key(struct bm_menu *menu, enum bm_key key, uint32_t unicode)
|
||||
bm_menu_filter(menu);
|
||||
|
||||
switch (key) {
|
||||
case BM_KEY_CUSTOM_1:
|
||||
case BM_KEY_CUSTOM_2:
|
||||
case BM_KEY_CUSTOM_3:
|
||||
case BM_KEY_CUSTOM_4:
|
||||
case BM_KEY_CUSTOM_5:
|
||||
case BM_KEY_CUSTOM_6:
|
||||
case BM_KEY_CUSTOM_7:
|
||||
case BM_KEY_CUSTOM_8:
|
||||
case BM_KEY_CUSTOM_9:
|
||||
case BM_KEY_CUSTOM_10:
|
||||
case BM_KEY_SHIFT_RETURN:
|
||||
case BM_KEY_RETURN:
|
||||
if (!bm_menu_get_selected_items(menu, NULL)) {
|
||||
bm_item_set_text(menu->filter_item, menu->filter);
|
||||
list_add_item(&menu->selection, menu->filter_item);
|
||||
}
|
||||
switch (key) {
|
||||
case BM_KEY_CUSTOM_1: return BM_RUN_RESULT_CUSTOM_1;
|
||||
case BM_KEY_CUSTOM_2: return BM_RUN_RESULT_CUSTOM_2;
|
||||
case BM_KEY_CUSTOM_3: return BM_RUN_RESULT_CUSTOM_3;
|
||||
case BM_KEY_CUSTOM_4: return BM_RUN_RESULT_CUSTOM_4;
|
||||
case BM_KEY_CUSTOM_5: return BM_RUN_RESULT_CUSTOM_5;
|
||||
case BM_KEY_CUSTOM_6: return BM_RUN_RESULT_CUSTOM_6;
|
||||
case BM_KEY_CUSTOM_7: return BM_RUN_RESULT_CUSTOM_7;
|
||||
case BM_KEY_CUSTOM_8: return BM_RUN_RESULT_CUSTOM_8;
|
||||
case BM_KEY_CUSTOM_9: return BM_RUN_RESULT_CUSTOM_9;
|
||||
case BM_KEY_CUSTOM_10: return BM_RUN_RESULT_CUSTOM_10;
|
||||
default: break;
|
||||
}
|
||||
return BM_RUN_RESULT_SELECTED;
|
||||
|
||||
case BM_KEY_ESCAPE: return BM_RUN_RESULT_CANCEL;
|
||||
default: break;
|
||||
}
|
||||
|
@ -290,12 +290,31 @@ poll_key(const struct bm_menu *menu, uint32_t *unicode)
|
||||
assert(unicode);
|
||||
*unicode = 0;
|
||||
curses.polled_once = true;
|
||||
bool alt = false;
|
||||
|
||||
if (!curses.stdscreen || curses.should_terminate)
|
||||
return BM_KEY_NONE;
|
||||
|
||||
get_wch((wint_t*)unicode);
|
||||
|
||||
// alt is sent as esc, so read another character to determine if this is alt+char.
|
||||
if (*unicode == 27){
|
||||
nodelay(curses.stdscreen, true);
|
||||
int ch = getch();
|
||||
if (ch != ERR){
|
||||
printf("alt-%d\n", ch);
|
||||
*unicode = ch;
|
||||
alt = true;
|
||||
}
|
||||
nodelay(curses.stdscreen, false);
|
||||
}
|
||||
|
||||
// some terminals set high ascii bit when pressing alt
|
||||
if (*unicode & (1 << 7)) {
|
||||
*unicode &= ~(1 << 7);
|
||||
alt = true;
|
||||
}
|
||||
|
||||
switch (*unicode) {
|
||||
#if KEY_RESIZE
|
||||
case KEY_RESIZE:
|
||||
@ -381,6 +400,37 @@ poll_key(const struct bm_menu *menu, uint32_t *unicode)
|
||||
terminate();
|
||||
return BM_KEY_RETURN;
|
||||
|
||||
case 49:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_1; }
|
||||
break;
|
||||
case 50:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_2; }
|
||||
break;
|
||||
case 51:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_3; }
|
||||
break;
|
||||
case 52:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_4; }
|
||||
break;
|
||||
case 53:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_5; }
|
||||
break;
|
||||
case 54:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_6; }
|
||||
break;
|
||||
case 55:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_7; }
|
||||
break;
|
||||
case 56:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_8; }
|
||||
break;
|
||||
case 57:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_9; }
|
||||
break;
|
||||
case 48:
|
||||
if (alt) { terminate(); return BM_KEY_CUSTOM_10; }
|
||||
break;
|
||||
|
||||
case 7: /* C-g */
|
||||
case 27: /* Escape */
|
||||
terminate();
|
||||
|
@ -170,6 +170,37 @@ poll_key(const struct bm_menu *menu, unsigned int *unicode)
|
||||
case XKB_KEY_m:
|
||||
return (mods & MOD_CTRL ? BM_KEY_RETURN : BM_KEY_UNICODE);
|
||||
|
||||
case XKB_KEY_1:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_1;
|
||||
break;
|
||||
case XKB_KEY_2:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_2;
|
||||
break;
|
||||
case XKB_KEY_3:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_3;
|
||||
break;
|
||||
case XKB_KEY_4:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_4;
|
||||
break;
|
||||
case XKB_KEY_5:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_5;
|
||||
break;
|
||||
case XKB_KEY_6:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_6;
|
||||
break;
|
||||
case XKB_KEY_7:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_7;
|
||||
break;
|
||||
case XKB_KEY_8:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_8;
|
||||
break;
|
||||
case XKB_KEY_9:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_9;
|
||||
break;
|
||||
case XKB_KEY_0:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_10;
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
@ -150,6 +150,37 @@ poll_key(const struct bm_menu *menu, unsigned int *unicode)
|
||||
case XK_m:
|
||||
return (mods & MOD_CTRL ? BM_KEY_RETURN : BM_KEY_UNICODE);
|
||||
|
||||
case XK_1:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_1;
|
||||
break;
|
||||
case XK_2:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_2;
|
||||
break;
|
||||
case XK_3:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_3;
|
||||
break;
|
||||
case XK_4:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_4;
|
||||
break;
|
||||
case XK_5:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_5;
|
||||
break;
|
||||
case XK_6:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_6;
|
||||
break;
|
||||
case XK_7:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_7;
|
||||
break;
|
||||
case XK_8:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_8;
|
||||
break;
|
||||
case XK_9:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_9;
|
||||
break;
|
||||
case XK_0:
|
||||
if ((mods & MOD_ALT)) return BM_KEY_CUSTOM_10;
|
||||
break;
|
||||
|
||||
default: break;
|
||||
}
|
||||
|
||||
|
@ -185,6 +185,7 @@ These options are only available on backends specified in the parentheses
|
||||
.SH EXIT STATUS
|
||||
|
||||
0 when the user selects an option, 1 when the user aborts the selection.
|
||||
If the user uses the custom action, exit code will be 10-19 dependent on which custom action was chosen.
|
||||
|
||||
.SH ENVIRONMENT
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user