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