-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch-monitor-source
executable file
·111 lines (97 loc) · 3.94 KB
/
switch-monitor-source
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
#!/bin/bash
# switch-monitor-source
# This script is used for switching input sources between hdmi/vga/displayport
# for my DELL P2217H monitor.
# It uses i2c-bus-by-monitor-serial script as a dependency.
# For your usage, you should edit the following in this script: TARGET_SERIAL (optional, if you
# want use your s/n as default), cases for your PNP_IDs, VALUEs for your monitor (I have described
# how to explore them here: https://askubuntu.com/a/1010096/795775) and possibly add additional keys
# for switching using them (for example, -h2 for hdmi2, -v2 for vga2).
if [[ $1 == "--help" ]]; then
echo -e "\
Usage:
switch-monitor-source [-s <serial_number>] [-i <i2c bus number>] [-p <Plug and Play ID>] -r|-u|-h|-v|-d|
-s - set serial number of your monitor which you want to control.
-i - needed when several monitors with same s/n were found. Otherwise is sets automatically.
-p - explicitly set PNP_ID instead of extracting from edid. For development needs.
-r - read current value for 0x60 command
-u - update cached i2c-id number. You need do this after every (?) reboot and after every (?) monitor
replugging.
-h|-v|-d - switch input source of monitor to hdmi/vga/displayport
--help - print this help
Example:
switch-monitor-source -s 0G2TG7360DXB -u # update file with i2c-id for monitor with specified s/n
switch-monitor-source -s 0G2TG7360DXB -v # switch that monitor to vga input
"
exit
fi
if [[ $1 == "-s" ]]; then
TARGET_SERIAL=$2 # use passed s/n
shift; shift
else
:
TARGET_SERIAL="0G2TG7360EMB" # default s/n. change to your monitor's serial number
fi
if [[ `lsmod | grep i2c_dev` == "" ]]; then
echo "Probing i2c-dev module..."
sudo modprobe i2c-dev # you can add it permanently by running "i2c-dev >> /etc/modules"
else : echo "i2c-dev module was already probbed"
fi
CACHED_ID_FILE="/tmp/$TARGET_SERIAL.i2c-id.txt"
if #[[ /dev/i2c-`cat $CACHED_ID_FILE` -nt $CACHED_ID_FILE || $1 == "-u" ]]; then # TODO automatically check if need to update cached id
[[ ${!#} == "-u" ]]; then
echo `i2c-bus-by-monitor-serial -s $TARGET_SERIAL | tail -n1` > $CACHED_ID_FILE
echo -e "$TARGET_SERIAL is now on bus i2c-" "`cat $CACHED_ID_FILE`\n$CACHED_ID_FILE has been updated"
exit 0
fi
I2C_ID="-1"
if [[ `grep "and" $CACHED_ID_FILE` != "" ]]; then
echo "Warning! There are several i2c ids with the same monitor serial number!"
if [[ $1 != "-i" ]]; then
echo "Use -i to specify i2c id manually! Choose from `cat $CACHED_ID_FILE`"
exit 1
else
I2C_ID=$2 # manually specified if there are several ids
shift; shift
fi
elif [[ ! -f $CACHED_ID_FILE || ! -s $CACHED_ID_FILE ]]; then
echo "$CACHED_ID_FILE does not exist or is empty. Use -u to update/create it."
else
I2C_ID=`cat $CACHED_ID_FILE` # we got univocal id
fi
if [[ ${!#} == "-r" ]]; then # show current value for 0x60 command
echo "Going to read 0x60"
sudo ddccontrol -r 0x60 dev:/dev/i2c-$I2C_ID | grep "0x60"
fi
PNP_ID=`sudo ddccontrol -c dev:/dev/i2c-$I2C_ID 2>/dev/null | grep "Plug and Play ID:" | cut -f5 -d" "`
echo "PNP_ID=$PNP_ID"
VALUE=""
if [[ $1 == "-p" ]]; then
PNP_ID=$2
shift; shift
# echo "PNP_ID=$PNP_ID"
fi
case $PNP_ID in
"DELA0D7") # Dell P2217H over vga transport
case ${!#} in
"-h") VALUE=273 ;; # Switch to HDMI source
"-v") VALUE=257 ;; # Switch to VGA source
"-d") VALUE=271 ;; # Switch to DisplayPort source
esac
;;
"DELA0D9") # Dell P2217H over hdmi transport
case ${!#} in
"-h") VALUE=4369 ;; # Switch to HDMI source
"-v") VALUE=4353 ;; # Switch to VGA source
"-d") VALUE=4367 ;; # Switch to DisplayPort source
#"") # TODO count arguments and if zero, then
#echo "You have not specified a switch action"
esac
;;
esac
if [[ $VALUE != "" ]]; then
echo "Writing $VALUE to 0x60"
sudo ddccontrol -r 0x60 -w $VALUE dev:/dev/i2c-$I2C_ID 1>/dev/null
fi
# TODO maybe reprogramm on C language for more performance? This script takes about 3,1 seconds to switch input source,
# however using just command sudo ddccontrol -r 0x60 -w 257 dev:/dev/i2c-2 1>/dev/null takes only around 1,2 seconds.