-
Notifications
You must be signed in to change notification settings - Fork 0
/
safe-eyes.sh
executable file
·135 lines (115 loc) · 3.83 KB
/
safe-eyes.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
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
#!/bin/bash
#############################################################################################
# "Strict" break reminder based on YAD. Inspired by https://github.com/slgobinath/SafeEyes.
# Motivation: Be simpler (fewer dependencies) than original SafeEyes and pause during calls.
# Usage: ./safe-eyes.sh start
#############################################################################################
MY_PATH="$(dirname $0)"
if [ ! -f $MY_PATH/config-current.sh ]; then
# copy default config if not exists
cp $MY_PATH/config-default.sh $MY_PATH/config-current.sh
fi
source $MY_PATH/config-current.sh
DEBUG=false
DEBUG_LOG_PATH=$MY_PATH/debug.log
_log() {
if ${DEBUG}; then
local msg="$1"
echo "$(date +"%Y-%m-%d %H:%M:%S") - $msg" >>$DEBUG_LOG_PATH
fi
}
_break_countdown() {
for i in $(seq 1 $BREAK_TIME_IN_SECONDS); do
LAST_LOCK=$(_seconds_since_last_event 'screenlocker')
if ((BREAK_TIME_IN_SECONDS > LAST_LOCK)); then
pkill -f "safe-eyes-break"
fi
echo $((100 * i / BREAK_TIME_IN_SECONDS))
sleep 1
done
}
_break_dialog() {
if [[ $(_is_webcam_used) == 1 || $(_is_microphone_used) == 1 ]]; then
_log "Webcam or microphone used - skipping break"
return
fi
_log "break"
# TODO "GDK_BACKEND=x11" is workaround on Wayland to prevent minimizing break dialog (i.e. skip break) trough shortcuts like "showing desktop (Win+D)"
# original "safeeyes" locks keyboard: https://github.com/slgobinath/SafeEyes/blob/master/safeeyes/ui/break_screen.py#L239
# possible solution: fork process to focus on break dialog using "wmctrl" every 1s
_break_countdown | GDK_BACKEND=x11 yad --progress --title="safe-eyes-break" --no-escape --sticky --on-top --undecorated --skip-taskbar --fullscreen --timeout="${BREAK_TIME_IN_SECONDS}" \
--text="\n${BREAK_TIME_IN_SECONDS} seconds break" --hide-text --text-align=center --css='*{background-color: #31363b; color: #fcfcfc; font: 25px Sans;}' \
--no-buttons
_beep_end_of_break
}
# returns 1 if true, 0 otherwise
_is_webcam_used() {
lsmod | grep ^uvcvideo | rev | cut -d' ' -f-1
}
# returns 1 if true, 0 otherwise
_is_microphone_used() {
pactl list sources | grep -c RUNNING
}
_beep_end_of_break() {
# if doesn't work, search other sounds at /usr/share/sounds/..
paplay /usr/share/sounds/Oxygen-Sys-App-Message.ogg
}
_seconds_since_last_event() {
local event_keyword="$1"
# https://superuser.com/questions/357275/how-to-find-the-uptime-since-last-wake-from-standby
LAST_LOGIN_TIMESTAMP=$(journalctl -n1 --grep "$event_keyword" -o short-unix | cut -d. -f1)
TIMESTAMP=$(date +%s)
DIFF=$((TIMESTAMP - LAST_LOGIN_TIMESTAMP))
echo $DIFF
}
_postpone_break_if_screen_was_locked() {
LAST_LOGIN=$(_seconds_since_last_event 'USER_AUTH')
POSTPONE=$((WORK_TIME_IN_SECONDS - LAST_LOGIN))
if ((POSTPONE > 0)); then
_log "Detected locked screen - postponing break by $POSTPONE seconds"
sleep ${POSTPONE}
fi
}
_main_program() {
_log "Program has started"
while true; do
sleep ${WORK_TIME_IN_SECONDS}
_postpone_break_if_screen_was_locked
_break_dialog
done
}
# directly invoke functions for testing purpose
if [[ "$1" == "--test" ]]; then # first param is --test and is declared as function
if declare -f "$2" >/dev/null; then
"$2" # call the function
else
echo "Unknown function: $2"
fi
exit 0
fi
case "$1" in
start)
if [[ $(pgrep -cf "$(basename $0) start") -gt 1 ]]; then
echo "Already started - skipping"
exit 0
fi
if [[ "$2" == "--debug" ]]; then
echo "Program will save logs at $DEBUG_LOG_PATH"
DEBUG=true
fi
_main_program 2>&1 & # run in background
exit 0
;;
stop)
pkill -e -9 -f "$(basename $0) start"
exit 0
;;
config)
vi config-current.sh
exit 0
;;
help | --help | -help | "") # prints this help
echo "Usage: $0 [start (--debug)|stop|config|--test {function name}]"
exit 0
;;
esac