blob: cc8786151649c831e9d3bafb33e16427567a2dc9 (
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
|
#!/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. #
# #
##################################################################
MAP_FILE="$HOME/.cache/kb_layout_per_window"
ICON="${XDG_CONFIG_HOME:-$HOME/.config}/swaync/images/ja.png"
SCRIPT_NAME="$(basename "$0")"
SCRIPT_PATH="$(readlink -f "$0")"
# Detect active Hyprland config mode
config_home="${XDG_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
# Ensure map file exists
touch "$MAP_FILE"
# Function to get layouts from config files
get_layouts() {
local layouts=""
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\s*=\s*"\([^"]*\)".*/\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\s*=\s*"\([^"]*\)".*/\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\s*=\s*"\([^"]*\)".*/\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\s*=\s*"\([^"]*\)".*/\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
echo "$layouts" | tr ',' ' '
}
raw_layouts=$(get_layouts)
if [[ -z "$raw_layouts" ]]; then
echo "Error: cannot find kb_layout in configuration files." >&2
exit 1
fi
kb_layouts=($raw_layouts)
count=${#kb_layouts[@]}
# Get current active window ID
get_win() {
hyprctl activewindow -j | jq -r '.address // .id'
}
# Get available keyboards
get_keyboards() {
hyprctl devices -j | jq -r '.keyboards[].name'
}
# Save window-specific layout
save_map() {
local W=$1 L=$2
grep -v "^${W}:" "$MAP_FILE" > "$MAP_FILE.tmp" 2>/dev/null
echo "${W}:${L}" >> "$MAP_FILE.tmp"
mv "$MAP_FILE.tmp" "$MAP_FILE"
}
# Load layout for window (fallback to default)
load_map() {
local W=$1
local E
E=$(grep "^${W}:" "$MAP_FILE" 2>/dev/null)
[[ -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
done
}
# Toggle layout for current window only
cmd_toggle() {
local W=$(get_win)
[[ -z "$W" || "$W" == "null" ]] && return
local 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]}"
notify-send -u low -i "$ICON" "kb_layout: ${kb_layouts[NEXT]}"
}
# Restore layout on focus
cmd_restore() {
local W=$(get_win)
[[ -z "$W" || "$W" == "null" ]] && return
local 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 SOCKET2="$XDG_RUNTIME_DIR/hypr/$HYPRLAND_INSTANCE_SIGNATURE/.socket2.sock"
if [[ ! -S "$SOCKET2" ]]; then
# Fallback if HYPRLAND_INSTANCE_SIGNATURE is not set correctly in this subshell
local sig=$(hyprctl instances -j | jq -r '.[0].instance' 2>/dev/null)
SOCKET2="$XDG_RUNTIME_DIR/hypr/$sig/.socket2.sock"
fi
[[ -S "$SOCKET2" ]] || {
echo "Error: Hyprland socket not found." >&2
exit 1
}
socat -u UNIX-CONNECT:"$SOCKET2" - | while read -r line; do
if [[ "$line" =~ ^activewindow ]]; then
cmd_restore
fi
done
}
# CLI
case "$1" in
--listener)
subscribe
;;
toggle|"")
# Ensure only one listener
if ! pgrep -f "$SCRIPT_NAME.*--listener" >/dev/null; then
"$SCRIPT_PATH" --listener &
fi
cmd_toggle
;;
*)
echo "Usage: $SCRIPT_NAME [toggle]" >&2; exit 1
;;
esac
|