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