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