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