2015-08-06 02:40:38 +00:00
|
|
|
#include <xkbcommon/xkbcommon.h>
|
|
|
|
#include <xkbcommon/xkbcommon-names.h>
|
2015-08-08 23:24:18 +00:00
|
|
|
#include <wlc/wlc.h>
|
2015-08-06 02:10:56 +00:00
|
|
|
#include <stdio.h>
|
2015-08-05 21:30:40 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include "stringop.h"
|
2015-08-08 21:01:22 +00:00
|
|
|
#include "log.h"
|
2015-08-05 21:30:40 +00:00
|
|
|
#include "commands.h"
|
|
|
|
|
|
|
|
int cmd_set(struct sway_config *config, int argc, char **argv) {
|
2015-08-06 02:10:56 +00:00
|
|
|
if (argc != 2) {
|
2015-08-08 21:01:22 +00:00
|
|
|
sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
|
2015-08-06 02:10:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
struct sway_variable *var = malloc(sizeof(struct sway_variable));
|
|
|
|
var->name = malloc(strlen(argv[0]) + 1);
|
|
|
|
strcpy(var->name, argv[0]);
|
|
|
|
var->value = malloc(strlen(argv[1]) + 1);
|
|
|
|
strcpy(var->value, argv[1]);
|
|
|
|
list_add(config->symbols, var);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-08 23:24:18 +00:00
|
|
|
int cmd_exit(struct sway_config *config, int argc, char **argv) {
|
|
|
|
if (argc != 0) {
|
|
|
|
sway_log(L_ERROR, "Invalid exit command (expected 1 arguments, got %d)", argc);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
// TODO: Some kind of clean up is probably in order
|
|
|
|
exit(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct modifier_key {
|
|
|
|
char *name;
|
|
|
|
uint32_t mod;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct modifier_key modifiers[] = {
|
|
|
|
{ XKB_MOD_NAME_SHIFT, WLC_BIT_MOD_SHIFT },
|
|
|
|
{ XKB_MOD_NAME_CAPS, WLC_BIT_MOD_CAPS },
|
|
|
|
{ XKB_MOD_NAME_CTRL, WLC_BIT_MOD_CTRL },
|
|
|
|
{ XKB_MOD_NAME_ALT, WLC_BIT_MOD_ALT },
|
|
|
|
{ XKB_MOD_NAME_NUM, WLC_BIT_MOD_MOD2 },
|
|
|
|
{ "Mod3", WLC_BIT_MOD_MOD3 },
|
|
|
|
{ XKB_MOD_NAME_LOGO, WLC_BIT_MOD_LOGO },
|
|
|
|
{ "Mod5", WLC_BIT_MOD_MOD5 },
|
|
|
|
};
|
|
|
|
|
2015-08-06 02:10:56 +00:00
|
|
|
int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
|
2015-08-06 02:40:38 +00:00
|
|
|
if (argc < 2) {
|
2015-08-08 21:01:22 +00:00
|
|
|
sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
|
2015-08-06 02:10:56 +00:00
|
|
|
return 1;
|
|
|
|
}
|
2015-08-06 02:40:38 +00:00
|
|
|
argv[0] = do_var_replacement(config, argv[0]);
|
|
|
|
|
|
|
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
|
|
|
binding->keys = create_list();
|
|
|
|
binding->modifiers = 0;
|
|
|
|
binding->command = join_args(argv + 1, argc - 1);
|
|
|
|
|
|
|
|
list_t *split = split_string(argv[0], "+");
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < split->length; ++i) {
|
2015-08-08 23:24:18 +00:00
|
|
|
// Check for a modifier key
|
|
|
|
int j;
|
|
|
|
bool is_mod = false;
|
|
|
|
for (j = 0; j < sizeof(modifiers) / sizeof(struct modifier_key); ++j) {
|
|
|
|
if (strcasecmp(modifiers[j].name, split->items[i]) == 0) {
|
|
|
|
binding->modifiers |= modifiers[j].mod;
|
|
|
|
is_mod = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (is_mod) continue;
|
|
|
|
// Check for xkb key
|
2015-08-06 02:40:38 +00:00
|
|
|
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
|
|
|
|
if (!sym) {
|
2015-08-08 21:01:22 +00:00
|
|
|
sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]);
|
2015-08-08 23:24:18 +00:00
|
|
|
return 1;
|
2015-08-06 02:40:38 +00:00
|
|
|
}
|
2015-08-06 02:41:46 +00:00
|
|
|
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
|
|
|
|
*key = sym;
|
|
|
|
list_add(binding->keys, key);
|
2015-08-06 02:40:38 +00:00
|
|
|
}
|
|
|
|
list_free(split);
|
|
|
|
|
2015-08-06 02:55:51 +00:00
|
|
|
// TODO: Check if there are other commands with this key binding
|
2015-08-06 02:40:38 +00:00
|
|
|
list_add(config->current_mode->bindings, binding);
|
|
|
|
|
2015-08-08 21:01:22 +00:00
|
|
|
sway_log(L_DEBUG, "bindsym - Bound %s to command %s", argv[0], binding->command);
|
2015-08-05 21:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Keep alphabetized */
|
|
|
|
struct cmd_handler handlers[] = {
|
2015-08-06 02:40:38 +00:00
|
|
|
{ "bindsym", cmd_bindsym },
|
2015-08-08 23:24:18 +00:00
|
|
|
{ "exit", cmd_exit },
|
|
|
|
{ "set", cmd_set },
|
2015-08-05 21:30:40 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
char **split_directive(char *line, int *argc) {
|
2015-08-06 02:10:56 +00:00
|
|
|
const char *delimiters = " ";
|
2015-08-05 21:30:40 +00:00
|
|
|
*argc = 0;
|
|
|
|
while (isspace(*line) && *line) ++line;
|
|
|
|
|
|
|
|
int capacity = 10;
|
|
|
|
char **parts = malloc(sizeof(char *) * capacity);
|
|
|
|
|
|
|
|
if (!*line) return parts;
|
|
|
|
|
|
|
|
int in_string = 0, in_character = 0;
|
|
|
|
int i, j, _;
|
|
|
|
for (i = 0, j = 0; line[i]; ++i) {
|
|
|
|
if (line[i] == '\\') {
|
|
|
|
++i;
|
|
|
|
} else if (line[i] == '"' && !in_character) {
|
|
|
|
in_string = !in_string;
|
|
|
|
} else if (line[i] == '\'' && !in_string) {
|
|
|
|
in_character = !in_character;
|
|
|
|
} else if (!in_character && !in_string) {
|
|
|
|
if (strchr(delimiters, line[i]) != NULL) {
|
|
|
|
char *item = malloc(i - j + 1);
|
|
|
|
strncpy(item, line + j, i - j);
|
|
|
|
item[i - j] = '\0';
|
|
|
|
item = strip_whitespace(item, &_);
|
|
|
|
if (item[0] == '\0') {
|
|
|
|
free(item);
|
|
|
|
} else {
|
|
|
|
if (*argc == capacity) {
|
|
|
|
capacity *= 2;
|
|
|
|
parts = realloc(parts, sizeof(char *) * capacity);
|
|
|
|
}
|
|
|
|
parts[*argc] = item;
|
|
|
|
j = i + 1;
|
|
|
|
++*argc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
char *item = malloc(i - j + 1);
|
|
|
|
strncpy(item, line + j, i - j);
|
|
|
|
item[i - j] = '\0';
|
|
|
|
item = strip_whitespace(item, &_);
|
|
|
|
if (*argc == capacity) {
|
|
|
|
capacity++;
|
|
|
|
parts = realloc(parts, sizeof(char *) * capacity);
|
|
|
|
}
|
|
|
|
parts[*argc] = item;
|
|
|
|
++*argc;
|
|
|
|
return parts;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handler_compare(const void *_a, const void *_b) {
|
|
|
|
const struct cmd_handler *a = _a;
|
|
|
|
const struct cmd_handler *b = _b;
|
|
|
|
return strcasecmp(a->command, b->command);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *line) {
|
|
|
|
struct cmd_handler d = { .command=line };
|
|
|
|
struct cmd_handler *res = bsearch(&d, handlers, l, sizeof(struct cmd_handler), handler_compare);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
int handle_command(struct sway_config *config, char *exec) {
|
2015-08-08 23:24:18 +00:00
|
|
|
sway_log(L_INFO, "Handling command '%s'", exec);
|
2015-08-05 21:30:40 +00:00
|
|
|
char *ptr, *cmd;
|
|
|
|
if ((ptr = strchr(exec, ' ')) == NULL) {
|
|
|
|
cmd = malloc(strlen(exec) + 1);
|
2015-08-08 23:24:18 +00:00
|
|
|
strcpy(cmd, exec);
|
2015-08-05 21:30:40 +00:00
|
|
|
} else {
|
|
|
|
int index = ptr - exec;
|
|
|
|
cmd = malloc(index + 1);
|
|
|
|
strncpy(cmd, exec, index);
|
|
|
|
cmd[index] = '\0';
|
|
|
|
}
|
|
|
|
struct cmd_handler *handler = find_handler(handlers, sizeof(handlers) / sizeof(struct cmd_handler), cmd);
|
|
|
|
if (handler == NULL) {
|
2015-08-08 21:01:22 +00:00
|
|
|
sway_log(L_ERROR, "Unknown command '%s'", cmd);
|
2015-08-06 02:59:06 +00:00
|
|
|
return 0; // TODO: return error, probably
|
2015-08-05 21:30:40 +00:00
|
|
|
}
|
|
|
|
int argc;
|
|
|
|
char **argv = split_directive(exec + strlen(handler->command), &argc);
|
2015-08-06 02:10:56 +00:00
|
|
|
int ret = handler->handle(config, argc, argv);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < argc; ++i) {
|
|
|
|
free(argv[i]);
|
|
|
|
}
|
|
|
|
free(argv);
|
|
|
|
return ret;
|
2015-08-05 21:30:40 +00:00
|
|
|
}
|