-
-
Notifications
You must be signed in to change notification settings - Fork 76
/
ScreenshotManager.vala
466 lines (372 loc) · 18.3 KB
/
ScreenshotManager.vala
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//
// Copyright (C) 2016 Rico Tzschichholz, Santiago León O.
//
// 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 <http://www.gnu.org/licenses/>.
//
namespace Gala {
private const string EXTENSION = ".png";
private const int UNCONCEAL_TEXT_TIMEOUT = 2000;
[DBus (name="org.gnome.Shell.Screenshot")]
public class ScreenshotManager : Object {
private static ScreenshotManager? instance;
[DBus (visible = false)]
public static unowned ScreenshotManager init (WindowManager wm) {
if (instance == null)
instance = new ScreenshotManager (wm);
return instance;
}
private WindowManager wm;
private Settings desktop_settings;
private string prev_font_regular;
private string prev_font_document;
private string prev_font_mono;
private uint conceal_timeout;
construct {
desktop_settings = new Settings ("org.gnome.desktop.interface");
}
private ScreenshotManager (WindowManager _wm) {
wm = _wm;
}
public void flash_area (int x, int y, int width, int height) throws DBusError, IOError {
debug ("Flashing area");
double[] keyframes = { 0.3f, 0.8f };
GLib.Value[] values = { 180U, 0U };
var transition = new Clutter.KeyframeTransition ("opacity") {
duration = 200,
remove_on_complete = true,
progress_mode = Clutter.AnimationMode.LINEAR
};
transition.set_key_frames (keyframes);
transition.set_values (values);
transition.set_to_value (0.0f);
var flash_actor = new Clutter.Actor ();
flash_actor.set_size (width, height);
flash_actor.set_position (x, y);
flash_actor.set_background_color (Clutter.Color.get_static (Clutter.StaticColor.WHITE));
flash_actor.set_opacity (0);
flash_actor.transitions_completed.connect ((actor) => {
wm.ui_group.remove_child (actor);
actor.destroy ();
});
wm.ui_group.add_child (flash_actor);
flash_actor.add_transition ("flash", transition);
}
public async void screenshot (bool include_cursor, bool flash, string filename, out bool success, out string filename_used) throws DBusError, IOError {
debug ("Taking screenshot");
unowned var display = wm.get_display ();
int width, height;
display.get_size (out width, out height);
var image = take_screenshot (0, 0, width, height, include_cursor);
unconceal_text ();
if (flash) {
flash_area (0, 0, width, height);
}
var scale = display.get_monitor_scale (display.get_primary_monitor ());
success = yield save_image (image, filename, scale, out filename_used);
if (success) {
play_shutter_sound ();
}
}
public async void screenshot_area (int x, int y, int width, int height, bool flash, string filename, out bool success, out string filename_used) throws DBusError, IOError {
yield screenshot_area_with_cursor (x, y, width, height, false, flash, filename, out success, out filename_used);
}
public async void screenshot_area_with_cursor (int x, int y, int width, int height, bool include_cursor, bool flash, string filename, out bool success, out string filename_used) throws DBusError, IOError {
debug ("Taking area screenshot");
yield wait_stage_repaint ();
var image = take_screenshot (x, y, width, height, include_cursor);
unconceal_text ();
if (flash) {
flash_area (x, y, width, height);
}
Meta.Rectangle rect = { x, y, width, height };
unowned var display = wm.get_display ();
var scale = display.get_monitor_scale (display.get_monitor_index_for_rect (rect));
success = yield save_image (image, filename, scale, out filename_used);
if (success) {
play_shutter_sound ();
} else {
throw new DBusError.FAILED ("Failed to save image");
}
}
public async void screenshot_window (bool include_frame, bool include_cursor, bool flash, string filename, out bool success, out string filename_used) throws DBusError, IOError {
debug ("Taking window screenshot");
var window = wm.get_display ().get_focus_window ();
if (window == null) {
unconceal_text ();
throw new DBusError.FAILED ("Cannot find active window");
}
var window_actor = (Meta.WindowActor) window.get_compositor_private ();
unowned Meta.ShapedTexture window_texture = (Meta.ShapedTexture) window_actor.get_texture ();
float actor_x, actor_y;
window_actor.get_position (out actor_x, out actor_y);
var rect = window.get_frame_rect ();
if ((include_frame && window.is_client_decorated ()) ||
(!include_frame && !window.is_client_decorated ())) {
rect = window.frame_rect_to_client_rect (rect);
}
Cairo.RectangleInt clip = { rect.x - (int) actor_x, rect.y - (int) actor_y, rect.width, rect.height };
var image = (Cairo.ImageSurface) window_texture.get_image (clip);
if (include_cursor) {
image = composite_stage_cursor (image, { rect.x, rect.y, rect.width, rect.height });
}
unconceal_text ();
if (flash) {
flash_area (rect.x, rect.y, rect.width, rect.height);
}
unowned var display = wm.get_display ();
var scale = display.get_monitor_scale (display.get_monitor_index_for_rect (rect));
success = yield save_image (image, filename, scale, out filename_used);
if (success) {
play_shutter_sound ();
}
}
public async void select_area (out int x, out int y, out int width, out int height) throws DBusError, IOError {
var selection_area = new SelectionArea (wm);
selection_area.closed.connect (() => Idle.add (select_area.callback));
wm.ui_group.add (selection_area);
selection_area.start_selection ();
yield;
selection_area.destroy ();
if (selection_area.cancelled) {
throw new GLib.IOError.CANCELLED ("Operation was cancelled");
}
yield wait_stage_repaint ();
var rect = selection_area.get_selection_rectangle ();
x = (int) GLib.Math.roundf (rect.origin.x);
y = (int) GLib.Math.roundf (rect.origin.y);
width = (int) GLib.Math.roundf (rect.size.width);
height = (int) GLib.Math.roundf (rect.size.height);
}
private void unconceal_text () {
if (conceal_timeout == 0) {
return;
}
desktop_settings.set_string ("font-name", prev_font_regular);
desktop_settings.set_string ("monospace-font-name", prev_font_mono);
desktop_settings.set_string ("document-font-name", prev_font_document);
Source.remove (conceal_timeout);
conceal_timeout = 0;
}
public async void conceal_text () throws DBusError, IOError {
if (conceal_timeout > 0) {
Source.remove (conceal_timeout);
} else {
prev_font_regular = desktop_settings.get_string ("font-name");
prev_font_mono = desktop_settings.get_string ("monospace-font-name");
prev_font_document = desktop_settings.get_string ("document-font-name");
desktop_settings.set_string ("font-name", "Redacted Script Regular 9");
desktop_settings.set_string ("monospace-font-name", "Redacted Script Light 10");
desktop_settings.set_string ("document-font-name", "Redacted Script Regular 10");
}
conceal_timeout = Timeout.add (UNCONCEAL_TEXT_TIMEOUT, () => {
unconceal_text ();
return Source.REMOVE;
});
}
public async GLib.HashTable<string, Variant> pick_color () throws DBusError, IOError {
var pixel_picker = new PixelPicker (wm);
pixel_picker.closed.connect (() => Idle.add (pick_color.callback));
wm.ui_group.add (pixel_picker);
pixel_picker.start_selection ();
yield;
pixel_picker.destroy ();
if (pixel_picker.cancelled) {
throw new GLib.IOError.CANCELLED ("Operation was cancelled");
}
var picker_point = pixel_picker.point;
var image = take_screenshot (
(int) GLib.Math.roundf (picker_point.x),
(int) GLib.Math.roundf (picker_point.y),
1, 1,
false
);
assert (image.get_format () == Cairo.Format.ARGB32);
unowned uchar[] data = image.get_data ();
double r, g, b;
if (GLib.ByteOrder.HOST == GLib.ByteOrder.LITTLE_ENDIAN) {
r = data[2] / 255.0f;
g = data[1] / 255.0f;
b = data[0] / 255.0f;
} else {
r = data[1] / 255.0f;
g = data[2] / 255.0f;
b = data[3] / 255.0f;
}
var result = new GLib.HashTable<string, Variant> (str_hash, str_equal);
result.insert ("color", new GLib.Variant ("(ddd)", r, g, b));
return result;
}
private static string find_target_path () {
// Try to create dedicated "Screenshots" subfolder in PICTURES xdg-dir
unowned string? base_path = Environment.get_user_special_dir (UserDirectory.PICTURES);
if (base_path != null && FileUtils.test (base_path, FileTest.EXISTS)) {
var path = Path.build_path (Path.DIR_SEPARATOR_S, base_path, _("Screenshots"));
if (FileUtils.test (path, FileTest.EXISTS)) {
return path;
} else if (DirUtils.create (path, 0755) == 0) {
return path;
} else {
return base_path;
}
}
return Environment.get_home_dir ();
}
private static async bool save_image (Cairo.ImageSurface image, string filename, float scale, out string used_filename) {
return (filename != "")
? yield save_image_to_file (image, filename, scale, out used_filename)
: save_image_to_clipboard (image, filename, out used_filename);
}
private static async bool save_image_to_file (Cairo.ImageSurface image, string filename, float scale, out string used_filename) {
used_filename = filename;
// We only alter non absolute filename because absolute
// filename is used for temp clipboard file and shouldn't be changed
if (!Path.is_absolute (used_filename)) {
if (!used_filename.has_suffix (EXTENSION)) {
used_filename = used_filename.concat (EXTENSION);
}
if (scale > 1) {
var scale_pos = -EXTENSION.length;
used_filename = used_filename.splice (scale_pos, scale_pos, "@%.1gx".printf (scale));
}
var path = find_target_path ();
used_filename = Path.build_filename (path, used_filename, null);
}
try {
var screenshot = Gdk.pixbuf_get_from_surface (image, 0, 0, image.get_width (), image.get_height ());
var file = File.new_for_path (used_filename);
FileIOStream stream;
if (file.query_exists ()) {
stream = yield file.open_readwrite_async (FileCreateFlags.NONE);
} else {
stream = yield file.create_readwrite_async (FileCreateFlags.NONE);
}
yield screenshot.save_to_stream_async (stream.output_stream, "png");
return true;
} catch (GLib.Error e) {
warning ("could not save file: %s", e.message);
return false;
}
}
private static bool save_image_to_clipboard (Cairo.ImageSurface image, string filename, out string used_filename) {
used_filename = filename;
unowned Gdk.Display display = Gdk.Display.get_default ();
unowned Gtk.Clipboard clipboard = Gtk.Clipboard.get_default (display);
var screenshot = Gdk.pixbuf_get_from_surface (image, 0, 0, image.get_width (), image.get_height ());
if (screenshot == null) {
warning ("could not save screenshot to clipboard: null pixbuf");
return false;
}
clipboard.set_image (screenshot);
return true;
}
private void play_shutter_sound () {
Canberra.Context context;
Canberra.Proplist props;
Canberra.Context.create (out context);
Canberra.Proplist.create (out props);
props.sets (Canberra.PROP_EVENT_ID, "screen-capture");
props.sets (Canberra.PROP_EVENT_DESCRIPTION, _("Screenshot taken"));
props.sets (Canberra.PROP_CANBERRA_CACHE_CONTROL, "permanent");
context.play_full (0, props, null);
}
private Cairo.ImageSurface take_screenshot (int x, int y, int width, int height, bool include_cursor) {
Cairo.ImageSurface image;
int image_width, image_height;
float scale;
wm.stage.get_capture_final_size ({x, y, width, height}, out image_width, out image_height, out scale);
image = new Cairo.ImageSurface (Cairo.Format.ARGB32, image_width, image_height);
var paint_flags = Clutter.PaintFlag.NO_CURSORS;
if (include_cursor) {
paint_flags |= Clutter.PaintFlag.FORCE_CURSORS;
}
try {
if (GLib.ByteOrder.HOST == GLib.ByteOrder.LITTLE_ENDIAN) {
wm.stage.paint_to_buffer (
{x, y, width, height},
scale,
image.get_data (),
image.get_stride (),
Cogl.PixelFormat.BGRA_8888_PRE,
paint_flags
);
} else {
wm.stage.paint_to_buffer (
{x, y, width, height},
scale,
image.get_data (),
image.get_stride (),
Cogl.PixelFormat.ARGB_8888_PRE,
paint_flags
);
}
} catch (Error e) {
warning (e.message);
}
return image;
}
private Cairo.ImageSurface composite_capture_images (Clutter.Capture[] captures, int x, int y, int width, int height) {
var image = new Cairo.ImageSurface (captures[0].image.get_format (), width, height);
var cr = new Cairo.Context (image);
foreach (unowned Clutter.Capture capture in captures) {
// Ignore capture regions with scale other than 1 for now; mutter can't
// produce them yet, so there is no way to test them.
double capture_scale = 1.0;
capture.image.get_device_scale (out capture_scale, null);
if (capture_scale != 1.0)
continue;
cr.save ();
cr.translate (capture.rect.x - x, capture.rect.y - y);
cr.set_source_surface (capture.image, 0, 0);
cr.restore ();
}
return image;
}
private Cairo.ImageSurface composite_stage_cursor (Cairo.ImageSurface image, Cairo.RectangleInt image_rect) {
unowned Meta.CursorTracker cursor_tracker = wm.get_display ().get_cursor_tracker ();
Graphene.Point coords = {};
cursor_tracker.get_pointer (out coords, null);
var region = new Cairo.Region.rectangle (image_rect);
if (!region.contains_point ((int) coords.x, (int) coords.y)) {
return image;
}
unowned Cogl.Texture texture = cursor_tracker.get_sprite ();
if (texture == null) {
return image;
}
int width = (int)texture.get_width ();
int height = (int)texture.get_height ();
uint8[] data = new uint8[width * height * 4];
texture.get_data (Cogl.PixelFormat.RGBA_8888, 0, data);
var cursor_image = new Cairo.ImageSurface.for_data (data, Cairo.Format.ARGB32, width, height, width * 4);
var target = new Cairo.ImageSurface (Cairo.Format.ARGB32, image_rect.width, image_rect.height);
var cr = new Cairo.Context (target);
cr.set_operator (Cairo.Operator.OVER);
cr.set_source_surface (image, 0, 0);
cr.paint ();
cr.set_operator (Cairo.Operator.OVER);
cr.set_source_surface (cursor_image, coords.x - image_rect.x, coords.y - image_rect.y);
cr.paint ();
return (Cairo.ImageSurface)cr.get_target ();
}
private async void wait_stage_repaint () {
ulong signal_id = 0UL;
signal_id = wm.stage.after_paint.connect (() => {
wm.stage.disconnect (signal_id);
Idle.add (wait_stage_repaint.callback);
});
wm.stage.queue_redraw ();
yield;
}
}
}