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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
|
/* gsd-screenshot-utils.c - utilities to take screenshots
*
* Copyright (C) 2012 Red Hat, Inc.
*
* Adapted from gnome-screenshot code, which is
* Copyright (C) 2001-2006 Jonathan Blandford <jrb@alum.mit.edu>
* Copyright (C) 2006 Emmanuele Bassi <ebassi@gnome.org>
* Copyright (C) 2008-2012 Cosimo Cecchi <cosimoc@gnome.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 2 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, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
*/
#include <config.h>
#include <canberra-gtk.h>
#include <gtk/gtk.h>
#include <gio/gio.h>
#include <glib/gi18n.h>
#include <string.h>
#include <glib/gstdio.h>
#include "gsd-screenshot-utils.h"
#define SHELL_SCREENSHOT_BUS_NAME "org.gnome.Shell"
#define SHELL_SCREENSHOT_BUS_PATH "/org/gnome/Shell/Screenshot"
#define SHELL_SCREENSHOT_BUS_IFACE "org.gnome.Shell.Screenshot"
typedef enum {
SCREENSHOT_TYPE_SCREEN,
SCREENSHOT_TYPE_WINDOW,
SCREENSHOT_TYPE_AREA
} ScreenshotType;
typedef struct {
ScreenshotType type;
gboolean copy_to_clipboard;
GdkRectangle area_selection;
gchar *save_filename;
gchar *used_filename;
GDBusConnection *connection;
} ScreenshotContext;
static void
screenshot_play_sound_effect (const gchar *event_id,
const gchar *event_desc)
{
ca_context *c;
c = ca_gtk_context_get ();
ca_context_play (c, 0,
CA_PROP_EVENT_ID, event_id,
CA_PROP_EVENT_DESCRIPTION, event_desc,
CA_PROP_CANBERRA_CACHE_CONTROL, "permanent",
NULL);
}
static void
screenshot_context_free (ScreenshotContext *ctx)
{
g_free (ctx->save_filename);
g_free (ctx->used_filename);
g_clear_object (&ctx->connection);
g_slice_free (ScreenshotContext, ctx);
}
static void
screenshot_play_error_sound_effect (void)
{
screenshot_play_sound_effect ("dialog-error", _("Unable to capture a screenshot"));
}
static void
screenshot_save_to_recent (ScreenshotContext *ctx)
{
GFile *file = g_file_new_for_path (ctx->used_filename);
gchar *uri = g_file_get_uri (file);
gtk_recent_manager_add_item (gtk_recent_manager_get_default (), uri);
g_free (uri);
g_object_unref (file);
}
static void
bus_call_ready_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
ScreenshotContext *ctx = user_data;
GVariant *variant;
gboolean success;
variant = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source), res, &error);
if (error != NULL)
{
screenshot_play_error_sound_effect ();
g_warning ("Failed to save a screenshot: %s\n", error->message);
g_error_free (error);
screenshot_context_free (ctx);
return;
}
g_variant_get (variant, "(bs)", &success, &ctx->used_filename);
if (success)
{
if (!ctx->copy_to_clipboard)
{
screenshot_play_sound_effect ("screen-capture", _("Screenshot taken"));
screenshot_save_to_recent (ctx);
}
}
screenshot_context_free (ctx);
g_variant_unref (variant);
}
static void
screenshot_call_shell (ScreenshotContext *ctx)
{
const gchar *method_name;
GVariant *method_params;
if (ctx->type == SCREENSHOT_TYPE_SCREEN)
{
method_name = "Screenshot";
method_params = g_variant_new ("(bbs)",
FALSE, /* include pointer */
TRUE, /* flash */
ctx->save_filename);
}
else if (ctx->type == SCREENSHOT_TYPE_WINDOW)
{
method_name = "ScreenshotWindow";
method_params = g_variant_new ("(bbbs)",
TRUE, /* include border */
FALSE, /* include pointer */
TRUE, /* flash */
ctx->save_filename);
}
else
{
method_name = "ScreenshotArea";
method_params = g_variant_new ("(iiiibs)",
ctx->area_selection.x, ctx->area_selection.y,
ctx->area_selection.width, ctx->area_selection.height,
TRUE, /* flash */
ctx->save_filename);
}
g_dbus_connection_call (ctx->connection,
SHELL_SCREENSHOT_BUS_NAME,
SHELL_SCREENSHOT_BUS_PATH,
SHELL_SCREENSHOT_BUS_IFACE,
method_name,
method_params,
NULL,
G_DBUS_CALL_FLAGS_NO_AUTO_START,
-1,
NULL,
bus_call_ready_cb,
ctx);
}
static void
area_selection_ready_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GdkRectangle rectangle;
ScreenshotContext *ctx = user_data;
GVariant *geometry;
geometry = g_dbus_connection_call_finish (G_DBUS_CONNECTION (source),
res, NULL);
/* cancelled by the user */
if (!geometry)
{
screenshot_context_free (ctx);
return;
}
g_variant_get (geometry, "(iiii)",
&rectangle.x, &rectangle.y,
&rectangle.width, &rectangle.height);
ctx->area_selection = rectangle;
screenshot_call_shell (ctx);
g_variant_unref (geometry);
}
static void
bus_connection_ready_cb (GObject *source,
GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
ScreenshotContext *ctx = user_data;
ctx->connection = g_bus_get_finish (res, &error);
if (error != NULL)
{
screenshot_play_error_sound_effect ();
g_warning ("Failed to save a screenshot: %s\n", error->message);
g_error_free (error);
screenshot_context_free (ctx);
return;
}
if (ctx->type == SCREENSHOT_TYPE_AREA)
g_dbus_connection_call (ctx->connection,
SHELL_SCREENSHOT_BUS_NAME,
SHELL_SCREENSHOT_BUS_PATH,
SHELL_SCREENSHOT_BUS_IFACE,
"SelectArea",
NULL,
NULL,
G_DBUS_CALL_FLAGS_NO_AUTO_START,
-1,
NULL,
area_selection_ready_cb,
ctx);
else
screenshot_call_shell (ctx);
}
static void
screenshot_take (ScreenshotContext *ctx)
{
g_bus_get (G_BUS_TYPE_SESSION, NULL, bus_connection_ready_cb, ctx);
}
static gchar *
screenshot_build_filename (void)
{
char *file_name, *origin;
GDateTime *d;
d = g_date_time_new_now_local ();
origin = g_date_time_format (d, "%Y-%m-%d %H-%M-%S");
g_date_time_unref (d);
/* translators: this is the name of the file that gets made up
* with the screenshot */
file_name = g_strdup_printf (_("Screenshot from %s"), origin);
g_free (origin);
return file_name;
}
static void
screenshot_check_name_ready (ScreenshotContext *ctx)
{
if (ctx->copy_to_clipboard)
ctx->save_filename = g_strdup ("");
else
ctx->save_filename = screenshot_build_filename ();
screenshot_take (ctx);
}
void
gsd_screenshot_take (MediaKeyType key_type)
{
ScreenshotContext *ctx = g_slice_new0 (ScreenshotContext);
ctx->copy_to_clipboard = (key_type == SCREENSHOT_CLIP_KEY ||
key_type == WINDOW_SCREENSHOT_CLIP_KEY ||
key_type == AREA_SCREENSHOT_CLIP_KEY);
switch (key_type)
{
case SCREENSHOT_KEY:
case SCREENSHOT_CLIP_KEY:
ctx->type = SCREENSHOT_TYPE_SCREEN;
break;
case WINDOW_SCREENSHOT_KEY:
case WINDOW_SCREENSHOT_CLIP_KEY:
ctx->type = SCREENSHOT_TYPE_WINDOW;
break;
case AREA_SCREENSHOT_KEY:
case AREA_SCREENSHOT_CLIP_KEY:
ctx->type = SCREENSHOT_TYPE_AREA;
break;
default:
g_assert_not_reached ();
break;
}
screenshot_check_name_ready (ctx);
}
|