New file: mdwfocus.c
[xtoys] / xscsize.c
CommitLineData
90b2c5d4 1/* -*-c-*-
2 *
f3b35b6b 3 * $Id: xscsize.c,v 1.2 1998/11/21 22:30:22 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
29/*----- Revision history --------------------------------------------------*
30 *
31 * $Log: xscsize.c,v $
f3b35b6b 32 * Revision 1.2 1998/11/21 22:30:22 mdw
33 * Support GNU-style long options throughout, and introduce proper help
34 * text to all programs. Update manual pages to match.
35 *
90b2c5d4 36 * Revision 1.1 1998/11/16 23:00:49 mdw
37 * Initial versions.
38 *
39 */
40
41/*----- Header files ------------------------------------------------------*/
42
43#include <stdio.h>
44#include <stdlib.h>
45#include <string.h>
46
90b2c5d4 47#include <X11/Xlib.h>
48
f3b35b6b 49#include "mdwopt.h"
50#include "quis.h"
51
90b2c5d4 52/*----- Main code ---------------------------------------------------------*/
53
f3b35b6b 54static void version(FILE *fp)
55{
56 fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
57}
58
59static void usage(FILE *fp)
60{
61 fprintf(fp, "Usage: %s [-bcx] [-d display]\n", QUIS);
62}
63
90b2c5d4 64int main(int argc, char *argv[])
65{
66 const char *display = 0;
67 unsigned f = 0;
68 unsigned long wd, ht;
69
70 enum {
71 f_sh = 1,
72 f_csh = 2,
73 f_shell = 3,
74 f_export = 4
75 };
76
77 /* --- Parse command line options --- */
78
f3b35b6b 79 ego(argv[0]);
80
90b2c5d4 81 for (;;) {
f3b35b6b 82 static struct option opt[] = {
83 { "help", 0, 0, 'h' },
84 { "usage", 0, 0, 'u' },
85 { "version", 0, 0, 'v' },
86 { "display", required_argument, 0, 'd' },
87 { "bourne-shell", 0, 0, 'b' },
88 { "c-shell", 0, 0, 'c' },
89 { "export", 0, 0, 'x' },
90 { 0, 0, 0, 0 }
91 };
92
93 int i = getopt_long(argc, argv, "huv d:bcx", opt, 0);
90b2c5d4 94 if (i < 0)
95 break;
96 switch (i) {
f3b35b6b 97 case 'h':
98 version(stdout);
99 fputs("\n", stdout);
100 usage(stdout);
101 fputs(
102"\n"
103"Reads the size of the X root window and outputs it in a form suitable\n"
104"for use as a shell assignment statement, defining variables XWIDTH and\n"
105"XHEIGHT.\n"
106"\n"
107"Options:\n"
108"\n"
109"-h, --help Display this help text\n"
110"-u, --usage Display a short usage summary\n"
111"-v, --version Display the program's version number\n"
112"\n"
113"-d, --display=DISPLAY Choose X display to connect to\n"
114"-b, --bourne-shell Output text suitable for a Bourne shell\n"
115"-c, --c-shell Output text suitable for a C shell\n"
116"-x, --export Export the variables into the environment\n",
117 stdout);
118 exit(0);
119 break;
120 case 'u':
121 usage(stdout);
122 exit(0);
123 break;
124 case 'v':
125 version(stdout);
126 exit(0);
127 break;
128
90b2c5d4 129 case 'd':
130 display = optarg;
131 break;
132 case 'b':
133 f |= f_sh;
134 break;
135 case 'c':
136 f |= f_csh;
137 break;
138 case 'x':
139 f |= f_export;
140 break;
141 default:
f3b35b6b 142 usage(stderr);
90b2c5d4 143 exit(EXIT_FAILURE);
144 break;
145 }
146 }
147
148 /* --- Sort out the shell type --- *
149 *
150 * If the shell name contains the string `csh' then assume it's a C shell.
151 * Otherwise assume it's Bourne. This seems to work in practice.
152 */
153
154 if (!(f & f_shell)) {
155 const char *s = getenv("SHELL");
156 if (!s)
157 f |= f_sh;
158 if (strstr(s, "csh"))
159 f |= f_csh;
160 else
161 f |= f_sh;
162 }
163
164 if ((f & f_sh) && (f & f_csh)) {
165 fprintf(stderr, "xscsize: make your mind up about your shell type\n");
166 exit(EXIT_FAILURE);
167 }
168
169 /* --- Get the important information --- */
170
171 {
172 Display *dpy = XOpenDisplay(display);
173 int sc;
174 if (!dpy) {
175 fprintf(stderr, "xscsize: couldn't open display\n");
176 exit(EXIT_FAILURE);
177 }
178 sc = DefaultScreen(dpy);
179 wd = DisplayWidth(dpy, sc);
180 ht = DisplayHeight(dpy, sc);
181 XCloseDisplay(dpy);
182 }
183
184 /* --- Do the output thing --- */
185
186 if (f & f_sh) {
187 printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
188 if (f & f_export)
189 printf("; export XWIDTH XHEIGHT");
190 }
191 if (f & f_csh) {
192 if (f & f_export)
193 printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
194 else
195 printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
196 }
197 putchar('\n');
198
199 /* --- Done --- */
200
201 return (0);
202}
203
204/*----- That's all, folks -------------------------------------------------*/