Commit | Line | Data |
---|---|---|
90b2c5d4 | 1 | /* -*-c-*- |
2 | * | |
90b2c5d4 | 3 | * Return X display size to shell script |
4 | * | |
5 | * (c) 1998 Straylight/Edgeware | |
6 | */ | |
7 | ||
cc52f3b6 | 8 | /*----- Licensing notice --------------------------------------------------* |
90b2c5d4 | 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. | |
cc52f3b6 | 16 | * |
90b2c5d4 | 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. | |
cc52f3b6 | 21 | * |
90b2c5d4 | 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 | ||
90b2c5d4 | 27 | /*----- Header files ------------------------------------------------------*/ |
28 | ||
29 | #include <stdio.h> | |
30 | #include <stdlib.h> | |
31 | #include <string.h> | |
32 | ||
90b2c5d4 | 33 | #include <X11/Xlib.h> |
243654c4 MW |
34 | #ifdef HAVE_XRANDR |
35 | # include <X11/extensions/Xrandr.h> | |
36 | #endif | |
90b2c5d4 | 37 | |
243654c4 MW |
38 | #include <mLib/alloc.h> |
39 | #include <mLib/dstr.h> | |
c4efa11c | 40 | #include <mLib/mdwopt.h> |
41 | #include <mLib/quis.h> | |
f3b35b6b | 42 | |
243654c4 MW |
43 | /*----- Data structures ---------------------------------------------------*/ |
44 | ||
45 | struct screen { | |
46 | int x, y; | |
47 | unsigned wd, ht; | |
48 | }; | |
49 | ||
3a4bdbcb MW |
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 | ||
90b2c5d4 | 58 | /*----- Main code ---------------------------------------------------------*/ |
59 | ||
f3b35b6b | 60 | static void version(FILE *fp) |
fb347ff4 | 61 | { pquis(fp, "$ (xtoys version " VERSION ")\n"); } |
f3b35b6b | 62 | |
63 | static void usage(FILE *fp) | |
ac50b4b3 | 64 | { pquis(fp, "Usage: $ [-bcmx] [-d DISPLAY]\n"); } |
cc52f3b6 MW |
65 | |
66 | static void help(FILE *fp) | |
f3b35b6b | 67 | { |
cc52f3b6 MW |
68 | version(fp); |
69 | fputc('\n', fp); | |
70 | usage(stdout); | |
fb347ff4 MW |
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\ | |
ac50b4b3 | 85 | -m, --multiscreen Describe each screen individually\n\ |
fb347ff4 | 86 | -x, --export Export the variables into the environment\n", |
cc52f3b6 | 87 | fp); |
f3b35b6b | 88 | } |
89 | ||
243654c4 | 90 | static void print_var(const char *name, int index, unsigned long value) |
e5c4fa9d | 91 | { |
243654c4 MW |
92 | dstr d = DSTR_INIT; |
93 | ||
e5c4fa9d MW |
94 | if (index >= 0) { |
95 | dstr_putf(&d, "XSCR%d_%s", index, name); | |
96 | name = d.buf; | |
97 | } | |
98 | if (flags & F_SH) { | |
99 | printf("%s=%lu", name, value); | |
100 | if (flags & F_EXPORT) printf("; export %s", name); | |
101 | } else if (flags & F_CSH) { | |
102 | if (flags & F_EXPORT) printf("setenv %s %lu", name, value); | |
103 | else printf("set %s=%lu", name, value); | |
104 | } | |
105 | putchar('\n'); | |
106 | dstr_destroy(&d); | |
107 | } | |
108 | ||
243654c4 MW |
109 | static int compare_screen(const void *a, const void *b) |
110 | { | |
111 | const struct screen *s = a, *t = b; | |
112 | if (s->y != t->y) return (s->y < t->y ? -1 : +1); | |
113 | else if (s->x != t->x) return (s->x < t->x ? -1 : +1); | |
114 | else return (0); | |
115 | } | |
116 | ||
90b2c5d4 | 117 | int main(int argc, char *argv[]) |
118 | { | |
cc52f3b6 MW |
119 | Display *dpy; |
120 | const char *s; | |
90b2c5d4 | 121 | const char *display = 0; |
122 | unsigned f = 0; | |
123 | unsigned long wd, ht; | |
cc52f3b6 | 124 | int sc; |
577e53c6 MW |
125 | struct screen *scr; |
126 | size_t nscr, j; | |
243654c4 MW |
127 | #ifdef HAVE_XRANDR |
128 | Window root; | |
129 | int rrev, rrerr, rrmaj, rrmin; | |
130 | XRRScreenResources *res; | |
131 | XRRCrtcInfo *crtc; | |
243654c4 MW |
132 | int i; |
133 | #endif | |
90b2c5d4 | 134 | |
3a4bdbcb | 135 | #define f_bogus 1u |
243654c4 | 136 | #define f_multi 2u |
90b2c5d4 | 137 | |
138 | /* --- Parse command line options --- */ | |
139 | ||
f3b35b6b | 140 | ego(argv[0]); |
141 | ||
90b2c5d4 | 142 | for (;;) { |
f3b35b6b | 143 | static struct option opt[] = { |
c1c9f4d2 MW |
144 | { "help", 0, 0, 'h' }, |
145 | { "usage", 0, 0, 'u' }, | |
146 | { "version", 0, 0, 'v' }, | |
147 | { "display", OPTF_ARGREQ, 0, 'd' }, | |
deeda449 | 148 | { "bourne-shell", 0, 0, 'b' }, |
149 | { "c-shell", 0, 0, 'c' }, | |
243654c4 | 150 | { "multiscreen", 0, 0, 'm' }, |
deeda449 | 151 | { "export", 0, 0, 'x' }, |
152 | { 0, 0, 0, 0 } | |
f3b35b6b | 153 | }; |
154 | ||
243654c4 | 155 | int i = getopt_long(argc, argv, "huv" "d:bcmx", opt, 0); |
fb347ff4 | 156 | if (i < 0) break; |
90b2c5d4 | 157 | switch (i) { |
fb347ff4 MW |
158 | case 'h': help(stdout); exit(0); break; |
159 | case 'u': usage(stdout); exit(0); break; | |
160 | case 'v': version(stdout); exit(0); break; | |
161 | case 'd': display = optarg; break; | |
3a4bdbcb MW |
162 | case 'b': flags |= F_SH; break; |
163 | case 'c': flags |= F_CSH; break; | |
243654c4 | 164 | case 'm': f |= f_multi; break; |
3a4bdbcb | 165 | case 'x': flags |= F_EXPORT; break; |
fb347ff4 | 166 | default: f |= f_bogus; break; |
90b2c5d4 | 167 | } |
168 | } | |
169 | ||
fb347ff4 MW |
170 | if (optind < argc) f |= f_bogus; |
171 | if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); } | |
172 | ||
90b2c5d4 | 173 | /* --- Sort out the shell type --- * |
174 | * | |
175 | * If the shell name contains the string `csh' then assume it's a C shell. | |
176 | * Otherwise assume it's Bourne. This seems to work in practice. | |
177 | */ | |
178 | ||
3a4bdbcb | 179 | if (!(flags & F_SHELL)) { |
cc52f3b6 | 180 | s = getenv("SHELL"); |
3a4bdbcb MW |
181 | if (!s) flags |= F_SH; |
182 | if (strstr(s, "csh")) flags |= F_CSH; | |
183 | else flags |= F_SH; | |
90b2c5d4 | 184 | } |
185 | ||
3a4bdbcb | 186 | if ((flags & F_SH) && (flags & F_CSH)) { |
90b2c5d4 | 187 | fprintf(stderr, "xscsize: make your mind up about your shell type\n"); |
188 | exit(EXIT_FAILURE); | |
189 | } | |
190 | ||
243654c4 | 191 | /* --- Open the display --- */ |
90b2c5d4 | 192 | |
cc52f3b6 MW |
193 | dpy = XOpenDisplay(display); |
194 | if (!dpy) { | |
195 | fprintf(stderr, "xscsize: couldn't open display\n"); | |
196 | exit(EXIT_FAILURE); | |
90b2c5d4 | 197 | } |
243654c4 MW |
198 | |
199 | /* --- Fetch the root window size --- * | |
200 | * | |
201 | * We might need this whatever happens, so go with the flow. | |
202 | */ | |
203 | ||
cc52f3b6 MW |
204 | sc = DefaultScreen(dpy); |
205 | wd = DisplayWidth(dpy, sc); | |
206 | ht = DisplayHeight(dpy, sc); | |
90b2c5d4 | 207 | |
243654c4 MW |
208 | /* --- Calculate and produce the necessary output --- * |
209 | * | |
210 | * If we're meant to report on individual screens then try to collect | |
211 | * information about them using the RANDR extension. If that doesn't | |
212 | * exist, or the version is too ancient, or its otherwise not going to | |
213 | * work, then pretend there's just one screen that's the size of the root | |
214 | * window. | |
215 | */ | |
216 | ||
217 | if (f & f_multi) { | |
218 | #ifdef HAVE_XRANDR | |
219 | if (XRRQueryExtension(dpy, &rrev, &rrerr) && | |
220 | XRRQueryVersion(dpy, &rrmaj, &rrmin) && | |
221 | (rrmaj > 1 || (rrmaj == 1 && rrmin >= 2))) { | |
222 | root = RootWindow(dpy, sc); | |
223 | res = XRRGetScreenResources(dpy, root); | |
224 | scr = xmalloc(res->ncrtc*sizeof(*scr)); j = 0; | |
225 | for (i = 0; i < res->ncrtc; i++) { | |
226 | crtc = XRRGetCrtcInfo(dpy, res, res->crtcs[i]); | |
227 | if (crtc->mode) { | |
228 | scr[j].x = crtc->x; scr[j].wd = crtc->width; | |
229 | scr[j].y = crtc->y; scr[j].ht = crtc->height; | |
230 | j++; | |
231 | } | |
232 | XRRFreeCrtcInfo(crtc); | |
233 | } | |
234 | nscr = j; | |
235 | XRRFreeScreenResources(res); | |
236 | } else | |
237 | #endif | |
238 | { | |
239 | /* --- The RANDR extension isn't available --- */ | |
240 | ||
241 | nscr = 1; | |
242 | scr = xmalloc(sizeof(*scr)); | |
243 | scr->x = 0; scr->wd = wd; | |
244 | scr->y = 0; scr->ht = ht; | |
245 | } | |
246 | ||
247 | /* --- Sort and report the screens --- * | |
248 | * | |
249 | * The chances are good that the screens reported by RANDR aren't in any | |
250 | * especially useful order. Sort them into (my) reading order. | |
251 | */ | |
90b2c5d4 | 252 | |
243654c4 MW |
253 | qsort(scr, nscr, sizeof(*scr), compare_screen); |
254 | print_var("XNSCR", -1, nscr); | |
255 | for (j = 0; j < nscr; j++) { | |
256 | print_var("X", j, scr[j].x); | |
257 | print_var("Y", j, scr[j].y); | |
258 | print_var("WIDTH", j, scr[j].wd); | |
259 | print_var("HEIGHT", j, scr[j].ht); | |
260 | } | |
261 | } else { | |
262 | print_var("XWIDTH", -1, wd); | |
263 | print_var("XHEIGHT", -1, ht); | |
264 | } | |
265 | ||
266 | /* --- We're done with the display now --- */ | |
267 | ||
268 | XCloseDisplay(dpy); | |
90b2c5d4 | 269 | |
270 | /* --- Done --- */ | |
271 | ||
272 | return (0); | |
273 | } | |
274 | ||
275 | /*----- That's all, folks -------------------------------------------------*/ |