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