Take screenshot in gnome environment via its dbus api

I’m trying to take a screenshot using the gnome dbus api data/org.gnome.Shell.Screenshot.xml · main · GNOME / xdg-desktop-portal-gnome · GitLab

#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);
    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

/org/gnome/Shell/Screenshot

now No such interface ?org.gnome.Shell? on object at path /org/gnome/Shell/Screenshot (g-dbus-error-quark, 19)
@youssefmsourani

Example from python and flatpak portal.
But I don’t know why the option of “uri” is ignored .

from gi.repository import GLib,Gio

loop         = GLib.MainLoop()
bus          = Gio.bus_get_sync(Gio.BusType.SESSION,None)
unique_name  = bus.get_unique_name().replace(":","").replace(".","_")




def on__response(proxy,sender_name,signal_name,result):
    if signal_name == "Response":
        if result[0] == 0:
            print(result[1]["uri"])
        else:
            print("Faild")
    loop.quit()

def get_proxy(name,object_path,interface_name):
    proxy = Gio.DBusProxy.new_sync(bus,
                          Gio.DBusProxyFlags.NONE,
                          None,
                          name,
                          object_path,
                          interface_name,
                          None
                          )
    return proxy
    

proxy = get_proxy("org.freedesktop.portal.Desktop",
                  "/org/freedesktop/portal/desktop",
                  "org.freedesktop.portal.Screenshot"
                  )


options =  {
            "handle_token" : GLib.Variant("s",unique_name + "90"),
            "modal"        : GLib.Variant("b",False),
            "interactive"  : GLib.Variant("b",False),
            "uri"          : GLib.Variant("s","file:///home/yuceff28"),
            }
                      
request_handler = proxy.call_sync("Screenshot",
                       GLib.Variant("(sa{sv})",("",options)),
                       Gio.DBusCallFlags.NONE,
                       -1,
                       None)[0]

handler_proxy = get_proxy("org.freedesktop.portal.Desktop",
                          request_handler,
                          "org.freedesktop.portal.Request"
                         )

handler_proxy.connect("g_signal",on__response)
loop.run()

https://docs.flatpak.org/en/latest/portal-api-reference.html#gdbus-org.freedesktop.portal.Screenshot

Because there is no such option. It’s not listed in the documentation you linked, and it doesn’t appear in the xdg-desktop-portal code.

1 Like

Yes, this is correct , I have now noticed that it is among the “results”

thanks you .

Is it possible to ensure that the “Share this screenshot with the requesting application” window does not pop up?

I used org.gnome.Shell for this very purpose, and not org.freedesktop.portal.Desktop, because I thought that if I took a screenshot through org.gnome.Shell, the window would not pop up
@youssefmsourani @fmuellner

Are you using an older xdg-desktop-portal version?

The current behavior (for non-interactive screenshots) is to ask the user once for permission and remember the choice (which can be revisited in Settings → Apps → Permissions)

I used org.gnome.Shell for this very purpose

Access to that interface is restricted, precisely because random apps should not be able to take screenshots without the user’s knowledge and consent.

I don’t know how to check this?
@fmuellner

Apps or Applications?
and I can’t find Permissions anywhere
@fmuellner

It used to be “Applications” and got renamed to “Apps” at one point.

Which is another indication that you are probably on an older version :slight_smile:

I don’t know how to check this?

You can search for “about” in Settings, the panel in question should include the GNOME version, which should be a good-enough approximation.

(How to get the xdg-desktop-portal version is distro-specific, so the above is easier)

version of what? ubuntu? gnome? I use ubuntu 22.04
@fmuellner

GNOME version is 42.9

GNOME 42 is indeed quite old - the current version is 46, the oldest version that is still supported upstream is 45.

Not sure “it will be less annoying in the future” is good enough for you …

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.