90b2c5d4 |
1 | /* -*-c-*- |
2 | * |
47747dbe |
3 | * $Id: xscsize.c,v 1.6 2004/04/08 01:36:29 mdw Exp $ |
90b2c5d4 |
4 | * |
5 | * Return X display size to shell script |
6 | * |
7 | * (c) 1998 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of the Edgeware X tools collection. |
13 | * |
14 | * X tools is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU General Public License as published by |
16 | * the Free Software Foundation; either version 2 of the License, or |
17 | * (at your option) any later version. |
18 | * |
19 | * X tools is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU General Public License |
25 | * along with X tools; if not, write to the Free Software Foundation, |
26 | * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
27 | */ |
28 | |
90b2c5d4 |
29 | /*----- Header files ------------------------------------------------------*/ |
30 | |
31 | #include <stdio.h> |
32 | #include <stdlib.h> |
33 | #include <string.h> |
34 | |
90b2c5d4 |
35 | #include <X11/Xlib.h> |
36 | |
c4efa11c |
37 | #include <mLib/mdwopt.h> |
38 | #include <mLib/quis.h> |
f3b35b6b |
39 | |
90b2c5d4 |
40 | /*----- Main code ---------------------------------------------------------*/ |
41 | |
f3b35b6b |
42 | static void version(FILE *fp) |
43 | { |
44 | fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); |
45 | } |
46 | |
47 | static void usage(FILE *fp) |
48 | { |
49 | fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS); |
50 | } |
51 | |
90b2c5d4 |
52 | int main(int argc, char *argv[]) |
53 | { |
54 | const char *display = 0; |
55 | unsigned f = 0; |
56 | unsigned long wd, ht; |
57 | |
6672918d |
58 | #define f_sh 1u |
59 | #define f_csh 2u |
60 | #define f_shell 3u |
61 | #define f_export 4u |
90b2c5d4 |
62 | |
63 | /* --- Parse command line options --- */ |
64 | |
f3b35b6b |
65 | ego(argv[0]); |
66 | |
90b2c5d4 |
67 | for (;;) { |
f3b35b6b |
68 | static struct option opt[] = { |
deeda449 |
69 | { "help", 0, 0, 'h' }, |
70 | { "usage", 0, 0, 'u' }, |
71 | { "version", 0, 0, 'v' }, |
72 | { "display", OPTF_ARGREQ, 0, 'd' }, |
73 | { "bourne-shell", 0, 0, 'b' }, |
74 | { "c-shell", 0, 0, 'c' }, |
75 | { "export", 0, 0, 'x' }, |
76 | { 0, 0, 0, 0 } |
f3b35b6b |
77 | }; |
78 | |
79 | int i = getopt_long(argc, argv, "huv d:bcx", opt, 0); |
90b2c5d4 |
80 | if (i < 0) |
81 | break; |
82 | switch (i) { |
f3b35b6b |
83 | case 'h': |
84 | version(stdout); |
85 | fputs("\n", stdout); |
86 | usage(stdout); |
87 | fputs( |
88 | "\n" |
89 | "Reads the size of the X root window and outputs it in a form suitable\n" |
90 | "for use as a shell assignment statement, defining variables XWIDTH and\n" |
91 | "XHEIGHT.\n" |
92 | "\n" |
93 | "Options:\n" |
94 | "\n" |
95 | "-h, --help Display this help text\n" |
96 | "-u, --usage Display a short usage summary\n" |
97 | "-v, --version Display the program's version number\n" |
98 | "\n" |
99 | "-d, --display=DISPLAY Choose X display to connect to\n" |
100 | "-b, --bourne-shell Output text suitable for a Bourne shell\n" |
101 | "-c, --c-shell Output text suitable for a C shell\n" |
102 | "-x, --export Export the variables into the environment\n", |
103 | stdout); |
104 | exit(0); |
105 | break; |
106 | case 'u': |
107 | usage(stdout); |
108 | exit(0); |
109 | break; |
110 | case 'v': |
111 | version(stdout); |
112 | exit(0); |
113 | break; |
114 | |
90b2c5d4 |
115 | case 'd': |
116 | display = optarg; |
117 | break; |
118 | case 'b': |
119 | f |= f_sh; |
120 | break; |
121 | case 'c': |
122 | f |= f_csh; |
123 | break; |
124 | case 'x': |
125 | f |= f_export; |
126 | break; |
127 | default: |
f3b35b6b |
128 | usage(stderr); |
90b2c5d4 |
129 | exit(EXIT_FAILURE); |
130 | break; |
131 | } |
132 | } |
133 | |
134 | /* --- Sort out the shell type --- * |
135 | * |
136 | * If the shell name contains the string `csh' then assume it's a C shell. |
137 | * Otherwise assume it's Bourne. This seems to work in practice. |
138 | */ |
139 | |
140 | if (!(f & f_shell)) { |
141 | const char *s = getenv("SHELL"); |
142 | if (!s) |
143 | f |= f_sh; |
144 | if (strstr(s, "csh")) |
145 | f |= f_csh; |
146 | else |
147 | f |= f_sh; |
148 | } |
149 | |
150 | if ((f & f_sh) && (f & f_csh)) { |
151 | fprintf(stderr, "xscsize: make your mind up about your shell type\n"); |
152 | exit(EXIT_FAILURE); |
153 | } |
154 | |
155 | /* --- Get the important information --- */ |
156 | |
157 | { |
158 | Display *dpy = XOpenDisplay(display); |
159 | int sc; |
160 | if (!dpy) { |
161 | fprintf(stderr, "xscsize: couldn't open display\n"); |
162 | exit(EXIT_FAILURE); |
163 | } |
164 | sc = DefaultScreen(dpy); |
165 | wd = DisplayWidth(dpy, sc); |
166 | ht = DisplayHeight(dpy, sc); |
167 | XCloseDisplay(dpy); |
168 | } |
169 | |
170 | /* --- Do the output thing --- */ |
171 | |
172 | if (f & f_sh) { |
173 | printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht); |
174 | if (f & f_export) |
175 | printf("; export XWIDTH XHEIGHT"); |
176 | } |
177 | if (f & f_csh) { |
178 | if (f & f_export) |
179 | printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht); |
180 | else |
181 | printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht); |
182 | } |
183 | putchar('\n'); |
184 | |
185 | /* --- Done --- */ |
186 | |
187 | return (0); |
188 | } |
189 | |
190 | /*----- That's all, folks -------------------------------------------------*/ |