This update includes the 0.20.3pre3 code
[wmaker-crm.git] / src / properties.c
blob6b7b3d0104f1fb240dc1801c4048326e203a4500
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"
33 #ifdef MWM_HINTS
34 # include "motif.h"
35 #endif
38 /* atoms */
39 extern Atom _XA_WM_STATE;
40 extern Atom _XA_WM_CHANGE_STATE;
41 extern Atom _XA_WM_PROTOCOLS;
42 extern Atom _XA_WM_CLIENT_LEADER;
43 extern Atom _XA_WM_TAKE_FOCUS;
44 extern Atom _XA_WM_DELETE_WINDOW;
45 extern Atom _XA_WM_SAVE_YOURSELF;
46 #ifdef R6SM
47 extern Atom _XA_WM_WINDOW_ROLE;
48 extern Atom _XA_SM_CLIENT_ID;
49 #endif
52 extern Atom _XA_GNUSTEP_WM_ATTR;
53 extern Atom _XA_GNUSTEP_WM_MINIATURIZE_WINDOW;
55 extern Atom _XA_WINDOWMAKER_WM_FUNCTION;
56 extern Atom _XA_WINDOWMAKER_MENU;
57 extern Atom _XA_WINDOWMAKER_WM_PROTOCOLS;
60 int
61 PropGetNormalHints(Window window, XSizeHints *size_hints, int *pre_iccm)
63 long supplied_hints;
65 if (!XGetWMNormalHints(dpy, window, size_hints, &supplied_hints)) {
66 return False;
68 if (supplied_hints==(USPosition|USSize|PPosition|PSize|PMinSize|PMaxSize
69 |PResizeInc|PAspect)) {
70 *pre_iccm = 1;
71 } else {
72 *pre_iccm = 0;
74 return True;
78 int
79 PropGetWMClass(Window window, char **wm_class, char **wm_instance)
81 XClassHint *class_hint;
83 class_hint = XAllocClassHint();
84 if (XGetClassHint(dpy,window,class_hint) == 0) {
85 *wm_class = NULL;
86 *wm_instance = NULL;
87 XFree(class_hint);
88 return False;
90 *wm_instance = class_hint->res_name;
91 *wm_class = class_hint->res_class;
93 XFree(class_hint);
94 return True;
97 void
98 PropGetProtocols(Window window, WProtocols *prots)
100 Atom *protocols;
101 int count, i;
103 memset(prots, 0, sizeof(WProtocols));
104 if (!XGetWMProtocols(dpy, window, &protocols, &count)) {
105 return;
107 for (i=0; i<count; i++) {
108 if (protocols[i]==_XA_WM_TAKE_FOCUS)
109 prots->TAKE_FOCUS=1;
110 else if (protocols[i]==_XA_WM_DELETE_WINDOW)
111 prots->DELETE_WINDOW=1;
112 else if (protocols[i]==_XA_WM_SAVE_YOURSELF)
113 prots->SAVE_YOURSELF=1;
114 else if (protocols[i]==_XA_GNUSTEP_WM_MINIATURIZE_WINDOW)
115 prots->MINIATURIZE_WINDOW=1;
117 XFree(protocols);
122 PropGetGNUstepWMAttr(Window window, GNUstepWMAttributes **attr)
124 Atom type_ret;
125 int fmt_ret;
126 unsigned long nitems_ret;
127 unsigned long bytes_after_ret;
128 unsigned long *data;
130 if (XGetWindowProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, 0, 9,
131 False, _XA_GNUSTEP_WM_ATTR,
132 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
133 (unsigned char **)&data)!=Success || !data)
134 return False;
135 if (type_ret!=_XA_GNUSTEP_WM_ATTR || !data || fmt_ret!=32)
136 return False;
138 *attr = malloc(sizeof(GNUstepWMAttributes));
139 if (!*attr) {
140 XFree(data);
141 return False;
143 (*attr)->flags = data[0];
144 (*attr)->window_style = data[1];
145 (*attr)->window_level = data[2];
146 (*attr)->reserved = data[3];
147 (*attr)->miniaturize_pixmap = data[4];
148 (*attr)->close_pixmap = data[5];
149 (*attr)->miniaturize_mask = data[6];
150 (*attr)->close_mask = data[7];
151 (*attr)->extra_flags = data[8];
153 XFree(data);
155 return True;
161 #ifdef MWM_HINTS
163 PropGetMotifWMHints(Window window, MWMHints **mwmhints)
165 Atom type_ret;
166 int fmt_ret;
167 unsigned long nitems_ret;
168 unsigned long bytes_after_ret;
169 unsigned long *data;
171 if (XGetWindowProperty(dpy, window, _XA_MOTIF_WM_HINTS, 0,
172 PROP_MWM_HINTS_ELEMENTS,
173 False, _XA_MOTIF_WM_HINTS,
174 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
175 (unsigned char **)&data)!=Success)
176 return 0;
178 if (type_ret!=_XA_MOTIF_WM_HINTS || fmt_ret!=32
179 || nitems_ret!=PROP_MWM_HINTS_ELEMENTS || !data)
180 return 0;
182 *mwmhints = malloc(sizeof(MWMHints));
183 if (!*mwmhints) {
184 XFree(data);
185 return 0;
188 (*mwmhints)->flags = data[0];
189 (*mwmhints)->functions = data[1];
190 (*mwmhints)->decorations = data[2];
191 (*mwmhints)->inputMode = data[3];
193 XFree(data);
195 if (type_ret==_XA_MOTIF_WM_HINTS)
196 return 1;
197 else
198 return 0;
200 #endif /* MWM_HINTS */
203 void
204 PropSetWMakerProtocols(Window root)
206 Atom protocols[2];
207 int count=0;
209 protocols[count++] = _XA_WINDOWMAKER_MENU;
210 protocols[count++] = _XA_WINDOWMAKER_WM_FUNCTION;
212 XChangeProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS, XA_ATOM,
213 32, PropModeReplace, (unsigned char *)protocols, count);
217 Window
218 PropGetClientLeader(Window window)
220 Atom type_ret;
221 int fmt_ret;
222 unsigned long nitems_ret;
223 unsigned long bytes_after_ret;
224 Window *win;
225 Window leader;
227 if (XGetWindowProperty(dpy, window, _XA_WM_CLIENT_LEADER, 0, 1,
228 False, AnyPropertyType,
229 &type_ret, &fmt_ret, &nitems_ret, &bytes_after_ret,
230 (unsigned char**)&win)!=Success || !win)
231 return None;
233 leader = (Window)*win;
234 XFree(win);
236 if (type_ret == XA_WINDOW && fmt_ret == 32 && nitems_ret == 1
237 && bytes_after_ret == 0)
238 return leader;
239 else
240 return None;
244 #ifdef R6SM
245 char*
246 PropGetClientID(Window window)
248 XTextProperty txprop;
250 txprop.value = NULL;
252 if (XGetTextProperty(dpy, window, &txprop, _XA_SM_CLIENT_ID)!=Success) {
253 return NULL;
256 if (txprop.encoding == XA_STRING && txprop.format == 8
257 && txprop.nitems > 0) {
259 return (char*)txprop.value;
260 } else {
262 if (txprop.value)
263 XFree(txprop.value);
265 return NULL;
270 char*
271 PropGetWindowRole(Window window)
273 XTextProperty txprop;
275 txprop.value = NULL;
277 if (XGetTextProperty(dpy, window, &txprop, _XA_WM_WINDOW_ROLE)!=Success) {
278 return NULL;
281 if (txprop.encoding == XA_STRING && txprop.format == 8
282 && txprop.nitems > 0) {
284 return (char*)txprop.value;
285 } else {
287 if (txprop.value)
288 XFree(txprop.value);
290 return NULL;
293 #endif /* R6SM */
296 void
297 PropWriteGNUstepWMAttr(Window window, GNUstepWMAttributes *attr)
299 unsigned long data[9];
301 data[0] = attr->flags;
302 data[1] = attr->window_style;
303 data[2] = attr->window_level;
304 data[3] = 0; /* reserved */
305 /* The X protocol says XIDs are 32bit */
306 data[4] = attr->miniaturize_pixmap;
307 data[5] = attr->close_pixmap;
308 data[6] = attr->miniaturize_mask;
309 data[7] = attr->close_mask;
310 data[8] = attr->extra_flags;
311 XChangeProperty(dpy, window, _XA_GNUSTEP_WM_ATTR, _XA_GNUSTEP_WM_ATTR,
312 32, PropModeReplace, (unsigned char *)data, 9);
316 void
317 PropCleanUp(Window root)
319 XDeleteProperty(dpy, root, _XA_WINDOWMAKER_WM_PROTOCOLS);
321 XDeleteProperty(dpy, root, XA_WM_ICON_SIZE);