Debian Codegraph @

src/screenshot-utils.c

raw symbols SCIP
1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include "screenshot-utils.h"
6 #include <X11/Xatom.h>
7 #include <gdk/gdkx.h>
8
9 static gboolean
10 get_atom_property (Window xwindow,
11 Atom atom,
12 Atom *val)
13 {
14 Atom type;
15 int format;
16 gulong nitems;
17 gulong bytes_after;
18 Atom *a = NULL;
19 int err, result;
20 guchar *atom_cast;
21
22 *val = 0;
23
24 gdk_error_trap_push ();
25 type = None;
26 result = XGetWindowProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
27 xwindow,
28 atom,
29 0, G_MAXLONG,
30 False, XA_ATOM, &type, &format, &nitems,
31 &bytes_after, &atom_cast);
32 a = (Atom *) atom_cast;
33 err = gdk_error_trap_pop ();
34 if (err != Success ||
35 result != Success)
36 return FALSE;
37
38 if (type != XA_ATOM)
39 {
40 XFree (a);
41 return FALSE;
42 }
43
44 *val = *a;
45
46 XFree (a);
47
48 return TRUE;
49 }
50
51 static Window
52 find_toplevel_window (Window xid)
53 {
54 Window root, parent, *children;
55 guint nchildren;
56
57 do
58 {
59 if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
60 &parent, &children, &nchildren) == 0)
61 {
62 g_warning ("Couldn't find window manager window");
63 return None;
64 }
65
66 if (root == parent)
67 return xid;
68
69 xid = parent;
70 }
71 while (TRUE);
72 }
73
74 static gboolean
75 screenshot_window_is_desktop (Window xid)
76 {
77 Window root_window = GDK_ROOT_WINDOW ();
78
79 if (xid == root_window)
80 return TRUE;
81
82 if (gdk_x11_screen_supports_net_wm_hint (gdk_screen_get_default (),
83 gdk_atom_intern ("_NET_WM_WINDOW_TYPE", FALSE)))
84 {
85 gboolean retval;
86 Atom property;
87
88 retval = get_atom_property (xid,
89 gdk_x11_get_xatom_by_name ("_NET_WM_WINDOW_TYPE"),
90 &property);
91 if (retval &&
92 property == gdk_x11_get_xatom_by_name ("_NET_WM_WINDOW_TYPE_DESKTOP"))
93 return TRUE;
94 }
95 return FALSE;
96 }
97
98 /* We don't actually honor include_decoration here. We need to search
99 * for WM_STATE;
100 */
101 static Window
102 screenshot_find_pointer_window (void)
103 {
104 Window root_window, root_return, child;
105 int unused;
106 guint mask;
107
108 root_window = GDK_ROOT_WINDOW ();
109
110 XQueryPointer (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), root_window,
111 &root_return, &child, &unused,
112 &unused, &unused, &unused, &mask);
113
114 return child;
115 }
116
117 #define MAXIMUM_WM_REPARENTING_DEPTH 4
118
119 /* adopted from eel code */
120 static Window
121 look_for_hint_helper (Window xid,
122 Atom property,
123 int depth)
124 {
125 Atom actual_type;
126 int actual_format;
127 gulong nitems, bytes_after;
128 gulong *prop;
129 Window root, parent, *children, window;
130 guint nchildren, i;
131
132 if (XGetWindowProperty (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()),
133 xid, property, 0, 1,
134 False, AnyPropertyType, &actual_type,
135 &actual_format, &nitems, &bytes_after,
136 (gpointer) &prop) == Success
137 && prop != NULL && actual_format == 32 && prop[0] == NormalState)
138 {
139 if (prop != NULL)
140 {
141 XFree (prop);
142 }
143
144 return xid;
145 }
146
147 if (depth < MAXIMUM_WM_REPARENTING_DEPTH)
148 {
149 if (XQueryTree (GDK_DISPLAY_XDISPLAY (gdk_display_get_default ()), xid, &root,
150 &parent, &children, &nchildren) != 0)
151 {
152 window = None;
153
154 for (i = 0; i < nchildren; i++)
155 {
156 window = look_for_hint_helper (children[i],
157 property,
158 depth + 1);
159 if (window != None)
160 break;
161 }
162
163 if (children != NULL)
164 XFree (children);
165
166 if (window)
167 return window;
168 }
169 }
170
171 return None;
172 }
173
174 static Window
175 look_for_hint (Window xid,
176 Atom property)
177 {
178 Window retval;
179
180 retval = look_for_hint_helper (xid, property, 0);
181
182 return retval;
183 }
184
185
186 Window
187 screenshot_find_current_window (gboolean include_decoration)
188 {
189 Window current_window;
190
191 current_window = screenshot_find_pointer_window ();
192
193 if (current_window)
194 {
195 if (screenshot_window_is_desktop (current_window))
196 /* if the current window is the desktop (eg. nautilus), we
197 * return None, as getting the whole screen makes more sense. */
198 return None;
199
200 /* Once we have a window, we walk the widget tree looking for
201 * the appropriate window. */
202 current_window = find_toplevel_window (current_window);
203 if (! include_decoration)
204 {
205 Window new_window;
206 new_window = look_for_hint (current_window, gdk_x11_get_xatom_by_name ("WM_STATE"));
207 if (new_window)
208 current_window = new_window;
209 }
210 }
211 return current_window;
212 }
213