Fix makefiles bug.
[wmaker-crm.git] / src / properties.c
blob7279de12d373676514339bca9c192bbd9600d38c
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "wconfig.h"
23 #include <X11/Xlib.h>
24 #include <X11/Xutil.h>
25 #include <X11/Xatom.h>
26 #include <string.h>
27 #include <stdlib.h>
29 #include "WindowMaker.h"
30 #include "window.h"
31 #include "GNUstep.h"
33 /* atoms */
34 extern Atom _XA_WM_STATE;
35 extern Atom _XA_WM_CLIENT_LEADER;
36 extern Atom _XA_WM_TAKE_FOCUS;
37 extern Atom _XA_WM_DELETE_WINDOW;
38 extern Atom _XA_WM_SAVE_YOURSELF;
39 extern Atom _XA_GNUSTEP_WM_ATTR;
40 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
41 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
42 extern Atom _XA_WINDOWMAKER_MENU;
43 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
44 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
45 extern Atom _XA_WINDOWMAKER_ICON_TILE;
47 int PropGetNormalHints(Window window, XSizeHints * size_hints, int *pre_iccm)
49 long supplied_hints;
51 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
52 return False;
54 if (supplied_hints == (USPosition | USSize | PPosition | PSize | PMinSize | PMaxSize
55 | PResizeInc | PAspect)) {
56 *pre_iccm = 1;
57 } else {
58 *pre_iccm = 0;
60 return True;
63 int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
65 XClassHint *class_hint;
67 class_hint = XAllocClassHint();
68 if (XGetClassHint(dpy, window, class_hint) == 0) {
69 *wm_class = strdup("default");
70 *wm_instance = strdup("default");
71 XFree(class_hint);
72 return False;
74 *wm_instance = class_hint->res_name;
75 *wm_class = class_hint->res_class;
77 XFree(class_hint);
78 return True;
81 void PropGetProtocols(Window window, WProtocols * prots)
83 Atom *protocols;
84 int count, i;
86 memset(prots, 0, sizeof(WProtocols));
87 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
88 return;
90 for (i = 0; i < count; i++) {
91 if (protocols[i] == _XA_WM_TAKE_FOCUS)
92 prots->TAKE_FOCUS = 1;
93 else if (protocols[i] == _XA_WM_DELETE_WINDOW)
94 prots->DELETE_WINDOW = 1;
95 else if (protocols[i] == _XA_WM_SAVE_YOURSELF)
96 prots->SAVE_YOURSELF = 1;
97 else if (protocols[i] == _XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
98 prots->MINIATURIZE_WINDOW = 1;
100 XFree(protocols);
103 unsigned char *PropGetCheckProperty(Window window, Atom hint, Atom type, int format, int count, int *retCount)
105 Atom type_ret;
106 int fmt_ret;
107 unsigned long nitems_ret;
108 unsigned long bytes_after_ret;
109 unsigned char *data;
110 int tmp;
112 if (count <= 0)
113 tmp = 0xffffff;
114 else
115 tmp = count;
117 if (XGetWindowProperty(dpy, window, hint, 0, tmp, False, type,
118 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
119 (unsigned char **)&data) != Success || !data)
120 return NULL;
122 if ((type != AnyPropertyType && type != type_ret)
123 || (count > 0 && nitems_ret != count)
124 || (format != 0 && format != fmt_ret)) {
125 XFree(data);
126 return NULL;
129 if (retCount)
130 *retCount = nitems_ret;
132 return data;
135 int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
137 unsigned long *data;
139 data = (unsigned long *)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
140 _XA_GNUSTEP_WM_ATTR, 32, 9, NULL);
142 if (!data)
143 return False;
145 *attr = malloc(sizeof(GNUstepWMAttributes));
146 if (!*attr) {
147 XFree(data);
148 return False;
150 (*attr)->flags = data[0];
151 (*attr)->window_style = data[1];
152 (*attr)->window_level = data[2];
153 (*attr)->reserved = data[3];
154 (*attr)->miniaturize_pixmap = data[4];
155 (*attr)->close_pixmap = data[5];
156 (*attr)->miniaturize_mask = data[6];
157 (*attr)->close_mask = data[7];
158 (*attr)->extra_flags = data[8];
160 XFree(data);
162 return True;
165 void PropSetWMakerProtocols(Window root)
167 Atom protocols[3];
168 int count = 0;
170 protocols[count++] = _XA_WINDOWMAKER_MENU;
171 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
172 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
174 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
175 32, PropModeReplace, (unsigned char *)protocols, count);
178 void PropSetIconTileHint(WScreen * scr, RImage * image)
180 static Atom imageAtom = 0;
181 unsigned char *tmp;
182 int x, y;
184 if (scr->info_window == None)
185 return;
187 if (!imageAtom) {
189 * WIDTH, HEIGHT (16 bits, MSB First)
190 * array of R,G,B,A bytes
192 imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
195 tmp = malloc(image->width * image->height * 4 + 4);
196 if (!tmp) {
197 wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
198 return;
201 tmp[0] = image->width >> 8;
202 tmp[1] = image->width & 0xff;
203 tmp[2] = image->height >> 8;
204 tmp[3] = image->height & 0xff;
206 if (image->format == RRGBAFormat) {
207 memcpy(&tmp[4], image->data, image->width * image->height * 4);
208 } else {
209 char *ptr = (char *)(tmp + 4);
210 char *src = (char *)image->data;
212 for (y = 0; y < image->height; y++) {
213 for (x = 0; x < image->width; x++) {
214 *ptr++ = *src++;
215 *ptr++ = *src++;
216 *ptr++ = *src++;
217 *ptr++ = 255;
222 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_ICON_TILE,
223 imageAtom, 8, PropModeReplace, tmp, image->width * image->height * 4 + 4);
224 wfree(tmp);
228 Window PropGetClientLeader(Window window)
230 Window *win;
231 Window leader;
233 win = (Window *) PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER, XA_WINDOW, 32, 1, NULL);
235 if (!win)
236 return None;
238 leader = (Window) * win;
239 XFree(win);
241 return leader;
244 void PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes * attr)
246 unsigned long data[9];
248 data[0] = attr->flags;
249 data[1] = attr->window_style;
250 data[2] = attr->window_level;
251 data[3] = 0; /* reserved */
252 /* The X protocol says XIDs are 32bit */
253 data[4] = attr->miniaturize_pixmap;
254 data[5] = attr->close_pixmap;
255 data[6] = attr->miniaturize_mask;
256 data[7] = attr->close_mask;
257 data[8] = attr->extra_flags;
258 XChangeProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, _XA_GNUSTEP_WM_ATTR,
259 32, PropModeReplace, (unsigned char *)data, 9);
262 int PropGetWindowState(Window window)
264 long *data;
265 long state;
267 data = (long *)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE, 32, 1, NULL);
269 if (!data)
270 return -1;
272 state = *data;
273 XFree(data);
275 return state;
278 void PropCleanUp(Window root)
280 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
281 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
282 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);