Compare commits

...

7 Commits

Author SHA1 Message Date
dabruh 7128f6c74e Add attribute for enabled/disabled monitors 2024-04-19 09:31:59 +02:00
dabruh b433ef57d3 Add back missing xrandr command 2024-04-19 08:46:35 +02:00
dabruh fc71836102 Remove unused func 2024-04-19 08:45:23 +02:00
dabruh 0cab2a6640 Cache xrandr output 2024-04-19 08:45:15 +02:00
dabruh 1888ec9c96 Fix unnecessary if statement logic 2024-04-19 08:40:40 +02:00
dabruh da28a0e96e Add logic to disable disconnected monitors 2024-04-19 08:39:51 +02:00
dabruh 1237dff14a Add display config for work laptop at home 2024-04-19 08:35:27 +02:00
1 changed files with 53 additions and 8 deletions

View File

@ -4,19 +4,40 @@
set -euo pipefail
# Get the number of monitors connected to the system
get_monitor_count() {
xrandr --listmonitors | head -n1 | cut -d' ' -f2
}
_monitors=() # Cache for the xrandr output
# Get the list of connected and disconnected monitors.
# Includes information about the primary monitor
# E.g:
# eDP-1,connected,primary
# HDMI-1,disconnected
# DP-1,disconnected
# eDP-1,connected,primary,1920x1080+0+0,enabled
# HDMI-1,disconnected,1920x1080+1920+0,enabled
# DP-1,disconnected,disabled
_get_monitors() {
xrandr | grep -Eo '^.* ((dis|)connected)( primary|) ' | sed 's/ /,/g' | sed 's/,$//g'
local monitors enabled_regex="[[:digit:]]+x[[:digit:]]+\+[[:digit:]]+\+[[:digit:]]+" # Regex for enabled monitors
# Use caching to avoid multiple calls to xrandr
if [ ${#_monitors[@]} -eq 0 ]; then
# Get the list of connected and disconnected monitors
monitors=$(xrandr | grep -Eo "^.* ((dis|)connected)( primary|) ($enabled_regex|)" | sed 's/ /,/g' | sed 's/,$//g')
for monitor in $monitors; do
# Set "enabled" if it matches the enabled regex and remove the matching string, else set "disabled"
if [[ "$monitor" =~ $enabled_regex ]]; then
monitor="$monitor,enabled"
else
monitor="$monitor,disabled"
fi
# Add to the list of monitors
_monitors+=("$monitor")
done
# _monitors="$monitors"
fi
for monitor in "${_monitors[@]}"; do
echo "$monitor"
done
}
# Function that gets all monitors and takes an optional amount of arguments to filter the list.
@ -82,6 +103,19 @@ monitors_exist() {
return 0
}
# Disable all monitors that are not in the list of connected monitors
disable_disconnected_monitors() {
local connected_monitors
connected_monitors=$(get_monitors disconnected)
echo "Disabling disconnected monitors"
for monitor in $connected_monitors; do
echo "Disabling monitor $(cut -d',' -f1 <<<"$monitor")"
xrandr --output "$(cut -d',' -f1 <<<"$monitor")" --off
done
}
# Get the list of USB devices using lsusb, outputs for example "06cb:00f0 Synaptics, Inc."
get_usb_devices() {
lsusb | grep -Eo ' ID .*' | cut -d' ' -f3-
@ -326,6 +360,16 @@ main() {
--output "${monitor_array[0]}" --mode 2560x1440 --rotate normal --pos 0x480 --primary \
--output "${monitor_array[1]}" --mode 3440x1440 --rotate normal --pos 2560x480 \
--output "${monitor_array[2]}" --mode 1920x1200 --rotate right --pos 6000x0
profile_found=true
fi
monitor_array=(DP-3-3 eDP-1 DP-3-1)
if monitors_exist "${monitor_array[@]}" >/dev/null; then
xrandr \
--output "${monitor_array[0]}" --mode 3440x1440 --pos 0x480 --rotate normal \
--output "${monitor_array[1]}" --mode 1920x1200 --pos 3440x720 --rotate normal --primary \
--output "${monitor_array[2]}" --mode 1920x1200 --pos 5360x0 --rotate right
profile_found=true
fi
fi
@ -334,6 +378,7 @@ main() {
return 1
fi
disable_disconnected_monitors
move_nonexistent_i3_workspaces "$primary_monitor" 10
i3-msg restart >/dev/null
}