如何获取当前的显示器分辨率或显示器名称(LVDS、VGA1 等)

Mer*_*ard 8 linux multiple-monitors resolution

我想获取当前显示器的分辨率(运行脚本的屏幕)或屏幕名称(LVDS、VGA1 等)。

如果我无法获得分辨率而只能获得显示器名称,我可以 grep 'xrandr -q' 输出以获得当前分辨率。

提前致谢。

ter*_*don 9

您应该能够通过组合来做到这一点xrandrxwininfo

  1. 获取屏幕、它们的分辨率和偏移量:

    $ xrandr | grep -w connected  | awk -F'[ \+]' '{print $1,$3,$4}'
    VGA-0 1440x900 1600
    DP-3 1600x900 0
    
    Run Code Online (Sandbox Code Playgroud)
  2. 获取当前窗口的位置

    $ xwininfo -id $(xdotool getactivewindow) | grep Absolute
     Absolute upper-left X:  1927
     Absolute upper-left Y:  70
    
    Run Code Online (Sandbox Code Playgroud)

因此,通过将两者结合起来,您应该能够获得当前屏幕的分辨率:

#!/usr/bin/env bash

## Get screen info
screen1=($(xrandr | grep -w connected  | awk -F'[ +]' '{print $1,$3,$4}' | 
    head -n 1))
screen2=($(xrandr | grep -w connected  | awk -F'[ +]' '{print $1,$3,$4}' | 
    tail -n 1))

## Figure out which screen is to the right of which
if [ ${screen1[2]} -eq 0  ]
then
    right=(${screen2[@]});
    left=(${screen1[@]});
else
    right=(${screen1[@]});
    left=(${screen2[@]});

fi

## Get window position
pos=$(xwininfo -id $(xdotool getactivewindow) | grep "Absolute upper-left X" | 
      awk '{print $NF}')

## Which screen is this window displayed in? If $pos
## is greater than the offset of the rightmost screen,
## then the window is on the right hand one
if [ "$pos" -gt "${right[2]}" ]
then
    echo "${right[0]} : ${right[1]}"    
else
    echo "${left[0]} : ${left[1]}"    
fi
Run Code Online (Sandbox Code Playgroud)

该脚本将打印当前屏幕的名称和分辨率。