Merge branch 'backport' into work
[mikachu/openbox.git] / obt / prop.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    obt/prop.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "obt/prop.h"
21 #include "obt/display.h"
22
23 #include <X11/Xatom.h>
24 #ifdef HAVE_STRING_H
25 #  include <string.h>
26 #endif
27
28 Atom prop_atoms[OBT_PROP_NUM_ATOMS];
29 gboolean prop_started = FALSE;
30
31 #define CREATE_NAME(var, name) (prop_atoms[OBT_PROP_##var] = \
32                                 XInternAtom((obt_display), (name), FALSE))
33 #define CREATE(var) CREATE_NAME(var, #var)
34 #define CREATE_(var) CREATE_NAME(var, "_" #var)
35
36 void obt_prop_startup(void)
37 {
38     if (prop_started) return;
39     prop_started = TRUE;
40
41     g_assert(obt_display);
42
43     CREATE(CARDINAL);
44     CREATE(WINDOW);
45     CREATE(PIXMAP);
46     CREATE(ATOM);
47     CREATE(STRING);
48     CREATE_NAME(UTF8, "UTF8_STRING");
49
50     CREATE(MANAGER);
51
52     CREATE(WM_COLORMAP_WINDOWS);
53     CREATE(WM_PROTOCOLS);
54     CREATE(WM_STATE);
55     CREATE(WM_CHANGE_STATE);
56     CREATE(WM_DELETE_WINDOW);
57     CREATE(WM_TAKE_FOCUS);
58     CREATE(WM_NAME);
59     CREATE(WM_ICON_NAME);
60     CREATE(WM_CLASS);
61     CREATE(WM_WINDOW_ROLE);
62     CREATE(WM_CLIENT_MACHINE);
63     CREATE(WM_COMMAND);
64     CREATE(WM_CLIENT_LEADER);
65     CREATE(WM_TRANSIENT_FOR);
66     CREATE_(MOTIF_WM_HINTS);
67
68     CREATE(SM_CLIENT_ID);
69
70     CREATE_(NET_WM_FULL_PLACEMENT);
71
72     CREATE_(NET_SUPPORTED);
73     CREATE_(NET_CLIENT_LIST);
74     CREATE_(NET_CLIENT_LIST_STACKING);
75     CREATE_(NET_NUMBER_OF_DESKTOPS);
76     CREATE_(NET_DESKTOP_GEOMETRY);
77     CREATE_(NET_DESKTOP_VIEWPORT);
78     CREATE_(NET_CURRENT_DESKTOP);
79     CREATE_(NET_DESKTOP_NAMES);
80     CREATE_(NET_ACTIVE_WINDOW);
81 /*    CREATE_(NET_RESTACK_WINDOW);*/
82     CREATE_(NET_WORKAREA);
83     CREATE_(NET_SUPPORTING_WM_CHECK);
84     CREATE_(NET_DESKTOP_LAYOUT);
85     CREATE_(NET_SHOWING_DESKTOP);
86
87     CREATE_(NET_CLOSE_WINDOW);
88     CREATE_(NET_WM_MOVERESIZE);
89     CREATE_(NET_MOVERESIZE_WINDOW);
90     CREATE_(NET_REQUEST_FRAME_EXTENTS);
91     CREATE_(NET_RESTACK_WINDOW);
92
93     CREATE_(NET_STARTUP_ID);
94
95     CREATE_(NET_WM_NAME);
96     CREATE_(NET_WM_VISIBLE_NAME);
97     CREATE_(NET_WM_ICON_NAME);
98     CREATE_(NET_WM_VISIBLE_ICON_NAME);
99     CREATE_(NET_WM_DESKTOP);
100     CREATE_(NET_WM_WINDOW_TYPE);
101     CREATE_(NET_WM_STATE);
102     CREATE_(NET_WM_STRUT);
103     CREATE_(NET_WM_STRUT_PARTIAL);
104     CREATE_(NET_WM_ICON);
105     CREATE_(NET_WM_ICON_GEOMETRY);
106     CREATE_(NET_WM_PID);
107     CREATE_(NET_WM_ALLOWED_ACTIONS);
108     CREATE_(NET_WM_USER_TIME);
109 /*  CREATE_(NET_WM_USER_TIME_WINDOW); */
110     CREATE_(KDE_NET_WM_FRAME_STRUT);
111     CREATE_(NET_FRAME_EXTENTS);
112
113     CREATE_(NET_WM_PING);
114 #ifdef SYNC
115     CREATE_(NET_WM_SYNC_REQUEST);
116     CREATE_(NET_WM_SYNC_REQUEST_COUNTER);
117 #endif
118
119     CREATE_(NET_WM_WINDOW_TYPE_DESKTOP);
120     CREATE_(NET_WM_WINDOW_TYPE_DOCK);
121     CREATE_(NET_WM_WINDOW_TYPE_TOOLBAR);
122     CREATE_(NET_WM_WINDOW_TYPE_MENU);
123     CREATE_(NET_WM_WINDOW_TYPE_UTILITY);
124     CREATE_(NET_WM_WINDOW_TYPE_SPLASH);
125     CREATE_(NET_WM_WINDOW_TYPE_DIALOG);
126     CREATE_(NET_WM_WINDOW_TYPE_NORMAL);
127     CREATE_(NET_WM_WINDOW_TYPE_POPUP_MENU);
128
129     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_TOPLEFT] = 0;
130     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_TOP] = 1;
131     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_TOPRIGHT] = 2;
132     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_RIGHT] = 3;
133     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT] = 4;
134     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_BOTTOM] = 5;
135     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT] = 6;
136     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_LEFT] = 7;
137     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_MOVE] = 8;
138     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_SIZE_KEYBOARD] = 9;
139     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_MOVE_KEYBOARD] = 10;
140     prop_atoms[OBT_PROP_NET_WM_MOVERESIZE_CANCEL] = 11;
141
142     CREATE_(NET_WM_ACTION_MOVE);
143     CREATE_(NET_WM_ACTION_RESIZE);
144     CREATE_(NET_WM_ACTION_MINIMIZE);
145     CREATE_(NET_WM_ACTION_SHADE);
146     CREATE_(NET_WM_ACTION_MAXIMIZE_HORZ);
147     CREATE_(NET_WM_ACTION_MAXIMIZE_VERT);
148     CREATE_(NET_WM_ACTION_FULLSCREEN);
149     CREATE_(NET_WM_ACTION_CHANGE_DESKTOP);
150     CREATE_(NET_WM_ACTION_CLOSE);
151     CREATE_(NET_WM_ACTION_ABOVE);
152     CREATE_(NET_WM_ACTION_BELOW);
153
154     CREATE_(NET_WM_STATE_MODAL);
155 /*    CREATE_(NET_WM_STATE_STICKY);*/
156     CREATE_(NET_WM_STATE_MAXIMIZED_VERT);
157     CREATE_(NET_WM_STATE_MAXIMIZED_HORZ);
158     CREATE_(NET_WM_STATE_SHADED);
159     CREATE_(NET_WM_STATE_SKIP_TASKBAR);
160     CREATE_(NET_WM_STATE_SKIP_PAGER);
161     CREATE_(NET_WM_STATE_HIDDEN);
162     CREATE_(NET_WM_STATE_FULLSCREEN);
163     CREATE_(NET_WM_STATE_ABOVE);
164     CREATE_(NET_WM_STATE_BELOW);
165     CREATE_(NET_WM_STATE_DEMANDS_ATTENTION);
166
167     prop_atoms[OBT_PROP_NET_WM_STATE_ADD] = 1;
168     prop_atoms[OBT_PROP_NET_WM_STATE_REMOVE] = 0;
169     prop_atoms[OBT_PROP_NET_WM_STATE_TOGGLE] = 2;
170
171     prop_atoms[OBT_PROP_NET_WM_ORIENTATION_HORZ] = 0;
172     prop_atoms[OBT_PROP_NET_WM_ORIENTATION_VERT] = 1;
173     prop_atoms[OBT_PROP_NET_WM_TOPLEFT] = 0;
174     prop_atoms[OBT_PROP_NET_WM_TOPRIGHT] = 1;
175     prop_atoms[OBT_PROP_NET_WM_BOTTOMRIGHT] = 2;
176     prop_atoms[OBT_PROP_NET_WM_BOTTOMLEFT] = 3;
177
178     CREATE_(KDE_WM_CHANGE_STATE);
179     CREATE_(KDE_NET_WM_WINDOW_TYPE_OVERRIDE);
180
181 /*
182     CREATE_NAME(ROOTPMAPId, "_XROOTPMAP_ID");
183     CREATE_NAME(ESETROOTId, "ESETROOT_PMAP_ID");
184 */
185
186     CREATE_(OPENBOX_PID);
187     CREATE_(OB_THEME);
188     CREATE_(OB_CONFIG_FILE);
189     CREATE_(OB_WM_ACTION_UNDECORATE);
190     CREATE_(OB_WM_STATE_UNDECORATED);
191     CREATE_(OB_CONTROL);
192 }
193
194 Atom obt_prop_atom(ObtPropAtom a)
195 {
196     g_assert(prop_started);
197     g_assert(a < OBT_PROP_NUM_ATOMS);
198     return prop_atoms[a];
199 }
200
201 static gboolean get_prealloc(Window win, Atom prop, Atom type, gint size,
202                              guchar *data, gulong num)
203 {
204     gboolean ret = FALSE;
205     gint res;
206     guchar *xdata = NULL;
207     Atom ret_type;
208     gint ret_size;
209     gulong ret_items, bytes_left;
210     glong num32 = 32 / size * num; /* num in 32-bit elements */
211
212     res = XGetWindowProperty(obt_display, win, prop, 0l, num32,
213                              FALSE, type, &ret_type, &ret_size,
214                              &ret_items, &bytes_left, &xdata);
215     if (res == Success && ret_items && xdata) {
216         if (ret_size == size && ret_items >= num) {
217             guint i;
218             for (i = 0; i < num; ++i)
219                 switch (size) {
220                 case 8:
221                     data[i] = xdata[i];
222                     break;
223                 case 16:
224                     ((guint16*)data)[i] = ((gushort*)xdata)[i];
225                     break;
226                 case 32:
227                     ((guint32*)data)[i] = ((gulong*)xdata)[i];
228                     break;
229                 default:
230                     g_assert_not_reached(); /* unhandled size */
231                 }
232             ret = TRUE;
233         }
234         XFree(xdata);
235     }
236     return ret;
237 }
238
239 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
240                         guchar **data, guint *num)
241 {
242     gboolean ret = FALSE;
243     gint res;
244     guchar *xdata = NULL;
245     Atom ret_type;
246     gint ret_size;
247     gulong ret_items, bytes_left;
248
249     res = XGetWindowProperty(obt_display, win, prop, 0l, G_MAXLONG,
250                              FALSE, type, &ret_type, &ret_size,
251                              &ret_items, &bytes_left, &xdata);
252     if (res == Success) {
253         if (ret_size == size && ret_items > 0) {
254             guint i;
255
256             *data = g_malloc(ret_items * (size / 8));
257             for (i = 0; i < ret_items; ++i)
258                 switch (size) {
259                 case 8:
260                     (*data)[i] = xdata[i];
261                     break;
262                 case 16:
263                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
264                     break;
265                 case 32:
266                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
267                     break;
268                 default:
269                     g_assert_not_reached(); /* unhandled size */
270                 }
271             *num = ret_items;
272             ret = TRUE;
273         }
274         XFree(xdata);
275     }
276     return ret;
277 }
278
279 static gboolean get_stringlist(Window win, Atom prop, gchar ***list, gint *nstr)
280 {
281     XTextProperty tprop;
282     gboolean ret = FALSE;
283
284     if (XGetTextProperty(obt_display, win, &tprop, prop) && tprop.nitems) {
285         if (XTextPropertyToStringList(&tprop, list, nstr))
286             ret = TRUE;
287         XFree(tprop.value);
288     }
289     return ret;
290 }
291
292 gboolean obt_prop_get32(Window win, Atom prop, Atom type, guint32 *ret)
293 {
294     return get_prealloc(win, prop, type, 32, (guchar*)ret, 1);
295 }
296
297 gboolean obt_prop_get_array32(Window win, Atom prop, Atom type, guint32 **ret,
298                               guint *nret)
299 {
300     return get_all(win, prop, type, 32, (guchar**)ret, nret);
301 }
302
303 gboolean obt_prop_get_string_locale(Window win, Atom prop, gchar **ret)
304 {
305     gchar **list;
306     gint nstr;
307     gchar *s;
308
309     if (get_stringlist(win, prop, &list, &nstr) && nstr) {
310         s = g_locale_to_utf8(list[0], -1, NULL, NULL, NULL);
311         XFreeStringList(list);
312         if (s) {
313             *ret = s;
314             return TRUE;
315         }
316     }
317     return FALSE;
318 }
319
320 gboolean obt_prop_get_strings_locale(Window win, Atom prop, gchar ***ret)
321 {
322     GSList *strs = NULL, *it;
323     gchar *raw, *p;
324     guint num, i, count = 0;
325
326     if (get_all(win, prop, OBT_PROP_ATOM(STRING), 8,
327                 (guchar**)&raw, &num))
328     {
329         p = raw;
330         while (p < raw + num) {
331             ++count;
332             strs = g_slist_append(strs, p);
333             p += strlen(p) + 1; /* next string */
334         }
335
336         *ret = g_new0(gchar*, count + 1);
337         (*ret)[count] = NULL; /* null terminated list */
338
339         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
340             (*ret)[i] = g_locale_to_utf8(it->data, -1, NULL, NULL, NULL);
341             /* make sure translation did not fail */
342             if (!(*ret)[i])
343                 (*ret)[i] = g_strdup("");
344         }
345         g_free(raw);
346         g_slist_free(strs);
347         return TRUE;
348     }
349     return FALSE;
350 }
351
352 gboolean obt_prop_get_string_utf8(Window win, Atom prop, gchar **ret)
353 {
354     gchar *raw;
355     gchar *str;
356     guint num;
357
358     if (get_all(win, prop, OBT_PROP_ATOM(UTF8), 8,
359                 (guchar**)&raw, &num))
360     {
361         str = g_strndup(raw, num); /* grab the first string from the list */
362         g_free(raw);
363         if (g_utf8_validate(str, -1, NULL)) {
364             *ret = str;
365             return TRUE;
366         }
367         g_free(str);
368     }
369     return FALSE;
370 }
371
372 gboolean obt_prop_get_strings_utf8(Window win, Atom prop, gchar ***ret)
373 {
374     GSList *strs = NULL, *it;
375     gchar *raw, *p;
376     guint num, i, count = 0;
377
378     if (get_all(win, prop, OBT_PROP_ATOM(UTF8), 8,
379                 (guchar**)&raw, &num))
380     {
381         p = raw;
382         while (p < raw + num) {
383             ++count;
384             strs = g_slist_append(strs, p);
385             p += strlen(p) + 1; /* next string */
386         }
387
388         *ret = g_new0(gchar*, count + 1);
389
390         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
391             if (g_utf8_validate(it->data, -1, NULL))
392                 (*ret)[i] = g_strdup(it->data);
393             else
394                 (*ret)[i] = g_strdup("");
395         }
396         g_free(raw);
397         g_slist_free(strs);
398         return TRUE;
399     }
400     return FALSE;
401 }
402
403 void obt_prop_set32(Window win, Atom prop, Atom type, gulong val)
404 {
405     XChangeProperty(obt_display, win, prop, type, 32, PropModeReplace,
406                     (guchar*)&val, 1);
407 }
408
409 void obt_prop_set_array32(Window win, Atom prop, Atom type, gulong *val,
410                       guint num)
411 {
412     XChangeProperty(obt_display, win, prop, type, 32, PropModeReplace,
413                     (guchar*)val, num);
414 }
415
416 void obt_prop_set_string_locale(Window win, Atom prop, const gchar *val)
417 {
418     gchar const *s[2] = { val, NULL };
419     obt_prop_set_strings_locale(win, prop, s);
420 }
421
422 void obt_prop_set_strings_locale(Window win, Atom prop, const gchar **strs)
423 {
424     gint i, count;
425     gchar **lstrs;
426     XTextProperty tprop;
427
428     /* count the strings in strs, and convert them to the locale format */
429     for (count = 0; strs[count]; ++count);
430     lstrs = g_new0(char*, count);
431     for (i = 0; i < count; ++i) {
432         lstrs[i] = g_locale_from_utf8(strs[i], -1, NULL, NULL, NULL);
433         if (!lstrs[i]) {
434             lstrs[i] = g_strdup(""); /* make it an empty string */
435             g_warning("Unable to translate string '%s' from UTF8 to locale "
436                       "format", strs[i]);
437         }
438     }
439
440
441     XStringListToTextProperty(lstrs, count, &tprop);
442     XSetTextProperty(obt_display, win, &tprop, prop);
443     XFree(tprop.value);
444 }
445
446 void obt_prop_set_string_utf8(Window win, Atom prop, const gchar *val)
447 {
448     XChangeProperty(obt_display, win, prop, OBT_PROP_ATOM(UTF8), 8,
449                     PropModeReplace, (const guchar*)val, strlen(val));
450 }
451
452 void obt_prop_set_strings_utf8(Window win, Atom prop, const gchar **strs)
453 {
454     GString *str;
455     gchar const **s;
456
457     str = g_string_sized_new(0);
458     for (s = strs; *s; ++s) {
459         str = g_string_append(str, *s);
460         str = g_string_append_c(str, '\0');
461     }
462     XChangeProperty(obt_display, win, prop, obt_prop_atom(OBT_PROP_UTF8), 8,
463                     PropModeReplace, (guchar*)str->str, str->len);
464     g_string_free(str, TRUE);
465 }
466
467 void obt_prop_erase(Window win, Atom prop)
468 {
469     XDeleteProperty(obt_display, win, prop);
470 }
471
472 void obt_prop_message(gint screen, Window about, Atom messagetype,
473                       glong data0, glong data1, glong data2, glong data3,
474                       glong data4, glong mask)
475 {
476     obt_prop_message_to(obt_root(screen), about, messagetype,
477                         data0, data1, data2, data3, data4, mask);
478 }
479
480 void obt_prop_message_to(Window to, Window about,
481                          Atom messagetype,
482                          glong data0, glong data1, glong data2, glong data3,
483                          glong data4, glong mask)
484 {
485     XEvent ce;
486     ce.xclient.type = ClientMessage;
487     ce.xclient.message_type = messagetype;
488     ce.xclient.display = obt_display;
489     ce.xclient.window = about;
490     ce.xclient.format = 32;
491     ce.xclient.data.l[0] = data0;
492     ce.xclient.data.l[1] = data1;
493     ce.xclient.data.l[2] = data2;
494     ce.xclient.data.l[3] = data3;
495     ce.xclient.data.l[4] = data4;
496     XSendEvent(obt_display, to, FALSE, mask, &ce);
497 }