From 074b2d1b5894f12fae78a569ea507612616663ff Mon Sep 17 00:00:00 2001 From: Jari Vetoniemi Date: Sat, 17 Jan 2015 18:53:29 +0200 Subject: [PATCH] Remove --backend and --prioritory options, change prioritory -> priorty and provide BEMENU_BACKEND env variable. Change API to reflect this change. There is no reliable way to detect when running on terminal (especially when stdin is a pipe), so we need to make curses backend explicit with BEMENU_BACKEND=curses or bm_menu_new("curses"), otherwise GUI backend will be choosed automatically. --- client/bemenu-run.c | 1 - client/bemenu.c | 1 - client/common/common.c | 48 +++++++++++++++------------------ client/common/common.h | 1 - lib/bemenu.h | 26 +++++++++--------- lib/internal.h | 4 +-- lib/library.c | 8 +++--- lib/menu.c | 18 ++++++++++--- lib/renderers/curses/curses.c | 2 +- lib/renderers/wayland/wayland.c | 2 +- lib/renderers/x11/x11.c | 2 +- test/bm_menu_new.c | 2 +- 12 files changed, 58 insertions(+), 57 deletions(-) diff --git a/client/bemenu-run.c b/client/bemenu-run.c index b68e715..0efc32a 100644 --- a/client/bemenu-run.c +++ b/client/bemenu-run.c @@ -8,7 +8,6 @@ #include "../lib/3rdparty/tinydir.h" static struct client client = { - .prioritory = BM_PRIO_ANY, .filter_mode = BM_FILTER_MODE_DMENU, .wrap = 0, .lines = 0, diff --git a/client/bemenu.c b/client/bemenu.c index 338617c..e441f3d 100644 --- a/client/bemenu.c +++ b/client/bemenu.c @@ -5,7 +5,6 @@ #include "common.h" static struct client client = { - .prioritory = BM_PRIO_ANY, .filter_mode = BM_FILTER_MODE_DMENU, .wrap = 0, .lines = 0, diff --git a/client/common/common.c b/client/common/common.c index 6bb2ec7..52f71bc 100644 --- a/client/common/common.c +++ b/client/common/common.c @@ -67,9 +67,14 @@ usage(FILE *out, const char *name) " -l, --list list items vertically with the given number of lines.\n" " -p, --prompt defines the prompt text to be displayed.\n" " -P, --prefix text to shown before highlighted item.\n" - " -I, --index select item at index automatically.\n" - " --backend options: curses, wayland, x11\n" - " --prioritory options: terminal, gui\n\n" + " -I, --index select item at index automatically.\n\n" + + "Use BEMENU_BACKEND env variable to force backend:\n" + " curses ncurses based terminal backend\n" + " wayland wayland backend\n" + " x11 x11 backend\n\n" + + "If BEMENU_BACKEND is empty, one of the GUI backends is selected automatically.\n\n" "Backend specific options\n" " c = ncurses, w == wayland, x == x11\n" @@ -109,26 +114,24 @@ parse_args(struct client *client, int *argc, char **argv[]) { "prompt", required_argument, 0, 'p' }, { "index", required_argument, 0, 'I' }, { "prefix", required_argument, 0, 'P' }, - { "backend", required_argument, 0, 0x100 }, - { "prioritory", required_argument, 0, 0x101 }, { "bottom", no_argument, 0, 'b' }, { "grab", no_argument, 0, 'f' }, { "monitor", required_argument, 0, 'm' }, - { "fn", required_argument, 0, 0x102 }, - { "bg", required_argument, 0, 0x103 }, - { "tb", required_argument, 0, 0x104 }, - { "tf", required_argument, 0, 0x105 }, - { "fb", required_argument, 0, 0x106 }, - { "ff", required_argument, 0, 0x107 }, - { "nb", required_argument, 0, 0x108 }, - { "nf", required_argument, 0, 0x109 }, - { "hb", required_argument, 0, 0x110 }, - { "hf", required_argument, 0, 0x111 }, - { "sb", required_argument, 0, 0x112 }, - { "sf", required_argument, 0, 0x113 }, + { "fn", required_argument, 0, 0x100 }, + { "bg", required_argument, 0, 0x101 }, + { "tb", required_argument, 0, 0x102 }, + { "tf", required_argument, 0, 0x103 }, + { "fb", required_argument, 0, 0x104 }, + { "ff", required_argument, 0, 0x105 }, + { "nb", required_argument, 0, 0x106 }, + { "nf", required_argument, 0, 0x107 }, + { "hb", required_argument, 0, 0x108 }, + { "hf", required_argument, 0, 0x109 }, + { "sb", required_argument, 0, 0x110 }, + { "sf", required_argument, 0, 0x111 }, - { "disco", no_argument, 0, 0x114 }, + { "disco", no_argument, 0, 0x112 }, { 0, 0, 0, 0 } }; @@ -172,13 +175,6 @@ parse_args(struct client *client, int *argc, char **argv[]) client->renderer = optarg; break; - case 0x101: - if (!strcmp(optarg, "terminal")) - client->prioritory = BM_PRIO_TERMINAL; - else if (!strcmp(optarg, "gui")) - client->prioritory = BM_PRIO_GUI; - break; - case 'b': client->bottom = 1; break; @@ -246,7 +242,7 @@ struct bm_menu* menu_with_options(struct client *client) { struct bm_menu *menu; - if (!(menu = bm_menu_new(client->renderer, client->prioritory))) + if (!(menu = bm_menu_new(NULL))) return NULL; bm_menu_set_font(menu, client->font); diff --git a/client/common/common.h b/client/common/common.h index bae498f..284b962 100644 --- a/client/common/common.h +++ b/client/common/common.h @@ -4,7 +4,6 @@ #include struct client { - enum bm_prioritory prioritory; enum bm_filter_mode filter_mode; int32_t wrap; uint32_t lines; diff --git a/lib/bemenu.h b/lib/bemenu.h index 63cf634..54d82f9 100644 --- a/lib/bemenu.h +++ b/lib/bemenu.h @@ -44,8 +44,10 @@ struct bm_item; * @{ */ /** - * Init bemenu. - * Loads up the renderers. + * Init bemenu, loads up the renderers. + * + * You can force single renderer with BEMENU_RENDERER env variable, + * and directory containing renderers with BEMENU_RENDERERS env variable. * * @return true on success, false on failure. */ @@ -85,13 +87,7 @@ const char* bm_version(void); /** * Prioritories for renderer plugins. */ -enum bm_prioritory { - /** - * Do not use this in renderers. - * This flag is for bm_menu_new, if any renderer is fine. - */ - BM_PRIO_ANY, - +enum bm_priorty { /** * Renderer runs in terminal. */ @@ -112,12 +108,12 @@ enum bm_prioritory { const char* bm_renderer_get_name(const struct bm_renderer *renderer); /** - * Get prioritory of the renderer. + * Get priorty of the renderer. * * @param renderer bm_renderer instance. - * @return bm_prioritory enum value. + * @return bm_priorty enum value. */ -enum bm_prioritory bm_renderer_get_prioritory(const struct bm_renderer *renderer); +enum bm_priorty bm_renderer_get_priorty(const struct bm_renderer *renderer); /** * @} Renderer */ @@ -210,11 +206,13 @@ enum bm_color { /** * Create new bm_menu instance. * + * If **NULL** is used as renderer, auto-detection will be used or the renderer with the name pointed by BEMENU_BACKEND env variable. + * It's good idea to use NULL, if you want user to have control over the renderer with this env variable. + * * @param renderer Name of renderer to be used for this instance, pass **NULL** for auto-detection. - * @param prioritory @link ::bm_prioritory @endlink enum for which kind of renderer is wanted. Pass BM_PRIO_ANY, for anything. * @return bm_menu for new menu instance, **NULL** if creation failed. */ -struct bm_menu* bm_menu_new(const char *renderer, enum bm_prioritory prioritory); +struct bm_menu* bm_menu_new(const char *renderer); /** * Release bm_menu instance. diff --git a/lib/internal.h b/lib/internal.h index 112e7bd..7b71eb4 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -93,10 +93,10 @@ struct render_api { const char *version; /** - * Prioritory of the plugin. + * Priorty of the plugin. * Terminal renderers should be first, then graphicals. */ - enum bm_prioritory prioritory; + enum bm_priorty priorty; }; /** diff --git a/lib/library.c b/lib/library.c index 2dc59fc..02c2af8 100644 --- a/lib/library.c +++ b/lib/library.c @@ -67,7 +67,7 @@ static int compare(const void *a, const void *b) { const struct bm_renderer *ra = *(struct bm_renderer**)a, *rb = *(struct bm_renderer**)b; - return (ra->api.prioritory > rb->api.prioritory); + return (ra->api.priorty > rb->api.priorty); } static bool @@ -180,11 +180,11 @@ bm_renderer_get_name(const struct bm_renderer *renderer) return renderer->name; } -enum bm_prioritory -bm_renderer_get_prioritory(const struct bm_renderer *renderer) +enum bm_priorty +bm_renderer_get_priorty(const struct bm_renderer *renderer) { assert(renderer); - return renderer->api.prioritory; + return renderer->api.priorty; } /* vim: set ts=8 sw=4 tw=0 :*/ diff --git a/lib/menu.c b/lib/menu.c index 5ab5286..4fc00e7 100644 --- a/lib/menu.c +++ b/lib/menu.c @@ -49,7 +49,7 @@ bm_menu_item_is_selected(const struct bm_menu *menu, const struct bm_item *item) } struct bm_menu* -bm_menu_new(const char *renderer, enum bm_prioritory prioritory) +bm_menu_new(const char *renderer) { struct bm_menu *menu; if (!(menu = calloc(1, sizeof(struct bm_menu)))) @@ -58,14 +58,24 @@ bm_menu_new(const char *renderer, enum bm_prioritory prioritory) uint32_t count; const struct bm_renderer **renderers = bm_get_renderers(&count); + const char *name = secure_getenv("BEMENU_BACKEND"); + name = (name && strlen(name) > 0 ? name : NULL); + for (uint32_t i = 0; i < count; ++i) { - if (prioritory != BM_PRIO_ANY && renderers[i]->api.prioritory != prioritory) + if (!name && !renderer && renderers[i]->api.priorty != BM_PRIO_GUI) continue; - if (renderer && strcmp(renderer, renderers[i]->name)) + if ((renderer && strcmp(renderer, renderers[i]->name)) || (name && strcmp(name, renderers[i]->name))) continue; - if (renderers[i]->api.prioritory == BM_PRIO_TERMINAL) { + if (renderers[i]->api.priorty == BM_PRIO_TERMINAL) { + /** + * Some sanity checks that we are in terminal. + * These however are not reliable, thus we don't auto-detect terminal based renderers. + * These will be only used when explicitly requested. + * + * Launching terminal based menu instance at background is not a good idea. + */ const char *term = getenv("TERM"); if (!term || !strlen(term) || getppid() == 1) continue; diff --git a/lib/renderers/curses/curses.c b/lib/renderers/curses/curses.c index 78449ac..af54846 100644 --- a/lib/renderers/curses/curses.c +++ b/lib/renderers/curses/curses.c @@ -358,7 +358,7 @@ register_renderer(struct render_api *api) api->get_displayed_count = get_displayed_count; api->poll_key = poll_key; api->render = render; - api->prioritory = BM_PRIO_TERMINAL; + api->priorty = BM_PRIO_TERMINAL; api->version = BM_PLUGIN_VERSION; return "curses"; } diff --git a/lib/renderers/wayland/wayland.c b/lib/renderers/wayland/wayland.c index a3866c6..252d86f 100644 --- a/lib/renderers/wayland/wayland.c +++ b/lib/renderers/wayland/wayland.c @@ -234,7 +234,7 @@ register_renderer(struct render_api *api) api->get_displayed_count = get_displayed_count; api->poll_key = poll_key; api->render = render; - api->prioritory = BM_PRIO_GUI; + api->priorty = BM_PRIO_GUI; api->version = BM_PLUGIN_VERSION; return "wayland"; } diff --git a/lib/renderers/x11/x11.c b/lib/renderers/x11/x11.c index 3e29efe..81ccc5d 100644 --- a/lib/renderers/x11/x11.c +++ b/lib/renderers/x11/x11.c @@ -226,7 +226,7 @@ register_renderer(struct render_api *api) api->set_bottom = set_bottom; api->set_monitor = set_monitor; api->grab_keyboard = grab_keyboard; - api->prioritory = BM_PRIO_GUI; + api->priorty = BM_PRIO_GUI; api->version = BM_PLUGIN_VERSION; return "x11"; } diff --git a/test/bm_menu_new.c b/test/bm_menu_new.c index b1f667e..e35d279 100644 --- a/test/bm_menu_new.c +++ b/test/bm_menu_new.c @@ -21,7 +21,7 @@ main(int argc, char **argv) uint32_t count; const struct bm_renderer **renderers = bm_get_renderers(&count); for (int32_t i = 0; i < count; ++i) { - struct bm_menu *menu = bm_menu_new(bm_renderer_get_name(renderers[i]), BM_PRIO_ANY); + struct bm_menu *menu = bm_menu_new(bm_renderer_get_name(renderers[i])); if (!strcmp(bm_renderer_get_name(renderers[i]), "curses") && !isatty(STDIN_FILENO)) { // do not test continue;