How to get the current monitor resolution or monitor name (LVDS, VGA1, etc)
You should be able to do this by a combination of xrandr
and xwininfo
.
Get the screens, their resolutions and offsets:
$ xrandr | grep -w connected | awk -F'[ \+]' '{print $1,$3,$4}' VGA-0 1440x900 1600 DP-3 1600x900 0
Get the position of the current window
$ xwininfo -id $(xdotool getactivewindow) | grep Absolute Absolute upper-left X: 1927 Absolute upper-left Y: 70
So, by combining the two you should be able to get the resolution of the current screen:
#!/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
The script will print the name and resolution of the current screen.