chiark / gitweb /
ace018e0a39dc99018a99362c11c8f359ef7eece
[xtoys] / xscsize.c
1 /* -*-c-*-
2  *
3  * $Id: xscsize.c,v 1.1 1998/11/16 23:00:49 mdw Exp $
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 $
32  * Revision 1.1  1998/11/16 23:00:49  mdw
33  * Initial versions.
34  *
35  */
36
37 /*----- Header files ------------------------------------------------------*/
38
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42
43 #include <getopt.h>
44 #include <X11/Xlib.h>
45
46 /*----- Main code ---------------------------------------------------------*/
47
48 int main(int argc, char *argv[])
49 {
50   const char *display = 0;
51   unsigned f = 0;
52   unsigned long wd, ht;
53
54   enum {
55     f_sh = 1,
56     f_csh = 2,
57     f_shell = 3,
58     f_export = 4
59   };
60
61   /* --- Parse command line options --- */
62
63   for (;;) {
64     int i = getopt(argc, argv, "d:bcx");
65     if (i < 0)
66       break;
67     switch (i) {
68       case 'd':
69         display = optarg;
70         break;
71       case 'b':
72         f |= f_sh;
73         break;
74       case 'c':
75         f |= f_csh;
76         break;
77       case 'x':
78         f |= f_export;
79         break;
80       default:
81         fprintf(stderr, "Usage: xscsize [-bcx] [-d DISPLAY]\n");
82         exit(EXIT_FAILURE);
83         break;
84     }
85   }
86
87   /* --- Sort out the shell type --- *
88    *
89    * If the shell name contains the string `csh' then assume it's a C shell.
90    * Otherwise assume it's Bourne.  This seems to work in practice.
91    */
92
93   if (!(f & f_shell)) {
94     const char *s = getenv("SHELL");
95     if (!s)
96       f |= f_sh;
97     if (strstr(s, "csh"))
98       f |= f_csh;
99     else
100       f |= f_sh;
101   }
102
103   if ((f & f_sh) && (f & f_csh)) {
104     fprintf(stderr, "xscsize: make your mind up about your shell type\n");
105     exit(EXIT_FAILURE);
106   }
107
108   /* --- Get the important information --- */
109
110   {
111     Display *dpy = XOpenDisplay(display);
112     int sc;
113     if (!dpy) {
114       fprintf(stderr, "xscsize: couldn't open display\n");
115       exit(EXIT_FAILURE);
116     }
117     sc = DefaultScreen(dpy);
118     wd = DisplayWidth(dpy, sc);
119     ht = DisplayHeight(dpy, sc);
120     XCloseDisplay(dpy);
121   }
122
123   /* --- Do the output thing --- */
124
125   if (f & f_sh) {
126     printf("XWIDTH=%lu XHEIGHT=%lu", wd, ht);
127     if (f & f_export)
128       printf("; export XWIDTH XHEIGHT");
129   }
130   if (f & f_csh) {
131     if (f & f_export)
132       printf("setenv XWIDTH %lu; setenv XHEIGHT %lu", wd, ht);
133     else
134       printf("set XWIDTH=%lu XHEIGHT=%lu", wd, ht);
135   }
136   putchar('\n');
137
138   /* --- Done --- */
139
140   return (0);
141 }
142
143 /*----- That's all, folks -------------------------------------------------*/