irongit
hygoland/scripts/SwitchKeyboardLayout.sh
104 lines3.0 KBBourne Again Shell (bash)
1#!/bin/bash
2# /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
3# This is for changing kb_layouts. Set kb_layouts in $settings_file
4
5layout_file="$HOME/.cache/kb_layout"
6settings_file="$HOME/.config/hypr/UserConfigs/UserSettings.conf"
7notif_icon="$HOME/.config/swaync/images/ja.png"
8
9# Refined ignore list with patterns or specific device names
10ignore_patterns=(
11 "--(avrcp)"
12 "Bluetooth Speaker"
13 "Other Device
14 Name"
15 )
16
17
18# Create layout file with default layout if it does not exist
19if [ ! -f "$layout_file" ]; then
20 echo "Creating layout file..."
21 default_layout=$(grep 'kb_layout = ' "$settings_file" | cut -d '=' -f 2 | tr -d '[:space:]' | cut -d ',' -f 1 2>/dev/null)
22 default_layout=${default_layout:-"us"} # Default to 'us' layout
23 echo "$default_layout" > "$layout_file"
24 echo "Default layout set to $default_layout"
25fi
26
27current_layout=$(cat "$layout_file")
28echo "Current layout: $current_layout"
29
30# Read available layouts from settings file
31if [ -f "$settings_file" ]; then
32 kb_layout_line=$(grep 'kb_layout = ' "$settings_file" | cut -d '=' -f 2)
33 # Remove leading and trailing spaces around each layout
34 kb_layout_line=$(echo "$kb_layout_line" | tr -d '[:space:]')
35 IFS=',' read -r -a layout_mapping <<< "$kb_layout_line"
36else
37 echo "Settings file not found!"
38 exit 1
39fi
40
41layout_count=${#layout_mapping[@]}
42echo "Number of layouts: $layout_count"
43
44# Find current layout index and calculate next layout
45for ((i = 0; i < layout_count; i++)); do
46 if [ "$current_layout" == "${layout_mapping[i]}" ]; then
47 current_index=$i
48 break
49 fi
50done
51
52next_index=$(( (current_index + 1) % layout_count ))
53new_layout="${layout_mapping[next_index]}"
54echo "Next layout: $new_layout"
55
56# Function to get keyboard names
57get_keyboard_names() {
58 hyprctl devices -j | jq -r '.keyboards[].name'
59}
60
61# Function to check if a device matches any ignore pattern
62is_ignored() {
63 local device_name=$1
64 for pattern in "${ignore_patterns[@]}"; do
65 if [[ "$device_name" == *"$pattern"* ]]; then
66 return 0 # Device matches ignore pattern
67 fi
68 done
69 return 1 # Device does not match any ignore pattern
70}
71
72# Function to change keyboard layout
73change_layout() {
74 local error_found=false
75
76 while read -r name; do
77 if is_ignored "$name"; then
78 echo "Skipping ignored device: $name"
79 continue
80 fi
81
82 echo "Switching layout for $name to $new_layout..."
83 hyprctl switchxkblayout "$name" "$next_index"
84 if [ $? -ne 0 ]; then
85 echo "Error while switching layout for $name." >&2
86 error_found=true
87 fi
88 done <<< "$(get_keyboard_names)"
89
90 $error_found && return 1
91 return 0
92}
93
94# Execute layout change and notify
95if ! change_layout; then
96 notify-send -u low -t 2000 'kb_layout' " Error:" " Layout change failed"
97 echo "Layout change failed." >&2
98 exit 1
99else
100 notify-send -u low -i "$notif_icon" " kb_layout: $new_layout"
101 echo "Layout change notification sent."
102fi
103
104echo "$new_layout" > "$layout_file"