Merge pull request #266 from versusvoid/window-switching

Window switching binding
This commit is contained in:
Drew DeVault 2017-10-12 20:00:04 -04:00 committed by GitHub
commit 1df9b0bc13
3 changed files with 49 additions and 48 deletions

View File

@ -11,6 +11,7 @@
#include <wlr/util/log.h> #include <wlr/util/log.h>
#include <wlr/types/wlr_box.h> #include <wlr/types/wlr_box.h>
#include "rootston/config.h" #include "rootston/config.h"
#include "rootston/input.h"
#include "rootston/ini.h" #include "rootston/ini.h"
static void usage(const char *name, int ret) { static void usage(const char *name, int ret) {
@ -109,6 +110,42 @@ static uint32_t parse_modifier(const char *symname) {
} }
} }
void add_binding_config(struct wl_list *bindings, const char* combination,
const char* command) {
struct binding_config *bc = calloc(1, sizeof(struct binding_config));
xkb_keysym_t keysyms[ROOTS_KEYBOARD_PRESSED_KEYSYMS_CAP];
char *symnames = strdup(combination);
char* symname = strtok(symnames, "+");
while (symname) {
uint32_t modifier = parse_modifier(symname);
if (modifier != 0) {
bc->modifiers |= modifier;
} else {
xkb_keysym_t sym = xkb_keysym_from_name(symname,
XKB_KEYSYM_NO_FLAGS);
if (sym == XKB_KEY_NoSymbol) {
wlr_log(L_ERROR, "got unknown key binding symbol: %s",
symname);
free(bc);
bc = NULL;
break;
}
keysyms[bc->keysyms_len] = sym;
bc->keysyms_len++;
}
symname = strtok(NULL, "+");
}
free(symnames);
if (bc) {
wl_list_insert(bindings, &bc->link);
bc->command = strdup(command);
bc->keysyms = malloc(bc->keysyms_len * sizeof(xkb_keysym_t));
memcpy(bc->keysyms, keysyms, bc->keysyms_len * sizeof(xkb_keysym_t));
}
}
static const char *output_prefix = "output:"; static const char *output_prefix = "output:";
static const char *device_prefix = "device:"; static const char *device_prefix = "device:";
@ -216,45 +253,7 @@ static int config_ini_handler(void *user, const char *section, const char *name,
wlr_log(L_ERROR, "got unknown keyboard config: %s", name); wlr_log(L_ERROR, "got unknown keyboard config: %s", name);
} }
} else if (strcmp(section, "bindings") == 0) { } else if (strcmp(section, "bindings") == 0) {
struct binding_config *bc = calloc(1, sizeof(struct binding_config)); add_binding_config(&config->bindings, name, value);
wl_list_insert(&config->bindings, &bc->link);
bc->command = strdup(value);
size_t keysyms_len = 1;
char *symnames = strdup(name);
for (char *c = symnames; *c != '\0'; c++) {
if (*c == '+') {
*c = '\0';
keysyms_len++;
}
}
// TODO: bc->keysyms is larger than needed
bc->keysyms = calloc(1, keysyms_len * sizeof(xkb_keysym_t));
char *symname = symnames;
for (size_t i = 0; i < keysyms_len; i++) {
uint32_t modifier = parse_modifier(symname);
if (modifier != 0) {
bc->modifiers |= modifier;
} else {
xkb_keysym_t sym = xkb_keysym_from_name(symname,
XKB_KEYSYM_NO_FLAGS);
if (sym == XKB_KEY_NoSymbol) {
wlr_log(L_ERROR, "got unknown key binding symbol: %s",
symname);
wl_list_remove(&bc->link);
free(bc->keysyms);
free(bc);
break;
}
bc->keysyms[bc->keysyms_len] = sym;
bc->keysyms_len++;
}
symname += strlen(symname) + 1;
}
free(symnames);
} else { } else {
wlr_log(L_ERROR, "got unknown config section: %s", section); wlr_log(L_ERROR, "got unknown config section: %s", section);
} }
@ -298,15 +297,9 @@ struct roots_config *parse_args(int argc, char *argv[]) {
if (result == -1) { if (result == -1) {
wlr_log(L_DEBUG, "No config file found. Using empty config."); wlr_log(L_DEBUG, "No config file found. Using empty config.");
add_binding_config(&config->bindings, "Logo+Shift+e", "exit");
struct binding_config *bc = calloc(1, sizeof(struct binding_config)); add_binding_config(&config->bindings, "Ctrl+q", "close");
wl_list_insert(&config->bindings, &bc->link); add_binding_config(&config->bindings, "Alt+Tab", "next_window");
bc->command = strdup("exit");
bc->modifiers = WLR_MODIFIER_LOGO;
bc->keysyms_len = 2;
bc->keysyms = calloc(1, bc->keysyms_len * sizeof(xkb_keysym_t));
bc->keysyms[0] = XKB_KEY_Meta_L;
bc->keysyms[1] = XKB_KEY_q;
} else if (result == -2) { } else if (result == -2) {
wlr_log(L_ERROR, "Could not allocate memory to parse config file"); wlr_log(L_ERROR, "Could not allocate memory to parse config file");
exit(1); exit(1);

View File

@ -32,6 +32,13 @@ static void keyboard_binding_execute(struct roots_keyboard *keyboard,
if (keyboard->input->last_active_view != NULL) { if (keyboard->input->last_active_view != NULL) {
view_close(keyboard->input->last_active_view); view_close(keyboard->input->last_active_view);
} }
} else if (strcmp(command, "next_window") == 0) {
if (server->desktop->views->length > 0) {
struct roots_view *view = server->desktop->views->items[0];
set_view_focus(keyboard->input, server->desktop, view);
wlr_seat_keyboard_notify_enter(keyboard->input->wl_seat,
view->wlr_surface);
}
} else if (strncmp(exec_prefix, command, strlen(exec_prefix)) == 0) { } else if (strncmp(exec_prefix, command, strlen(exec_prefix)) == 0) {
const char *shell_cmd = command + strlen(exec_prefix); const char *shell_cmd = command + strlen(exec_prefix);
pid_t pid = fork(); pid_t pid = fork();

View File

@ -38,3 +38,4 @@ meta-key = Logo
[bindings] [bindings]
Logo+Shift+e = exit # Stop the compositor Logo+Shift+e = exit # Stop the compositor
Logo+q = close # Close the current view Logo+q = close # Close the current view
Alt+Tab = next_window # Cycle through windows