diff --git a/client/bemenu-run.c b/client/bemenu-run.c index 8dd9ac3..9fe5614 100644 --- a/client/bemenu-run.c +++ b/client/bemenu-run.c @@ -251,6 +251,13 @@ launch(const char *bin) } } +static void +item_cb(struct bm_item *item, const char *text) +{ + (void)item; // may be null + launch(text); +} + int main(int argc, char **argv) { @@ -272,22 +279,7 @@ main(int argc, char **argv) return EXIT_FAILURE; read_items_to_menu_from_path(menu); - bm_menu_set_highlighted_index(menu, client.selected); - - enum bm_run_result status = run_menu(menu); - - 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) { - const char *text = bm_item_get_text(items[i]); - launch(text); - } - - if (!count && bm_menu_get_filter(menu)) - launch(bm_menu_get_filter(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); } diff --git a/client/bemenu.c b/client/bemenu.c index d909a17..9fcbcdc 100644 --- a/client/bemenu.c +++ b/client/bemenu.c @@ -57,6 +57,13 @@ read_items_to_menu_from_stdin(struct bm_menu *menu) free(buffer); } +static void +item_cb(struct bm_item *item, const char *text) +{ + (void)item; // may be null + printf("%s\n", (text ? text : "")); +} + int main(int argc, char **argv) { @@ -70,22 +77,7 @@ main(int argc, char **argv) return EXIT_FAILURE; read_items_to_menu_from_stdin(menu); - bm_menu_set_highlighted_index(menu, client.selected); - - enum bm_run_result status = run_menu(menu); - - 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) { - const char *text = bm_item_get_text(items[i]); - printf("%s\n", (text ? text : "")); - } - - if (!count && bm_menu_get_filter(menu)) - printf("%s\n", bm_menu_get_filter(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); } diff --git a/client/common/common.c b/client/common/common.c index 50ff848..35bc9e3 100644 --- a/client/common/common.c +++ b/client/common/common.c @@ -275,8 +275,9 @@ menu_with_options(const struct client *client) } enum bm_run_result -run_menu(struct bm_menu *menu) +run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(struct bm_item *item, const char *text)) { + bm_menu_set_highlighted_index(menu, client->selected); bm_menu_grab_keyboard(menu, true); uint32_t unicode; @@ -286,6 +287,19 @@ run_menu(struct bm_menu *menu) bm_menu_render(menu); 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) { + const char *text = bm_item_get_text(items[i]); + item_cb(items[i], text); + } + + if (!count && bm_menu_get_filter(menu)) + item_cb(NULL, bm_menu_get_filter(menu)); + } + return status; } diff --git a/client/common/common.h b/client/common/common.h index 0e53835..af03127 100644 --- a/client/common/common.h +++ b/client/common/common.h @@ -20,7 +20,7 @@ struct client { void parse_args(struct client *client, int *argc, char **argv[]); struct bm_menu* menu_with_options(const struct client *client); -enum bm_run_result run_menu(struct bm_menu *menu); +enum bm_run_result run_menu(const struct client *client, struct bm_menu *menu, void (*item_cb)(struct bm_item *item, const char *text)); #endif /* _BM_COMMON_H_ */