forked from 3o14r473/qmenu
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathqmenu-dm
executable file
·167 lines (105 loc) · 3.53 KB
/
qmenu-dm
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
#!/bin/sh
get_qube_label() {
qube_label=$(echo "$qube_list" | grep -w "$target_qube" | awk '{print $2}')
# Change label colors according to ~/.config/qmenu.conf if that file exists.
if [ -f "$HOME/.config/qmenu.conf" ]; then
qube_label=$(grep "^$qube_label=" "$HOME"/.config/qmenu.conf | cut -d= -f2)
fi
}
rearrange_qube_list() {
qube_list=$(echo "$qube_list" |\
awk -v ListInOrder="purple,blue,gray,green,yellow,orange,red,black" '
BEGIN {
split(ListInOrder, a, ",")
for (i = 1; i <= length(a); ++i) ListToOrdinal[a[i]] = i
}
{print ListToOrdinal[$2] "-" $0}
' | sort -t- -k1,1n | cut -d- -f2-)
}
if [ "$2" = "--light-theme" ]; then
theme_0='#ffffff'
theme_1='#000000'
else
theme_0='#000000'
theme_1='#ffffff'
fi
# Note that the '-m 0' option in 'dmenu' is important
# for security as it restricts it to monitor 0.
dmenu_fmc="-f -m 0 -nb $theme_0 -nf $theme_1 -sb $theme_1 -sf $theme_0"
case $1 in
--all)
device_type=$(printf "Audio input devices\nBlock devices\nUSB devices" |\
dmenu -i -l 3 $dmenu_fmc | awk '{print $1}');;
--audio-input)
device_type="Audio";;
--block)
device_type="Block";;
--usb)
device_type="USB";;
*)
printf "qmenu-dm [OPTION] (--light-theme)\nList and manage your connected devices.\n\n --all\n --audio-input\n --block\n --usb\n\n\nqmenu v1.3\n<https://github.com/sine3o14nnae/qmenu/>\n<https://gitlab.com/sine3o14nnae/qmenu/>\n\n"
if [ "$1" = "--help" ]; then exit 0; fi
esac
# Depending on the device type, we have to
# 'awk' different fields later on.
if [ "$device_type" = "Audio" ]; then
device_type="device mic"
awk_devicename0=2
awk_devicename1=10
awk_targetqube=3
elif [ "$device_type" = "Block" ]; then
device_type="block"
awk_devicename0=2
awk_devicename1=3
awk_targetqube=4
elif [ "$device_type" = "USB" ]; then
device_type="usb"
awk_devicename0=2
awk_devicename1=10
awk_targetqube=3
else
exit 2
fi
qube_list=$(qvm-ls --no-spinner --running -O NAME,LABEL |\
grep -vw 'dom0\|sys-usb')
rearrange_qube_list
devices_list=$(qvm-$device_type)
device=0
while [ -n "$device" ]; do
device=$(echo "$devices_list" | dmenu -l 16 $dmenu_fmc)
device_id=$(echo "$device" | awk '{print $1}')
device_name=$(echo "$device" |\
awk '{print $'$awk_devicename0'$'$awk_devicename1'}')
if [ -n "$device" ]; then
target_qube=$(echo "$devices_list" | grep "$device_id" |\
awk '{print $'$awk_targetqube'}')
if [ -z "$target_qube" ]; then
target_qube=$(echo "$qube_list" |\
sed '1d' | dmenu -p "attach to:" -l 32 $dmenu_fmc | awk '{print $1}')
if [ -n "$target_qube" ]; then
get_qube_label
prompt=$(printf "No\nYes" |\
dmenu -i -p "Attach $device_name to $target_qube?"\
-f -m 0 -nb $theme_0 -nf $theme_1 -sb $qube_label -sf $theme_1)
if [ "$prompt" = "Yes" ]; then
if qvm-$device_type attach "$target_qube" "$device_id"; then exit 0; fi
echo "Quit..." |\
dmenu -p "Failed to attach device!" $dmenu_fmc > /dev/null 2>&1
exit 2
fi
fi
else
get_qube_label
prompt=$(printf "No\nYes" |\
dmenu -i -p "Detach $device_name from $target_qube?"\
-f -m 0 -nb $theme_0 -nf $theme_1 -sb $qube_label -sf $theme_1)
if [ "$prompt" = "Yes" ]; then
if qvm-$device_type detach "$target_qube" "$device_id"; then exit 0; fi
echo "Quit..." |\
dmenu -p "Failed to detach device!" $dmenu_fmc > /dev/null 2>&1
exit 2
fi
fi
fi
done
exit 1