-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeylaytog
More file actions
executable file
·88 lines (68 loc) · 1.93 KB
/
keylaytog
File metadata and controls
executable file
·88 lines (68 loc) · 1.93 KB
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
#!/bin/bash
#
# keylaytog -- Toggle keyboard layout (us, us_intl)
#
main()
{
layouts=(us us_intl)
ensure_display_access || return 1
# Get current setup
cur_layout=$(setxkbmap -query | \grep layout | awk '{print $2}')
# Cycle through layouts (equal to toggling for two options)
# If current layout is not found, default to first
n=${#layouts[@]}
new_i=0
for i in $(seq 0 $((n - 1)))
do
layout_i="${layouts[${i}]}"
if [ "${cur_layout}" == "${layout_i}" ]
then
# Current layout found: select next layout
# If the current is the last, stick with the
# first, otherwise select the next one
[ ${i} -lt $((n - 1)) ] && new_i=$((i + 1))
break
fi
done
new_layout="${layouts[${new_i}]}"
printf "${cur_layout} -> ${new_layout}\n"
setxkbmap -layout ${new_layout}
}
#
# Make sure there's access to display
#
ensure_display_access()
{
check_display_available && return 0
# Damn, no display access!
# Assumption: Lost connection when reattaching to running
# remote tmux session (without nested SSH connection).
source tmux_update_display
check_display_available && return 0
# Damn, still no display access!
echo "Cannot open display '${DISPLAY}'" >&2
return 1
}
#
# Check whether access to display is available
#
check_display_available()
{
# Set test command (optional arguments, otherwise default)
local cmd="setxkbmap"
[ ${#} -gt 0 ] && cmd="${@}"
# Runn test command, storing sterr
local err=$(setxkbmap 2>&1 >/dev/null)
# Assumption: no error message if display works, otherwise error message
if [ "${err}" == '' ]
then
return 0
elif [ "${err:0:19}" == "Cannot open display" ]
then
return 255
else
echo "check_display_available: unexpected error running ${cmd}: ${err}">&2
return 1
fi
}
main "${@}"