Remove #ifdef SYS_SIGLIST_DECLARED constructs
[wmaker-crm.git] / src / properties.c
blob76f29ed50b8ab51b7db9d9ae3b1a54169501f35f
1 /*
2 * Window Maker window manager
4 * Copyright (c) 1997-2003 Alfredo K. Kojima
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.
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.
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.
22 #include "wconfig.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #include <X11/Xatom.h>
27 #include <string.h>
28 #include <stdlib.h>
30 #include "WindowMaker.h"
31 #include "window.h"
32 #include "GNUstep.h"
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
47 extern Atom _XA_GNUSTEP_WM_ATTR;
48 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
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;
57 int PropGetNormalHints(Window window, XSizeHints * size_hints, int *pre_iccm)
59 long supplied_hints;
61 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
62 return False;
64 if (supplied_hints == (USPosition | USSize | PPosition | PSize | PMinSize | PMaxSize
65 | PResizeInc | PAspect)) {
66 *pre_iccm = 1;
67 } else {
68 *pre_iccm = 0;
70 return True;
73 int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
75 XClassHint *class_hint;
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;
84 *wm_instance = class_hint->res_name;
85 *wm_class = class_hint->res_class;
87 XFree(class_hint);
88 return True;
91 void PropGetProtocols(Window window, WProtocols * prots)
93 Atom *protocols;
94 int count, i;
96 memset(prots, 0, sizeof(WProtocols));
97 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
98 return;
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;
110 XFree(protocols);
113 unsigned char *PropGetCheckProperty(Window window, Atom hint, Atom type, int format, int count, int *retCount)
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;
122 if (count <= 0)
123 tmp = 0xffffff;
124 else
125 tmp = count;
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;
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;
139 if (retCount)
140 *retCount = nitems_ret;
142 return data;
145 int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
147 unsigned long *data;
149 data = (unsigned long *)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
150 _XA_GNUSTEP_WM_ATTR, 32, 9, NULL);
152 if (!data)
153 return False;
155 *attr = malloc(sizeof(GNUstepWMAttributes));
156 if (!*attr) {
157 XFree(data);
158 return False;
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];
170 XFree(data);
172 return True;
175 void PropSetWMakerProtocols(Window root)
177 Atom protocols[3];
178 int count = 0;
180 protocols[count++] = _XA_WINDOWMAKER_MENU;
181 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
182 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
184 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
185 32, PropModeReplace, (unsigned char *)protocols, count);
188 void PropSetIconTileHint(WScreen * scr, RImage * image)
190 static Atom imageAtom = 0;
191 unsigned char *tmp;
192 int x, y;
194 if (scr->info_window == None)
195 return;
197 if (!imageAtom) {
199 * WIDTH, HEIGHT (16 bits, MSB First)
200 * array of R,G,B,A bytes
202 imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
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;
211 tmp[0] = image->width >> 8;
212 tmp[1] = image->width & 0xff;
213 tmp[2] = image->height >> 8;
214 tmp[3] = image->height & 0xff;
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;
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;
232 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_ICON_TILE,
233 imageAtom, 8, PropModeReplace, tmp, image->width * image->height * 4 + 4);
234 wfree(tmp);
238 Window PropGetClientLeader(Window window)
240 Window *win;
241 Window leader;
243 win = (Window *) PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER, XA_WINDOW, 32, 1, NULL);
245 if (!win)
246 return None;
248 leader = (Window) * win;
249 XFree(win);
251 return leader;
254 #ifdef XSMP_ENABLED
255 char *PropGetClientID(Window window)
257 XTextProperty txprop;
259 txprop.value = NULL;
261 if (XGetTextProperty(dpy, window, &txprop, _XA_SM_CLIENT_ID) != Success) {
262 return NULL;
265 if (txprop.encoding == XA_STRING && txprop.format == 8 && txprop.nitems > 0) {
267 return (char *)txprop.value;
268 } else {
270 if (txprop.value)
271 XFree(txprop.value);
273 return NULL;
277 char *PropGetWindowRole(Window window)
279 XTextProperty txprop;
281 txprop.value = NULL;
283 if (XGetTextProperty(dpy, window, &txprop, _XA_WM_WINDOW_ROLE) != Success) {
284 return NULL;
287 if (txprop.encoding == XA_STRING && txprop.format == 8 && txprop.nitems > 0) {
289 return (char *)txprop.value;
290 } else {
292 if (txprop.value)
293 XFree(txprop.value);
295 return NULL;
298 #endif /* XSMP_ENABLED */
300 void PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes * attr)
302 unsigned long data[9];
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);
318 int PropGetWindowState(Window window)
320 long *data;
321 long state;
323 data = (long *)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE, 32, 1, NULL);
325 if (!data)
326 return -1;
328 state = *data;
329 XFree(data);
331 return state;
334 void PropCleanUp(Window root)
336 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
338 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
340 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);