Debian Codegraph @

src/xutils.c

raw symbols SCIP
1 /**
2 * This file is part of devilspie2
3 * Copyright (C) 2001 Havoc Pennington, 2011-2017 Andreas Rönnquist
4 *
5 * devilspie2 is free software: you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as published
7 * by the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * devilspie2 is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with devilspie2.
17 * If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include <X11/Xatom.h>
20
21 #include <glib.h>
22 #include <gdk/gdk.h>
23 #include <gdk/gdkx.h>
24 #include <X11/Xlib.h>
25 #include <string.h>
26
27 #define WNCK_I_KNOW_THIS_IS_UNSTABLE
28 #include <libwnck/libwnck.h>
29
30 #include <locale.h>
31
32 #include <libintl.h>
33 #define _(String) gettext (String)
34 #define gettext_noop(String) String
35 #define N_(String) gettext_noop (String)
36
37 #include "xutils.h"
38
39
40 #if (GTK_MAJOR_VERSION >= 3)
41 #define HAVE_GTK3
42 #endif
43
44
45 static GHashTable *atom_hash = NULL;
46 static GHashTable *reverse_atom_hash = NULL;
47
48
49 /**
50 *
51 */
52 Atom my_wnck_atom_get(const char *atom_name)
53 {
54 Atom retval;
55
56 g_return_val_if_fail (atom_name != NULL, None);
57
58 if (!atom_hash) {
59 atom_hash = g_hash_table_new (g_str_hash, g_str_equal);
60 reverse_atom_hash = g_hash_table_new (NULL, NULL);
61 }
62
63 retval = GPOINTER_TO_UINT (g_hash_table_lookup (atom_hash, atom_name));
64 if (!retval) {
65 retval = XInternAtom (gdk_x11_get_default_xdisplay(), atom_name, FALSE);
66
67 if (retval != None) {
68 char *name_copy;
69
70 name_copy = g_strdup (atom_name);
71
72 g_hash_table_insert (atom_hash,
73 name_copy,
74 GUINT_TO_POINTER (retval));
75 g_hash_table_insert (reverse_atom_hash,
76 GUINT_TO_POINTER (retval),
77 name_copy);
78 }
79 }
80 return retval;
81 }
82
83
84 /**
85 *
86 */
87 void devilspie2_change_state(Screen *screen, Window xwindow,
88 gboolean add,
89 Atom state1,
90 Atom state2)
91 {
92 XEvent xev;
93
94 #define _NET_WM_STATE_REMOVE 0 /* remove/unset property */
95 #define _NET_WM_STATE_ADD 1 /* add/set property */
96 #define _NET_WM_STATE_TOGGLE 2 /* toggle property */
97
98 xev.xclient.type = ClientMessage;
99 xev.xclient.serial = 0;
100 xev.xclient.send_event = True;
101 xev.xclient.display = gdk_x11_get_default_xdisplay();
102 xev.xclient.window = xwindow;
103 xev.xclient.message_type = my_wnck_atom_get ("_NET_WM_STATE");
104 xev.xclient.format = 32;
105 xev.xclient.data.l[0] = add ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
106 xev.xclient.data.l[1] = state1;
107 xev.xclient.data.l[2] = state2;
108
109 XSendEvent (gdk_x11_get_default_xdisplay(),
110 RootWindowOfScreen (screen),
111 False,
112 SubstructureRedirectMask | SubstructureNotifyMask,
113 &xev);
114 }
115
116
117 /**
118 *
119 */
120 void devilspie2_error_trap_push()
121 {
122 gdk_error_trap_push();
123 }
124
125
126 /**
127 *
128 */
129 int devilspie2_error_trap_pop()
130 {
131 XSync(gdk_x11_get_default_xdisplay(),False);
132 return gdk_error_trap_pop();
133 }
134
135
136 /**
137 *
138 */
139 static void set_decorations(Window xid /*WnckWindow *window*/, gboolean decorate)
140 {
141 #define PROP_MOTIF_WM_HINTS_ELEMENTS 5
142 #define MWM_HINTS_DECORATIONS (1L << 1)
143 struct {
144 unsigned long flags;
145 unsigned long functions;
146 unsigned long decorations;
147 long inputMode;
148 unsigned long status;
149 } hints = {0,};
150
151 hints.flags = MWM_HINTS_DECORATIONS;
152 hints.decorations = decorate ? 1 : 0;
153
154 /* Set Motif hints, most window managers handle these */
155 XChangeProperty(gdk_x11_get_default_xdisplay(), xid /*wnck_window_get_xid (window)*/,
156 my_wnck_atom_get ("_MOTIF_WM_HINTS"),
157 my_wnck_atom_get ("_MOTIF_WM_HINTS"), 32, PropModeReplace,
158 (unsigned char *)&hints, PROP_MOTIF_WM_HINTS_ELEMENTS);
159
160
161 //Window xid;
162 XWindowAttributes attrs;
163
164 //xid = wnck_window_get_xid (window);
165 XGetWindowAttributes(gdk_x11_get_default_xdisplay(), xid, &attrs);
166
167 /* Apart from OpenBox, which doesn't respect it changing after mapping.
168 Instead it has this workaround. */
169 devilspie2_change_state (attrs.screen,
170 xid /*wnck_window_get_xid(window)*/, !decorate,
171 my_wnck_atom_get ("_OB_WM_STATE_UNDECORATED"), 0);
172
173 }
174
175
176 /**
177 *
178 */
179 gboolean decorate_window(Window xid)
180 {
181 devilspie2_error_trap_push();
182
183 set_decorations(xid, TRUE);
184
185 if (devilspie2_error_trap_pop()) {
186 g_printerr("decorate_window %s\n", _("Failed!"));
187 return FALSE;
188 }
189
190 return TRUE;
191 }
192
193
194 /**
195 *
196 */
197 gboolean undecorate_window(Window xid)
198 {
199 devilspie2_error_trap_push();
200
201 set_decorations(xid, FALSE);
202
203 if (devilspie2_error_trap_pop()) {
204 g_printerr("decorate_window %s\n", _("Failed!"));
205 return FALSE;
206 }
207
208 return TRUE;
209 }
210
211
212 /**
213 *
214 */
215 Screen *devilspie2_window_get_xscreen(Window xid)
216 {
217 XWindowAttributes attrs;
218
219 XGetWindowAttributes(gdk_x11_get_default_xdisplay(), xid, &attrs);
220
221 return attrs.screen;
222 }
223
224
225 /**
226 *
227 */
228 char* my_wnck_get_string_property_latin1(Window xwindow, Atom atom)
229 {
230 Atom type;
231 int format;
232 gulong nitems;
233 gulong bytes_after;
234 unsigned char *property;
235 int err, result;
236 char *retval;
237
238 devilspie2_error_trap_push();
239 property = NULL;
240 result = XGetWindowProperty (gdk_x11_get_default_xdisplay (),
241 xwindow, atom,
242 0, G_MAXLONG,
243 False, AnyPropertyType, &type,
244 &format, &nitems,
245 &bytes_after, &property);
246
247 err = devilspie2_error_trap_pop ();
248 if (err != Success || result != Success)
249 return NULL;
250
251 retval = NULL;
252
253 if (type == XA_STRING) {
254 retval = g_strdup ((char*)property);
255 } else if (type == XA_ATOM && nitems > 0 && format == 32) {
256 long *pp;
257
258 pp = (long *)property; // we can assume (long *) since format == 32
259 if (nitems == 1) {
260 char* prop_name;
261 prop_name = XGetAtomName (gdk_x11_get_default_xdisplay (), *pp);
262 if (prop_name) {
263 retval = g_strdup (prop_name);
264 XFree (prop_name);
265 }
266 } else {
267 int i;
268 char** prop_names;
269
270 prop_names = g_new (char *, nitems + 1);
271 prop_names[nitems] = NULL;
272 for (i=0; i < nitems; i++) {
273 prop_names[i] = XGetAtomName (gdk_x11_get_default_xdisplay (),
274 *pp++);
275 }
276 retval = g_strjoinv (", ", prop_names);
277 for (i=0; i < nitems; i++) {
278 if (prop_names[i]) XFree (prop_names[i]);
279 }
280 g_free (prop_names);
281 }
282 } else if (type == XA_CARDINAL && nitems == 1) {
283 switch(format) {
284 case 32:
285 retval = g_strdup_printf("%lu", *(unsigned long*)property);
286 break;
287 case 16:
288 retval = g_strdup_printf("%u", *(unsigned int*)property);
289 break;
290 case 8:
291 retval = g_strdup_printf("%c", *(unsigned char*)property);
292 break;
293 }
294 } else if (type == XA_WINDOW && nitems == 1) {
295 /* unsinged long is the same format used for XID by libwnck:
296 https://git.gnome.org/browse/libwnck/tree/libwnck/window.c?h=3.14.0#n763 */
297 retval = g_strdup_printf("%lu", (gulong) *(Window *)property);
298 }
299
300 XFree (property);
301 return retval;
302 }
303
304
305 /**
306 *
307 */
308 gboolean
309 my_wnck_get_cardinal_list (Window xwindow, Atom atom,
310 gulong **cardinals, int *len)
311 {
312 Atom type;
313 int format;
314 gulong nitems;
315 gulong bytes_after;
316 gulong *nums;
317 int err, result;
318
319 *cardinals = NULL;
320 *len = 0;
321
322 devilspie2_error_trap_push();
323 type = None;
324 result = XGetWindowProperty(gdk_x11_get_default_xdisplay (),
325 xwindow,
326 atom,
327 0, G_MAXLONG,
328 False, XA_CARDINAL, &type, &format, &nitems,
329 &bytes_after, (void*)&nums);
330
331 err = devilspie2_error_trap_pop();
332
333 if ((err != Success) || (result != Success))
334 return FALSE;
335
336 if (type != XA_CARDINAL) {
337 XFree (nums);
338 return FALSE;
339 }
340
341 *cardinals = g_new(gulong, nitems);
342 memcpy(*cardinals, nums, sizeof (gulong) * nitems);
343 *len = nitems;
344
345 XFree(nums);
346
347 return TRUE;
348 }
349
350
351 /**
352 * Get viewport start coordinates to the x and y integers,
353 * returns 0 on success and non-zero on error.
354 */
355 int devilspie2_get_viewport_start(Window xid, int *x, int *y)
356 {
357 gulong *list;
358 int len;
359
360 int result = -1;
361
362 my_wnck_get_cardinal_list(RootWindowOfScreen(
363 devilspie2_window_get_xscreen(xid)),
364 my_wnck_atom_get("_NET_DESKTOP_VIEWPORT"),
365 &list, &len);
366
367 if (len > 0) {
368 *x = list[0];
369 *y = list[1];
370
371 result = 0;
372 }
373
374 g_free(list);
375
376 return result;
377 }
378
379
380 /**
381 *
382 */
383 void my_window_set_window_type(Window xid, gchar *window_type)
384 {
385 Display *display = gdk_x11_get_default_xdisplay();
386
387 Atom atoms[10];
388
389 /*
390 _NET_WM_WINDOW_TYPE_DESKTOP, ATOM
391 _NET_WM_WINDOW_TYPE_DOCK, ATOM
392 _NET_WM_WINDOW_TYPE_TOOLBAR, ATOM
393 _NET_WM_WINDOW_TYPE_MENU, ATOM
394 _NET_WM_WINDOW_TYPE_UTILITY, ATOM
395 _NET_WM_WINDOW_TYPE_SPLASH, ATOM
396 _NET_WM_WINDOW_TYPE_DIALOG, ATOM
397 _NET_WM_WINDOW_TYPE_NORMAL, ATOM
398 */
399
400 gchar *type = NULL;
401
402 // Make it a recognized _NET_WM_TYPE
403
404 if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_DESKTOP") == 0) {
405 type = g_strdup("_NET_WM_WINDOW_TYPE_DESKTOP");
406
407 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_DOCK") == 0) {
408 type = g_strdup("_NET_WM_WINDOW_TYPE_DOCK");
409
410 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_TOOLBAR") == 0) {
411 type = g_strdup("_NET_WM_WINDOW_TYPE_TOOLBAR");
412
413 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_MENU") == 0) {
414 type = g_strdup("_NET_WM_WINDOW_TYPE_MENU");
415
416 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_UTILITY") == 0) {
417 type = g_strdup("_NET_WM_WINDOW_TYPE_UTILITY");
418
419 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_SPLASH") == 0) {
420 type = g_strdup("_NET_WM_WINDOW_TYPE_SPLASH");
421
422 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_DIALOG") == 0) {
423 type = g_strdup("_NET_WM_WINDOW_TYPE_DIALOG");
424
425 } else if (g_ascii_strcasecmp(window_type, "WINDOW_TYPE_NORMAL") == 0) {
426 type = g_strdup("_NET_WM_WINDOW_TYPE_NORMAL");
427
428 } else {
429 type = g_strdup(window_type);
430 }
431
432 atoms[0] = XInternAtom(display, type, False);
433
434 XChangeProperty(gdk_x11_get_default_xdisplay(), xid,
435 XInternAtom(display, "_NET_WM_WINDOW_TYPE", False), XA_ATOM, 32,
436 PropModeReplace, (unsigned char *) &atoms, 1);
437
438 if (type) g_free(type);
439 }
440
441
442 /**
443 *
444 */
445 void my_window_set_opacity(Window xid, double value)
446 {
447 Display *display = gdk_x11_get_default_xdisplay();
448
449 unsigned int opacity = (uint)(0xffffffff * value);
450 Atom atom_net_wm_opacity = XInternAtom(display, "_NET_WM_WINDOW_OPACITY", False);
451
452
453 XChangeProperty(gdk_x11_get_default_xdisplay(), xid,
454 atom_net_wm_opacity, XA_CARDINAL, 32,
455 PropModeReplace, (unsigned char *) &opacity, 1L);
456
457 }
458
459
460 /**
461 *
462 */
463 void set_window_geometry(WnckWindow *window, int x, int y, int w, int h)
464 {
465
466 if (window) {
467 WnckScreen *screen = wnck_window_get_screen(window);
468 int sw = wnck_screen_get_width(screen);
469 int sh = wnck_screen_get_height(screen);
470
471 int gravity = WNCK_WINDOW_GRAVITY_CURRENT;
472 if (x >= 0 && y >= 0)
473 gravity = WNCK_WINDOW_GRAVITY_NORTHWEST;
474 if (x >= 0 && y < 0)
475 gravity = WNCK_WINDOW_GRAVITY_SOUTHWEST;
476 if (x < 0 && y >= 0)
477 gravity = WNCK_WINDOW_GRAVITY_NORTHEAST;
478 if (x < 0 && y < 0)
479 gravity = WNCK_WINDOW_GRAVITY_SOUTHEAST;
480 if (x < 0)
481 x = sw + x;
482 if (y < 0)
483 y = sh + y;
484
485 wnck_window_set_geometry(window,
486 gravity,
487 WNCK_WINDOW_CHANGE_X +
488 WNCK_WINDOW_CHANGE_Y +
489 WNCK_WINDOW_CHANGE_WIDTH +
490 WNCK_WINDOW_CHANGE_HEIGHT,
491 x, y, w, h);
492 }
493
494 }