binding config
This commit is contained in:
parent
e1c5adc918
commit
ba69f06695
|
@ -46,7 +46,8 @@ struct sway_mouse_binding {
|
||||||
*/
|
*/
|
||||||
struct sway_mode {
|
struct sway_mode {
|
||||||
char *name;
|
char *name;
|
||||||
list_t *bindings;
|
list_t *keysym_bindings;
|
||||||
|
list_t *keycode_bindings;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -127,6 +127,8 @@ struct cmd_results *add_color(const char *name, char *buffer, const char *color)
|
||||||
|
|
||||||
/* Keep alphabetized */
|
/* Keep alphabetized */
|
||||||
static struct cmd_handler handlers[] = {
|
static struct cmd_handler handlers[] = {
|
||||||
|
{ "bindcode", cmd_bindcode },
|
||||||
|
{ "bindsym", cmd_bindsym },
|
||||||
{ "exec", cmd_exec },
|
{ "exec", cmd_exec },
|
||||||
{ "exec_always", cmd_exec_always },
|
{ "exec_always", cmd_exec_always },
|
||||||
{ "exit", cmd_exit },
|
{ "exit", cmd_exit },
|
||||||
|
|
173
sway/commands/bind.c
Normal file
173
sway/commands/bind.c
Normal file
|
@ -0,0 +1,173 @@
|
||||||
|
#ifdef __linux__
|
||||||
|
#include <linux/input-event-codes.h>
|
||||||
|
#elif __FreeBSD__
|
||||||
|
#include <dev/evdev/input-event-codes.h>
|
||||||
|
#endif
|
||||||
|
#include <xkbcommon/xkbcommon.h>
|
||||||
|
#include <xkbcommon/xkbcommon-names.h>
|
||||||
|
#include <strings.h>
|
||||||
|
#include "sway/commands.h"
|
||||||
|
#include "sway/config.h"
|
||||||
|
#include "list.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include "stringop.h"
|
||||||
|
#include "util.h"
|
||||||
|
|
||||||
|
int binding_order = 0;
|
||||||
|
|
||||||
|
void free_sway_binding(struct sway_binding *binding) {
|
||||||
|
if (binding->keys) {
|
||||||
|
for (int i = 0; i < binding->keys->length; i++) {
|
||||||
|
free(binding->keys->items[i]);
|
||||||
|
}
|
||||||
|
list_free(binding->keys);
|
||||||
|
}
|
||||||
|
if (binding->command) {
|
||||||
|
free(binding->command);
|
||||||
|
}
|
||||||
|
free(binding);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct cmd_results *cmd_bindsym(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "bindsym", EXPECTED_MORE_THAN, 1))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
||||||
|
if (!binding) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
|
binding->keys = create_list();
|
||||||
|
binding->modifiers = 0;
|
||||||
|
binding->release = false;
|
||||||
|
binding->bindcode = false;
|
||||||
|
|
||||||
|
// Handle --release
|
||||||
|
if (strcmp("--release", argv[0]) == 0) {
|
||||||
|
if (argc >= 3) {
|
||||||
|
binding->release = true;
|
||||||
|
argv++;
|
||||||
|
argc--;
|
||||||
|
} else {
|
||||||
|
free_sway_binding(binding);
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Invalid bindsym command "
|
||||||
|
"(expected more than 2 arguments, got %d)", argc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
binding->command = join_args(argv + 1, argc - 1);
|
||||||
|
|
||||||
|
list_t *split = split_string(argv[0], "+");
|
||||||
|
for (int i = 0; i < split->length; ++i) {
|
||||||
|
// Check for a modifier key
|
||||||
|
uint32_t mod;
|
||||||
|
if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) {
|
||||||
|
binding->modifiers |= mod;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// Check for xkb key
|
||||||
|
xkb_keysym_t sym = xkb_keysym_from_name(split->items[i],
|
||||||
|
XKB_KEYSYM_CASE_INSENSITIVE);
|
||||||
|
|
||||||
|
// Check for mouse binding
|
||||||
|
if (strncasecmp(split->items[i], "button", strlen("button")) == 0 &&
|
||||||
|
strlen(split->items[i]) == strlen("button0")) {
|
||||||
|
sym = ((char *)split->items[i])[strlen("button")] - '1' + BTN_LEFT;
|
||||||
|
}
|
||||||
|
if (!sym) {
|
||||||
|
struct cmd_results *ret = cmd_results_new(CMD_INVALID, "bindsym",
|
||||||
|
"Unknown key '%s'", (char *)split->items[i]);
|
||||||
|
free_sway_binding(binding);
|
||||||
|
free_flat_list(split);
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
xkb_keysym_t *key = malloc(sizeof(xkb_keysym_t));
|
||||||
|
if (!key) {
|
||||||
|
free_sway_binding(binding);
|
||||||
|
free_flat_list(split);
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
|
*key = sym;
|
||||||
|
list_add(binding->keys, key);
|
||||||
|
}
|
||||||
|
free_flat_list(split);
|
||||||
|
|
||||||
|
struct sway_mode *mode = config->current_mode;
|
||||||
|
// TODO overwrite the binding if it already exists
|
||||||
|
binding->order = binding_order++;
|
||||||
|
list_add(mode->keysym_bindings, binding);
|
||||||
|
|
||||||
|
sway_log(L_DEBUG, "bindsym - Bound %s to command %s",
|
||||||
|
argv[0], binding->command);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
||||||
|
|
||||||
|
struct cmd_results *cmd_bindcode(int argc, char **argv) {
|
||||||
|
struct cmd_results *error = NULL;
|
||||||
|
if ((error = checkarg(argc, "bindcode", EXPECTED_MORE_THAN, 1))) {
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct sway_binding *binding = malloc(sizeof(struct sway_binding));
|
||||||
|
if (!binding) {
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindsym",
|
||||||
|
"Unable to allocate binding");
|
||||||
|
}
|
||||||
|
binding->keys = create_list();
|
||||||
|
binding->modifiers = 0;
|
||||||
|
binding->release = false;
|
||||||
|
binding->bindcode = true;
|
||||||
|
|
||||||
|
// Handle --release
|
||||||
|
if (strcmp("--release", argv[0]) == 0) {
|
||||||
|
if (argc >= 3) {
|
||||||
|
binding->release = true;
|
||||||
|
argv++;
|
||||||
|
argc--;
|
||||||
|
} else {
|
||||||
|
free_sway_binding(binding);
|
||||||
|
return cmd_results_new(CMD_FAILURE, "bindcode",
|
||||||
|
"Invalid bindcode command "
|
||||||
|
"(expected more than 2 arguments, got %d)", argc);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
binding->command = join_args(argv + 1, argc - 1);
|
||||||
|
|
||||||
|
list_t *split = split_string(argv[0], "+");
|
||||||
|
for (int i = 0; i < split->length; ++i) {
|
||||||
|
// Check for a modifier key
|
||||||
|
uint32_t mod;
|
||||||
|
if ((mod = get_modifier_mask_by_name(split->items[i])) > 0) {
|
||||||
|
binding->modifiers |= mod;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// parse keycode
|
||||||
|
xkb_keycode_t keycode = (int)strtol(split->items[i], NULL, 10);
|
||||||
|
if (!xkb_keycode_is_legal_ext(keycode)) {
|
||||||
|
error =
|
||||||
|
cmd_results_new(CMD_INVALID, "bindcode",
|
||||||
|
"Invalid keycode '%s'", (char *)split->items[i]);
|
||||||
|
free_sway_binding(binding);
|
||||||
|
list_free(split);
|
||||||
|
return error;
|
||||||
|
}
|
||||||
|
xkb_keycode_t *key = malloc(sizeof(xkb_keycode_t));
|
||||||
|
*key = keycode - 8;
|
||||||
|
list_add(binding->keys, key);
|
||||||
|
}
|
||||||
|
free_flat_list(split);
|
||||||
|
|
||||||
|
struct sway_mode *mode = config->current_mode;
|
||||||
|
// TODO overwrite binding if it already exists
|
||||||
|
binding->order = binding_order++;
|
||||||
|
list_add(mode->keycode_bindings, binding);
|
||||||
|
|
||||||
|
sway_log(L_DEBUG, "bindcode - Bound %s to command %s",
|
||||||
|
argv[0], binding->command);
|
||||||
|
return cmd_results_new(CMD_SUCCESS, NULL, NULL);
|
||||||
|
}
|
|
@ -53,7 +53,8 @@ static void config_defaults(struct sway_config *config) {
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
|
if (!(config->current_mode->name = malloc(sizeof("default")))) goto cleanup;
|
||||||
strcpy(config->current_mode->name, "default");
|
strcpy(config->current_mode->name, "default");
|
||||||
if (!(config->current_mode->bindings = create_list())) goto cleanup;
|
if (!(config->current_mode->keysym_bindings = create_list())) goto cleanup;
|
||||||
|
if (!(config->current_mode->keycode_bindings = create_list())) goto cleanup;
|
||||||
list_add(config->modes, config->current_mode);
|
list_add(config->modes, config->current_mode);
|
||||||
|
|
||||||
config->floating_mod = 0;
|
config->floating_mod = 0;
|
||||||
|
|
|
@ -6,6 +6,7 @@ sway_sources = files(
|
||||||
'input/seat.c',
|
'input/seat.c',
|
||||||
'input/cursor.c',
|
'input/cursor.c',
|
||||||
'input/keyboard.c',
|
'input/keyboard.c',
|
||||||
|
'commands/bind.c',
|
||||||
'commands/exit.c',
|
'commands/exit.c',
|
||||||
'commands/exec.c',
|
'commands/exec.c',
|
||||||
'commands/exec_always.c',
|
'commands/exec_always.c',
|
||||||
|
|
Loading…
Reference in a new issue