3 * Return X display size to shell script
5 * (c) 1998 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of the Edgeware X tools collection.
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.
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.
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.
27 /*----- Header files ------------------------------------------------------*/
35 #include <mLib/mdwopt.h>
36 #include <mLib/quis.h>
38 /*----- Static variables --------------------------------------------------*/
40 static unsigned int flags = 0;
46 /*----- Main code ---------------------------------------------------------*/
48 static void version(FILE *fp)
49 { pquis(fp, "$ (xtoys version " VERSION ")\n"); }
51 static void usage(FILE *fp)
52 { pquis(fp, "Usage: $ [-bcx] [-d DISPLAY]\n"); }
54 static void help(FILE *fp)
60 Reads the size of the X root window and outputs it in a form suitable\n\
61 for use as a shell assignment statement, defining variables XWIDTH and\n\
66 -h, --help Display this help text\n\
67 -u, --usage Display a short usage summary\n\
68 -v, --version Display the program's version number\n\
70 -d, --display=DISPLAY Choose X display to connect to\n\
71 -b, --bourne-shell Output text suitable for a Bourne shell\n\
72 -c, --c-shell Output text suitable for a C shell\n\
73 -x, --export Export the variables into the environment\n",
77 int main(int argc, char *argv[])
81 const char *display = 0;
88 /* --- Parse command line options --- */
93 static struct option opt[] = {
94 { "help", 0, 0, 'h' },
95 { "usage", 0, 0, 'u' },
96 { "version", 0, 0, 'v' },
97 { "display", OPTF_ARGREQ, 0, 'd' },
98 { "bourne-shell", 0, 0, 'b' },
99 { "c-shell", 0, 0, 'c' },
100 { "export", 0, 0, 'x' },
104 int i = getopt_long(argc, argv, "huv" "d:bcx", opt, 0);
107 case 'h': help(stdout); exit(0); break;
108 case 'u': usage(stdout); exit(0); break;
109 case 'v': version(stdout); exit(0); break;
110 case 'd': display = optarg; break;
111 case 'b': flags |= F_SH; break;
112 case 'c': flags |= F_CSH; break;
113 case 'x': flags |= F_EXPORT; break;
114 default: f |= f_bogus; break;
118 if (optind < argc) f |= f_bogus;
119 if (f & f_bogus) { usage(stderr); exit(EXIT_FAILURE); }
121 /* --- Sort out the shell type --- *
123 * If the shell name contains the string `csh' then assume it's a C shell.
124 * Otherwise assume it's Bourne. This seems to work in practice.
127 if (!(flags & F_SHELL)) {
129 if (!s) flags |= F_SH;
130 if (strstr(s, "csh")) flags |= F_CSH;
134 if ((flags & F_SH) && (flags & F_CSH)) {
135 fprintf(stderr, "xscsize: make your mind up about your shell type\n");
139 /* --- Get the important information --- */
141 dpy = XOpenDisplay(display);
143 fprintf(stderr, "xscsize: couldn't open display\n");
146 sc = DefaultScreen(dpy);
147 wd = DisplayWidth(dpy, sc);
148 ht = DisplayHeight(dpy, sc);
151 /* --- Do the output thing --- */
154 printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
156 printf("; export XWIDTH XHEIGHT");
160 printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
162 printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
171 /*----- That's all, folks -------------------------------------------------*/