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