blob: a058a136b9151a7572bd2b7f2c1d68e27b2c2ddd (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
#!/usr/bin/env bash
# ==================================================
# KoolDots (2026)
# Project URL: https://github.com/LinuxBeginnings
# License: GNU GPLv3
# SPDX-License-Identifier: GPL-3.0-or-later
# ==================================================
##################################################################
# #
# TAK_0'S Per-Window-Switch #
# #
# Just a little script that I made to switch keyboard layouts #
# per-window instead of global switching for the more #
# smooth and comfortable workflow. #
# #
##################################################################
set -uo pipefail
MAP_FILE="${XDG_CACHE_HOME:-$HOME/.cache}/kb_layout_per_window"
notif="${XDG_CONFIG_HOME:-$HOME/.config}/swaync/images"
icons_dir="${XDG_CONFIG_HOME:-$HOME/.config}/swaync/icons"
SCRIPT_NAME="$(basename "$0")"
SCRIPT_PATH="$(readlink -f "$0")"
# Detect active Hyprland config mode
config_home="${XDG_CONFIG_HOME:-$HOME/.config}"
hypr_dir="$config_home/hypr"
lua_entry="$hypr_dir/hyprland.lua"
legacy_lua_entry="$config_home/hyprland.lua"
if [[ -f "$lua_entry" || -f "$legacy_lua_entry" ]]; then
hypr_config_mode="lua"
else
hypr_config_mode="conf"
fi
# Pick notification icon
if [[ -f "$icons_dir/keyboard.png" ]]; then
ICON="$icons_dir/keyboard.png"
elif [[ -f "$notif/ja.png" ]]; then
ICON="$notif/ja.png"
else
ICON="$notif/note.png"
fi
# Ensure cache directory and map file exist
mkdir -p "$(dirname "$MAP_FILE")"
touch "$MAP_FILE"
# Function to get layouts (queries live Hyprland option first, then config files)
get_layouts() {
local layouts=""
# 1. Primary: Live option from active Hyprland session (fast, authoritative in both Lua & conf modes)
layouts=$(hyprctl -j getoption input:kb_layout 2>/dev/null | jq -r '.str // empty' || true)
# 2. Secondary: Query keyboards directly from devices JSON
if [[ -z "$layouts" || "$layouts" == "null" ]]; then
layouts=$(hyprctl -j devices 2>/dev/null | jq -r '[.keyboards[].layout | select(. != null and . != "")] | first // empty' || true)
fi
# 3. Fallback: Parse configuration files
if [[ -z "$layouts" || "$layouts" == "null" ]]; then
if [[ "$hypr_config_mode" == "lua" ]]; then
local lua_user="$hypr_dir/UserConfigs/user_settings.lua"
local lua_sys="$hypr_dir/configs/system_settings.lua"
local lua_legacy_sys="$hypr_dir/UserConfigs/system_settings.lua"
local lua_pristine_sys="$hypr_dir/lua/settings.lua"
if [[ -f "$lua_user" ]] && grep -q 'kb_layout' "$lua_user" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$lua_user" | sed -n "s/.*kb_layout[[:space:]]*=[[:space:]]*[\"']\([^\"']*\)[\"'].*/\1/p" | head -n1)
elif [[ -f "$lua_sys" ]] && grep -q 'kb_layout' "$lua_sys" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$lua_sys" | sed -n "s/.*kb_layout[[:space:]]*=[[:space:]]*[\"']\([^\"']*\)[\"'].*/\1/p" | head -n1)
elif [[ -f "$lua_legacy_sys" ]] && grep -q 'kb_layout' "$lua_legacy_sys" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$lua_legacy_sys" | sed -n "s/.*kb_layout[[:space:]]*=[[:space:]]*[\"']\([^\"']*\)[\"'].*/\1/p" | head -n1)
elif [[ -f "$lua_pristine_sys" ]] && grep -q 'kb_layout' "$lua_pristine_sys" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$lua_pristine_sys" | sed -n "s/.*kb_layout[[:space:]]*=[[:space:]]*[\"']\([^\"']*\)[\"'].*/\1/p" | head -n1)
fi
else
local conf_user="$hypr_dir/UserConfigs/UserSettings.conf"
local conf_sys="$hypr_dir/configs/SystemSettings.conf"
if [[ -f "$conf_user" ]] && grep -q 'kb_layout' "$conf_user" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$conf_user" | cut -d '=' -f2 | tr -d '[:space:]' | head -n1)
elif [[ -f "$conf_sys" ]] && grep -q 'kb_layout' "$conf_sys" 2>/dev/null; then
layouts=$(grep 'kb_layout' "$conf_sys" | cut -d '=' -f2 | tr -d '[:space:]' | head -n1)
fi
fi
fi
echo "${layouts:-us}" | tr ',' ' '
}
raw_layouts=$(get_layouts)
if [[ -z "$raw_layouts" ]]; then
echo "Error: cannot find kb_layout in configuration files or active session." >&2
exit 1
fi
kb_layouts=($raw_layouts)
count=${#kb_layouts[@]}
# Get current active window ID/address
get_win() {
hyprctl activewindow -j 2>/dev/null | jq -r '.address // .id // empty'
}
# Get available keyboard names
get_keyboards() {
hyprctl devices -j 2>/dev/null | jq -r '.keyboards[].name // empty'
}
# Save window-specific layout
save_map() {
local W=$1 L=$2
grep -v "^${W}:" "$MAP_FILE" > "$MAP_FILE.tmp" 2>/dev/null || true
echo "${W}:${L}" >> "$MAP_FILE.tmp"
mv "$MAP_FILE.tmp" "$MAP_FILE"
}
# Load layout for window (fallback to default layout)
load_map() {
local W=$1
local E
E=$(grep "^${W}:" "$MAP_FILE" 2>/dev/null || true)
[[ -n "$E" ]] && echo "${E#*:}" || echo "${kb_layouts[0]}"
}
# Switch layout for all keyboards to layout index
do_switch() {
local IDX=$1
for kb in $(get_keyboards); do
hyprctl switchxkblayout "$kb" "$IDX" >/dev/null 2>&1 || true
done
}
# Toggle layout for current window only
cmd_toggle() {
local W
W=$(get_win)
[[ -z "$W" || "$W" == "null" ]] && return
local CUR
CUR=$(load_map "$W")
local i=0
local NEXT
for idx in "${!kb_layouts[@]}"; do
if [[ "${kb_layouts[idx]}" == "$CUR" ]]; then
i=$idx
break
fi
done
NEXT=$(( (i + 1) % count ))
do_switch "$NEXT"
save_map "$W" "${kb_layouts[NEXT]}"
if command -v notify-send >/dev/null 2>&1 && [[ -n "${WAYLAND_DISPLAY:-}${DISPLAY:-}" ]]; then
notify-send -e \
-h string:x-canonical-private-synchronous:kb_layout_notif \
-h boolean:SWAYNC_BYPASS_DND:true \
-u low \
-i "$ICON" \
" Keyboard Layout (Per-Window)" " ${kb_layouts[NEXT]}" 2>/dev/null || true
fi
}
# Restore layout on focus
cmd_restore() {
local W
W=$(get_win)
[[ -z "$W" || "$W" == "null" ]] && return
local LAY
LAY=$(load_map "$W")
for idx in "${!kb_layouts[@]}"; do
if [[ "${kb_layouts[idx]}" == "$LAY" ]]; then
do_switch "$idx"
break
fi
done
}
# Listen to focus events and restore window-specific layouts
subscribe() {
local runtime_dir="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
local SOCKET2=""
if [[ -n "${HYPRLAND_INSTANCE_SIGNATURE:-}" ]]; then
SOCKET2="$runtime_dir/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
fi
if [[ ! -S "$SOCKET2" ]]; then
local sig
sig=$(hyprctl instances -j 2>/dev/null | jq -r '.[0].instance // empty')
[[ -n "$sig" ]] && SOCKET2="$runtime_dir/hypr/$sig/.socket2.sock"
fi
if [[ ! -S "$SOCKET2" ]]; then
echo "Error: Hyprland socket not found." >&2
exit 1
fi
if ! command -v socat >/dev/null 2>&1; then
echo "Error: socat is required for socket listening." >&2
exit 1
fi
socat -u UNIX-CONNECT:"$SOCKET2" - 2>/dev/null | while read -r line; do
if [[ "$line" =~ ^activewindow ]]; then
cmd_restore
fi
done
}
# CLI
case "${1:-toggle}" in
--listener)
subscribe
;;
status)
W=$(get_win)
load_map "$W"
;;
toggle|"")
# Ensure only one listener is active
if ! pgrep -f "$SCRIPT_NAME.*--listener" >/dev/null 2>&1; then
"$SCRIPT_PATH" --listener &
fi
cmd_toggle
;;
*)
echo "Usage: $SCRIPT_NAME [toggle|status]" >&2
exit 1
;;
esac
|