X-Git-Url: https://git.distorted.org.uk/~mdw/xtoys/blobdiff_plain/c1c9f4d2f03061ab9de1563a66f9ee7b816ce8a4..4be8cb2b59106a6c55a235f713cc4ac2ba655df8:/xscsize.c diff --git a/xscsize.c b/xscsize.c index 15abcc0..99d729f 100644 --- a/xscsize.c +++ b/xscsize.c @@ -31,41 +31,88 @@ #include #include +#ifdef HAVE_XRANDR +# include +#endif +#include +#include #include #include +/*----- Data structures ---------------------------------------------------*/ + +struct screen { + int x, y; + unsigned wd, ht; +}; + +/*----- Static variables --------------------------------------------------*/ + +static unsigned int flags = 0; +#define F_SH 1u +#define F_CSH 2u +#define F_SHELL 3u +#define F_EXPORT 4u + /*----- Main code ---------------------------------------------------------*/ static void version(FILE *fp) - { fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); } + { pquis(fp, "$ (xtoys version " VERSION ")\n"); } static void usage(FILE *fp) - { fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS); } + { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); } static void help(FILE *fp) { version(fp); fputc('\n', fp); usage(stdout); - fputs("\n" -"Reads the size of the X root window and outputs it in a form suitable\n" -"for use as a shell assignment statement, defining variables XWIDTH and\n" -"XHEIGHT.\n" -"\n" -"Options:\n" -"\n" -"-h, --help Display this help text\n" -"-u, --usage Display a short usage summary\n" -"-v, --version Display the program's version number\n" -"\n" -"-d, --display=DISPLAY Choose X display to connect to\n" -"-b, --bourne-shell Output text suitable for a Bourne shell\n" -"-c, --c-shell Output text suitable for a C shell\n" -"-x, --export Export the variables into the environment\n", + fputs("\n\ +Reads the size of the X root window and outputs it in a form suitable\n\ +for use as a shell assignment statement, defining variables XWIDTH and\n\ +XHEIGHT.\n\ +\n\ +Options:\n\ +\n\ +-h, --help Display this help text\n\ +-u, --usage Display a short usage summary\n\ +-v, --version Display the program's version number\n\ +\n\ +-d, --display=DISPLAY Choose X display to connect to\n\ +-b, --bourne-shell Output text suitable for a Bourne shell\n\ +-c, --c-shell Output text suitable for a C shell\n\ +-x, --export Export the variables into the environment\n", fp); } +static void print_var(const char *name, int index, unsigned long value) +{ + dstr d = DSTR_INIT; + + if (index >= 0) { + dstr_putf(&d, "XSCR%d_%s", index, name); + name = d.buf; + } + if (flags & F_SH) { + printf("%s=%lu", name, value); + if (flags & F_EXPORT) printf("; export %s", name); + } else if (flags & F_CSH) { + if (flags & F_EXPORT) printf("setenv %s %lu", name, value); + else printf("set %s=%lu", name, value); + } + putchar('\n'); + dstr_destroy(&d); +} + +static int compare_screen(const void *a, const void *b) +{ + const struct screen *s = a, *t = b; + if (s->y != t->y) return (s->y < t->y ? -1 : +1); + else if (s->x != t->x) return (s->x < t->x ? -1 : +1); + else return (0); +} + int main(int argc, char *argv[]) { Display *dpy; @@ -74,11 +121,18 @@ int main(int argc, char *argv[]) unsigned f = 0; unsigned long wd, ht; int sc; +#ifdef HAVE_XRANDR + Window root; + int rrev, rrerr, rrmaj, rrmin; + XRRScreenResources *res; + XRRCrtcInfo *crtc; + struct screen *scr; + size_t nscr, j; + int i; +#endif -#define f_sh 1u -#define f_csh 2u -#define f_shell 3u -#define f_export 4u +#define f_bogus 1u +#define f_multi 2u /* --- Parse command line options --- */ @@ -92,93 +146,125 @@ int main(int argc, char *argv[]) { "display", OPTF_ARGREQ, 0, 'd' }, { "bourne-shell", 0, 0, 'b' }, { "c-shell", 0, 0, 'c' }, + { "multiscreen", 0, 0, 'm' }, { "export", 0, 0, 'x' }, { 0, 0, 0, 0 } }; - int i = getopt_long(argc, argv, "huv d:bcx", opt, 0); - if (i < 0) - break; + int i = getopt_long(argc, argv, "huv" "d:bcmx", opt, 0); + if (i < 0) break; switch (i) { - case 'h': - help(stdout); - exit(0); - break; - case 'u': - usage(stdout); - exit(0); - break; - case 'v': - version(stdout); - exit(0); - break; - - case 'd': - display = optarg; - break; - case 'b': - f |= f_sh; - break; - case 'c': - f |= f_csh; - break; - case 'x': - f |= f_export; - break; - default: - usage(stderr); - exit(EXIT_FAILURE); - break; + case 'h': help(stdout); exit(0); break; + case 'u': usage(stdout); exit(0); break; + case 'v': version(stdout); exit(0); break; + case 'd': display = optarg; break; + case 'b': flags |= F_SH; break; + case 'c': flags |= F_CSH; break; + case 'm': f |= f_multi; break; + case 'x': flags |= F_EXPORT; break; + default: f |= f_bogus; break; } } + if (optind < argc) f |= f_bogus; + if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); } + /* --- Sort out the shell type --- * * * If the shell name contains the string `csh' then assume it's a C shell. * Otherwise assume it's Bourne. This seems to work in practice. */ - if (!(f & f_shell)) { + if (!(flags & F_SHELL)) { s = getenv("SHELL"); - if (!s) - f |= f_sh; - if (strstr(s, "csh")) - f |= f_csh; - else - f |= f_sh; + if (!s) flags |= F_SH; + if (strstr(s, "csh")) flags |= F_CSH; + else flags |= F_SH; } - if ((f & f_sh) && (f & f_csh)) { + if ((flags & F_SH) && (flags & F_CSH)) { fprintf(stderr, "xscsize: make your mind up about your shell type\n"); exit(EXIT_FAILURE); } - /* --- Get the important information --- */ + /* --- Open the display --- */ dpy = XOpenDisplay(display); if (!dpy) { fprintf(stderr, "xscsize: couldn't open display\n"); exit(EXIT_FAILURE); } + + /* --- Fetch the root window size --- * + * + * We might need this whatever happens, so go with the flow. + */ + sc = DefaultScreen(dpy); wd = DisplayWidth(dpy, sc); ht = DisplayHeight(dpy, sc); - XCloseDisplay(dpy); - /* --- Do the output thing --- */ + /* --- Calculate and produce the necessary output --- * + * + * If we're meant to report on individual screens then try to collect + * information about them using the RANDR extension. If that doesn't + * exist, or the version is too ancient, or its otherwise not going to + * work, then pretend there's just one screen that's the size of the root + * window. + */ - if (f & f_sh) { - printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht); - if (f & f_export) - printf("; export XWIDTH XHEIGHT"); - } - if (f & f_csh) { - if (f & f_export) - printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht); - else - printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht); + if (f & f_multi) { +#ifdef HAVE_XRANDR + if (XRRQueryExtension(dpy, &rrev, &rrerr) && + XRRQueryVersion(dpy, &rrmaj, &rrmin) && + (rrmaj > 1 || (rrmaj == 1 && rrmin >= 2))) { + root = RootWindow(dpy, sc); + res = XRRGetScreenResources(dpy, root); + scr = xmalloc(res->ncrtc*sizeof(*scr)); j = 0; + for (i = 0; i < res->ncrtc; i++) { + crtc = XRRGetCrtcInfo(dpy, res, res->crtcs[i]); + if (crtc->mode) { + scr[j].x = crtc->x; scr[j].wd = crtc->width; + scr[j].y = crtc->y; scr[j].ht = crtc->height; + j++; + } + XRRFreeCrtcInfo(crtc); + } + nscr = j; + XRRFreeScreenResources(res); + } else +#endif + { + /* --- The RANDR extension isn't available --- */ + + nscr = 1; + scr = xmalloc(sizeof(*scr)); + scr->x = 0; scr->wd = wd; + scr->y = 0; scr->ht = ht; + } + + /* --- Sort and report the screens --- * + * + * The chances are good that the screens reported by RANDR aren't in any + * especially useful order. Sort them into (my) reading order. + */ + + qsort(scr, nscr, sizeof(*scr), compare_screen); + print_var("XNSCR", -1, nscr); + for (j = 0; j < nscr; j++) { + print_var("X", j, scr[j].x); + print_var("Y", j, scr[j].y); + print_var("WIDTH", j, scr[j].wd); + print_var("HEIGHT", j, scr[j].ht); + } + } else { + print_var("XWIDTH", -1, wd); + print_var("XHEIGHT", -1, ht); } - putchar('\n'); + + /* --- We're done with the display now --- */ + + XCloseDisplay(dpy); /* --- Done --- */