From 1bb99c78da484ce6036dc997962ed2f4c0d11208 Mon Sep 17 00:00:00 2001
From: wochap <gean.marroquin@gmail.com>
Date: Thu, 19 Oct 2023 23:21:49 -0500
Subject: [PATCH 1/2] apply main...Stivvo:toggleKbLayout.patch

---
 config.def.h |  6 ++++++
 dwl.c        | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/config.def.h b/config.def.h
index db0babc..caa09ea 100644
--- a/config.def.h
+++ b/config.def.h
@@ -57,6 +57,11 @@ static const struct xkb_rule_names xkb_rules = {
 static const int repeat_rate = 25;
 static const int repeat_delay = 600;
 
+/* gb will be set the first time togglekblayout is called, then us.. it is
+ * recommended to set the same layout in position 0 of kblayouts and in
+ * xkb_rules */
+static const char *kblayouts[] = {"us", "gb"};
+
 /* Trackpad */
 static const int tap_to_click = 1;
 static const int tap_and_drag = 1;
@@ -141,6 +146,7 @@ static const Key keys[] = {
 	{ MODKEY,                    XKB_KEY_period,     focusmon,       {.i = WLR_DIRECTION_RIGHT} },
 	{ MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_less,       tagmon,         {.i = WLR_DIRECTION_LEFT} },
 	{ MODKEY|WLR_MODIFIER_SHIFT, XKB_KEY_greater,    tagmon,         {.i = WLR_DIRECTION_RIGHT} },
+	{ MODKEY,                    XKB_KEY_w,          togglekblayout, {0} },
 	TAGKEYS(          XKB_KEY_1, XKB_KEY_exclam,                     0),
 	TAGKEYS(          XKB_KEY_2, XKB_KEY_at,                         1),
 	TAGKEYS(          XKB_KEY_3, XKB_KEY_numbersign,                 2),
diff --git a/dwl.c b/dwl.c
index ef27a1d..25458e6 100644
--- a/dwl.c
+++ b/dwl.c
@@ -312,6 +312,7 @@ static void tag(const Arg *arg);
 static void tagmon(const Arg *arg);
 static void tile(Monitor *m);
 static void togglefloating(const Arg *arg);
+static void togglekblayout(const Arg *arg);
 static void togglefullscreen(const Arg *arg);
 static void toggletag(const Arg *arg);
 static void toggleview(const Arg *arg);
@@ -368,6 +369,7 @@ static struct wl_listener lock_listener = {.notify = locksession};
 
 static struct wlr_seat *seat;
 static struct wl_list keyboards;
+static unsigned int kblayout = 0; /* index of kblayouts */
 static unsigned int cursor_mode;
 static Client *grabc;
 static int grabcx, grabcy; /* client-relative */
@@ -2454,6 +2456,24 @@ togglefullscreen(const Arg *arg)
 		setfullscreen(sel, !sel->isfullscreen);
 }
 
+void
+togglekblayout(const Arg *arg)
+{
+	Keyboard *kb;
+	struct xkb_rule_names newrule = xkb_rules;
+
+	kblayout = (kblayout + 1) % LENGTH(kblayouts);
+	newrule.layout = kblayouts[kblayout];
+	wl_list_for_each(kb, &keyboards, link) {
+		struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
+		struct xkb_keymap *keymap = xkb_map_new_from_names(context, &newrule,
+				XKB_KEYMAP_COMPILE_NO_FLAGS);
+		wlr_keyboard_set_keymap(kb->device->keyboard, keymap);
+		xkb_keymap_unref(keymap);
+		xkb_context_unref(context);
+	}
+}
+
 void
 toggletag(const Arg *arg)
 {
-- 
2.42.0


From 3428168a686e2da8ba8a9dc1473350610afaef19 Mon Sep 17 00:00:00 2001
From: wochap <gean.marroquin@gmail.com>
Date: Thu, 19 Oct 2023 23:46:06 -0500
Subject: [PATCH 2/2] fix build

---
 dwl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/dwl.c b/dwl.c
index 25458e6..090280f 100644
--- a/dwl.c
+++ b/dwl.c
@@ -2468,7 +2468,7 @@ togglekblayout(const Arg *arg)
 		struct xkb_context *context = xkb_context_new(XKB_CONTEXT_NO_FLAGS);
 		struct xkb_keymap *keymap = xkb_map_new_from_names(context, &newrule,
 				XKB_KEYMAP_COMPILE_NO_FLAGS);
-		wlr_keyboard_set_keymap(kb->device->keyboard, keymap);
+		wlr_keyboard_set_keymap(kb->wlr_keyboard, keymap);
 		xkb_keymap_unref(keymap);
 		xkb_context_unref(context);
 	}
-- 
2.42.0
