1 /* 2000-08-10T18:03:54Z
3 * open X11 display and sound
10 #include <X11/Intrinsic.h>
11 #include <X11/keysymdef.h>
13 #include <X11/keysym.h>
15 #include <sys/types.h>
31 #if defined(TARGET_X11)
62 char play[SAMPLE_MAX];
64 static int sound_pid = -1;
65 int sound_pipe[2] = { -1, -1 }; /* for communication */
66 short *sound_data[SAMPLE_MAX]; /* pointer to sound data */
67 long sound_length[SAMPLE_MAX]; /* length of sound data */
69 static Screen *defaultScreen;
70 static Visual *defaultVisual;
71 static Colormap defaultColourmap;
72 static Window defaultRootWindow;
73 static unsigned int screenDepth;
74 static unsigned int screenWidth;
75 static unsigned int screenHeight;
76 static unsigned long screenBlackPixel;
77 static unsigned long screenWhitePixel;
79 static XGCValues gcValues;
82 static Bitmap *pcxBitmapsX2[4];
85 static const char *sound_names[SAMPLE_MAX] =
87 "00.blank.au","01.roll.au","02.stone.au","03.nut.au","04.crack.au",
88 "05.bug.au","06.tank.au","07.android.au","08.spring.au","09.slurp.au",
89 "10.eater.au","11.alien.au","12.collect.au","13.diamond.au","14.squash.au",
90 "15.drip.au","16.push.au","17.dirt.au","18.acid.au","19.ball.au",
91 "20.grow.au","21.wonder.au","22.door.au","23.exit.au","24.dynamite.au",
92 "25.tick.au","26.press.au","27.wheel.au","28.boom.au","29.time.au",
95 static const int sound_volume[SAMPLE_MAX] =
97 20,100,100,100,100,20,20,100,100,100,
98 50,100,100,100,100,100,100,100,100,100,
99 100,20,100,100,100,100,100,20,100,100,
105 char name[MAXNAME+2];
108 defaultScreen = DefaultScreenOfDisplay(display);
109 defaultVisual = DefaultVisualOfScreen(defaultScreen);
110 defaultColourmap = DefaultColormapOfScreen(defaultScreen);
111 defaultRootWindow = RootWindowOfScreen(defaultScreen);
112 screenDepth = DefaultDepthOfScreen(defaultScreen);
113 screenWidth = WidthOfScreen(defaultScreen);
114 screenHeight = HeightOfScreen(defaultScreen);
115 screenBlackPixel = BlackPixelOfScreen(defaultScreen);
116 screenWhitePixel = WhitePixelOfScreen(defaultScreen);
119 SetBitmaps_EM(pcxBitmapsX2);
121 objBitmap = pcxBitmapsX2[0];
122 botBitmap = pcxBitmapsX2[1];
123 sprBitmap = pcxBitmapsX2[2];
124 ttlBitmap = pcxBitmapsX2[3];
126 objPixmap = pcxBitmapsX2[0]->drawable;
127 botPixmap = pcxBitmapsX2[1]->drawable;
128 sprPixmap = pcxBitmapsX2[2]->drawable;
129 ttlPixmap = pcxBitmapsX2[3]->drawable;
131 objmaskBitmap = pcxBitmapsX2[0]->clip_mask;
132 botmaskBitmap = pcxBitmapsX2[1]->clip_mask;
133 sprmaskBitmap = pcxBitmapsX2[2]->clip_mask;
134 ttlmaskBitmap = pcxBitmapsX2[3]->clip_mask;
136 screenBitmap = CreateBitmap(MAX_BUF_XSIZE * TILEX, MAX_BUF_YSIZE * TILEY,
138 scoreBitmap = CreateBitmap(20 * TILEX, SCOREY, DEFAULT_DEPTH);
140 screenPixmap = screenBitmap->drawable;
141 scorePixmap = scoreBitmap->drawable;
144 spriteBitmap = XCreatePixmap(display, xwindow, TILEX, TILEY, 1);
145 if (spriteBitmap == 0)
147 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
148 XDisplayName(arg_display), "failed to create pixmap",
153 gcValues.graphics_exposures = False;
154 screenGC = XCreateGC(display, screenPixmap, GCGraphicsExposures, &gcValues);
157 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
158 XDisplayName(arg_display), "failed to create graphics context",
163 gcValues.graphics_exposures = False;
164 scoreGC = XCreateGC(display, scorePixmap, GCGraphicsExposures, &gcValues);
167 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
168 XDisplayName(arg_display), "failed to create graphics context",
174 objmaskBitmap ? GXcopyInverted : sprmaskBitmap ? GXcopy : GXset;
175 gcValues.graphics_exposures = False;
176 spriteGC = XCreateGC(display, spriteBitmap, GCFunction | GCGraphicsExposures,
180 fprintf(stderr, "%s: \"%s\": %s: %s\n", progname,
181 XDisplayName(arg_display), "failed to create graphics context",
186 /* ----------------------------------------------------------------- */
188 #if defined(PLATFORM_LINUX) || defined(PLATFORM_BSD)
190 if (arg_silence == 0)
192 for (i = 0; i < SAMPLE_MAX; i++)
198 snprintf(name, MAXNAME+2, "%s/%s/%s", arg_basedir, EM_SND_DIR,
203 snprintf(name, MAXNAME+2, "%s/%s", EM_SND_DIR, sound_names[i]);
206 if (name[MAXNAME]) snprintf_overflow("read sounds/ directory");
208 if (read_sample(name, &sound_data[i], &sound_length[i]))
213 int mult = sound_volume[i] * 65536 / (100 * MIXER_MAX);
214 stop = sound_data[i] + sound_length[i];
215 for (ptr = sound_data[i]; ptr < stop; ptr++)
216 *ptr = (*ptr * mult) / 65536;
220 if (pipe(sound_pipe) == -1)
222 fprintf(stderr, "%s: %s: %s\n", progname, "unable to create sound pipe",
230 fprintf(stderr, "%s: %s: %s\n", progname, "unable to fork sound thread",
235 close(sound_pipe[sound_pid == 0]); sound_pipe[sound_pid == 0] = -1;
237 _exit(sound_thread());
239 signal(SIGPIPE, SIG_IGN); /* dont crash if sound process dies */
242 #endif /* defined(PLATFORM_LINUX) || defined(PLATFORM_BSD) */
253 kill(sound_pid, SIGTERM);
254 waitpid(sound_pid, 0, 0);
257 if (sound_pipe[0] != -1)
258 close(sound_pipe[0]);
259 if (sound_pipe[1] != -1)
260 close(sound_pipe[1]);
262 for (i = 0; i < SAMPLE_MAX; i++)
267 XFreeGC(display, screenGC);
269 XFreeGC(display, scoreGC);
271 XFreeGC(display, spriteGC);
274 XFreePixmap(display, spriteBitmap);
277 /* ---------------------------------------------------------------------- */
279 void sound_play(void)
281 if (sound_pipe[1] != -1)
283 if (write(sound_pipe[1], &play, sizeof(play)) == -1)
285 fprintf(stderr, "%s: %s: %s\n", progname, "write sound",
288 if (sound_pipe[0] != -1)
290 close(sound_pipe[0]);
294 if (sound_pipe[1] != -1)
296 close(sound_pipe[1]);
302 memset(play, 0, sizeof(play));