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