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