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