-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmorse_code_open.sh
executable file
·105 lines (94 loc) · 1.94 KB
/
morse_code_open.sh
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
#!/usr/bin/env bash
opened_at=$(date +%s%3N)
pkill -f morse_code_close.sh
# Max duration of a dot and dash, in milliseconds
dot_length=250
dash_length=3000
# Duration to pause before typing a letter or space, in seconds
letter_pause=2
space_pause=2
declare -A morse_letters=(
[.-]=A
[-...]=B
[-.-.]=C
[-..]=D
[.]=E
[..-.]=F
[--.]=G
[....]=H
[..]=I
[.---]=J
[-.-]=K
[.-..]=L
[--]=M
[-.]=N
[---]=O
[.--.]=P
[--.-]=Q
[.-.]=R
[...]=S
[-]=T
[..-]=U
[...-]=V
[.--]=W
[-..-]=X
[-.--]=Y
[--..]=Z
[-----]=0
[.----]=1
[..---]=2
[...--]=3
[....-]=4
[.....]=5
[-....]=6
[--...]=7
[---..]=8
[----.]=9
[.-.-.-]=.
[--..--]=,
[---...]=:
[..--..]=?
[.----.]="'"
[-....-]=-
[-..-.]=/
[-.--.]='('
[-.--.-]=')'
[.-..-.]='"'
[-...-]==
[.-.-.]=+
[.--.-.]=@
)
# Grab environment variables to interact with X
# From https://gist.github.com/AladW/de1c5676d93d05a5a0e1/
pid=$(pgrep -t tty$(fgconsole) xinit)
pid=$(pgrep -P $pid -n)
import_environment() {
(( pid )) && for var; do
IFS='=' read key val < <(egrep -z "$var" /proc/$pid/environ)
printf -v "$key" %s "$val"
[[ ${!key} ]] && export "$key"
done
}
import_environment XAUTHORITY USER DISPLAY
echo "$opened_at" > /tmp/morse_code_open.timestamp
if [ ! -f /tmp/morse_code_close.timestamp ]; then
exit 0
fi
closed_at=$(cat /tmp/morse_code_close.timestamp)
elapsed="$((opened_at - closed_at))"
if [ "$elapsed" -lt "$dot_length" ]; then
printf "%s" "." >> /tmp/morse_code_letter
elif [ "$elapsed" -lt "$dash_length" ]; then
printf "%s" "-" >> /tmp/morse_code_letter
else
exit 0
fi
sleep "$letter_pause"
sequence=$(cat /tmp/morse_code_letter)
if [[ ! -v "morse_letters[$sequence]" ]] ; then
exit 0
fi
xdotool type "${morse_letters[$sequence]}"
rm /tmp/morse_code_letter
sleep "$space_pause"
xdotool type " "