]> icculus.org git repositories - mikachu/openbox.git/blob - openbox/prop.c
we werent dropping it in screen, we were dropping it in prop
[mikachu/openbox.git] / openbox / prop.c
1 /* -*- indent-tabs-mode: nil; tab-width: 4; c-basic-offset: 4; -*-
2
3    prop.c for the Openbox window manager
4    Copyright (c) 2006        Mikael Magnusson
5    Copyright (c) 2003-2007   Dana Jansens
6
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16
17    See the COPYING file for a copy of the GNU General Public License.
18 */
19
20 #include "prop.h"
21 #include "openbox.h"
22
23 #include <X11/Xatom.h>
24
25 Atoms prop_atoms;
26
27 #define CREATE(var, name) (prop_atoms.var = \
28                            XInternAtom(ob_display, name, FALSE))
29
30 void prop_startup()
31 {
32     CREATE(cardinal, "CARDINAL");
33     CREATE(window, "WINDOW");
34     CREATE(pixmap, "PIXMAP");
35     CREATE(atom, "ATOM");
36     CREATE(string, "STRING");
37     CREATE(utf8, "UTF8_STRING");
38
39     CREATE(manager, "MANAGER");
40      
41     CREATE(wm_colormap_windows, "WM_COLORMAP_WINDOWS");
42     CREATE(wm_protocols, "WM_PROTOCOLS");
43     CREATE(wm_state, "WM_STATE");
44     CREATE(wm_change_state, "WM_CHANGE_STATE");
45     CREATE(wm_delete_window, "WM_DELETE_WINDOW");
46     CREATE(wm_take_focus, "WM_TAKE_FOCUS");
47     CREATE(wm_name, "WM_NAME");
48     CREATE(wm_icon_name, "WM_ICON_NAME");
49     CREATE(wm_class, "WM_CLASS");
50     CREATE(wm_window_role, "WM_WINDOW_ROLE");
51     CREATE(wm_client_machine, "WM_CLIENT_MACHINE");
52     CREATE(wm_command, "WM_COMMAND");
53     CREATE(wm_client_leader, "WM_CLIENT_LEADER");
54     CREATE(motif_wm_hints, "_MOTIF_WM_HINTS");
55
56     CREATE(sm_client_id, "SM_CLIENT_ID");
57
58     CREATE(net_wm_full_placement, "_NET_WM_FULL_PLACEMENT");
59
60     CREATE(net_supported, "_NET_SUPPORTED");
61     CREATE(net_client_list, "_NET_CLIENT_LIST");
62     CREATE(net_client_list_stacking, "_NET_CLIENT_LIST_STACKING");
63     CREATE(net_number_of_desktops, "_NET_NUMBER_OF_DESKTOPS");
64     CREATE(net_desktop_geometry, "_NET_DESKTOP_GEOMETRY");
65     CREATE(net_desktop_viewport, "_NET_DESKTOP_VIEWPORT");
66     CREATE(net_current_desktop, "_NET_CURRENT_DESKTOP");
67     CREATE(net_desktop_names, "_NET_DESKTOP_NAMES");
68     CREATE(net_active_window, "_NET_ACTIVE_WINDOW");
69 /*    CREATE(net_restack_window, "_NET_RESTACK_WINDOW");*/
70     CREATE(net_workarea, "_NET_WORKAREA");
71     CREATE(net_supporting_wm_check, "_NET_SUPPORTING_WM_CHECK");
72     CREATE(net_desktop_layout, "_NET_DESKTOP_LAYOUT");
73     CREATE(net_showing_desktop, "_NET_SHOWING_DESKTOP");
74
75     CREATE(net_close_window, "_NET_CLOSE_WINDOW");
76     CREATE(net_wm_moveresize, "_NET_WM_MOVERESIZE");
77     CREATE(net_moveresize_window, "_NET_MOVERESIZE_WINDOW");
78
79     CREATE(net_startup_id, "_NET_STARTUP_ID");
80
81     CREATE(net_wm_name, "_NET_WM_NAME");
82     CREATE(net_wm_visible_name, "_NET_WM_VISIBLE_NAME");
83     CREATE(net_wm_icon_name, "_NET_WM_ICON_NAME");
84     CREATE(net_wm_visible_icon_name, "_NET_WM_VISIBLE_ICON_NAME");
85     CREATE(net_wm_desktop, "_NET_WM_DESKTOP");
86     CREATE(net_wm_window_type, "_NET_WM_WINDOW_TYPE");
87     CREATE(net_wm_state, "_NET_WM_STATE");
88     CREATE(net_wm_strut, "_NET_WM_STRUT");
89     CREATE(net_wm_strut_partial, "_NET_WM_STRUT_PARTIAL");
90     CREATE(net_wm_icon, "_NET_WM_ICON");
91     CREATE(net_wm_icon_geometry, "_NET_WM_ICON_GEOMETRY");
92 /*   CREATE(net_wm_pid, "_NET_WM_PID"); */
93     CREATE(net_wm_allowed_actions, "_NET_WM_ALLOWED_ACTIONS");
94     CREATE(net_wm_user_time, "_NET_WM_USER_TIME");
95     CREATE(kde_net_wm_frame_strut, "_KDE_NET_WM_FRAME_STRUT");
96     CREATE(net_frame_extents, "_NET_FRAME_EXTENTS");
97
98 /*   CREATE(net_wm_ping, "_NET_WM_PING"); */
99 #ifdef SYNC
100     CREATE(net_wm_sync_request, "_NET_WM_SYNC_REQUEST");
101     CREATE(net_wm_sync_request_counter, "_NET_WM_SYNC_REQUEST_COUNTER");
102 #endif
103
104     CREATE(net_wm_window_type_desktop, "_NET_WM_WINDOW_TYPE_DESKTOP");
105     CREATE(net_wm_window_type_dock, "_NET_WM_WINDOW_TYPE_DOCK");
106     CREATE(net_wm_window_type_toolbar, "_NET_WM_WINDOW_TYPE_TOOLBAR");
107     CREATE(net_wm_window_type_menu, "_NET_WM_WINDOW_TYPE_MENU");
108     CREATE(net_wm_window_type_utility, "_NET_WM_WINDOW_TYPE_UTILITY");
109     CREATE(net_wm_window_type_splash, "_NET_WM_WINDOW_TYPE_SPLASH");
110     CREATE(net_wm_window_type_dialog, "_NET_WM_WINDOW_TYPE_DIALOG");
111     CREATE(net_wm_window_type_normal, "_NET_WM_WINDOW_TYPE_NORMAL");
112
113     prop_atoms.net_wm_moveresize_size_topleft = 0;
114     prop_atoms.net_wm_moveresize_size_top = 1;
115     prop_atoms.net_wm_moveresize_size_topright = 2;
116     prop_atoms.net_wm_moveresize_size_right = 3;
117     prop_atoms.net_wm_moveresize_size_bottomright = 4;
118     prop_atoms.net_wm_moveresize_size_bottom = 5;
119     prop_atoms.net_wm_moveresize_size_bottomleft = 6;
120     prop_atoms.net_wm_moveresize_size_left = 7;
121     prop_atoms.net_wm_moveresize_move = 8;
122     prop_atoms.net_wm_moveresize_size_keyboard = 9;
123     prop_atoms.net_wm_moveresize_move_keyboard = 10;
124     prop_atoms.net_wm_moveresize_cancel = 11;
125
126     CREATE(net_wm_action_move, "_NET_WM_ACTION_MOVE");
127     CREATE(net_wm_action_resize, "_NET_WM_ACTION_RESIZE");
128     CREATE(net_wm_action_minimize, "_NET_WM_ACTION_MINIMIZE");
129     CREATE(net_wm_action_shade, "_NET_WM_ACTION_SHADE");
130     CREATE(net_wm_action_maximize_horz, "_NET_WM_ACTION_MAXIMIZE_HORZ");
131     CREATE(net_wm_action_maximize_vert, "_NET_WM_ACTION_MAXIMIZE_VERT");
132     CREATE(net_wm_action_fullscreen, "_NET_WM_ACTION_FULLSCREEN");
133     CREATE(net_wm_action_change_desktop, "_NET_WM_ACTION_CHANGE_DESKTOP");
134     CREATE(net_wm_action_close, "_NET_WM_ACTION_CLOSE");
135     CREATE(net_wm_state_modal, "_NET_WM_STATE_MODAL");
136 /*    CREATE(net_wm_state_sticky, "_NET_WM_STATE_STICKY");*/
137     CREATE(net_wm_state_maximized_vert, "_NET_WM_STATE_MAXIMIZED_VERT");
138     CREATE(net_wm_state_maximized_horz, "_NET_WM_STATE_MAXIMIZED_HORZ");
139     CREATE(net_wm_state_shaded, "_NET_WM_STATE_SHADED");
140     CREATE(net_wm_state_skip_taskbar, "_NET_WM_STATE_SKIP_TASKBAR");
141     CREATE(net_wm_state_skip_pager, "_NET_WM_STATE_SKIP_PAGER");
142     CREATE(net_wm_state_hidden, "_NET_WM_STATE_HIDDEN");
143     CREATE(net_wm_state_fullscreen, "_NET_WM_STATE_FULLSCREEN");
144     CREATE(net_wm_state_above, "_NET_WM_STATE_ABOVE");
145     CREATE(net_wm_state_below, "_NET_WM_STATE_BELOW");
146     CREATE(net_wm_state_demands_attention, "_NET_WM_STATE_DEMANDS_ATTENTION");
147   
148     prop_atoms.net_wm_state_add = 1;
149     prop_atoms.net_wm_state_remove = 0;
150     prop_atoms.net_wm_state_toggle = 2;
151
152     prop_atoms.net_wm_orientation_horz = 0;
153     prop_atoms.net_wm_orientation_vert = 1;
154     prop_atoms.net_wm_topleft = 0;
155     prop_atoms.net_wm_topright = 1;
156     prop_atoms.net_wm_bottomright = 2;
157     prop_atoms.net_wm_bottomleft = 3;
158
159     CREATE(kde_wm_change_state, "_KDE_WM_CHANGE_STATE");
160     CREATE(kde_net_wm_window_type_override,"_KDE_NET_WM_WINDOW_TYPE_OVERRIDE");
161
162 /*
163     CREATE(rootpmapid, "_XROOTPMAP_ID");
164     CREATE(esetrootid, "ESETROOT_PMAP_ID");
165 */
166
167     CREATE(openbox_pid, "_OPENBOX_PID");
168     CREATE(openbox_config, "_OPENBOX_CONFIG");
169     CREATE(openbox_wm_state_undecorated, "_OB_WM_STATE_UNDECORATED");
170     CREATE(openbox_control, "_OB_CONTROL");
171 }
172
173 #include <X11/Xutil.h>
174 #include <glib.h>
175 #include <string.h>
176
177 /* this just isn't used... and it also breaks on 64bit, watch out
178 static gboolean get(Window win, Atom prop, Atom type, gint size,
179                     guchar **data, gulong num)
180 {
181     gboolean ret = FALSE;
182     gint res;
183     guchar *xdata = NULL;
184     Atom ret_type;
185     gint ret_size;
186     gulong ret_items, bytes_left;
187     glong num32 = 32 / size * num; /\* num in 32-bit elements *\/
188
189     res = XGetWindowProperty(display, win, prop, 0l, num32,
190                              FALSE, type, &ret_type, &ret_size,
191                              &ret_items, &bytes_left, &xdata);
192     if (res == Success && ret_items && xdata) {
193         if (ret_size == size && ret_items >= num) {
194             *data = g_memdup(xdata, num * (size / 8));
195             ret = TRUE;
196         }
197         XFree(xdata);
198     }
199     return ret;
200 }
201 */
202
203 static gboolean get_prealloc(Window win, Atom prop, Atom type, gint size,
204                              guchar *data, gulong num)
205 {
206     gboolean ret = FALSE;
207     gint res;
208     guchar *xdata = NULL;
209     Atom ret_type;
210     gint ret_size;
211     gulong ret_items, bytes_left;
212     glong num32 = 32 / size * num; /* num in 32-bit elements */
213
214     res = XGetWindowProperty(ob_display, win, prop, 0l, num32,
215                              FALSE, type, &ret_type, &ret_size,
216                              &ret_items, &bytes_left, &xdata);
217     if (res == Success && ret_items && xdata) {
218         if (ret_size == size && ret_items >= num) {
219             guint i;
220             for (i = 0; i < num; ++i)
221                 switch (size) {
222                 case 8:
223                     data[i] = xdata[i];
224                     break;
225                 case 16:
226                     ((guint16*)data)[i] = ((gushort*)xdata)[i];
227                     break;
228                 case 32:
229                     ((guint32*)data)[i] = ((gulong*)xdata)[i];
230                     break;
231                 default:
232                     g_assert_not_reached(); /* unhandled size */
233                 }
234             ret = TRUE;
235         }
236         XFree(xdata);
237     }
238     return ret;
239 }
240
241 static gboolean get_all(Window win, Atom prop, Atom type, gint size,
242                         guchar **data, guint *num)
243 {
244     gboolean ret = FALSE;
245     gint res;
246     guchar *xdata = NULL;
247     Atom ret_type;
248     gint ret_size;
249     gulong ret_items, bytes_left;
250
251     res = XGetWindowProperty(ob_display, win, prop, 0l, G_MAXLONG,
252                              FALSE, type, &ret_type, &ret_size,
253                              &ret_items, &bytes_left, &xdata);
254     if (res == Success) {
255         if (ret_size == size && ret_items > 0) {
256             guint i;
257
258             *data = g_malloc(ret_items * (size / 8));
259             for (i = 0; i < ret_items; ++i)
260                 switch (size) {
261                 case 8:
262                     (*data)[i] = xdata[i];
263                     break;
264                 case 16:
265                     ((guint16*)*data)[i] = ((gushort*)xdata)[i];
266                     break;
267                 case 32:
268                     ((guint32*)*data)[i] = ((gulong*)xdata)[i];
269                     break;
270                 default:
271                     g_assert_not_reached(); /* unhandled size */
272                 }
273             *num = ret_items;
274             ret = TRUE;
275         }
276         XFree(xdata);
277     }
278     return ret;
279 }
280
281 static gboolean get_stringlist(Window win, Atom prop, gchar ***list, gint *nstr)
282 {
283     XTextProperty tprop;
284     gboolean ret = FALSE;
285
286     if (XGetTextProperty(ob_display, win, &tprop, prop) && tprop.nitems) {
287         if (XTextPropertyToStringList(&tprop, list, nstr))
288             ret = TRUE;
289         XFree(tprop.value);
290     }
291     return ret;
292 }
293
294 gboolean prop_get32(Window win, Atom prop, Atom type, guint32 *ret)
295 {
296     return get_prealloc(win, prop, type, 32, (guchar*)ret, 1);
297 }
298
299 gboolean prop_get_array32(Window win, Atom prop, Atom type, guint32 **ret,
300                           guint *nret)
301 {
302     return get_all(win, prop, type, 32, (guchar**)ret, nret);
303 }
304
305 gboolean prop_get_string_locale(Window win, Atom prop, gchar **ret)
306 {
307     gchar **list;
308     gint nstr;
309     gchar *s;
310
311     if (get_stringlist(win, prop, &list, &nstr) && nstr) {
312         s = g_locale_to_utf8(list[0], -1, NULL, NULL, NULL);
313         XFreeStringList(list);
314         if (s) {
315             *ret = s;
316             return TRUE;
317         }
318     }
319     return FALSE;
320 }
321
322 gboolean prop_get_strings_locale(Window win, Atom prop, gchar ***ret)
323 {
324     GSList *strs = NULL, *it;
325     gchar *raw, *p;
326     guint num, i, count = 0;
327
328     if (get_all(win, prop, prop_atoms.string, 8, (guchar**)&raw, &num)) {
329
330         p = raw;
331         while (p < raw + num) {
332             ++count;
333             strs = g_slist_append(strs, p);
334             p += strlen(p) + 1; /* next string */
335         }
336
337         *ret = g_new0(gchar*, count + 1);
338         (*ret)[count] = NULL; /* null terminated list */
339
340         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
341             (*ret)[i] = g_locale_to_utf8(it->data, -1, NULL, NULL, NULL);
342             /* make sure translation did not fail */
343             if (!(*ret)[i])
344                 (*ret)[i] = g_strdup("");
345         }
346         g_free(raw);
347         g_slist_free(strs);
348         return TRUE;
349     }
350     return FALSE;
351 }
352
353 gboolean prop_get_string_utf8(Window win, Atom prop, gchar **ret)
354 {
355     gchar *raw;
356     gchar *str;
357     guint num;
358      
359     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
360         str = g_strndup(raw, num); /* grab the first string from the list */
361         g_free(raw);
362         if (g_utf8_validate(str, -1, NULL)) {
363             *ret = str;
364             return TRUE;
365         }
366         g_free(str);
367     }
368     return FALSE;
369 }
370
371 gboolean prop_get_strings_utf8(Window win, Atom prop, gchar ***ret)
372 {
373     GSList *strs = NULL, *it;
374     gchar *raw, *p;
375     guint num, i, count = 0;
376
377     if (get_all(win, prop, prop_atoms.utf8, 8, (guchar**)&raw, &num)) {
378
379         p = raw;
380         while (p < raw + num) {
381             ++count;
382             strs = g_slist_append(strs, p);
383             p += strlen(p) + 1; /* next string */
384         }
385
386         *ret = g_new0(gchar*, count + 1);
387
388         for (i = 0, it = strs; it; ++i, it = g_slist_next(it)) {
389             if (g_utf8_validate(it->data, -1, NULL))
390                 (*ret)[i] = g_strdup(it->data);
391             else
392                 (*ret)[i] = g_strdup("");
393         }
394         g_free(raw);
395         g_slist_free(strs);
396         return TRUE;
397     }
398     return FALSE;
399 }
400
401 void prop_set32(Window win, Atom prop, Atom type, gulong val)
402 {
403     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
404                     (guchar*)&val, 1);
405 }
406
407 void prop_set_array32(Window win, Atom prop, Atom type, gulong *val,
408                       guint num)
409 {
410     XChangeProperty(ob_display, win, prop, type, 32, PropModeReplace,
411                     (guchar*)val, num);
412 }
413
414 void prop_set_string_utf8(Window win, Atom prop, const gchar *val)
415 {
416     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
417                     PropModeReplace, (const guchar*)val, strlen(val));
418 }
419
420 void prop_set_strings_utf8(Window win, Atom prop, gchar **strs)
421 {
422     GString *str;
423     gchar **s;
424
425     str = g_string_sized_new(0);
426     for (s = strs; *s; ++s) {
427         str = g_string_append(str, *s);
428         str = g_string_append_c(str, '\0');
429     }
430     XChangeProperty(ob_display, win, prop, prop_atoms.utf8, 8,
431                     PropModeReplace, (guchar*)str->str, str->len);
432     g_string_free(str, TRUE);
433 }
434
435 void prop_erase(Window win, Atom prop)
436 {
437     XDeleteProperty(ob_display, win, prop);
438 }
439
440 void prop_message(Window about, Atom messagetype, glong data0, glong data1,
441                   glong data2, glong data3, glong mask)
442 {
443     XEvent ce;
444     ce.xclient.type = ClientMessage;
445     ce.xclient.message_type = messagetype;
446     ce.xclient.display = ob_display;
447     ce.xclient.window = about;
448     ce.xclient.format = 32;
449     ce.xclient.data.l[0] = data0;
450     ce.xclient.data.l[1] = data1;
451     ce.xclient.data.l[2] = data2;
452     ce.xclient.data.l[3] = data3;
453     ce.xclient.data.l[4] = 0;
454     XSendEvent(ob_display, RootWindow(ob_display, ob_screen), FALSE,
455                mask, &ce);
456 }