- Fixed the PPosition/PSize code (I hope) --Dan
[wmaker-crm.git] / src / properties.c
blob1f56f8af7061d39cb8c0f9685781c150b4fe6be2
1 /*
2 * Window Maker window manager
3 *
4 * Copyright (c) 1997, 1998 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.
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"
35 /* atoms */
36 extern Atom _XA_WM_STATE;
37 extern Atom _XA_WM_CHANGE_STATE;
38 extern Atom _XA_WM_PROTOCOLS;
39 extern Atom _XA_WM_CLIENT_LEADER;
40 extern Atom _XA_WM_TAKE_FOCUS;
41 extern Atom _XA_WM_DELETE_WINDOW;
42 extern Atom _XA_WM_SAVE_YOURSELF;
43 #ifdef XSMP_ENABLED
44 extern Atom _XA_WM_WINDOW_ROLE;
45 extern Atom _XA_SM_CLIENT_ID;
46 #endif
49 extern Atom _XA_GNUSTEP_WM_ATTR;
50 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
52 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
53 extern Atom _XA_WINDOWMAKER_MENU;
54 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
55 extern Atom _XA_WINDOWMAKER_NOTICEBOARD;
57 int
58 PropGetNormalHints(Window window, XSizeHints *size_hints, int *pre_iccm)
60 long supplied_hints;
62 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
63 return False;
65 if (supplied_hints==(USPosition|USSize|PPosition|PSize|PMinSize|PMaxSize
66 |PResizeInc|PAspect)) {
67 *pre_iccm = 1;
68 } else {
69 *pre_iccm = 0;
71 return True;
75 int
76 PropGetWMClass(Window window, char **wm_class, char **wm_instance)
78 XClassHint *class_hint;
80 class_hint = XAllocClassHint();
81 if (XGetClassHint(dpy,window,class_hint) == 0) {
82 *wm_class = NULL;
83 *wm_instance = NULL;
84 XFree(class_hint);
85 return False;
87 *wm_instance = class_hint->res_name;
88 *wm_class = class_hint->res_class;
90 XFree(class_hint);
91 return True;
95 void
96 PropGetProtocols(Window window, WProtocols *prots)
98 Atom *protocols;
99 int count, i;
101 memset(prots, 0, sizeof(WProtocols));
102 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
103 return;
105 for (i=0; i<count; i++) {
106 if (protocols[i]==_XA_WM_TAKE_FOCUS)
107 prots->TAKE_FOCUS=1;
108 else if (protocols[i]==_XA_WM_DELETE_WINDOW)
109 prots->DELETE_WINDOW=1;
110 else if (protocols[i]==_XA_WM_SAVE_YOURSELF)
111 prots->SAVE_YOURSELF=1;
112 else if (protocols[i]==_XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
113 prots->MINIATURIZE_WINDOW=1;
115 XFree(protocols);
119 unsigned char*
120 PropGetCheckProperty(Window window, Atom hint, Atom type, int format,
121 int count, int *retCount)
123 Atom type_ret;
124 int fmt_ret;
125 unsigned long nitems_ret;
126 unsigned long bytes_after_ret;
127 unsigned char *data;
128 int tmp;
130 if (count <= 0)
131 tmp = 0xffffff;
132 else
133 tmp = count;
135 if (XGetWindowProperty(dpy, window, hint, 0, tmp, False, type,
136 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
137 (unsigned char **)&data)!=Success || !data)
138 return NULL;
140 if ((type!=AnyPropertyType && type!=type_ret)
141 || (count > 0 && nitems_ret != count)
142 || (format != 0 && format != fmt_ret)) {
143 XFree(data);
144 return NULL;
147 if (retCount)
148 *retCount = nitems_ret;
150 return data;
155 PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes **attr)
157 unsigned long *data;
159 data = (unsigned long*)PropGetCheckProperty(window, _XA_GNUSTEP_WM_ATTR,
160 _XA_GNUSTEP_WM_ATTR, 32, 9,
161 NULL);
163 if (!data)
164 return False;
166 *attr = malloc(sizeof(GNUstepWMAttributes));
167 if (!*attr) {
168 XFree(data);
169 return False;
171 (*attr)->flags = data[0];
172 (*attr)->window_style = data[1];
173 (*attr)->window_level = data[2];
174 (*attr)->reserved = data[3];
175 (*attr)->miniaturize_pixmap = data[4];
176 (*attr)->close_pixmap = data[5];
177 (*attr)->miniaturize_mask = data[6];
178 (*attr)->close_mask = data[7];
179 (*attr)->extra_flags = data[8];
181 XFree(data);
183 return True;
188 void
189 PropSetWMakerProtocols(Window root)
191 Atom protocols[3];
192 int count=0;
194 protocols[count++] = _XA_WINDOWMAKER_MENU;
195 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
196 protocols[count++] = _XA_WINDOWMAKER_NOTICEBOARD;
198 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
199 32, PropModeReplace, (unsigned char *)protocols, count);
203 Window
204 PropGetClientLeader(Window window)
206 Window *win;
207 Window leader;
209 win = (Window*)PropGetCheckProperty(window, _XA_WM_CLIENT_LEADER,
210 XA_WINDOW, 32, 1, NULL);
212 if (!win)
213 return None;
215 leader = (Window)*win;
216 XFree(win);
218 return leader;
222 #ifdef XSMP_ENABLED
223 char*
224 PropGetClientID(Window window)
226 XTextProperty txprop;
228 txprop.value = NULL;
230 if (XGetTextProperty(dpy, window, &txprop, _XA_SM_CLIENT_ID)!=Success) {
231 return NULL;
234 if (txprop.encoding == XA_STRING && txprop.format == 8
235 && txprop.nitems > 0) {
237 return (char*)txprop.value;
238 } else {
240 if (txprop.value)
241 XFree(txprop.value);
243 return NULL;
248 char*
249 PropGetWindowRole(Window window)
251 XTextProperty txprop;
253 txprop.value = NULL;
255 if (XGetTextProperty(dpy, window, &txprop, _XA_WM_WINDOW_ROLE)!=Success) {
256 return NULL;
259 if (txprop.encoding == XA_STRING && txprop.format == 8
260 && txprop.nitems > 0) {
262 return (char*)txprop.value;
263 } else {
265 if (txprop.value)
266 XFree(txprop.value);
268 return NULL;
271 #endif /* XSMP_ENABLED */
274 void
275 PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes *attr)
277 unsigned long data[9];
279 data[0] = attr->flags;
280 data[1] = attr->window_style;
281 data[2] = attr->window_level;
282 data[3] = 0; /* reserved */
283 /* The X protocol says XIDs are 32bit */
284 data[4] = attr->miniaturize_pixmap;
285 data[5] = attr->close_pixmap;
286 data[6] = attr->miniaturize_mask;
287 data[7] = attr->close_mask;
288 data[8] = attr->extra_flags;
289 XChangeProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, _XA_GNUSTEP_WM_ATTR,
290 32, PropModeReplace, (unsigned char *)data, 9);
295 PropGetWindowState(Window window)
297 long *data;
298 long state;
300 data = (long*)PropGetCheckProperty(window, _XA_WM_STATE, _XA_WM_STATE,
301 32, 1, NULL);
303 if (!data)
304 return -1;
306 state = *data;
307 XFree(data);
309 return state;
313 void
314 PropCleanUp(Window root)
316 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
318 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_NOTICEBOARD);
320 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);
322 #ifdef KWM_HINTS
323 XDeleteProperty(dpy, root, XInternAtom(dpy, "KWM_RUNNING", False));
324 #endif