From 7c810dc344c28d1876c5ee158cb0806289d0f813 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 30 May 2018 13:20:02 -0400 Subject: Make command block implementation generic --- sway/commands/seat.c | 61 +++++++++++++++------------------------------------- 1 file changed, 17 insertions(+), 44 deletions(-) (limited to 'sway/commands/seat.c') diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 5916015f..6080bf64 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -3,59 +3,32 @@ #include "sway/commands.h" #include "sway/input/input-manager.h" #include "log.h" +#include "stringop.h" + +// must be in order for the bsearch +static struct cmd_handler seat_handlers[] = { + { "attach", seat_cmd_attach }, + { "cursor", seat_cmd_cursor }, + { "fallback", seat_cmd_fallback }, +}; struct cmd_results *cmd_seat(int argc, char **argv) { struct cmd_results *error = NULL; - if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 2))) { + if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 1))) { return error; } - if (config->reading && strcmp("{", argv[1]) == 0) { - free_seat_config(config->handler_context.seat_config); - config->handler_context.seat_config = new_seat_config(argv[0]); - if (!config->handler_context.seat_config) { - return cmd_results_new(CMD_FAILURE, NULL, - "Couldn't allocate config"); - } - wlr_log(L_DEBUG, "entering seat block: %s", argv[0]); - return cmd_results_new(CMD_BLOCK_SEAT, NULL, NULL); + config->handler_context.seat_config = new_seat_config(argv[0]); + if (!config->handler_context.seat_config) { + return cmd_results_new(CMD_FAILURE, NULL, + "Couldn't allocate config"); } - if ((error = checkarg(argc, "seat", EXPECTED_AT_LEAST, 3))) { - return error; - } - - bool has_context = (config->handler_context.seat_config != NULL); - if (!has_context) { - config->handler_context.seat_config = new_seat_config(argv[0]); - if (!config->handler_context.seat_config) { - return cmd_results_new(CMD_FAILURE, NULL, - "Couldn't allocate config"); - } - } + struct cmd_results *res = subcommand(argv + 1, argc - 1, seat_handlers, + sizeof(seat_handlers)); - int argc_new = argc-2; - char **argv_new = argv+2; - - struct cmd_results *res; - if (strcasecmp("attach", argv[1]) == 0) { - res = seat_cmd_attach(argc_new, argv_new); - } else if (strcasecmp("cursor", argv[1]) == 0) { - res = seat_cmd_cursor(argc_new, argv_new); - } else if (strcasecmp("fallback", argv[1]) == 0) { - res = seat_cmd_fallback(argc_new, argv_new); - } else { - res = - cmd_results_new(CMD_INVALID, - "seat ", "Unknown command %s", - argv[1]); - } - - if (!has_context) { - // clean up the context we created earlier - free_seat_config(config->handler_context.seat_config); - config->handler_context.seat_config = NULL; - } + free_seat_config(config->handler_context.seat_config); + config->handler_context.seat_config = NULL; return res; } -- cgit v1.2.3 From 8bfa2def8876bb507aa0174e6b159a0a226889b4 Mon Sep 17 00:00:00 2001 From: Brian Ashworth Date: Wed, 30 May 2018 22:23:11 -0400 Subject: Address first round of review for generic blocks --- sway/commands/seat.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sway/commands/seat.c') diff --git a/sway/commands/seat.c b/sway/commands/seat.c index 6080bf64..5e3c0bd0 100644 --- a/sway/commands/seat.c +++ b/sway/commands/seat.c @@ -24,8 +24,8 @@ struct cmd_results *cmd_seat(int argc, char **argv) { "Couldn't allocate config"); } - struct cmd_results *res = subcommand(argv + 1, argc - 1, seat_handlers, - sizeof(seat_handlers)); + struct cmd_results *res = config_subcommand(argv + 1, argc - 1, + seat_handlers, sizeof(seat_handlers)); free_seat_config(config->handler_context.seat_config); config->handler_context.seat_config = NULL; -- cgit v1.2.3