Release 1.6.0.
[xtoys] / xscsize.c
1 /* -*-c-*-
2 *
3 * Return X display size to shell script
4 *
5 * (c) 1998 Straylight/Edgeware
6 */
7
8 /*----- Licensing notice --------------------------------------------------*
9 *
10 * This file is part of the Edgeware X tools collection.
11 *
12 * X tools is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
16 *
17 * X tools is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with X tools; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
25 */
26
27 /*----- Header files ------------------------------------------------------*/
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include <X11/Xlib.h>
34 #ifdef HAVE_XRANDR
35 # include <X11/extensions/Xrandr.h>
36 #endif
37
38 #include <mLib/alloc.h>
39 #include <mLib/dstr.h>
40 #include <mLib/mdwopt.h>
41 #include <mLib/quis.h>
42
43 /*----- Data structures ---------------------------------------------------*/
44
45 struct screen {
46 int x, y;
47 unsigned wd, ht;
48 };
49
50 /*----- Static variables --------------------------------------------------*/
51
52 static unsigned int flags = 0;
53 #define F_SH 1u
54 #define F_CSH 2u
55 #define F_SHELL 3u
56 #define F_EXPORT 4u
57
58 /*----- Main code ---------------------------------------------------------*/
59
60 static void version(FILE *fp)
61 { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
62
63 static void usage(FILE *fp)
64 { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
65
66 static void help(FILE *fp)
67 {
68 version(fp);
69 fputc('\n', fp);
70 usage(stdout);
71 fputs("\n\
72 Reads the size of the X root window and outputs it in a form suitable\n\
73 for use as a shell assignment statement, defining variables XWIDTH and\n\
74 XHEIGHT.\n\
75 \n\
76 Options:\n\
77 \n\
78 -h, --help Display this help text\n\
79 -u, --usage Display a short usage summary\n\
80 -v, --version Display the program's version number\n\
81 \n\
82 -d, --display=DISPLAY Choose X display to connect to\n\
83 -b, --bourne-shell Output text suitable for a Bourne shell\n\
84 -c, --c-shell Output text suitable for a C shell\n\
85 -x, --export Export the variables into the environment\n",
86 fp);
87 }
88
89 static void print_var(const char *name, int index, unsigned long value)
90 {
91 dstr d = DSTR_INIT;
92
93 if (index >= 0) {
94 dstr_putf(&d, "XSCR%d_%s", index, name);
95 name = d.buf;
96 }
97 if (flags & F_SH) {
98 printf("%s=%lu", name, value);
99 if (flags & F_EXPORT) printf("; export %s", name);
100 } else if (flags & F_CSH) {
101 if (flags & F_EXPORT) printf("setenv %s %lu", name, value);
102 else printf("set %s=%lu", name, value);
103 }
104 putchar('\n');
105 dstr_destroy(&d);
106 }
107
108 static int compare_screen(const void *a, const void *b)
109 {
110 const struct screen *s = a, *t = b;
111 if (s->y != t->y) return (s->y < t->y ? -1 : +1);
112 else if (s->x != t->x) return (s->x < t->x ? -1 : +1);
113 else return (0);
114 }
115
116 int main(int argc, char *argv[])
117 {
118 Display *dpy;
119 const char *s;
120 const char *display = 0;
121 unsigned f = 0;
122 unsigned long wd, ht;
123 int sc;
124 #ifdef HAVE_XRANDR
125 Window root;
126 int rrev, rrerr, rrmaj, rrmin;
127 XRRScreenResources *res;
128 XRRCrtcInfo *crtc;
129 struct screen *scr;
130 size_t nscr, j;
131 int i;
132 #endif
133
134 #define f_bogus 1u
135 #define f_multi 2u
136
137 /* --- Parse command line options --- */
138
139 ego(argv[0]);
140
141 for (;;) {
142 static struct option opt[] = {
143 { "help", 0, 0, 'h' },
144 { "usage", 0, 0, 'u' },
145 { "version", 0, 0, 'v' },
146 { "display", OPTF_ARGREQ, 0, 'd' },
147 { "bourne-shell", 0, 0, 'b' },
148 { "c-shell", 0, 0, 'c' },
149 { "multiscreen", 0, 0, 'm' },
150 { "export", 0, 0, 'x' },
151 { 0, 0, 0, 0 }
152 };
153
154 int i = getopt_long(argc, argv, "huv" "d:bcmx", opt, 0);
155 if (i < 0) break;
156 switch (i) {
157 case 'h': help(stdout); exit(0); break;
158 case 'u': usage(stdout); exit(0); break;
159 case 'v': version(stdout); exit(0); break;
160 case 'd': display = optarg; break;
161 case 'b': flags |= F_SH; break;
162 case 'c': flags |= F_CSH; break;
163 case 'm': f |= f_multi; break;
164 case 'x': flags |= F_EXPORT; break;
165 default: f |= f_bogus; break;
166 }
167 }
168
169 if (optind < argc) f |= f_bogus;
170 if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
171
172 /* --- Sort out the shell type --- *
173 *
174 * If the shell name contains the string `csh' then assume it's a C shell.
175 * Otherwise assume it's Bourne. This seems to work in practice.
176 */
177
178 if (!(flags & F_SHELL)) {
179 s = getenv("SHELL");
180 if (!s) flags |= F_SH;
181 if (strstr(s, "csh")) flags |= F_CSH;
182 else flags |= F_SH;
183 }
184
185 if ((flags & F_SH) && (flags & F_CSH)) {
186 fprintf(stderr, "xscsize: make your mind up about your shell type\n");
187 exit(EXIT_FAILURE);
188 }
189
190 /* --- Open the display --- */
191
192 dpy = XOpenDisplay(display);
193 if (!dpy) {
194 fprintf(stderr, "xscsize: couldn't open display\n");
195 exit(EXIT_FAILURE);
196 }
197
198 /* --- Fetch the root window size --- *
199 *
200 * We might need this whatever happens, so go with the flow.
201 */
202
203 sc = DefaultScreen(dpy);
204 wd = DisplayWidth(dpy, sc);
205 ht = DisplayHeight(dpy, sc);
206
207 /* --- Calculate and produce the necessary output --- *
208 *
209 * If we're meant to report on individual screens then try to collect
210 * information about them using the RANDR extension. If that doesn't
211 * exist, or the version is too ancient, or its otherwise not going to
212 * work, then pretend there's just one screen that's the size of the root
213 * window.
214 */
215
216 if (f & f_multi) {
217 #ifdef HAVE_XRANDR
218 if (XRRQueryExtension(dpy, &rrev, &rrerr) &&
219 XRRQueryVersion(dpy, &rrmaj, &rrmin) &&
220 (rrmaj > 1 || (rrmaj == 1 && rrmin >= 2))) {
221 root = RootWindow(dpy, sc);
222 res = XRRGetScreenResources(dpy, root);
223 scr = xmalloc(res->ncrtc*sizeof(*scr)); j = 0;
224 for (i = 0; i < res->ncrtc; i++) {
225 crtc = XRRGetCrtcInfo(dpy, res, res->crtcs[i]);
226 if (crtc->mode) {
227 scr[j].x = crtc->x; scr[j].wd = crtc->width;
228 scr[j].y = crtc->y; scr[j].ht = crtc->height;
229 j++;
230 }
231 XRRFreeCrtcInfo(crtc);
232 }
233 nscr = j;
234 XRRFreeScreenResources(res);
235 } else
236 #endif
237 {
238 /* --- The RANDR extension isn't available --- */
239
240 nscr = 1;
241 scr = xmalloc(sizeof(*scr));
242 scr->x = 0; scr->wd = wd;
243 scr->y = 0; scr->ht = ht;
244 }
245
246 /* --- Sort and report the screens --- *
247 *
248 * The chances are good that the screens reported by RANDR aren't in any
249 * especially useful order. Sort them into (my) reading order.
250 */
251
252 qsort(scr, nscr, sizeof(*scr), compare_screen);
253 print_var("XNSCR", -1, nscr);
254 for (j = 0; j < nscr; j++) {
255 print_var("X", j, scr[j].x);
256 print_var("Y", j, scr[j].y);
257 print_var("WIDTH", j, scr[j].wd);
258 print_var("HEIGHT", j, scr[j].ht);
259 }
260 } else {
261 print_var("XWIDTH", -1, wd);
262 print_var("XHEIGHT", -1, ht);
263 }
264
265 /* --- We're done with the display now --- */
266
267 XCloseDisplay(dpy);
268
269 /* --- Done --- */
270
271 return (0);
272 }
273
274 /*----- That's all, folks -------------------------------------------------*/