start factoring Key actions into functions

This commit is contained in:
Devin J. Pohly 2020-04-11 19:05:27 -05:00
parent e385acf69d
commit df1aaa4d15
2 changed files with 50 additions and 21 deletions

View File

@ -5,3 +5,10 @@ static const struct xkb_rule_names xkb_rules = {
.variant = NULL, .variant = NULL,
.options = "ctrl:nocaps,altwin:swap_lalt_lwin,terminate:ctrl_alt_bksp", .options = "ctrl:nocaps,altwin:swap_lalt_lwin,terminate:ctrl_alt_bksp",
}; };
#define MODKEY WLR_MODIFIER_ALT
static const Key keys[] = {
{ MODKEY, XKB_KEY_Escape, quit, {0} },
{ MODKEY, XKB_KEY_F1, focusnext, {0} },
};

58
dwl.c
View File

@ -94,6 +94,23 @@ struct dwl_keyboard {
struct wl_listener key; struct wl_listener key;
}; };
typedef union {
int i;
unsigned int ui;
float f;
const void *v;
} Arg;
typedef struct {
uint32_t mod;
xkb_keysym_t keysym;
void (*func)(struct dwl_server *, const Arg *);
const Arg arg;
} Key;
static void focusnext(struct dwl_server *, const Arg *);
static void quit(struct dwl_server *, const Arg *);
#include "config.h" #include "config.h"
static void focus_view(struct dwl_view *view, struct wlr_surface *surface) { static void focus_view(struct dwl_view *view, struct wlr_surface *surface) {
@ -151,22 +168,14 @@ static void keyboard_handle_modifiers(
&keyboard->device->keyboard->modifiers); &keyboard->device->keyboard->modifiers);
} }
static bool handle_keybinding(struct dwl_server *server, xkb_keysym_t sym) { static void quit(struct dwl_server *server, const Arg *unused) {
/*
* 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:
wl_display_terminate(server->wl_display); wl_display_terminate(server->wl_display);
break; }
case XKB_KEY_F1:
static void focusnext(struct dwl_server *server, const Arg *unused) {
/* Cycle to the next view */ /* Cycle to the next view */
if (wl_list_length(&server->views) < 2) { if (wl_list_length(&server->views) < 2) {
break; return;
} }
struct dwl_view *current_view = wl_container_of( struct dwl_view *current_view = wl_container_of(
server->views.next, current_view, link); server->views.next, current_view, link);
@ -176,6 +185,20 @@ static bool handle_keybinding(struct dwl_server *server, xkb_keysym_t sym) {
/* Move the previous view to the end of the list */ /* Move the previous view to the end of the list */
wl_list_remove(&current_view->link); wl_list_remove(&current_view->link);
wl_list_insert(server->views.prev, &current_view->link); wl_list_insert(server->views.prev, &current_view->link);
}
static bool handle_keybinding(struct dwl_server *server, uint32_t mods, xkb_keysym_t sym) {
/*
* 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.
*/
switch (sym) {
case XKB_KEY_Escape:
quit(server, NULL);
break;
case XKB_KEY_F1:
focusnext(server, NULL);
break; break;
default: default:
return false; return false;
@ -200,12 +223,11 @@ static void keyboard_handle_key(
keyboard->device->keyboard->xkb_state, keycode, &syms); keyboard->device->keyboard->xkb_state, keycode, &syms);
bool handled = false; bool handled = false;
uint32_t modifiers = wlr_keyboard_get_modifiers(keyboard->device->keyboard); uint32_t mods = wlr_keyboard_get_modifiers(keyboard->device->keyboard);
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 to /* On _press_, attempt to process a compositor keybinding. */
* process it as a compositor keybinding. */
for (int i = 0; i < nsyms; i++) { for (int i = 0; i < nsyms; i++) {
handled = handle_keybinding(server, syms[i]); handled = handle_keybinding(server, mods, syms[i]) || handled;
} }
} }