剧本

剧本

我在使用 Ubuntu 18.04 时遇到了一个非常烦人的问题。我的笔记本电脑有一个 4k 15 英寸显示屏。由于这会使所有东西都变得非常小,因此我更喜欢在使用时将其缩小到 1080x1920。在工作中,我有一个双显示器设置,两个显示器都连接到一个小型底座/适配器,我使用雷电 3 将其连接到我的机器。在我的 Nvidia X 服务器设置中,我已将双屏幕配置为彼此相邻,并且笔记本电脑屏幕已关闭。现在来谈谈问题。

每当我断开这些显示器的连接时,我的笔记本电脑屏幕就会重新打开,并显示全 4k 分辨率。如果我进入 Nvidia X Server,我可以将其再次更改为 1080。但是下次我再次连接到双显示器时,设置就完全错误了,我必须花 5 分钟重新启动并重新正确配置所有内容。

有没有合适的方法可以解决这个问题?有没有办法自动更改显示配置,或者使用热键或其他方法?

我正在使用所有最新更新和 Nvidia 驱动程序 430。

以下是运行 xrandr 的输出

//Only on laptop screen(no external screen connected)

Screen 0: minimum 8 x 8, current 3840 x 2160, maximum 16384 x 16384
DP-0 disconnected (normal left inverted right x axis y axis)
DP-1 disconnected (normal left inverted right x axis y axis)
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 connected primary 3840x2160+0+0 (normal left inverted right x axis y axis) 346mm x 194mm
   3840x2160     60.00*+  50.00    48.02  
DP-4 disconnected (normal left inverted right x axis y axis)
DP-4.1 disconnected (normal left inverted right x axis y axis)
DP-4.2 disconnected (normal left inverted right x axis y axis)


//When Its connected to my dock, laptop screen disabled and two 1080 monitors. 

Screen 0: minimum 8 x 8, current 3840 x 1080, maximum 16384 x 16384
DP-0 disconnected (normal left inverted right x axis y axis)
DP-1 disconnected (normal left inverted right x axis y axis)
DP-2 disconnected (normal left inverted right x axis y axis)
DP-3 connected (normal left inverted right x axis y axis)
   3840x2160     60.00 +  50.00    48.02  
DP-4 disconnected (normal left inverted right x axis y axis)
DP-4.1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080     60.00*+  75.00    59.94    50.00  
   1680x1050     59.95  
   1600x1200     60.00  
   1600x900      60.00  
   1440x900      59.89  
   1280x1024     75.02    60.02  
   1280x800      59.81  
   1280x720      60.00    59.94    50.00  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       75.00    59.94    59.93  
DP-4.2 connected 1920x1080+1920+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080     60.00*+  75.00    59.94    50.00  
   1680x1050     59.95  
   1600x1200     60.00  
   1600x900      60.00  
   1440x900      59.89  
   1280x1024     75.02    60.02  
   1280x800      59.81  
   1280x720      60.00    59.94    50.00  
   1024x768      75.03    60.00  
   800x600       75.00    60.32  
   720x576       50.00  
   720x480       59.94  
   640x480       75.00    59.94    59.93  

答案1

这听起来像是您需要一个脚本,该脚本使用您喜欢的显示模式调用 xrandr。然后您可以将该脚本映射到热键。

剧本

我遇到了非常类似的问题,经过长时间的研究我发现这个答案梅斯特里狮。在其中他们引用了可以在 github 上找到的自制脚本。出于存档原因,现将其复制如下。

您可以将该脚本复制粘贴到名为“switch-monitor”的文件中,并激活可执行权限。

假设文件位于,/home/<username>您可以通过键入以下内容来运行脚本:

/home/<username>/switch-monitor --list

这将为您提供已连接监视器的列表并创建一个配置文件/home/.config/switch-monitor.conf

#!/bin/bash
#
# monitor-switch - switch outputs using xrand
#
#    Copyright (C) 2012 Rodrigo Silva (MestreLion) <[email protected]>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. See <http://www.gnu.org/licenses/gpl.html>

declare -A monitor_opts
declare -a monitors

myname="${0##*/}"
verbose=0

# Read settings from config file
config=${XDG_CONFIG_HOME:-"$HOME"/.config}/"$myname".conf
if [[ -f "$config" ]]; then
    source "$config"
fi

print_monitors() {
    while read -r output conn hex; do
        echo "# $output $conn   $(xxd -r -p <<<"$hex")"
    done < <(xrandr --prop | awk '
    !/^[ \t]/ {
        if (output && hex) print output, conn, hex
        output=$1
        hex=""
    }
    /ConnectorType:/ {conn=$2}
    /[:.]/ && h {
        sub(/.*000000fc00/, "", hex)
        hex = substr(hex, 0, 26) "0a"
        sub(/0a.*/, "", hex)
        h=0
    }
    h {sub(/[ \t]+/, ""); hex = hex $0}
    /EDID.*:/ {h=1}')
}

# if there's no pre-defined monitors list, read from xrandr
# and save them to config file
if [[ -z "$monitors" ]]; then
    while read -r output ; do
        monitors+=("$output")
    done < <(xrandr | awk '$2 ~/^c/{print $1}' | sort)
    cat > "$config" <<-EOF
        # $myname config file

        # List of monitors, from left to right. Edit to your actual layout
        monitors=(${monitors[@]})

        # Extra xrandr options for each monitor.
        # Useful when EDID data does not reflect actual preferred mode
        # Options for non-existing outputs (such as the examples below) are ignored
        # Examples:
        monitor_opts[DFPx]="--mode 1920x1080 --rate 60"
        monitor_opts[DFPy]="--mode 1280x720"

        # As a reference, these were the connected monitors when this config file was created
        # use it as a guide when editing the above monitors list and extra options
        $(print_monitors)

        # For an updated list, run $myname --list
    EOF
fi

message() { printf "%s\n" "$1" >&2 ; }
fatal()   { [[ "$1" ]] && message "$myname: error: $1" ; exit ${2:-1} ; }
argerr()  { printf "%s: %s\n" "$myname" "${1:-error}" >&2 ; usage 1 ; }
invalid() { argerr "invalid argument: $1" ; }
missing() { argerr "missing ${2:+$2 }operand${1:+ from $1}." ; }

usage() {
    cat <<-USAGE
    Usage: $myname [options]
    USAGE
    if [[ "$1" ]] ; then
        cat >&2 <<- USAGE
        Try '$myname --help' for more information.
        USAGE
        exit 1
    fi
    cat <<-USAGE

    Switch monitors using xrandr.

    Options:
      -h|--help          - show this page.
      -v|--verbose       - print in terminal the full xrandr command executed.

      -l|--list          - list connector and monitor names of connected outputs

      -a|--all           - enable all monitors.
      -s|--select OUTPUT - enable monitor OUTPUT, disable all others.
      -l|--left          - enable leftmost monitor.  Alias for --select ${monitors[0]}
      -r|--right         - enable rightmost monitor. Alias for --select ${monitors[${#monitors[@]}-1]}

    Copyright (C) 2012 Rodrigo Silva (MestreLion) <[email protected]>
    License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
    USAGE
    exit 0
}

# Option handling
for arg in "$@"; do [[ "$arg" == "-h" || "$arg" == "--help" ]] && usage ; done
while (( $# )); do
    case "$1" in
    -v|--verbose) verbose=1 ;;
    -q|--no-notify) notify=0 ;;
    -l|--list) list=1 ;;
    -a|--all) all=1 ;;
    -s|--select) shift ; monitor="$1" ;;
    -l|--left ) monitor="${monitors[0]}" ;;
    -r|--right) monitor="${monitors[${#monitors[@]}-1]}" ;;
    *) invalid "$1" ;;
    esac
    shift
done

if ((list)); then
    echo "Connected monitors:"
    print_monitors
    exit
fi

if [[ -z "$monitor" && -z "$all" ]]; then
    usage
fi

# Loop outputs (monitors)
for output in "${monitors[@]}"; do
    if ((all)) || [[ "$output" = "$monitor" ]]; then
        xrandropts+=(--output "$output" --auto ${monitor_opts["$output"]})
        if ((all)); then
            if [[ "$output" = "${monitors[0]}" ]]; then
                xrandropts+=(--pos 0x0 --primary)
            else
                xrandropts+=(--right-of "$previous")
            fi
            previous="$output"
        else
            xrandropts+=(--primary)
        fi
    else
        xrandropts+=(--output "$output" --off)
    fi
done

((verbose)) && message "$myname: executing xrandr ${xrandropts[*]}"
xrandr "${xrandropts[@]}"

配置文件

打开上面创建的配置文件/home/.config/switch-monitor.conf

# List of monitors, from left to right. Edit to your actual layout
monitors=(<list of monitors>)

# Extra xrandr options for each monitor.
# Useful when EDID data does not reflect actual preferred mode
# Options for non-existing outputs (such as the examples below) are ignored
# Examples:
monitor_opts[DFPx]="--mode 1920x1080 --rate 60"
monitor_opts[DFPy]="--mode 1280x720"

在其中,您可以更改显示器的物理布局以反映您的设置,并且可以定义所需的模式。因此,对于您的情况,您应该像这样更改它:

# List of monitors, from left to right. Edit to your actual layout
monitors=(DP-4.1 DP-4.2 DP-3)

# Extra xrandr options for each monitor.
# Useful when EDID data does not reflect actual preferred mode
# Options for non-existing outputs (such as the examples below) are ignored
# Examples:
monitor_opts[DP-4.1]="--primary --mode 1920x1080 --rate 60"
monitor_opts[DP-4.2]="--mode 1280x720 --rate 60"
monitor_opts[DP-3]="--mode 1920x1080 --rate 60"

热键

只需关注此程序添加快捷方式。command您应该在框中注册:

/home/<username>/switch-monitor --all

这会将所有监视器与配置文件中注册的给定选项连接起来。

希望这可以帮助 :)

相关内容