summaryrefslogtreecommitdiffstats
path: root/plug-ins/screenshot/screenshot-gnome-shell.c
blob: 8fd531a1beea80522b126ff2ca3f14c04d0cec0c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/* GIMP - The GNU Image Manipulation Program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Screenshot plug-in
 * Copyright 1998-2007 Sven Neumann <sven@gimp.org>
 * Copyright 2003      Henrik Brix Andersen <brix@gimp.org>
 * Copyright 2016      Michael Natterer <mitch@gimp.org>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include "config.h"

#include <glib.h>
#include <glib/gstdio.h> /* g_unlink() */

#include <libgimp/gimp.h>
#include <libgimp/gimpui.h>

#include "screenshot.h"
#include "screenshot-gnome-shell.h"


static GDBusProxy *proxy = NULL;


gboolean
screenshot_gnome_shell_available (void)
{
  proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SESSION,
                                         G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START,
                                         NULL,
                                         "org.gnome.Shell.Screenshot",
                                         "/org/gnome/Shell/Screenshot",
                                         "org.gnome.Shell.Screenshot",
                                         NULL, NULL);

  if (proxy)
    {
      GError *error = NULL;

      g_dbus_proxy_call_sync (proxy, "org.freedesktop.DBus.Peer.Ping",
                              NULL,
                              G_DBUS_CALL_FLAGS_NONE,
                              -1, NULL, &error);
      if (! error)
        return TRUE;

      g_clear_error (&error);

      g_object_unref (proxy);
      proxy = NULL;
    }

  return FALSE;
}

ScreenshotCapabilities
screenshot_gnome_shell_get_capabilities (void)
{
  return (SCREENSHOT_CAN_SHOOT_DECORATIONS |
          SCREENSHOT_CAN_SHOOT_POINTER     |
          SCREENSHOT_CAN_SHOOT_REGION      |
          SCREENSHOT_CAN_SHOOT_WINDOW);
}

GimpPDBStatusType
screenshot_gnome_shell_shoot (ScreenshotValues  *shootvals,
                              GdkScreen         *screen,
                              gint32            *image_ID,
                              GError           **error)
{
  gchar       *filename;
  const gchar *method = NULL;
  GVariant    *args   = NULL;
  GVariant    *retval;
  gint         monitor = shootvals->monitor;
  gboolean     success;

  filename = gimp_temp_name ("png");

  switch (shootvals->shoot_type)
    {
    case SHOOT_ROOT:
      if (shootvals->screenshot_delay > 0)
        screenshot_delay (shootvals->screenshot_delay);

      method = "Screenshot";
      args   = g_variant_new ("(bbs)",
                              shootvals->show_cursor,
                              TRUE, /* flash */
                              filename);

      /* FIXME: figure profile */
      break;

    case SHOOT_REGION:
      if (shootvals->select_delay > 0)
        screenshot_delay (shootvals->select_delay);

      retval = g_dbus_proxy_call_sync (proxy, "SelectArea", NULL,
                                       G_DBUS_CALL_FLAGS_NONE,
                                       -1, NULL, error);
      if (! retval)
        goto failure;

      g_variant_get (retval, "(iiii)",
                     &shootvals->x1,
                     &shootvals->y1,
                     &shootvals->x2,
                     &shootvals->y2);
      g_variant_unref (retval);

      shootvals->x2 += shootvals->x1;
      shootvals->y2 += shootvals->y1;

      method = "ScreenshotArea";
      args   = g_variant_new ("(iiiibs)",
                              shootvals->x1,
                              shootvals->y1,
                              shootvals->x2 - shootvals->x1,
                              shootvals->y2 - shootvals->y1,
                              TRUE, /* flash */
                              filename);

      monitor =
        gdk_screen_get_monitor_at_point (screen,
                                         (shootvals->x1 + shootvals->x2) / 2,
                                         (shootvals->y1 + shootvals->y2) / 2);

      if (shootvals->screenshot_delay > 0)
        screenshot_delay (shootvals->screenshot_delay);
      break;

    case SHOOT_WINDOW:
      if (shootvals->screenshot_delay > 0)
        screenshot_delay (shootvals->screenshot_delay);

      method = "ScreenshotWindow";
      args   = g_variant_new ("(bbbs)",
                              shootvals->decorate,
                              shootvals->show_cursor,
                              TRUE, /* flash */
                              filename);

      /* FIXME: figure monitor */
      break;
    }

  g_free (filename);
  filename = NULL;

  retval = g_dbus_proxy_call_sync (proxy, method, args,
                                   G_DBUS_CALL_FLAGS_NONE,
                                   -1, NULL, error);
  if (! retval)
    goto failure;

  g_variant_get (retval, "(bs)",
                 &success,
                 &filename);

  g_variant_unref (retval);

  if (success && filename)
    {
      GimpColorProfile *profile;

      *image_ID = gimp_file_load (GIMP_RUN_NONINTERACTIVE,
                                  filename, filename);
      gimp_image_set_filename (*image_ID, "screenshot.png");

      profile = gimp_screen_get_color_profile (screen, monitor);

      if (profile)
        {
          gimp_image_set_color_profile (*image_ID, profile);
          g_object_unref (profile);
        }

      g_unlink (filename);
      g_free (filename);

      g_object_unref (proxy);
      proxy = NULL;

      return GIMP_PDB_SUCCESS;
    }

 failure:

  g_free (filename);

  g_object_unref (proxy);
  proxy = NULL;

  return GIMP_PDB_EXECUTION_ERROR;
}