xrandr

16 篇文章 3 订阅

xrandr

xrandr 是用于与 X RandR 扩展进行交互的命令行工具。它允许对 X server 进行实时配置(即无需重新启动即可使配置生效),xrandr 提供了显示模式(如分辨率、刷新率等)的自动检测,以及动态配置输出(如调整大小、旋转、移动屏幕等)的功能。

作者:炭烤毛蛋 ,点击博主了解更多。


提示:xrandr 需要系统有正常显示才可查看。


前期准备

ubuntu 安装 xrandr 命令,xrandr 安装依赖 x11-xserver-utils。

sudo apt install x11-xserver-utils

1. xrandr 测试

1.1 分辨率调整

  • 查看分辨率
Screen 0: minimum 320 x 200, current 1920 x 1080, maximum 16384 x 16384
HDMI-1 connected primary 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
   1920x1080     60.00*+  74.97    50.00    59.94
   1680x1050     59.88
   1280x1024     75.02    60.02
   1440x900      59.90
   1280x960      60.00
   1280x720      60.00    50.00    59.94
   1024x768      75.03    70.07    60.00
   832x624       74.55
   800x600       72.19    75.00    60.32    56.25
   720x576       50.00
   720x480       60.00    59.94
   640x480       75.00    72.81    66.67    60.00    59.94
   720x400       70.08
HDMI-2 disconnected (normal left inverted right x axis y axis)
  • 设置具体分辨率
xrandr -s 1024x768
  • 恢复到原来的分辨率
xrandr -s 0
  • 旋转屏幕
xrandr -o left     # 向左旋转90度
xrandr -o right    # 向右旋转90度
xrandr -o inverted # 上下翻转
xrandr -o normal   # 回到正常角度

1.2 指定屏分辨率设定

  • 设置分辨率(HDMI-1)
xrandr --output HDMI-1 --mode 1920x1080
  • 设置分辨率和刷新率(HDMI-1)
xrandr --output HDMI-1 --mode 1920x1080 --rate 60
  • 改为自动模式
xrandr --output HDMI-1 --auto
  • 关闭显示1,开启显示2
xrandr --output HDMI-1 --off --output HDMI-2 --auto

1.3 xrandr 命令详情

# xrandr --help
usage: xrandr [options]
  where options are:
  --display <display> or -d <display>
  --help
  -o <normal,inverted,left,right,0,1,2,3>
            or --orientation <normal,inverted,left,right,0,1,2,3>
  -q        or --query
  -s <size>/<width>x<height> or --size <size>/<width>x<height>
  -r <rate> or --rate <rate> or --refresh <rate>
  -v        or --version
  -x        (reflect in x)
  -y        (reflect in y)
  --screen <screen>
  --verbose
  --current
  --dryrun
  --nograb
  --prop or --properties
  --fb <width>x<height>
  --fbmm <width>x<height>
  --dpi <dpi>/<output>
  --output <output>
      --auto
      --mode <mode>
      --preferred
      --pos <x>x<y>
      --rate <rate> or --refresh <rate>
      --reflect normal,x,y,xy
      --rotate normal,inverted,left,right
      --left-of <output>
      --right-of <output>
      --above <output>
      --below <output>
      --same-as <output>
      --set <property> <value>
      --scale <x>x<y>
      --scale-from <w>x<h>
      --transform <a>,<b>,<c>,<d>,<e>,<f>,<g>,<h>,<i>
      --off
      --crtc <crtc>
      --panning <w>x<h>[+<x>+<y>[/<track:w>x<h>+<x>+<y>[/<border:l>/<t>/<r>/<b>]]]
      --gamma <r>:<g>:<b>
      --brightness <value>
      --primary
  --noprimary
  --newmode <name> <clock MHz>
            <hdisp> <hsync-start> <hsync-end> <htotal>
            <vdisp> <vsync-start> <vsync-end> <vtotal>
            [flags...]
            Valid flags: +HSync -HSync +VSync -VSync
                         +CSync -CSync CSync Interlace DoubleScan
  --rmmode <name>
  --addmode <output> <name>
  --delmode <output> <name>
  --listproviders
  --setprovideroutputsource <prov-xid> <source-xid>
  --setprovideroffloadsink <prov-xid> <sink-xid>
  --listmonitors
  --listactivemonitors
  --setmonitor <name> {auto|<w>/<mmw>x<h>/<mmh>+<x>+<y>} {none|<output>,<output>,...}
  --delmonitor <name>

2 显示设置

2.1 交换内外屏

#!/bin/sh
intern=LVDS1
extern=VGA1

if xrandr | grep "$extern disconnected"; then
    xrandr --output "$extern" --off --output "$intern" --auto
else
    xrandr --output "$intern" --off --output "$extern" --auto
    # 接入外平时更换默认配置可以采用如下参数
    # xrandr --output "$intern" --primary --auto --output "$extern" --right-of "$intern" --auto
fi

2.2 规避显示 crash问题

  • 前置重新连接
    休眠唤醒时部分设备掉线,唤醒后可能发生crash问题。可以通过多次查询显示信息规避crash问题。
declare -i count=2
declare -i seconds=1

while ((count)); do
    xrandr >/dev/null
    sleep $seconds
    ((count--))
done

当然也可以强制关闭开机某些屏幕

xrasengan --force -on DisplayPort-0 -off HDMI-0

  • 重新加载配置
xrasengan --try-reload-active-layout

3. 添加为检测到分辨率

3.1 xrandr 模式设定

显示器有效的分辨率 xrandr 可能未能全部检测 ,可以通过 xrandr 里为显示器添加所需的分辨率。

  • 添加未支持的分辨率
xrandr --newmode '1280x720_60' 60.00 1280  1304 1340 1500 720 723 729 758 -hsync -vsync
  • 该分辨率添加到当前的输出设备(将其命名为 DisplayPort-1)
xrandr --addmode DisplayPort-1 1280x720_60
  • DisplayPort-1 的分辨率设定为刚刚添加模式
xrandr --output  DisplayPort-1 --mode 1280x720_60
  • 删除 DisplayPort-1 xrandr 模式
xrandr --rmmode 1280x720_60

归档单一表达式

xrandr --newmode '1280x720_60' 60.00 1280  1304 1340 1500 720 723 729 758 -hsync -vsync && sleep 5 && xrandr --addmode DisplayPort-1 1280x720_60 && xrandr --output  DisplayPort-1 --mode 1280x720_60

结语

不枉博主详细讲解,欢迎订阅博主–炭烤毛蛋

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值