SetHints.c revision 1ab64890
1/* $Xorg: SetHints.c,v 1.5 2001/02/09 02:03:36 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/SetHints.c,v 1.4 2001/12/14 19:54:06 dawes Exp $ */
50
51#ifdef HAVE_CONFIG_H
52#include <config.h>
53#endif
54#include <X11/Xlibint.h>
55#include <X11/Xutil.h>
56#include "Xatomtype.h"
57#include <X11/Xatom.h>
58#include <X11/Xos.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#define size_of_the_real_thing sizeof	/* avoid grepping screwups */
187	unsigned nbytes = count * size_of_the_real_thing(xPropIconSize);
188#undef size_of_the_real_thing
189	if ((prop = pp = (xPropIconSize *) Xmalloc (nbytes))) {
190	    for (i = 0; i < count; i++) {
191		pp->minWidth  = list->min_width;
192		pp->minHeight = list->min_height;
193		pp->maxWidth  = list->max_width;
194		pp->maxHeight = list->max_height;
195		pp->widthInc  = list->width_inc;
196		pp->heightInc = list->height_inc;
197		pp += 1;
198		list += 1;
199	    }
200	    XChangeProperty (dpy, w, XA_WM_ICON_SIZE, XA_WM_ICON_SIZE, 32,
201			     PropModeReplace, (unsigned char *) prop,
202			     count * NumPropIconSizeElements);
203	    Xfree ((char *)prop);
204	}
205	return 1;
206}
207
208int
209XSetCommand (
210	Display *dpy,
211	Window w,
212	char **argv,
213	int argc)
214{
215	register int i;
216	register int nbytes;
217	register char *buf, *bp;
218	for (i = 0, nbytes = 0; i < argc; i++) {
219		nbytes += safestrlen(argv[i]) + 1;
220	}
221	if ((bp = buf = Xmalloc((unsigned) 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 (icon_string != NULL) {
263	    XChangeProperty (dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
264		PropModeReplace, (unsigned char *)icon_string, safestrlen(icon_string));
265		}
266
267	if (icon_pixmap != None) {
268		phints.icon_pixmap = icon_pixmap;
269		phints.flags |= IconPixmapHint;
270		}
271	if (argv != NULL) XSetCommand(dpy, w, argv, argc);
272
273	if (hints != NULL) XSetNormalHints(dpy, w, hints);
274
275	if (phints.flags != 0) XSetWMHints(dpy, w, &phints);
276
277	return 1;
278}
279
280int
281XSetTransientForHint(
282	Display *dpy,
283	Window w,
284	Window propWindow)
285{
286	return XChangeProperty(dpy, w, XA_WM_TRANSIENT_FOR, XA_WINDOW, 32,
287			       PropModeReplace, (unsigned char *) &propWindow, 1);
288}
289
290int
291XSetClassHint(
292	Display *dpy,
293	Window w,
294	XClassHint *classhint)
295{
296	char *class_string;
297	char *s;
298	int len_nm, len_cl;
299
300	len_nm = safestrlen(classhint->res_name);
301	len_cl = safestrlen(classhint->res_class);
302	if ((class_string = s = Xmalloc((unsigned) (len_nm + len_cl + 2)))) {
303	    if (len_nm) {
304		strcpy(s, classhint->res_name);
305		s += len_nm + 1;
306	    }
307	    else
308		*s++ = '\0';
309	    if (len_cl)
310		strcpy(s, classhint->res_class);
311	    else
312		*s = '\0';
313	    XChangeProperty(dpy, w, XA_WM_CLASS, XA_STRING, 8,
314			    PropModeReplace, (unsigned char *) class_string,
315			    len_nm+len_cl+2);
316	    Xfree(class_string);
317	}
318	return 1;
319}
320