|
2 | 2 | # /* ---- 💫 https://github.com/JaKooLit 💫 ---- */ ##
|
3 | 3 | # This is for changing kb_layouts. Set kb_layouts in $settings_file
|
4 | 4 |
|
5 |
| -layout_f="$HOME/.cache/kb_layout" |
| 5 | +layout_file="$HOME/.cache/kb_layout" |
6 | 6 | settings_file="$HOME/.config/hypr/UserConfigs/UserSettings.conf"
|
7 |
| -notif="$HOME/.config/swaync/images/bell.png" |
8 |
| - |
9 |
| -echo "Starting script..." |
10 |
| - |
11 |
| -# Check if ~/.cache/kb_layout exists and create it with a default layout from Settings.conf if not found |
12 |
| -if [ ! -f "$layout_f" ]; then |
13 |
| - echo "Creating layout file as it does not exist..." |
14 |
| - default_layout=$(grep 'kb_layout = ' "$settings_file" | cut -d '=' -f 2 | cut -d ',' -f 1 2>/dev/null) |
15 |
| - if [ -z "$default_layout" ]; then |
16 |
| - default_layout="us" # Default to 'us' layout if Settings.conf or 'kb_layout' is not found |
17 |
| - fi |
18 |
| - echo "$default_layout" > "$layout_f" |
| 7 | +notif_icon="$HOME/.config/swaync/images/bell.png" |
| 8 | + |
| 9 | +# Refined ignore list with patterns or specific device names |
| 10 | +ignore_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 |
| 19 | +if [ ! -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" |
19 | 24 | echo "Default layout set to $default_layout"
|
20 | 25 | fi
|
21 | 26 |
|
22 |
| -current_layout=$(cat "$layout_f") |
| 27 | +current_layout=$(cat "$layout_file") |
23 | 28 | echo "Current layout: $current_layout"
|
24 | 29 |
|
25 |
| -# Read keyboard layout settings from Settings.conf |
| 30 | +# Read available layouts from settings file |
26 | 31 | if [ -f "$settings_file" ]; then
|
27 |
| - echo "Reading keyboard layout settings from $settings_file..." |
28 | 32 | kb_layout_line=$(grep 'kb_layout = ' "$settings_file" | cut -d '=' -f 2)
|
29 |
| - IFS=',' read -ra layout_mapping <<< "$kb_layout_line" |
30 |
| - echo "Available layouts: ${layout_mapping[@]}" |
| 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" |
31 | 36 | else
|
32 | 37 | echo "Settings file not found!"
|
33 | 38 | exit 1
|
|
36 | 41 | layout_count=${#layout_mapping[@]}
|
37 | 42 | echo "Number of layouts: $layout_count"
|
38 | 43 |
|
39 |
| -# Find the index of the current layout in the mapping |
| 44 | +# Find current layout index and calculate next layout |
40 | 45 | for ((i = 0; i < layout_count; i++)); do
|
41 | 46 | if [ "$current_layout" == "${layout_mapping[i]}" ]; then
|
42 | 47 | current_index=$i
|
43 |
| - echo "Current layout index: $current_index" |
44 | 48 | break
|
45 | 49 | fi
|
46 | 50 | done
|
47 | 51 |
|
48 |
| -# Calculate the index of the next layout |
49 | 52 | next_index=$(( (current_index + 1) % layout_count ))
|
50 | 53 | new_layout="${layout_mapping[next_index]}"
|
51 | 54 | echo "Next layout: $new_layout"
|
52 | 55 |
|
53 |
| -# Created by T-Crypt |
54 |
| - |
| 56 | +# Function to get keyboard names |
55 | 57 | get_keyboard_names() {
|
56 | 58 | hyprctl devices -j | jq -r '.keyboards[].name'
|
57 | 59 | }
|
58 | 60 |
|
| 61 | +# Function to check if a device matches any ignore pattern |
| 62 | +is_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 |
59 | 73 | change_layout() {
|
60 |
| - local got_error=false |
| 74 | + local error_found=false |
61 | 75 |
|
62 | 76 | while read -r name; do
|
| 77 | + if is_ignored "$name"; then |
| 78 | + echo "Skipping ignored device: $name" |
| 79 | + continue |
| 80 | + fi |
| 81 | + |
63 | 82 | echo "Switching layout for $name to $new_layout..."
|
64 |
| - hyprctl switchxkblayout "$name" "$new_layout" |
65 |
| - if [[ $? -eq 0 ]]; then |
66 |
| - echo "Switched the layout for $name." |
67 |
| - else |
68 |
| - >&2 echo "Error while switching the layout for $name." |
69 |
| - got_error=true |
| 83 | + hyprctl switchxkblayout "$name" next |
| 84 | + if [ $? -ne 0 ]; then |
| 85 | + echo "Error while switching layout for $name." >&2 |
| 86 | + error_found=true |
70 | 87 | fi
|
71 | 88 | done <<< "$(get_keyboard_names)"
|
72 | 89 |
|
73 |
| - if [ "$got_error" = true ]; then |
74 |
| - >&2 echo "Some errors were found during the process..." |
75 |
| - return 1 |
76 |
| - fi |
77 |
| - |
78 |
| - return 0 # All layouts had been cycled successfully |
| 90 | + $error_found && return 1 |
| 91 | + return 0 |
79 | 92 | }
|
80 | 93 |
|
| 94 | +# Execute layout change and notify |
81 | 95 | if ! change_layout; then
|
82 |
| - notify-send -u low -t 2000 'Keyboard layout' 'Error: Layout change failed' |
83 |
| - >&2 echo "Layout change failed." |
| 96 | + notify-send -u low -t 2000 'kb_layout' 'Error: Layout change failed' |
| 97 | + echo "Layout change failed." >&2 |
84 | 98 | exit 1
|
85 | 99 | else
|
86 |
| - # Notification for the new keyboard layout |
87 |
| - notify-send -u low -i "$notif" "new KB_Layout: $new_layout" |
| 100 | + notify-send -u low -i "$notif_icon" "New kb_layout: $new_layout" |
88 | 101 | echo "Layout change notification sent."
|
89 | 102 | fi
|
90 | 103 |
|
91 |
| -echo "$new_layout" > "$layout_f" |
| 104 | +echo "$new_layout" > "$layout_file" |
0 commit comments