Add framework for switching command sets

This will allow the bar {} block to have a different command set (and
also bar { colors { } }.
This commit is contained in:
Drew DeVault 2015-11-29 17:20:27 -05:00
parent 4f89735fc4
commit 44d0f731c3
3 changed files with 30 additions and 7 deletions

View file

@ -41,7 +41,7 @@ struct cmd_results *handle_command(char *command);
* *
* Do not use this under normal conditions. * Do not use this under normal conditions.
*/ */
struct cmd_results *config_command(char *command); struct cmd_results *config_command(char *command, enum cmd_status block);
/** /**
* Allocates a cmd_results object. * Allocates a cmd_results object.

View file

@ -1463,15 +1463,38 @@ static struct cmd_handler handlers[] = {
{ "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth }, { "workspace_auto_back_and_forth", cmd_ws_auto_back_and_forth },
}; };
static struct cmd_handler bar_handlers[] = {
{ "binding_mode_indicator", NULL },
{ "bindsym", NULL },
{ "colors", NULL },
{ "font", NULL },
{ "hidden_state", NULL },
{ "id", NULL },
{ "mode", NULL },
{ "modifier", NULL },
{ "output", NULL },
{ "position", NULL },
{ "seperator_symbol", NULL },
{ "status_command", NULL },
{ "strip_workspace_numbers", NULL },
{ "tray_output", NULL },
{ "tray_padding", NULL },
{ "workspace_buttons", NULL },
};
static int handler_compare(const void *_a, const void *_b) { static int handler_compare(const void *_a, const void *_b) {
const struct cmd_handler *a = _a; const struct cmd_handler *a = _a;
const struct cmd_handler *b = _b; const struct cmd_handler *b = _b;
return strcasecmp(a->command, b->command); return strcasecmp(a->command, b->command);
} }
static struct cmd_handler *find_handler(char *line) { static struct cmd_handler *find_handler(char *line, enum cmd_status block) {
struct cmd_handler *h = handlers;
if (block == CMD_BLOCK_BAR) {
h = bar_handlers;
}
struct cmd_handler d = { .command=line }; struct cmd_handler d = { .command=line };
struct cmd_handler *res = bsearch(&d, handlers, struct cmd_handler *res = bsearch(&d, h,
sizeof(handlers) / sizeof(struct cmd_handler), sizeof(handlers) / sizeof(struct cmd_handler),
sizeof(struct cmd_handler), handler_compare); sizeof(struct cmd_handler), handler_compare);
return res; return res;
@ -1537,7 +1560,7 @@ struct cmd_results *handle_command(char *_exec) {
if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) { if (argc>1 && (*argv[1] == '\"' || *argv[1] == '\'')) {
strip_quotes(argv[1]); strip_quotes(argv[1]);
} }
struct cmd_handler *handler = find_handler(argv[0]); struct cmd_handler *handler = find_handler(argv[0], CMD_BLOCK_END);
if (!handler) { if (!handler) {
if (results) { if (results) {
free_cmd_results(results); free_cmd_results(results);
@ -1574,7 +1597,7 @@ struct cmd_results *handle_command(char *_exec) {
// be chained together) // be chained together)
// 4) handle_command handles all state internally while config_command has some // 4) handle_command handles all state internally while config_command has some
// state handled outside (notably the block mode, in read_config) // state handled outside (notably the block mode, in read_config)
struct cmd_results *config_command(char *exec) { struct cmd_results *config_command(char *exec, enum cmd_status block) {
struct cmd_results *results = NULL; struct cmd_results *results = NULL;
int argc; int argc;
char **argv = split_args(exec, &argc); char **argv = split_args(exec, &argc);
@ -1589,7 +1612,7 @@ struct cmd_results *config_command(char *exec) {
results = cmd_results_new(CMD_BLOCK_END, NULL, NULL); results = cmd_results_new(CMD_BLOCK_END, NULL, NULL);
goto cleanup; goto cleanup;
} }
struct cmd_handler *handler = find_handler(argv[0]); struct cmd_handler *handler = find_handler(argv[0], block);
if (!handler) { if (!handler) {
char *input = argv[0] ? argv[0] : "(empty)"; char *input = argv[0] ? argv[0] : "(empty)";
results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command"); results = cmd_results_new(CMD_INVALID, input, "Unknown/invalid command");

View file

@ -224,7 +224,7 @@ bool read_config(FILE *file, bool is_active) {
if (line[0] == '#') { if (line[0] == '#') {
continue; continue;
} }
struct cmd_results *res = config_command(line); struct cmd_results *res = config_command(line, block);
switch(res->status) { switch(res->status) {
case CMD_FAILURE: case CMD_FAILURE:
case CMD_INVALID: case CMD_INVALID: