SetHints.c revision 9c927599
1
2/***********************************************************
3
4Copyright 1987, 1998  The Open Group
5
6Permission to use, copy, modify, distribute, and sell this software and its
7documentation for any purpose is hereby granted without fee, provided that
8the above copyright notice appear in all copies and that both that
9copyright notice and this permission notice appear in supporting
10documentation.
11
12The above copyright notice and this permission notice shall be included in
13all copies or substantial portions of the Software.
14
15THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
18OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
19AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22Except as contained in this notice, the name of The Open Group shall not be
23used in advertising or otherwise to promote the sale, use or other dealings
24in this Software without prior written authorization from The Open Group.
25
26
27Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
28
29                        All Rights Reserved
30
31Permission to use, copy, modify, and distribute this software and its
32documentation for any purpose and without fee is hereby granted,
33provided that the above copyright notice appear in all copies and that
34both that copyright notice and this permission notice appear in
35supporting documentation, and that the name of Digital not be
36used in advertising or publicity pertaining to distribution of the
37software without specific, written prior permission.
38
39DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
40ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
41DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
42ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
43WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
44ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
45SOFTWARE.
46
47******************************************************************/
48
49#ifdef HAVE_CONFIG_H
50#include <config.h>
51#endif
52#include <limits.h>
53#include <X11/Xlibint.h>
54#include <X11/Xutil.h>
55#include "Xatomtype.h"
56#include <X11/Xatom.h>
57#include <X11/Xos.h>
58#include "reallocarray.h"
59
60#define safestrlen(s) ((s) ? strlen(s) : 0)
61
62int
63XSetSizeHints(		/* old routine */
64	Display *dpy,
65	Window w,
66	XSizeHints *hints,
67        Atom property)
68{
69	xPropSizeHints prop;
70	memset(&prop, 0, sizeof(prop));
71	prop.flags = (hints->flags & (USPosition|USSize|PAllHints));
72	if (hints->flags & (USPosition|PPosition)) {
73	    prop.x = hints->x;
74	    prop.y = hints->y;
75	}
76	if (hints->flags & (USSize|PSize)) {
77	    prop.width = hints->width;
78	    prop.height = hints->height;
79	}
80	if (hints->flags & PMinSize) {
81	    prop.minWidth = hints->min_width;
82	    prop.minHeight = hints->min_height;
83	}
84	if (hints->flags & PMaxSize) {
85	    prop.maxWidth  = hints->max_width;
86	    prop.maxHeight = hints->max_height;
87	}
88	if (hints->flags & PResizeInc) {
89	    prop.widthInc = hints->width_inc;
90	    prop.heightInc = hints->height_inc;
91	}
92	if (hints->flags & PAspect) {
93	    prop.minAspectX = hints->min_aspect.x;
94	    prop.minAspectY = hints->min_aspect.y;
95	    prop.maxAspectX = hints->max_aspect.x;
96	    prop.maxAspectY = hints->max_aspect.y;
97	}
98	return XChangeProperty (dpy, w, property, XA_WM_SIZE_HINTS, 32,
99				PropModeReplace, (unsigned char *) &prop,
100				OldNumPropSizeElements);
101}
102
103/*
104 * XSetWMHints sets the property
105 *	WM_HINTS 	type: WM_HINTS	format:32
106 */
107
108int
109XSetWMHints (
110	Display *dpy,
111	Window w,
112	XWMHints *wmhints)
113{
114	xPropWMHints prop;
115	memset(&prop, 0, sizeof(prop));
116	prop.flags = wmhints->flags;
117	if (wmhints->flags & InputHint)
118	    prop.input = (wmhints->input == True ? 1 : 0);
119	if (wmhints->flags & StateHint)
120	    prop.initialState = wmhints->initial_state;
121	if (wmhints->flags & IconPixmapHint)
122	    prop.iconPixmap = wmhints->icon_pixmap;
123	if (wmhints->flags & IconWindowHint)
124	    prop.iconWindow = wmhints->icon_window;
125	if (wmhints->flags & IconPositionHint) {
126	    prop.iconX = wmhints->icon_x;
127	    prop.iconY = wmhints->icon_y;
128	}
129	if (wmhints->flags & IconMaskHint)
130	    prop.iconMask = wmhints->icon_mask;
131	if (wmhints->flags & WindowGroupHint)
132	    prop.windowGroup = wmhints->window_group;
133	return XChangeProperty (dpy, w, XA_WM_HINTS, XA_WM_HINTS, 32,
134				PropModeReplace, (unsigned char *) &prop,
135				NumPropWMHintsElements);
136}
137
138
139
140/*
141 * XSetZoomHints sets the property
142 *	WM_ZOOM_HINTS 	type: WM_SIZE_HINTS format: 32
143 */
144
145int
146XSetZoomHints (
147	Display *dpy,
148	Window w,
149	XSizeHints *zhints)
150{
151	return XSetSizeHints (dpy, w, zhints, XA_WM_ZOOM_HINTS);
152}
153
154
155/*
156 * XSetNormalHints sets the property
157 *	WM_NORMAL_HINTS 	type: WM_SIZE_HINTS format: 32
158 */
159
160int
161XSetNormalHints (			/* old routine */
162	Display *dpy,
163	Window w,
164	XSizeHints *hints)
165{
166	return XSetSizeHints (dpy, w, hints, XA_WM_NORMAL_HINTS);
167}
168
169
170
171/*
172 * Note, the following is one of the few cases were we really do want sizeof
173 * when examining a protocol structure.  This is because the XChangeProperty
174 * routine will take care of converting to host to network data structures.
175 */
176
177int
178XSetIconSizes (
179	Display *dpy,
180	Window w,	/* typically, root */
181	XIconSize *list,
182	int count) 	/* number of items on the list */
183{
184	register int i;
185	xPropIconSize *pp, *prop;
186
187	if ((prop = pp = Xmallocarray (count, sizeof(xPropIconSize)))) {
188	    for (i = 0; i < count; i++) {
189		pp->minWidth  = list->min_width;
190		pp->minHeight = list->min_height;
191		pp->maxWidth  = list->max_width;
192		pp->maxHeight = list->max_height;
193		pp->widthInc  = list->width_inc;
194		pp->heightInc = list->height_inc;
195		pp += 1;
196		list += 1;
197	    }
198	    XChangeProperty (dpy, w, XA_WM_ICON_SIZE, XA_WM_ICON_SIZE, 32,
199			     PropModeReplace, (unsigned char *) prop,
200			     count * NumPropIconSizeElements);
201	    Xfree (prop);
202	}
203	return 1;
204}
205
206int
207XSetCommand (
208	Display *dpy,
209	Window w,
210	char **argv,
211	int argc)
212{
213	register int i;
214	size_t nbytes;
215	register char *buf, *bp;
216	for (i = 0, nbytes = 0; i < argc; i++) {
217		nbytes += safestrlen(argv[i]) + 1;
218		if (nbytes >= USHRT_MAX)
219                    return 1;
220	}
221	if ((bp = buf = Xmalloc(nbytes))) {
222	    /* copy arguments into single buffer */
223	    for (i = 0; i < argc; i++) {
224		if (argv[i]) {
225		    (void) strcpy(bp, argv[i]);
226		    bp += strlen(argv[i]) + 1;
227		}
228		else
229		    *bp++ = '\0';
230	    }
231	    XChangeProperty (dpy, w, XA_WM_COMMAND, XA_STRING, 8,
232			     PropModeReplace, (unsigned char *)buf, nbytes);
233	    Xfree(buf);
234	}
235	return 1;
236}
237/*
238 * XSetStandardProperties sets the following properties:
239 *	WM_NAME		  type: STRING		format: 8
240 *	WM_ICON_NAME	  type: STRING		format: 8
241 *	WM_HINTS	  type: WM_HINTS	format: 32
242 *	WM_COMMAND	  type: STRING
243 *	WM_NORMAL_HINTS	  type: WM_SIZE_HINTS 	format: 32
244 */
245
246int
247XSetStandardProperties (
248    	Display *dpy,
249    	Window w,		/* window to decorate */
250    	_Xconst char *name,	/* name of application */
251    	_Xconst char *icon_string,/* name string for icon */
252	Pixmap icon_pixmap,	/* pixmap to use as icon, or None */
253    	char **argv,		/* command to be used to restart application */
254    	int argc,		/* count of arguments */
255    	XSizeHints *hints)	/* size hints for window in its normal state */
256{
257	XWMHints phints;
258	phints.flags = 0;
259
260	if (name != NULL) XStoreName (dpy, w, name);
261
262        if (safestrlen(icon_string) >= USHRT_MAX)
263            return 1;
264	if (icon_string != NULL) {
265	    XChangeProperty (dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
266                             PropModeReplace,
267                             (_Xconst unsigned char *)icon_string,
268                             (int)safestrlen(icon_string));
269		}
270
271	if (icon_pixmap != None) {
272		phints.icon_pixmap = icon_pixmap;
273		phints.flags |= IconPixmapHint;
274		}
275	if (argv != NULL) XSetCommand(dpy, w, argv, argc);
276
277	if (hints != NULL) XSetNormalHints(dpy, w, hints);
278
279	if (phints.flags != 0) XSetWMHints(dpy, w, &phints);
280
281	return 1;
282}
283
284int
285XSetTransientForHint(
286	Display *dpy,
287	Window w,
288	Window propWindow)
289{
290	return XChangeProperty(dpy, w, XA_WM_TRANSIENT_FOR, XA_WINDOW, 32,
291			       PropModeReplace, (unsigned char *) &propWindow, 1);
292}
293
294int
295XSetClassHint(
296	Display *dpy,
297	Window w,
298	XClassHint *classhint)
299{
300	char *class_string;
301	char *s;
302	size_t len_nm, len_cl;
303
304	len_nm = safestrlen(classhint->res_name);
305	len_cl = safestrlen(classhint->res_class);
306        if (len_nm + len_cl >= USHRT_MAX)
307            return 1;
308	if ((class_string = s = Xmalloc(len_nm + len_cl + 2))) {
309	    if (len_nm) {
310		strcpy(s, classhint->res_name);
311		s += len_nm + 1;
312	    }
313	    else
314		*s++ = '\0';
315	    if (len_cl)
316		strcpy(s, classhint->res_class);
317	    else
318		*s = '\0';
319	    XChangeProperty(dpy, w, XA_WM_CLASS, XA_STRING, 8,
320			    PropModeReplace, (unsigned char *) class_string,
321			    len_nm+len_cl+2);
322	    Xfree(class_string);
323	}
324	return 1;
325}
326