1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
9 #include <X11/keysym.h>
10 #include <X11/cursorfont.h>
12 #include <sys/types.h>
21 #include "../libgame/platform.h"
52 KeyCode northKeyCode[3];
53 KeyCode eastKeyCode[3];
54 KeyCode southKeyCode[3];
55 KeyCode westKeyCode[3];
56 KeyCode fireKeyCode[3];
57 KeyCode escKeyCode[1];
59 char play[SAMPLE_MAX];
61 static int sound_pid = -1;
62 int sound_pipe[2] = { -1, -1 }; /* for communication */
63 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
64 long sound_length[SAMPLE_MAX]; /* length of sound data */
66 static Screen *defaultScreen;
67 static Visual *defaultVisual;
68 static Colormap defaultColourmap;
69 static Window defaultRootWindow;
70 static unsigned int screenDepth;
71 static unsigned int screenWidth;
72 static unsigned int screenHeight;
73 static unsigned long screenBlackPixel;
74 static unsigned long screenWhitePixel;
76 static XSizeHints sizeHints;
77 static XSetWindowAttributes setWindowAttributes;
78 static XWMHints wmHints;
79 static XVisualInfo visualInfo;
80 static XGCValues gcValues;
82 static Colormap privateColourmap;
84 static XColor *privateColours;
85 static unsigned char *privateFlags;
86 static int privateNumColours;
88 static XColor redColour;
89 static XColor whiteColour;
93 static Pixmap xpmPixmaps[4];
94 static Pixmap xpmBitmaps[4];
95 static XpmAttributes xpmAttributes[4];
98 static int xpmAllocColourFunc(Display *, Colormap, char *, XColor *, void *);
99 static int xpmFreeColoursFunc(Display *, Colormap, unsigned long *, int, void *);
101 static KeyCode keycodes[16];
103 static const char *xpmNames[4] = { "object.xpm", "score.xpm", "sprite.xpm", "title.xpm" };
104 static const int xpmCloseness[4] = { 10000, 10000, 40000, 50000 };
105 static const KeySym keysyms[16] = {
106 XK_Up, XK_KP_Up, XK_r, /* north */
107 XK_Right, XK_KP_Right, XK_g, /* east */
108 XK_Down, XK_KP_Down, XK_f, /* south */
109 XK_Left, XK_KP_Left, XK_d, /* west */
110 XK_Shift_L, XK_Control_R, XK_space, /* fire */
111 XK_Escape /* escape */
113 static const char *sound_names[SAMPLE_MAX] = {
114 "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
115 "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
116 "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
117 "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
118 "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
119 "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
122 static const int sound_volume[SAMPLE_MAX] = {
123 20,100,100,100,100,20,20,100,100,100,
124 50,100,100,100,100,100,100,100,100,100,
125 100,20,100,100,100,100,100,20,100,100,
131 char name[MAXNAME+2];
136 display = XOpenDisplay(arg_display);
138 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to open display", strerror(errno));
142 defaultScreen = DefaultScreenOfDisplay(display);
143 defaultVisual = DefaultVisualOfScreen(defaultScreen);
144 defaultColourmap = DefaultColormapOfScreen(defaultScreen);
145 defaultRootWindow = RootWindowOfScreen(defaultScreen);
146 screenDepth = DefaultDepthOfScreen(defaultScreen);
147 screenWidth = WidthOfScreen(defaultScreen);
148 screenHeight = HeightOfScreen(defaultScreen);
149 screenBlackPixel = BlackPixelOfScreen(defaultScreen);
150 screenWhitePixel = WhitePixelOfScreen(defaultScreen);
153 visualInfo.visualid = XVisualIDFromVisual(defaultVisual);
154 dummyptr = XGetVisualInfo(display, VisualIDMask, &visualInfo, &dummyint);
156 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to get visual info", strerror(errno));
159 memcpy(&visualInfo, dummyptr, sizeof(visualInfo));
162 if(visualInfo.class != PseudoColor) {
163 fprintf(stderr, "%s: \"%s\": %s\n", progname, XDisplayName(arg_display), "private colourmap only supported for pseudocolour display");
167 privateColourmap = XCreateColormap(display, defaultRootWindow, defaultVisual, AllocAll);
168 if(privateColourmap == 0) {
169 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create colourmap", strerror(errno));
173 privateNumColours = visualInfo.colormap_size;
175 privateColours = malloc(privateNumColours * sizeof(XColor));
176 if(privateColours == 0) {
177 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed", privateNumColours * sizeof(XColor), strerror(errno));
180 for(dummyint = 0; dummyint < privateNumColours; dummyint++) privateColours[dummyint].pixel = dummyint;
181 XQueryColors(display, defaultColourmap, privateColours, privateNumColours);
182 XStoreColors(display, privateColourmap, privateColours, privateNumColours);
184 privateFlags = malloc(privateNumColours);
185 if(privateFlags == 0) {
186 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed", privateNumColours, strerror(errno));
189 memset(privateFlags, 0, privateNumColours);
190 privateFlags[0] = 1; /* first two entries (black and white) are already allocated */
194 sizeHints.flags = PSize | PMinSize | PMaxSize;
195 sizeHints.width = 20 * TILEX;
196 sizeHints.height = 12 * TILEY + SCOREY;
197 sizeHints.min_width = sizeHints.max_width = sizeHints.width;
198 sizeHints.min_height = sizeHints.max_height = sizeHints.height;
200 dummyint = XWMGeometry(display, XScreenNumberOfScreen(defaultScreen), arg_geometry, 0, 2, &sizeHints, &sizeHints.x, &sizeHints.y, &dummyint, &dummyint, &sizeHints.win_gravity);
201 if(dummyint & (XValue | YValue)) sizeHints.flags |= USPosition | PWinGravity;
204 xwindow = XCreateWindow(display, defaultRootWindow, sizeHints.x, sizeHints.y, sizeHints.width, sizeHints.height, 2, screenDepth, InputOutput, CopyFromParent, 0, 0);
206 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to open window", strerror(errno));
210 setWindowAttributes.background_pixel = screenBlackPixel;
211 setWindowAttributes.border_pixel = screenWhitePixel;
212 setWindowAttributes.backing_store = NotUseful;
213 setWindowAttributes.override_redirect = False;
214 setWindowAttributes.event_mask = KeyPressMask | EnterWindowMask | LeaveWindowMask | ExposureMask;
215 setWindowAttributes.colormap = privateColourmap ? privateColourmap : defaultColourmap;
216 XChangeWindowAttributes(display, xwindow, CWBackPixel | CWBorderPixel | CWBackingStore | CWOverrideRedirect | CWEventMask | CWColormap, &setWindowAttributes);
218 XStoreName(display, xwindow, "Emerald Mine");
220 wmHints.flags = InputHint | StateHint;
221 wmHints.input = True;
222 wmHints.initial_state = NormalState;
223 XSetWMHints(display, xwindow, &wmHints);
225 XSetWMNormalHints(display, xwindow, &sizeHints);
227 deleteAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
228 XSetWMProtocols(display, xwindow, &deleteAtom, 1);
230 cursor = XCreateFontCursor(display, XC_trek);
231 if(cursor) XDefineCursor(display, xwindow, cursor);
233 XMapWindow(display, xwindow);
235 for(i = 0; i < 4; i++) {
238 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_GFX_DIR, xpmNames[i]);
240 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, xpmNames[i]);
242 if(name[MAXNAME]) snprintf_overflow("read graphics/ files");
244 xpmAttributes[i].valuemask = XpmColormap | XpmReturnAllocPixels | XpmExactColors | XpmCloseness | XpmAllocColor | XpmFreeColors;
245 xpmAttributes[i].colormap = privateColourmap ? privateColourmap : defaultColourmap;
246 xpmAttributes[i].exactColors = False;
247 xpmAttributes[i].closeness = xpmCloseness[i];
248 xpmAttributes[i].alloc_color = xpmAllocColourFunc;
249 xpmAttributes[i].free_colors = xpmFreeColoursFunc;
250 dummyint = XpmReadFileToPixmap(display, xwindow, name, &xpmPixmaps[i], &xpmBitmaps[i], &xpmAttributes[i]);
252 fprintf(stderr, "%s: \"%s\": \"%s\": %s: %s: %s\n", progname, XDisplayName(arg_display), name, "failed to read xpm", XpmGetErrorString(dummyint), strerror(errno));
258 objPixmap = xpmPixmaps[0];
259 botPixmap = xpmPixmaps[1];
260 sprPixmap = xpmPixmaps[2];
261 ttlPixmap = xpmPixmaps[3];
262 objmaskBitmap = xpmBitmaps[0];
263 botmaskBitmap = xpmBitmaps[1];
264 sprmaskBitmap = xpmBitmaps[2];
265 ttlmaskBitmap = xpmBitmaps[3];
267 screenPixmap = XCreatePixmap(display, xwindow, 22 * TILEX, 14 * TILEY, screenDepth);
268 if(screenPixmap == 0) {
269 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
273 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY, screenDepth);
274 if(scorePixmap == 0) {
275 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
279 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
280 if(spriteBitmap == 0) {
281 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
285 redColour.pixel = screenWhitePixel;
286 whiteColour.pixel = screenBlackPixel;
287 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap : defaultColourmap, "red", &redColour, 0) > 0);
288 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap : defaultColourmap, "white", &whiteColour, 0) > 0);
290 gcValues.graphics_exposures = False;
291 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
293 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
297 gcValues.graphics_exposures = False;
298 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
300 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
304 gcValues.function = objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
305 gcValues.graphics_exposures = False;
306 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures, &gcValues);
308 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
312 gcValues.foreground = redColour.pixel;
313 gcValues.background = whiteColour.pixel;
314 gcValues.line_style = LineDoubleDash;
315 gcValues.graphics_exposures = False;
316 antsGC = XCreateGC(display, screenPixmap, GCForeground | GCBackground | GCLineStyle | GCGraphicsExposures, &gcValues);
318 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
322 for(i = 0; i < 16; i++) {
323 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
325 for(i = 0; i < 3; i++) northKeyCode[i] = keycodes[i + 0];
326 for(i = 0; i < 3; i++) eastKeyCode[i] = keycodes[i + 3];
327 for(i = 0; i < 3; i++) southKeyCode[i] = keycodes[i + 6];
328 for(i = 0; i < 3; i++) westKeyCode[i] = keycodes[i + 9];
329 for(i = 0; i < 3; i++) fireKeyCode[i] = keycodes[i + 12];
330 for(i = 0; i < 1; i++) escKeyCode[i] = keycodes[i + 15];
332 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
333 if(arg_silence == 0) {
334 for(i = 0; i < SAMPLE_MAX; i++) {
337 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR, sound_names[i]);
339 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
341 if(name[MAXNAME]) snprintf_overflow("read sounds/ directory");
343 if(read_sample(name, &sound_data[i], &sound_length[i])) return(1);
347 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
348 stop = sound_data[i] + sound_length[i];
349 for(ptr = sound_data[i]; ptr < stop; ptr++) *ptr = (*ptr * mult) / 65536;
353 if(pipe(sound_pipe) == -1) {
354 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe", strerror(errno));
358 if(sound_pid == -1) {
359 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread", strerror(errno));
362 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
363 if(sound_pid == 0) _exit(sound_thread());
364 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
366 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
375 if(sound_pid != -1) {
376 kill(sound_pid, SIGTERM);
377 waitpid(sound_pid, 0, 0);
379 if(sound_pipe[0] != -1) close(sound_pipe[0]);
380 if(sound_pipe[1] != -1) close(sound_pipe[1]);
381 for(i = 0; i < SAMPLE_MAX; i++) if(sound_data[i]) free(sound_data[i]);
383 for(i = 0; i < 4; i++) if(xpmPixmaps[i]) XFreePixmap(display, xpmPixmaps[i]);
384 for(i = 0; i < 4; i++) if(xpmBitmaps[i]) XFreePixmap(display, xpmBitmaps[i]);
385 for(i = 0; i < 4; i++) if(xpmGot[i]) {
386 xpmFreeColoursFunc(display, xpmAttributes[i].colormap, xpmAttributes[i].alloc_pixels, xpmAttributes[i].nalloc_pixels, 0);
387 XpmFreeAttributes(&xpmAttributes[i]);
389 if(gotRed) xpmFreeColoursFunc(display, privateColourmap ? privateColourmap : defaultColourmap, &redColour.pixel, 1, 0);
390 if(gotWhite) xpmFreeColoursFunc(display, privateColourmap ? privateColourmap : defaultColourmap, &whiteColour.pixel, 1, 0);
391 if(screenGC) XFreeGC(display, screenGC);
392 if(scoreGC) XFreeGC(display, scoreGC);
393 if(spriteGC) XFreeGC(display, spriteGC);
394 if(antsGC) XFreeGC(display, antsGC);
395 if(screenPixmap) XFreePixmap(display, screenPixmap);
396 if(scorePixmap) XFreePixmap(display, scorePixmap);
397 if(spriteBitmap) XFreePixmap(display, spriteBitmap);
398 if(xwindow) XDestroyWindow(display, xwindow);
399 if(cursor) XFreeCursor(display, cursor);
400 if(privateColourmap) XFreeColormap(display, privateColourmap);
401 if(privateColours) free(privateColours);
402 if(privateFlags) free(privateFlags);
403 if(display) XCloseDisplay(display);
406 /* ---------------------------------------------------------------------- */
408 void sound_play(void)
410 if(sound_pipe[1] != -1) {
411 if(write(sound_pipe[1], &play, sizeof(play)) == -1) {
412 fprintf(stderr, "%s: %s: %s\n", progname, "write sound", strerror(errno));
413 if(sound_pipe[0] != -1) { close(sound_pipe[0]); sound_pipe[0] = -1; }
414 if(sound_pipe[1] != -1) { close(sound_pipe[1]); sound_pipe[1] = -1; }
417 memset(play, 0, sizeof(play));
420 /* ---------------------------------------------------------------------- */
422 static int xpmAllocColourFunc(Display *display, Colormap colourmap, char *colourname, XColor *xcolour, void *closure)
428 if(colourname) if(XParseColor(display, colourmap, colourname, xcolour) == 0) return(-1); /* invalid name */
429 if(colourmap != privateColourmap) return(XAllocColor(display, colourmap, xcolour) != 0);
431 /* first try to find an exact match */
433 for(i = 0; i < privateNumColours; i++) {
434 if(privateColours[i].red == xcolour->red && privateColours[i].green == xcolour->green && privateColours[i].blue == xcolour->blue) match = i;
437 privateFlags[match] = 1;
438 xcolour->pixel = privateColours[match].pixel;
442 /* then find an unallocated colour that is close to what we want */
445 for(i = 0; i < privateNumColours; i++) {
446 if(privateFlags[i]) continue; /* skip if it is already allocated */
447 r = (privateColours[i].red - xcolour->red) / 256;
448 g = (privateColours[i].green - xcolour->green) / 256;
449 b = (privateColours[i].blue - xcolour->blue) / 256;
450 sum = r * r + g * g + b * b;
457 privateFlags[match] = 1;
458 privateColours[match].red = xcolour->red;
459 privateColours[match].green = xcolour->green;
460 privateColours[match].blue = xcolour->blue;
461 XStoreColor(display, colourmap, &privateColours[match]);
462 xcolour->pixel = privateColours[match].pixel;
463 return(1); /* found a close match */
466 /* if all else fails, just find the closest colour and return it */
469 for(i = 0; i < privateNumColours; i++) {
470 r = (privateColours[i].red - xcolour->red) / 256;
471 g = (privateColours[i].green - xcolour->green) / 256;
472 b = (privateColours[i].blue - xcolour->blue) / 256;
473 sum = r * r + g * g + b * b;
480 xcolour->red = privateColours[match].red;
481 xcolour->green = privateColours[match].green;
482 xcolour->blue = privateColours[match].blue;
483 xcolour->pixel = privateColours[match].pixel;
484 return(1); /* best we could do */
486 return(0); /* still didnt find one, give up */
489 static int xpmFreeColoursFunc(Display *display, Colormap colourmap, unsigned long *pixels, int npixels, void *closure)
491 if(colourmap != privateColourmap) XFreeColors(display, colourmap, pixels, npixels, 0);
492 return(1); /* non-zero for success */