2014-10-25 17:43:37 +00:00
|
|
|
#include "common.h"
|
2014-03-18 17:35:10 +00:00
|
|
|
#include <stdlib.h>
|
2014-04-09 22:09:35 +00:00
|
|
|
#include <string.h>
|
2014-04-14 17:43:01 +00:00
|
|
|
#include <signal.h>
|
2014-10-25 17:43:37 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2014-04-12 19:12:44 +00:00
|
|
|
#include <getopt.h>
|
2014-10-25 17:43:37 +00:00
|
|
|
#include <assert.h>
|
2014-04-12 19:12:44 +00:00
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
static void
|
|
|
|
disco_trap(int sig)
|
2014-04-14 17:43:01 +00:00
|
|
|
{
|
|
|
|
(void)sig;
|
2014-10-31 19:28:07 +00:00
|
|
|
fprintf(stderr, "\e[?25h");
|
|
|
|
fflush(stderr);
|
2014-04-14 17:43:01 +00:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
static void
|
|
|
|
disco(void)
|
2014-04-14 17:43:01 +00:00
|
|
|
{
|
|
|
|
struct sigaction action;
|
|
|
|
memset(&action, 0, sizeof(struct sigaction));
|
2014-10-22 19:46:51 +00:00
|
|
|
action.sa_handler = disco_trap;
|
2014-04-14 17:43:01 +00:00
|
|
|
sigaction(SIGABRT, &action, NULL);
|
|
|
|
sigaction(SIGSEGV, &action, NULL);
|
|
|
|
sigaction(SIGTRAP, &action, NULL);
|
|
|
|
sigaction(SIGINT, &action, NULL);
|
|
|
|
|
2014-10-24 22:41:57 +00:00
|
|
|
uint32_t cc, c = 80;
|
2014-10-31 19:28:07 +00:00
|
|
|
fprintf(stderr, "\e[?25l");
|
2014-04-14 17:43:01 +00:00
|
|
|
while (1) {
|
2014-10-24 22:41:57 +00:00
|
|
|
for (uint32_t i = 1; i < c - 1; ++i) {
|
2014-10-31 19:28:07 +00:00
|
|
|
fprintf(stderr, "\r %*s%s %s %s ", (i > c / 2 ? c - i : i), " ", ((i % 2) ? "<o/" : "\\o>"), ((i % 4) ? "DISCO" : " "), ((i %2) ? "\\o>" : "<o/"));
|
|
|
|
for (cc = 0; cc < (i < c / 2 ? c / 2 - i : i - c / 2); ++cc) fprintf(stderr, ((i % 2) ? "^" : "'"));
|
|
|
|
fprintf(stderr, "%s %s \r %s %s", ((i % 2) ? "*" : "•"), ((i % 3) ? "\\o" : "<o"), ((i % 3) ? "o/" : "o>"), ((i % 2) ? "*" : "•"));
|
|
|
|
for (cc = 2; cc < (i > c / 2 ? c - i : i); ++cc) fprintf(stderr, ((i % 2) ? "^" : "'"));
|
|
|
|
fflush(stderr);
|
2014-04-14 17:43:01 +00:00
|
|
|
usleep(140 * 1000);
|
|
|
|
}
|
|
|
|
}
|
2014-10-31 19:28:07 +00:00
|
|
|
fprintf(stderr, "\e[?25h");
|
2014-04-14 17:43:01 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
static void
|
|
|
|
version(const char *name)
|
2014-04-12 19:12:44 +00:00
|
|
|
{
|
2014-10-25 17:43:37 +00:00
|
|
|
assert(name);
|
2014-04-12 19:12:44 +00:00
|
|
|
char *base = strrchr(name, '/');
|
2014-10-22 19:46:51 +00:00
|
|
|
printf("%s v%s\n", (base ? base + 1 : name), bm_version());
|
2014-04-14 17:42:10 +00:00
|
|
|
exit(EXIT_SUCCESS);
|
2014-04-12 19:12:44 +00:00
|
|
|
}
|
|
|
|
|
2014-10-22 19:46:51 +00:00
|
|
|
static void
|
|
|
|
usage(FILE *out, const char *name)
|
2014-04-12 19:12:44 +00:00
|
|
|
{
|
2014-10-25 17:43:37 +00:00
|
|
|
assert(out && name);
|
|
|
|
|
2014-04-12 19:12:44 +00:00
|
|
|
char *base = strrchr(name, '/');
|
|
|
|
fprintf(out, "usage: %s [options]\n", (base ? base + 1 : name));
|
|
|
|
fputs("Options\n"
|
|
|
|
" -h, --help display this help and exit.\n"
|
|
|
|
" -v, --version display version.\n"
|
|
|
|
" -i, --ignorecase match items case insensitively.\n"
|
|
|
|
" -w, --wrap wraps cursor selection.\n"
|
|
|
|
" -l, --list list items vertically with the given number of lines.\n"
|
|
|
|
" -p, --prompt defines the prompt text to be displayed.\n"
|
2014-10-26 13:42:09 +00:00
|
|
|
" -P, --prefix text to shown before highlighted item.\n"
|
2015-01-17 22:59:44 +00:00
|
|
|
" -I, --index select item at index automatically.\n"
|
2015-01-18 00:07:30 +00:00
|
|
|
" --scrollbar display scrollbar. (always, autohide)\n\n"
|
2015-01-17 16:53:29 +00:00
|
|
|
|
|
|
|
"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"
|
2014-04-12 19:12:44 +00:00
|
|
|
|
|
|
|
"Backend specific options\n"
|
2015-01-15 23:59:09 +00:00
|
|
|
" c = ncurses, w == wayland, x == x11\n"
|
2014-04-12 19:12:44 +00:00
|
|
|
" (...) At end of help indicates the backend support for option.\n\n"
|
|
|
|
|
2015-01-15 23:59:09 +00:00
|
|
|
" -b, --bottom appears at the bottom of the screen. (x)\n"
|
|
|
|
" -f, --grab grabs the keyboard before reading stdin. (x)\n"
|
|
|
|
" -m, --monitor index of monitor where menu will appear. (x)\n"
|
|
|
|
" --fn defines the font to be used ('name [size]'). (wx)\n"
|
|
|
|
" --bg defines the background color. (wx)\n"
|
|
|
|
" --tb defines the title background color. (wx)\n"
|
|
|
|
" --tf defines the title foreground color. (wx)\n"
|
|
|
|
" --fb defines the filter background color. (wx)\n"
|
|
|
|
" --ff defines the filter foreground color. (wx)\n"
|
|
|
|
" --nb defines the normal background color. (wx)\n"
|
|
|
|
" --nf defines the normal foreground color. (wx)\n"
|
|
|
|
" --hb defines the highlighted background color. (wx)\n"
|
|
|
|
" --hf defines the highlighted foreground color. (wx)\n"
|
|
|
|
" --sb defines the selected background color. (wx)\n"
|
2015-01-17 23:38:25 +00:00
|
|
|
" --sf defines the selected foreground color. (wx)\n"
|
|
|
|
" --scb defines the scrollbar background color. (wx)\n"
|
|
|
|
" --scf defines the scrollbar foreground color. (wx)\n", out);
|
2014-10-25 17:43:37 +00:00
|
|
|
|
2014-04-12 19:12:44 +00:00
|
|
|
exit((out == stderr ? EXIT_FAILURE : EXIT_SUCCESS));
|
|
|
|
}
|
|
|
|
|
2014-10-25 17:43:37 +00:00
|
|
|
void
|
|
|
|
parse_args(struct client *client, int *argc, char **argv[])
|
2014-04-12 19:12:44 +00:00
|
|
|
{
|
2014-10-25 17:43:37 +00:00
|
|
|
assert(client && argc && argv);
|
|
|
|
|
2014-04-12 19:12:44 +00:00
|
|
|
static const struct option opts[] = {
|
|
|
|
{ "help", no_argument, 0, 'h' },
|
|
|
|
{ "version", no_argument, 0, 'v' },
|
|
|
|
|
|
|
|
{ "ignorecase", no_argument, 0, 'i' },
|
|
|
|
{ "wrap", no_argument, 0, 'w' },
|
|
|
|
{ "list", required_argument, 0, 'l' },
|
|
|
|
{ "prompt", required_argument, 0, 'p' },
|
|
|
|
{ "index", required_argument, 0, 'I' },
|
2014-10-26 13:42:09 +00:00
|
|
|
{ "prefix", required_argument, 0, 'P' },
|
2015-01-18 00:07:30 +00:00
|
|
|
{ "scrollbar", required_argument, 0, 0x113 },
|
2014-04-12 19:12:44 +00:00
|
|
|
|
|
|
|
{ "bottom", no_argument, 0, 'b' },
|
|
|
|
{ "grab", no_argument, 0, 'f' },
|
|
|
|
{ "monitor", required_argument, 0, 'm' },
|
2015-01-17 16:53:29 +00:00
|
|
|
{ "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 },
|
2015-01-17 23:38:25 +00:00
|
|
|
{ "scb", required_argument, 0, 0x114 },
|
|
|
|
{ "scf", required_argument, 0, 0x115 },
|
2015-01-17 16:53:29 +00:00
|
|
|
|
|
|
|
{ "disco", no_argument, 0, 0x112 },
|
2014-04-12 19:12:44 +00:00
|
|
|
{ 0, 0, 0, 0 }
|
|
|
|
};
|
|
|
|
|
2014-04-12 19:15:46 +00:00
|
|
|
/* TODO: getopt does not support -sf, -sb etc..
|
|
|
|
* Either break the interface and make them --sf, --sb (like they are now),
|
|
|
|
* or parse them before running getopt.. */
|
|
|
|
|
2014-04-12 19:12:44 +00:00
|
|
|
for (;;) {
|
2014-10-26 13:42:09 +00:00
|
|
|
int32_t opt = getopt_long(*argc, *argv, "hviwl:I:p:P:I:bfm:", opts, NULL);
|
2014-04-12 19:12:44 +00:00
|
|
|
if (opt < 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
switch (opt) {
|
|
|
|
case 'h':
|
|
|
|
usage(stdout, *argv[0]);
|
|
|
|
break;
|
|
|
|
case 'v':
|
2014-04-14 17:42:10 +00:00
|
|
|
version(*argv[0]);
|
2014-04-14 16:39:29 +00:00
|
|
|
break;
|
2014-04-12 19:12:44 +00:00
|
|
|
|
|
|
|
case 'i':
|
2014-10-25 17:43:37 +00:00
|
|
|
client->filter_mode = BM_FILTER_MODE_DMENU_CASE_INSENSITIVE;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2015-01-17 22:59:11 +00:00
|
|
|
client->wrap = true;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
2014-10-25 17:43:37 +00:00
|
|
|
client->lines = strtol(optarg, NULL, 10);
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2014-10-25 17:43:37 +00:00
|
|
|
client->title = optarg;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
2014-10-26 13:42:09 +00:00
|
|
|
case 'P':
|
|
|
|
client->prefix = optarg;
|
|
|
|
break;
|
2014-04-12 19:12:44 +00:00
|
|
|
case 'I':
|
2014-10-25 17:43:37 +00:00
|
|
|
client->selected = strtol(optarg, NULL, 10);
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
2015-01-17 22:59:44 +00:00
|
|
|
case 0x113:
|
2015-01-18 00:07:30 +00:00
|
|
|
client->scrollbar = (!strcmp(optarg, "always") ? BM_SCROLLBAR_ALWAYS : (!strcmp(optarg, "autohide") ? BM_SCROLLBAR_AUTOHIDE : BM_SCROLLBAR_NONE));
|
2015-01-17 22:59:44 +00:00
|
|
|
break;
|
2014-04-12 19:12:44 +00:00
|
|
|
|
|
|
|
case 'b':
|
2015-01-17 22:59:11 +00:00
|
|
|
client->bottom = true;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
case 'f':
|
2015-01-17 22:59:11 +00:00
|
|
|
client->grab = true;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
case 'm':
|
2014-10-25 17:43:37 +00:00
|
|
|
client->monitor = strtol(optarg, NULL, 10);
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
|
|
|
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x100:
|
2015-01-17 13:25:50 +00:00
|
|
|
client->font = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x101:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_BG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x102:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_TITLE_BG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x103:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_TITLE_FG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x104:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_FILTER_BG] = optarg;
|
2014-04-12 19:12:44 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x105:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_FILTER_FG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x106:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_ITEM_BG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x107:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_ITEM_FG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x108:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_HIGHLIGHTED_BG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x109:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_HIGHLIGHTED_FG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x110:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_SELECTED_BG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x111:
|
2014-10-25 17:43:37 +00:00
|
|
|
client->colors[BM_COLOR_SELECTED_FG] = optarg;
|
2014-10-25 17:02:34 +00:00
|
|
|
break;
|
2015-01-17 23:38:25 +00:00
|
|
|
case 0x114:
|
|
|
|
client->colors[BM_COLOR_SCROLLBAR_BG] = optarg;
|
|
|
|
break;
|
|
|
|
case 0x115:
|
|
|
|
client->colors[BM_COLOR_SCROLLBAR_FG] = optarg;
|
|
|
|
break;
|
2014-10-25 17:02:34 +00:00
|
|
|
|
2015-01-17 17:05:39 +00:00
|
|
|
case 0x112:
|
2014-04-14 17:43:01 +00:00
|
|
|
disco();
|
|
|
|
break;
|
|
|
|
|
2014-04-14 16:39:41 +00:00
|
|
|
case ':':
|
|
|
|
case '?':
|
|
|
|
fputs("\n", stderr);
|
|
|
|
usage(stderr, *argv[0]);
|
|
|
|
break;
|
2014-04-12 19:12:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*argc -= optind;
|
|
|
|
*argv += optind;
|
|
|
|
}
|
2014-03-18 17:35:10 +00:00
|
|
|
|
2014-10-25 17:43:37 +00:00
|
|
|
struct bm_menu*
|
|
|
|
menu_with_options(struct client *client)
|
2014-03-18 17:35:10 +00:00
|
|
|
{
|
2014-10-22 19:46:51 +00:00
|
|
|
struct bm_menu *menu;
|
2015-01-17 16:53:29 +00:00
|
|
|
if (!(menu = bm_menu_new(NULL)))
|
2014-10-25 17:43:37 +00:00
|
|
|
return NULL;
|
2014-03-28 19:33:20 +00:00
|
|
|
|
2015-01-17 13:25:50 +00:00
|
|
|
bm_menu_set_font(menu, client->font);
|
2014-10-25 17:43:37 +00:00
|
|
|
bm_menu_set_title(menu, client->title);
|
2014-10-26 13:42:09 +00:00
|
|
|
bm_menu_set_prefix(menu, client->prefix);
|
2014-10-25 17:43:37 +00:00
|
|
|
bm_menu_set_filter_mode(menu, client->filter_mode);
|
|
|
|
bm_menu_set_lines(menu, client->lines);
|
|
|
|
bm_menu_set_wrap(menu, client->wrap);
|
2015-01-15 23:59:09 +00:00
|
|
|
bm_menu_set_bottom(menu, client->bottom);
|
|
|
|
bm_menu_set_monitor(menu, client->monitor);
|
2015-01-17 22:59:44 +00:00
|
|
|
bm_menu_set_scrollbar(menu, client->scrollbar);
|
2014-04-12 19:12:44 +00:00
|
|
|
|
2014-10-25 17:02:34 +00:00
|
|
|
for (uint32_t i = 0; i < BM_COLOR_LAST; ++i)
|
2014-10-25 17:43:37 +00:00
|
|
|
bm_menu_set_color(menu, i, client->colors[i]);
|
2014-10-25 17:02:34 +00:00
|
|
|
|
2015-01-15 23:59:09 +00:00
|
|
|
if (client->grab)
|
|
|
|
bm_menu_grab_keyboard(menu, true);
|
|
|
|
|
2014-10-25 17:43:37 +00:00
|
|
|
return menu;
|
|
|
|
}
|
2014-04-12 19:12:44 +00:00
|
|
|
|
2014-10-25 17:43:37 +00:00
|
|
|
enum bm_run_result
|
|
|
|
run_menu(struct bm_menu *menu)
|
|
|
|
{
|
2015-01-15 23:59:09 +00:00
|
|
|
bm_menu_grab_keyboard(menu, true);
|
|
|
|
|
2014-10-24 22:41:57 +00:00
|
|
|
uint32_t unicode;
|
2014-10-25 17:43:37 +00:00
|
|
|
enum bm_key key;
|
|
|
|
enum bm_run_result status = BM_RUN_RESULT_RUNNING;
|
2014-04-09 22:09:35 +00:00
|
|
|
do {
|
2014-10-22 19:46:51 +00:00
|
|
|
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);
|
2014-10-25 17:43:37 +00:00
|
|
|
return status;
|
2014-03-18 17:35:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* vim: set ts=8 sw=4 tw=0 :*/
|