Implement key bindings

This commit is contained in:
Drew DeVault 2015-08-08 19:24:18 -04:00
parent 4181c36862
commit a78b921803
6 changed files with 97 additions and 7 deletions

View file

@ -1,5 +1,6 @@
#include <xkbcommon/xkbcommon.h>
#include <xkbcommon/xkbcommon-names.h>
#include <wlc/wlc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -22,6 +23,32 @@ int cmd_set(struct sway_config *config, int argc, char **argv) {
return 0;
}
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 },
};
int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
if (argc < 2) {
sway_log(L_ERROR, "Invalid set command (expected 2 arguments, got %d)", argc);
@ -37,12 +64,22 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
list_t *split = split_string(argv[0], "+");
int i;
for (i = 0; i < split->length; ++i) {
// TODO: Parse modifier keys
// 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
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i], XKB_KEYSYM_CASE_INSENSITIVE);
if (!sym) {
sway_log(L_ERROR, "bindsym - unknown key %s", (char *)split->items[i]);
// Ignore for now, we need to deal with modifier keys
// return 1;
return 1;
}
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
*key = sym;
@ -60,7 +97,8 @@ int cmd_bindsym(struct sway_config *config, int argc, char **argv) {
/* Keep alphabetized */
struct cmd_handler handlers[] = {
{ "bindsym", cmd_bindsym },
{ "set", cmd_set }
{ "exit", cmd_exit },
{ "set", cmd_set },
};
char **split_directive(char *line, int *argc) {
@ -128,10 +166,11 @@ struct cmd_handler *find_handler(struct cmd_handler handlers[], int l, char *lin
}
int handle_command(struct sway_config *config, char *exec) {
sway_log(L_INFO, "Handling command '%s'", exec);
char *ptr, *cmd;
if ((ptr = strchr(exec, ' ')) == NULL) {
cmd = malloc(strlen(exec) + 1);
strcpy(exec, cmd);
strcpy(cmd, exec);
} else {
int index = ptr - exec;
cmd = malloc(index + 1);