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 change
7 # to a recommended GPL license.
9 # Copyright 2024 Ian Kelling
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
15 # http://www.apache.org/licenses/LICENSE-2.0
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.
23 set -e; .
/usr
/local
/lib
/bash-bear
; set +e
25 script_name
="${0##*/}"
28 Usage: $script_name [OPTIONS]
30 Turns on/off secondary monitor.
38 -r Restart secondary monitor
40 -s No stream setup. Normally, split monitor into 2 virtual screens and
41 set max resolution 2560x1440
44 -h|--help Print help and exit.
46 Note: Uses GNU getopt options parsing style
52 sha256sum
</sys
/class
/drm
/$card/edid |
grep -oE '^.{10}'
56 if [[ $I3SOCK ]]; then
62 local target_out output tmps
66 if [[ ! ${ws_out[*]} ]]; then
68 tmps
=$
(i3
-t get_workspaces
)
69 if [[ ! $tmps ]]; then
72 tmps
=$
(jq
-r '.[] | .num, .output' <<<"$tmps")
74 read -r output ||
break
80 if [[ ${ws_out[$ws]} && ${ws_out[$ws]} != "$target_out" ]]; then
81 # if the workspace is already there, this will fail.
82 # if the workspace doesn't exist yet, it fails with:
83 # ERROR: No output matched
84 m i3
'[workspace="'$ws'"]' move workspace to output
$target_out
89 date "+%A, %B %d, %r, %S seconds"
91 ##### begin command line parsing ########
96 temp
=$
(getopt
-l help nrs
"$@") || usage
1
103 -h|
--help) usage
0 ;;
105 *) echo "$0: Internal error! unexpected args: $*" ; exit 1 ;;
110 ##### end command line parsing ########
116 m
() { printf "$script_name: %s\n" "$*"; "$@"; }
118 m
() { printf "$script_name: %s\n" "$*"; }
121 for mon_suffix
in LEFT RIGHT
; do
122 if xrandr
--listmonitors |
awk '$2 == "BIG-'$mon_suffix\" |
grep .
>/dev
/null
; then
123 m xrandr
--delmonitor BIG-
$mon_suffix
128 secondary_out
=$
(awk '$2 == "connected" && $3 != "primary" {print $1}' $xf)
129 primary_info
=$
(awk '$2 == "connected" && $3 == "primary" {print $1,$4}' $xf)
131 # res_info eg: 3840x2160+1920+0
132 read -r primary_out primary_res_info
<<<"$primary_info"
134 primary_res
=${primary_res_info%%+*}
135 primary_x
=${primary_res%x*}
136 primary_y
=${primary_res#*x}
139 if [[ $secondary_out ]]; then
141 # some monitors need off then on to become active
143 m xrandr
--output $secondary_out --off
148 left_right_arg
=--right-of
149 if [[ $secondary_out == HDMI2
&& $
(edid card0-HDMI-A-2
) == 192efbdcef
]] || \
150 [[ $secondary_out == HDMI-1
&& $
(edid card1-HDMI-A-1
) == 7c58f9ac1e
]] || \
151 [[ $secondary_out == DP-1
&& $
(edid card1-DP-1
) == f3364bc6c1
]]; then
152 left_right_arg
=--left-of
155 left_right_arg
="$left_right_arg $primary_out"
157 target_out
=$secondary_out
158 target_res
=$
(grep -A1 -E "^$secondary_out " $xf |
tail -n1 |
awk '{print $1}')
159 target_x
=${target_res%x*}
160 target_y
=${target_res#*x}
162 # disables any leftover secondary.
164 target_out
=$primary_out
166 target_res
=$primary_res
173 if $stream && (( target_x
> 1280 )); then
175 if (( target_x
> 2560 )); then
178 target_res
=${target_x}x
$target_y
181 if [[ $secondary_out ||
$primary_res != "$target_res" ]]; then
182 m xrandr
--output $target_out $left_right_arg --mode $target_res
185 half_x
=$
(( target_x
/ 2 ))
186 # 298 & 336 are millimeters. I took them from a monitor I was using. I
187 # don't know if they are important, I assume not important enough to
188 # change for different monitors.
189 m xrandr
--setmonitor BIG-LEFT
$half_x/298x
$target_y/336+$x_offset+0 $target_out
190 m xrandr
--setmonitor BIG-RIGHT
$half_x/298x
$target_y/336+$
(( x_offset
+ half_x
))+0 none
193 move-ws BIG-RIGHT
3 4 5 6 7 8 9 10
196 m xrandr
--output $target_out $left_right_arg --mode $target_res
197 move-ws
$target_out 2 3 5 6 7 8 9 10
200 m
/a
/bin
/ds
/i3-sway
/gen
204 date "+%A, %B %d, %r, %S seconds"