]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/less/scrsize.c
Note r317395 as merged.
[FreeBSD/FreeBSD.git] / contrib / less / scrsize.c
1 /*
2  * Copyright (C) 1984-2015  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(Display *dpy, Window window, int *p_width, int *p_height)
49 {
50         XWindowAttributes win_attributes;
51         XSizeHints hints;
52         long longjunk;
53
54         if (!XGetWindowAttributes(dpy, window, &win_attributes))
55                 return 1;
56         if (!XGetWMNormalHints(dpy, window, &hints, &longjunk))
57                 return 1;
58         if (!(hints.flags & PResizeInc))
59                 return 1;
60         if (hints.width_inc == 0 || hints.height_inc == 0)
61                 return 1;
62         if (!(hints.flags & (PBaseSize|PMinSize)))
63                 return 1;
64         if (hints.flags & PBaseSize)
65         {
66                 win_attributes.width -= hints.base_width;
67                 win_attributes.height -= hints.base_height;
68         } else
69         {
70                 win_attributes.width -= hints.min_width;
71                 win_attributes.height -= hints.min_height;
72         }
73         *p_width = win_attributes.width / hints.width_inc;
74         *p_height = win_attributes.height / hints.height_inc;
75         return 0;
76 }
77
78 int main(int argc, char *argv[])
79 {
80         char *cp;
81         Display *dpy;
82         int size[2];
83
84         _scrsize(size);
85         cp = getenv("WINDOWID");
86         if (cp != NULL)
87         {
88                 dpy = XOpenDisplay(NULL);
89                 if (dpy != NULL)
90                 {
91                         get_winsize(dpy, (Window) atol(cp), &size[0], &size[1]);
92                         XCloseDisplay(dpy);
93                 }
94         }
95         printf("%i %i\n", size[0], size[1]);
96         return (0);
97 }