File: animate.c

package info (click to toggle)
acm 5.0-23.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 8,364 kB
  • ctags: 4,793
  • sloc: ansic: 42,444; makefile: 706; cpp: 293; perl: 280; sh: 198
file content (287 lines) | stat: -rw-r--r-- 7,944 bytes parent folder | download | duplicates (15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
 *  acm : an aerial combat simulator for X
 *  Copyright (C) 1991,1992  Riley Rainey
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 dated June, 1991.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program;  if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave., Cambridge, MA 02139, USA.
 */
#include <stdio.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>

static char *id = "V library animation test -- Riley Rainey";

#define STRING	"V library test"
#define BORDER	1
#define FONT	"fixed"
#define	ARG_FONT		"font"
#define	ARG_BORDER_COLOR	"borderColor"
#define	ARG_BACKGROUND		"background"
#define ARG_BORDER		"borderWidth"
#define	ARG_GEOMETRY		"geometry"
#define DEFAULT_BACKGROUND	"#29350B"
#define DEFAULT_BORDER		"black"

#define SW_BORDER	1
#define SW_BG		3
#define SW_GEOM		4

struct {
	char	*sw;
	int	value;
	}	switches[] = {
	"-bw", SW_BORDER,
	"-bg", SW_BG,
	"-geometry", SW_GEOM,
	NULL, 0}, *swp;

char *background = NULL, *filename;
int  borderWidth = 1;
int  mono;


/*
 * This structure forms the WM_HINTS property of the window,
 * letting the window manager know how to handle this window.
 * See Section 9.1 of the Xlib manual.
 */
XWMHints	xwmh = {
    (InputHint|StateHint),	/* flags */
    False,			/* input */
    NormalState,		/* initial_state */
    0,				/* icon pixmap */
    0,				/* icon window */
    0, 0,			/* icon location */
    0,				/* icon mask */
    0,				/* Window group */
};

Display		*dpy;		/* X server connection */
Window		win;		/* Window ID */
GC		gc;		/* GC to draw with */
XGCValues	gcv;
XSizeHints	xsh;		/* Size hints for window manager */
Colormap	cmap;

static unsigned long	planemask [6];	/* plane masks from XAllocColorCells */
static unsigned long   pixels [16];	/* pixel values from XAllocColorCells */
GC		curGC;

main(argc,argv)
    int argc;
    char **argv;
{
    char       *fontName;	/* Name of font for string */
    XFontStruct *fontstruct;	/* Font descriptor */
    unsigned long ftw, fth, pad;/* Font size parameters */
    unsigned long bg, bd;	/* Pixel values */
    unsigned long bw;		/* Border width */
    char       *tempstr;	/* Temporary string */
    XColor      color;		/* Temporary color */
    char       *geomSpec = NULL;/* Window geometry string */
    XSetWindowAttributes xswa;	/* Temporary Set Window Attribute struct */
    char	**c;

/*
 *  Parse command line
 */

    for (c= &argv[1]; *c != (char *) NULL; ++c)
	if (**c == '-') {
	    for (swp= &switches[0]; swp->value != 0; ++swp)
		if (strcmp (swp->sw, *c) == 0) {
			switch (swp->value) {
			case SW_GEOM: 
				geomSpec = *(++c);
				break;
			case SW_BG:
				background = *(++c);
				break;
			case SW_BORDER:
				borderWidth = atoi (*(++c));
				break;
			}
		        break;
		}
	    if (swp->value == 0) {
		fprintf (stderr, "%s: invalid switch %s", argv[0], *c);
		exit (1);
	    }
	}
	else
	    filename = *c;

    /*
     * Open the display using the $DISPLAY environment variable to locate
     * the X server.  See Section 2.1.
     */
    if ((dpy = XOpenDisplay(NULL)) == NULL) {
	fprintf(stderr, "%s: can't open %s\n", argv[0], XDisplayName(NULL));
	exit(1);
    }

    mono = (DisplayPlanes(dpy, 0) < 2) ? 1 : 0;

    /*
     * Load the font to use.  See Sections 10.2 & 6.5.1
     */
    if ((fontName = XGetDefault(dpy, argv[0], ARG_FONT)) == NULL) {
	fontName = FONT;
    }
    if ((fontstruct = XLoadQueryFont(dpy, fontName)) == NULL) {
	fprintf(stderr, "%s: display %s doesn't know font %s\n",
		argv[0], DisplayString(dpy), fontName);
	exit(1);
    }
    fth = fontstruct->max_bounds.ascent + fontstruct->max_bounds.descent;
    ftw = fontstruct->max_bounds.width;

    /*
     * Select colors for the border,  the window background,  and the
     * foreground.  We use the default colormap to allocate the colors in.
     * See Sections 2.2.1, 5.1.2, & 10.4.
     */

    cmap = DefaultColormap(dpy, DefaultScreen(dpy));

    if ((tempstr = XGetDefault(dpy, argv[0], ARG_BORDER_COLOR)) == NULL)
	tempstr = DEFAULT_BORDER;
    if (XParseColor(dpy, cmap, tempstr, &color) == 0) {
	fprintf (stderr, "Can't get border color %s\n", tempstr);
	exit (1);
    }
	bd = color.pixel;

/*
 *  Allocate Color Planes and one extra cell for the border color.
 */

    if (mono == 0)
    if (XAllocColorCells (dpy, cmap, False, planemask, 0, pixels, 1) == 0) {
	fprintf (stderr, "Cannot allocate color cells\n");
	exit (1);
    }
    bg = WhitePixel (dpy, 0);

/*
 *  Store border color
 */

    if (mono)
	bd = BlackPixel (dpy, 0);
    else {
    	bd = color.pixel = pixels [0];
    	XStoreColor (dpy, cmap, &color);
    }

/*
 *  Background Color (pixels [0] defines this).
 */

    if (background == NULL)
	background = XGetDefault (dpy, argv[0], ARG_BACKGROUND);
    if (background == NULL)
	background = DEFAULT_BACKGROUND;

    /*
     * Set the border width of the window,  and the gap between the text
     * and the edge of the window, "pad".
     */
    pad = BORDER;
    if ((tempstr = XGetDefault(dpy, argv[0], ARG_BORDER)) == NULL)
	bw = 1;
    else
	bw = atoi(tempstr);

    /*
     * Deal with providing the window with an initial position & size.
     * Fill out the XSizeHints struct to inform the window manager. See
     * Sections 9.1.6 & 10.3.
     */
    if (geomSpec == NULL)
        geomSpec = XGetDefault(dpy, argv[0], ARG_GEOMETRY);

    if (geomSpec == NULL) {
	/*
	 * The defaults database doesn't contain a specification of the
	 * initial size & position - fit the window to the text and locate
	 * it in the center of the screen.
	 */
	xsh.flags = (PPosition | PSize);
	xsh.height = 499;
	xsh.width = 1200;
	xsh.x = (DisplayWidth(dpy, DefaultScreen(dpy)) - xsh.width) / 2;
	xsh.y = (DisplayHeight(dpy, DefaultScreen(dpy)) - xsh.height) / 2;
    }
    else {
	int         bitmask;

	bzero(&xsh, sizeof(xsh));
	bitmask = XGeometry(dpy, DefaultScreen(dpy), geomSpec, geomSpec,
			    bw, ftw, fth, pad, pad, &(xsh.x), &(xsh.y),
			    &(xsh.width), &(xsh.height));
	if (bitmask & (XValue | YValue)) {
	    xsh.flags |= USPosition;
	}
	if (bitmask & (WidthValue | HeightValue)) {
	    xsh.flags |= USSize;
	}
    }

    /*
     * Create the Window with the information in the XSizeHints, the
     * border width,  and the border & background pixels. See Section 3.3.
     */
    win = XCreateSimpleWindow(dpy, DefaultRootWindow(dpy),
			      xsh.x, xsh.y, xsh.width, xsh.height,
			      bw, bd, bg);

    /*
     * Set the standard properties for the window managers. See Section
     * 9.1.
     */
    XSetStandardProperties(dpy, win, STRING, STRING, None, argv, argc, &xsh);
    XSetWMHints(dpy, win, &xwmh);

    /*
     * Ensure that the window's colormap field points to the default
     * colormap,  so that the window manager knows the correct colormap to
     * use for the window.  See Section 3.2.9. Also,  set the window's Bit
     * Gravity to reduce Expose events.
     */
    xswa.colormap = DefaultColormap(dpy, DefaultScreen(dpy));
    xswa.bit_gravity = NorthWestGravity;
    XChangeWindowAttributes(dpy, win, (CWColormap | CWBitGravity), &xswa);

    /*
     * Create the GC for drawing the picture.
     */
    gcv.font = fontstruct->fid;
    curGC = XCreateGC(dpy, win, GCFont, &gcv);

    /*
     * Specify the event types we're interested in - only Exposures.  See
     * Sections 8.5 & 8.4.5.1
     */
    XSelectInput(dpy, win, ExposureMask);

    /*
     * Map the window to make it visible.  See Section 3.5.
     */
    XMapWindow(dpy, win);

    app(background);

    exit(1);
}