2018-03-30 04:10:33 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "sway/commands.h"
|
|
|
|
#include "sway/config.h"
|
|
|
|
#include "sway/ipc-server.h"
|
|
|
|
#include "list.h"
|
|
|
|
#include "log.h"
|
2018-05-30 19:20:02 +02:00
|
|
|
#include "stringop.h"
|
|
|
|
|
|
|
|
// Must be in order for the bsearch
|
2021-02-03 01:54:35 +01:00
|
|
|
static const struct cmd_handler mode_handlers[] = {
|
2018-05-30 19:20:02 +02:00
|
|
|
{ "bindcode", cmd_bindcode },
|
2022-04-23 10:27:47 +02:00
|
|
|
{ "bindgesture", cmd_bindgesture },
|
2019-03-20 04:47:29 +01:00
|
|
|
{ "bindswitch", cmd_bindswitch },
|
2019-03-20 04:07:32 +01:00
|
|
|
{ "bindsym", cmd_bindsym },
|
|
|
|
{ "set", cmd_set },
|
2019-06-20 06:33:29 +02:00
|
|
|
{ "unbindcode", cmd_unbindcode },
|
2022-04-23 10:27:47 +02:00
|
|
|
{ "unbindgesture", cmd_unbindgesture },
|
2019-06-20 06:33:29 +02:00
|
|
|
{ "unbindswitch", cmd_unbindswitch },
|
|
|
|
{ "unbindsym", cmd_unbindsym },
|
2018-05-30 19:20:02 +02:00
|
|
|
};
|
2018-03-30 04:10:33 +02: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;
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:12:14 +02:00
|
|
|
bool pango = strcmp(*argv, "--pango_markup") == 0;
|
|
|
|
if (pango) {
|
|
|
|
argc--; argv++;
|
|
|
|
if (argc == 0) {
|
2019-01-11 00:27:21 +01:00
|
|
|
return cmd_results_new(CMD_FAILURE, "Mode name is missing");
|
2018-07-06 00:12:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 06:33:29 +02:00
|
|
|
if (config->reading && argc == 1) {
|
|
|
|
return cmd_results_new(CMD_DEFER, NULL);
|
|
|
|
}
|
|
|
|
|
2018-07-06 00:12:14 +02:00
|
|
|
char *mode_name = *argv;
|
|
|
|
strip_quotes(mode_name);
|
2018-03-30 04:10:33 +02: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];
|
2019-08-04 07:12:41 +02:00
|
|
|
if (strcmp(test->name, mode_name) == 0) {
|
2018-03-30 04:10:33 +02:00
|
|
|
mode = test;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Create mode if it doesn't exist
|
2018-05-30 19:20:02 +02:00
|
|
|
if (!mode && argc > 1) {
|
2018-03-30 04:10:33 +02:00
|
|
|
mode = calloc(1, sizeof(struct sway_mode));
|
|
|
|
if (!mode) {
|
2019-01-11 00:27:21 +01:00
|
|
|
return cmd_results_new(CMD_FAILURE, "Unable to allocate mode");
|
2018-03-30 04:10:33 +02:00
|
|
|
}
|
|
|
|
mode->name = strdup(mode_name);
|
|
|
|
mode->keysym_bindings = create_list();
|
|
|
|
mode->keycode_bindings = create_list();
|
2018-08-01 05:49:42 +02:00
|
|
|
mode->mouse_bindings = create_list();
|
2019-03-20 04:47:29 +01:00
|
|
|
mode->switch_bindings = create_list();
|
2022-04-23 10:27:47 +02:00
|
|
|
mode->gesture_bindings = create_list();
|
2018-07-06 00:12:14 +02:00
|
|
|
mode->pango = pango;
|
2018-03-30 04:10:33 +02:00
|
|
|
list_add(config->modes, mode);
|
|
|
|
}
|
|
|
|
if (!mode) {
|
2019-01-11 00:27:21 +01:00
|
|
|
error = cmd_results_new(CMD_INVALID, "Unknown mode `%s'", mode_name);
|
2018-03-30 04:10:33 +02:00
|
|
|
return error;
|
|
|
|
}
|
|
|
|
// Set current mode
|
2019-08-27 04:58:01 +02:00
|
|
|
struct sway_mode *stored_mode = config->current_mode;
|
2018-03-30 04:10:33 +02:00
|
|
|
config->current_mode = mode;
|
2018-05-30 19:20:02 +02:00
|
|
|
if (argc == 1) {
|
2018-03-30 04:10:33 +02:00
|
|
|
// trigger IPC mode event
|
2019-06-20 06:33:29 +02:00
|
|
|
sway_log(SWAY_DEBUG, "Switching to mode `%s' (pango=%d)",
|
|
|
|
mode->name, mode->pango);
|
2018-07-06 00:12:14 +02:00
|
|
|
ipc_event_mode(config->current_mode->name,
|
|
|
|
config->current_mode->pango);
|
2019-01-11 00:27:21 +01:00
|
|
|
return cmd_results_new(CMD_SUCCESS, NULL);
|
2018-03-30 04:10:33 +02:00
|
|
|
}
|
2018-05-30 19:20:02 +02:00
|
|
|
|
|
|
|
// Create binding
|
2018-05-31 04:23:11 +02:00
|
|
|
struct cmd_results *result = config_subcommand(argv + 1, argc - 1,
|
|
|
|
mode_handlers, sizeof(mode_handlers));
|
2019-08-27 04:58:01 +02:00
|
|
|
config->current_mode = stored_mode;
|
2018-05-30 19:20:02 +02:00
|
|
|
|
|
|
|
return result;
|
2018-03-30 04:10:33 +02:00
|
|
|
}
|