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