Change to the linux kernel coding style
[wmaker-crm.git] / src / properties.c
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 * USA.
20 */
21
22 #include "wconfig.h"
23
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "GNUstep.h"
33
34 /* atoms */
35 extern Atom _XA_WM_STATE;
36 extern Atom _XA_WM_CHANGE_STATE;
37 extern Atom _XA_WM_PROTOCOLS;
38 extern Atom _XA_WM_CLIENT_LEADER;
39 extern Atom _XA_WM_TAKE_FOCUS;
40 extern Atom _XA_WM_DELETE_WINDOW;
41 extern Atom _XA_WM_SAVE_YOURSELF;
42 #ifdef XSMP_ENABLED
43 extern Atom _XA_WM_WINDOW_ROLE;
44 extern Atom _XA_SM_CLIENT_ID;
45 #endif
46
47 extern Atom _XA_GNUSTEP_WM_ATTR;
48 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
49
50 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
51 extern Atom _XA_WINDOWMAKER_MENU;
52 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
53 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
54 extern Atom _XA_WINDOWMAKER_ICON_TILE;
55 extern Atom _XA_WINDOWMAKER_ICON_SIZE;
56
57 int PropGetNormalHints(Window window, XSizeHints * size_hints, int *pre_iccm)
58 {
59 long supplied_hints;
60
61 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
62 return False;
63 }
64 if (supplied_hints == (USPosition | USSize | PPosition | PSize | PMinSize | PMaxSize
65 | PResizeInc | PAspect)) {
66 *pre_iccm = 1;
67 } else {
68 *pre_iccm = 0;
69 }
70 return True;
71 }
72
73 int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
74 {
75 XClassHint *class_hint;
76
77 class_hint = XAllocClassHint();
78 if (XGetClassHint(dpy, window, class_hint) == 0) {
79 *wm_class = NULL;
80 *wm_instance = NULL;
81 XFree(class_hint);
82 return False;
83 }
84 *wm_instance = class_hint->res_name;
85 *wm_class = class_hint->res_class;
86
87 XFree(class_hint);
88 return True;
89 }
90
91 void PropGetProtocols(Window window, WProtocols * prots)
92 {
93 Atom *protocols;
94 int count, i;
95
96 memset(prots, 0, sizeof(WProtocols));
97 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
98 return;
99 }
100 for (i = 0; i < count; i++) {
101 if (protocols[i] == _XA_WM_TAKE_FOCUS)
102 prots->TAKE_FOCUS = 1;
103 else if (protocols[i] == _XA_WM_DELETE_WINDOW)
104 prots->DELETE_WINDOW = 1;
105 else if (protocols[i] == _XA_WM_SAVE_YOURSELF)
106 prots->SAVE_YOURSELF = 1;
107 else if (protocols[i] == _XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
108 prots->MINIATURIZE_WINDOW = 1;
109 }
110 XFree(protocols);
111 }
112
113 unsigned char *PropGetCheckProperty(Window window, Atom hint, Atom type, int format, int count, int *retCount)
114 {
115 Atom type_ret;
116 int fmt_ret;
117 unsigned long nitems_ret;
118 unsigned long bytes_after_ret;
119 unsigned char *data;
120 int tmp;
121
122 if (count <= 0)
123 tmp = 0xffffff;
124 else
125 tmp = count;
126
127 if (XGetWindowProperty(dpy, window, hint, 0, tmp, False, type,
128 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
129 (unsigned char **)&data) != Success || !data)
130 return NULL;
131
132 if ((type != AnyPropertyType && type != type_ret)
133 || (count > 0 && nitems_ret != count)
134 || (format != 0 && format != fmt_ret)) {
135 XFree(data);
136 return NULL;
137 }
138
139 if (retCount)
140 *retCount = nitems_ret;
141
142 return data;
143 }
144
145 int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
146 {
147 unsigned long *data;
148
149 data = (unsigned long *)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
150 _XA_GNUSTEP_WM_ATTR, 32, 9, NULL);
151
152 if (!data)
153 return False;
154
155 *attr = malloc(sizeof(GNUstepWMAttributes));
156 if (!*attr) {
157 XFree(data);
158 return False;
159 }
160 (*attr)->flags = data[0];
161 (*attr)->window_style = data[1];
162 (*attr)->window_level = data[2];
163 (*attr)->reserved = data[3];
164 (*attr)->miniaturize_pixmap = data[4];
165 (*attr)->close_pixmap = data[5];
166 (*attr)->miniaturize_mask = data[6];
167 (*attr)->close_mask = data[7];
168 (*attr)->extra_flags = data[8];
169
170 XFree(data);
171
172 return True;
173 }
174
175 void PropSetWMakerProtocols(Window root)
176 {
177 Atom protocols[3];
178 int count = 0;
179
180 protocols[count++] = _XA_WINDOWMAKER_MENU;
181 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
182 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
183
184 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
185 32, PropModeReplace, (unsigned char *)protocols, count);
186 }
187
188 void PropSetIconTileHint(WScreen * scr, RImage * image)
189 {
190 static Atom imageAtom = 0;
191 unsigned char *tmp;
192 int x, y;
193
194 if (scr->info_window == None)
195 return;
196
197 if (!imageAtom) {
198 /*
199 * WIDTH, HEIGHT (16 bits, MSB First)
200 * array of R,G,B,A bytes
201 */
202 imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
203 }
204
205 tmp = malloc(image->width * image->height * 4 + 4);
206 if (!tmp) {
207 wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
208 return;
209 }
210
211 tmp[0] = image->width >> 8;
212 tmp[1] = image->width & 0xff;
213 tmp[2] = image->height >> 8;
214 tmp[3] = image->height & 0xff;
215
216 if (image->format == RRGBAFormat) {
217 memcpy(&tmp[4], image->data, image->width * image->height * 4);
218 } else {
219 char *ptr = (char *)(tmp + 4);
220 char *src = (char *)image->data;
221
222 for (y = 0; y < image->height; y++) {
223 for (x = 0; x < image->width; x++) {
224 *ptr++ = *src++;
225 *ptr++ = *src++;
226 *ptr++ = *src++;
227 *ptr++ = 255;
228 }
229 }
230 }
231
232 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_ICON_TILE,
233 imageAtom, 8, PropModeReplace, tmp, image->width * image->height * 4 + 4);
234 wfree(tmp);
235
236 }
237
238 Window PropGetClientLeader(Window window)
239 {
240 Window *win;
241 Window leader;
242
243 win = (Window *) PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER, XA_WINDOW, 32, 1, NULL);
244
245 if (!win)
246 return None;
247
248 leader = (Window) * win;
249 XFree(win);
250
251 return leader;
252 }
253
254 #ifdef XSMP_ENABLED
255 char *PropGetClientID(Window window)
256 {
257 XTextProperty txprop;
258
259 txprop.value = NULL;
260
261 if (XGetTextProperty(dpy, window, &txprop, _XA_SM_CLIENT_ID) != Success) {
262 return NULL;
263 }
264
265 if (txprop.encoding == XA_STRING && txprop.format == 8 && txprop.nitems > 0) {
266
267 return (char *)txprop.value;
268 } else {
269
270 if (txprop.value)
271 XFree(txprop.value);
272
273 return NULL;
274 }
275 }
276
277 char *PropGetWindowRole(Window window)
278 {
279 XTextProperty txprop;
280
281 txprop.value = NULL;
282
283 if (XGetTextProperty(dpy, window, &txprop, _XA_WM_WINDOW_ROLE) != Success) {
284 return NULL;
285 }
286
287 if (txprop.encoding == XA_STRING && txprop.format == 8 && txprop.nitems > 0) {
288
289 return (char *)txprop.value;
290 } else {
291
292 if (txprop.value)
293 XFree(txprop.value);
294
295 return NULL;
296 }
297 }
298 #endif /* XSMP_ENABLED */
299
300 void PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes * attr)
301 {
302 unsigned long data[9];
303
304 data[0] = attr->flags;
305 data[1] = attr->window_style;
306 data[2] = attr->window_level;
307 data[3] = 0; /* reserved */
308 /* The X protocol says XIDs are 32bit */
309 data[4] = attr->miniaturize_pixmap;
310 data[5] = attr->close_pixmap;
311 data[6] = attr->miniaturize_mask;
312 data[7] = attr->close_mask;
313 data[8] = attr->extra_flags;
314 XChangeProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, _XA_GNUSTEP_WM_ATTR,
315 32, PropModeReplace, (unsigned char *)data, 9);
316 }
317
318 int PropGetWindowState(Window window)
319 {
320 long *data;
321 long state;
322
323 data = (long *)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE, 32, 1, NULL);
324
325 if (!data)
326 return -1;
327
328 state = *data;
329 XFree(data);
330
331 return state;
332 }
333
334 void PropCleanUp(Window root)
335 {
336 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
337
338 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
339
340 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);
341 }