How can I list connected monitor(s) with xrandr?

I am not sure how you are going to apply it in your application ("enable a user to have their desired resolution without requiring graphics drivers" ?), but:

A terminal command to list connected screens

xrandr | grep " connected " | awk '{ print$1 }'

This wil give you the connected screens for further processing, like:

VGA-0
DVI-I-1

Since you mention python, the snippet below will also list connected screens:

#!/usr/bin/env python3
import subprocess

def screens():
    output = [l for l in subprocess.check_output(["xrandr"]).decode("utf-8").splitlines()]
    return [l.split()[0] for l in output if " connected " in l]

print(screens())

This wil also give you the connected screens, like:

['VGA-0', 'DVI-I-1']

Note

Note the spaces around " connected " in the searched string. They are needed to prevent mismatches with disconnected.

EDIT 2019

Using python, not necessary to use xrandr or any other system call at all. Better use Gdk:

#!/usr/bin/env python3
import gi
gi.require_version("Gdk", "3.0")
from gi.repository import Gdk

allmonitors = []

gdkdsp = Gdk.Display.get_default()
for i in range(gdkdsp.get_n_monitors()):
    monitor = gdkdsp.get_monitor(i)
    scale = monitor.get_scale_factor()
    geo = monitor.get_geometry()
    allmonitors.append([
        monitor.get_model()] + [n * scale for n in [
            geo.x, geo.y, geo.width, geo.height
        ]
    ])

print(allmonitors)

Example output:

[['eDP-1', 0, 0, 3840, 2160], ['DP-2', 3840, 562, 1680, 1050]]

Depending on the desired info, you can make your choice from https://lazka.github.io/pgi-docs/Gdk-3.0/classes/Monitor.html


You can use the bash command with popen:

import os
list_display = os.popen("xrandr --listmonitors | grep '*' | awk {'print $4'}").read().splitlines()
# or based on the comment of this answer 
list_display = os.popen("xrandr --listmonitors | grep '+' | awk {'print $4'}").read().splitlines()

or I wrote a old gist on the subject https://gist.github.com/antoinebou13/7a212ccd84cc95e040b2dd0e14662445


You can use python and just python to get the connected monitor names:

$ python3 -c 'from gi.repository import Gdk; screen=Gdk.Screen.get_default(); \
[print(screen.get_monitor_plug_name(i)) for i in range(screen.get_n_monitors())]'
DP1
LVDS1