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