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