-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudev.in
140 lines (121 loc) · 3.75 KB
/
udev.in
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
# This is free and unencumbered software released into the public domain.
#
# Anyone is free to copy, modify, publish, use, compile, sell, or
# distribute this software, either in source code form or as a compiled
# binary, for any purpose, commercial or non-commercial, and by any
# means.
# In jurisdictions that recognize copyright laws, the author or authors
# of this software dedicate any and all copyright interest in the
# software to the public domain. We make this dedication for the benefit
# of the public at large and to the detriment of our heirs and
# successors. We intend this dedication to be an overt act of
# relinquishment in perpetuity of all present and future rights to this
# software under copyright law.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# For more information, please refer to <https://unlicense.org>
#
# revision @version@
udev_trigger_set=0
declare -A udev_type
# select which device types to generate udev event for
# outputs (speakers, headphones) are "sinks"
# inputs (microphones) are "sources"
# either comment out lines with unwanted types, or set value to "no"
udev_type[a2dp_sink]=yes
udev_type[a2dp_source]=yes
udev_type[sco_sink]=yes
udev_type[sco_source]=yes
udev_shortopts="u::"
udev_longopts="udev::"
udev_print_help() {
cat <<-EOF
-u,--udev generate udev event for device connect/dusconnect
- optional arg: [a2dp|sco][,[capture|playback]]
- default is a2dp, both capture and playback
EOF
}
udev_parse_options() {
while (( $# > 0 )) ; do
case "$1" in
-p|--udev)
bluealsa_monitor_enable_udev=true
shift
[[ -n "$1" ]] && udev_types="$1"
shift
;;
*) shift ;;
esac
done
}
udev_parse_types() {
local IFS=','
local temp=( $udev_types )
local word sink source a2dp sco
for word in "${temp[@]}"; do
case "$word" in
'capture') source=1 ;;
'playback') sink=1 ;;
'a2dp') a2dp=1 ;;
'sco') sco=1 ;;
*)
echo "Invalid --udev argument '$word'" >&2
exit 1
;;
esac
done
[[ "$source" = 1 ]] && {
[[ "$a2dp" = 1 ]] || udev_type[a2dp_source]=no
[[ "$sco" = 1 ]] || udev_type[sco_source]=no
}
[[ "$sink" = 1 ]] && {
[[ "$a2dp" = 1 ]] || udev_type[a2dp_sink]=no
[[ "$sco" = 1 ]] || udev_type[sco_sink]=no
}
}
# generate synthetic udev event
udev_action() {
bluealsa_internal_event UdevTrigger
sudo -n /bin/udevadm trigger --action=change /dev/snd/controlC0
}
# trigger a udev soundcard event
# delay the action for a short time to avoid multiple events for a device
# that supports multiple PCMs
udev_trigger() {
[[ "$bluealsa_monitor_enable_udev" ]] || return
[[ "$udev_trigger_set" = 0 ]] || return
udev_trigger_set=1
{ sleep 0.5 ; udev_action ; } &
}
udev_init() {
[[ "$bluealsa_monitor_enable_udev" ]] || return
[[ "$udev_types" ]] && udev_parse_types
}
udev_pcm_added() {
[[ "$bluealsa_monitor_enable_udev" ]] || return
local profile=a2dp
[[ "${bluealsa_new_pcm[profile]}" = HFP || "${bluealsa_new_pcm[profile]}" = HSP ]] && profile=sco
[[ ${udev_type[${profile}_${bluealsa_new_pcm[mode]}]} = yes ]] || return
udev_trigger
}
udev_pcm_removed() {
[[ "$bluealsa_monitor_enable_udev" ]] || return
udev_trigger
}
udev_service_stopped() {
[[ "$bluealsa_monitor_enable_udev" ]] || return
udev_trigger
}
udev_handle_internal_event() {
[[ "$1" = UdevTrigger ]] && udev_trigger_set=0
}
udev_finished() {
udev_service_stopped
}