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