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