add most of the root window props get functions
[mikachu/openbox.git] / cwmcc / client_props.c
1 #include "cwmcc_internal.h"
2 #include "atom.h"
3 #include "prop.h"
4 #include "client_props.h"
5
6 #include <X11/Xutil.h>
7 #ifdef HAVE_STRING_H
8 #  include <string.h>
9 #endif
10
11 void cwmcc_client_get_protocols(Window win, Atom **protocols)
12 {
13     gulong num;
14
15     if (!prop_get_array32(win, CWMCC_ATOM(client, wm_protocols),
16                           CWMCC_ATOM(type, atom), protocols, &num)) {
17         g_warning("Failed to read WM_PROTOCOLS from 0x%lx", win);
18         *protocols = NULL;
19     }
20 }
21
22 void cwmcc_client_set_protocols(Window win, Atom *protocols)
23 {
24     gulong n;
25     Atom *a;
26
27     for (a = protocols, n = 0; *a; ++a, ++n);
28     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, wm_state),
29                     CWMCC_ATOM(type, atom), 32, PropModeReplace,
30                     (guchar*)protocols, n);
31 }
32
33 void cwmcc_client_get_wm_state(Window win, gulong *state)
34 {
35     if (!prop_get32(win, CWMCC_ATOM(client, wm_state),
36                     CWMCC_ATOM(client, wm_state), state)) {
37         g_warning("Failed to read WM_STATE from 0x%lx", win);
38         *state = NormalState;
39     }
40 }
41
42 void cwmcc_client_set_wm_state(Window win, gulong state)
43 {
44     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, wm_state),
45                     CWMCC_ATOM(client, wm_state), 32, PropModeReplace,
46                     (guchar*)&state, 1);
47 }
48
49 void cwmcc_client_get_name(Window win, char **name)
50 {
51     if (!prop_get_string_utf8(win, CWMCC_ATOM(client, net_wm_name), name))
52         if (!prop_get_string_locale(win, CWMCC_ATOM(client, wm_name), name)) {
53             g_warning("Failed to read a name from 0x%lx", win);
54             *name = g_strdup("Unnamed Window");
55         }
56 }
57
58 void cwmcc_client_set_name(Window win, char *name)
59 {
60     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, net_wm_name),
61                     CWMCC_ATOM(type, utf8), 32, PropModeReplace,
62                     (guchar*)name, strlen(name));
63 }
64
65 void cwmcc_client_get_icon_name(Window win, char **name)
66 {
67     if (!prop_get_string_utf8(win, CWMCC_ATOM(client, net_wm_icon_name), name))
68         if (!prop_get_string_locale(win,
69                                     CWMCC_ATOM(client, wm_icon_name), name)) {
70             g_warning("Failed to read an icon name from 0x%lx", win);
71             *name = g_strdup("Unnamed Window");
72         }
73 }
74
75 void cwmcc_client_set_icon_name(Window win, char *name)
76 {
77     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, net_wm_icon_name),
78                     CWMCC_ATOM(type, utf8), 32, PropModeReplace,
79                     (guchar*)name, strlen(name));
80 }
81
82 void cwmcc_client_get_class(Window win, char **class, char **name)
83 {
84     char **s;
85
86     if (!prop_get_strings_locale(win, CWMCC_ATOM(client, wm_class), &s)) {
87         g_warning("Failed to read WM_CLASS from 0x%lx", win);
88         *class = g_strdup("");
89         *name = g_strdup("");
90     } else {
91         if (!s[0]) {
92             g_warning("Failed to read class element of WM_CLASS from 0x%lx",
93                       win);
94             *class = g_strdup("");
95         } else
96             *class = g_strdup(s[0]);
97         if (!s[0] || !s[1]) {
98             g_warning("Failed to read name element of WM_CLASS from 0x%lx",
99                       win);
100             *name = g_strdup("");
101         } else
102             *name = g_strdup(s[1]);
103     }
104     g_strfreev(s);
105 }
106
107 void cwmcc_client_get_role(Window win, char **role)
108 {
109     if (!prop_get_string_locale(win,
110                                 CWMCC_ATOM(client, wm_window_role), role)) {
111         g_warning("Failed to read WM_WINDOW_ROLE from 0x%lx", win);
112         *role = g_strdup("");
113     }
114 }
115
116 void cwmcc_client_get_mwmhints(Window win, struct Cwmcc_MwmHints *hints)
117 {
118     gulong *l = NULL, num;
119
120     if (!prop_get_array32(win, CWMCC_ATOM(client, motif_wm_hints),
121                           CWMCC_ATOM(client, motif_wm_hints), &l, &num)) {
122         g_warning("Failed to read Motif WM Hints from 0x%lx", win);
123         hints->flags = 0;
124     } else if (num < 3) {
125         g_warning("Read incomplete Motif WM Hints from 0x%lx", win);
126         hints->flags = 0;
127     } else {
128         hints->flags = l[0];
129         hints->functions = l[1];
130         hints->decorations = l[2];
131     }
132     g_free(l);
133 }
134
135 void cwmcc_client_get_desktop(Window win, gulong *desk)
136 {
137     if (!prop_get32(win, CWMCC_ATOM(client, net_wm_desktop),
138                     CWMCC_ATOM(type, cardinal), desk)) {
139         g_warning("Failed to read NET_WM_DESKTOP from 0x%lx", win);
140         *desk = 0;
141     }
142 }
143
144 void cwmcc_client_set_desktop(Window win, gulong desk)
145 {
146     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, net_wm_desktop),
147                     CWMCC_ATOM(type, cardinal), 32, PropModeReplace,
148                     (guchar*)&desk, 1);
149 }
150
151 void cwmcc_client_get_type(Window win, gulong **types)
152 {
153     gulong num;
154
155     if (!prop_get_array32(win, CWMCC_ATOM(client, net_wm_window_type),
156                     CWMCC_ATOM(type, atom), types, &num)) {
157         g_warning("Failed to read NET_WM_WINDOW_TYPE from 0x%lx", win);
158         *types = g_new(Atom, 2);
159         (*types)[0] = CWMCC_ATOM(data, net_wm_window_type_normal);
160         (*types)[1] = 0;
161     }
162 }
163
164 void cwmcc_client_set_type(Window win, gulong *types)
165 {
166     gulong n;
167     gulong *t;
168
169     for (t = types, n = 0; *t; ++t, ++n);
170     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, wm_state),
171                     CWMCC_ATOM(type, atom), 32, PropModeReplace,
172                     (guchar*)types, n);
173 }
174
175 void cwmcc_client_get_state(Window win, gulong **states)
176 {
177     gulong num;
178
179     if (!prop_get_array32(win, CWMCC_ATOM(client, net_wm_state),
180                     CWMCC_ATOM(type, atom), states, &num)) {
181         g_warning("Failed to read NET_WM_STATE from 0x%lx", win);
182         *states = g_new(Atom, 1);
183         (*states)[0] = 0;
184     }
185 }
186
187 void cwmcc_client_set_state(Window win, gulong *states)
188 {
189     gulong n;
190     gulong *s;
191
192     for (s = states, n = 0; *s; ++s, ++n);
193     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, wm_state),
194                     CWMCC_ATOM(type, atom), 32, PropModeReplace,
195                     (guchar*)states, n);
196 }
197
198 void cwmcc_client_get_strut(Window win, int *l, int *t, int *r, int *b)
199 {
200     gulong *data = NULL, num;
201
202     if (!prop_get_array32(win, CWMCC_ATOM(client, net_wm_strut),
203                     CWMCC_ATOM(type, cardinal), &data, &num)) {
204         g_warning("Failed to read NET_WM_STRUT from 0x%lx", win);
205         *l = *t = *r = *b = 0;
206     } else if (num != 4) {
207         g_warning("Read invalid NET_WM_STRUT from 0x%lx", win);
208         *l = *t = *r = *b = 0;
209     } else {
210         *l = data[0];
211         *r = data[1];
212         *t = data[2];
213         *b = data[3];
214     }
215     g_free(data);
216 }
217
218 static void convert_pixmap_to_icon(Pixmap pix, Pixmap mask,
219                                    struct Cwmcc_Icon *icon)
220 {
221 /*
222     guint pw, ph, mw, mh, depth;
223     Window wjunk;
224     int ijunk;
225     guint uijunk;
226     guint x, y;
227
228     if (!XGetGeometry(cwmcc_display, pix, &wjunk, &ijunk, &ijunk, &pw, &ph,
229                       &uijunk, &depth)) {
230         g_message("Unable to read pixmap icon's geometry");
231         icon->width = icon->height = 0;
232         icon->data = NULL;
233         return;
234     }
235     if (!XGetGeometry(cwmcc_display, mask, &wjunk, &ijunk, &ijunk, &mw, &mh,
236                       &uijunk, &ujunk)) {
237         g_message("Unable to read pixmap icon's mask's geometry");
238         icon->width = icon->height = 0;
239         icon->data = NULL;
240         return;
241     }
242     if (pw != mw || ph !_ mh) {
243         g_warning("Pixmap icon's mask does not match icon's dimensions");
244         icon->width = icon->height = 0;
245         icon->data = NULL;
246         return;
247     }
248
249     for (y = 0; y < ph; ++y)
250         for (x = 0; x < pw; ++x) {
251         }
252 */
253     icon->width = icon->height = 0;
254     icon->data = NULL;
255 }
256
257 void cwmcc_client_get_icon(Window win, struct Cwmcc_Icon **icons)
258 {
259     gulong *data = NULL, num;
260     gulong w, h, i;
261     int j;
262     int nicons;
263
264     if (!prop_get_array32(win, CWMCC_ATOM(client, net_wm_icon),
265                           CWMCC_ATOM(type, cardinal), &data, &num)) {
266         g_warning("Failed to read NET_WM_ICON from 0x%lx", win);
267         *icons = NULL;
268         nicons = 0;
269     } else { 
270         /* figure out how many valid icons are in here */
271         i = 0;
272         nicons = 0;
273         while (num - i > 2) {
274             w = data[i++];
275             h = data[i++];
276             i += w * h;
277             if (i > num) break;
278             ++nicons;
279         }
280
281         *icons = g_new(struct Cwmcc_Icon, nicons + 1);
282         (*icons)[nicons].data = NULL;
283     
284         /* store the icons */
285         i = 0;
286         for (j = 0; j < nicons; ++j) {
287             w = (*icons)[j].width = data[i++];
288             h = (*icons)[j].height = data[i++];
289             (*icons)[j].data =
290                 g_memdup(&data[i], w * h * sizeof(gulong));
291             i += w * h;
292             g_assert(i <= num);
293         }
294     }
295     g_free(data);
296
297     data = NULL;
298     if (!prop_get_array32(win, CWMCC_ATOM(client, kwm_win_icon),
299                           CWMCC_ATOM(client, kwm_win_icon), &data, &num)) {
300         g_warning("Failed to read KWM_WIN_ICON from 0x%lx", win);
301     } else if (num != 2) {
302         g_warning("Read invalid KWM_WIN_ICON from 0x%lx", win);
303     } else {
304         Pixmap p, m;
305         struct Cwmcc_Icon icon;
306
307         p = data[0];
308         m = data[1];
309
310         convert_pixmap_to_icon(p, m, &icon);
311
312         if (icon.data) {
313             *icons = g_renew(struct Cwmcc_Icon, *icons, nicons + 2);
314             (*icons[nicons + 1]).data = NULL;
315             g_memmove(&(*icons)[nicons], &icon, sizeof(struct Cwmcc_Icon));
316         }
317     }
318     g_free(data);
319
320 }
321
322 void cwmcc_client_get_premax(Window win, int *x, int *y, int *w, int *h)
323 {
324     gulong *l = NULL, num;
325
326     if (!prop_get_array32(win, CWMCC_ATOM(client, openbox_premax),
327                     CWMCC_ATOM(type, cardinal), &l, &num)) {
328         g_warning("Failed to read OPENBOX_PREMAX from 0x%lx", win);
329         *x = *y = *w = *h = 0;
330     } else if (num != 4) {
331         g_warning("Read invalid OPENBOX_PREMAX from 0x%lx", win);
332         *x = *y = *w = *h = 0;
333     } else {
334         *x = l[0];
335         *y = l[1];
336         *w = l[2];
337         *h = l[3];
338     }
339     g_free(l);
340 }
341
342 void cwmcc_client_set_premax(Window win, int x, int y, int w, int h)
343 {
344     gulong l[4];
345
346     l[0] = x;
347     l[1] = y;
348     l[2] = w;
349     l[3] = h;
350     XChangeProperty(cwmcc_display, win, CWMCC_ATOM(client, openbox_premax),
351                     CWMCC_ATOM(type, cardinal), 32, PropModeReplace,
352                     (guchar*)l, 4);
353 }