-
Notifications
You must be signed in to change notification settings - Fork 0
/
hyprland-per-window-xkblayout
executable file
·354 lines (300 loc) · 11.3 KB
/
hyprland-per-window-xkblayout
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env bash
#
# https://github.com/MahouShoujoMivutilde/hyprland-per-window-layout
#
# Hyprland per window xkb layout
#
# How it works:
#
# At script's launch every window (if any) gets its layout defined as currently selected.
#
# When new windows is opened the current layout is used (configurable).
# When existing window is selected - its defined layout is used.
set -e
# shell colors {{{
RESTORE='\033[0m'
RED='\033[00;31m'
GREEN='\033[00;32m'
YELLOW='\033[00;33m'
BLUE='\033[00;34m'
PURPLE='\033[00;35m'
CYAN='\033[00;36m'
LIGHTGRAY='\033[00;37m'
LRED='\033[01;31m'
LGREEN='\033[01;32m'
LYELLOW='\033[01;33m'
LBLUE='\033[01;34m'
LPURPLE='\033[01;35m'
LCYAN='\033[01;36m'
WHITE='\033[01;37m'
colorme() {
while read -r line; do
echo -e "${1}${line}${RESTORE}"
done
}
# }}}
# output/debug utils {{{
print_debug() {
if $DEBUG; then
echo -e "LOG:" "$@"
fi
}
dump_windows_old_hash=''
dump_windows() {
local dump="$(hyprctl clients -j | gojq -r '.[] | "\(.address) \(.workspace.name) \(.title)"' |
while read -r win; do
local addr="${win%% *}"
if [[ -v windows["$addr"] ]]; then
echo " ${win/${addr}/${addr}-${windows["$addr"]}}" | colorme $GREEN
else
echo " ${win} - NO LAYOUT??" | colorme $LGREEN
fi
done)"
current_hash="$(echo "$dump" | md5sum)"
if [[ "$dump_windows_old_hash" != "$current_hash" ]]; then
dump_windows_old_hash="$current_hash"
echo "DUMP:" | colorme $GREEN
echo "$dump"
fi
}
echons() {
echo "$1"
notify-send -a 'hyprland-per-window-xkblayout' -i globe "Hyprland-per-window-xkblayout script" "$1"
}
# }}}
# OPTIONS
prefer_first_layout="${HPWX_PREFER_FIRST:-false}"
DEBUG="${HPWX_DEBUG:-false}"
# ( ⬇ guessed automatically if there is no config file)
keyboard="your-keyboard-name-with-no-spaces-all-lowercase"
declare -A layouts_short
#
# -------------------------------------------------------
#
config="$HOME/.config/hypr/xkb_layout.conf"
if [[ -f "$config" ]]; then
print_debug "loaded config from ${config}\n"
source "$config"
for i in 2s 4s 8s; do
kbd="$(hyprctl devices -j | gojq -r ".keyboards | .[] | select(.name == \"$keyboard\")")"
if [[ "$kbd" != "" ]]; then
break
fi
print_debug "No such device - '$keyboard'" | colorme $RED
print_debug "IPC might be slow to populate right after Hyprland's launch (v0.42+), waiting $i" | colorme $LRED
sleep "$i"
done
if [[ "$kbd" == "" ]]; then
echons "❗No such device - '$keyboard' found after 3 tries" | colorme $RED
exit 1
fi
else
print_debug "no config file, trying to guess options..."
# Find keyboards that have multiple layouts defined,
# pick the first one of them, because if you have
# multiple keyboards with different layouts - you
# obviously know what you're doing and don't need this script.
for i in 2s 4s 8s; do
keyboard="$(hyprctl devices -j |
gojq -r ".keyboards | .[] | select(.layout | contains(\",\")) | .name" |
head -n 1)"
if [[ "$keyboard" != "" ]]; then
break
fi
print_debug "No keyboard with multiple layouts defined found" | colorme $RED
print_debug "IPC might be slow to populate right after Hyprland's launch (v0.42+), waiting $i" | colorme $LRED
sleep "$i"
done
if [[ "$keyboard" == "" ]]; then
echons "❗No keyboard with multiple layouts defined found after 3 tries" | colorme $RED
exit 1
fi
fi
# FIXME: eww this looks horrible {{{
# kb_layout, but as an array that we can index
read -r -a kb_layout_arr < <(
hyprctl devices -j |
gojq -r ".keyboards | .[] | select(.name == \"$keyboard\") | .layout" |
tr ',' ' '
)
layout_long="$(hyprctl devices -j |
gojq -r ".keyboards | .[] | select(.name == \"$keyboard\") | .active_keymap")"
# short layout names to index
declare -A kb_layout
for index in "${!kb_layout_arr[@]}"; do
# inverse
short_name="${kb_layout_arr["$index"]}"
kb_layout["$short_name"]="$index"
if ! [[ -f "$config" ]]; then
# find long names
hyprctl switchxkblayout "$keyboard" "$index"
long_name="$(hyprctl devices -j |
gojq -r ".keyboards | .[] | select(.name == \"$keyboard\") | .active_keymap")"
layouts_short["$long_name"]="$short_name"
print_debug "found layout: '$short_name' ($index) is '$long_name'"
fi
done
layout="${layouts_short["$layout_long"]}"
index="${kb_layout[$layout]}"
hyprctl switchxkblayout "$keyboard" "$index"
# }}}
# addr to layout map
declare -A windows
# predefine current layout for all windows
for addr in $(hyprctl clients -j | gojq -r '.[] | .address'); do
windows[$addr]="$layout"
print_debug "define window $addr layout as '$layout'"
done
# to ignore `activelayout` events generated by script itself
from_script=false
restore_layout() {
# valid window address such as 0x560ef4fe1d00
local current="$1"
# assert that window has a valid address
if ! [[ "$current" =~ 0[xX][0-9a-fA-F]+ ]]; then
print_debug "window $current - invalid address. Ignoring." | colorme $LRED
return
fi
if [[ -v windows["$current"] ]]; then
# loading saved layout
local short="${windows[$current]}"
local index="${kb_layout[$short]}"
print_debug "window $current with defined layout '$short' (current is '$layout')" | colorme $CYAN
if [[ "$layout" == "$short" ]]; then
return
fi
from_script=true
# BUG: under extreme load handling an event could take unexpectedly long time,
# during which you could still manage to switch windows, resulting in 2 or
# more calls to switch layout chained to fire events at the same time.
#
# Manifests as `from_script` being set to false by the first `activelayout` processed,
# and the second (and those after) actually redefining layout for a given window.
#
# Repro:
# 1. Run with HPWX_DEBUG=true, because delay from the write to stdout or at least
# from `dump_windows()` seems important. CAN'T reproduce without HPWX_DEBUG=true at all.
#
# 2. `stress -c $(nproc)`
#
# 3. Swiftly switch between 2 windows with different layouts defined,
# until layout stops matching expected
#
# `activewindow` case opens with `hyprctl activewindow` call, thus stalling changes, it helps, but not by much.
#
# Tested everything with `sleep 1s`. It sucks, but it's more accurate than regular hyprctl under stress test.
# The difference between `hyprctl switchxkblayout` under stress test and idle is just 10ms, so it's probably debug output issue.
hyprctl switchxkblayout "$keyboard" "$index"
layout="$short"
print_debug " ⤷ RESTORED layout '$short' ($index)" | colorme $LCYAN
else
# NOTE: sometimes openwindow is emitted after activewindow / activewindowv2
# e.g. pinentry-qt:
# windowtitle>>55f3a1f70910
# activewindow>>org.gnupg.pinentry-qt,[125350]....
# activewindowv2>>55f3a1f70910
# openwindow>>55f3a1f70910,3,org.gnupg.pinentry-qt
# ....
print_debug "window $current without saved layout. Ignoring. (Possible only when activewindow is emitted before openwindow)" | colorme $LRED
fi
}
handle() {
event="${1%%>>*}"
event_data="${1##*>>}"
if $DEBUG; then
echo "$1"
fi
case "$event" in
activelayout)
local current="$(hyprctl activewindow -j | gojq -r '.address')"
if [[ "$current" == null ]]; then
return
fi
if $from_script; then
from_script=false
return
fi
# This monstrosity is here to handle cases like
# `activelayout>>some-keyboard-(international,-lmao),Some,(Layout,Name)`
# tracking: https://github.com/hyprwm/Hyprland/issues/6298
case "$event_data" in
# only handle events from the keyboard we care about
"$keyboard"*)
local layout_fullname="${event_data##"$keyboard",}"
if ! [[ -v layouts_short["$layout_fullname"] ]]; then
# layout can also be `error` or `none`
# happens after using `wtype`
print_debug "No such layout '$layout_fullname', window $current" | colorme $YELLOW
return
fi
;;
*)
print_debug "Ignoring '$event_data' because it doesn't match our keyboard = '${keyboard}', window $current" | colorme $YELLOW
return
;;
esac
print_debug "Extracted layout as '$layout_fullname', window $current" | colorme $YELLOW
layout="${layouts_short["$layout_fullname"]}"
windows["$current"]="$layout"
print_debug "define layout for window $current as '$layout'" | colorme $LYELLOW
;;
openwindow)
local open_addr="0x${event_data%%,*}"
if $prefer_first_layout; then
windows["$open_addr"]="${kb_layout_arr[0]}"
# NOTE: apparently `openwindow` can be after `activewindow`,
# in which case the above will not change layout:
# `activewindow` sees current layout as right, does nothing,
# `openwindow` redefines it to the first, ensures change when window is refocused, but not immediately
#
# example: libreoffice writer's save window
#
# Restoring manually to fix this:
restore_layout "$open_addr"
else
windows["$open_addr"]="$layout"
fi
print_debug "open window $open_addr, define layout as '$layout'" | colorme $YELLOW
;;
activewindow)
# can be `null` (empty workspace)
local current="$(hyprctl activewindow -j | gojq -r '.address')"
restore_layout "$current"
;;
# # XXX: Slightly more likely to be wrong under extreme cpu load, see the comment in `restore_layout()`
# activewindowv2)
# # can be `0x` (empty workspace)
# local current="0x${event_data}"
# restore_layout "$current"
# ;;
closewindow)
local close_addr="0x${event_data}"
unset windows["$close_addr"]
print_debug "clear window $close_addr from map" | colorme $PURPLE
;;
esac
if $DEBUG; then
# slow, increases the chances of `restore_layout()` bug dramatically
dump_windows
fi
}
print_debug "\nOptions set:\n"
print_debug "DEBUG = $DEBUG, PREFER_FIRST = $prefer_first_layout, keyboard = '$keyboard'"
print_debug "Long layout names to short names:"
print_debug " $(declare -p layouts_short)"
print_debug "Index of a given layout in Hyprland's 'kb_layout':"
print_debug " $(declare -p kb_layout)"
print_debug "\n...waiting for new events...\n"
runtime="${XDG_RUNTIME_DIR:-/run/user/$(id -u)}"
if [[ -d "$runtime/hypr" ]]; then
hl_instance="$runtime/hypr/$HYPRLAND_INSTANCE_SIGNATURE"
else
# pre https://github.com/hyprwm/Hyprland/pull/5788
hl_instance="/tmp/hypr/$HYPRLAND_INSTANCE_SIGNATURE"
fi
socat - \
UNIX-CONNECT:"$hl_instance/.socket2.sock" |
while read -r line; do
handle "$line"
done
# vim:foldmethod=marker