SetHints.c revision 9c019ec5
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 <X11/Xlibint.h>
53#include <X11/Xutil.h>
54#include "Xatomtype.h"
55#include <X11/Xatom.h>
56#include <X11/Xos.h>
57#include "reallocarray.h"
58
59#define safestrlen(s) ((s) ? strlen(s) : 0)
60
61int
62XSetSizeHints(		/* old routine */
63	Display *dpy,
64	Window w,
65	XSizeHints *hints,
66        Atom property)
67{
68	xPropSizeHints prop;
69	memset(&prop, 0, sizeof(prop));
70	prop.flags = (hints->flags & (USPosition|USSize|PAllHints));
71	if (hints->flags & (USPosition|PPosition)) {
72	    prop.x = hints->x;
73	    prop.y = hints->y;
74	}
75	if (hints->flags & (USSize|PSize)) {
76	    prop.width = hints->width;
77	    prop.height = hints->height;
78	}
79	if (hints->flags & PMinSize) {
80	    prop.minWidth = hints->min_width;
81	    prop.minHeight = hints->min_height;
82	}
83	if (hints->flags & PMaxSize) {
84	    prop.maxWidth  = hints->max_width;
85	    prop.maxHeight = hints->max_height;
86	}
87	if (hints->flags & PResizeInc) {
88	    prop.widthInc = hints->width_inc;
89	    prop.heightInc = hints->height_inc;
90	}
91	if (hints->flags & PAspect) {
92	    prop.minAspectX = hints->min_aspect.x;
93	    prop.minAspectY = hints->min_aspect.y;
94	    prop.maxAspectX = hints->max_aspect.x;
95	    prop.maxAspectY = hints->max_aspect.y;
96	}
97	return XChangeProperty (dpy, w, property, XA_WM_SIZE_HINTS, 32,
98				PropModeReplace, (unsigned char *) &prop,
99				OldNumPropSizeElements);
100}
101
102/*
103 * XSetWMHints sets the property
104 *	WM_HINTS 	type: WM_HINTS	format:32
105 */
106
107int
108XSetWMHints (
109	Display *dpy,
110	Window w,
111	XWMHints *wmhints)
112{
113	xPropWMHints prop;
114	memset(&prop, 0, sizeof(prop));
115	prop.flags = wmhints->flags;
116	if (wmhints->flags & InputHint)
117	    prop.input = (wmhints->input == True ? 1 : 0);
118	if (wmhints->flags & StateHint)
119	    prop.initialState = wmhints->initial_state;
120	if (wmhints->flags & IconPixmapHint)
121	    prop.iconPixmap = wmhints->icon_pixmap;
122	if (wmhints->flags & IconWindowHint)
123	    prop.iconWindow = wmhints->icon_window;
124	if (wmhints->flags & IconPositionHint) {
125	    prop.iconX = wmhints->icon_x;
126	    prop.iconY = wmhints->icon_y;
127	}
128	if (wmhints->flags & IconMaskHint)
129	    prop.iconMask = wmhints->icon_mask;
130	if (wmhints->flags & WindowGroupHint)
131	    prop.windowGroup = wmhints->window_group;
132	return XChangeProperty (dpy, w, XA_WM_HINTS, XA_WM_HINTS, 32,
133				PropModeReplace, (unsigned char *) &prop,
134				NumPropWMHintsElements);
135}
136
137
138
139/*
140 * XSetZoomHints sets the property
141 *	WM_ZOOM_HINTS 	type: WM_SIZE_HINTS format: 32
142 */
143
144int
145XSetZoomHints (
146	Display *dpy,
147	Window w,
148	XSizeHints *zhints)
149{
150	return XSetSizeHints (dpy, w, zhints, XA_WM_ZOOM_HINTS);
151}
152
153
154/*
155 * XSetNormalHints sets the property
156 *	WM_NORMAL_HINTS 	type: WM_SIZE_HINTS format: 32
157 */
158
159int
160XSetNormalHints (			/* old routine */
161	Display *dpy,
162	Window w,
163	XSizeHints *hints)
164{
165	return XSetSizeHints (dpy, w, hints, XA_WM_NORMAL_HINTS);
166}
167
168
169
170/*
171 * Note, the following is one of the few cases were we really do want sizeof
172 * when examining a protocol structure.  This is because the XChangeProperty
173 * routine will take care of converting to host to network data structures.
174 */
175
176int
177XSetIconSizes (
178	Display *dpy,
179	Window w,	/* typically, root */
180	XIconSize *list,
181	int count) 	/* number of items on the list */
182{
183	register int i;
184	xPropIconSize *pp, *prop;
185
186	if ((prop = pp = Xmallocarray (count, sizeof(xPropIconSize)))) {
187	    for (i = 0; i < count; i++) {
188		pp->minWidth  = list->min_width;
189		pp->minHeight = list->min_height;
190		pp->maxWidth  = list->max_width;
191		pp->maxHeight = list->max_height;
192		pp->widthInc  = list->width_inc;
193		pp->heightInc = list->height_inc;
194		pp += 1;
195		list += 1;
196	    }
197	    XChangeProperty (dpy, w, XA_WM_ICON_SIZE, XA_WM_ICON_SIZE, 32,
198			     PropModeReplace, (unsigned char *) prop,
199			     count * NumPropIconSizeElements);
200	    Xfree (prop);
201	}
202	return 1;
203}
204
205int
206XSetCommand (
207	Display *dpy,
208	Window w,
209	char **argv,
210	int argc)
211{
212	register int i;
213	size_t nbytes;
214	register char *buf, *bp;
215	for (i = 0, nbytes = 0; i < argc; i++) {
216		nbytes += safestrlen(argv[i]) + 1;
217	}
218	if ((bp = buf = Xmalloc(nbytes))) {
219	    /* copy arguments into single buffer */
220	    for (i = 0; i < argc; i++) {
221		if (argv[i]) {
222		    (void) strcpy(bp, argv[i]);
223		    bp += strlen(argv[i]) + 1;
224		}
225		else
226		    *bp++ = '\0';
227	    }
228	    XChangeProperty (dpy, w, XA_WM_COMMAND, XA_STRING, 8,
229			     PropModeReplace, (unsigned char *)buf, nbytes);
230	    Xfree(buf);
231	}
232	return 1;
233}
234/*
235 * XSetStandardProperties sets the following properties:
236 *	WM_NAME		  type: STRING		format: 8
237 *	WM_ICON_NAME	  type: STRING		format: 8
238 *	WM_HINTS	  type: WM_HINTS	format: 32
239 *	WM_COMMAND	  type: STRING
240 *	WM_NORMAL_HINTS	  type: WM_SIZE_HINTS 	format: 32
241 */
242
243int
244XSetStandardProperties (
245    	Display *dpy,
246    	Window w,		/* window to decorate */
247    	_Xconst char *name,	/* name of application */
248    	_Xconst char *icon_string,/* name string for icon */
249	Pixmap icon_pixmap,	/* pixmap to use as icon, or None */
250    	char **argv,		/* command to be used to restart application */
251    	int argc,		/* count of arguments */
252    	XSizeHints *hints)	/* size hints for window in its normal state */
253{
254	XWMHints phints;
255	phints.flags = 0;
256
257	if (name != NULL) XStoreName (dpy, w, name);
258
259	if (icon_string != NULL) {
260	    XChangeProperty (dpy, w, XA_WM_ICON_NAME, XA_STRING, 8,
261                             PropModeReplace,
262                             (_Xconst unsigned char *)icon_string,
263                             (int)safestrlen(icon_string));
264		}
265
266	if (icon_pixmap != None) {
267		phints.icon_pixmap = icon_pixmap;
268		phints.flags |= IconPixmapHint;
269		}
270	if (argv != NULL) XSetCommand(dpy, w, argv, argc);
271
272	if (hints != NULL) XSetNormalHints(dpy, w, hints);
273
274	if (phints.flags != 0) XSetWMHints(dpy, w, &phints);
275
276	return 1;
277}
278
279int
280XSetTransientForHint(
281	Display *dpy,
282	Window w,
283	Window propWindow)
284{
285	return XChangeProperty(dpy, w, XA_WM_TRANSIENT_FOR, XA_WINDOW, 32,
286			       PropModeReplace, (unsigned char *) &propWindow, 1);
287}
288
289int
290XSetClassHint(
291	Display *dpy,
292	Window w,
293	XClassHint *classhint)
294{
295	char *class_string;
296	char *s;
297	size_t len_nm, len_cl;
298
299	len_nm = safestrlen(classhint->res_name);
300	len_cl = safestrlen(classhint->res_class);
301	if ((class_string = s = Xmalloc(len_nm + len_cl + 2))) {
302	    if (len_nm) {
303		strcpy(s, classhint->res_name);
304		s += len_nm + 1;
305	    }
306	    else
307		*s++ = '\0';
308	    if (len_cl)
309		strcpy(s, classhint->res_class);
310	    else
311		*s = '\0';
312	    XChangeProperty(dpy, w, XA_WM_CLASS, XA_STRING, 8,
313			    PropModeReplace, (unsigned char *) class_string,
314			    len_nm+len_cl+2);
315	    Xfree(class_string);
316	}
317	return 1;
318}
319