d57b43f1b6ec49f43d50a82e9a72036acb35ab54
[distro-setup] / laptop-xrandr
1 #!/bin/bash
2 # I, Ian Kelling, follow the GNU license recommendations at
3 # https://www.gnu.org/licenses/license-recommendations.en.html. They
4 # recommend that small programs, < 300 lines, be licensed under the
5 # Apache License 2.0. This file contains or is part of one or more small
6 # programs. If a small program grows beyond 300 lines, I plan to switch
7 # its license to GPL.
8
9 # Copyright 2024 Ian Kelling
10
11 # Licensed under the Apache License, Version 2.0 (the "License");
12 # you may not use this file except in compliance with the License.
13 # You may obtain a copy of the License at
14
15 # http://www.apache.org/licenses/LICENSE-2.0
16
17 # Unless required by applicable law or agreed to in writing, software
18 # distributed under the License is distributed on an "AS IS" BASIS,
19 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 # See the License for the specific language governing permissions and
21 # limitations under the License.
22
23 set -e; . /usr/local/lib/bash-bear; set +e
24
25 xf=$(mktemp)
26 xrandr >$xf
27
28 # eg eDP-1 connected primary
29 laptop_out=$(awk '$3 == "primary" {print $1}' $xf)
30 output=$(grep -E "^(HDMI|DP)[^ ]* connected [0-9]" $xf| awk '{print $1}' ||:)
31
32 edid() {
33 card=$1
34 sha256sum </sys/class/drm/$card/edid | grep -oE '^.{10}'
35 }
36
37 restart=false
38 left_right_arg=--right-of
39 if [[ $output ]]; then
40
41 mode=$(grep -A1 -E "^$output" $xf | tail -n1 | awk '{print $1}')
42
43 primary_res=$(awk '$2 == "connected" && $3 == "primary" { print $4 }' $xf | sed 's/+.*//')
44 primary_x=${primary_res%x*}
45
46 x=${mode%x*}
47 y=${mode#*x}
48
49 half_x=$(( x / 2 ))
50
51 x_offset=$primary_x
52
53 # identify monitors that are always on the left.
54 if [[ $output == HDMI2 && $(edid card0-HDMI-A-2) == 192efbdcef ]] || \
55 [[ $output == HDMI-1 && $(edid card1-HDMI-A-1 ) == 7c58f9ac1e ]]; then
56 left_right_arg=--left-of
57 x_offset=0
58 fi
59
60 # some monitors need off then on to become active
61 if $restart; then
62 xrandr --output $output --off
63 sleep 2
64 fi
65
66
67 # eg: 3840x2160
68 echo mode=$mode
69 xrandr --output $output $left_right_arg $laptop_out --mode $mode
70
71
72 # This splits a monitor into 2 virtual screens, which makes it split
73 # into 2 workspaces in i3, which is a much more convenient setup for
74 # managing windows.
75 # todo: add an option to turn this off
76 # todo: do this on systems with just 1 monitor.
77 #
78 # /298 & 336 are millimeters. I took them from a monitor I was using. I
79 # don't know if they are important, I assume not important enough to
80 # change for different monitors.
81
82 for x in LEFT RIGHT; do
83 if xrandr --listmonitors | awk '$2 == BIG-'$x |grep . >/dev/null; then
84 xrandr --delmonitor BIG-$x
85 fi
86 done
87 xrandr --setmonitor BIG-LEFT $half_x/298x$y/336+0+$x_offset $output
88 xrandr --setmonitor BIG-RIGHT $half_x/298x$y/336+$(( x_offset + half_x ))+0 none
89
90
91 for i in 2 3 4 5 6 7 8 9 10; do
92 # if the workspace is already there, this will fail.
93 # if the workspace doesn't exist yet, it fails with:
94 # ERROR: No output matched
95 i3-msg '[workspace="'$i'"]' move workspace to output $output ||:
96 done
97 else
98 xrandr --auto
99
100 fi