]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/keyboard.c: handle list of keybinds
authorJohan Malm <jgm323@gmail.com>
Wed, 17 Jun 2020 20:21:28 +0000 (21:21 +0100)
committerJohan Malm <jgm323@gmail.com>
Wed, 17 Jun 2020 20:21:28 +0000 (21:21 +0100)
src/config/keybind.c
src/keyboard.c

index 942a3fa71994a6016486aca7bb1f83740384cd62..3acf27f3ad7071daecfa118004a54fb3bf41a708 100644 (file)
@@ -57,14 +57,15 @@ void keybind_add(struct wl_list *keybinds, const char *keybind,
 void keybind_init()
 {
        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()
 {
        struct keybind *keybind;
        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++)
                        printf("    %d\n", keybind->keysyms[i]);
        }
index 6a08c86efddbb167bf635b3dd8a6903a80499705..fcaf06eab912da040579fdfe34fefc6c383e0509 100644 (file)
@@ -1,4 +1,5 @@
 #include "labwc.h"
+#include "rcxml.h"
 
 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);
 }
 
-static bool handle_keybinding(struct server *server, xkb_keysym_t sym)
+static void action(struct server *server, struct keybind *keybind)
 {
-       /*
-        * Here we handle compositor keybindings. This is when the compositor is
-        * processing keys, rather than passing them on to the client for its
-        * own processing.
-        *
-        * This function assumes Alt is held down.
-        */
-       switch (sym) {
-       case XKB_KEY_Escape:
+       if (!keybind || !keybind->action)
+               return;
+       if (!strcmp(keybind->action, "exit")) {
                wl_display_terminate(server->wl_display);
-               break;
-       case XKB_KEY_F1:
-       case XKB_KEY_F2:
+       } else if (!strcmp(keybind->action, "cycle")) {
                server->cycle_view = next_toplevel(view_front_toplevel(server));
-               break;
-       case XKB_KEY_F3:
-               if (fork() == 0) {
+       } else if (!strcmp(keybind->action, "exec")) {
+               if (!fork())
                        execl("/bin/dmenu_run", "/bin/dmenu_run", (void *)NULL);
-               }
-               break;
-       case XKB_KEY_F12:
+       } else if (!strcmp(keybind->action, "debug-views")) {
                dbg_show_views(server);
-               break;
-       default:
-               return false;
+       } else {
+               fprintf(stderr, "warn: action (%s) not supported\n",
+                       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)
@@ -86,13 +92,10 @@ static void keyboard_handle_key(struct wl_listener *listener, void *data)
        }
 
        /* Handle compositor key bindings */
-       if ((modifiers & WLR_MODIFIER_ALT) && event->state == WLR_KEY_PRESSED) {
-               /* If alt is held down and this button was _pressed_, we attempt
-                * to process it as a compositor keybinding. */
-               for (int i = 0; i < nsyms; i++) {
-                       handled = handle_keybinding(server, syms[i]);
-               }
-       }
+       if (event->state == WLR_KEY_PRESSED)
+               for (int i = 0; i < nsyms; i++)
+                       handled = handle_keybinding(server, modifiers, syms[i]);
+
        if (!handled) {
                /* Otherwise, we pass it along to the client. */
                wlr_seat_set_keyboard(seat, keyboard->device);