swayfx/sway/commands/mode.c

87 lines
2.2 KiB
C
Raw Normal View History

2018-03-30 13:10:33 +11:00
#define _XOPEN_SOURCE 500
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include "sway/commands.h"
#include "sway/config.h"
#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 }
};
2018-03-30 13:10:33 +11:00
struct cmd_results *cmd_mode(int argc, char **argv) {
struct cmd_results *error = NULL;
if ((error = checkarg(argc, "mode", EXPECTED_AT_LEAST, 1))) {
return error;
}
if (argc > 1 && !config->reading) {
2018-03-30 13:10:33 +11:00
return cmd_results_new(CMD_FAILURE,
"mode", "Can only be used in config file.");
}
2018-07-06 08:12:14 +10:00
bool pango = strcmp(*argv, "--pango_markup") == 0;
if (pango) {
argc--; argv++;
if (argc == 0) {
return cmd_results_new(CMD_FAILURE, "mode",
"Mode name is missing");
}
}
char *mode_name = *argv;
strip_quotes(mode_name);
2018-03-30 13:10:33 +11:00
struct sway_mode *mode = NULL;
// Find mode
for (int i = 0; i < config->modes->length; ++i) {
struct sway_mode *test = config->modes->items[i];
if (strcasecmp(test->name, mode_name) == 0) {
mode = test;
break;
}
}
// Create mode if it doesn't exist
if (!mode && argc > 1) {
2018-03-30 13:10:33 +11:00
mode = calloc(1, sizeof(struct sway_mode));
if (!mode) {
return cmd_results_new(CMD_FAILURE,
"mode", "Unable to allocate mode");
}
mode->name = strdup(mode_name);
mode->keysym_bindings = create_list();
mode->keycode_bindings = create_list();
2018-07-06 08:12:14 +10:00
mode->pango = pango;
2018-03-30 13:10:33 +11:00
list_add(config->modes, mode);
}
if (!mode) {
error = cmd_results_new(CMD_INVALID,
"mode", "Unknown mode `%s'", mode_name);
return error;
}
if ((config->reading && argc > 1) || (!config->reading && argc == 1)) {
2018-07-10 07:54:30 +10:00
wlr_log(WLR_DEBUG, "Switching to mode `%s' (pango=%d)",
2018-07-06 08:12:14 +10:00
mode->name, mode->pango);
2018-03-30 13:10:33 +11:00
}
// Set current mode
config->current_mode = mode;
if (argc == 1) {
2018-03-30 13:10:33 +11:00
// trigger IPC mode event
2018-07-06 08:12:14 +10:00
ipc_event_mode(config->current_mode->name,
config->current_mode->pango);
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
2018-03-30 13:10:33 +11:00
}
// Create binding
struct cmd_results *result = config_subcommand(argv + 1, argc - 1,
mode_handlers, sizeof(mode_handlers));
config->current_mode = config->modes->items[0];
return result;
2018-03-30 13:10:33 +11:00
}