aboutsummaryrefslogtreecommitdiff
path: root/local/bin/mxrandr
blob: 7c51128e257e55c12dbe194987695dd7eb51d576 (plain)
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
#!/bin/bash

# get info from xrandr
connectedOutputs=$(xrandr | grep " connected" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
activeOutput=$(xrandr | grep -E " connected (primary )?[1-9]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")
disconnectedOutputs=$(xrandr | grep -E " disconnected (primary )?[1-9]+" | awk '{print $1}')

cmd="xrandr "
cmd_def=$cmd

INTERNAL=eDP1
HDMI=HDMI1
VGA=VGA1

for device in "$disconnectedOutputs"; do
	if [ -n "$defice" ]; then
		if [[ "$activeOutput" == *"$device"* ]]; then
			cmd=$cmd" --output $device --off"
		fi
	fi
done

function only_internal {
	cmd=$cmd" --output $INTERNAL --auto --primary"
	cmd=$cmd" --output $HDMI --off"
	cmd=$cmd" --output $VGA --off"
}

function internal_vga {
	cmd=$cmd" --output $INTERNAL --auto --primary"
	cmd=$cmd" --output $HDMI --off"
	cmd=$cmd" --output $VGA --auto --right-of $INTERNAL"
}

function internal_hdmi {
	cmd=$cmd" --output $INTERNAL --auto --right-of $HDMI"
	cmd=$cmd" --output $HDMI --auto --primary"
	cmd=$cmd" --output $VGA --off"
}

function internal_hdmi_vga {
	cmd=$cmd" --output $INTERNAL --auto --right-of $HDMI"
	cmd=$cmd" --output $HDMI --auto --primary"
	cmd=$cmd" --output $VGA --auto --left-of $HDMI"
}

function cmd_exec {
	if [ "$cmd" != "$cmd_def" ]; then
		echo $cmd
		`$cmd`
	fi
}


if [ $# -le 1 ]; then
	if [[ "$connectedOutputs" == *"$HDMI"* ]]; then
		if [[ "$connectedOutputs" == *"$VGA"* ]]; then
			internal_hdmi_vga
		else
			internal_hdmi
		fi
	else
		if [[ "$connectedOutputs" == *"$VGA"* ]]; then
			internal_vga
		else
			only_internal
		fi
	fi
	cmd_exec
	exit
fi

if [ "$2" != "mode" ]; then
	if [[ "$connectedOutputs" != *"$2"* ]]; then
		echo No $2 display known
		exit
	fi
fi

case "$1" in
	toggle)
		case "$2" in
			$INTERNAL)
				if [[ "$activeOutput" == *"$INTERNAL"* ]]; then
					cmd=$cmd" --output $INTERNAL --off"
				else
					if [[ "$activeOutput" == *"$HDMI"* ]]; then
						cmd=$cmd" --output $INTERNAL --auto --right-of $HDMI"
					else
						cmd=$cmd" --output $INTERNAL --auto --primary"
					fi
				fi
				;;
			$HDMI)
				if [[ "$activeOutput" == *"$HDMI"* ]]; then
					cmd=$cmd" --output $HDMI --off"
				else
					if [[ "$activeOutput" == *"$VGA"* ]]; then
						cmd=$cmd" --output $HDMI --auto --primary"
					else
						cmd=$cmd" --output $HDMI --auto --right-of $INTERNAL"
					fi
				fi
				;;
			$VGA)
				if [[ "$activeOutput" == *"$VGA"* ]]; then
					cmd=$cmd" --output $VGA --off"
				else
					if [[ "$activeOutput" == *"$HDMI"* ]]; then
						cmd=$cmd" --output $VGA --auto --left-of $HDMI"
					else
						cmd=$cmd" --output $VGA --auto --right-of $INTERNAL"
					fi
				fi
				;;
		esac
		;;
esac
cmd_exec