]> icculus.org git repositories - dana/openbox.git/blob - openbox/prop.c
use the new ptr cursor defn
[dana/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
49     CREATE(net_wm_name, "_NET_WM_NAME");
50     CREATE(net_wm_visible_name, "_NET_WM_VISIBLE_NAME");
51     CREATE(net_wm_icon_name, "_NET_WM_ICON_NAME");
52     CREATE(net_wm_visible_icon_name, "_NET_WM_VISIBLE_ICON_NAME");
53     CREATE(net_wm_desktop, "_NET_WM_DESKTOP");
54     CREATE(net_wm_window_type, "_NET_WM_WINDOW_TYPE");
55     CREATE(net_wm_state, "_NET_WM_STATE");
56     CREATE(net_wm_strut, "_NET_WM_STRUT");
57     CREATE(net_wm_icon, "_NET_WM_ICON");
58 /*   CREATE(net_wm_pid, "_NET_WM_PID"); */
59     CREATE(net_wm_allowed_actions, "_NET_WM_ALLOWED_ACTIONS");
60
61 /*   CREATE(net_wm_ping, "_NET_WM_PING"); */
62   
63     CREATE(net_wm_window_type_desktop, "_NET_WM_WINDOW_TYPE_DESKTOP");
64     CREATE(net_wm_window_type_dock, "_NET_WM_WINDOW_TYPE_DOCK");
65     CREATE(net_wm_window_type_toolbar, "_NET_WM_WINDOW_TYPE_TOOLBAR");
66     CREATE(net_wm_window_type_menu, "_NET_WM_WINDOW_TYPE_MENU");
67     CREATE(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY");
68     CREATE(net_wm_window_type_splash, "_NET_WM_WINDOW_TYPE_SPLASH");
69     CREATE(net_wm_window_type_dialog, "_NET_WM_WINDOW_TYPE_DIALOG");
70     CREATE(net_wm_window_type_normal, "_NET_WM_WINDOW_TYPE_NORMAL");
71
72     prop_atoms.net_wm_moveresize_size_topleft = 0;
73     prop_atoms.net_wm_moveresize_size_top = 1;
74     prop_atoms.net_wm_moveresize_size_topright = 2;
75     prop_atoms.net_wm_moveresize_size_right = 3;
76     prop_atoms.net_wm_moveresize_size_bottomright = 4;
77     prop_atoms.net_wm_moveresize_size_bottom = 5;
78     prop_atoms.net_wm_moveresize_size_bottomleft = 6;
79     prop_atoms.net_wm_moveresize_size_left = 7;
80     prop_atoms.net_wm_moveresize_move = 8;
81     prop_atoms.net_wm_moveresize_size_keyboard = 9;
82     prop_atoms.net_wm_moveresize_move_keyboard = 10;
83
84     CREATE(net_wm_action_move, "_NET_WM_ACTION_MOVE");
85     CREATE(net_wm_action_resize, "_NET_WM_ACTION_RESIZE");
86     CREATE(net_wm_action_minimize, "_NET_WM_ACTION_MINIMIZE");
87     CREATE(net_wm_action_shade, "_NET_WM_ACTION_SHADE");
88     CREATE(net_wm_action_stick, "_NET_WM_ACTION_STICK");
89     CREATE(net_wm_action_maximize_horz, "_NET_WM_ACTION_MAXIMIZE_HORZ");
90     CREATE(net_wm_action_maximize_vert, "_NET_WM_ACTION_MAXIMIZE_VERT");
91     CREATE(net_wm_action_fullscreen, "_NET_WM_ACTION_FULLSCREEN");
92     CREATE(net_wm_action_change_desktop, "_NET_WM_ACTION_CHANGE_DESKTOP");
93     CREATE(net_wm_action_close, "_NET_WM_ACTION_CLOSE");
94     CREATE(net_wm_state_modal, "_NET_WM_STATE_MODAL");
95     CREATE(net_wm_state_sticky, "_NET_WM_STATE_STICKY");
96     CREATE(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT");
97     CREATE(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ");
98     CREATE(net_wm_state_shaded, "_NET_WM_STATE_SHADED");
99     CREATE(net_wm_state_skip_taskbar, "_NET_WM_STATE_SKIP_TASKBAR");
100     CREATE(net_wm_state_skip_pager, "_NET_WM_STATE_SKIP_PAGER");
101     CREATE(net_wm_state_hidden, "_NET_WM_STATE_HIDDEN");
102     CREATE(net_wm_state_fullscreen, "_NET_WM_STATE_FULLSCREEN");
103     CREATE(net_wm_state_above, "_NET_WM_STATE_ABOVE");
104     CREATE(net_wm_state_below, "_NET_WM_STATE_BELOW");
105   
106     prop_atoms.net_wm_state_add = 1;
107     prop_atoms.net_wm_state_remove = 0;
108     prop_atoms.net_wm_state_toggle = 2;
109
110     prop_atoms.net_wm_orientation_horz = 0;
111     prop_atoms.net_wm_orientation_vert = 1;
112     prop_atoms.net_wm_topleft = 0;
113     prop_atoms.net_wm_topright = 1;
114     prop_atoms.net_wm_bottomright = 2;
115     prop_atoms.net_wm_bottomleft = 3;
116
117     CREATE(kde_net_system_tray_windows, "_KDE_NET_SYSTEM_TRAY_WINDOWS");
118     CREATE(kde_net_wm_system_tray_window_for,
119            "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
120     CREATE(kde_net_wm_window_type_override,
121            "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
122
123     CREATE(kwm_win_icon, "KWM_WIN_ICON");
124   
125     CREATE(rootpmapid, "_XROOTPMAP_ID");
126     CREATE(esetrootid, "ESETROOT_PMAP_ID");
127
128     CREATE(openbox_pid, "_OPENBOX_PID");
129     CREATE(openbox_premax, "_OPENBOX_PREMAX");
130 }
131
132 #include <X11/Xutil.h>
133 #include <glib.h>
134 #include <string.h>
135
136 /* this just isn't used... and it also breaks on 64bit, watch out
137 static gboolean get(Window win, Atom prop, Atom type, int size,
138                     guchar **data, gulong num)
139 {
140     gboolean ret = FALSE;
141     int res;
142     guchar *xdata = NULL;
143     Atom ret_type;
144     int ret_size;
145     gulong ret_items, bytes_left;
146     long num32 = 32 / size * num; /\* num in 32-bit elements *\/
147
148     res = XGetWindowProperty(display, win, prop, 0l, num32,
149                              FALSE, type, &ret_type, &ret_size,
150                              &ret_items, &bytes_left, &xdata);
151     if (res == Success && ret_items && xdata) {
152         if (ret_size == size && ret_items >= num) {
153             *data = g_memdup(xdata, num * (size / 8));
154             ret = TRUE;
155         }
156         XFree(xdata);
157     }
158     return ret;
159 }
160 */
161
162 static gboolean get_prealloc(Window win, Atom prop, Atom type, int size,
163                              guchar *data, gulong num)
164 {
165     gboolean ret = FALSE;
166     int res;
167     guchar *xdata = NULL;
168     Atom ret_type;
169     int ret_size;
170     gulong ret_items, bytes_left;
171     long num32 = 32 / size * num; /* num in 32-bit elements */
172
173     res = XGetWindowProperty(ob_display, win, prop, 0l, num32,
174                              FALSE, type, &ret_type, &ret_size,
175                              &ret_items, &bytes_left, &xdata);
176     if (res == Success && ret_items && xdata) {
177         if (ret_size == size && ret_items >= num) {
178             guint i;
179             for (i = 0; i < num; ++i)
180                 switch (size) {
181                 case 8:
182                     data[i] = xdata[i];
183                     break;
184                 case 16:
185                     ((guint16*)data)[i] = ((guint16*)xdata)[i];
186                     break;
187                 case 32:
188                     ((guint32*)data)[i] = ((guint32*)xdata)[i];
189                     break;
190                 default:
191                     g_assert_not_reached(); /* unhandled size */
192                 }
193             ret = TRUE;
194         }
195         XFree(xdata);
196     }
197     return ret;
198 }
199
200 static gboolean get_all(Window win, Atom prop, Atom type, int size,
201                         guchar **data, guint *num)
202 {
203     gboolean ret = FALSE;
204     int res;
205     guchar *xdata = NULL;
206     Atom ret_type;
207     int ret_size;
208     gulong ret_items, bytes_left;
209
210     res = XGetWindowProperty(ob_display, win, prop, 0l, G_MAXLONG,
211                              FALSE, type, &ret_type, &ret_size,
212                              &ret_items, &bytes_left, &xdata);
213     if (res == Success) {
214         if (ret_size == size && ret_items > 0) {
215             guint i;
216
217             *data = g_malloc(ret_items * (size / 8));
218             for (i = 0; i < ret_items; ++i)
219                 switch (size) {
220                 case 8:
221                     (*data)[i] = xdata[i];
222                     break;
223                 case 16:
224                     ((guint16*)*data)[i] = ((guint16*)xdata)[i];
225                     break;
226                 case 32:
227                     ((guint32*)*data)[i] = ((guint32*)xdata)[i];
228                     break;
229                 default:
230                     g_assert_not_reached(); /* unhandled size */
231                 }
232             *num = ret_items;
233             ret = TRUE;
234         }
235         XFree(xdata);
236     }
237     return ret;
238 }
239
240 static gboolean get_stringlist(Window win, Atom prop, char ***list, int *nstr)
241 {
242     XTextProperty tprop;
243     gboolean ret = FALSE;
244
245     if (XGetTextProperty(ob_display, win, &tprop, prop) && tprop.nitems) {
246         if (XTextPropertyToStringList(&tprop, list, nstr))
247             ret = TRUE;
248         XFree(tprop.value);
249     }
250     return ret;
251 }
252
253 gboolean prop_get32(Window win, Atom prop, Atom type, guint32 *ret)
254 {
255     return get_prealloc(win, prop, type, 32, (guchar*)ret, 1);
256 }
257
258 gboolean prop_get_array32(Window win, Atom prop, Atom type, guint32 **ret,
259                           guint *nret)
260 {
261     return get_all(win, prop, type, 32, (guchar**)ret, nret);
262 }
263
264 gboolean prop_get_string_locale(Window win, Atom prop, char **ret)
265 {
266     char **list;
267     int nstr;
268
269     if (get_stringlist(win, prop, &list, &nstr) && nstr) {
270         *ret = g_locale_to_utf8(list[0], -1, NULL, NULL, NULL);
271         XFreeStringList(list);
272         if (ret) return TRUE;
273     }
274     return FALSE;
275 }
276
277 gboolean prop_get_strings_locale(Window win, Atom prop, char ***ret)
278 {
279     char *raw, *p;
280     guint num, i;
281
282     if (get_all(win, prop, prop_atoms.string, 8, (guchar**)&raw, &num)){
283         *ret = g_new(char*, num + 1);
284         (*ret)[num] = NULL; /* null terminated list */
285
286         p = raw;
287         for (i = 0; i < num; ++i) {
288             (*ret)[i] = g_locale_to_utf8(p, -1, NULL, NULL, NULL);
289             /* make sure translation did not fail */
290             if (!(*ret)[i]) {
291                 g_strfreev(*ret); /* free what we did so far */
292                 break; /* the force is not strong with us */
293             }
294             p = strchr(p, '\0');
295         }
296         g_free(raw);
297         if (i == num)
298             return TRUE;
299     }
300     return FALSE;
301 }
302
303 gboolean prop_get_string_utf8(Window win, Atom prop, char **ret)
304 {
305     char *raw;
306     guint num;
307      
308     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
309         *ret = g_strndup(raw, num); /* grab the first string from the list */
310         g_free(raw);
311         return TRUE;
312     }
313     return FALSE;
314 }
315
316 gboolean prop_get_strings_utf8(Window win, Atom prop, char ***ret)
317 {
318     char *raw, *p;
319     guint num, i;
320
321     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
322         *ret = g_new(char*, num + 1);
323         (*ret)[num] = NULL; /* null terminated list */
324
325         p = raw;
326         for (i = 0; i < num; ++i) {
327             (*ret)[i] = g_strdup(p);
328             p = strchr(p, '\0');
329         }
330         g_free(raw);
331         return TRUE;
332     }
333     return FALSE;
334 }
335
336 void prop_set32(Window win, Atom prop, Atom type, guint32 val)
337 {
338     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
339                     (guchar*)&val, 1);
340 }
341
342 void prop_set_array32(Window win, Atom prop, Atom type, guint32 *val,
343                       guint num)
344 {
345     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
346                     (guchar*)val, num);
347 }
348
349 void prop_set_string_utf8(Window win, Atom prop, char *val)
350 {
351     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
352                     PropModeReplace, (guchar*)val, strlen(val));
353 }
354
355 void prop_set_strings_utf8(Window win, Atom prop, char **strs)
356 {
357     GString *str;
358     char **s;
359
360     str = g_string_sized_new(0);
361     for (s = strs; *s; ++s) {
362         str = g_string_append(str, *s);
363         str = g_string_append_c(str, '\0');
364     }
365     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
366                     PropModeReplace, (guchar*)str->str, str->len);
367 }
368
369 void prop_erase(Window win, Atom prop)
370 {
371     XDeleteProperty(ob_display, win, prop);
372 }
373
374 void prop_message(Window about, Atom messagetype, long data0, long data1,
375                   long data2, long data3)
376 {
377     XEvent ce;
378     ce.xclient.type = ClientMessage;
379     ce.xclient.message_type = messagetype;
380     ce.xclient.display = ob_display;
381     ce.xclient.window = about;
382     ce.xclient.format = 32;
383     ce.xclient.data.l[0] = data0;
384     ce.xclient.data.l[1] = data1;
385     ce.xclient.data.l[2] = data2;
386     ce.xclient.data.l[3] = data3;
387     XSendEvent(ob_display, ob_root, FALSE,
388                SubstructureNotifyMask | SubstructureRedirectMask, &ce);
389 }