2

I found this code to take a screenshot on Ted Mielczarek's website.

/*
 * gdk-screenshot.cpp: Save a screenshot of the root window in .png format.
 *  If a filename is specified as the first argument on the commandline,
 *  then the image will be saved to that filename. Otherwise, the image will
 *  be saved as "screenshot.png" in the current working directory.
 *
 * Compile with:
 * g++ -o gdk-screenshot gdk-screenshot.cpp `pkg-config --cflags --libs gdk-x11-2.0`
 */

#include <gdk/gdk.h>
#include <gdk/gdkx.h>

int main(int argc, char** argv)
{
  gdk_init(&argc, &argv);
  GdkWindow* window = window = gdk_get_default_root_window ();
  GdkPixbuf* screenshot = gdk_pixbuf_get_from_drawable (NULL, window, NULL,
                            0, 0, 0, 0,
                            gdk_screen_width(),
                            gdk_screen_height());
  GError* error = NULL;
  const char* filename = (argc > 1) ? argv[1] : "screenshot.png";
  return TRUE == gdk_pixbuf_save (screenshot, filename, "png",
                  &error, NULL);
}

I compiled it as described and it appears to work, in that it produces an image with the correct dimensions, but the screenshot is entirely black. black screenshot

This appears to be a common issue on systems running Wayland (I'm running Archlinux with Wayland), so my question is:

What modifications need to be made to this code to get it to produce a proper screenshot on Wayland (and X)?

mikewilliamson
  • 24,303
  • 17
  • 59
  • 90
  • 2
    The reply to the second bug you link states that it can't be done: "By design, Wayland is a lot more secure than X11 and does not allow one application to capture the content of other applications' windows" – Nick Matteo Jan 10 '17 at 03:53
  • 2
    `gnome-screenshot` and gimp are both able to produce a screenshot of another program under Wayland, and Super+s is supposed to be set up for screenshots under Weston (https://wiki.archlinux.org/index.php/taking_a_screenshot#Weston) which I assume could be triggered by any program. I wish that person had included some explanation of why they feel this is impossible. – mikewilliamson Jan 10 '17 at 04:03

1 Answers1

0

I had a similar problem.

I use Xlib to take screenshots, but this method can't work on Wayland. Every time I run to xgetimage, I report an error. After looking up the data, I find Wayland doesn't allow such screenshots. However, in this way, I can still get the correct screen size.

Now I use DBUS to call the session bus of the system to take a screenshot, which I learned from reading the source code of Gnome screenshot.

This is a simple summary 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);

The document link is here: https://developer.gnome.org/references

The GitHub of Gnome screenshot is here: https://github.com/GNOME/gnome-screenshot

Yatogami
  • 69
  • 7