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