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