GetHints.c revision 61b2299d
1/* $Xorg: GetHints.c,v 1.5 2001/02/09 02:03:33 xorgcvs Exp $ */
2
3/***********************************************************
4
5Copyright 1987, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27
28Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
29
30                        All Rights Reserved
31
32Permission to use, copy, modify, and distribute this software and its
33documentation for any purpose and without fee is hereby granted,
34provided that the above copyright notice appear in all copies and that
35both that copyright notice and this permission notice appear in
36supporting documentation, and that the name of Digital not be
37used in advertising or publicity pertaining to distribution of the
38software without specific, written prior permission.
39
40DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
41ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
42DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
43ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
44WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
45ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
46SOFTWARE.
47
48******************************************************************/
49/* $XFree86: xc/lib/X11/GetHints.c,v 1.3 2001/01/17 19:41:36 dawes Exp $ */
50
51#ifdef HAVE_CONFIG_H
52#include <config.h>
53#endif
54#include <X11/Xlibint.h>
55#include <X11/Xos.h>
56#include <X11/Xutil.h>
57#include "Xatomtype.h"
58#include <X11/Xatom.h>
59#include <stdio.h>
60
61Status XGetSizeHints (
62	Display *dpy,
63	Window w,
64	XSizeHints *hints,
65        Atom property)
66{
67	xPropSizeHints *prop = NULL;
68        Atom actual_type;
69        int actual_format;
70        unsigned long leftover;
71        unsigned long nitems;
72	if (XGetWindowProperty(dpy, w, property, 0L,
73			       (long) OldNumPropSizeElements,
74	    False, XA_WM_SIZE_HINTS, &actual_type, &actual_format,
75            &nitems, &leftover, (unsigned char **)&prop)
76            != Success) return (0);
77
78        if ((actual_type != XA_WM_SIZE_HINTS) ||
79	    (nitems < OldNumPropSizeElements) || (actual_format != 32)) {
80		if (prop != NULL) Xfree ((char *)prop);
81                return(0);
82		}
83	hints->flags	  = (prop->flags & (USPosition|USSize|PAllHints));
84	hints->x 	  = cvtINT32toInt (prop->x);
85	hints->y 	  = cvtINT32toInt (prop->y);
86	hints->width      = cvtINT32toInt (prop->width);
87	hints->height     = cvtINT32toInt (prop->height);
88	hints->min_width  = cvtINT32toInt (prop->minWidth);
89	hints->min_height = cvtINT32toInt (prop->minHeight);
90	hints->max_width  = cvtINT32toInt (prop->maxWidth);
91	hints->max_height = cvtINT32toInt (prop->maxHeight);
92	hints->width_inc  = cvtINT32toInt (prop->widthInc);
93	hints->height_inc = cvtINT32toInt (prop->heightInc);
94	hints->min_aspect.x = cvtINT32toInt (prop->minAspectX);
95	hints->min_aspect.y = cvtINT32toInt (prop->minAspectY);
96	hints->max_aspect.x = cvtINT32toInt (prop->maxAspectX);
97	hints->max_aspect.y = cvtINT32toInt (prop->maxAspectY);
98	Xfree((char *)prop);
99	return(1);
100}
101
102/*
103 * must return a pointer to the hint, in malloc'd memory, or routine is not
104 * extensible; any use of the caller's memory would cause things to be stepped
105 * on.
106 */
107
108XWMHints *XGetWMHints (
109	Display *dpy,
110	Window w)
111{
112	xPropWMHints *prop = NULL;
113	register XWMHints *hints;
114        Atom actual_type;
115        int actual_format;
116        unsigned long leftover;
117        unsigned long nitems;
118	if (XGetWindowProperty(dpy, w, XA_WM_HINTS,
119	    0L, (long)NumPropWMHintsElements,
120	    False, XA_WM_HINTS, &actual_type, &actual_format,
121            &nitems, &leftover, (unsigned char **)&prop)
122            != Success) return (NULL);
123
124	/* If the property is undefined on the window, return null pointer. */
125	/* pre-R3 bogusly truncated window_group, don't fail on them */
126
127        if ((actual_type != XA_WM_HINTS) ||
128	    (nitems < (NumPropWMHintsElements - 1)) || (actual_format != 32)) {
129		if (prop != NULL) Xfree ((char *)prop);
130                return(NULL);
131		}
132	/* static copies not allowed in library, due to reentrancy constraint*/
133	if ((hints = (XWMHints *) Xcalloc (1, (unsigned) sizeof(XWMHints)))) {
134	    hints->flags = prop->flags;
135	    hints->input = (prop->input ? True : False);
136	    hints->initial_state = cvtINT32toInt (prop->initialState);
137	    hints->icon_pixmap = prop->iconPixmap;
138	    hints->icon_window = prop->iconWindow;
139	    hints->icon_x = cvtINT32toInt (prop->iconX);
140	    hints->icon_y = cvtINT32toInt (prop->iconY);
141	    hints->icon_mask = prop->iconMask;
142	    if (nitems >= NumPropWMHintsElements)
143		hints->window_group = prop->windowGroup;
144	    else
145		hints->window_group = 0;
146	}
147	Xfree ((char *)prop);
148	return(hints);
149}
150
151Status
152XGetZoomHints (
153	Display *dpy,
154	Window w,
155	XSizeHints *zhints)
156{
157	return (XGetSizeHints(dpy, w, zhints, XA_WM_ZOOM_HINTS));
158}
159
160Status
161XGetNormalHints (
162	Display *dpy,
163	Window w,
164	XSizeHints *hints)
165{
166	return (XGetSizeHints(dpy, w, hints, XA_WM_NORMAL_HINTS));
167}
168
169
170/*
171 * XGetIconSizes reads the property
172 *	ICONSIZE_ATOM	type: ICONSIZE_ATOM format: 32
173 */
174
175Status XGetIconSizes (
176	Display *dpy,
177	Window w,	/* typically, root */
178	XIconSize **size_list,	/* RETURN */
179	int *count) 		/* RETURN number of items on the list */
180{
181	xPropIconSize *prop = NULL;
182	register xPropIconSize *pp;
183	register XIconSize *hp, *hints;
184        Atom actual_type;
185        int actual_format;
186        unsigned long leftover;
187        unsigned long nitems;
188	register int i;
189
190	if (XGetWindowProperty(dpy, w, XA_WM_ICON_SIZE, 0L, 60L,
191	    False, XA_WM_ICON_SIZE, &actual_type, &actual_format,
192            &nitems, &leftover, (unsigned char **)&prop)
193            != Success) return (0);
194
195	pp = prop;
196
197        if ((actual_type != XA_WM_ICON_SIZE) ||
198	    (nitems < NumPropIconSizeElements) ||
199	    (nitems % NumPropIconSizeElements != 0) ||
200	    (actual_format != 32)) {
201		if (prop != NULL) Xfree ((char *)prop);
202                return(0);
203		}
204
205	/* static copies not allowed in library, due to reentrancy constraint*/
206
207	nitems /= NumPropIconSizeElements;
208	if (! (hp = hints = (XIconSize *)
209	  Xcalloc ((unsigned) nitems, (unsigned) sizeof(XIconSize)))) {
210	    if (prop) Xfree ((char *) prop);
211	    return 0;
212	}
213
214	/* march down array putting things into native form */
215	for (i = 0; i < nitems; i++) {
216	    hp->min_width  = cvtINT32toInt (pp->minWidth);
217	    hp->min_height = cvtINT32toInt (pp->minHeight);
218	    hp->max_width  = cvtINT32toInt (pp->maxWidth);
219	    hp->max_height = cvtINT32toInt (pp->maxHeight);
220	    hp->width_inc  = cvtINT32toInt (pp->widthInc);
221	    hp->height_inc = cvtINT32toInt (pp->heightInc);
222	    hp += 1;
223	    pp += 1;
224	}
225	*count = nitems;
226	*size_list = hints;
227	Xfree ((char *)prop);
228	return(1);
229}
230
231
232Status XGetCommand (
233    Display *dpy,
234    Window w,
235    char ***argvp,
236    int *argcp)
237{
238    XTextProperty tp;
239    int argc;
240    char **argv;
241
242    if (!XGetTextProperty (dpy, w, &tp, XA_WM_COMMAND)) return 0;
243
244    if (tp.encoding != XA_STRING || tp.format != 8) {
245	if (tp.value) Xfree ((char *) tp.value);
246	return 0;
247    }
248
249
250    /*
251     * ignore final <NUL> if present since UNIX WM_COMMAND is nul-terminated
252     */
253    if (tp.nitems && (tp.value[tp.nitems - 1] == '\0')) tp.nitems--;
254
255
256    /*
257     * create a string list and return if successful
258     */
259    if (!XTextPropertyToStringList (&tp, &argv, &argc)) {
260	if (tp.value) Xfree ((char *) tp.value);
261	return (0);
262    }
263
264    if (tp.value) Xfree ((char *) tp.value);
265    *argvp = argv;
266    *argcp = argc;
267    return 1;
268}
269
270
271Status
272XGetTransientForHint(
273	Display *dpy,
274	Window w,
275	Window *propWindow)
276{
277    Atom actual_type;
278    int actual_format;
279    unsigned long nitems;
280    unsigned long leftover;
281    Window *data = NULL;
282    if (XGetWindowProperty(dpy, w, XA_WM_TRANSIENT_FOR, 0L, 1L, False,
283        XA_WINDOW,
284	&actual_type,
285	&actual_format, &nitems, &leftover, (unsigned char **) &data)
286	!= Success) {
287	*propWindow = None;
288	return (0);
289	}
290    if ( (actual_type == XA_WINDOW) && (actual_format == 32) &&
291	 (nitems != 0) ) {
292	*propWindow = *data;
293	Xfree( (char *) data);
294	return (1);
295	}
296    *propWindow = None;
297    if (data) Xfree( (char *) data);
298    return(0);
299}
300
301Status
302XGetClassHint(
303	Display *dpy,
304	Window w,
305	XClassHint *classhint)	/* RETURN */
306{
307    int len_name, len_class;
308
309    Atom actual_type;
310    int actual_format;
311    unsigned long nitems;
312    unsigned long leftover;
313    unsigned char *data = NULL;
314    if (XGetWindowProperty(dpy, w, XA_WM_CLASS, 0L, (long)BUFSIZ, False,
315        XA_STRING,
316	&actual_type,
317	&actual_format, &nitems, &leftover, &data) != Success)
318           return (0);
319
320   if ( (actual_type == XA_STRING) && (actual_format == 8) ) {
321	len_name = strlen((char *) data);
322	if (! (classhint->res_name = Xmalloc((unsigned) (len_name+1)))) {
323	    Xfree((char *) data);
324	    return (0);
325	}
326	strcpy(classhint->res_name, (char *) data);
327	if (len_name == nitems) len_name--;
328	len_class = strlen((char *) (data+len_name+1));
329	if (! (classhint->res_class = Xmalloc((unsigned) (len_class+1)))) {
330	    Xfree(classhint->res_name);
331	    classhint->res_name = (char *) NULL;
332	    Xfree((char *) data);
333	    return (0);
334	}
335	strcpy(classhint->res_class, (char *) (data+len_name+1));
336	Xfree( (char *) data);
337	return(1);
338	}
339    if (data) Xfree( (char *) data);
340    return(0);
341}
342