Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

分享:Linux下使用xrandr一键切换显示器 #58

Open
woodongwong opened this issue May 1, 2021 · 0 comments
Open

分享:Linux下使用xrandr一键切换显示器 #58

woodongwong opened this issue May 1, 2021 · 0 comments
Labels

Comments

@woodongwong
Copy link
Owner

woodongwong commented May 1, 2021

效果图:

output

链接:https://wiki.archlinux.org/index.php/xrandr#Example_3

下面是原文中的代码,将此脚本设置成快捷键,便可实现一键切换显示器。
xrandr好像只支持X11,不支持Wayland

#!/bin/bash

# get info from xrandr
connectedOutputs=$(xrandr | awk '$2 == "connected"{print $1}')
activeOutput=$(xrandr | grep -E " connected (primary )?[1-9]+" | sed -e "s/\([A-Z0-9]\+\) connected.*/\1/")

# initialize variables
execute="xrandr "
default="xrandr "
i=1
switch=0

for display in $connectedOutputs
do
    # build default configuration
    if [ $i -eq 1 ]
    then
        default=$default"--output $display --auto "
    else
        default=$default"--output $display --off "
    fi

    # build "switching" configuration
    if [ $switch -eq 1 ]
    then
        execute=$execute"--output $display --auto "
        switch=0
    else
        execute=$execute"--output $display --off "
    fi

    # check whether the next output should be switched on
    if [ $display = $activeOutput ]
    then
        switch=1
    fi

    i=$(( $i + 1 ))
done

# check if the default setup needs to be executed then run it
if ! (echo "$execute" | grep -q "auto")
then
    `$default`
else
    `$execute`
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant