-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoggle-monitor-grayscale.sh
executable file
·224 lines (173 loc) · 5.07 KB
/
toggle-monitor-grayscale.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
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
#!/bin/bash
# should also work with compositor=compton, untested
compositor=picom
function usage {
bin=$(basename $0)
echo
echo "Toggle monitors between color and grayscale mode."
echo
echo "$bin [$compositor|nvidia|ddc|auto]"
echo "$bin $compositor [$compositor args]"
echo "$bin nvidia [nv mon]"
echo "$bin ddc [ddc mon]"
echo
echo "$compositor: use a GLX shader to set grayscale"
echo "nvidia: use NVIDIA proprietary driver Digital Vibrance setting to set grayscale"
echo "ddc: use DDC/CI monitor protocol to set the monitor saturation to 0 (grayscale) if supported by monitor"
echo "auto: use $compositor if running, otherwise nvidia if available, otherwise ddc if available"
echo
echo "$compositor args: in $compositor mode, optional $compositor parameters"
echo
echo "nv mon: in nvidia mode, an optional monitor name as enumerated by xrandr."
echo " if unspecified, apply to all monitors managed by the NVIDIA driver"
echo "ddc mon: in ddc mode, optional ddcutil options to identify the monitor. See 'man ddcutil'"
echo " if unspecified, apply to the first monitor detected by ddcutil"
echo "if invoked with no argument, auto is used."
echo
exit 0
}
function toggle_nvidia {
dpy=$1
value=$(nvidia-settings -t -q DigitalVibrance)
# set a value in ]-1024..0[ range to desaturate colors instead of full grayscale
# -1024 => full grayscale
desaturate_value=-1024
if (( value == $desaturate_value )); then
value=0
toggle_mode="color"
else
value=$desaturate_value
toggle_mode="grayscale"
fi
if [ -n "$dpy" ]; then
param="[DPY:$dpy]/DigitalVibrance"
else
param="DigitalVibrance"
fi
nvidia-settings -a ${param}=${value} > /dev/null
}
function toggle_compositor {
if $compositor --help | grep legacy-backends > /dev/null; then
use_experimental_backends=1;
grep_string="window-shader-fg"
else
use_experimental_backends=0;
grep_string="glx-fshader-win"
fi
if pgrep -a -x $compositor | grep $grep_string > /dev/null; then
pkill -x $compositor
sleep 1
$compositor $* -b
toggle_mode="color"
else
pkill -x $compositor
sleep 1
if (( $use_experimental_backends == 1 )); then
tmpfile=$(mktemp)
trap 'rm -f "${tmpfile}"' EXIT
cat > ${tmpfile} <<EOF
#version 330
in vec2 texcoord;
uniform sampler2D tex;
uniform float opacity;
vec4 default_post_processing(vec4 c);
vec4 window_shader() {
vec4 c = default_post_processing(texelFetch(tex, ivec2(texcoord), 0));
float y = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722));
c = opacity*vec4(y, y, y, c.a);
return c;
}
EOF
$compositor $* -b --backend glx --window-shader-fg ${tmpfile} 2> /dev/null
else
shader='uniform sampler2D tex; uniform float opacity; void main() { vec4 c = texture2D(tex, gl_TexCoord[0].xy); float y = dot(c.rgb, vec3(0.2126, 0.7152, 0.0722)); gl_FragColor = opacity*vec4(y, y, y, c.a); }'
$compositor $* -b --backend glx --glx-fshader-win "${shader}" 2> /dev/null
fi
toggle_mode="grayscale"
fi
}
function toggle_ddc {
out=($(ddcutil $* getvcp 8a -t))
if (( $? != 0 )); then
echo "ddc: this monitor does not support saturation control"
exit 1
fi
# out array:
#
# VCP 8A C 100 200
# | |
# cur max
if (( ${#out[@]} != 5 )); then
echo "ddc: unexpected output getting current saturation state"
exit 1
fi
cur_saturation=${out[3]}
max_saturation=${out[4]}
# set a value in ]0..max/2[ range to desaturate colors instead of full grayscale
# 0 => full grayscale
desaturate_value=0
if (( cur_saturation == desaturate_value )); then
new_saturation=$(( max_saturation / 2 )) # nominal saturation
toggle_mode="color"
else
new_saturation=$desaturate_value
toggle_mode="grayscale"
fi
ddcutil $* setvcp 8a $new_saturation
}
mode=$1
case $mode in
--help|-h)
usage
;;
$compositor)
if ! pgrep -x $compositor > /dev/null; then
echo "$compositor is not running"
exit 1
fi
;;
nvidia)
if ! which nvidia-settings &> /dev/null; then
echo "nvidia-settings is not installed"
exit 1
fi
;;
ddc)
if ! which ddcutil &> /dev/null; then
echo "ddcutil is not installed"
exit 1
fi
;;
*)
[ -z "$mode" ] && mode=auto
if [ "$mode" = "auto" ]; then
if pgrep -x $compositor > /dev/null; then
mode=$compositor
elif which nvidia-settings &> /dev/null; then
mode=nvidia
elif which ddcutil &> /dev/null; then
mode=ddc
else
echo "neither $compositor is running, nor nvidia-settings installed, nor ddcutil installed"
exit 1
fi
else
usage
fi
esac
# pass eventual remaining arguments to toggle_* function
if (( $# > 0 )); then
shift
fi
if [ "$mode" = "nvidia" ]; then
toggle_nvidia $*
elif [ "$mode" = "$compositor" ]; then
toggle_compositor $*
else
toggle_ddc $*
fi
if (( $? == 0 )); then
echo "$mode: set to $toggle_mode"
else
echo "$mode: toggle failed"
fi