-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmy-udev-notify.sh
More file actions
306 lines (237 loc) · 8.12 KB
/
Copy pathmy-udev-notify.sh
File metadata and controls
306 lines (237 loc) · 8.12 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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/bin/bash
# thanks:
# - to guys from linux.org.ru;
# - to 'iptable' user from ##linux at irc.freenode.net.
# test command:
# sudo /bin/bash my-udev-notify -a add -p 'test_path' -b '555' -d '777'
# get path to this script
DIR="$(dirname $(readlink -f "$0"))"
# set default options {{{
# file for storing list of currently plugged devices
devlist_file="/var/tmp/udev-notify-devices"
show_notifications=true
notification_icons=true
play_sounds=true
use_espeak=false
plug_sound_path="$DIR/sounds/plug_sound.wav"
unplug_sound_path="$DIR/sounds/unplug_sound.wav"
# }}}
# read config file {{{
{
if [ -r /etc/my-udev-notify.conf ]; then
. /etc/my-udev-notify.conf
fi
#if [ -r ~/.my-udev-notify.conf ]; then
#. ~/.my-udev-notify.conf
#fi
}
# }}}
# retrieve options from command line {{{
# action: "add" or "remove"
action=
# dev_path: path like /devices/pci0000:00/0000:00:1d.0/usb5/5-1
dev_path=
# bus number and device number:
# they are needed since device is also stored at /dev/bus/usb/<bus_num>/<dev_num>
bus_num=
dev_num=
while getopts a:p:b:d: opt; do
case $opt in
a)
action=$OPTARG
;;
p)
dev_path=$OPTARG
;;
b)
bus_num=$OPTARG
;;
d)
dev_num=$OPTARG
;;
esac
done
shift $((OPTIND - 1))
# }}}
get_device_icon()
{
local dev_data=`echo "$1" | sed 's/###/\n/g' | grep -e 'bInterfaceClass' -A 2 -m 1 | cut -d ' ' -f 2,3,4,5,6,7,8,9 | tr -s ' ' '_'`
OLD_IFS=$IFS
IFS="
"
local dev_array=($dev_data)
IFS=$OLD_IFS
local class=${dev_array[0]}
local subclass=${dev_array[1]}
local protocol=${dev_array[2]}
case "$class:$subclass:$protocol" in
Audio:* )
dev_icon="audio-card"
;;
Communications:Abstract* )
dev_icon="modem"
;;
Communications:Ethernet* )
dev_icon="network-wired"
;;
Human_Interface_Device:*:Keyboard )
dev_icon="input-keyboard"
;;
Human_Interface_Device:*:Mouse )
dev_icon="input-mouse"
;;
Mass_Storage:RBC:* )
dev_icon="media-removable"
;;
Mass_Storage:Floppy:* )
dev_icon="media-floppy"
;;
Mass_Storage:SCSI:* )
dev_icon="media-removable"
;;
Printer:* )
dev_icon="printer"
;;
Hub:* )
dev_icon="emblem-shared"
;;
Video:* )
dev_icon="camera-web"
;;
Xbox:Controller:* )
dev_icon="input-gaming"
;;
Wireless:Radio_Frequency:Bluetooth )
dev_icon="bluetooth"
;;
*)
dev_icon="dialog-information"
;;
esac
}
show_visual_notification()
{
# TODO: wait for 'iptable' user from ##linux to say how to do it better
# or, at least it's better to use 'who' command instead of 'w',
# because 'who' echoes display number like (:0), and echoes nothing if no display,
# which is more convenient to parse.
local header=$1
local text=$2
if [[ $notification_icons == true ]]; then
get_device_icon "$text"
else
dev_icon=''
fi
text=`echo "$text" | sed 's/###/\n/g'`
declare -a logged_users=($(print_logged_users))
for (( i=0; i<${#logged_users[@]}; i=($i + 1) )); do
cur_user=${logged_users[$i]}
su $cur_user -c "notify-send -i '$dev_icon' '$header' '$text'"
done
}
print_logged_users()
{
who | grep "(.*)" | sed 's/^\s*\(\S\+\).*/\1/g' | uniq | sort
}
sound_or_speak()
{
local soundfile=$1
local speaktext=$2
declare -a logged_users=($(print_logged_users))
for (( i=0; i<${#logged_users[@]}; i=($i + 1) )); do
cur_user=${logged_users[$i]}
if [[ $use_espeak == true ]]; then
if [[ "$speaktext" != "" ]]; then
su $cur_user -c "/usr/bin/espeak '$speaktext'"
fi
else
if [[ -r "$soundfile" ]]; then
su $cur_user -c "/usr/bin/play -q '$soundfile'"
fi
fi
done
}
# notification for plugged device {{{
notify_plugged()
{
local dev_title=$1
local dev_name=$2
if [[ $show_notifications == true ]]; then
#notify-send "device plugged" "$dev_title" &
show_visual_notification "device plugged" "$dev_title"
fi
if [[ $play_sounds == true ]]; then
sound_or_speak "$plug_sound_path" "Device plugged: $dev_name"
fi
}
# }}}
# notification for unplugged device {{{
notify_unplugged()
{
local dev_title=$1
local dev_name=$2
if [[ $show_notifications == true ]]; then
#notify-send "device unplugged" "$dev_title" &
show_visual_notification "device unplugged" "$dev_title"
fi
if [[ $play_sounds == true ]]; then
sound_or_speak "$unplug_sound_path" "Device unplugged: $dev_name"
fi
}
# }}}
(
# we need for lock our $devlist_file
flock -w 10 200 || exit 1
case $action in
"reboot" )
rm $devlist_file
;;
"add" )
# ------------------- PLUG -------------------
if [[ "$bus_num" != "" && "$dev_num" != "" ]]; then
# make bus_num and dev_num have leading zeros
bus_num=`printf %03d $bus_num`
dev_num=`printf %03d $dev_num`
# Retrieve device title. Currently it's done just by lsusb and grep.
# Not so good: if one day lsusb change its output format, this script
# might stop working.
dev_title=`lsusb -D /dev/bus/usb/$bus_num/$dev_num | grep '^Device:\|bInterfaceClass\|bInterfaceSubClass\|bInterfaceProtocol'|sed 's/^\s*\([a-zA-Z]\+\):*\s*[0-9]*\s*/<b>\1:<\/b> /' | awk 1 ORS='###'`
dev_name=`lsusb -D /dev/bus/usb/$bus_num/$dev_num | grep idProduct | tr -s ' ' | cut -s -d' ' -f4,5,6,7,8,9`
# Sometimes we might have the same device attached to different bus_num or dev_num:
# in this case, we just modify bus_num and dev_num to the current ones.
# At least, it often happens on reboot: during previous session, user plugged/unplugged
# devices, and dev_num is increased every time. But after reboot numbers are reset,
# so with this substitution we won't have duplicates in our devlist.
escaped_dev_path=`echo "$dev_path" | sed 's/[\/&*.^$]/\\\&/g'`
sed -i "s#^\([0-9]\{3\}:\)\{2\}\($escaped_dev_path\)#$bus_num:$dev_num:$dev_path#" $devlist_file
# udev often generates many events for the same device
# (I still don't know how to write udev rule to prevent it)
# so we need to check if this device is already stored in our devlist file
existing_dev_on_bus_cnt=`cat $devlist_file | grep "^$bus_num:$dev_num:" | awk 'END {print NR}'`
if [[ $existing_dev_on_bus_cnt == 0 ]]; then
# this device isn't stored yet in the devlist, so let's write it there.
echo "$bus_num:$dev_num:$dev_path title=\"$dev_title\"" >> $devlist_file
echo "$bus_num:$dev_num:$dev_path name=\"$dev_name\"" >> $devlist_file
# and, finally, notify the user.
notify_plugged "$dev_title" "$dev_name"
fi
fi
;;
"remove" )
# ------------------- UNPLUG -------------------
# Unfortunately, udev doesn't emit bus_num and dev_num for "remove" events,
# and there's even no vendor_id and product_id.
# But it emits dev_path. So we have to maintain our own plugged devices list.
# Now we retrieve stored device title from our devlist by its dev_path.
dev_title=`cat $devlist_file | grep "$dev_path " | grep 'title="' | sed 's/.*title=\"\(.*\)\".*/\1/g'`
dev_name=`cat $devlist_file | grep "$dev_path " | grep 'name="' | sed 's/.*name=\"\(.*\)\".*/\1/g'`
# remove that device from list (since it was just unplugged)
cat $devlist_file | grep -v "$dev_path " > ${devlist_file}_tmp
mv ${devlist_file}_tmp $devlist_file
# if we have found title, then notify user, after all.
if [[ "$dev_title" != "" ]]; then
notify_unplugged "$dev_title" "$dev_name"
fi
;;
esac
) 200>/var/lock/.udev-notify-devices.exclusivelock