2 #if defined(TARGET_X11)
4 /* 2000-08-10T18:03:54Z
6 * open X11 display and sound
11 #include <X11/Xutil.h>
12 #include <X11/Xatom.h>
14 #include <X11/Intrinsic.h>
15 #include <X11/keysymdef.h>
19 #include <X11/Xutil.h>
20 #include <X11/Xatom.h>
21 #include <X11/keysym.h>
22 #include <X11/cursorfont.h>
24 #include <sys/types.h>
33 #include "../libgame/platform.h"
78 KeyCode northKeyCode[3];
79 KeyCode eastKeyCode[3];
80 KeyCode southKeyCode[3];
81 KeyCode westKeyCode[3];
82 KeyCode fireKeyCode[3];
83 KeyCode escKeyCode[1];
85 char play[SAMPLE_MAX];
87 static int sound_pid = -1;
88 int sound_pipe[2] = { -1, -1 }; /* for communication */
89 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
90 long sound_length[SAMPLE_MAX]; /* length of sound data */
92 static Screen *defaultScreen;
93 static Visual *defaultVisual;
94 static Colormap defaultColourmap;
95 static Window defaultRootWindow;
96 static unsigned int screenDepth;
97 static unsigned int screenWidth;
98 static unsigned int screenHeight;
99 static unsigned long screenBlackPixel;
100 static unsigned long screenWhitePixel;
103 static XSizeHints sizeHints;
104 static XSetWindowAttributes setWindowAttributes;
105 static XWMHints wmHints;
106 static XVisualInfo visualInfo;
108 static XGCValues gcValues;
110 static Colormap privateColourmap;
111 static Cursor cursor;
112 static XColor *privateColours;
113 static unsigned char *privateFlags;
114 static int privateNumColours;
116 static XColor redColour;
117 static XColor whiteColour;
122 static Bitmap *pcxBitmaps[4];
123 static Bitmap *pcxBitmapsX2[4];
127 static Pixmap xpmPixmaps[4];
128 static Pixmap xpmBitmaps[4];
129 static XpmAttributes xpmAttributes[4];
130 static int xpmGot[4];
133 static int xpmAllocColourFunc(Display *, Colormap, char *, XColor *, void *);
134 static int xpmFreeColoursFunc(Display *, Colormap, unsigned long *, int, void *);
136 static KeyCode keycodes[16];
139 static const char *xpmNames[4] = { "object.xpm", "score.xpm", "sprite.xpm", "title.xpm" };
143 static const char *pcxNames[4] = { "object.pcx", "score.pcx", "sprite.pcx", "title.pcx" };
146 static const int xpmCloseness[4] = { 10000, 10000, 40000, 50000 };
147 static const KeySym keysyms[16] = {
148 XK_Up, XK_KP_Up, XK_r, /* north */
149 XK_Right, XK_KP_Right, XK_g, /* east */
150 XK_Down, XK_KP_Down, XK_f, /* south */
151 XK_Left, XK_KP_Left, XK_d, /* west */
152 XK_Shift_L, XK_Control_R, XK_space, /* fire */
153 XK_Escape /* escape */
155 static const char *sound_names[SAMPLE_MAX] = {
156 "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
157 "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
158 "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
159 "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
160 "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
161 "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
164 static const int sound_volume[SAMPLE_MAX] = {
165 20,100,100,100,100,20,20,100,100,100,
166 50,100,100,100,100,100,100,100,100,100,
167 100,20,100,100,100,100,100,20,100,100,
173 char name[MAXNAME+2];
181 display = XOpenDisplay(arg_display);
183 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to open display", strerror(errno));
188 defaultScreen = DefaultScreenOfDisplay(display);
189 defaultVisual = DefaultVisualOfScreen(defaultScreen);
190 defaultColourmap = DefaultColormapOfScreen(defaultScreen);
191 defaultRootWindow = RootWindowOfScreen(defaultScreen);
192 screenDepth = DefaultDepthOfScreen(defaultScreen);
193 screenWidth = WidthOfScreen(defaultScreen);
194 screenHeight = HeightOfScreen(defaultScreen);
195 screenBlackPixel = BlackPixelOfScreen(defaultScreen);
196 screenWhitePixel = WhitePixelOfScreen(defaultScreen);
200 visualInfo.visualid = XVisualIDFromVisual(defaultVisual);
201 dummyptr = XGetVisualInfo(display, VisualIDMask, &visualInfo, &dummyint);
203 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to get visual info", strerror(errno));
206 memcpy(&visualInfo, dummyptr, sizeof(visualInfo));
209 if(visualInfo.class != PseudoColor) {
210 fprintf(stderr, "%s: \"%s\": %s\n", progname, XDisplayName(arg_display), "private colourmap only supported for pseudocolour display");
214 privateColourmap = XCreateColormap(display, defaultRootWindow, defaultVisual, AllocAll);
215 if(privateColourmap == 0) {
216 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create colourmap", strerror(errno));
220 privateNumColours = visualInfo.colormap_size;
222 privateColours = malloc(privateNumColours * sizeof(XColor));
223 if(privateColours == 0) {
224 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed", privateNumColours * sizeof(XColor), strerror(errno));
227 for(dummyint = 0; dummyint < privateNumColours; dummyint++) privateColours[dummyint].pixel = dummyint;
228 XQueryColors(display, defaultColourmap, privateColours, privateNumColours);
229 XStoreColors(display, privateColourmap, privateColours, privateNumColours);
231 privateFlags = malloc(privateNumColours);
232 if(privateFlags == 0) {
233 fprintf(stderr, "%s: %s (%d): %s\n", progname, "malloc failed", privateNumColours, strerror(errno));
236 memset(privateFlags, 0, privateNumColours);
237 privateFlags[0] = 1; /* first two entries (black and white) are already allocated */
241 sizeHints.flags = PSize | PMinSize | PMaxSize;
242 sizeHints.width = 20 * TILEX;
243 sizeHints.height = 12 * TILEY + SCOREY;
244 sizeHints.min_width = sizeHints.max_width = sizeHints.width;
245 sizeHints.min_height = sizeHints.max_height = sizeHints.height;
247 dummyint = XWMGeometry(display, XScreenNumberOfScreen(defaultScreen), arg_geometry, 0, 2, &sizeHints, &sizeHints.x, &sizeHints.y, &dummyint, &dummyint, &sizeHints.win_gravity);
248 if(dummyint & (XValue | YValue)) sizeHints.flags |= USPosition | PWinGravity;
251 xwindow = XCreateWindow(display, defaultRootWindow, sizeHints.x, sizeHints.y, sizeHints.width, sizeHints.height, 2, screenDepth, InputOutput, CopyFromParent, 0, 0);
253 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to open window", strerror(errno));
257 setWindowAttributes.background_pixel = screenBlackPixel;
258 setWindowAttributes.border_pixel = screenWhitePixel;
259 setWindowAttributes.backing_store = NotUseful;
260 setWindowAttributes.override_redirect = False;
261 setWindowAttributes.event_mask = KeyPressMask | EnterWindowMask | LeaveWindowMask | ExposureMask;
262 setWindowAttributes.colormap = privateColourmap ? privateColourmap : defaultColourmap;
263 XChangeWindowAttributes(display, xwindow, CWBackPixel | CWBorderPixel | CWBackingStore | CWOverrideRedirect | CWEventMask | CWColormap, &setWindowAttributes);
265 XStoreName(display, xwindow, "Emerald Mine");
267 wmHints.flags = InputHint | StateHint;
268 wmHints.input = True;
269 wmHints.initial_state = NormalState;
270 XSetWMHints(display, xwindow, &wmHints);
272 XSetWMNormalHints(display, xwindow, &sizeHints);
274 deleteAtom = XInternAtom(display, "WM_DELETE_WINDOW", False);
275 XSetWMProtocols(display, xwindow, &deleteAtom, 1);
277 cursor = XCreateFontCursor(display, XC_trek);
278 if(cursor) XDefineCursor(display, xwindow, cursor);
280 XMapWindow(display, xwindow);
284 for(i = 0; i < 4; i++) {
287 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_GFX_DIR, xpmNames[i]);
289 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, xpmNames[i]);
291 if(name[MAXNAME]) snprintf_overflow("read graphics/ files");
293 xpmAttributes[i].valuemask = XpmColormap | XpmReturnAllocPixels | XpmExactColors | XpmCloseness | XpmAllocColor | XpmFreeColors;
294 xpmAttributes[i].colormap = privateColourmap ? privateColourmap : defaultColourmap;
295 xpmAttributes[i].exactColors = False;
296 xpmAttributes[i].closeness = xpmCloseness[i];
297 xpmAttributes[i].alloc_color = xpmAllocColourFunc;
298 xpmAttributes[i].free_colors = xpmFreeColoursFunc;
299 dummyint = XpmReadFileToPixmap(display, xwindow, name, &xpmPixmaps[i], &xpmBitmaps[i], &xpmAttributes[i]);
301 fprintf(stderr, "%s: \"%s\": \"%s\": %s: %s: %s\n", progname, XDisplayName(arg_display), name, "failed to read xpm", XpmGetErrorString(dummyint), strerror(errno));
308 for(i = 0; i < 4; i++)
311 snprintf(name, MAXNAME+2, "%s/%s", EM_GFX_DIR, pcxNames[i]);
314 snprintf_overflow("read graphics/ files");
316 if ((pcxBitmaps[i] = LoadImage(name)) == NULL)
318 printf("::: LoadImage() failed for file '%s'\n", name);
323 objBitmap = pcxBitmaps[0];
324 botBitmap = pcxBitmaps[1];
325 sprBitmap = pcxBitmaps[2];
326 ttlBitmap = pcxBitmaps[3];
329 objPixmap = xpmPixmaps[0];
330 botPixmap = xpmPixmaps[1];
331 sprPixmap = xpmPixmaps[2];
332 ttlPixmap = xpmPixmaps[3];
333 objmaskBitmap = xpmBitmaps[0];
334 botmaskBitmap = xpmBitmaps[1];
335 sprmaskBitmap = xpmBitmaps[2];
336 ttlmaskBitmap = xpmBitmaps[3];
338 objPixmap = pcxBitmaps[0]->drawable;
339 botPixmap = pcxBitmaps[1]->drawable;
340 sprPixmap = pcxBitmaps[2]->drawable;
341 ttlPixmap = pcxBitmaps[3]->drawable;
342 objmaskBitmap = pcxBitmaps[0]->clip_mask;
343 botmaskBitmap = pcxBitmaps[1]->clip_mask;
344 sprmaskBitmap = pcxBitmaps[2]->clip_mask;
345 ttlmaskBitmap = pcxBitmaps[3]->clip_mask;
349 for (i = 0; i < 4; i++)
350 pcxBitmapsX2[i] = ZoomBitmap(pcxBitmaps[i],
351 pcxBitmaps[i]->width * 2,
352 pcxBitmaps[i]->height * 2);
354 objBitmap = pcxBitmapsX2[0];
355 botBitmap = pcxBitmapsX2[1];
356 sprBitmap = pcxBitmapsX2[2];
357 ttlBitmap = pcxBitmapsX2[3];
359 objPixmap = pcxBitmapsX2[0]->drawable;
360 botPixmap = pcxBitmapsX2[1]->drawable;
361 sprPixmap = pcxBitmapsX2[2]->drawable;
362 ttlPixmap = pcxBitmapsX2[3]->drawable;
363 objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
364 botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
365 sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
366 ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
370 screenBitmap = CreateBitmap(22 * TILEX, 14 * TILEY, DEFAULT_DEPTH);
371 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
374 screenPixmap = XCreatePixmap(display, xwindow, 22 * TILEX, 14 * TILEY, screenDepth);
375 if(screenPixmap == 0) {
376 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
380 scorePixmap = XCreatePixmap(display, xwindow, 20 * TILEX, SCOREY, screenDepth);
381 if(scorePixmap == 0) {
382 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
386 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
387 if(spriteBitmap == 0) {
388 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create pixmap", strerror(errno));
392 redColour.pixel = screenWhitePixel;
393 whiteColour.pixel = screenBlackPixel;
394 gotRed = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap : defaultColourmap, "red", &redColour, 0) > 0);
395 gotWhite = (xpmAllocColourFunc(display, privateColourmap ? privateColourmap : defaultColourmap, "white", &whiteColour, 0) > 0);
397 gcValues.graphics_exposures = False;
398 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
400 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
404 gcValues.graphics_exposures = False;
405 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
407 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
411 gcValues.function = objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
412 gcValues.graphics_exposures = False;
413 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures, &gcValues);
415 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
419 gcValues.foreground = redColour.pixel;
420 gcValues.background = whiteColour.pixel;
421 gcValues.line_style = LineDoubleDash;
422 gcValues.graphics_exposures = False;
423 antsGC = XCreateGC(display, screenPixmap, GCForeground | GCBackground | GCLineStyle | GCGraphicsExposures, &gcValues);
425 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname, XDisplayName(arg_display), "failed to create graphics context", strerror(errno));
429 for(i = 0; i < 16; i++) {
430 keycodes[i] = XKeysymToKeycode(display, keysyms[i]);
432 for(i = 0; i < 3; i++) northKeyCode[i] = keycodes[i + 0];
433 for(i = 0; i < 3; i++) eastKeyCode[i] = keycodes[i + 3];
434 for(i = 0; i < 3; i++) southKeyCode[i] = keycodes[i + 6];
435 for(i = 0; i < 3; i++) westKeyCode[i] = keycodes[i + 9];
436 for(i = 0; i < 3; i++) fireKeyCode[i] = keycodes[i + 12];
437 for(i = 0; i < 1; i++) escKeyCode[i] = keycodes[i + 15];
439 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
440 if(arg_silence == 0) {
441 for(i = 0; i < SAMPLE_MAX; i++) {
444 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR, sound_names[i]);
446 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
448 if(name[MAXNAME]) snprintf_overflow("read sounds/ directory");
450 if(read_sample(name, &sound_data[i], &sound_length[i])) return(1);
454 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
455 stop = sound_data[i] + sound_length[i];
456 for(ptr = sound_data[i]; ptr < stop; ptr++) *ptr = (*ptr * mult) / 65536;
460 if(pipe(sound_pipe) == -1) {
461 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe", strerror(errno));
465 if(sound_pid == -1) {
466 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread", strerror(errno));
469 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
470 if(sound_pid == 0) _exit(sound_thread());
471 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
473 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
482 if(sound_pid != -1) {
483 kill(sound_pid, SIGTERM);
484 waitpid(sound_pid, 0, 0);
486 if(sound_pipe[0] != -1) close(sound_pipe[0]);
487 if(sound_pipe[1] != -1) close(sound_pipe[1]);
488 for(i = 0; i < SAMPLE_MAX; i++) if(sound_data[i]) free(sound_data[i]);
491 for(i = 0; i < 4; i++) if(xpmPixmaps[i]) XFreePixmap(display, xpmPixmaps[i]);
492 for(i = 0; i < 4; i++) if(xpmBitmaps[i]) XFreePixmap(display, xpmBitmaps[i]);
493 for(i = 0; i < 4; i++) if(xpmGot[i]) {
494 xpmFreeColoursFunc(display, xpmAttributes[i].colormap, xpmAttributes[i].alloc_pixels, xpmAttributes[i].nalloc_pixels, 0);
495 XpmFreeAttributes(&xpmAttributes[i]);
499 if(gotRed) xpmFreeColoursFunc(display, privateColourmap ? privateColourmap : defaultColourmap, &redColour.pixel, 1, 0);
500 if(gotWhite) xpmFreeColoursFunc(display, privateColourmap ? privateColourmap : defaultColourmap, &whiteColour.pixel, 1, 0);
502 if(screenGC) XFreeGC(display, screenGC);
503 if(scoreGC) XFreeGC(display, scoreGC);
504 if(spriteGC) XFreeGC(display, spriteGC);
505 if(antsGC) XFreeGC(display, antsGC);
506 if(screenPixmap) XFreePixmap(display, screenPixmap);
507 if(scorePixmap) XFreePixmap(display, scorePixmap);
508 if(spriteBitmap) XFreePixmap(display, spriteBitmap);
509 if(xwindow) XDestroyWindow(display, xwindow);
510 if(cursor) XFreeCursor(display, cursor);
511 if(privateColourmap) XFreeColormap(display, privateColourmap);
512 if(privateColours) free(privateColours);
513 if(privateFlags) free(privateFlags);
514 if(display) XCloseDisplay(display);
517 /* ---------------------------------------------------------------------- */
519 void sound_play(void)
521 if(sound_pipe[1] != -1) {
522 if(write(sound_pipe[1], &play, sizeof(play)) == -1) {
523 fprintf(stderr, "%s: %s: %s\n", progname, "write sound", strerror(errno));
524 if(sound_pipe[0] != -1) { close(sound_pipe[0]); sound_pipe[0] = -1; }
525 if(sound_pipe[1] != -1) { close(sound_pipe[1]); sound_pipe[1] = -1; }
528 memset(play, 0, sizeof(play));
531 /* ---------------------------------------------------------------------- */
533 static int xpmAllocColourFunc(Display *display, Colormap colourmap, char *colourname, XColor *xcolour, void *closure)
539 if(colourname) if(XParseColor(display, colourmap, colourname, xcolour) == 0) return(-1); /* invalid name */
540 if(colourmap != privateColourmap) return(XAllocColor(display, colourmap, xcolour) != 0);
542 /* first try to find an exact match */
544 for(i = 0; i < privateNumColours; i++) {
545 if(privateColours[i].red == xcolour->red && privateColours[i].green == xcolour->green && privateColours[i].blue == xcolour->blue) match = i;
548 privateFlags[match] = 1;
549 xcolour->pixel = privateColours[match].pixel;
553 /* then find an unallocated colour that is close to what we want */
556 for(i = 0; i < privateNumColours; i++) {
557 if(privateFlags[i]) continue; /* skip if it is already allocated */
558 r = (privateColours[i].red - xcolour->red) / 256;
559 g = (privateColours[i].green - xcolour->green) / 256;
560 b = (privateColours[i].blue - xcolour->blue) / 256;
561 sum = r * r + g * g + b * b;
568 privateFlags[match] = 1;
569 privateColours[match].red = xcolour->red;
570 privateColours[match].green = xcolour->green;
571 privateColours[match].blue = xcolour->blue;
572 XStoreColor(display, colourmap, &privateColours[match]);
573 xcolour->pixel = privateColours[match].pixel;
574 return(1); /* found a close match */
577 /* if all else fails, just find the closest colour and return it */
580 for(i = 0; i < privateNumColours; i++) {
581 r = (privateColours[i].red - xcolour->red) / 256;
582 g = (privateColours[i].green - xcolour->green) / 256;
583 b = (privateColours[i].blue - xcolour->blue) / 256;
584 sum = r * r + g * g + b * b;
591 xcolour->red = privateColours[match].red;
592 xcolour->green = privateColours[match].green;
593 xcolour->blue = privateColours[match].blue;
594 xcolour->pixel = privateColours[match].pixel;
595 return(1); /* best we could do */
597 return(0); /* still didnt find one, give up */
600 static int xpmFreeColoursFunc(Display *display, Colormap colourmap, unsigned long *pixels, int npixels, void *closure)
602 if(colourmap != privateColourmap) XFreeColors(display, colourmap, pixels, npixels, 0);
603 return(1); /* non-zero for success */