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