forked from richardgv/skippy-xd
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathskippy-xd-runner
executable file
·162 lines (127 loc) · 4.44 KB
/
skippy-xd-runner
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
#!/bin/sh
#
# skippy-xd-runner
#
# A wrapper script to deal with too many requests / locks up skippy
# When skippy is bound to a global keybinding, and held keys repeat
#
# Functions:
# * Restart skippy daemon if stuck / unresponsive (> 1 sec)
# * Don't overload skippy with any simultaneous requests
# * Auto-start daemon for --activate and --toggle commands
#
# Usage:
#
# skippy-xd-runner <args>
#
# The available commands are:
# [no command] - activate expose once without daemon.
# --help - show this message.
# -S - enable debugging logs.
# --config - read configuration file from path.
# --config-reload - reload configuration file from the previous path.
# --start-daemon - runs as daemon mode.
# --stop-daemon - terminates skippy-xd daemon.
# --switch - connects to daemon and switch to next window.
# --switch-prev - connects to daemon and switch to previous window.
# --expose - connects to daemon and activate expose.
# --paging - connects to daemon and activate paging.
#
#
#
_kill_skippy() {
killall 'skippy-xd'
if [ -f '/tmp/skippy-xd-fifo' ]; then
rm /tmp/skippy-xd-fifo
touch /tmp/skippy-xd-fifo
fi
}
_parse_args()
{
while [ "$1" ]; do
arg="$1"
case $arg in
--help|-h) _help=true ;;
-S) _sync=true ;;
--config) shift && _config="$1" ;;
--config-reload) _config_reload=true ;;
--start-daemon|--start) _start_daemon=true ;;
--stop-daemon|--stop) _stop_daemon=true ;;
--switch) _activate=true; _next=true ;;
--switch-prev) _activate=true; _prev=true ;;
--expose) _activate=true; _expose=true ;;
--paging) _activate=true; _paging=true ;;
--toggle) _toggle=true ;;
--deactivate) _deactivate=true ;;
esac
shift
done
unset _first_arg
if [ "$_next" ]; then
_first_arg="--switch"
fi
if [ "$_prev" ]; then
_first_arg="--switch-prev"
fi
if [ "$_expose" ]; then
_first_arg="--expose"
fi
if [ "$_paging" ]; then
_first_arg="--paging"
fi
if [ "$_first_arg" ]; then
unset _start_daemon
fi
}
_main()
{
# if [ "$(command -v xdotool)" ]; then
# if xdotool search -class --onlyvisible skippy-xd > /dev/null; then
# echo "ignored. skippy is already open / active"
# exit 1
# fi
# fi
lastActivationTimeoutSeconds=1
psSkippyAltTabOut="`pgrep -f 'skippy-xd --switch'`"
psSkippyAltTabOut="$psSkippyAltTabOut `pgrep -f 'skippy-xd --switch-prev'`"
psSkippyExposeOut="`pgrep -f 'skippy-xd --expose'`"
psSkippyPagingOut="`pgrep -f 'skippy-xd --paging'`"
_other_clients=0
for pid in $psSkippyAltTabOut $psSkippyExposeOut $psSkippyPagingOut; do
ptime="$(ps -o etimes= -p "$pid")"
if [ "$ptime" -ge "$lastActivationTimeoutSeconds" ]; then
_killall=true
break
fi
_other_clients="$(expr $i + 1)"
done
if [ "$_other_clients" -gt "1" ]; then
_killall=true
fi
# if a process to activate skippy already exists, with a runtime that is too long
# (> 1sec). Then assume skippy-xd is stuck, so we must kill its all of its locked processess
if [ "$_killall" ]; then
_kill_skippy;
# if the skippy-xd daemon is busy, and servicing only 1 other recent request (< 1 sec old)
# then we assume the alt-tab key is being held down. so we must skip / omit our extra
# requests such as this one, since too many requests will lock up the skippy daemon
elif [ "$_first_arg" ] && [ "$_other_clients" -eq "1" ]; then
echo "refused. skippy is still busy servicing the previous client request"
exit 1
fi
# if the action requires the skippy-xd daemon, but it is not already running, we should start it
if [ "$_first_arg" ] && [ ! "$_deactivate" ] && [ ! "$(pgrep -f 'skippy-xd ')" ]; then
if [ "$_config" ]; then
skippy-xd --start-daemon --config "$_config" &
else
skippy-xd --start-daemon &
fi
fi
# pass all arguments onto skippy unmodified. but repeat $first_arg up front, to show up in pgrep -f output
skippy-xd $_first_arg "$@"
exit $?
}
(
_parse_args "$@";
_main "$@"
)