src/keyboard.c: handle list of keybinds

This commit is contained in:
Johan Malm 2020-06-17 21:21:28 +01:00
parent 40f01ed3c9
commit 1e342f8976
2 changed files with 36 additions and 32 deletions

View file

@ -57,14 +57,15 @@ void keybind_add(struct wl_list *keybinds, const char *keybind,
void keybind_init() void keybind_init()
{ {
keybind_add(&rc.keybinds, "A-Escape", "exit"); keybind_add(&rc.keybinds, "A-Escape", "exit");
keybind_add(&rc.keybinds, "A-F2", "cycle"); keybind_add(&rc.keybinds, "A-Tab", "cycle");
keybind_add(&rc.keybinds, "A-F3", "exec");
} }
void keybind_print() void keybind_print()
{ {
struct keybind *keybind; struct keybind *keybind;
wl_list_for_each_reverse (keybind, &rc.keybinds, link) { wl_list_for_each_reverse (keybind, &rc.keybinds, link) {
printf("KEY=%s\n", keybind->action); printf("KEY=%s-", keybind->action);
for (size_t i = 0; i < keybind->keysyms_len; i++) for (size_t i = 0; i < keybind->keysyms_len; i++)
printf(" %d\n", keybind->keysyms[i]); printf(" %d\n", keybind->keysyms[i]);
} }

View file

@ -1,4 +1,5 @@
#include "labwc.h" #include "labwc.h"
#include "rcxml.h"
static void keyboard_handle_modifiers(struct wl_listener *listener, void *data) static void keyboard_handle_modifiers(struct wl_listener *listener, void *data)
{ {
@ -20,35 +21,40 @@ static void keyboard_handle_modifiers(struct wl_listener *listener, void *data)
keyboard->server->seat, &keyboard->device->keyboard->modifiers); keyboard->server->seat, &keyboard->device->keyboard->modifiers);
} }
static bool handle_keybinding(struct server *server, xkb_keysym_t sym) static void action(struct server *server, struct keybind *keybind)
{ {
/* if (!keybind || !keybind->action)
* Here we handle compositor keybindings. This is when the compositor is return;
* processing keys, rather than passing them on to the client for its if (!strcmp(keybind->action, "exit")) {
* own processing.
*
* This function assumes Alt is held down.
*/
switch (sym) {
case XKB_KEY_Escape:
wl_display_terminate(server->wl_display); wl_display_terminate(server->wl_display);
break; } else if (!strcmp(keybind->action, "cycle")) {
case XKB_KEY_F1:
case XKB_KEY_F2:
server->cycle_view = next_toplevel(view_front_toplevel(server)); server->cycle_view = next_toplevel(view_front_toplevel(server));
break; } else if (!strcmp(keybind->action, "exec")) {
case XKB_KEY_F3: if (!fork())
if (fork() == 0) {
execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL); execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL);
} } else if (!strcmp(keybind->action, "debug-views")) {
break;
case XKB_KEY_F12:
dbg_show_views(server); dbg_show_views(server);
break; } else {
default: fprintf(stderr, "warn: action (%s) not supported\n",
return false; keybind->action);
} }
return true; }
static bool handle_keybinding(struct server *server, uint32_t modifiers,
xkb_keysym_t sym)
{
struct keybind *keybind;
wl_list_for_each_reverse (keybind, &rc.keybinds, link) {
if (modifiers ^ keybind->modifiers)
continue;
for (size_t i = 0; i < keybind->keysyms_len; i++) {
if (sym == keybind->keysyms[i]) {
action(server, keybind);
return true;
}
}
}
return false;
} }
static void keyboard_handle_key(struct wl_listener *listener, void *data) static void keyboard_handle_key(struct wl_listener *listener, void *data)
@ -86,13 +92,10 @@ static void keyboard_handle_key(struct wl_listener *listener, void *data)
} }
/* Handle compositor key bindings */ /* Handle compositor key bindings */
if ((modifiers & WLR_MODIFIER_ALT) && event->state == WLR_KEY_PRESSED) { if (event->state == WLR_KEY_PRESSED)
/* If alt is held down and this button was _pressed_, we attempt for (int i = 0; i < nsyms; i++)
* to process it as a compositor keybinding. */ handled = handle_keybinding(server, modifiers, syms[i]);
for (int i = 0; i < nsyms; i++) {
handled = handle_keybinding(server, syms[i]);
}
}
if (!handled) { if (!handled) {
/* Otherwise, we pass it along to the client. */ /* Otherwise, we pass it along to the client. */
wlr_seat_set_keyboard(seat, keyboard->device); wlr_seat_set_keyboard(seat, keyboard->device);