merge the C branch into HEAD
[mikachu/openbox.git] / openbox / prop.c
1 #include "prop.h"
2 #include "openbox.h"
3 #include <X11/Xatom.h>
4
5 Atoms prop_atoms;
6
7 #define CREATE(var, name) (prop_atoms.var = \
8                            XInternAtom(ob_display, name, FALSE))
9
10 void prop_startup()
11 {
12     g_assert(ob_display != NULL);
13
14     CREATE(cardinal, "CARDINAL");
15     CREATE(window, "WINDOW");
16     CREATE(pixmap, "PIXMAP");
17     CREATE(atom, "ATOM");
18     CREATE(string, "STRING");
19     CREATE(utf8, "UTF8_STRING");
20      
21     CREATE(wm_colormap_windows, "WM_COLORMAP_WINDOWS");
22     CREATE(wm_protocols, "WM_PROTOCOLS");
23     CREATE(wm_state, "WM_STATE");
24     CREATE(wm_change_state, "WM_CHANGE_STATE");
25     CREATE(wm_delete_window, "WM_DELETE_WINDOW");
26     CREATE(wm_take_focus, "WM_TAKE_FOCUS");
27     CREATE(wm_name, "WM_NAME");
28     CREATE(wm_icon_name, "WM_ICON_NAME");
29     CREATE(wm_class, "WM_CLASS");
30     CREATE(wm_window_role, "WM_WINDOW_ROLE");
31     CREATE(motif_wm_hints, "_MOTIF_WM_HINTS");
32
33     CREATE(net_supported, "_NET_SUPPORTED");
34     CREATE(net_client_list, "_NET_CLIENT_LIST");
35     CREATE(net_client_list_stacking, "_NET_CLIENT_LIST_STACKING");
36     CREATE(net_number_of_desktops, "_NET_NUMBER_OF_DESKTOPS");
37     CREATE(net_desktop_geometry, "_NET_DESKTOP_GEOMETRY");
38     CREATE(net_desktop_viewport, "_NET_DESKTOP_VIEWPORT");
39     CREATE(net_current_desktop, "_NET_CURRENT_DESKTOP");
40     CREATE(net_desktop_names, "_NET_DESKTOP_NAMES");
41     CREATE(net_active_window, "_NET_ACTIVE_WINDOW");
42     CREATE(net_workarea, "_NET_WORKAREA");
43     CREATE(net_supporting_wm_check, "_NET_SUPPORTING_WM_CHECK");
44 /*   CREATE(net_virtual_roots, "_NET_VIRTUAL_ROOTS"); */
45     CREATE(net_desktop_layout, "_NET_DESKTOP_LAYOUT");
46     CREATE(net_showing_desktop, "_NET_SHOWING_DESKTOP");
47
48     CREATE(net_close_window, "_NET_CLOSE_WINDOW");
49     CREATE(net_wm_moveresize, "_NET_WM_MOVERESIZE");
50
51 /*   CREATE(net_properties, "_NET_PROPERTIES"); */
52     CREATE(net_wm_name, "_NET_WM_NAME");
53     CREATE(net_wm_visible_name, "_NET_WM_VISIBLE_NAME");
54     CREATE(net_wm_icon_name, "_NET_WM_ICON_NAME");
55     CREATE(net_wm_visible_icon_name, "_NET_WM_VISIBLE_ICON_NAME");
56     CREATE(net_wm_desktop, "_NET_WM_DESKTOP");
57     CREATE(net_wm_window_type, "_NET_WM_WINDOW_TYPE");
58     CREATE(net_wm_state, "_NET_WM_STATE");
59     CREATE(net_wm_strut, "_NET_WM_STRUT");
60 /*   CREATE(net_wm_icon_geometry, "_NET_WM_ICON_GEOMETRY"); */
61     CREATE(net_wm_icon, "_NET_WM_ICON");
62 /*   CREATE(net_wm_pid, "_NET_WM_PID"); */
63 /*   CREATE(net_wm_handled_icons, "_NET_WM_HANDLED_ICONS"); */
64     CREATE(net_wm_allowed_actions, "_NET_WM_ALLOWED_ACTIONS");
65
66 /*   CREATE(net_wm_ping, "_NET_WM_PING"); */
67   
68     CREATE(net_wm_window_type_desktop, "_NET_WM_WINDOW_TYPE_DESKTOP");
69     CREATE(net_wm_window_type_dock, "_NET_WM_WINDOW_TYPE_DOCK");
70     CREATE(net_wm_window_type_toolbar, "_NET_WM_WINDOW_TYPE_TOOLBAR");
71     CREATE(net_wm_window_type_menu, "_NET_WM_WINDOW_TYPE_MENU");
72     CREATE(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY");
73     CREATE(net_wm_window_type_splash, "_NET_WM_WINDOW_TYPE_SPLASH");
74     CREATE(net_wm_window_type_dialog, "_NET_WM_WINDOW_TYPE_DIALOG");
75     CREATE(net_wm_window_type_normal, "_NET_WM_WINDOW_TYPE_NORMAL");
76
77     CREATE(net_wm_moveresize_size_topleft, "_NET_WM_MOVERESIZE_SIZE_TOPLEFT");
78     CREATE(net_wm_moveresize_size_topright,
79            "_NET_WM_MOVERESIZE_SIZE_TOPRIGHT");
80     CREATE(net_wm_moveresize_size_bottomleft,
81            "_NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT");
82     CREATE(net_wm_moveresize_size_bottomright,
83            "_NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT");
84     CREATE(net_wm_moveresize_move, "_NET_WM_MOVERESIZE_MOVE");
85  
86     CREATE(net_wm_action_move, "_NET_WM_ACTION_MOVE");
87     CREATE(net_wm_action_resize, "_NET_WM_ACTION_RESIZE");
88     CREATE(net_wm_action_minimize, "_NET_WM_ACTION_MINIMIZE");
89     CREATE(net_wm_action_shade, "_NET_WM_ACTION_SHADE");
90     CREATE(net_wm_action_stick, "_NET_WM_ACTION_STICK");
91     CREATE(net_wm_action_maximize_horz, "_NET_WM_ACTION_MAXIMIZE_HORZ");
92     CREATE(net_wm_action_maximize_vert, "_NET_WM_ACTION_MAXIMIZE_VERT");
93     CREATE(net_wm_action_fullscreen, "_NET_WM_ACTION_FULLSCREEN");
94     CREATE(net_wm_action_change_desktop, "_NET_WM_ACTION_CHANGE_DESKTOP");
95     CREATE(net_wm_action_close, "_NET_WM_ACTION_CLOSE");
96     CREATE(net_wm_state_modal, "_NET_WM_STATE_MODAL");
97     CREATE(net_wm_state_sticky, "_NET_WM_STATE_STICKY");
98     CREATE(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT");
99     CREATE(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ");
100     CREATE(net_wm_state_shaded, "_NET_WM_STATE_SHADED");
101     CREATE(net_wm_state_skip_taskbar, "_NET_WM_STATE_SKIP_TASKBAR");
102     CREATE(net_wm_state_skip_pager, "_NET_WM_STATE_SKIP_PAGER");
103     CREATE(net_wm_state_hidden, "_NET_WM_STATE_HIDDEN");
104     CREATE(net_wm_state_fullscreen, "_NET_WM_STATE_FULLSCREEN");
105     CREATE(net_wm_state_above, "_NET_WM_STATE_ABOVE");
106     CREATE(net_wm_state_below, "_NET_WM_STATE_BELOW");
107   
108     prop_atoms.net_wm_state_add = 1;
109     prop_atoms.net_wm_state_remove = 0;
110     prop_atoms.net_wm_state_toggle = 2;
111
112     prop_atoms.net_wm_orientation_horz = 0;
113     prop_atoms.net_wm_orientation_vert = 1;
114     prop_atoms.net_wm_topleft = 0;
115     prop_atoms.net_wm_topright = 1;
116     prop_atoms.net_wm_bottomright = 2;
117     prop_atoms.net_wm_bottomleft = 3;
118
119     CREATE(kde_net_system_tray_windows, "_KDE_NET_SYSTEM_TRAY_WINDOWS");
120     CREATE(kde_net_wm_system_tray_window_for,
121            "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR");
122     CREATE(kde_net_wm_window_type_override,
123            "_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
124
125     CREATE(kwm_win_icon, "KWM_WIN_ICON");
126   
127     CREATE(rootpmapid, "_XROOTPMAP_ID");
128     CREATE(esetrootid, "ESETROOT_PMAP_ID");
129
130     CREATE(openbox_pid, "_OPENBOX_PID");
131     CREATE(openbox_premax, "_OPENBOX_PREMAX");
132 }
133
134 gboolean prop_get(Window win, Atom prop, Atom type, int size,
135                   guchar **data, gulong num)
136 {
137     gboolean ret = FALSE;
138     int res;
139     guchar *xdata = NULL;
140     Atom ret_type;
141     int ret_size;
142     gulong ret_items, bytes_left;
143     long num32 = 32 / size * num; /* num in 32-bit elements */
144
145     res = XGetWindowProperty(ob_display, win, prop, 0l, num32,
146                              FALSE, type, &ret_type, &ret_size,
147                              &ret_items, &bytes_left, &xdata);
148     if (res == Success && ret_items && xdata) {
149         if (ret_size == size && ret_items >= num) {
150             *data = g_memdup(xdata, num * (size / 8));
151             ret = TRUE;
152         }
153         XFree(xdata);
154     }
155     return ret;
156 }
157
158 gboolean prop_get_prealloc(Window win, Atom prop, Atom type, int size,
159                            guchar *data, gulong num)
160 {
161     gboolean ret = FALSE;
162     int res;
163     guchar *xdata = NULL;
164     Atom ret_type;
165     int ret_size;
166     gulong ret_items, bytes_left;
167     long num32 = 32 / size * num; /* num in 32-bit elements */
168
169     res = XGetWindowProperty(ob_display, win, prop, 0l, num32,
170                              FALSE, type, &ret_type, &ret_size,
171                              &ret_items, &bytes_left, &xdata);
172     if (res == Success && ret_items && xdata) {
173         if (ret_size == size && ret_items >= num) {
174             gulong i;
175             for (i = 0; i < num; ++i)
176                 switch (size) {
177                 case 8:
178                     data[i] = xdata[i];
179                     break;
180                 case 16:
181                     ((guint16*)data)[i] = ((guint16*)xdata)[i];
182                     break;
183                 case 32:
184                     ((guint32*)data)[i] = ((guint32*)xdata)[i];
185                     break;
186                 default:
187                     g_assert_not_reached(); /* unhandled size */
188                 }
189             ret = TRUE;
190         }
191         XFree(xdata);
192     }
193     return ret;
194 }
195
196 gboolean prop_get_all(Window win, Atom prop, Atom type, int size,
197                       guchar **data, gulong *num)
198 {
199     gboolean ret = FALSE;
200     int res;
201     guchar *xdata = NULL;
202     Atom ret_type;
203     int ret_size;
204     gulong ret_items, bytes_left;
205
206     res = XGetWindowProperty(ob_display, win, prop, 0l, G_MAXLONG,
207                              FALSE, type, &ret_type, &ret_size,
208                              &ret_items, &bytes_left, &xdata);
209     if (res == Success) {
210         if (ret_size == size && ret_items > 0) {
211             *data = g_memdup(xdata, ret_items * (size / 8));
212             *num = ret_items;
213             ret = TRUE;
214         }
215         XFree(xdata);
216     }
217     return ret;
218 }
219
220 gboolean prop_get_string(Window win, Atom prop, Atom type, guchar **data)
221 {
222     guchar *raw;
223     gulong num;
224     GString *str;
225      
226     if (prop_get_all(win, prop, type, 8, &raw, &num)) {
227         str = g_string_new_len((char*)raw, num);
228         g_assert(str->str[num] == '\0');
229
230         g_free(raw);
231
232         *data = (guchar*)g_string_free(str, FALSE);
233         return TRUE;
234     }
235     return FALSE;
236 }
237
238 gboolean prop_get_strings(Window win, Atom prop, Atom type,
239                           GPtrArray *data)
240 {
241     guchar *raw;
242     gulong num;
243     GString *str, *str2;
244     guint i, start;
245      
246     if (prop_get_all(win, prop, type, 8, &raw, &num)) {
247         str = g_string_new_len((gchar*)raw, num);
248         g_assert(str->str[num] == '\0'); /* assuming this is always true.. */
249
250         g_free(raw);
251
252         /* split it into the list */
253         for (start = 0, i = 0; i < str->len; ++i) {
254             if (str->str[i] == '\0') {
255                 str2 = g_string_new_len(&str->str[start], i - start);
256                 g_ptr_array_add(data, g_string_free(str2, FALSE));
257                 start = i + 1;
258             }
259         }
260         g_string_free(str, TRUE);
261
262         if (data->len > 0)
263             return TRUE;
264     }
265     return FALSE;
266 }
267
268 void prop_erase(Window win, Atom prop)
269 {
270     XDeleteProperty(ob_display, win, prop);
271 }
272
273 void prop_message(Window about, Atom messagetype, long data0, long data1,
274                   long data2, long data3)
275 {
276     XEvent ce;
277     ce.xclient.type = ClientMessage;
278     ce.xclient.message_type = messagetype;
279     ce.xclient.display = ob_display;
280     ce.xclient.window = about;
281     ce.xclient.format = 32;
282     ce.xclient.data.l[0] = data0;
283     ce.xclient.data.l[1] = data1;
284     ce.xclient.data.l[2] = data2;
285     ce.xclient.data.l[3] = data3;
286     XSendEvent(ob_display, ob_root, FALSE,
287                SubstructureNotifyMask | SubstructureRedirectMask, &ce);
288 }