bed869d3594d7d293200c49b13c6b724e390bec5
[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" | tail -n1 | awk '{print $1}' $xf)
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=0
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=$primary_x
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 xrandr --setmonitor BIG-LEFT $half_x/298x$y/336+0+$x_offset $output
82 xrandr --setmonitor BIG-RIGHT $half_x/298x$y/336+$(( x_offset + half_x ))+0 none
83
84
85 for i in 2 3 4 5 6 7 8 9 10; do
86 # if the workspace is already there, this will fail.
87 # if the workspace doesn't exist yet, it fails with:
88 # ERROR: No output matched
89 i3-msg '[workspace="'$i'"]' move workspace to output $output ||:
90 done
91 else
92 xrandr --auto
93
94 fi