home · contact · privacy
Add keyboard layout toggling.
authorPlom Heller <plom@plomlompom.com>
Sat, 19 Sep 2026 09:43:54 +0000 (11:43 +0200)
committerPlom Heller <plom@plomlompom.com>
Sat, 19 Sep 2026 09:43:54 +0000 (11:43 +0200)
home/user/.config/sway/config
home/user/.config/sway/status.py
home/user/.profile.d/kb [new file with mode: 0644]

index e6471d545eed58fad19314797aa9dec3df8721e9..2f297b3d34ee58d592ecfa3876357a8671004183 100644 (file)
@@ -11,8 +11,8 @@ workspace_layout tabbed
 # simple green background
 output * background #559911 solid_color
 
-# keyboard layout
-input * xkb_layout "us"
+# keyboard layout – to be switched with ~/.profile.d/kb
+input * xkb_layout "us,de"
 
 # deactivate t490s touchpad
 input 1267:47:Elan_Touchpad events disabled
index acfb97aa75cced72e75789b6d7e1be6c3d7a18fe..2d2949a3ec60c42678b56b99f037a00830df35e7 100755 (executable)
@@ -1,7 +1,7 @@
 #!/usr/bin/env python3
 'Sway bar status line generator.'
 from datetime import datetime
-from json import dumps as json_dumps
+from json import dumps as json_dumps, loads as json_loads
 from pathlib import Path
 from re import compile as re_compile
 from subprocess import run as subprocess_run
@@ -17,6 +17,8 @@ CMD_PASTE = 'wl-paste'
 CLIP_LEN_MAX = 32
 MSG_CLIPBOARD_EMPTY = 'Nothing is copied'
 
+KB_LAYOUTS = ('us', 'de')  # keep in sync with sway config's xkb_layout
+
 AUDIO_DEVS_LABELS = (('sink', 'vol'),
                      ('source', '--mic'))
 
@@ -102,6 +104,18 @@ def info_datetime(
     return datetime.now().astimezone().strftime('%Y-%m-%d %H:%M:%S %z/%Z')
 
 
+def info_layout(
+        ) -> str:
+    'Produce two-character name of active keyboard layout.'
+    inputs = json_loads(subprocess_run(('swaymsg', '-t', 'get_inputs', '-r'),
+                                       capture_output=True,
+                                       check=True).stdout)
+    indices = {dev['xkb_active_layout_index']
+               for dev in inputs if 'xkb_active_layout_index' in dev}
+    assert len(indices) == 1
+    return KB_LAYOUTS[indices.pop()]
+
+
 def info_network(
         ) -> tuple[str, ...]:
     'Produce one status block full_text per connected ethernet/wifi interface.'
@@ -173,6 +187,7 @@ if __name__ == '__main__':
                               info_battery(),
                               info_thermal(),
                               info_datetime(),
+                              info_layout(),
                               info_audio())]),
               end=',\n', flush=True)
         sleep(1)
diff --git a/home/user/.profile.d/kb b/home/user/.profile.d/kb
new file mode 100644 (file)
index 0000000..af82ed0
--- /dev/null
@@ -0,0 +1 @@
+alias kb="swaymsg 'input * xkb_switch_layout next'"