1 /*
2 
3 Copyright 1989, 1998  The Open Group
4 
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10 
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13 
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
17 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
18 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 
21 Except as contained in this notice, the name of The Open Group shall not be
22 used in advertising or otherwise to promote the sale, use or other dealings
23 in this Software without prior written authorization from The Open Group.
24 
25 */
26 
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
30 #include "Xlibint.h"
31 #include "Xutil.h"
32 
33 static int _GeometryMaskToGravity(
34     int mask);
35 
36 /*
37  * This routine given a user supplied positional argument and a default
38  * argument (fully qualified) will return the position the window should take
39  * as well as the gravity to be set in the WM_NORMAL_HINTS size hints.
40  * Always sets all return values and returns a mask describing which fields
41  * were set by the user or'ed with whether or not the x and y values should
42  * be considered "negative".
43  */
44 
45 int
XWMGeometry(Display * dpy,int screen,_Xconst char * user_geom,_Xconst char * def_geom,unsigned int bwidth,XSizeHints * hints,int * x_return,int * y_return,int * width_return,int * height_return,int * gravity_return)46 XWMGeometry (
47     Display *dpy,			/* user's display connection */
48     int screen,				/* screen on which to do computation */
49     _Xconst char *user_geom,		/* user provided geometry spec */
50     _Xconst char *def_geom,		/* default geometry spec for window */
51     unsigned int bwidth,		/* border width */
52     XSizeHints *hints,			/* usually WM_NORMAL_HINTS */
53     int *x_return,			/* location of window */
54     int *y_return,			/* location of window */
55     int *width_return,			/* size of window */
56     int *height_return,			/* size of window */
57     int *gravity_return)		/* gravity of window */
58 {
59     int ux, uy;				/* returned values from parse */
60     unsigned int uwidth, uheight;	/* returned values from parse */
61     int umask;				/* parse mask of returned values */
62     int dx, dy;				/* default values from parse */
63     unsigned int dwidth, dheight;	/* default values from parse */
64     int dmask;				/* parse mask of returned values */
65     int base_width, base_height;	/* valid amounts */
66     int min_width, min_height;		/* valid amounts */
67     int width_inc, height_inc;		/* valid amounts */
68     int rx, ry, rwidth, rheight;	/* return values */
69     int rmask;				/* return mask */
70 
71     /*
72      * Get the base sizes and increments.  Section 4.1.2.3 of the ICCCM
73      * states that the base and minimum sizes are defaults for each other.
74      * If neither is given, then the base sizes should be 0.  These parameters
75      * control the sets of sizes that window managers should allow for the
76      * window according to the following formulae:
77      *
78      *          width = base_width  + (i * width_inc)
79      *         height = base_height + (j * height_inc)
80      */
81     base_width =  ((hints->flags & PBaseSize) ? hints->base_width :
82 		   ((hints->flags & PMinSize) ? hints->min_width : 0));
83     base_height = ((hints->flags & PBaseSize) ? hints->base_height :
84 		   ((hints->flags & PMinSize) ? hints->min_height : 0));
85     min_width = ((hints->flags & PMinSize) ? hints->min_width : base_width);
86     min_height = ((hints->flags & PMinSize) ? hints->min_height : base_height);
87     width_inc = (hints->flags & PResizeInc) ? hints->width_inc : 1;
88     height_inc = (hints->flags & PResizeInc) ? hints->height_inc : 1;
89 
90 
91     /*
92      * parse the two geometry masks
93      */
94     rmask = umask = XParseGeometry (user_geom, &ux, &uy, &uwidth, &uheight);
95     dmask = XParseGeometry (def_geom, &dx, &dy, &dwidth, &dheight);
96 
97 
98     /*
99      * get the width and height:
100      *     1.  if user-specified, then take that value
101      *     2.  else, if program-specified, then take that value
102      *     3.  else, take 1
103      *     4.  multiply by the size increment
104      *     5.  and add to the base size
105      */
106     rwidth = ((((umask & WidthValue) ? uwidth :
107 		((dmask & WidthValue) ? dwidth : 1)) * width_inc) +
108 	      base_width);
109     rheight = ((((umask & HeightValue) ? uheight :
110 		 ((dmask & HeightValue) ? dheight : 1)) * height_inc) +
111 	       base_height);
112 
113     /*
114      * Make sure computed size is within limits.  Note that we always do the
115      * lower bounds check since the base size (which defaults to 0) should
116      * be used if a minimum size isn't specified.
117      */
118     if (rwidth < min_width) rwidth = min_width;
119     if (rheight < min_height) rheight = min_height;
120 
121     if (hints->flags & PMaxSize) {
122 	if (rwidth > hints->max_width) rwidth = hints->max_width;
123 	if (rheight > hints->max_height) rheight = hints->max_height;
124     }
125 
126 
127     /*
128      * Compute the location.  Set the negative flags in the return mask
129      * (and watch out for borders), if necessary.
130      */
131     if (umask & XValue) {
132 	rx = ((umask & XNegative) ?
133 	      (DisplayWidth (dpy, screen) + ux - rwidth - 2 * bwidth) : ux);
134     } else if (dmask & XValue) {
135 	if (dmask & XNegative) {
136 	    rx = (DisplayWidth (dpy, screen) + dx - rwidth - 2 * bwidth);
137 	    rmask |= XNegative;
138 	} else
139 	  rx = dx;
140     } else {
141 	rx = 0;				/* gotta choose something... */
142     }
143 
144     if (umask & YValue) {
145 	ry = ((umask & YNegative) ?
146 	      (DisplayHeight(dpy, screen) + uy - rheight - 2 * bwidth) : uy);
147     } else if (dmask & YValue) {
148 	if (dmask & YNegative) {
149 	    ry = (DisplayHeight(dpy, screen) + dy - rheight - 2 * bwidth);
150 	    rmask |= YNegative;
151 	} else
152 	  ry = dy;
153     } else {
154 	ry = 0;				/* gotta choose something... */
155     }
156 
157 
158     /*
159      * All finished, so set the return variables.
160      */
161     *x_return = rx;
162     *y_return = ry;
163     *width_return = rwidth;
164     *height_return = rheight;
165     *gravity_return = _GeometryMaskToGravity (rmask);
166     return rmask;
167 }
168 
169 
_GeometryMaskToGravity(int mask)170 static int _GeometryMaskToGravity(
171     int mask)
172 {
173     switch (mask & (XNegative|YNegative)) {
174       case 0:
175         return NorthWestGravity;
176       case XNegative:
177         return NorthEastGravity;
178       case YNegative:
179         return SouthWestGravity;
180       default:
181         return SouthEastGravity;
182     }
183 }
184