2022-06-01 06:13:27 -04:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2022-06-02 11:44:02 -04:00
|
|
|
import argparse
|
2022-06-01 06:13:27 -04:00
|
|
|
import enum
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
|
2022-06-03 13:00:30 -04:00
|
|
|
from gi.repository import GLib, Gio
|
2022-06-01 06:13:27 -04:00
|
|
|
|
|
|
|
|
2022-06-03 13:00:30 -04:00
|
|
|
NAME = 'org.gnome.Mutter.DisplayConfig'
|
|
|
|
INTERFACE = 'org.gnome.Mutter.DisplayConfig'
|
|
|
|
OBJECT_PATH = '/org/gnome/Mutter/DisplayConfig'
|
2022-06-01 06:13:27 -04:00
|
|
|
|
2022-06-03 12:58:38 -04:00
|
|
|
TRANSFORM_STRINGS = {
|
|
|
|
0: 'normal',
|
|
|
|
1: '90',
|
|
|
|
2: '180',
|
|
|
|
3: '270',
|
|
|
|
4: 'flipped',
|
|
|
|
5: 'flipped-90',
|
|
|
|
6: 'flipped-180',
|
|
|
|
7: 'flipped-270',
|
|
|
|
}
|
2022-06-01 06:13:27 -04:00
|
|
|
|
2022-06-03 13:00:30 -04:00
|
|
|
|
|
|
|
class Source(enum.Enum):
|
|
|
|
DBUS = 1
|
|
|
|
COMMAND_LINE = 2
|
|
|
|
FILE = 3
|
|
|
|
|
|
|
|
|
|
|
|
class MonitorConfig:
|
|
|
|
CONFIG_VARIANT_TYPE = GLib.VariantType.new(
|
|
|
|
'(ua((ssss)a(siiddada{sv})a{sv})a(iiduba(ssss)a{sv})a{sv})')
|
|
|
|
|
|
|
|
def get_current_state(self) -> GLib.Variant:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2022-06-03 13:08:29 -04:00
|
|
|
def parse_data(self):
|
|
|
|
"""TODO: add data parser so that can be used for reconfiguring"""
|
|
|
|
|
2022-06-27 05:13:55 -04:00
|
|
|
def print_data(self, *, level, is_last, lines, data):
|
2022-06-03 13:08:29 -04:00
|
|
|
if is_last:
|
|
|
|
link = '└'
|
|
|
|
else:
|
|
|
|
link = '├'
|
|
|
|
padding = ' '
|
|
|
|
|
|
|
|
if level >= 0:
|
|
|
|
indent = level
|
|
|
|
buffer = f'{link:{padding}>{indent * 4}}──{data}'
|
|
|
|
buffer = list(buffer)
|
|
|
|
for line in lines:
|
|
|
|
if line == level:
|
|
|
|
continue
|
|
|
|
index = line * 4
|
|
|
|
if line > 0:
|
|
|
|
index -= 1
|
|
|
|
buffer[index] = '│'
|
|
|
|
buffer = ''.join(buffer)
|
|
|
|
else:
|
|
|
|
buffer = data
|
|
|
|
|
|
|
|
print(buffer)
|
|
|
|
|
|
|
|
if is_last and level in lines:
|
|
|
|
lines.remove(level)
|
|
|
|
elif not is_last and level not in lines:
|
|
|
|
lines.append(level)
|
|
|
|
|
2022-06-27 05:13:55 -04:00
|
|
|
def print_properties(self, *, level, lines, properties):
|
2022-06-03 13:08:29 -04:00
|
|
|
property_list = list(properties)
|
|
|
|
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=level, is_last=True, lines=lines,
|
|
|
|
data=f'Properties: ({len(property_list)})')
|
2022-06-03 13:08:29 -04:00
|
|
|
for property in property_list:
|
|
|
|
is_last = property == property_list[-1]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=level + 1, is_last=is_last, lines=lines,
|
|
|
|
data=f'{property} ⇒ {properties[property]}')
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
def print_current_state(self, short):
|
|
|
|
variant = self.get_current_state()
|
|
|
|
|
|
|
|
print('Serial: {}'.format(variant[0]))
|
|
|
|
print()
|
|
|
|
print('Monitors:')
|
|
|
|
monitors = variant[1]
|
|
|
|
lines = []
|
|
|
|
for monitor in monitors:
|
|
|
|
is_last = monitor == monitors[-1]
|
|
|
|
spec = monitor[0]
|
|
|
|
modes = monitor[1]
|
|
|
|
properties = monitor[2]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=0, is_last=is_last, lines=lines,
|
|
|
|
data='Monitor {}'.format(spec[0]))
|
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'EDID: vendor: {spec[1]}, product: {spec[2]}, serial: {spec[3]}')
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
mode_count = len(modes)
|
|
|
|
if short:
|
|
|
|
modes = [mode for mode in modes if len(mode[6]) > 0]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Modes ({len(modes)}, {mode_count - len(modes)} omitted)')
|
2022-06-03 13:08:29 -04:00
|
|
|
else:
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Modes ({len(modes)})')
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
for mode in modes:
|
|
|
|
is_last = mode == modes[-1]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=2, is_last=is_last, lines=lines,
|
|
|
|
data=f'{mode[0]}')
|
|
|
|
self.print_data(level=3, is_last=False, lines=lines,
|
|
|
|
data=f'Dimension: {mode[1]}x{mode[2]}')
|
|
|
|
self.print_data(level=3, is_last=False, lines=lines,
|
2022-08-17 04:02:04 -04:00
|
|
|
data=f'Refresh rate: {mode[3]:.3f}')
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=3, is_last=False, lines=lines,
|
|
|
|
data=f'Preferred scale: {mode[4]}')
|
|
|
|
self.print_data(level=3, is_last=False, lines=lines,
|
|
|
|
data=f'Supported scales: {mode[5]}')
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
mode_properties = mode[6]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_properties(level=3, lines=lines,
|
|
|
|
properties=mode_properties)
|
2022-06-03 13:08:29 -04:00
|
|
|
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_properties(level=1, lines=lines, properties=properties)
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
print()
|
|
|
|
print('Logical monitors:')
|
|
|
|
logical_monitors = variant[2]
|
|
|
|
index = 1
|
|
|
|
for logical_monitor in logical_monitors:
|
|
|
|
is_last = logical_monitor == logical_monitors[-1]
|
|
|
|
properties = logical_monitor[2]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=0, is_last=is_last, lines=lines,
|
|
|
|
data=f'Logical monitor #{index}')
|
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Position: ({logical_monitor[0]}, {logical_monitor[1]})')
|
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Scale: {logical_monitor[2]}')
|
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Transform: {TRANSFORM_STRINGS.get(logical_monitor[3])}')
|
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Primary: {logical_monitor[4]}')
|
2022-06-03 13:08:29 -04:00
|
|
|
monitors = logical_monitor[5]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=1, is_last=False, lines=lines,
|
|
|
|
data=f'Monitors: ({len(monitors)})')
|
2022-06-03 13:08:29 -04:00
|
|
|
for monitor in monitors:
|
|
|
|
is_last = monitor == monitors[-1]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_data(level=2, is_last=is_last, lines=lines,
|
|
|
|
data=f'{monitor[0]} ({monitor[1]}, {monitor[2]}, {monitor[3]})')
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
properties = logical_monitor[6]
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_properties(level=1, lines=lines, properties=properties)
|
2022-06-03 13:08:29 -04:00
|
|
|
|
|
|
|
index += 1
|
|
|
|
|
|
|
|
properties = variant[3]
|
|
|
|
print()
|
2022-06-27 05:13:55 -04:00
|
|
|
self.print_properties(level=-1, lines=lines, properties=properties)
|
2022-06-03 13:08:29 -04:00
|
|
|
|
2022-06-03 13:00:30 -04:00
|
|
|
|
|
|
|
class MonitorConfigDBus(MonitorConfig):
|
|
|
|
def __init__(self):
|
|
|
|
self._proxy = Gio.DBusProxy.new_for_bus_sync(
|
|
|
|
bus_type=Gio.BusType.SESSION,
|
|
|
|
flags=Gio.DBusProxyFlags.NONE,
|
|
|
|
info=None,
|
|
|
|
name=NAME,
|
|
|
|
object_path=OBJECT_PATH,
|
|
|
|
interface_name=INTERFACE,
|
|
|
|
cancellable=None,
|
|
|
|
)
|
|
|
|
|
|
|
|
def get_current_state(self) -> GLib.Variant:
|
|
|
|
variant = self._proxy.call_sync(
|
|
|
|
method_name='GetCurrentState',
|
|
|
|
parameters=None,
|
|
|
|
flags=Gio.DBusCallFlags.NO_AUTO_START,
|
|
|
|
timeout_msec=-1,
|
|
|
|
cancellable=None
|
|
|
|
)
|
|
|
|
assert variant.get_type().equal(self.CONFIG_VARIANT_TYPE)
|
|
|
|
return variant
|
|
|
|
|
|
|
|
|
|
|
|
class MonitorConfigCommandLine(MonitorConfig):
|
|
|
|
def get_current_state(self) -> GLib.Variant:
|
|
|
|
command = ('gdbus call -e '
|
|
|
|
f'-d {NAME} '
|
|
|
|
f'-o {OBJECT_PATH} '
|
|
|
|
f'-m {INTERFACE}.GetCurrentState')
|
|
|
|
|
|
|
|
result = subprocess.run(command, shell=True,
|
|
|
|
check=True, capture_output=True, text=True)
|
|
|
|
return GLib.variant_parse(self.CONFIG_VARIANT_TYPE, result.stdout)
|
|
|
|
|
|
|
|
|
|
|
|
class MonitorConfigFile(MonitorConfig):
|
|
|
|
def __init__(self, file_path):
|
|
|
|
if file_path == '-':
|
|
|
|
self._data = sys.stdin.read()
|
|
|
|
else:
|
|
|
|
with open(file_path) as file:
|
|
|
|
self._data = file.read()
|
|
|
|
|
|
|
|
def get_current_state(self) -> GLib.Variant:
|
|
|
|
return GLib.variant_parse(self.CONFIG_VARIANT_TYPE, self._data)
|
|
|
|
|
|
|
|
|
2022-06-03 12:57:41 -04:00
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(description='Get display state')
|
2022-06-03 13:00:30 -04:00
|
|
|
parser.add_argument('--file', metavar='FILE', type=str, nargs='?',
|
2022-06-03 12:57:41 -04:00
|
|
|
help='Read the output from gdbus call instead of calling D-Bus')
|
2022-06-03 13:00:30 -04:00
|
|
|
parser.add_argument('--gdbus', action='store_true')
|
2022-06-03 12:57:41 -04:00
|
|
|
parser.add_argument('--short', action='store_true')
|
2022-06-01 06:13:27 -04:00
|
|
|
|
2022-06-03 12:57:41 -04:00
|
|
|
args = parser.parse_args()
|
2022-06-01 06:13:27 -04:00
|
|
|
|
2022-06-03 13:00:30 -04:00
|
|
|
if args.file and args.gdbus:
|
|
|
|
raise argparse.ArgumentTypeError('Incompatible arguments')
|
|
|
|
|
|
|
|
if args.file:
|
|
|
|
monitor_config = MonitorConfigFile(args.file)
|
|
|
|
elif args.gdbus:
|
|
|
|
monitor_config = MonitorConfigCommandLine()
|
|
|
|
else:
|
|
|
|
monitor_config = MonitorConfigDBus()
|
|
|
|
|
2022-06-03 13:08:29 -04:00
|
|
|
monitor_config.print_current_state(short=args.short)
|