How to programmatically change display resolution with dbus?

Hi all,

I am working on configuring the GNOME desktop settings via D-Bus, specifically aiming to change the display resolution using org.gnome.Mutter.DisplayConfig and the ApplyMonitorsConfig method (I am aware that we can change the display resolution with xrandr, but I am restricted to dbus at the moment ). However, I am encountering a type conversion error when attempting to use the following code:

method_params = GLib.Variant('(uuba(iiduba(ssa{sv})))', (serial, 1, True, new_logical_monitors))
proxy.call_sync('ApplyMonitorsConfig', method_params, Gio.DBusCallFlags.NONE, -1, None)

My full function is as follows

def change_resolution(width, height):
    proxy = get_display_config_interface()

    current_state = proxy.call_sync('GetCurrentState', None, Gio.DBusCallFlags.NONE, -1, None)
    serial, monitors, logical_monitors, properties = current_state.unpack()

    new_logical_monitors = []
    for monitor in logical_monitors:
        # Unpack existing monitor configuration
        x, y, scale, transform, primary, monitor_spec = monitor[:6]
        if primary:
           current_mode = monitor_spec[0][1] 
           new_mode = (current_mode[0], width, height, current_mode[3])  # New mode with current refresh rate

        new_monitor_config = (x, y, scale, transform, primary, monitor_spec)
        new_logical_monitors.append(new_monitor_config)

    # Pack the method parameters into GLib.Variant
    method_params = GLib.Variant('(uuba(iiduba(ssa{sv})))', (serial, 1, True, new_logical_monitors))

    # Apply new configuration
    proxy.call_sync('ApplyMonitorsConfig',
                    method_params,
                    Gio.DBusCallFlags.NONE, -1, None)

change_resolution(800, 600)

Unfortunately, I am constantly hitting a type conversion error.
Since I have very little experience with this topic, I’d appreciate it if someone could provide me with a simple example or point me to the right documentation. I searched online but didn’t recognize anything particularly useful.

Thanks in advance!

The type signature for the method parameters seems a little bit off; where did you get it?

It looks like it should be (uua(iiduba(ssa{sv}))a{sv}). You have an extraneous b and you’re missing the properties dict at the end.

1 Like

Hi Chris,

Thanks for the reply.

That is a great catch! You are right it should not have that b. I confirmed it again by running

gdbus introspect --session --dest org.gnome.Mutter.DisplayConfig --object-path /org/gnome/Mutter/DisplayConfig

I am actually suspecting the correctness of my data here:

x, y, scale, transform, primary, monitor_spec = monitor[:6]

So stepping through the debugger, monitor has a value of

(0, 0 ,1.0 ,0 ,True, [('Virtual-1', 'unknown', 'unknown', 'unknown')],{})

This looks kinda of odd to me. Specially this piece [('Virtual-1', 'unknown', 'unknown', 'unknown')],{}

Any thoughts? What should a good input looks like?