From 40509154fb5d5e9b566fba47f70fbe76a381f678 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20Trevisan=20=28Trevi=C3=B1o=29?= Date: Fri, 3 Jun 2022 18:57:41 +0200 Subject: [PATCH] get-state: Move code into __main__ functions Part-of: --- tools/get-state.py | 192 +++++++++++++++++++++++---------------------- 1 file changed, 99 insertions(+), 93 deletions(-) diff --git a/tools/get-state.py b/tools/get-state.py index b721e3d4e..de0dfbf89 100755 --- a/tools/get-state.py +++ b/tools/get-state.py @@ -13,41 +13,6 @@ class Source(enum.Enum): FILE = 2 -parser = argparse.ArgumentParser(description='Get display state') -parser.add_argument('file', metavar='FILE', type=str, nargs='?', - help='Read the output from gdbus call instead of calling D-Bus') -parser.add_argument('--short', action='store_true') - -args = parser.parse_args() - -if args.file: - source = Source.FILE - path = args.file -else: - source = Source.DBUS - -short = args.short - -type_signature = '(ua((ssss)a(siiddada{sv})a{sv})a(iiduba(ssss)a{sv})a{sv})' -variant_type = GLib.VariantType.new(type_signature) - -if source == Source.DBUS: - command = 'gdbus call -e '\ - '-d org.gnome.Mutter.DisplayConfig '\ - '-o /org/gnome/Mutter/DisplayConfig '\ - '-m org.gnome.Mutter.DisplayConfig.GetCurrentState' - result = subprocess.run(command, - shell=True, check=True, capture_output=True, text=True) - data = result.stdout -else: - if path == '-': - data = sys.stdin.read() - else: - with open(path) as file: - data = file.read() - -variant = GLib.variant_parse(variant_type, data) - def transform_to_string(transform): match transform: case 0: return 'normal' @@ -95,72 +60,113 @@ def print_properties(level, lines, properties): for property in property_list: is_last = property == property_list[-1] print_data(level + 1, is_last, lines, - f'{property} ⇒ {properties[property]}') + f'{property} ⇒ {properties[property]}') -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] - print_data(0, is_last, lines, 'Monitor {}'.format(spec[0])) - print_data(1, False, lines, f'EDID: vendor: {spec[1]}, product: {spec[2]}, serial: {spec[3]}') - - mode_count = len(modes) - if short: - modes = [mode for mode in modes if len(mode[6]) > 0] - print_data(1, False, lines, - f'Modes ({len(modes)}, {mode_count - len(modes)} omitted)') +def print_current_state(args): + if args.file: + source = Source.FILE + path = args.file else: - print_data(1, False, lines, - f'Modes ({len(modes)})') + source = Source.DBUS - for mode in modes: - is_last = mode == modes[-1] - print_data(2, is_last, lines, f'{mode[0]}') - print_data(3, False, lines, f'Dimension: {mode[1]}x{mode[2]}') - print_data(3, False, lines, f'Refresh rate: {mode[3]}') - print_data(3, False, lines, f'Preferred scale: {mode[4]}') - print_data(3, False, lines, f'Supported scales: {mode[5]}') + short = args.short - mode_properties = mode[6] - print_properties(3, lines, mode_properties) + type_signature = '(ua((ssss)a(siiddada{sv})a{sv})a(iiduba(ssss)a{sv})a{sv})' + variant_type = GLib.VariantType.new(type_signature) - print_properties(1, lines, properties) + if source == Source.DBUS: + command = 'gdbus call -e '\ + '-d org.gnome.Mutter.DisplayConfig '\ + '-o /org/gnome/Mutter/DisplayConfig '\ + '-m org.gnome.Mutter.DisplayConfig.GetCurrentState' + result = subprocess.run(command, + shell=True, check=True, capture_output=True, text=True) + data = result.stdout + else: + if path == '-': + data = sys.stdin.read() + else: + with open(path) as file: + data = file.read() -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] - print_data(0, is_last, lines, f'Logical monitor #{index}') - print_data(1, False, lines, - f'Position: ({logical_monitor[0]}, {logical_monitor[1]})') - print_data(1, False, lines, - f'Scale: {logical_monitor[2]}') - print_data(1, False, lines, - f'Transform: {transform_to_string(logical_monitor[3])}') - print_data(1, False, lines, - f'Primary: {logical_monitor[4]}') - monitors = logical_monitor[5] - print_data(1, False, lines, - f'Monitors: ({len(monitors)})') + variant = GLib.variant_parse(variant_type, data) + + print('Serial: {}'.format(variant[0])) + print() + print('Monitors:') + monitors = variant[1] + lines = [] for monitor in monitors: is_last = monitor == monitors[-1] - print_data(2, is_last, lines, - f'{monitor[0]} ({monitor[1]}, {monitor[2]}, {monitor[3]})') + spec = monitor[0] + modes = monitor[1] + properties = monitor[2] + print_data(0, is_last, lines, 'Monitor {}'.format(spec[0])) + print_data( + 1, False, lines, f'EDID: vendor: {spec[1]}, product: {spec[2]}, serial: {spec[3]}') - properties = logical_monitor[6] - print_properties(1, lines, properties) + mode_count = len(modes) + if short: + modes = [mode for mode in modes if len(mode[6]) > 0] + print_data(1, False, lines, + f'Modes ({len(modes)}, {mode_count - len(modes)} omitted)') + else: + print_data(1, False, lines, + f'Modes ({len(modes)})') - index += 1 + for mode in modes: + is_last = mode == modes[-1] + print_data(2, is_last, lines, f'{mode[0]}') + print_data(3, False, lines, f'Dimension: {mode[1]}x{mode[2]}') + print_data(3, False, lines, f'Refresh rate: {mode[3]}') + print_data(3, False, lines, f'Preferred scale: {mode[4]}') + print_data(3, False, lines, f'Supported scales: {mode[5]}') -properties = variant[3] -print() -print_properties(-1, lines, properties) + mode_properties = mode[6] + print_properties(3, lines, mode_properties) + + print_properties(1, lines, properties) + + 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] + print_data(0, is_last, lines, f'Logical monitor #{index}') + print_data(1, False, lines, + f'Position: ({logical_monitor[0]}, {logical_monitor[1]})') + print_data(1, False, lines, + f'Scale: {logical_monitor[2]}') + print_data(1, False, lines, + f'Transform: {transform_to_string(logical_monitor[3])}') + print_data(1, False, lines, + f'Primary: {logical_monitor[4]}') + monitors = logical_monitor[5] + print_data(1, False, lines, + f'Monitors: ({len(monitors)})') + for monitor in monitors: + is_last = monitor == monitors[-1] + print_data(2, is_last, lines, + f'{monitor[0]} ({monitor[1]}, {monitor[2]}, {monitor[3]})') + + properties = logical_monitor[6] + print_properties(1, lines, properties) + + index += 1 + + properties = variant[3] + print() + print_properties(-1, lines, properties) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Get display state') + parser.add_argument('file', metavar='FILE', type=str, nargs='?', + help='Read the output from gdbus call instead of calling D-Bus') + parser.add_argument('--short', action='store_true') + + args = parser.parse_args() + + print_current_state(args)