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