Search code examples
linuxglibdbusgnome

Take screenshot in gnome environment via its dbus api


I'm trying to take a screenshot using the gnome dbus api https://gitlab.gnome.org/GNOME/xdg-desktop-portal-gnome/-/blob/main/data/org.gnome.Shell.Screenshot.xml

#include <stdbool.h>
#include <stdio.h>
#include <glib/gprintf.h>
#include <gio/gio.h>

int main(void)
{
    GDBusProxy *proxy;
    GDBusConnection *conn;
    GError *error = NULL;
    conn = g_bus_get_sync(G_BUS_TYPE_SESSION, NULL, &error);
    g_assert_no_error(error);

    proxy = g_dbus_proxy_new_sync(conn,
                  G_DBUS_PROXY_FLAGS_NONE,
                  NULL,             /* GDBusInterfaceInfo */
                  "org.gnome.Shell",        /* name */
                  "/org/gnome/Shell",   /* object path */
                  "org.gnome.Shell.Screenshot", /* interface */
                  NULL,             /* GCancellable */
                  &error);
    g_assert_no_error(error);

    int success;
    char filename_used[100];

    g_dbus_proxy_call_sync(proxy,"Screenshot",
    g_variant_new ("(bbsbs)", TRUE, FALSE, "/tmp/screenshot",&success,filename_used),
                G_DBUS_CALL_FLAGS_NONE,
                -1,
                NULL,
                &error);
    g_assert_no_error(error);
    return 0;
}

the error: ERROR:dbus-stack.c:33:main: assertion failed (error == NULL): GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method ?Screenshot? (g-dbus-error-quark, 19) Bail out! ERROR:dbus-stack.c:33:main: assertion failed (error == NULL): GDBus.Error:org.freedesktop.DBus.Error.UnknownMethod: No such method ?Screenshot? (g-dbus-error-quark, 19) Aborted (core dumped)

obviously I'm doing something wrong


Solution

  • I haven't tested my solution, but you are using wrong object_path, correct one is /org/gnome/Shell/Screenshot. Also the method accepts only three input parameters, so g_variant first parameter should be "(bbs)".

    You can use d-feet to introspect names, this can be very helpful for you. Please let me know if this is the correct answer, otherwise I will test it myself.