2012-02-14 19:33:56 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
|
|
|
|
|
|
|
#include <clutter/clutter.h>
|
|
|
|
#include <cogl/cogl.h>
|
|
|
|
#include <meta/display.h>
|
|
|
|
#include <meta/util.h>
|
|
|
|
#include <meta/meta-plugin.h>
|
2013-08-14 08:48:45 +00:00
|
|
|
#include <meta/meta-cursor-tracker.h>
|
2019-03-12 21:41:18 +00:00
|
|
|
#include <st/st.h>
|
2012-02-14 19:33:56 +00:00
|
|
|
|
|
|
|
#include "shell-global.h"
|
|
|
|
#include "shell-screenshot.h"
|
2016-08-19 02:47:57 +00:00
|
|
|
#include "shell-util.h"
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
typedef enum _ShellScreenshotFlag
|
|
|
|
{
|
|
|
|
SHELL_SCREENSHOT_FLAG_NONE,
|
|
|
|
SHELL_SCREENSHOT_FLAG_INCLUDE_CURSOR,
|
|
|
|
} ShellScreenshotFlag;
|
|
|
|
|
2020-12-26 10:57:24 +00:00
|
|
|
typedef enum _ShellScreenshotMode
|
|
|
|
{
|
|
|
|
SHELL_SCREENSHOT_SCREEN,
|
|
|
|
SHELL_SCREENSHOT_WINDOW,
|
|
|
|
SHELL_SCREENSHOT_AREA,
|
|
|
|
} ShellScreenshotMode;
|
|
|
|
|
2021-02-09 11:51:33 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
SCREENSHOT_TAKEN,
|
|
|
|
|
|
|
|
LAST_SIGNAL
|
|
|
|
};
|
|
|
|
|
|
|
|
static guint signals[LAST_SIGNAL] = { 0, };
|
|
|
|
|
2015-09-24 18:07:44 +00:00
|
|
|
typedef struct _ShellScreenshotPrivate ShellScreenshotPrivate;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
|
|
|
struct _ShellScreenshot
|
|
|
|
{
|
|
|
|
GObject parent_instance;
|
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
2012-02-14 19:33:56 +00:00
|
|
|
};
|
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
struct _ShellScreenshotPrivate
|
|
|
|
{
|
|
|
|
ShellGlobal *global;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
GOutputStream *stream;
|
2020-12-14 14:11:37 +00:00
|
|
|
ShellScreenshotFlag flags;
|
2020-12-26 10:57:24 +00:00
|
|
|
ShellScreenshotMode mode;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2017-11-17 12:02:06 +00:00
|
|
|
GDateTime *datetime;
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
cairo_surface_t *image;
|
|
|
|
cairo_rectangle_int_t screenshot_area;
|
|
|
|
|
2014-12-04 14:38:28 +00:00
|
|
|
gboolean include_frame;
|
2022-01-15 15:22:44 +00:00
|
|
|
|
|
|
|
float scale;
|
2021-08-16 15:12:05 +00:00
|
|
|
ClutterContent *cursor_content;
|
|
|
|
graphene_point_t cursor_point;
|
|
|
|
float cursor_scale;
|
2014-09-27 11:35:22 +00:00
|
|
|
};
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
G_DEFINE_TYPE_WITH_PRIVATE (ShellScreenshot, shell_screenshot, G_TYPE_OBJECT);
|
2012-02-14 19:33:56 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
shell_screenshot_class_init (ShellScreenshotClass *screenshot_class)
|
|
|
|
{
|
2021-02-09 11:51:33 +00:00
|
|
|
signals[SCREENSHOT_TAKEN] =
|
|
|
|
g_signal_new ("screenshot-taken",
|
|
|
|
G_TYPE_FROM_CLASS(screenshot_class),
|
|
|
|
G_SIGNAL_RUN_LAST,
|
|
|
|
0,
|
|
|
|
NULL, NULL, NULL,
|
|
|
|
G_TYPE_NONE,
|
|
|
|
1,
|
|
|
|
META_TYPE_RECTANGLE);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
shell_screenshot_init (ShellScreenshot *screenshot)
|
|
|
|
{
|
2014-09-27 11:35:22 +00:00
|
|
|
screenshot->priv = shell_screenshot_get_instance_private (screenshot);
|
|
|
|
screenshot->priv->global = shell_global_get ();
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-25 18:09:11 +00:00
|
|
|
on_screenshot_written (GObject *source,
|
2018-07-19 13:54:33 +00:00
|
|
|
GAsyncResult *task,
|
2015-09-25 18:09:11 +00:00
|
|
|
gpointer user_data)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2014-09-27 11:35:22 +00:00
|
|
|
ShellScreenshot *screenshot = SHELL_SCREENSHOT (source);
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *result = user_data;
|
2014-09-27 11:35:22 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
g_task_return_boolean (result, g_task_propagate_boolean (G_TASK (task), NULL));
|
|
|
|
g_object_unref (result);
|
2014-09-27 11:35:22 +00:00
|
|
|
|
|
|
|
g_clear_pointer (&priv->image, cairo_surface_destroy);
|
2019-11-05 23:46:41 +00:00
|
|
|
g_clear_object (&priv->stream);
|
2017-11-17 12:02:06 +00:00
|
|
|
g_clear_pointer (&priv->datetime, g_date_time_unref);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-09-25 18:09:11 +00:00
|
|
|
write_screenshot_thread (GTask *result,
|
|
|
|
gpointer object,
|
|
|
|
gpointer task_data,
|
2012-02-14 19:33:56 +00:00
|
|
|
GCancellable *cancellable)
|
|
|
|
{
|
2014-09-27 11:35:22 +00:00
|
|
|
ShellScreenshot *screenshot = SHELL_SCREENSHOT (object);
|
|
|
|
ShellScreenshotPrivate *priv;
|
2020-03-24 06:10:27 +00:00
|
|
|
g_autoptr (GOutputStream) stream = NULL;
|
|
|
|
g_autoptr(GdkPixbuf) pixbuf = NULL;
|
|
|
|
g_autofree char *creation_time = NULL;
|
2020-03-24 06:15:26 +00:00
|
|
|
GError *error = NULL;
|
2014-09-27 11:35:22 +00:00
|
|
|
|
|
|
|
g_assert (screenshot != NULL);
|
2012-11-09 18:02:01 +00:00
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
priv = screenshot->priv;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
stream = g_object_ref (priv->stream);
|
2012-11-09 18:02:01 +00:00
|
|
|
|
2020-03-24 06:10:27 +00:00
|
|
|
pixbuf = gdk_pixbuf_get_from_surface (priv->image,
|
|
|
|
0, 0,
|
|
|
|
cairo_image_surface_get_width (priv->image),
|
|
|
|
cairo_image_surface_get_height (priv->image));
|
|
|
|
creation_time = g_date_time_format (priv->datetime, "%c");
|
2019-04-01 16:06:49 +00:00
|
|
|
|
2020-03-24 06:10:27 +00:00
|
|
|
if (!creation_time)
|
|
|
|
creation_time = g_date_time_format (priv->datetime, "%FT%T%z");
|
2013-02-14 17:22:24 +00:00
|
|
|
|
2020-03-24 06:15:26 +00:00
|
|
|
gdk_pixbuf_save_to_stream (pixbuf, stream, "png", NULL, &error,
|
|
|
|
"tEXt::Software", "gnome-screenshot",
|
|
|
|
"tEXt::Creation Time", creation_time,
|
|
|
|
NULL);
|
2012-11-09 18:02:01 +00:00
|
|
|
|
2020-03-24 06:15:26 +00:00
|
|
|
if (error)
|
|
|
|
g_task_return_error (result, error);
|
|
|
|
else
|
|
|
|
g_task_return_boolean (result, TRUE);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-07-30 07:51:06 +00:00
|
|
|
do_grab_screenshot (ShellScreenshot *screenshot,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
ShellScreenshotFlag flags)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2014-09-27 11:35:22 +00:00
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
2020-07-30 07:51:06 +00:00
|
|
|
ClutterStage *stage = shell_global_get_stage (priv->global);
|
2017-12-23 03:54:48 +00:00
|
|
|
cairo_rectangle_int_t screenshot_rect = { x, y, width, height };
|
2020-07-30 07:51:06 +00:00
|
|
|
int image_width;
|
|
|
|
int image_height;
|
|
|
|
float scale;
|
|
|
|
cairo_surface_t *image;
|
|
|
|
ClutterPaintFlag paint_flags = CLUTTER_PAINT_FLAG_NONE;
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
|
|
|
|
clutter_stage_get_capture_final_size (stage, &screenshot_rect,
|
|
|
|
&image_width,
|
|
|
|
&image_height,
|
|
|
|
&scale);
|
|
|
|
image = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
|
|
|
image_width, image_height);
|
|
|
|
|
|
|
|
if (flags & SHELL_SCREENSHOT_FLAG_INCLUDE_CURSOR)
|
|
|
|
paint_flags |= CLUTTER_PAINT_FLAG_FORCE_CURSORS;
|
2016-08-19 02:47:57 +00:00
|
|
|
else
|
2020-07-30 07:51:06 +00:00
|
|
|
paint_flags |= CLUTTER_PAINT_FLAG_NO_CURSORS;
|
|
|
|
if (!clutter_stage_paint_to_buffer (stage, &screenshot_rect, scale,
|
|
|
|
cairo_image_surface_get_data (image),
|
|
|
|
cairo_image_surface_get_stride (image),
|
|
|
|
CLUTTER_CAIRO_FORMAT_ARGB32,
|
|
|
|
paint_flags,
|
|
|
|
&error))
|
2017-12-23 03:54:48 +00:00
|
|
|
{
|
2020-07-30 07:51:06 +00:00
|
|
|
cairo_surface_destroy (image);
|
|
|
|
g_warning ("Failed to take screenshot: %s", error->message);
|
|
|
|
return;
|
2017-12-23 03:54:48 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
priv->image = image;
|
2016-06-27 07:57:28 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
priv->datetime = g_date_time_new_now_local ();
|
2019-02-13 01:13:47 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 08:48:45 +00:00
|
|
|
static void
|
2019-02-13 01:01:03 +00:00
|
|
|
draw_cursor_image (cairo_surface_t *surface,
|
|
|
|
cairo_rectangle_int_t area)
|
2013-08-14 08:48:45 +00:00
|
|
|
{
|
|
|
|
CoglTexture *texture;
|
|
|
|
int width, height;
|
|
|
|
int stride;
|
|
|
|
guint8 *data;
|
2019-02-13 01:01:03 +00:00
|
|
|
MetaDisplay *display;
|
|
|
|
MetaCursorTracker *tracker;
|
2012-02-14 19:33:56 +00:00
|
|
|
cairo_surface_t *cursor_surface;
|
|
|
|
cairo_region_t *screenshot_region;
|
|
|
|
cairo_t *cr;
|
2013-08-14 08:48:45 +00:00
|
|
|
int x, y;
|
|
|
|
int xhot, yhot;
|
2019-02-13 01:01:03 +00:00
|
|
|
double xscale, yscale;
|
2020-07-20 10:08:43 +00:00
|
|
|
graphene_point_t point;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2019-02-13 01:01:03 +00:00
|
|
|
display = shell_global_get_display (shell_global_get ());
|
|
|
|
tracker = meta_cursor_tracker_get_for_display (display);
|
2016-09-19 13:34:03 +00:00
|
|
|
texture = meta_cursor_tracker_get_sprite (tracker);
|
2019-02-13 01:01:03 +00:00
|
|
|
|
2016-09-19 13:34:03 +00:00
|
|
|
if (!texture)
|
|
|
|
return;
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
screenshot_region = cairo_region_create_rectangle (&area);
|
2020-07-20 10:08:43 +00:00
|
|
|
meta_cursor_tracker_get_pointer (tracker, &point, NULL);
|
|
|
|
x = point.x;
|
|
|
|
y = point.y;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-07-20 10:08:43 +00:00
|
|
|
if (!cairo_region_contains_point (screenshot_region, point.x, point.y))
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2013-08-14 08:48:45 +00:00
|
|
|
cairo_region_destroy (screenshot_region);
|
|
|
|
return;
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2013-08-14 08:48:45 +00:00
|
|
|
meta_cursor_tracker_get_hot (tracker, &xhot, &yhot);
|
|
|
|
width = cogl_texture_get_width (texture);
|
|
|
|
height = cogl_texture_get_height (texture);
|
|
|
|
stride = 4 * width;
|
|
|
|
data = g_new (guint8, stride * height);
|
|
|
|
cogl_texture_get_data (texture, CLUTTER_CAIRO_FORMAT_ARGB32, stride, data);
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2013-08-14 08:48:45 +00:00
|
|
|
/* FIXME: cairo-gl? */
|
|
|
|
cursor_surface = cairo_image_surface_create_for_data (data,
|
|
|
|
CAIRO_FORMAT_ARGB32,
|
|
|
|
width, height,
|
|
|
|
stride);
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2019-02-13 01:30:28 +00:00
|
|
|
cairo_surface_get_device_scale (surface, &xscale, &yscale);
|
|
|
|
|
|
|
|
if (xscale != 1.0 || yscale != 1.0)
|
|
|
|
{
|
|
|
|
int monitor;
|
|
|
|
float monitor_scale;
|
|
|
|
MetaRectangle cursor_rect = {
|
|
|
|
.x = x, .y = y, .width = width, .height = height
|
|
|
|
};
|
|
|
|
|
|
|
|
monitor = meta_display_get_monitor_index_for_rect (display, &cursor_rect);
|
|
|
|
monitor_scale = meta_display_get_monitor_scale (display, monitor);
|
|
|
|
|
|
|
|
cairo_surface_set_device_scale (cursor_surface, monitor_scale, monitor_scale);
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
cr = cairo_create (surface);
|
|
|
|
cairo_set_source_surface (cr,
|
|
|
|
cursor_surface,
|
2013-08-14 08:48:45 +00:00
|
|
|
x - xhot - area.x,
|
|
|
|
y - yhot - area.y);
|
2012-02-14 19:33:56 +00:00
|
|
|
cairo_paint (cr);
|
|
|
|
|
|
|
|
cairo_destroy (cr);
|
|
|
|
cairo_surface_destroy (cursor_surface);
|
|
|
|
cairo_region_destroy (screenshot_region);
|
2013-08-14 08:48:45 +00:00
|
|
|
g_free (data);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:35:55 +00:00
|
|
|
static void
|
2020-07-30 07:51:06 +00:00
|
|
|
grab_screenshot (ShellScreenshot *screenshot,
|
|
|
|
ShellScreenshotFlag flags,
|
|
|
|
GTask *result)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2020-07-30 07:51:06 +00:00
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
2018-01-03 07:55:38 +00:00
|
|
|
MetaDisplay *display;
|
2012-02-14 19:33:56 +00:00
|
|
|
int width, height;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *task;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2018-01-03 07:55:38 +00:00
|
|
|
display = shell_global_get_display (priv->global);
|
|
|
|
meta_display_get_size (display, &width, &height);
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
do_grab_screenshot (screenshot,
|
|
|
|
0, 0, width, height,
|
|
|
|
flags);
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2014-09-27 11:35:22 +00:00
|
|
|
priv->screenshot_area.x = 0;
|
|
|
|
priv->screenshot_area.y = 0;
|
|
|
|
priv->screenshot_area.width = width;
|
|
|
|
priv->screenshot_area.height = height;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
task = g_task_new (screenshot, NULL, on_screenshot_written, result);
|
|
|
|
g_task_run_in_thread (task, write_screenshot_thread);
|
|
|
|
g_object_unref (task);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 15:22:44 +00:00
|
|
|
static void
|
|
|
|
grab_screenshot_content (ShellScreenshot *screenshot,
|
|
|
|
GTask *result)
|
|
|
|
{
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
|
|
|
MetaDisplay *display;
|
|
|
|
int width, height;
|
|
|
|
cairo_rectangle_int_t screenshot_rect;
|
|
|
|
ClutterStage *stage;
|
|
|
|
int image_width;
|
|
|
|
int image_height;
|
|
|
|
float scale;
|
|
|
|
g_autoptr (GError) error = NULL;
|
|
|
|
g_autoptr (ClutterContent) content = NULL;
|
2021-08-16 15:12:05 +00:00
|
|
|
MetaCursorTracker *tracker;
|
|
|
|
CoglTexture *cursor_texture;
|
|
|
|
int cursor_hot_x, cursor_hot_y;
|
2022-01-15 15:22:44 +00:00
|
|
|
|
|
|
|
display = shell_global_get_display (priv->global);
|
|
|
|
meta_display_get_size (display, &width, &height);
|
|
|
|
screenshot_rect = (cairo_rectangle_int_t) {
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
.width = width,
|
|
|
|
.height = height,
|
|
|
|
};
|
|
|
|
|
|
|
|
stage = shell_global_get_stage (priv->global);
|
|
|
|
|
|
|
|
clutter_stage_get_capture_final_size (stage, &screenshot_rect,
|
|
|
|
&image_width,
|
|
|
|
&image_height,
|
|
|
|
&scale);
|
|
|
|
|
|
|
|
priv->scale = scale;
|
|
|
|
|
|
|
|
content = clutter_stage_paint_to_content (stage, &screenshot_rect, scale,
|
|
|
|
CLUTTER_PAINT_FLAG_NO_CURSORS,
|
|
|
|
&error);
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
g_task_return_error (result, g_steal_pointer (&error));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-08-16 15:12:05 +00:00
|
|
|
tracker = meta_cursor_tracker_get_for_display (display);
|
|
|
|
cursor_texture = meta_cursor_tracker_get_sprite (tracker);
|
|
|
|
|
|
|
|
// If the cursor is invisible, the texture is NULL.
|
|
|
|
if (cursor_texture)
|
|
|
|
{
|
|
|
|
unsigned int width, height;
|
|
|
|
CoglContext *ctx;
|
|
|
|
CoglPipeline *pipeline;
|
|
|
|
CoglTexture2D *texture;
|
|
|
|
CoglOffscreen *offscreen;
|
|
|
|
ClutterStageView *view;
|
|
|
|
|
|
|
|
// Copy the texture to prevent it from changing shortly after.
|
|
|
|
width = cogl_texture_get_width (cursor_texture);
|
|
|
|
height = cogl_texture_get_height (cursor_texture);
|
|
|
|
|
|
|
|
ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
|
|
|
|
|
|
|
|
texture = cogl_texture_2d_new_with_size (ctx, width, height);
|
|
|
|
offscreen = cogl_offscreen_new_with_texture (texture);
|
|
|
|
cogl_framebuffer_clear4f (COGL_FRAMEBUFFER (offscreen),
|
|
|
|
COGL_BUFFER_BIT_COLOR,
|
|
|
|
0, 0, 0, 0);
|
|
|
|
|
|
|
|
pipeline = cogl_pipeline_new (ctx);
|
|
|
|
cogl_pipeline_set_layer_texture (pipeline, 0, cursor_texture);
|
|
|
|
|
|
|
|
cogl_framebuffer_draw_textured_rectangle (COGL_FRAMEBUFFER (offscreen),
|
|
|
|
pipeline,
|
|
|
|
-1, 1, 1, -1,
|
|
|
|
0, 0, 1, 1);
|
|
|
|
cogl_object_unref (pipeline);
|
|
|
|
g_object_unref (offscreen);
|
|
|
|
|
|
|
|
priv->cursor_content =
|
|
|
|
clutter_texture_content_new_from_texture (texture, NULL);
|
|
|
|
cogl_object_unref (texture);
|
|
|
|
|
|
|
|
priv->cursor_scale = meta_cursor_tracker_get_scale (tracker);
|
|
|
|
|
|
|
|
meta_cursor_tracker_get_pointer (tracker, &priv->cursor_point, NULL);
|
|
|
|
|
|
|
|
view = clutter_stage_get_view_at (stage,
|
|
|
|
priv->cursor_point.x,
|
|
|
|
priv->cursor_point.y);
|
|
|
|
|
|
|
|
meta_cursor_tracker_get_hot (tracker, &cursor_hot_x, &cursor_hot_y);
|
|
|
|
priv->cursor_point.x -= cursor_hot_x * priv->cursor_scale;
|
|
|
|
priv->cursor_point.y -= cursor_hot_y * priv->cursor_scale;
|
|
|
|
|
|
|
|
// Align the coordinates to the pixel grid the same way it's done in
|
|
|
|
// MetaCursorRenderer.
|
|
|
|
if (view)
|
|
|
|
{
|
|
|
|
cairo_rectangle_int_t view_layout;
|
|
|
|
float view_scale;
|
|
|
|
|
|
|
|
clutter_stage_view_get_layout (view, &view_layout);
|
|
|
|
view_scale = clutter_stage_view_get_scale (view);
|
|
|
|
|
|
|
|
priv->cursor_point.x -= view_layout.x;
|
|
|
|
priv->cursor_point.y -= view_layout.y;
|
|
|
|
|
|
|
|
priv->cursor_point.x =
|
|
|
|
floorf (priv->cursor_point.x * view_scale) / view_scale;
|
|
|
|
priv->cursor_point.y =
|
|
|
|
floorf (priv->cursor_point.y * view_scale) / view_scale;
|
|
|
|
|
|
|
|
priv->cursor_point.x += view_layout.x;
|
|
|
|
priv->cursor_point.y += view_layout.y;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-15 15:22:44 +00:00
|
|
|
g_task_return_pointer (result, g_steal_pointer (&content), g_object_unref);
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
static void
|
2020-07-30 07:51:06 +00:00
|
|
|
grab_window_screenshot (ShellScreenshot *screenshot,
|
|
|
|
ShellScreenshotFlag flags,
|
|
|
|
GTask *result)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2014-12-04 14:38:28 +00:00
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *task;
|
2018-01-03 07:55:38 +00:00
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
2014-12-04 14:38:28 +00:00
|
|
|
MetaWindow *window = meta_display_get_focus_window (display);
|
|
|
|
ClutterActor *window_actor;
|
|
|
|
gfloat actor_x, actor_y;
|
|
|
|
MetaRectangle rect;
|
|
|
|
|
|
|
|
window_actor = CLUTTER_ACTOR (meta_window_get_compositor_private (window));
|
|
|
|
clutter_actor_get_position (window_actor, &actor_x, &actor_y);
|
|
|
|
|
|
|
|
meta_window_get_frame_rect (window, &rect);
|
|
|
|
|
|
|
|
if (!priv->include_frame)
|
|
|
|
meta_window_frame_rect_to_client_rect (window, &rect, &rect);
|
|
|
|
|
2019-10-14 15:13:11 +00:00
|
|
|
priv->screenshot_area = rect;
|
2014-12-04 14:38:28 +00:00
|
|
|
|
2019-08-26 13:37:09 +00:00
|
|
|
priv->image = meta_window_actor_get_image (META_WINDOW_ACTOR (window_actor),
|
2019-10-14 15:13:11 +00:00
|
|
|
NULL);
|
2020-10-18 14:01:05 +00:00
|
|
|
|
|
|
|
if (!priv->image)
|
|
|
|
{
|
|
|
|
g_task_report_new_error (screenshot, on_screenshot_written, result, NULL,
|
|
|
|
G_IO_ERROR, G_IO_ERROR_FAILED,
|
|
|
|
"Capturing window failed");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-17 12:02:06 +00:00
|
|
|
priv->datetime = g_date_time_new_now_local ();
|
2014-12-04 14:38:28 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
if (flags & SHELL_SCREENSHOT_FLAG_INCLUDE_CURSOR)
|
2019-02-13 01:30:28 +00:00
|
|
|
{
|
|
|
|
if (meta_window_get_client_type (window) == META_WINDOW_CLIENT_TYPE_WAYLAND)
|
|
|
|
{
|
|
|
|
float resource_scale;
|
2020-05-28 12:39:11 +00:00
|
|
|
resource_scale = clutter_actor_get_resource_scale (window_actor);
|
2019-02-13 01:30:28 +00:00
|
|
|
|
|
|
|
cairo_surface_set_device_scale (priv->image, resource_scale, resource_scale);
|
|
|
|
}
|
|
|
|
|
|
|
|
draw_cursor_image (priv->image, priv->screenshot_area);
|
|
|
|
}
|
2019-02-13 01:01:03 +00:00
|
|
|
|
2021-02-09 11:51:33 +00:00
|
|
|
g_signal_emit (screenshot, signals[SCREENSHOT_TAKEN], 0, &rect);
|
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
task = g_task_new (screenshot, NULL, on_screenshot_written, result);
|
|
|
|
g_task_run_in_thread (task, write_screenshot_thread);
|
|
|
|
g_object_unref (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
finish_screenshot (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
cairo_rectangle_int_t **area,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
|
|
|
|
|
|
|
if (!g_task_propagate_boolean (G_TASK (result), error))
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
if (area)
|
|
|
|
*area = &priv->screenshot_area;
|
|
|
|
|
|
|
|
return TRUE;
|
2014-12-04 14:38:28 +00:00
|
|
|
}
|
|
|
|
|
2020-12-14 14:11:37 +00:00
|
|
|
static void
|
|
|
|
on_after_paint (ClutterStage *stage,
|
|
|
|
ClutterStageView *view,
|
|
|
|
GTask *result)
|
|
|
|
{
|
|
|
|
ShellScreenshot *screenshot = g_task_get_task_data (result);
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
2020-12-26 10:57:24 +00:00
|
|
|
GTask *task;
|
2020-12-14 14:11:37 +00:00
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (stage, on_after_paint, result);
|
|
|
|
|
2020-12-26 10:57:24 +00:00
|
|
|
if (priv->mode == SHELL_SCREENSHOT_AREA)
|
|
|
|
{
|
|
|
|
do_grab_screenshot (screenshot,
|
|
|
|
priv->screenshot_area.x,
|
|
|
|
priv->screenshot_area.y,
|
|
|
|
priv->screenshot_area.width,
|
|
|
|
priv->screenshot_area.height,
|
|
|
|
priv->flags);
|
|
|
|
|
|
|
|
task = g_task_new (screenshot, NULL, on_screenshot_written, result);
|
|
|
|
g_task_run_in_thread (task, write_screenshot_thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
grab_screenshot (screenshot, priv->flags, result);
|
|
|
|
}
|
2020-12-14 14:11:37 +00:00
|
|
|
|
2021-02-09 11:51:33 +00:00
|
|
|
g_signal_emit (screenshot, signals[SCREENSHOT_TAKEN], 0,
|
|
|
|
(cairo_rectangle_int_t *) &priv->screenshot_area);
|
|
|
|
|
2020-12-14 14:11:37 +00:00
|
|
|
meta_enable_unredirect_for_display (display);
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @include_cursor: Whether to include the cursor or not
|
2019-11-05 23:46:41 +00:00
|
|
|
* @stream: The stream for the screenshot
|
2012-02-14 19:33:56 +00:00
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* of the async grabbing
|
2018-07-19 13:54:33 +00:00
|
|
|
* @user_data: the data to pass to callback function
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
* Takes a screenshot of the whole screen
|
2019-11-05 23:46:41 +00:00
|
|
|
* in @stream as png image.
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2018-07-19 13:54:33 +00:00
|
|
|
shell_screenshot_screenshot (ShellScreenshot *screenshot,
|
|
|
|
gboolean include_cursor,
|
2019-11-05 23:46:41 +00:00
|
|
|
GOutputStream *stream,
|
2018-07-19 13:54:33 +00:00
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *result;
|
2020-07-30 07:51:06 +00:00
|
|
|
ShellScreenshotFlag flags;
|
2014-09-27 11:35:22 +00:00
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_if_fail (SHELL_IS_SCREENSHOT (screenshot));
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
|
|
priv = screenshot->priv;
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
if (priv->stream != NULL) {
|
2014-09-27 11:35:22 +00:00
|
|
|
if (callback)
|
2018-07-19 13:54:33 +00:00
|
|
|
g_task_report_new_error (screenshot,
|
|
|
|
callback,
|
|
|
|
user_data,
|
|
|
|
shell_screenshot_screenshot,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_PENDING,
|
|
|
|
"Only one screenshot operation at a time "
|
|
|
|
"is permitted");
|
2014-09-27 11:35:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
result = g_task_new (screenshot, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (result, shell_screenshot_screenshot);
|
2020-12-14 14:11:37 +00:00
|
|
|
g_task_set_task_data (result, screenshot, NULL);
|
2018-07-19 13:54:33 +00:00
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
priv->stream = g_object_ref (stream);
|
2014-12-04 14:38:28 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
flags = SHELL_SCREENSHOT_FLAG_NONE;
|
2019-02-13 01:23:41 +00:00
|
|
|
if (include_cursor)
|
2020-07-30 07:51:06 +00:00
|
|
|
flags |= SHELL_SCREENSHOT_FLAG_INCLUDE_CURSOR;
|
2019-02-13 01:23:41 +00:00
|
|
|
|
2020-12-14 14:11:37 +00:00
|
|
|
if (meta_is_wayland_compositor ())
|
|
|
|
{
|
|
|
|
grab_screenshot (screenshot, flags, result);
|
2021-02-09 11:51:33 +00:00
|
|
|
|
|
|
|
g_signal_emit (screenshot, signals[SCREENSHOT_TAKEN], 0,
|
|
|
|
(cairo_rectangle_int_t *) &priv->screenshot_area);
|
2020-12-14 14:11:37 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
|
|
|
ClutterStage *stage = shell_global_get_stage (priv->global);
|
|
|
|
|
|
|
|
meta_disable_unredirect_for_display (display);
|
|
|
|
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
|
|
|
|
priv->flags = flags;
|
2020-12-26 10:57:24 +00:00
|
|
|
priv->mode = SHELL_SCREENSHOT_SCREEN;
|
2020-12-14 14:11:37 +00:00
|
|
|
g_signal_connect (stage, "after-paint",
|
|
|
|
G_CALLBACK (on_after_paint), result);
|
|
|
|
}
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_finish:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @area: (out) (transfer none): the area that was grabbed in screen coordinates
|
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by shell_screenshot_screenshot()
|
|
|
|
* and obtain its result.
|
|
|
|
*
|
|
|
|
* Returns: whether the operation was successful
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
shell_screenshot_screenshot_finish (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
cairo_rectangle_int_t **area,
|
|
|
|
GError **error)
|
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_val_if_fail (SHELL_IS_SCREENSHOT (screenshot), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
2018-07-19 13:54:33 +00:00
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_screenshot),
|
|
|
|
FALSE);
|
2019-11-05 23:46:41 +00:00
|
|
|
return finish_screenshot (screenshot, result, area, error);
|
2018-07-19 13:54:33 +00:00
|
|
|
}
|
|
|
|
|
2022-01-15 15:22:44 +00:00
|
|
|
static void
|
|
|
|
screenshot_stage_to_content_on_after_paint (ClutterStage *stage,
|
|
|
|
ClutterStageView *view,
|
|
|
|
GTask *result)
|
|
|
|
{
|
|
|
|
ShellScreenshot *screenshot = g_task_get_task_data (result);
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
|
|
|
|
|
|
|
g_signal_handlers_disconnect_by_func (stage,
|
|
|
|
screenshot_stage_to_content_on_after_paint,
|
|
|
|
result);
|
|
|
|
|
|
|
|
meta_enable_unredirect_for_display (display);
|
|
|
|
|
|
|
|
grab_screenshot_content (screenshot, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_stage_to_content:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* of the async grabbing
|
|
|
|
* @user_data: the data to pass to callback function
|
|
|
|
*
|
|
|
|
* Takes a screenshot of the whole screen as #ClutterContent.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
shell_screenshot_screenshot_stage_to_content (ShellScreenshot *screenshot,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
ShellScreenshotPrivate *priv;
|
|
|
|
GTask *result;
|
|
|
|
|
|
|
|
g_return_if_fail (SHELL_IS_SCREENSHOT (screenshot));
|
|
|
|
|
|
|
|
result = g_task_new (screenshot, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (result, shell_screenshot_screenshot_stage_to_content);
|
|
|
|
g_task_set_task_data (result, screenshot, NULL);
|
|
|
|
|
|
|
|
if (meta_is_wayland_compositor ())
|
|
|
|
{
|
|
|
|
grab_screenshot_content (screenshot, result);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
priv = screenshot->priv;
|
|
|
|
|
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
|
|
|
ClutterStage *stage = shell_global_get_stage (priv->global);
|
|
|
|
|
|
|
|
meta_disable_unredirect_for_display (display);
|
|
|
|
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
|
|
|
|
g_signal_connect (stage, "after-paint",
|
|
|
|
G_CALLBACK (screenshot_stage_to_content_on_after_paint),
|
|
|
|
result);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_stage_to_content_finish:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @scale: (out) (optional): location to store the content scale
|
2021-08-16 15:12:05 +00:00
|
|
|
* @cursor_content: (out) (optional): location to store the cursor content
|
|
|
|
* @cursor_point: (out) (optional): location to store the point at which to
|
|
|
|
* draw the cursor content
|
|
|
|
* @cursor_scale: (out) (optional): location to store the cursor scale
|
2022-01-15 15:22:44 +00:00
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by
|
|
|
|
* shell_screenshot_screenshot_stage_to_content() and obtain its result.
|
|
|
|
*
|
|
|
|
* Returns: (transfer full): the #ClutterContent, or NULL
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
ClutterContent *
|
2021-08-16 15:12:05 +00:00
|
|
|
shell_screenshot_screenshot_stage_to_content_finish (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
float *scale,
|
|
|
|
ClutterContent **cursor_content,
|
|
|
|
graphene_point_t *cursor_point,
|
|
|
|
float *cursor_scale,
|
|
|
|
GError **error)
|
2022-01-15 15:22:44 +00:00
|
|
|
{
|
|
|
|
ShellScreenshotPrivate *priv = screenshot->priv;
|
|
|
|
ClutterContent *content;
|
|
|
|
|
|
|
|
g_return_val_if_fail (SHELL_IS_SCREENSHOT (screenshot), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_screenshot_stage_to_content),
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
content = g_task_propagate_pointer (G_TASK (result), error);
|
|
|
|
if (!content)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (scale)
|
|
|
|
*scale = priv->scale;
|
|
|
|
|
2021-08-16 15:12:05 +00:00
|
|
|
if (cursor_content)
|
|
|
|
*cursor_content = g_steal_pointer (&priv->cursor_content);
|
|
|
|
else
|
|
|
|
g_clear_pointer (&priv->cursor_content, g_object_unref);
|
|
|
|
|
|
|
|
if (cursor_point)
|
|
|
|
*cursor_point = priv->cursor_point;
|
|
|
|
|
|
|
|
if (cursor_scale)
|
|
|
|
*cursor_scale = priv->cursor_scale;
|
|
|
|
|
2022-01-15 15:22:44 +00:00
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_area:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @x: The X coordinate of the area
|
|
|
|
* @y: The Y coordinate of the area
|
|
|
|
* @width: The width of the area
|
|
|
|
* @height: The height of the area
|
2019-11-05 23:46:41 +00:00
|
|
|
* @stream: The stream for the screenshot
|
2012-02-14 19:33:56 +00:00
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* of the async grabbing
|
2018-07-19 13:54:33 +00:00
|
|
|
* @user_data: the data to pass to callback function
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
* Takes a screenshot of the passed in area and saves it
|
2019-11-05 23:46:41 +00:00
|
|
|
* in @stream as png image.
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2018-07-19 13:54:33 +00:00
|
|
|
shell_screenshot_screenshot_area (ShellScreenshot *screenshot,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
2019-11-05 23:46:41 +00:00
|
|
|
GOutputStream *stream,
|
2018-07-19 13:54:33 +00:00
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *result;
|
2020-07-30 07:51:06 +00:00
|
|
|
g_autoptr (GTask) task = NULL;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_if_fail (SHELL_IS_SCREENSHOT (screenshot));
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
|
|
priv = screenshot->priv;
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
if (priv->stream != NULL) {
|
2014-09-27 11:35:22 +00:00
|
|
|
if (callback)
|
2018-07-19 13:54:33 +00:00
|
|
|
g_task_report_new_error (screenshot,
|
|
|
|
callback,
|
|
|
|
NULL,
|
|
|
|
shell_screenshot_screenshot_area,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_PENDING,
|
|
|
|
"Only one screenshot operation at a time "
|
|
|
|
"is permitted");
|
2014-09-27 11:35:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
result = g_task_new (screenshot, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (result, shell_screenshot_screenshot_area);
|
2020-12-26 10:57:24 +00:00
|
|
|
g_task_set_task_data (result, screenshot, NULL);
|
2018-07-19 13:54:33 +00:00
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
priv->stream = g_object_ref (stream);
|
2014-09-27 11:35:22 +00:00
|
|
|
priv->screenshot_area.x = x;
|
|
|
|
priv->screenshot_area.y = y;
|
|
|
|
priv->screenshot_area.width = width;
|
|
|
|
priv->screenshot_area.height = height;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
|
|
|
|
2020-12-26 10:57:24 +00:00
|
|
|
if (meta_is_wayland_compositor ())
|
|
|
|
{
|
|
|
|
do_grab_screenshot (screenshot,
|
|
|
|
priv->screenshot_area.x,
|
|
|
|
priv->screenshot_area.y,
|
|
|
|
priv->screenshot_area.width,
|
|
|
|
priv->screenshot_area.height,
|
|
|
|
SHELL_SCREENSHOT_FLAG_NONE);
|
|
|
|
|
2021-02-09 11:51:33 +00:00
|
|
|
g_signal_emit (screenshot, signals[SCREENSHOT_TAKEN], 0,
|
|
|
|
(cairo_rectangle_int_t *) &priv->screenshot_area);
|
|
|
|
|
2020-12-26 10:57:24 +00:00
|
|
|
task = g_task_new (screenshot, NULL, on_screenshot_written, result);
|
|
|
|
g_task_run_in_thread (task, write_screenshot_thread);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MetaDisplay *display = shell_global_get_display (priv->global);
|
|
|
|
ClutterStage *stage = shell_global_get_stage (priv->global);
|
|
|
|
|
|
|
|
meta_disable_unredirect_for_display (display);
|
|
|
|
clutter_actor_queue_redraw (CLUTTER_ACTOR (stage));
|
|
|
|
priv->flags = SHELL_SCREENSHOT_FLAG_NONE;
|
|
|
|
priv->mode = SHELL_SCREENSHOT_AREA;
|
|
|
|
g_signal_connect (stage, "after-paint",
|
|
|
|
G_CALLBACK (on_after_paint), result);
|
|
|
|
}
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_area_finish:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @area: (out) (transfer none): the area that was grabbed in screen coordinates
|
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by shell_screenshot_screenshot_area()
|
|
|
|
* and obtain its result.
|
|
|
|
*
|
|
|
|
* Returns: whether the operation was successful
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
shell_screenshot_screenshot_area_finish (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
cairo_rectangle_int_t **area,
|
|
|
|
GError **error)
|
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_val_if_fail (SHELL_IS_SCREENSHOT (screenshot), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
2018-07-19 13:54:33 +00:00
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_screenshot_area),
|
|
|
|
FALSE);
|
2019-11-05 23:46:41 +00:00
|
|
|
return finish_screenshot (screenshot, result, area, error);
|
2018-07-19 13:54:33 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 19:33:56 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_window:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @include_frame: Whether to include the frame or not
|
|
|
|
* @include_cursor: Whether to include the cursor or not
|
2019-11-05 23:46:41 +00:00
|
|
|
* @stream: The stream for the screenshot
|
2012-02-14 19:33:56 +00:00
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* of the async grabbing
|
2018-07-19 13:54:33 +00:00
|
|
|
* @user_data: the data to pass to callback function
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
* Takes a screenshot of the focused window (optionally omitting the frame)
|
2019-11-05 23:46:41 +00:00
|
|
|
* in @stream as png image.
|
2012-02-14 19:33:56 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
2018-07-19 13:54:33 +00:00
|
|
|
shell_screenshot_screenshot_window (ShellScreenshot *screenshot,
|
|
|
|
gboolean include_frame,
|
|
|
|
gboolean include_cursor,
|
2019-11-05 23:46:41 +00:00
|
|
|
GOutputStream *stream,
|
2018-07-19 13:54:33 +00:00
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
2012-02-14 19:33:56 +00:00
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
|
|
|
MetaDisplay *display;
|
|
|
|
MetaWindow *window;
|
2018-07-19 13:54:33 +00:00
|
|
|
GTask *result;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_if_fail (SHELL_IS_SCREENSHOT (screenshot));
|
|
|
|
g_return_if_fail (G_IS_OUTPUT_STREAM (stream));
|
|
|
|
|
|
|
|
priv = screenshot->priv;
|
|
|
|
display = shell_global_get_display (priv->global);
|
|
|
|
window = meta_display_get_focus_window (display);
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
if (priv->stream != NULL || !window) {
|
2014-09-27 11:35:22 +00:00
|
|
|
if (callback)
|
2018-07-19 13:54:33 +00:00
|
|
|
g_task_report_new_error (screenshot,
|
|
|
|
callback,
|
|
|
|
NULL,
|
|
|
|
shell_screenshot_screenshot_window,
|
|
|
|
G_IO_ERROR,
|
|
|
|
G_IO_ERROR_PENDING,
|
|
|
|
"Only one screenshot operation at a time "
|
|
|
|
"is permitted");
|
2014-09-27 11:35:22 +00:00
|
|
|
return;
|
|
|
|
}
|
2013-05-19 10:26:49 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
result = g_task_new (screenshot, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (result, shell_screenshot_screenshot_window);
|
|
|
|
|
2019-11-05 23:46:41 +00:00
|
|
|
priv->stream = g_object_ref (stream);
|
2014-12-04 14:38:28 +00:00
|
|
|
priv->include_frame = include_frame;
|
2012-02-14 19:33:56 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
grab_window_screenshot (screenshot, include_cursor, result);
|
2012-02-14 19:33:56 +00:00
|
|
|
}
|
2012-02-14 20:57:34 +00:00
|
|
|
|
2018-07-19 13:54:33 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_screenshot_window_finish:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @area: (out) (transfer none): the area that was grabbed in screen coordinates
|
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by shell_screenshot_screenshot_window()
|
|
|
|
* and obtain its result.
|
|
|
|
*
|
|
|
|
* Returns: whether the operation was successful
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
shell_screenshot_screenshot_window_finish (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
cairo_rectangle_int_t **area,
|
|
|
|
GError **error)
|
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_val_if_fail (SHELL_IS_SCREENSHOT (screenshot), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
2018-07-19 13:54:33 +00:00
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_screenshot_window),
|
|
|
|
FALSE);
|
2019-11-05 23:46:41 +00:00
|
|
|
return finish_screenshot (screenshot, result, area, error);
|
2018-07-19 13:54:33 +00:00
|
|
|
}
|
|
|
|
|
2018-07-19 12:50:49 +00:00
|
|
|
/**
|
|
|
|
* shell_screenshot_pick_color:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @x: The X coordinate to pick
|
|
|
|
* @y: The Y coordinate to pick
|
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* of the async grabbing
|
|
|
|
*
|
|
|
|
* Picks the pixel at @x, @y and returns its color as #ClutterColor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
shell_screenshot_pick_color (ShellScreenshot *screenshot,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
2020-07-30 07:51:06 +00:00
|
|
|
g_autoptr (GTask) result = NULL;
|
2018-07-19 12:50:49 +00:00
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_if_fail (SHELL_IS_SCREENSHOT (screenshot));
|
|
|
|
|
2018-07-19 12:50:49 +00:00
|
|
|
result = g_task_new (screenshot, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (result, shell_screenshot_pick_color);
|
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
priv = screenshot->priv;
|
|
|
|
|
2018-07-19 12:50:49 +00:00
|
|
|
priv->screenshot_area.x = x;
|
|
|
|
priv->screenshot_area.y = y;
|
|
|
|
priv->screenshot_area.width = 1;
|
|
|
|
priv->screenshot_area.height = 1;
|
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
do_grab_screenshot (screenshot,
|
|
|
|
priv->screenshot_area.x,
|
|
|
|
priv->screenshot_area.y,
|
|
|
|
1,
|
|
|
|
1,
|
|
|
|
SHELL_SCREENSHOT_FLAG_NONE);
|
2018-07-19 12:50:49 +00:00
|
|
|
|
2020-07-30 07:51:06 +00:00
|
|
|
g_task_return_boolean (result, TRUE);
|
2018-07-19 12:50:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#if G_BYTE_ORDER == G_LITTLE_ENDIAN
|
|
|
|
#define INDEX_A 3
|
|
|
|
#define INDEX_R 2
|
|
|
|
#define INDEX_G 1
|
|
|
|
#define INDEX_B 0
|
|
|
|
#else
|
|
|
|
#define INDEX_A 0
|
|
|
|
#define INDEX_R 1
|
|
|
|
#define INDEX_G 2
|
|
|
|
#define INDEX_B 3
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_screenshot_pick_color_finish:
|
|
|
|
* @screenshot: the #ShellScreenshot
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @color: (out caller-allocates): the picked color
|
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by shell_screenshot_pick_color()
|
|
|
|
* and obtain its result.
|
|
|
|
*
|
|
|
|
* Returns: whether the operation was successful
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
gboolean
|
|
|
|
shell_screenshot_pick_color_finish (ShellScreenshot *screenshot,
|
|
|
|
GAsyncResult *result,
|
|
|
|
ClutterColor *color,
|
|
|
|
GError **error)
|
|
|
|
{
|
2020-03-24 07:45:12 +00:00
|
|
|
ShellScreenshotPrivate *priv;
|
2018-07-19 12:50:49 +00:00
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
g_return_val_if_fail (SHELL_IS_SCREENSHOT (screenshot), FALSE);
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
|
|
|
g_return_val_if_fail (color != NULL, FALSE);
|
2018-07-19 12:50:49 +00:00
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_pick_color),
|
|
|
|
FALSE);
|
|
|
|
|
|
|
|
if (!g_task_propagate_boolean (G_TASK (result), error))
|
|
|
|
return FALSE;
|
|
|
|
|
2020-03-24 07:45:12 +00:00
|
|
|
priv = screenshot->priv;
|
|
|
|
|
2018-07-19 12:50:49 +00:00
|
|
|
/* protect against mutter changing the format used for stage captures */
|
|
|
|
g_assert (cairo_image_surface_get_format (priv->image) == CAIRO_FORMAT_ARGB32);
|
|
|
|
|
|
|
|
if (color)
|
|
|
|
{
|
|
|
|
uint8_t *data = cairo_image_surface_get_data (priv->image);
|
|
|
|
|
|
|
|
color->alpha = data[INDEX_A];
|
|
|
|
color->red = data[INDEX_R];
|
|
|
|
color->green = data[INDEX_G];
|
|
|
|
color->blue = data[INDEX_B];
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef INDEX_A
|
|
|
|
#undef INDEX_R
|
|
|
|
#undef INDEX_G
|
|
|
|
#undef INDEX_B
|
|
|
|
|
2022-01-15 15:20:51 +00:00
|
|
|
static void
|
2021-11-26 17:06:36 +00:00
|
|
|
composite_to_stream_on_png_saved (GObject *pixbuf,
|
2022-01-15 15:20:51 +00:00
|
|
|
GAsyncResult *result,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
GTask *task = G_TASK (user_data);
|
|
|
|
GError *error = NULL;
|
|
|
|
|
|
|
|
if (!gdk_pixbuf_save_to_stream_finish (result, &error))
|
|
|
|
g_task_return_error (task, error);
|
|
|
|
else
|
2021-11-26 17:06:36 +00:00
|
|
|
g_task_return_pointer (task, g_object_ref (pixbuf), g_object_unref);
|
2022-01-15 15:20:51 +00:00
|
|
|
|
|
|
|
g_object_unref (task);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_screenshot_composite_to_stream:
|
|
|
|
* @texture: the source texture
|
|
|
|
* @x: x coordinate of the rectangle
|
|
|
|
* @y: y coordinate of the rectangle
|
|
|
|
* @width: width of the rectangle, or -1 to use the full texture
|
|
|
|
* @height: height of the rectangle, or -1 to use the full texture
|
2021-08-16 15:12:05 +00:00
|
|
|
* @scale: scale of the source texture
|
|
|
|
* @cursor: (nullable): the cursor texture
|
|
|
|
* @cursor_x: x coordinate to put the cursor texture at, relative to the full
|
|
|
|
* source texture
|
|
|
|
* @cursor_y: y coordinate to put the cursor texture at, relative to the full
|
|
|
|
* source texture
|
|
|
|
* @cursor_scale: scale of the cursor texture
|
2022-01-15 15:20:51 +00:00
|
|
|
* @stream: the stream to write the PNG image into
|
|
|
|
* @callback: (scope async): function to call returning success or failure
|
|
|
|
* @user_data: the data to pass to callback function
|
|
|
|
*
|
|
|
|
* Composite a rectangle defined by x, y, width, height from the texture to a
|
|
|
|
* pixbuf and write it as a PNG image into the stream.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
shell_screenshot_composite_to_stream (CoglTexture *texture,
|
|
|
|
int x,
|
|
|
|
int y,
|
|
|
|
int width,
|
|
|
|
int height,
|
2021-08-16 15:12:05 +00:00
|
|
|
float scale,
|
|
|
|
CoglTexture *cursor,
|
|
|
|
int cursor_x,
|
|
|
|
int cursor_y,
|
|
|
|
float cursor_scale,
|
2022-01-15 15:20:51 +00:00
|
|
|
GOutputStream *stream,
|
|
|
|
GAsyncReadyCallback callback,
|
|
|
|
gpointer user_data)
|
|
|
|
{
|
|
|
|
CoglContext *ctx;
|
|
|
|
CoglTexture *sub_texture;
|
|
|
|
cairo_surface_t *surface;
|
2021-08-16 15:12:05 +00:00
|
|
|
cairo_surface_t *cursor_surface;
|
|
|
|
cairo_t *cr;
|
2022-01-15 15:20:51 +00:00
|
|
|
g_autoptr (GTask) task = NULL;
|
|
|
|
g_autoptr (GdkPixbuf) pixbuf = NULL;
|
|
|
|
g_autofree char *creation_time = NULL;
|
|
|
|
g_autoptr (GDateTime) date_time = NULL;
|
|
|
|
|
|
|
|
task = g_task_new (NULL, NULL, callback, user_data);
|
|
|
|
g_task_set_source_tag (task, shell_screenshot_composite_to_stream);
|
|
|
|
|
|
|
|
if (width == -1 || height == -1)
|
|
|
|
{
|
|
|
|
x = 0;
|
|
|
|
y = 0;
|
|
|
|
width = cogl_texture_get_width (texture);
|
|
|
|
height = cogl_texture_get_height (texture);
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx = clutter_backend_get_cogl_context (clutter_get_default_backend ());
|
|
|
|
sub_texture = cogl_sub_texture_new (ctx, texture, x, y, width, height);
|
|
|
|
|
|
|
|
surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
|
|
|
cogl_texture_get_width (sub_texture),
|
|
|
|
cogl_texture_get_height (sub_texture));
|
|
|
|
|
|
|
|
cogl_texture_get_data (sub_texture, CLUTTER_CAIRO_FORMAT_ARGB32,
|
|
|
|
cairo_image_surface_get_stride (surface),
|
|
|
|
cairo_image_surface_get_data (surface));
|
|
|
|
cairo_surface_mark_dirty (surface);
|
|
|
|
|
|
|
|
cogl_object_unref (sub_texture);
|
|
|
|
|
2021-08-16 15:12:05 +00:00
|
|
|
cairo_surface_set_device_scale (surface, scale, scale);
|
|
|
|
|
|
|
|
if (cursor != NULL)
|
|
|
|
{
|
|
|
|
// Paint the cursor on top.
|
|
|
|
cursor_surface =
|
|
|
|
cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
|
|
|
|
cogl_texture_get_width (cursor),
|
|
|
|
cogl_texture_get_height (cursor));
|
|
|
|
cogl_texture_get_data (cursor, CLUTTER_CAIRO_FORMAT_ARGB32,
|
|
|
|
cairo_image_surface_get_stride (cursor_surface),
|
|
|
|
cairo_image_surface_get_data (cursor_surface));
|
|
|
|
cairo_surface_mark_dirty (cursor_surface);
|
|
|
|
|
|
|
|
cairo_surface_set_device_scale (cursor_surface,
|
|
|
|
1 / cursor_scale,
|
|
|
|
1 / cursor_scale);
|
|
|
|
|
|
|
|
cr = cairo_create (surface);
|
|
|
|
cairo_set_source_surface (cr, cursor_surface,
|
|
|
|
(cursor_x - x) / scale,
|
|
|
|
(cursor_y - y) / scale);
|
|
|
|
cairo_paint (cr);
|
|
|
|
cairo_destroy (cr);
|
|
|
|
|
|
|
|
cairo_surface_destroy (cursor_surface);
|
|
|
|
}
|
|
|
|
|
2022-01-15 15:20:51 +00:00
|
|
|
// Save to an image.
|
|
|
|
pixbuf = gdk_pixbuf_get_from_surface (surface,
|
|
|
|
0, 0,
|
|
|
|
cairo_image_surface_get_width (surface),
|
|
|
|
cairo_image_surface_get_height (surface));
|
|
|
|
cairo_surface_destroy (surface);
|
|
|
|
|
|
|
|
date_time = g_date_time_new_now_local ();
|
|
|
|
creation_time = g_date_time_format (date_time, "%c");
|
|
|
|
|
|
|
|
if (!creation_time)
|
|
|
|
creation_time = g_date_time_format (date_time, "%FT%T%z");
|
|
|
|
|
|
|
|
gdk_pixbuf_save_to_stream_async (pixbuf, stream, "png", NULL,
|
|
|
|
composite_to_stream_on_png_saved,
|
|
|
|
g_steal_pointer (&task),
|
|
|
|
"tEXt::Software", "gnome-screenshot",
|
|
|
|
"tEXt::Creation Time", creation_time,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* shell_screenshot_composite_to_stream_finish:
|
|
|
|
* @result: the #GAsyncResult that was provided to the callback
|
|
|
|
* @error: #GError for error reporting
|
|
|
|
*
|
|
|
|
* Finish the asynchronous operation started by
|
|
|
|
* shell_screenshot_composite_to_stream () and obtain its result.
|
|
|
|
*
|
2021-11-26 17:06:36 +00:00
|
|
|
* Returns: (transfer full) (nullable): a GdkPixbuf with the final image if the
|
|
|
|
* operation was successful, or NULL on error.
|
2022-01-15 15:20:51 +00:00
|
|
|
*
|
|
|
|
*/
|
2021-11-26 17:06:36 +00:00
|
|
|
GdkPixbuf *
|
2022-01-15 15:20:51 +00:00
|
|
|
shell_screenshot_composite_to_stream_finish (GAsyncResult *result,
|
|
|
|
GError **error)
|
|
|
|
{
|
|
|
|
g_return_val_if_fail (G_IS_TASK (result), FALSE);
|
|
|
|
g_return_val_if_fail (g_async_result_is_tagged (result,
|
|
|
|
shell_screenshot_composite_to_stream),
|
|
|
|
FALSE);
|
|
|
|
|
2021-11-26 17:06:36 +00:00
|
|
|
return g_task_propagate_pointer (G_TASK (result), error);
|
2022-01-15 15:20:51 +00:00
|
|
|
}
|
|
|
|
|
2012-02-14 20:57:34 +00:00
|
|
|
ShellScreenshot *
|
|
|
|
shell_screenshot_new (void)
|
|
|
|
{
|
|
|
|
return g_object_new (SHELL_TYPE_SCREENSHOT, NULL);
|
|
|
|
}
|