use const char*'s when we should be
[dana/openbox.git] / openbox / prop.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    prop.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003        Ben 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 "prop.h"
21 #include "openbox.h"
22
23 #include <X11/Xatom.h>
24
25 Atoms prop_atoms;
26
27 #define CREATE(var, name) (prop_atoms.var = \
28                            XInternAtom(ob_display, name, FALSE))
29
30 void prop_startup()
31 {
32     CREATE(cardinal, "CARDINAL");
33     CREATE(window, "WINDOW");
34     CREATE(pixmap, "PIXMAP");
35     CREATE(atom, "ATOM");
36     CREATE(string, "STRING");
37     CREATE(utf8, "UTF8_STRING");
38
39     CREATE(manager, "MANAGER");
40      
41     CREATE(wm_colormap_windows, "WM_COLORMAP_WINDOWS");
42     CREATE(wm_protocols, "WM_PROTOCOLS");
43     CREATE(wm_state, "WM_STATE");
44     CREATE(wm_change_state, "WM_CHANGE_STATE");
45     CREATE(wm_delete_window, "WM_DELETE_WINDOW");
46     CREATE(wm_take_focus, "WM_TAKE_FOCUS");
47     CREATE(wm_name, "WM_NAME");
48     CREATE(wm_icon_name, "WM_ICON_NAME");
49     CREATE(wm_class, "WM_CLASS");
50     CREATE(wm_window_role, "WM_WINDOW_ROLE");
51     CREATE(motif_wm_hints, "_MOTIF_WM_HINTS");
52
53     CREATE(sm_client_id, "SM_CLIENT_ID");
54
55     CREATE(net_wm_full_placement, "_NET_WM_FULL_PLACEMENT");
56
57     CREATE(net_supported, "_NET_SUPPORTED");
58     CREATE(net_client_list, "_NET_CLIENT_LIST");
59     CREATE(net_client_list_stacking, "_NET_CLIENT_LIST_STACKING");
60     CREATE(net_number_of_desktops, "_NET_NUMBER_OF_DESKTOPS");
61     CREATE(net_desktop_geometry, "_NET_DESKTOP_GEOMETRY");
62     CREATE(net_desktop_viewport, "_NET_DESKTOP_VIEWPORT");
63     CREATE(net_current_desktop, "_NET_CURRENT_DESKTOP");
64     CREATE(net_desktop_names, "_NET_DESKTOP_NAMES");
65     CREATE(net_active_window, "_NET_ACTIVE_WINDOW");
66     CREATE(net_workarea, "_NET_WORKAREA");
67     CREATE(net_supporting_wm_check, "_NET_SUPPORTING_WM_CHECK");
68     CREATE(net_desktop_layout, "_NET_DESKTOP_LAYOUT");
69     CREATE(net_showing_desktop, "_NET_SHOWING_DESKTOP");
70
71     CREATE(net_close_window, "_NET_CLOSE_WINDOW");
72     CREATE(net_wm_moveresize, "_NET_WM_MOVERESIZE");
73     CREATE(net_moveresize_window, "_NET_MOVERESIZE_WINDOW");
74
75     CREATE(net_startup_id, "_NET_STARTUP_ID");
76
77     CREATE(net_wm_name, "_NET_WM_NAME");
78     CREATE(net_wm_visible_name, "_NET_WM_VISIBLE_NAME");
79     CREATE(net_wm_icon_name, "_NET_WM_ICON_NAME");
80     CREATE(net_wm_visible_icon_name, "_NET_WM_VISIBLE_ICON_NAME");
81     CREATE(net_wm_desktop, "_NET_WM_DESKTOP");
82     CREATE(net_wm_window_type, "_NET_WM_WINDOW_TYPE");
83     CREATE(net_wm_state, "_NET_WM_STATE");
84     CREATE(net_wm_strut, "_NET_WM_STRUT");
85     CREATE(net_wm_strut_partial, "_NET_WM_STRUT_PARTIAL");
86     CREATE(net_wm_icon, "_NET_WM_ICON");
87 /*   CREATE(net_wm_pid, "_NET_WM_PID"); */
88     CREATE(net_wm_allowed_actions, "_NET_WM_ALLOWED_ACTIONS");
89     CREATE(net_wm_user_time, "_NET_WM_USER_TIME");
90     CREATE(net_frame_extents, "_NET_FRAME_EXTENTS");
91
92 /*   CREATE(net_wm_ping, "_NET_WM_PING"); */
93   
94     CREATE(net_wm_window_type_desktop, "_NET_WM_WINDOW_TYPE_DESKTOP");
95     CREATE(net_wm_window_type_dock, "_NET_WM_WINDOW_TYPE_DOCK");
96     CREATE(net_wm_window_type_toolbar, "_NET_WM_WINDOW_TYPE_TOOLBAR");
97     CREATE(net_wm_window_type_menu, "_NET_WM_WINDOW_TYPE_MENU");
98     CREATE(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY");
99     CREATE(net_wm_window_type_splash, "_NET_WM_WINDOW_TYPE_SPLASH");
100     CREATE(net_wm_window_type_dialog, "_NET_WM_WINDOW_TYPE_DIALOG");
101     CREATE(net_wm_window_type_normal, "_NET_WM_WINDOW_TYPE_NORMAL");
102
103     prop_atoms.net_wm_moveresize_size_topleft = 0;
104     prop_atoms.net_wm_moveresize_size_top = 1;
105     prop_atoms.net_wm_moveresize_size_topright = 2;
106     prop_atoms.net_wm_moveresize_size_right = 3;
107     prop_atoms.net_wm_moveresize_size_bottomright = 4;
108     prop_atoms.net_wm_moveresize_size_bottom = 5;
109     prop_atoms.net_wm_moveresize_size_bottomleft = 6;
110     prop_atoms.net_wm_moveresize_size_left = 7;
111     prop_atoms.net_wm_moveresize_move = 8;
112     prop_atoms.net_wm_moveresize_size_keyboard = 9;
113     prop_atoms.net_wm_moveresize_move_keyboard = 10;
114     prop_atoms.net_wm_moveresize_cancel = 11;
115
116     CREATE(net_wm_action_move, "_NET_WM_ACTION_MOVE");
117     CREATE(net_wm_action_resize, "_NET_WM_ACTION_RESIZE");
118     CREATE(net_wm_action_minimize, "_NET_WM_ACTION_MINIMIZE");
119     CREATE(net_wm_action_shade, "_NET_WM_ACTION_SHADE");
120     CREATE(net_wm_action_stick, "_NET_WM_ACTION_STICK");
121     CREATE(net_wm_action_maximize_horz, "_NET_WM_ACTION_MAXIMIZE_HORZ");
122     CREATE(net_wm_action_maximize_vert, "_NET_WM_ACTION_MAXIMIZE_VERT");
123     CREATE(net_wm_action_fullscreen, "_NET_WM_ACTION_FULLSCREEN");
124     CREATE(net_wm_action_change_desktop, "_NET_WM_ACTION_CHANGE_DESKTOP");
125     CREATE(net_wm_action_close, "_NET_WM_ACTION_CLOSE");
126     CREATE(net_wm_state_modal, "_NET_WM_STATE_MODAL");
127     CREATE(net_wm_state_sticky, "_NET_WM_STATE_STICKY");
128     CREATE(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT");
129     CREATE(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ");
130     CREATE(net_wm_state_shaded, "_NET_WM_STATE_SHADED");
131     CREATE(net_wm_state_skip_taskbar, "_NET_WM_STATE_SKIP_TASKBAR");
132     CREATE(net_wm_state_skip_pager, "_NET_WM_STATE_SKIP_PAGER");
133     CREATE(net_wm_state_hidden, "_NET_WM_STATE_HIDDEN");
134     CREATE(net_wm_state_fullscreen, "_NET_WM_STATE_FULLSCREEN");
135     CREATE(net_wm_state_above, "_NET_WM_STATE_ABOVE");
136     CREATE(net_wm_state_below, "_NET_WM_STATE_BELOW");
137     CREATE(net_wm_state_demands_attention, "_NET_WM_STATE_DEMANDS_ATTENTION");
138   
139     prop_atoms.net_wm_state_add = 1;
140     prop_atoms.net_wm_state_remove = 0;
141     prop_atoms.net_wm_state_toggle = 2;
142
143     prop_atoms.net_wm_orientation_horz = 0;
144     prop_atoms.net_wm_orientation_vert = 1;
145     prop_atoms.net_wm_topleft = 0;
146     prop_atoms.net_wm_topright = 1;
147     prop_atoms.net_wm_bottomright = 2;
148     prop_atoms.net_wm_bottomleft = 3;
149
150     CREATE(kde_wm_change_state, "_KDE_WM_CHANGE_STATE");
151     CREATE(kde_net_wm_window_type_override,"_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
152
153     CREATE(rootpmapid, "_XROOTPMAP_ID");
154     CREATE(esetrootid, "ESETROOT_PMAP_ID");
155
156     CREATE(openbox_pid, "_OPENBOX_PID");
157     CREATE(ob_wm_state_undecorated, "_OB_WM_STATE_UNDECORATED");
158     CREATE(ob_control, "_OB_CONTROL");
159 }
160
161 #include <X11/Xutil.h>
162 #include <glib.h>
163 #include <string.h>
164
165 /* this just isn't used... and it also breaks on 64bit, watch out
166 static gboolean get(Window win, Atom prop, Atom type, gint size,
167                     guchar **data, gulong num)
168 {
169     gboolean ret = FALSE;
170     gint res;
171     guchar *xdata = NULL;
172     Atom ret_type;
173     gint ret_size;
174     gulong ret_items, bytes_left;
175     glong num32 = 32 / size * num; /\* num in 32-bit elements *\/
176
177     res = XGetWindowProperty(display, win, prop, 0l, num32,
178                              FALSE, type, &ret_type, &ret_size,
179                              &ret_items, &bytes_left, &xdata);
180     if (res == Success && ret_items && xdata) {
181         if (ret_size == size && ret_items >= num) {
182             *data = g_memdup(xdata, num * (size / 8));
183             ret = TRUE;
184         }
185         XFree(xdata);
186     }
187     return ret;
188 }
189 */
190
191 static gboolean get_prealloc(Window win, Atom prop, Atom type, gint size,
192                              guchar *data, gulong num)
193 {
194     gboolean ret = FALSE;
195     gint res;
196     guchar *xdata = NULL;
197     Atom ret_type;
198     gint ret_size;
199     gulong ret_items, bytes_left;
200     glong num32 = 32 / size * num; /* num in 32-bit elements */
201
202     res = XGetWindowProperty(ob_display, win, prop, 0l, num32,
203                              FALSE, type, &ret_type, &ret_size,
204                              &ret_items, &bytes_left, &xdata);
205     if (res == Success && ret_items && xdata) {
206         if (ret_size == size && ret_items >= num) {
207             guint i;
208             for (i = 0; i < num; ++i)
209                 switch (size) {
210                 case 8:
211                     data[i] = xdata[i];
212                     break;
213                 case 16:
214                     ((guint16*)data)[i] = ((gushort*)xdata)[i];
215                     break;
216                 case 32:
217                     ((guint32*)data)[i] = ((gulong*)xdata)[i];
218                     break;
219                 default:
220                     g_assert_not_reached(); /* unhandled size */
221                 }
222             ret = TRUE;
223         }
224         XFree(xdata);
225     }
226     return ret;
227 }
228
229 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
230                         guchar **data, guint *num)
231 {
232     gboolean ret = FALSE;
233     gint res;
234     guchar *xdata = NULL;
235     Atom ret_type;
236     gint ret_size;
237     gulong ret_items, bytes_left;
238
239     res = XGetWindowProperty(ob_display, win, prop, 0l, G_MAXLONG,
240                              FALSE, type, &ret_type, &ret_size,
241                              &ret_items, &bytes_left, &xdata);
242     if (res == Success) {
243         if (ret_size == size && ret_items > 0) {
244             guint i;
245
246             *data = g_malloc(ret_items * (size / 8));
247             for (i = 0; i < ret_items; ++i)
248                 switch (size) {
249                 case 8:
250                     (*data)[i] = xdata[i];
251                     break;
252                 case 16:
253                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
254                     break;
255                 case 32:
256                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
257                     break;
258                 default:
259                     g_assert_not_reached(); /* unhandled size */
260                 }
261             *num = ret_items;
262             ret = TRUE;
263         }
264         XFree(xdata);
265     }
266     return ret;
267 }
268
269 static gboolean get_stringlist(Window win, Atom prop, gchar ***list, gint *nstr)
270 {
271     XTextProperty tprop;
272     gboolean ret = FALSE;
273
274     if (XGetTextProperty(ob_display, win, &tprop, prop) && tprop.nitems) {
275         if (XTextPropertyToStringList(&tprop, list, nstr))
276             ret = TRUE;
277         XFree(tprop.value);
278     }
279     return ret;
280 }
281
282 gboolean prop_get32(Window win, Atom prop, Atom type, guint32 *ret)
283 {
284     return get_prealloc(win, prop, type, 32, (guchar*)ret, 1);
285 }
286
287 gboolean prop_get_array32(Window win, Atom prop, Atom type, guint32 **ret,
288                           guint *nret)
289 {
290     return get_all(win, prop, type, 32, (guchar**)ret, nret);
291 }
292
293 gboolean prop_get_string_locale(Window win, Atom prop, gchar **ret)
294 {
295     gchar **list;
296     gint nstr;
297     gchar *s;
298
299     if (get_stringlist(win, prop, &list, &nstr) && nstr) {
300         s = g_convert(list[0], strlen(list[0]), "UTF-8", "ISO-8859-1",
301                       NULL, NULL, NULL);
302         XFreeStringList(list);
303         if (s) {
304             *ret = s;
305             return TRUE;
306         }
307     }
308     return FALSE;
309 }
310
311 gboolean prop_get_strings_locale(Window win, Atom prop, gchar ***ret)
312 {
313     GSList *strs = NULL, *it;
314     gchar *raw, *p;
315     guint num, i, count = 0;
316
317     if (get_all(win, prop, prop_atoms.string, 8, (guchar**)&raw, &num)) {
318
319         p = raw;
320         while (p < raw + num - 1) {
321             ++count;
322             strs = g_slist_append(strs, p);
323             p += strlen(p) + 1; /* next string */
324         }
325
326         *ret = g_new0(gchar*, count + 1);
327         (*ret)[count] = NULL; /* null terminated list */
328
329         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
330             (*ret)[i] = g_convert(it->data, -1, "UTF-8", "ISO-8859-1",
331                                   NULL, NULL, NULL);
332             /* make sure translation did not fail */
333             if (!(*ret)[i])
334                 (*ret)[i] = g_strdup("");
335         }
336         g_free(raw);
337         g_slist_free(strs);
338         return TRUE;
339     }
340     return FALSE;
341 }
342
343 gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
344 {
345     gchar *raw;
346     gchar *str;
347     guint num;
348      
349     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
350         str = g_strndup(raw, num); /* grab the first string from the list */
351         g_free(raw);
352         if (g_utf8_validate(str, -1, NULL)) {
353             *ret = str;
354             return TRUE;
355         }
356         g_free(str);
357     }
358     return FALSE;
359 }
360
361 gboolean prop_get_strings_utf8(Window win, Atom prop, gchar ***ret)
362 {
363     GSList *strs = NULL, *it;
364     gchar *raw, *p;
365     guint num, i, count = 0;
366
367     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
368
369         p = raw;
370         while (p < raw + num - 1) {
371             ++count;
372             strs = g_slist_append(strs, p);
373             p += strlen(p) + 1; /* next string */
374         }
375
376         *ret = g_new0(gchar*, count + 1);
377
378         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
379             if (g_utf8_validate(it->data, -1, NULL))
380                 (*ret)[i] = g_strdup(it->data);
381             else
382                 (*ret)[i] = g_strdup("");
383         }
384         g_free(raw);
385         g_slist_free(strs);
386         return TRUE;
387     }
388     return FALSE;
389 }
390
391 void prop_set32(Window win, Atom prop, Atom type, gulong val)
392 {
393     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
394                     (guchar*)&val, 1);
395 }
396
397 void prop_set_array32(Window win, Atom prop, Atom type, gulong *val,
398                       guint num)
399 {
400     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
401                     (guchar*)val, num);
402 }
403
404 void prop_set_string_utf8(Window win, Atom prop, const gchar *val)
405 {
406     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
407                     PropModeReplace, (guchar*)val, strlen(val));
408 }
409
410 void prop_set_strings_utf8(Window win, Atom prop, gchar **strs)
411 {
412     GString *str;
413     gchar **s;
414
415     str = g_string_sized_new(0);
416     for (s = strs; *s; ++s) {
417         str = g_string_append(str, *s);
418         str = g_string_append_c(str, '\0');
419     }
420     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
421                     PropModeReplace, (guchar*)str->str, str->len);
422     g_string_free(str, TRUE);
423 }
424
425 void prop_erase(Window win, Atom prop)
426 {
427     XDeleteProperty(ob_display, win, prop);
428 }
429
430 void prop_message(Window about, Atom messagetype, glong data0, glong data1,
431                   glong data2, glong data3, glong mask)
432 {
433     XEvent ce;
434     ce.xclient.type = ClientMessage;
435     ce.xclient.message_type = messagetype;
436     ce.xclient.display = ob_display;
437     ce.xclient.window = about;
438     ce.xclient.format = 32;
439     ce.xclient.data.l[0] = data0;
440     ce.xclient.data.l[1] = data1;
441     ce.xclient.data.l[2] = data2;
442     ce.xclient.data.l[3] = data3;
443     XSendEvent(ob_display, RootWindow(ob_display, ob_screen), FALSE,
444                mask, &ce);
445 }