xref: /netbsd-src/external/bsd/less/dist/scrsize.c (revision 20006a0bde522c99e03e2f0935b37976c345030a)
1 /*	$NetBSD	*/
2 
3 /*
4  * Copyright (C) 1984-2011  Mark Nudelman
5  *
6  * You may distribute under the terms of either the GNU General Public
7  * License or the Less License, as specified in the README file.
8  *
9  * For more information about less, or for information on how to
10  * contact the author, see the README file.
11  */
12 
13 /*
14  * This program is used to determine the screen dimensions on OS/2 systems.
15  * Adapted from code written by Kyosuke Tokoro (NBG01720@nifty.ne.jp).
16  */
17 
18 /*
19  * When I wrote this routine, I consulted some part of the source code
20  * of the xwininfo utility by X Consortium.
21  *
22  * Copyright (c) 1987, X Consortium
23  *
24  * Permission is hereby granted, free of charge, to any person obtaining a copy
25  * of this software and associated documentation files (the "Software"), to
26  * deal in the Software without restriction, including without limitation the
27  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
28  * sell copies of the Software, and to permit persons to whom the Software is
29  * furnished to do so, subject to the following conditions:
30  *
31  * The above copyright notice and this permission notice shall be included in
32  * all copies or substantial portions of the Software.
33  *
34  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
37  * X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
38  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
39  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
40  *
41  * Except as contained in this notice, the name of the X Consortium shall not
42  * be used in advertising or otherwise to promote the sale, use or other
43  * dealings in this Software without prior written authorization from the X
44  * Consortium.
45  */
46 #include <X11/Xlib.h>
47 #include <X11/Xutil.h>
48 #include <stdlib.h>
49 #include <stdio.h>
50 
51 static int get_winsize(dpy, window, p_width, p_height)
52 	Display *dpy;
53 	Window window;
54 	int *p_width;
55 	int *p_height;
56 {
57 	XWindowAttributes win_attributes;
58 	XSizeHints hints;
59 	long longjunk;
60 
61 	if (!XGetWindowAttributes(dpy, window, &win_attributes))
62 		return 1;
63 	if (!XGetWMNormalHints(dpy, window, &hints, &longjunk))
64 		return 1;
65 	if (!(hints.flags & PResizeInc))
66 		return 1;
67 	if (hints.width_inc == 0 || hints.height_inc == 0)
68 		return 1;
69 	if (!(hints.flags & (PBaseSize|PMinSize)))
70 		return 1;
71 	if (hints.flags & PBaseSize)
72 	{
73 		win_attributes.width -= hints.base_width;
74 		win_attributes.height -= hints.base_height;
75 	} else
76 	{
77 		win_attributes.width -= hints.min_width;
78 		win_attributes.height -= hints.min_height;
79 	}
80 	*p_width = win_attributes.width / hints.width_inc;
81 	*p_height = win_attributes.height / hints.height_inc;
82 	return 0;
83 }
84 
85 int main(argc, argv)
86 	int argc;
87 	char *argv[];
88 {
89 	char *cp;
90 	Display *dpy;
91 	int size[2];
92 
93 	_scrsize(size);
94 	cp = getenv("WINDOWID");
95 	if (cp != NULL)
96 	{
97 		dpy = XOpenDisplay(NULL);
98 		if (dpy != NULL)
99 		{
100 			get_winsize(dpy, (Window) atol(cp), &size[0], &size[1]);
101 			XCloseDisplay(dpy);
102 		}
103 	}
104 	printf("%i %i\n", size[0], size[1]);
105 	return (0);
106 }
107