From 77e34dc45a12e85d17498b83c67ae11e82931854 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Leonardo=20Hern=C3=A1ndez=20Hern=C3=A1ndez?=
 <leohdz172@protonmail.com>
Date: Sun, 4 Apr 2021 19:56:09 -0500
Subject: [PATCH] add option to enable numlock/capslock

---
 config.def.h |  4 ++++
 dwl.c        | 27 +++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/config.def.h b/config.def.h
index 6c56eea..daebeb2 100644
--- a/config.def.h
+++ b/config.def.h
@@ -57,6 +57,10 @@ static const struct xkb_rule_names xkb_rules = {
 	.options = NULL,
 };
 
+/* numlock and capslock */
+static const int numlock = 1;
+static const int capslock = 0;
+
 static const int repeat_rate = 25;
 static const int repeat_delay = 600;
 
diff --git a/dwl.c b/dwl.c
index e635b0c..f9b7da7 100644
--- a/dwl.c
+++ b/dwl.c
@@ -14,6 +14,7 @@
 #include <wayland-server-core.h>
 #include <wlr/backend.h>
 #include <wlr/backend/libinput.h>
+#include <wlr/interfaces/wlr_keyboard.h>
 #include <wlr/render/allocator.h>
 #include <wlr/render/wlr_renderer.h>
 #include <wlr/types/wlr_compositor.h>
@@ -851,6 +852,8 @@ createkeyboard(struct wlr_keyboard *keyboard)
 {
 	struct xkb_context *context;
 	struct xkb_keymap *keymap;
+	uint32_t leds = 0;
+	xkb_mod_mask_t locked_mods = 0;
 	Keyboard *kb = keyboard->data = ecalloc(1, sizeof(*kb));
 	kb->wlr_keyboard = keyboard;
 
@@ -874,6 +877,30 @@ createkeyboard(struct wlr_keyboard *keyboard)
 	kb->key_repeat_source = wl_event_loop_add_timer(
 			wl_display_get_event_loop(dpy), keyrepeat, kb);
 
+	if (numlock) {
+		xkb_mod_index_t mod_index = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_NUM);
+		if (mod_index != XKB_MOD_INVALID) {
+			locked_mods |= (uint32_t)1 << mod_index;
+		}
+	}
+
+	if (capslock) {
+		xkb_mod_index_t mod_index = xkb_keymap_mod_get_index(keymap, XKB_MOD_NAME_CAPS);
+		if (mod_index != XKB_MOD_INVALID) {
+			locked_mods |= (uint32_t)1 << mod_index;
+		}
+	}
+
+	if (locked_mods) {
+		wlr_keyboard_notify_modifiers(keyboard, 0, 0, locked_mods, 0);
+
+		for (uint32_t i = 0; i < WLR_LED_COUNT; ++i)
+			if (xkb_state_led_index_is_active(keyboard->xkb_state,
+					keyboard->led_indexes[i]))
+				leds |= (1 << i);
+		wlr_keyboard_led_update(keyboard, leds);
+	}
+
 	/* And add the keyboard to our list of keyboards */
 	wl_list_insert(&keyboards, &kb->link);
 }