Prevent crash when switchpanel is not initialised.
[wmaker-crm.git] / src / properties.c
blob5bde91240bf5ffd70f6826cb9529acd85549182e
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"
32 #include "properties.h"
34 /* atoms */
35 extern Atom _XA_WM_STATE;
36 extern Atom _XA_WM_CLIENT_LEADER;
37 extern Atom _XA_WM_TAKE_FOCUS;
38 extern Atom _XA_WM_DELETE_WINDOW;
39 extern Atom _XA_WM_SAVE_YOURSELF;
40 extern Atom _XA_GNUSTEP_WM_ATTR;
41 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
42 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
43 extern Atom _XA_WINDOWMAKER_MENU;
44 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
45 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
46 extern Atom _XA_WINDOWMAKER_ICON_TILE;
48 int PropGetNormalHints(Window window, XSizeHints * size_hints, int *pre_iccm)
50 long supplied_hints;
52 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
53 return False;
55 if (supplied_hints == (USPosition | USSize | PPosition | PSize | PMinSize | PMaxSize
56 | PResizeInc | PAspect)) {
57 *pre_iccm = 1;
58 } else {
59 *pre_iccm = 0;
61 return True;
64 int PropGetWMClass(Window window, char **wm_class, char **wm_instance)
66 XClassHint *class_hint;
68 class_hint = XAllocClassHint();
69 if (XGetClassHint(dpy, window, class_hint) == 0) {
70 *wm_class = strdup("default");
71 *wm_instance = strdup("default");
72 XFree(class_hint);
73 return False;
75 *wm_instance = strdup(class_hint->res_name);
76 *wm_class = strdup(class_hint->res_class);
78 XFree(class_hint->res_name);
79 XFree(class_hint->res_class);
80 XFree(class_hint);
82 return True;
85 void PropGetProtocols(Window window, WProtocols * prots)
87 Atom *protocols;
88 int count, i;
90 memset(prots, 0, sizeof(WProtocols));
91 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
92 return;
94 for (i = 0; i < count; i++) {
95 if (protocols[i] == _XA_WM_TAKE_FOCUS)
96 prots->TAKE_FOCUS = 1;
97 else if (protocols[i] == _XA_WM_DELETE_WINDOW)
98 prots->DELETE_WINDOW = 1;
99 else if (protocols[i] == _XA_WM_SAVE_YOURSELF)
100 prots->SAVE_YOURSELF = 1;
101 else if (protocols[i] == _XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
102 prots->MINIATURIZE_WINDOW = 1;
104 XFree(protocols);
107 unsigned char *PropGetCheckProperty(Window window, Atom hint, Atom type, int format, int count, int *retCount)
109 Atom type_ret;
110 int fmt_ret;
111 unsigned long nitems_ret;
112 unsigned long bytes_after_ret;
113 unsigned char *data;
114 int tmp;
116 if (count <= 0)
117 tmp = 0xffffff;
118 else
119 tmp = count;
121 if (XGetWindowProperty(dpy, window, hint, 0, tmp, False, type,
122 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
123 (unsigned char **)&data) != Success || !data)
124 return NULL;
126 if ((type != AnyPropertyType && type != type_ret)
127 || (count > 0 && nitems_ret != count)
128 || (format != 0 && format != fmt_ret)) {
129 XFree(data);
130 return NULL;
133 if (retCount)
134 *retCount = nitems_ret;
136 return data;
139 int PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes ** attr)
141 unsigned long *data;
143 data = (unsigned long *)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
144 _XA_GNUSTEP_WM_ATTR, 32, 9, NULL);
146 if (!data)
147 return False;
149 *attr = malloc(sizeof(GNUstepWMAttributes));
150 if (!*attr) {
151 XFree(data);
152 return False;
154 (*attr)->flags = data[0];
155 (*attr)->window_style = data[1];
156 (*attr)->window_level = data[2];
157 (*attr)->reserved = data[3];
158 (*attr)->miniaturize_pixmap = data[4];
159 (*attr)->close_pixmap = data[5];
160 (*attr)->miniaturize_mask = data[6];
161 (*attr)->close_mask = data[7];
162 (*attr)->extra_flags = data[8];
164 XFree(data);
166 return True;
169 void PropSetWMakerProtocols(Window root)
171 Atom protocols[3];
172 int count = 0;
174 protocols[count++] = _XA_WINDOWMAKER_MENU;
175 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
176 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
178 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
179 32, PropModeReplace, (unsigned char *)protocols, count);
182 void PropSetIconTileHint(WScreen * scr, RImage * image)
184 static Atom imageAtom = 0;
185 unsigned char *tmp;
186 int x, y;
188 if (scr->info_window == None)
189 return;
191 if (!imageAtom) {
193 * WIDTH, HEIGHT (16 bits, MSB First)
194 * array of R,G,B,A bytes
196 imageAtom = XInternAtom(dpy, "_RGBA_IMAGE", False);
199 tmp = malloc(image->width * image->height * 4 + 4);
200 if (!tmp) {
201 wwarning("could not allocate memory to set _WINDOWMAKER_ICON_TILE hint");
202 return;
205 tmp[0] = image->width >> 8;
206 tmp[1] = image->width & 0xff;
207 tmp[2] = image->height >> 8;
208 tmp[3] = image->height & 0xff;
210 if (image->format == RRGBAFormat) {
211 memcpy(&tmp[4], image->data, image->width * image->height * 4);
212 } else {
213 char *ptr = (char *)(tmp + 4);
214 char *src = (char *)image->data;
216 for (y = 0; y < image->height; y++) {
217 for (x = 0; x < image->width; x++) {
218 *ptr++ = *src++;
219 *ptr++ = *src++;
220 *ptr++ = *src++;
221 *ptr++ = 255;
226 XChangeProperty(dpy, scr->info_window, _XA_WINDOWMAKER_ICON_TILE,
227 imageAtom, 8, PropModeReplace, tmp, image->width * image->height * 4 + 4);
228 wfree(tmp);
232 Window PropGetClientLeader(Window window)
234 Window *win;
235 Window leader;
237 win = (Window *) PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER, XA_WINDOW, 32, 1, NULL);
239 if (!win)
240 return None;
242 leader = (Window) * win;
243 XFree(win);
245 return leader;
248 int PropGetWindowState(Window window)
250 long *data;
251 long state;
253 data = (long *)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE, 32, 1, NULL);
255 if (!data)
256 return -1;
258 state = *data;
259 XFree(data);
261 return state;
264 void PropCleanUp(Window root)
266 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
267 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
268 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);