xref: /netbsd-src/external/bsd/less/dist/scrsize.c (revision 838f5788460f0f133b15d706e644d692a9d4d6ec)
1*838f5788Ssimonb /*	$NetBSD: scrsize.c,v 1.4 2023/10/06 05:49:49 simonb Exp $	*/
220006a0bStron 
320006a0bStron /*
4*838f5788Ssimonb  * Copyright (C) 1984-2023  Mark Nudelman
520006a0bStron  *
620006a0bStron  * You may distribute under the terms of either the GNU General Public
720006a0bStron  * License or the Less License, as specified in the README file.
820006a0bStron  *
9ec18bca0Stron  * For more information, see the README file.
1020006a0bStron  */
1120006a0bStron 
1220006a0bStron /*
1320006a0bStron  * This program is used to determine the screen dimensions on OS/2 systems.
1420006a0bStron  * Adapted from code written by Kyosuke Tokoro (NBG01720@nifty.ne.jp).
1520006a0bStron  */
1620006a0bStron 
1720006a0bStron /*
1820006a0bStron  * When I wrote this routine, I consulted some part of the source code
1920006a0bStron  * of the xwininfo utility by X Consortium.
2020006a0bStron  *
2120006a0bStron  * Copyright (c) 1987, X Consortium
2220006a0bStron  *
2320006a0bStron  * Permission is hereby granted, free of charge, to any person obtaining a copy
2420006a0bStron  * of this software and associated documentation files (the "Software"), to
2520006a0bStron  * deal in the Software without restriction, including without limitation the
2620006a0bStron  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
2720006a0bStron  * sell copies of the Software, and to permit persons to whom the Software is
2820006a0bStron  * furnished to do so, subject to the following conditions:
2920006a0bStron  *
3020006a0bStron  * The above copyright notice and this permission notice shall be included in
3120006a0bStron  * all copies or substantial portions of the Software.
3220006a0bStron  *
3320006a0bStron  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3420006a0bStron  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3520006a0bStron  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
3620006a0bStron  * X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
3720006a0bStron  * AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
3820006a0bStron  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
3920006a0bStron  *
4020006a0bStron  * Except as contained in this notice, the name of the X Consortium shall not
4120006a0bStron  * be used in advertising or otherwise to promote the sale, use or other
4220006a0bStron  * dealings in this Software without prior written authorization from the X
4320006a0bStron  * Consortium.
4420006a0bStron  */
4520006a0bStron #include <X11/Xlib.h>
4620006a0bStron #include <X11/Xutil.h>
4720006a0bStron #include <stdlib.h>
4820006a0bStron #include <stdio.h>
4920006a0bStron 
get_winsize(dpy,window,p_width,p_height)5020006a0bStron static int get_winsize(dpy, window, p_width, p_height)
5120006a0bStron 	Display *dpy;
5220006a0bStron 	Window window;
5320006a0bStron 	int *p_width;
5420006a0bStron 	int *p_height;
5520006a0bStron {
5620006a0bStron 	XWindowAttributes win_attributes;
5720006a0bStron 	XSizeHints hints;
5820006a0bStron 	long longjunk;
5920006a0bStron 
6020006a0bStron 	if (!XGetWindowAttributes(dpy, window, &win_attributes))
6120006a0bStron 		return 1;
6220006a0bStron 	if (!XGetWMNormalHints(dpy, window, &hints, &longjunk))
6320006a0bStron 		return 1;
6420006a0bStron 	if (!(hints.flags & PResizeInc))
6520006a0bStron 		return 1;
6620006a0bStron 	if (hints.width_inc == 0 || hints.height_inc == 0)
6720006a0bStron 		return 1;
6820006a0bStron 	if (!(hints.flags & (PBaseSize|PMinSize)))
6920006a0bStron 		return 1;
7020006a0bStron 	if (hints.flags & PBaseSize)
7120006a0bStron 	{
7220006a0bStron 		win_attributes.width -= hints.base_width;
7320006a0bStron 		win_attributes.height -= hints.base_height;
7420006a0bStron 	} else
7520006a0bStron 	{
7620006a0bStron 		win_attributes.width -= hints.min_width;
7720006a0bStron 		win_attributes.height -= hints.min_height;
7820006a0bStron 	}
7920006a0bStron 	*p_width = win_attributes.width / hints.width_inc;
8020006a0bStron 	*p_height = win_attributes.height / hints.height_inc;
8120006a0bStron 	return 0;
8220006a0bStron }
8320006a0bStron 
main(argc,argv)8420006a0bStron int main(argc, argv)
8520006a0bStron 	int argc;
8620006a0bStron 	char *argv[];
8720006a0bStron {
8820006a0bStron 	char *cp;
8920006a0bStron 	Display *dpy;
9020006a0bStron 	int size[2];
9120006a0bStron 
9220006a0bStron 	_scrsize(size);
9320006a0bStron 	cp = getenv("WINDOWID");
9420006a0bStron 	if (cp != NULL)
9520006a0bStron 	{
9620006a0bStron 		dpy = XOpenDisplay(NULL);
9720006a0bStron 		if (dpy != NULL)
9820006a0bStron 		{
9920006a0bStron 			get_winsize(dpy, (Window) atol(cp), &size[0], &size[1]);
10020006a0bStron 			XCloseDisplay(dpy);
10120006a0bStron 		}
10220006a0bStron 	}
10320006a0bStron 	printf("%i %i\n", size[0], size[1]);
10420006a0bStron 	return (0);
10520006a0bStron }
106