-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdpa
executable file
·117 lines (115 loc) · 5.04 KB
/
dpa
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
#!/usr/bin/env bash
###########################################################
###########################################################
########### Start user configuration ######################
# TODO: Fetch preferred mode from EDID
# TODO: Multiple monitors support for saving and resuming modes
# Resolution to restore to until default mode and/or mode restore is implemented
_default_resolution=2560x1440
_default_refresh_rate=120
_default_vrr_mode=automatic
# FIXME: Non-predictable connector name.
_dummy_plug=HDMI-A-1
_preferred_monitor_index=1
########### End of user configuration #####################
###########################################################
###########################################################
### No configurable values below
_session_type=$(loginctl 2>/dev/null show-session "$(awk '/tty/ {print $1}' <(loginctl list-sessions | grep "$USER" | grep "active" -w))" -p Type | awk -F= '{print $2}')
if [[ "$_session_type" != "x11" ]] && [[ "$_session_type" != "wayland" ]]; then
echo "Falling back to environment for session type detection"
environment=$(printenv)
if grep -qc "WAYLAND_DISPLAY" <<<"$environment"; then
_session_type="wayland"
elif grep -qc "DISPLAY" <<<"$environment"; then
_session_type="x11"
else
echo "Unknown session type: '$_session_type'. Please fix me!"
echo 0
fi
unset environment
fi
_connector_name_format="DP-"
# One day, we'll have a bloody standard for connector names...
if [[ "$_session_type" == "x11" ]]; then
_connector_name_format="DisplayPort-"
_dummy_plug=HDMI-A-0
fi
_preferred_monitor=${PREFERRED_DISPLAY:-${_connector_name_format}-${_preferred_monitor_index}}
_primary=""
if [[ $XDG_CURRENT_DESKTOP == "KDE" ]]; then
echo "KDE Mode"
outputs=$(kscreen-doctor -o | sed -r "s/\x1B\[([0-9]{1,3}(;[0-9]{1,2};?)?)?[mGK]//g")
# transform output into lines, remove disabled outputs, and sort by priority, then return the output to prefer by priority
p=$(echo "$outputs" | grep -v "Modes" | grep Output -A4 | xargs | sed 's/Output\:/\n/g' | grep -v disabled | awk '{print $6 " " $2}' | sed '/^\s*$/d' | sort | head -n1 | cut -d ' ' -f 2)
echo "p: $p"
_primary=$(echo "$outputs" | /bin/grep -B3 -A0 "$p" | grep Output: -B0 -A0 | cut -d ' ' -f 3)
unset p outputs
# TODO: Make this more readable
else
echo "non-KDE Mode"
# is our preferred display enabled?
if [[ "$_session_type" == "wayland" ]]; then
# NOTE: wlr-randr has no concept of primary monitor
# TODO: Make sure we don't disable the only present display
if [[ $(wlr-randr | grep -B5 'Enabled: yes' | grep "$_preferred_monitor" -c) -gt 0 ]]; then
wlr-randr --output "$_dummy_plug" --off
_primary=$_preferred_monitor
else
if wlr-randr | grep "$_dummy_plug" -A5 | grep 'Enabled: no'; then
wlr-randr --output "$_dummy_plug" --on
fi
_primary=$_dummy_plug
fi
else
# TODO: properly handle 'disconnected primary'. Not sure HOW this happens, but I'm staring at it right now.
_primary=$(xrandr | grep connected -w | awk '{print $1}' | head -n1)
fi
# echo "unsupported yet"
# notify-send "Unsupported environment" "Resolution not adjusted"
# return 0
fi
echo "Primary output: $_primary"
_height=${SUNSHINE_CLIENT_HEIGHT:-720}
_width=${SUNSHINE_CLIENT_WIDTH:-1280}
_refresh=${SUNSHINE_CLIENT_FPS:-60}
if [[ -n $_primary ]]; then
if [[ $1 == "do" ]]; then
# TODO: X11 support using xrandr
if [[ $XDG_CURRENT_DESKTOP == "KDE" ]]; then
# sh -c \"if pgrep plasmashell; then output=`~/scripts/get_primary_monitor`; kscreen-doctor output.${output}.vrrpolicy.never; kscreen-doctor.output.${output}.mode.2560x1440@120; fi\""}]
# TODO: revisit once KDE has fully native support without Xwayland
kscreen-doctor output."${_primary}".mode."${_width}"x"${_height}"@"${_refresh}"
kscreen-doctor output."${_primary}".vrrpolicy.never
elif [[ "$_session_type" == "wayland" ]]; then
wlr-randr --output "${_primary}" --mode "${_width}"x"${_height}"@"${_refresh}" --adaptive-sync disabled
else
xrandr --output "${_primary}" --mode "${_width}"x"${_height}" -r "${_refresh}"
fi
elif [[ $1 == "undo" ]]; then
# TODO: make disabling "$_dummy_plug" optional
if [[ $XDG_CURRENT_DESKTOP == "KDE" ]]; then
# TODO: Find preferred mode. it is available through command completion
kscreen-doctor output."${_primary}".mode.${_default_resolution}@${_default_refresh_rate}
kscreen-doctor output."${_primary}".vrrpolicy.${_default_vrr_mode}
kscreen-doctor output.${_dummy_plug}.disable
elif [[ "$_session_type" == "wayland" ]]; then
wlr-randr --output "${_primary}" --mode ${_default_resolution}@${_default_refresh_rate} --adaptive-sync ${_default_vrr_mode}
if [[ $_primary != "$_dummy_plug" ]]; then
wlr-randr --output "$_dummy_plug" --off
fi
else
xrandr --output "${_primary}" --mode ${_default_resolution} -r ${_default_refresh_rate}
if [[ $_primary != "$_dummy_plug" ]]; then
wlr-randr --output "$_dummy_plug" --off
fi
fi
fi
fi
unset _preferred_monitor _session_type _primary _height _width_refresh
# TODO: COSMIC
# TODO: GNOME
# TODO: Hyprland
# TODO: awesomewm, dwm, etc
# TODO: NVIDIA and Intel gpus support
# vim: ft=bash