Come ottenere la risoluzione o il nome del monitor corrente (LVDS, VGA1, ecc.)


4

Vorrei ottenere la risoluzione del monitor corrente (lo schermo da cui eseguo lo script) o il nome dello schermo (LVDS, VGA1, ecc.).

Se non riesco a ottenere la risoluzione ma solo il nome del monitor, potrei visualizzare l'output 'xrandr -q' per ottenere la risoluzione corrente.

Grazie in anticipo.

Risposte:


4

Dovresti essere in grado di farlo tramite una combinazione di xrandre xwininfo.

  1. Ottieni gli schermi, le loro risoluzioni e offset:

    $ xrandr | grep -w connected  | awk -F'[ \+]' '{print $1,$3,$4}'
    VGA-0 1440x900 1600
    DP-3 1600x900 0
    
  2. Ottieni la posizione della finestra corrente

    $ xwininfo -id $(xdotool getactivewindow) | grep Absolute
     Absolute upper-left X:  1927
     Absolute upper-left Y:  70
    

Quindi, combinando i due dovresti essere in grado di ottenere la risoluzione della schermata corrente:

#!/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

Lo script stamperà il nome e la risoluzione della schermata corrente.


1
Fantastico grazie! Non ero a conoscenza di questo xdotool. Il mio gestore delle finestre sfortunatamente non supporta 'getactivewindow' ma la eval $(xdotool getmouselocation --shell)volontà farà il trucco. Grazie ancora.
— Merlin Gaillard,

Se xrandridentifica una delle schermate come principale, questa estrarrà l' awkestrazione del campo. L'inserimento di un sedcomando per eliminare l'etichetta primaria consentirà di risolvere questo problema:xrandr | grep -w connected | sed 's/primary //' | awk -F'[ +]' '{print $1,$3,$4}'
— mapeters

1

Ho modificato la soluzione (eccellente) di @ terdon in modo che funzioni con qualsiasi numero di monitor impilati in orizzontale e / o in verticale, e ho cambiato il modo in cui gli offset sono catturati da xrandr (non funzionava sulla mia configurazione, forse causato da una modifica nel formato di output xrandr).

#!/usr/bin/env bash

OFFSET_RE="\+([-0-9]+)\+([-0-9]+)"

# Get the window position
pos=($(xwininfo -id $(xdotool getactivewindow) | 
  sed -nr "s/^.*geometry .*$OFFSET_RE.*$/\1 \2/p"))

# Loop through each screen and compare the offset with the window
# coordinates.
while read name width height xoff yoff
do
  if [ "${pos[0]}" -ge "$xoff" \
    -a "${pos[1]}" -ge "$yoff" \
    -a "${pos[0]}" -lt "$(($xoff+$width))" \
    -a "${pos[1]}" -lt "$(($yoff+$height))" ]
  then
    monitor=$name   
  fi
done < <(xrandr | grep -w connected |
  sed -r "s/^([^ ]*).*\b([-0-9]+)x([-0-9]+)$OFFSET_RE.*$/\1 \2 \3 \4 \5/" |
  sort -nk4,5)

# If we found a monitor, echo it out, otherwise print an error.
if [ ! -z "$monitor" ]
then
  echo $monitor
  exit 0
else
  echo "Couldn't find any monitor for the current window." >&2
  exit 1
fi

Vale anche la pena notare che xdotoolpuò emettere lo schermo su cui si trova una finestra, ma, se si utilizza Xinerama, che fa apparire tutti i monitor come uno schermo grande, questo produrrà sempre e solo uno 0.


0

Per qualche motivo non sono riuscito a ottenere la risposta di @ adam-bowen lavorando con un gestore di finestre di affiancamento, ma alcune modifiche minori per usare le coordinate del mouse hanno funzionato.

#!/usr/bin/env bash
#
# Print's the current screen index (zero based).
#
# Modified from:
# https://superuser.com/a/992924/240907

OFFSET_RE="\+([-0-9]+)\+([-0-9]+)"

# Get the window position
eval "$(xdotool getmouselocation --shell)"

# Loop through each screen and compare the offset with the window
# coordinates.
monitor_index=0
while read name width height xoff yoff
do
    if [ "${X}" -ge "$xoff" \
      -a "${Y}" -ge "$yoff" \
      -a "${X}" -lt "$(($xoff+$width))" \
      -a "${Y}" -lt "$(($yoff+$height))" ]
    then
        monitor=$name
        break
    fi
    ((monitor_index++))
done < <(xrandr | grep -w connected |
    sed -r "s/^([^ ]*).*\b([-0-9]+)x([-0-9]+)$OFFSET_RE.*$/\1 \2 \3 \4 \5/" |
    sort -nk4,5)

# If we found a monitor, echo it out, otherwise print an error.
if [ ! -z "$monitor" ]
then
    # echo $monitor
    echo $monitor_index
    exit 0
else
    echo "Couldn't find any monitor for the current window." >&2
    exit 1
fi
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.