Search code examples
cscreenshotx11wayland

How can I take a screenshot on Wayland?


Now I need to develop a plug-in that can take screenshots on Ubuntu, CentOS and fedora. I try to write it in Xlib. It can run on Ubuntu and CentOS, but Fedora uses Wayland, and the plug-in that I developed can't work.

Gnome screenshot can work on these platforms, but after reading his source code, I found that it can achieve screenshots by calling other processes, which is hard for me to learn from.

So is there any way I can take a screenshot on Wayland engine?


Solution

  • I get it in using dbus, here is the light code:

        method_name = "Screenshot";
        method_params = g_variant_new ("(bbs)",
                                         TRUE,
                                         FALSE, /* flash */
                                         filename);
    
        connection = g_application_get_dbus_connection (g_application_get_default ());
        g_dbus_connection_call_sync (connection,
                                   "org.gnome.Shell.Screenshot",
                                   "/org/gnome/Shell/Screenshot",
                                   "org.gnome.Shell.Screenshot",
                                   method_name,
                                   method_params,
                                   NULL,
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1,
                                   NULL,
                                   &error);