-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpixelchanged.sh
executable file
·128 lines (104 loc) · 2.68 KB
/
pixelchanged.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
#!/bin/bash
set -eu
set -o pipefail
readonly TEMP_SCREENSHOT=/tmp/pixelchanged.png
readonly TEMP_SCREENSHOT_LAST=/tmp/pixelchanged_last.png
# period for checking changes in seconds.
readonly period_sleep=5
last_hash=""
check_command() {
local cmd="${1:-}"
if ! hash "${cmd}" 2>/dev/null; then
echo -e "pixelchanged: Could not find command '${cmd}'. Please install it."
exit 1
fi
}
# determine if current desktop is wayland or x11
determine_desktop_type()
{
for session_id in $(loginctl list-sessions --no-legend | awk '{ print $1 }'); do
local is_active=$(loginctl show-session -p Active --value $session_id)
if [ $is_active == "yes" ];then
desktop_type=$(loginctl show-session -p Type --value $session_id)
echo "${desktop_type}"
return 0
fi
done
# todo notfound
}
check_commands()
{
check_command "awk"
if [ ${desktop_type} == "wayland" ];then
check_command "grim"
check_command "slurp"
return 0
fi
# x11
check_command "import"
check_command "slop"
}
take_screenshot()
{
if [ $desktop_type == "wayland" ];then
grim -g "${pixel_area}" ${TEMP_SCREENSHOT}
return 0
fi
# x11
read -r G < <(echo $pixel_area)
import -window root -crop $G ${TEMP_SCREENSHOT}
}
# select a region of the screen for monitoring changes
select_pixel_area()
{
# wayland
if [ $desktop_type == "wayland" ];then
echo "$(slurp)"
return 0
fi
# x11
echo $(slop -of "%g")
}
# example function to play a sound
play_sound()
{
check_command "cvlc"
cvlc --play-and-exit ./Whistling.mp3
}
# example function to desktop notification
notify_desktop()
{
notify-send --urgency=low -t 3000 "pixelchanged" "The was a pixel change!"
}
change_handler()
{
echo "Playing sound"
# put commands here that are executed when a change in the pixel happens
#notify_desktop
play_sound
}
readonly desktop_type=$(determine_desktop_type)
check_commands
echo "Please select a region of the screen"
readonly pixel_area=$(select_pixel_area)
echo "Selected region: ${pixel_area}. Desktop type: ${desktop_type}"
echo
echo "📡 Starting monitoring for changes of the selected region each ${period_sleep} seconds."
while :
do
take_screenshot
h="$(sha256sum ${TEMP_SCREENSHOT})"
if [ -z "$last_hash" ]; then
last_hash="$h"
mv ${TEMP_SCREENSHOT} ${TEMP_SCREENSHOT_LAST}
sleep ${period_sleep}
continue
fi
# pixel changes
if [ "$h" != "$last_hash" ]; then
change_handler
fi
last_hash="$h"
mv ${TEMP_SCREENSHOT} ${TEMP_SCREENSHOT_LAST}
sleep ${period_sleep}
done