mirror of
https://github.com/swaywm/sway.git
synced 2025-11-08 13:29:50 -05:00
Make command block implementation generic
This commit is contained in:
parent
2d480e754e
commit
7c810dc344
12 changed files with 293 additions and 410 deletions
|
|
@ -1,3 +1,4 @@
|
|||
#define _XOPEN_SOURCE 500
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <wlr/util/log.h>
|
||||
|
|
@ -5,53 +6,110 @@
|
|||
#include "sway/config.h"
|
||||
#include "util.h"
|
||||
|
||||
// Must be in alphabetical order for bsearch
|
||||
static struct cmd_handler bar_handlers[] = {
|
||||
{ "activate_button", bar_cmd_activate_button },
|
||||
{ "binding_mode_indicator", bar_cmd_binding_mode_indicator },
|
||||
{ "bindsym", bar_cmd_bindsym },
|
||||
{ "colors", bar_cmd_colors },
|
||||
{ "context_button", bar_cmd_context_button },
|
||||
{ "font", bar_cmd_font },
|
||||
{ "height", bar_cmd_height },
|
||||
{ "hidden_state", bar_cmd_hidden_state },
|
||||
{ "icon_theme", bar_cmd_icon_theme },
|
||||
{ "id", bar_cmd_id },
|
||||
{ "mode", bar_cmd_mode },
|
||||
{ "modifier", bar_cmd_modifier },
|
||||
{ "output", bar_cmd_output },
|
||||
{ "pango_markup", bar_cmd_pango_markup },
|
||||
{ "position", bar_cmd_position },
|
||||
{ "secondary_button", bar_cmd_secondary_button },
|
||||
{ "separator_symbol", bar_cmd_separator_symbol },
|
||||
{ "status_command", bar_cmd_status_command },
|
||||
{ "strip_workspace_numbers", bar_cmd_strip_workspace_numbers },
|
||||
{ "swaybar_command", bar_cmd_swaybar_command },
|
||||
{ "tray_output", bar_cmd_tray_output },
|
||||
{ "tray_padding", bar_cmd_tray_padding },
|
||||
{ "workspace_buttons", bar_cmd_workspace_buttons },
|
||||
{ "wrap_scroll", bar_cmd_wrap_scroll },
|
||||
};
|
||||
|
||||
// Must be in alphabetical order for bsearch
|
||||
static struct cmd_handler bar_config_handlers[] = {
|
||||
{ "hidden_state", bar_cmd_hidden_state },
|
||||
{ "mode", bar_cmd_mode }
|
||||
};
|
||||
|
||||
struct cmd_results *cmd_bar(int argc, char **argv) {
|
||||
struct cmd_results *error = NULL;
|
||||
if ((error = checkarg(argc, "bar", EXPECTED_AT_LEAST, 1))) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (config->reading && strcmp("{", argv[0]) != 0) {
|
||||
return cmd_results_new(CMD_INVALID, "bar",
|
||||
"Expected '{' at start of bar config definition.");
|
||||
}
|
||||
|
||||
if (!config->reading) {
|
||||
if (argc > 1) {
|
||||
if (strcasecmp("mode", argv[0]) == 0) {
|
||||
return bar_cmd_mode(argc-1, argv + 1);
|
||||
}
|
||||
|
||||
if (strcasecmp("hidden_state", argv[0]) == 0) {
|
||||
return bar_cmd_hidden_state(argc-1, argv + 1);
|
||||
}
|
||||
if (!find_handler(argv[0], bar_config_handlers,
|
||||
sizeof(bar_config_handlers))) {
|
||||
return cmd_results_new(CMD_FAILURE, "bar",
|
||||
"Can only be used in config file.");
|
||||
}
|
||||
return cmd_results_new(CMD_FAILURE, "bar", "Can only be used in config file.");
|
||||
return subcommand(argv, argc, bar_config_handlers,
|
||||
sizeof(bar_config_handlers));
|
||||
}
|
||||
|
||||
// Create new bar with default values
|
||||
struct bar_config *bar = default_bar_config();
|
||||
if (!bar) {
|
||||
return cmd_results_new(CMD_FAILURE, "bar", "Unable to allocate bar state");
|
||||
}
|
||||
|
||||
// set bar id
|
||||
for (int i = 0; i < config->bars->length; ++i) {
|
||||
if (bar == config->bars->items[i]) {
|
||||
const int len = 5 + numlen(i); // "bar-" + i + \0
|
||||
bar->id = malloc(len * sizeof(char));
|
||||
if (bar->id) {
|
||||
snprintf(bar->id, len, "bar-%d", i);
|
||||
} else {
|
||||
return cmd_results_new(CMD_FAILURE,
|
||||
"bar", "Unable to allocate bar ID");
|
||||
if (argc > 1) {
|
||||
struct bar_config *bar = NULL;
|
||||
if (!find_handler(argv[0], bar_handlers, sizeof(bar_handlers))
|
||||
&& find_handler(argv[1], bar_handlers, sizeof(bar_handlers))) {
|
||||
for (int i = 0; i < config->bars->length; ++i) {
|
||||
struct bar_config *item = config->bars->items[i];
|
||||
if (strcmp(item->id, argv[0]) == 0) {
|
||||
wlr_log(L_DEBUG, "Selecting bar: %s", argv[0]);
|
||||
bar = item;
|
||||
break;
|
||||
}
|
||||
}
|
||||
break;
|
||||
if (!bar) {
|
||||
wlr_log(L_DEBUG, "Creating bar: %s", argv[0]);
|
||||
bar = default_bar_config();
|
||||
if (!bar) {
|
||||
return cmd_results_new(CMD_FAILURE, "bar",
|
||||
"Unable to allocate bar state");
|
||||
}
|
||||
|
||||
bar->id = strdup(argv[0]);
|
||||
}
|
||||
config->current_bar = bar;
|
||||
++argv; --argc;
|
||||
}
|
||||
}
|
||||
|
||||
// Set current bar
|
||||
config->current_bar = bar;
|
||||
wlr_log(L_DEBUG, "Configuring bar %s", bar->id);
|
||||
return cmd_results_new(CMD_BLOCK_BAR, NULL, NULL);
|
||||
if (!config->current_bar) {
|
||||
// Create new bar with default values
|
||||
struct bar_config *bar = default_bar_config();
|
||||
if (!bar) {
|
||||
return cmd_results_new(CMD_FAILURE, "bar",
|
||||
"Unable to allocate bar state");
|
||||
}
|
||||
|
||||
// set bar id
|
||||
for (int i = 0; i < config->bars->length; ++i) {
|
||||
if (bar == config->bars->items[i]) {
|
||||
const int len = 5 + numlen(i); // "bar-" + i + \0
|
||||
bar->id = malloc(len * sizeof(char));
|
||||
if (bar->id) {
|
||||
snprintf(bar->id, len, "bar-%d", i);
|
||||
} else {
|
||||
return cmd_results_new(CMD_FAILURE,
|
||||
"bar", "Unable to allocate bar ID");
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Set current bar
|
||||
config->current_bar = bar;
|
||||
wlr_log(L_DEBUG, "Creating bar %s", bar->id);
|
||||
}
|
||||
|
||||
return subcommand(argv, argc, bar_handlers, sizeof(bar_handlers));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,21 @@
|
|||
#include <string.h>
|
||||
#include "sway/commands.h"
|
||||
|
||||
// Must be in alphabetical order for bsearch
|
||||
static struct cmd_handler bar_colors_handlers[] = {
|
||||
{ "active_workspace", bar_colors_cmd_active_workspace },
|
||||
{ "background", bar_colors_cmd_background },
|
||||
{ "binding_mode", bar_colors_cmd_binding_mode },
|
||||
{ "focused_background", bar_colors_cmd_focused_background },
|
||||
{ "focused_separator", bar_colors_cmd_focused_separator },
|
||||
{ "focused_statusline", bar_colors_cmd_focused_statusline },
|
||||
{ "focused_workspace", bar_colors_cmd_focused_workspace },
|
||||
{ "inactive_workspace", bar_colors_cmd_inactive_workspace },
|
||||
{ "separator", bar_colors_cmd_separator },
|
||||
{ "statusline", bar_colors_cmd_statusline },
|
||||
{ "urgent_workspace", bar_colors_cmd_urgent_workspace },
|
||||
};
|
||||
|
||||
static struct cmd_results *parse_single_color(char **color,
|
||||
const char *cmd_name, int argc, char **argv) {
|
||||
struct cmd_results *error = NULL;
|
||||
|
|
@ -37,15 +52,8 @@ static struct cmd_results *parse_three_colors(char ***colors,
|
|||
}
|
||||
|
||||
struct cmd_results *bar_cmd_colors(int argc, char **argv) {
|
||||
struct cmd_results *error = NULL;
|
||||
if ((error = checkarg(argc, "colors", EXPECTED_EQUAL_TO, 1))) {
|
||||
return error;
|
||||
}
|
||||
if (strcmp("{", argv[0]) != 0) {
|
||||
return cmd_results_new(CMD_INVALID, "colors",
|
||||
"Expected '{' at the start of colors config definition.");
|
||||
}
|
||||
return cmd_results_new(CMD_BLOCK_BAR_COLORS, NULL, NULL);
|
||||
return subcommand(argv, argc, bar_colors_handlers,
|
||||
sizeof(bar_colors_handlers));
|
||||
}
|
||||
|
||||
struct cmd_results *bar_colors_cmd_active_workspace(int argc, char **argv) {
|
||||
|
|
|
|||
|
|
@ -11,6 +11,9 @@ struct cmd_results *bar_cmd_id(int argc, char **argv) {
|
|||
|
||||
const char *name = argv[0];
|
||||
const char *oldname = config->current_bar->id;
|
||||
if (strcmp(name, oldname) == 0) {
|
||||
return cmd_results_new(CMD_SUCCESS, NULL, NULL); // NOP
|
||||
}
|
||||
// check if id is used by a previously defined bar
|
||||
for (int i = 0; i < config->bars->length; ++i) {
|
||||
struct bar_config *find = config->bars->items[i];
|
||||
|
|
|
|||
|
|
@ -3,85 +3,50 @@
|
|||
#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 input_handlers[] = {
|
||||
{ "accel_profile", input_cmd_accel_profile },
|
||||
{ "click_method", input_cmd_click_method },
|
||||
{ "drag_lock", input_cmd_drag_lock },
|
||||
{ "dwt", input_cmd_dwt },
|
||||
{ "events", input_cmd_events },
|
||||
{ "left_handed", input_cmd_left_handed },
|
||||
{ "map_from_region", input_cmd_map_from_region },
|
||||
{ "map_to_output", input_cmd_map_to_output },
|
||||
{ "middle_emulation", input_cmd_middle_emulation },
|
||||
{ "natural_scroll", input_cmd_natural_scroll },
|
||||
{ "pointer_accel", input_cmd_pointer_accel },
|
||||
{ "repeat_delay", input_cmd_repeat_delay },
|
||||
{ "repeat_rate", input_cmd_repeat_rate },
|
||||
{ "scroll_method", input_cmd_scroll_method },
|
||||
{ "tap", input_cmd_tap },
|
||||
{ "xkb_layout", input_cmd_xkb_layout },
|
||||
{ "xkb_model", input_cmd_xkb_model },
|
||||
{ "xkb_options", input_cmd_xkb_options },
|
||||
{ "xkb_rules", input_cmd_xkb_rules },
|
||||
{ "xkb_variant", input_cmd_xkb_variant },
|
||||
};
|
||||
|
||||
struct cmd_results *cmd_input(int argc, char **argv) {
|
||||
struct cmd_results *error = NULL;
|
||||
if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 2))) {
|
||||
if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 1))) {
|
||||
return error;
|
||||
}
|
||||
|
||||
if (config->reading && strcmp("{", argv[1]) == 0) {
|
||||
free_input_config(config->handler_context.input_config);
|
||||
config->handler_context.input_config = new_input_config(argv[0]);
|
||||
if (!config->handler_context.input_config) {
|
||||
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
|
||||
}
|
||||
wlr_log(L_DEBUG, "entering input block: %s", argv[0]);
|
||||
return cmd_results_new(CMD_BLOCK_INPUT, NULL, NULL);
|
||||
wlr_log(L_DEBUG, "entering input block: %s", argv[0]);
|
||||
|
||||
config->handler_context.input_config = new_input_config(argv[0]);
|
||||
if (!config->handler_context.input_config) {
|
||||
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
|
||||
}
|
||||
|
||||
if ((error = checkarg(argc, "input", EXPECTED_AT_LEAST, 3))) {
|
||||
return error;
|
||||
}
|
||||
struct cmd_results *res = subcommand(argv + 1, argc - 1, input_handlers,
|
||||
sizeof(input_handlers));
|
||||
|
||||
bool has_context = (config->handler_context.input_config != NULL);
|
||||
if (!has_context) {
|
||||
// caller did not give a context so create one just for this command
|
||||
config->handler_context.input_config = new_input_config(argv[0]);
|
||||
if (!config->handler_context.input_config) {
|
||||
return cmd_results_new(CMD_FAILURE, NULL, "Couldn't allocate config");
|
||||
}
|
||||
}
|
||||
|
||||
int argc_new = argc-2;
|
||||
char **argv_new = argv+2;
|
||||
|
||||
struct cmd_results *res;
|
||||
if (strcasecmp("accel_profile", argv[1]) == 0) {
|
||||
res = input_cmd_accel_profile(argc_new, argv_new);
|
||||
} else if (strcasecmp("click_method", argv[1]) == 0) {
|
||||
res = input_cmd_click_method(argc_new, argv_new);
|
||||
} else if (strcasecmp("drag_lock", argv[1]) == 0) {
|
||||
res = input_cmd_drag_lock(argc_new, argv_new);
|
||||
} else if (strcasecmp("dwt", argv[1]) == 0) {
|
||||
res = input_cmd_dwt(argc_new, argv_new);
|
||||
} else if (strcasecmp("events", argv[1]) == 0) {
|
||||
res = input_cmd_events(argc_new, argv_new);
|
||||
} else if (strcasecmp("left_handed", argv[1]) == 0) {
|
||||
res = input_cmd_left_handed(argc_new, argv_new);
|
||||
} else if (strcasecmp("middle_emulation", argv[1]) == 0) {
|
||||
res = input_cmd_middle_emulation(argc_new, argv_new);
|
||||
} else if (strcasecmp("natural_scroll", argv[1]) == 0) {
|
||||
res = input_cmd_natural_scroll(argc_new, argv_new);
|
||||
} else if (strcasecmp("pointer_accel", argv[1]) == 0) {
|
||||
res = input_cmd_pointer_accel(argc_new, argv_new);
|
||||
} else if (strcasecmp("repeat_delay", argv[1]) == 0) {
|
||||
res = input_cmd_repeat_delay(argc_new, argv_new);
|
||||
} else if (strcasecmp("repeat_rate", argv[1]) == 0) {
|
||||
res = input_cmd_repeat_rate(argc_new, argv_new);
|
||||
} else if (strcasecmp("scroll_method", argv[1]) == 0) {
|
||||
res = input_cmd_scroll_method(argc_new, argv_new);
|
||||
} else if (strcasecmp("tap", argv[1]) == 0) {
|
||||
res = input_cmd_tap(argc_new, argv_new);
|
||||
} else if (strcasecmp("xkb_layout", argv[1]) == 0) {
|
||||
res = input_cmd_xkb_layout(argc_new, argv_new);
|
||||
} else if (strcasecmp("xkb_model", argv[1]) == 0) {
|
||||
res = input_cmd_xkb_model(argc_new, argv_new);
|
||||
} else if (strcasecmp("xkb_options", argv[1]) == 0) {
|
||||
res = input_cmd_xkb_options(argc_new, argv_new);
|
||||
} else if (strcasecmp("xkb_rules", argv[1]) == 0) {
|
||||
res = input_cmd_xkb_rules(argc_new, argv_new);
|
||||
} else if (strcasecmp("xkb_variant", argv[1]) == 0) {
|
||||
res = input_cmd_xkb_variant(argc_new, argv_new);
|
||||
} else {
|
||||
res = cmd_results_new(CMD_INVALID, "input <device>", "Unknown command %s", argv[1]);
|
||||
}
|
||||
|
||||
if (!has_context) {
|
||||
// clean up the context we created earlier
|
||||
free_input_config(config->handler_context.input_config);
|
||||
config->handler_context.input_config = NULL;
|
||||
}
|
||||
free_input_config(config->handler_context.input_config);
|
||||
config->handler_context.input_config = NULL;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -7,6 +7,13 @@
|
|||
#include "sway/ipc-server.h"
|
||||
#include "list.h"
|
||||
#include "log.h"
|
||||
#include "stringop.h"
|
||||
|
||||
// Must be in order for the bsearch
|
||||
static struct cmd_handler mode_handlers[] = {
|
||||
{ "bindcode", cmd_bindcode },
|
||||
{ "bindsym", cmd_bindsym }
|
||||
};
|
||||
|
||||
struct cmd_results *cmd_mode(int argc, char **argv) {
|
||||
struct cmd_results *error = NULL;
|
||||
|
|
@ -14,12 +21,12 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
|
|||
return error;
|
||||
}
|
||||
|
||||
const char *mode_name = argv[0];
|
||||
bool new_mode = (argc == 2 && strcmp(argv[1], "{") == 0);
|
||||
if (new_mode && !config->reading) {
|
||||
if (argc > 1 && !config->reading) {
|
||||
return cmd_results_new(CMD_FAILURE,
|
||||
"mode", "Can only be used in config file.");
|
||||
}
|
||||
|
||||
const char *mode_name = argv[0];
|
||||
struct sway_mode *mode = NULL;
|
||||
// Find mode
|
||||
for (int i = 0; i < config->modes->length; ++i) {
|
||||
|
|
@ -30,7 +37,7 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
|
|||
}
|
||||
}
|
||||
// Create mode if it doesn't exist
|
||||
if (!mode && new_mode) {
|
||||
if (!mode && argc > 1) {
|
||||
mode = calloc(1, sizeof(struct sway_mode));
|
||||
if (!mode) {
|
||||
return cmd_results_new(CMD_FAILURE,
|
||||
|
|
@ -46,14 +53,21 @@ struct cmd_results *cmd_mode(int argc, char **argv) {
|
|||
"mode", "Unknown mode `%s'", mode_name);
|
||||
return error;
|
||||
}
|
||||
if ((config->reading && new_mode) || (!config->reading && !new_mode)) {
|
||||
if ((config->reading && argc > 1) || (!config->reading && argc == 1)) {
|
||||
wlr_log(L_DEBUG, "Switching to mode `%s'",mode->name);
|
||||
}
|
||||
// Set current mode
|
||||
config->current_mode = mode;
|
||||
if (!new_mode) {
|
||||
if (argc == 1) {
|
||||
// trigger IPC mode event
|
||||
ipc_event_mode(config->current_mode->name);
|
||||
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||
}
|
||||
return cmd_results_new(new_mode ? CMD_BLOCK_MODE : CMD_SUCCESS, NULL, NULL);
|
||||
|
||||
// Create binding
|
||||
struct cmd_results *result = subcommand(argv + 1, argc - 1, mode_handlers,
|
||||
sizeof(mode_handlers));
|
||||
config->current_mode = config->modes->items[0];
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
struct cmd_results *res = subcommand(argv + 1, argc - 1, seat_handlers,
|
||||
sizeof(seat_handlers));
|
||||
|
||||
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");
|
||||
}
|
||||
}
|
||||
|
||||
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 <name>", "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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue