-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import subprocess | ||
|
||
class Screen: | ||
resolutions = [] | ||
def __init__(self, index): | ||
self.index = index | ||
|
||
def add_resolution(self, resolution_str): | ||
if 'x' not in resolution_str: | ||
return | ||
|
||
xy = resolution_str.split('x') | ||
|
||
if len(xy) != 2: | ||
return | ||
|
||
self.resolutions.append(xy) | ||
|
||
|
||
def parse_xrandr(input): | ||
lines = input.split('\n') | ||
screens = [] | ||
screen = None | ||
index = 0 | ||
|
||
for line in lines: | ||
components = [] | ||
spaces = line.split(' ') | ||
|
||
for space in spaces: | ||
space = space.strip() | ||
if len(space) > 0: | ||
components.append(space) | ||
|
||
if len(components) < 2: | ||
continue | ||
|
||
if components[0] == 'Screen': | ||
screen = Screen(index) | ||
screens.append(screen) | ||
index += 1 | ||
|
||
screen.add_resolution(components[0]) | ||
|
||
return screens | ||
|
||
def xrandr(): | ||
cmd = ['/usr/bin/xrandr'] | ||
proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) | ||
o, e = proc.communicate() | ||
return (proc.returncode, o.decode('ascii'), e.decode('ascii')) | ||
|
||
result = xrandr() | ||
screens = parse_xrandr(result[1]) | ||
|
||
print(f"Number of monitors: {len(screens)}") | ||
for screen in screens: | ||
print(f"Screen #{screen.index}") | ||
for resolution in screen.resolutions: | ||
print(resolution) | ||
print('\n') | ||
|