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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
|
/* screenshot-utils.c - common functions for MATE Screenshot
*
* Copyright (C) 2001-2006 Jonathan Blandford <jrb@alum.mit.edu>
* Copyright (C) 2008 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., 51 Franklin St, Fifth Floor,
*/
#include "config.h"
#include "screenshot-utils.h"
#include <X11/Xatom.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
#include <glib.h>
#include <glib/gi18n.h>
#ifdef HAVE_X11_EXTENSIONS_SHAPE_H
#include <X11/extensions/shape.h>
#endif
static GtkWidget *selection_window;
#define SELECTION_NAME "_MATE_PANEL_SCREENSHOT"
static char *
get_utf8_property (GdkWindow *window,
GdkAtom atom)
{
gboolean res;
GdkAtom utf8_string;
GdkAtom type;
int actual_format, actual_length;
guchar *data;
char *retval;
utf8_string = gdk_x11_xatom_to_atom (gdk_x11_get_xatom_by_name ("UTF8_STRING"));
res = gdk_property_get (window, atom, utf8_string,
0, G_MAXLONG, FALSE,
&type,
&actual_format, &actual_length,
&data);
if (!res)
return NULL;
if (type != utf8_string || actual_format != 8 || actual_length == 0)
{
g_free (data);
return NULL;
}
if (!g_utf8_validate ((gchar *) data, actual_length, NULL))
{
char *atom_name = gdk_atom_name (atom);
g_warning ("Property `%s' (format: %d, length: %d) contained "
"invalid UTF-8",
atom_name,
actual_format,
actual_length);
g_free (atom_name);
g_free (data);
return NULL;
}
retval = g_strndup ((gchar *) data, actual_length);
g_free (data);
return retval;
}
/* To make sure there is only one screenshot taken at a time,
* (Imagine key repeat for the print screen key) we hold a selection
* until we are done taking the screenshot
*/
gboolean
screenshot_grab_lock (void)
{
GdkAtom selection_atom;
gboolean result = FALSE;
selection_atom = gdk_atom_intern (SELECTION_NAME, FALSE);
gdk_x11_grab_server ();
if (gdk_selection_owner_get (selection_atom) != NULL)
goto out;
selection_window = gtk_invisible_new ();
gtk_widget_show (selection_window);
if (!gtk_selection_owner_set (selection_window,
gdk_atom_intern (SELECTION_NAME, FALSE),
GDK_CURRENT_TIME))
{
gtk_widget_destroy (selection_window);
selection_window = NULL;
goto out;
}
result = TRUE;
out:
gdk_x11_ungrab_server ();
gdk_flush ();
return result;
}
void
screenshot_release_lock (void)
{
if (selection_window)
{
gtk_widget_destroy (selection_window);
selection_window = NULL;
}
gdk_flush ();
}
static GdkWindow *
screen_get_active_window (GdkScreen *screen)
{
GdkWindow *ret = NULL;
Atom type_return;
gint format_return;
gulong nitems_return;
gulong bytes_after_return;
guchar *data = NULL;
if (!gdk_x11_screen_supports_net_wm_hint (screen,
gdk_atom_intern_static_string ("_NET_ACTIVE_WINDOW")))
return NULL;
if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen)),
RootWindow (GDK_DISPLAY_XDISPLAY (gdk_screen_get_display (screen)),
GDK_SCREEN_XNUMBER (screen)),
gdk_x11_get_xatom_by_name_for_display (gdk_screen_get_display (screen),
"_NET_ACTIVE_WINDOW"),
0, 1, False, XA_WINDOW, &type_return,
&format_return, &nitems_return,
&bytes_after_return, &data)
== Success)
{
if ((type_return == XA_WINDOW) && (format_return == 32) && (data))
{
Window window = *(Window *) data;
if (window != None)
{
ret = gdk_x11_window_foreign_new_for_display (gdk_screen_get_display (screen),
window);
}
}
}
if (data)
XFree (data);
return ret;
}
static GdkWindow *
screenshot_find_active_window (void)
{
GdkWindow *window;
GdkScreen *default_screen;
default_screen = gdk_screen_get_default ();
window = screen_get_active_window (default_screen);
return window;
}
static gboolean
screenshot_window_is_desktop (GdkWindow *window)
{
GdkWindow *root_window = gdk_get_default_root_window ();
GdkWindowTypeHint window_type_hint;
if (window == root_window)
return TRUE;
window_type_hint = gdk_window_get_type_hint (window);
if (window_type_hint == GDK_WINDOW_TYPE_HINT_DESKTOP)
return TRUE;
return FALSE;
}
#define MAXIMUM_WM_REPARENTING_DEPTH 4
static GdkWindow *
look_for_hint_helper (GdkWindow *window,
GdkAtom property,
int depth)
{
gboolean res;
GdkAtom actual_type;
int actual_format, actual_length;
guchar *data;
res = gdk_property_get (window, property, GDK_NONE,
0, 1, FALSE,
&actual_type,
&actual_format, &actual_length,
&data);
if (res == TRUE &&
data != NULL &&
actual_format == 32 &&
data[0] == 1)
{
g_free (data);
return window;
}
if (depth < MAXIMUM_WM_REPARENTING_DEPTH)
{
GList *children, *l;
children = gdk_window_get_children (window);
if (children != NULL)
{
for (l = children; l; l = l->next)
{
window = look_for_hint_helper (l->data, property, depth + 1);
if (window)
break;
}
g_list_free (children);
if (window)
return window;
}
}
return NULL;
}
static GdkWindow *
look_for_hint (GdkWindow *window,
GdkAtom property)
{
GdkWindow *retval;
retval = look_for_hint_helper (window, property, 0);
return retval;
}
GdkWindow *
screenshot_find_current_window ()
{
GdkWindow *current_window;
#if GTK_CHECK_VERSION (3, 20, 0)
GdkDisplay *display;
GdkSeat *seat;
#else
GdkDeviceManager *manager;
#endif
GdkDevice *device;
current_window = screenshot_find_active_window ();
#if GTK_CHECK_VERSION (3, 20, 0)
display = gdk_window_get_display (current_window);
seat = gdk_display_get_default_seat (display);
device = gdk_seat_get_pointer (seat);
#else
manager = gdk_display_get_device_manager (gdk_display_get_default ());
device = gdk_device_manager_get_client_pointer (manager);
#endif
/* If there's no active window, we fall back to returning the
* window that the cursor is in.
*/
if (!current_window)
current_window = gdk_device_get_window_at_position (device, NULL, NULL);
if (current_window)
{
if (screenshot_window_is_desktop (current_window))
/* if the current window is the desktop (e.g. caja), we
* return NULL, as getting the whole screen makes more sense.
*/
return NULL;
/* Once we have a window, we take the toplevel ancestor. */
current_window = gdk_window_get_toplevel (current_window);
}
return current_window;
}
typedef struct {
GdkRectangle rect;
gboolean button_pressed;
GtkWidget *window;
} select_area_filter_data;
static gboolean
select_area_button_press (GtkWidget *window,
GdkEventButton *event,
select_area_filter_data *data)
{
if (data->button_pressed)
return TRUE;
data->button_pressed = TRUE;
data->rect.x = event->x_root;
data->rect.y = event->y_root;
return TRUE;
}
static gboolean
select_area_motion_notify (GtkWidget *window,
GdkEventMotion *event,
select_area_filter_data *data)
{
GdkRectangle draw_rect;
if (!data->button_pressed)
return TRUE;
draw_rect.width = ABS (data->rect.x - event->x_root);
draw_rect.height = ABS (data->rect.y - event->y_root);
draw_rect.x = MIN (data->rect.x, event->x_root);
draw_rect.y = MIN (data->rect.y, event->y_root);
if (draw_rect.width <= 0 || draw_rect.height <= 0)
{
gtk_window_move (GTK_WINDOW (window), -100, -100);
gtk_window_resize (GTK_WINDOW (window), 10, 10);
return TRUE;
}
gtk_window_move (GTK_WINDOW (window), draw_rect.x, draw_rect.y);
gtk_window_resize (GTK_WINDOW (window), draw_rect.width, draw_rect.height);
/* We (ab)use app-paintable to indicate if we have an RGBA window */
if (!gtk_widget_get_app_paintable (window))
{
GdkWindow *gdkwindow = gtk_widget_get_window (window);
/* Shape the window to make only the outline visible */
if (draw_rect.width > 2 && draw_rect.height > 2)
{
cairo_region_t *region, *region2;
cairo_rectangle_int_t region_rect = {
0, 0,
draw_rect.width - 2, draw_rect.height - 2
};
region = cairo_region_create_rectangle (®ion_rect);
region_rect.x++;
region_rect.y++;
region_rect.width -= 2;
region_rect.height -= 2;
region2 = cairo_region_create_rectangle (®ion_rect);
cairo_region_subtract (region, region2);
gdk_window_shape_combine_region (gdkwindow, region, 0, 0);
cairo_region_destroy (region);
cairo_region_destroy (region2);
}
else
gdk_window_shape_combine_region (gdkwindow, NULL, 0, 0);
}
return TRUE;
}
static gboolean
select_area_button_release (GtkWidget *window,
GdkEventButton *event,
select_area_filter_data *data)
{
if (!data->button_pressed)
return TRUE;
data->rect.width = ABS (data->rect.x - event->x_root);
data->rect.height = ABS (data->rect.y - event->y_root);
data->rect.x = MIN (data->rect.x, event->x_root);
data->rect.y = MIN (data->rect.y, event->y_root);
gtk_main_quit ();
return TRUE;
}
static gboolean
select_area_key_press (GtkWidget *window,
GdkEventKey *event,
select_area_filter_data *data)
{
if (event->keyval == GDK_KEY_Escape)
{
data->rect.x = 0;
data->rect.y = 0;
data->rect.width = 0;
data->rect.height = 0;
gtk_main_quit ();
}
return TRUE;
}
static gboolean
draw (GtkWidget *window, cairo_t *cr, gpointer unused)
{
GtkStyleContext *style;
style = gtk_widget_get_style_context (window);
if (gtk_widget_get_app_paintable (window))
{
cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
cairo_set_source_rgba (cr, 0, 0, 0, 0);
cairo_paint (cr);
gtk_style_context_save (style);
gtk_style_context_add_class (style, GTK_STYLE_CLASS_RUBBERBAND);
gtk_render_background (style, cr,
0, 0,
gtk_widget_get_allocated_width (window),
gtk_widget_get_allocated_height (window));
gtk_render_frame (style, cr,
0, 0,
gtk_widget_get_allocated_width (window),
gtk_widget_get_allocated_height (window));
gtk_style_context_restore (style);
}
return TRUE;
}
static GtkWidget *
create_select_window (void)
{
GdkScreen *screen = gdk_screen_get_default ();
GtkWidget *window = gtk_window_new (GTK_WINDOW_POPUP);
GdkVisual *visual = gdk_screen_get_rgba_visual (screen);
if (gdk_screen_is_composited (screen) && visual)
{
gtk_widget_set_visual (window, visual);
gtk_widget_set_app_paintable (window, TRUE);
}
g_signal_connect (window, "draw", G_CALLBACK (draw), NULL);
gtk_window_move (GTK_WINDOW (window), -100, -100);
gtk_window_resize (GTK_WINDOW (window), 10, 10);
gtk_widget_show (window);
return window;
}
typedef struct {
GdkRectangle rectangle;
SelectAreaCallback callback;
} CallbackData;
static gboolean
emit_select_callback_in_idle (gpointer user_data)
{
CallbackData *data = user_data;
data->callback (&data->rectangle);
g_slice_free (CallbackData, data);
return FALSE;
}
void
screenshot_select_area_async (SelectAreaCallback callback)
{
GdkDisplay *display;
GdkCursor *cursor;
#if GTK_CHECK_VERSION (3, 20, 0)
GdkSeat *seat;
#else
GdkDeviceManager *manager;
GdkDevice *pointer, *keyboard;
#endif
GdkGrabStatus res;
select_area_filter_data data;
GdkRectangle *rectangle;
CallbackData *cb_data;
data.rect.x = 0;
data.rect.y = 0;
data.rect.width = 0;
data.rect.height = 0;
data.button_pressed = FALSE;
data.window = create_select_window();
cb_data = g_slice_new0 (CallbackData);
cb_data->callback = callback;
g_signal_connect (data.window, "key-press-event", G_CALLBACK (select_area_key_press), &data);
g_signal_connect (data.window, "button-press-event", G_CALLBACK (select_area_button_press), &data);
g_signal_connect (data.window, "button-release-event", G_CALLBACK (select_area_button_release), &data);
g_signal_connect (data.window, "motion-notify-event", G_CALLBACK (select_area_motion_notify), &data);
display = gdk_display_get_default ();
cursor = gdk_cursor_new_for_display (display, GDK_CROSSHAIR);
#if GTK_CHECK_VERSION (3, 20, 0)
seat = gdk_display_get_default_seat (display);
res = gdk_seat_grab (seat,
gtk_widget_get_window (data.window),
GDK_SEAT_CAPABILITY_ALL,
FALSE,
cursor,
NULL,
NULL,
NULL);
if (res != GDK_GRAB_SUCCESS)
{
g_object_unref (cursor);
goto out;
}
gtk_main ();
gdk_seat_ungrab (seat);
#else
manager = gdk_display_get_device_manager (display);
pointer = gdk_device_manager_get_client_pointer (manager);
keyboard = gdk_device_get_associated_device (pointer);
res = gdk_device_grab (pointer, gtk_widget_get_window (data.window),
GDK_OWNERSHIP_NONE, FALSE,
GDK_POINTER_MOTION_MASK |
GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK,
cursor, GDK_CURRENT_TIME);
if (res != GDK_GRAB_SUCCESS)
{
g_object_unref (cursor);
goto out;
}
res = gdk_device_grab (keyboard, gtk_widget_get_window (data.window),
GDK_OWNERSHIP_NONE, FALSE,
GDK_KEY_PRESS_MASK |
GDK_KEY_RELEASE_MASK,
NULL, GDK_CURRENT_TIME);
if (res != GDK_GRAB_SUCCESS)
{
gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
g_object_unref (cursor);
goto out;
}
gtk_main ();
gdk_device_ungrab (keyboard, GDK_CURRENT_TIME);
gdk_device_ungrab (pointer, GDK_CURRENT_TIME);
#endif
gtk_widget_destroy (data.window);
g_object_unref (cursor);
gdk_flush ();
out:
cb_data->rectangle = data.rect;
/* FIXME: we should actually be emitting the callback When
* the compositor has finished re-drawing, but there seems to be no easy
* way to know that.
*/
g_timeout_add (200, emit_select_callback_in_idle, cb_data);
}
static Window
find_wm_window (Window xid)
{
Window root, parent, *children;
unsigned int nchildren;
do
{
if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
&parent, &children, &nchildren) == 0)
{
g_warning ("Couldn't find window manager window");
return None;
}
if (root == parent)
return xid;
xid = parent;
}
while (TRUE);
}
static cairo_region_t *
make_region_with_monitors (GdkScreen *screen)
{
#if GTK_CHECK_VERSION (3, 22, 0)
GdkDisplay *display;
#endif
cairo_region_t *region;
int num_monitors;
int i;
#if GTK_CHECK_VERSION (3, 22, 0)
display = gdk_screen_get_display (screen);
num_monitors = gdk_display_get_n_monitors (display);
#else
num_monitors = gdk_screen_get_n_monitors (screen);
#endif
region = cairo_region_create ();
for (i = 0; i < num_monitors; i++)
{
GdkRectangle rect;
#if GTK_CHECK_VERSION (3, 22, 0)
gdk_monitor_get_geometry (gdk_display_get_monitor (display, i), &rect);
#else
gdk_screen_get_monitor_geometry (screen, i, &rect);
#endif
cairo_region_union_rectangle (region, &rect);
}
return region;
}
static void
blank_rectangle_in_pixbuf (GdkPixbuf *pixbuf, GdkRectangle *rect)
{
int x, y;
int x2, y2;
guchar *pixels;
int rowstride;
int n_channels;
guchar *row;
gboolean has_alpha;
g_assert (gdk_pixbuf_get_colorspace (pixbuf) == GDK_COLORSPACE_RGB);
x2 = rect->x + rect->width;
y2 = rect->y + rect->height;
pixels = gdk_pixbuf_get_pixels (pixbuf);
rowstride = gdk_pixbuf_get_rowstride (pixbuf);
has_alpha = gdk_pixbuf_get_has_alpha (pixbuf);
n_channels = gdk_pixbuf_get_n_channels (pixbuf);
for (y = rect->y; y < y2; y++)
{
guchar *p;
row = pixels + y * rowstride;
p = row + rect->x * n_channels;
for (x = rect->x; x < x2; x++)
{
*p++ = 0;
*p++ = 0;
*p++ = 0;
if (has_alpha)
*p++ = 255; /* opaque black */
}
}
}
static void
blank_region_in_pixbuf (GdkPixbuf *pixbuf, cairo_region_t *region)
{
int n_rects;
int i;
int width, height;
cairo_rectangle_int_t pixbuf_rect;
n_rects = cairo_region_num_rectangles (region);
width = gdk_pixbuf_get_width (pixbuf);
height = gdk_pixbuf_get_height (pixbuf);
pixbuf_rect.x = 0;
pixbuf_rect.y = 0;
pixbuf_rect.width = width;
pixbuf_rect.height = height;
for (i = 0; i < n_rects; i++)
{
cairo_rectangle_int_t rect, dest;
cairo_region_get_rectangle (region, i, &rect);
if (gdk_rectangle_intersect (&rect, &pixbuf_rect, &dest))
blank_rectangle_in_pixbuf (pixbuf, &dest);
}
}
/* When there are multiple monitors with different resolutions, the visible area
* within the root window may not be rectangular (it may have an L-shape, for
* example). In that case, mask out the areas of the root window which would
* not be visible in the monitors, so that screenshot do not end up with content
* that the user won't ever see.
*/
static void
mask_monitors (GdkPixbuf *pixbuf, GdkWindow *root_window)
{
GdkScreen *screen;
cairo_region_t *region_with_monitors;
cairo_region_t *invisible_region;
cairo_rectangle_int_t rect;
screen = gdk_window_get_screen (root_window);
region_with_monitors = make_region_with_monitors (screen);
rect.x = 0;
rect.y = 0;
gdk_window_get_geometry (gdk_screen_get_root_window (screen), NULL, NULL,
&rect.width, &rect.height);
invisible_region = cairo_region_create_rectangle (&rect);
cairo_region_subtract (invisible_region, region_with_monitors);
blank_region_in_pixbuf (pixbuf, invisible_region);
cairo_region_destroy (region_with_monitors);
cairo_region_destroy (invisible_region);
}
GdkPixbuf *
screenshot_get_pixbuf (GdkWindow *window,
GdkRectangle *rectangle,
gboolean include_pointer,
gboolean include_border,
gboolean include_mask)
{
GdkWindow *root;
GdkPixbuf *screenshot;
gint x_real_orig, y_real_orig, x_orig, y_orig;
gint width, real_width, height, real_height;
gint sc_width, sc_height;
gdk_window_get_geometry (gdk_screen_get_root_window (gdk_screen_get_default()),
NULL, NULL, &sc_width, &sc_height);
/* If the screenshot should include the border, we look for the WM window. */
if (include_border)
{
Window xid, wm;
xid = GDK_WINDOW_XID (window);
wm = find_wm_window (xid);
if (wm != None)
window = gdk_x11_window_foreign_new_for_display (gdk_display_get_default (), wm);
/* fallback to no border if we can't find the WM window. */
}
root = gdk_get_default_root_window ();
real_width = gdk_window_get_width(window);
real_height = gdk_window_get_height(window);
gdk_window_get_origin (window, &x_real_orig, &y_real_orig);
x_orig = x_real_orig;
y_orig = y_real_orig;
width = real_width;
height = real_height;
if (x_orig < 0)
{
width = width + x_orig;
x_orig = 0;
}
if (y_orig < 0)
{
height = height + y_orig;
y_orig = 0;
}
if (x_orig + width > sc_width)
width = sc_width - x_orig;
if (y_orig + height > sc_height)
height = sc_height - y_orig;
if (rectangle)
{
x_orig = rectangle->x - x_orig;
y_orig = rectangle->y - y_orig;
width = rectangle->width;
height = rectangle->height;
}
screenshot = gdk_pixbuf_get_from_window (root,
x_orig, y_orig,
width, height);
/*
* Masking currently only works properly with full-screen shots
*/
if (include_mask)
mask_monitors (screenshot, root);
#ifdef HAVE_X11_EXTENSIONS_SHAPE_H
if (include_border)
{
XRectangle *rectangles;
GdkPixbuf *tmp;
int rectangle_count, rectangle_order, i;
/* we must use XShape to avoid showing what's under the rounder corners
* of the WM decoration.
*/
rectangles = XShapeGetRectangles (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
GDK_WINDOW_XID (window),
ShapeBounding,
&rectangle_count,
&rectangle_order);
if (rectangles && rectangle_count > 0 && window != root)
{
gboolean has_alpha = gdk_pixbuf_get_has_alpha (screenshot);
tmp = gdk_pixbuf_new (GDK_COLORSPACE_RGB, TRUE, 8, width, height);
gdk_pixbuf_fill (tmp, 0);
for (i = 0; i < rectangle_count; i++)
{
gint rec_x, rec_y;
gint rec_width, rec_height;
gint y;
rec_x = rectangles[i].x;
rec_y = rectangles[i].y;
rec_width = rectangles[i].width;
rec_height = rectangles[i].height;
if (x_real_orig < 0)
{
rec_x += x_real_orig;
rec_x = MAX(rec_x, 0);
rec_width += x_real_orig;
}
if (y_real_orig < 0)
{
rec_y += y_real_orig;
rec_y = MAX(rec_y, 0);
rec_height += y_real_orig;
}
if (x_orig + rec_x + rec_width > sc_width)
rec_width = sc_width - x_orig - rec_x;
if (y_orig + rec_y + rec_height > sc_height)
rec_height = sc_height - y_orig - rec_y;
for (y = rec_y; y < rec_y + rec_height; y++)
{
guchar *src_pixels, *dest_pixels;
gint x;
src_pixels = gdk_pixbuf_get_pixels (screenshot)
+ y * gdk_pixbuf_get_rowstride(screenshot)
+ rec_x * (has_alpha ? 4 : 3);
dest_pixels = gdk_pixbuf_get_pixels (tmp)
+ y * gdk_pixbuf_get_rowstride (tmp)
+ rec_x * 4;
for (x = 0; x < rec_width; x++)
{
*dest_pixels++ = *src_pixels++;
*dest_pixels++ = *src_pixels++;
*dest_pixels++ = *src_pixels++;
if (has_alpha)
*dest_pixels++ = *src_pixels++;
else
*dest_pixels++ = 255;
}
}
}
g_object_unref (screenshot);
screenshot = tmp;
}
}
#endif /* HAVE_X11_EXTENSIONS_SHAPE_H */
/* if we have a selected area, there were by definition no cursor in the
* screenshot */
if (include_pointer && !rectangle)
{
GdkCursor *cursor;
GdkPixbuf *cursor_pixbuf;
cursor = gdk_cursor_new_for_display (gdk_display_get_default (), GDK_LEFT_PTR);
cursor_pixbuf = gdk_cursor_get_image (cursor);
if (cursor_pixbuf != NULL)
{
#if GTK_CHECK_VERSION (3, 20, 0)
GdkDisplay *display;
GdkSeat *seat;
#else
GdkDeviceManager *manager;
#endif
GdkDevice *device;
GdkRectangle r1, r2;
gint cx, cy, xhot, yhot;
#if GTK_CHECK_VERSION (3, 20, 0)
display = gdk_window_get_display (window);
seat = gdk_display_get_default_seat (display);
device = gdk_seat_get_pointer (seat);
#else
manager = gdk_display_get_device_manager (gdk_display_get_default ());
device = gdk_device_manager_get_client_pointer (manager);
#endif
gdk_window_get_device_position (window, device, &cx, &cy, NULL);
sscanf (gdk_pixbuf_get_option (cursor_pixbuf, "x_hot"), "%d", &xhot);
sscanf (gdk_pixbuf_get_option (cursor_pixbuf, "y_hot"), "%d", &yhot);
/* in r1 we have the window coordinates */
r1.x = x_real_orig;
r1.y = y_real_orig;
r1.width = real_width;
r1.height = real_height;
/* in r2 we have the cursor window coordinates */
r2.x = cx + x_real_orig;
r2.y = cy + y_real_orig;
r2.width = gdk_pixbuf_get_width (cursor_pixbuf);
r2.height = gdk_pixbuf_get_height (cursor_pixbuf);
/* see if the pointer is inside the window */
if (gdk_rectangle_intersect (&r1, &r2, &r2))
{
gdk_pixbuf_composite (cursor_pixbuf, screenshot,
cx - xhot, cy - yhot,
r2.width, r2.height,
cx - xhot, cy - yhot,
1.0, 1.0,
GDK_INTERP_BILINEAR,
255);
}
g_object_unref (cursor_pixbuf);
g_object_unref (cursor);
}
}
return screenshot;
}
gchar *
screenshot_get_window_title (GdkWindow *win)
{
gchar *name;
win = gdk_window_get_toplevel (win);
win = look_for_hint (win, gdk_x11_xatom_to_atom (gdk_x11_get_xatom_by_name ("WM_STATE")));
name = get_utf8_property (win, gdk_x11_xatom_to_atom (gdk_x11_get_xatom_by_name ("_NET_WM_NAME")));
if (name)
return name;
/* TODO: maybe we should also look at WM_NAME and WM_CLASS? */
return g_strdup (_("Untitled Window"));
}
void
screenshot_show_error_dialog (GtkWindow *parent,
const gchar *message,
const gchar *detail)
{
GtkWidget *dialog;
g_return_if_fail ((parent == NULL) || (GTK_IS_WINDOW (parent)));
g_return_if_fail (message != NULL);
dialog = gtk_message_dialog_new (parent,
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_ERROR,
GTK_BUTTONS_OK,
"%s", message);
gtk_window_set_title (GTK_WINDOW (dialog), "");
if (detail)
gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
"%s", detail);
if (parent && gtk_window_get_group (parent))
gtk_window_group_add_window (gtk_window_get_group (parent), GTK_WINDOW (dialog));
gtk_dialog_run (GTK_DIALOG (dialog));
gtk_widget_destroy (dialog);
}
void
screenshot_show_gerror_dialog (GtkWindow *parent,
const gchar *message,
GError *error)
{
g_return_if_fail (parent == NULL || GTK_IS_WINDOW (parent));
g_return_if_fail (message != NULL);
g_return_if_fail (error != NULL);
screenshot_show_error_dialog (parent, message, error->message);
}
|