rnd-20021123-1-src
[rocksndiamonds.git] / src / libgame / system.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * system.c                                                 *
12 ***********************************************************/
13
14 #include <string.h>
15 #include <signal.h>
16
17 #include "platform.h"
18
19 #if defined(PLATFORM_MSDOS)
20 #include <fcntl.h>
21 #endif
22
23 #include "system.h"
24 #include "sound.h"
25 #include "setup.h"
26 #include "joystick.h"
27 #include "misc.h"
28
29
30 /* ========================================================================= */
31 /* exported variables                                                        */
32 /* ========================================================================= */
33
34 struct ProgramInfo      program;
35 struct OptionInfo       options;
36 struct VideoSystemInfo  video;
37 struct AudioSystemInfo  audio;
38 struct GfxInfo          gfx;
39 struct ArtworkInfo      artwork;
40 struct JoystickInfo     joystick;
41 struct SetupInfo        setup;
42
43 LevelDirTree           *leveldir_first = NULL;
44 LevelDirTree           *leveldir_current = NULL;
45 int                     level_nr;
46
47 Display                *display = NULL;
48 Visual                 *visual = NULL;
49 int                     screen = 0;
50 Colormap                cmap = None;
51
52 DrawWindow             *window = NULL;
53 DrawBuffer             *backbuffer = NULL;
54 DrawBuffer             *drawto = NULL;
55
56 int                     button_status = MB_NOT_PRESSED;
57 boolean                 motion_status = FALSE;
58
59 int                     redraw_mask = REDRAW_NONE;
60 int                     redraw_tiles = 0;
61
62 int                     FrameCounter = 0;
63
64
65 /* ========================================================================= */
66 /* init/close functions                                                      */
67 /* ========================================================================= */
68
69 void InitCommandName(char *argv0)
70 {
71   program.command_basename =
72     (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
73 }
74
75 void InitExitFunction(void (*exit_function)(int))
76 {
77   program.exit_function = exit_function;
78
79   /* set signal handlers to custom exit function */
80   signal(SIGINT, exit_function);
81   signal(SIGTERM, exit_function);
82
83 #if defined(TARGET_SDL)
84   /* set exit function to automatically cleanup SDL stuff after exit() */
85   atexit(SDL_Quit);
86 #endif
87 }
88
89 void InitPlatformDependantStuff(void)
90 {
91 #if defined(PLATFORM_MSDOS)
92   _fmode = O_BINARY;
93 #endif
94
95 #if !defined(PLATFORM_UNIX)
96   program.userdata_directory = "userdata";
97 #endif
98
99 #if defined(PLATFORM_MSDOS)
100   initErrorFile();
101 #endif
102
103 #if defined(TARGET_SDL)
104   if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
105     Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
106 #endif
107 }
108
109 void ClosePlatformDependantStuff(void)
110 {
111 #if defined(PLATFORM_MSDOS)
112   dumpErrorFile();
113 #endif
114 }
115
116 void InitProgramInfo(char *unix_userdata_directory, char *program_title,
117                      char *window_title, char *icon_title,
118                      char *x11_icon_filename, char *x11_iconmask_filename,
119                      char *msdos_pointer_filename,
120                      char *cookie_prefix, char *filename_prefix,
121                      int program_version)
122 {
123 #if defined(PLATFORM_UNIX)
124   program.userdata_directory = unix_userdata_directory;
125 #else
126   program.userdata_directory = "userdata";
127 #endif
128
129   program.program_title = program_title;
130   program.window_title = window_title;
131   program.icon_title = icon_title;
132   program.x11_icon_filename = x11_icon_filename;
133   program.x11_iconmask_filename = x11_iconmask_filename;
134   program.msdos_pointer_filename = msdos_pointer_filename;
135
136   program.cookie_prefix = cookie_prefix;
137   program.filename_prefix = filename_prefix;
138
139   program.version_major = VERSION_MAJOR(program_version);
140   program.version_minor = VERSION_MINOR(program_version);
141   program.version_patch = VERSION_PATCH(program_version);
142 }
143
144 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
145                       int real_sx, int real_sy,
146                       int full_sxsize, int full_sysize)
147 {
148   gfx.sx = sx;
149   gfx.sy = sy;
150   gfx.sxsize = sxsize;
151   gfx.sysize = sysize;
152   gfx.real_sx = real_sx;
153   gfx.real_sy = real_sy;
154   gfx.full_sxsize = full_sxsize;
155   gfx.full_sysize = full_sysize;
156
157   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
158 }
159
160 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
161 {
162   gfx.dx = dx;
163   gfx.dy = dy;
164   gfx.dxsize = dxsize;
165   gfx.dysize = dysize;
166 }
167
168 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
169 {
170   gfx.vx = vx;
171   gfx.vy = vy;
172   gfx.vxsize = vxsize;
173   gfx.vysize = vysize;
174 }
175
176 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
177 {
178   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
179   gfx.scrollbuffer_width = scrollbuffer_width;
180   gfx.scrollbuffer_height = scrollbuffer_height;
181 }
182
183 void SetDrawDeactivationMask(int draw_deactivation_mask)
184 {
185   gfx.draw_deactivation_mask = draw_deactivation_mask;
186 }
187
188
189 /* ========================================================================= */
190 /* video functions                                                           */
191 /* ========================================================================= */
192
193 inline static int GetRealDepth(int depth)
194 {
195   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
196 }
197
198 inline void InitVideoDisplay(void)
199 {
200 #if defined(TARGET_SDL)
201   SDLInitVideoDisplay();
202 #else
203   X11InitVideoDisplay();
204 #endif
205 }
206
207 inline void CloseVideoDisplay(void)
208 {
209   KeyboardAutoRepeatOn();
210
211 #if defined(TARGET_SDL)
212   SDL_QuitSubSystem(SDL_INIT_VIDEO);
213 #else
214
215   if (display)
216     XCloseDisplay(display);
217 #endif
218 }
219
220 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
221                             int width, int height, int depth,
222                             boolean fullscreen)
223 {
224   video.width = width;
225   video.height = height;
226   video.depth = GetRealDepth(depth);
227   video.fullscreen_available = FULLSCREEN_STATUS;
228   video.fullscreen_enabled = FALSE;
229
230 #ifdef TARGET_SDL
231   SDLInitVideoBuffer(backbuffer, window, fullscreen);
232 #else
233   X11InitVideoBuffer(backbuffer, window);
234 #endif
235 }
236
237 inline Bitmap *CreateBitmapStruct(void)
238 {
239 #ifdef TARGET_SDL
240   return checked_calloc(sizeof(struct SDLSurfaceInfo));
241 #else
242   return checked_calloc(sizeof(struct X11DrawableInfo));
243 #endif
244 }
245
246 inline Bitmap *CreateBitmap(int width, int height, int depth)
247 {
248   Bitmap *new_bitmap = CreateBitmapStruct();
249   int real_depth = GetRealDepth(depth);
250
251 #ifdef TARGET_SDL
252   SDL_Surface *surface_tmp, *surface_native;
253
254   if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
255                                           real_depth, 0, 0, 0, 0))
256       == NULL)
257     Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
258
259   if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
260     Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
261
262   SDL_FreeSurface(surface_tmp);
263
264   new_bitmap->surface = surface_native;
265 #else
266   Pixmap pixmap;
267
268   if ((pixmap = XCreatePixmap(display, window->drawable,
269                               width, height, real_depth))
270       == None)
271     Error(ERR_EXIT, "cannot create pixmap");
272
273   new_bitmap->drawable = pixmap;
274
275   if (window == NULL)
276     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
277
278   new_bitmap->gc = window->gc;
279
280   new_bitmap->line_gc[0] = window->line_gc[0];
281   new_bitmap->line_gc[1] = window->line_gc[1];
282 #endif
283
284   return new_bitmap;
285 }
286
287 inline static void FreeBitmapPointers(Bitmap *bitmap)
288 {
289   if (bitmap == NULL)
290     return;
291
292 #ifdef TARGET_SDL
293   if (bitmap->surface)
294     SDL_FreeSurface(bitmap->surface);
295   if (bitmap->surface_masked)
296     SDL_FreeSurface(bitmap->surface_masked);
297   bitmap->surface = NULL;
298   bitmap->surface_masked = NULL;
299 #else
300   /* The X11 version seems to have a memory leak here -- although
301      "XFreePixmap()" is called, the correspondig memory seems not
302      to be freed (according to "ps"). The SDL version apparently
303      does not have this problem. */
304
305   if (bitmap->drawable)
306     XFreePixmap(display, bitmap->drawable);
307   if (bitmap->clip_mask)
308     XFreePixmap(display, bitmap->clip_mask);
309   if (bitmap->stored_clip_gc)
310     XFreeGC(display, bitmap->stored_clip_gc);
311   /* the other GCs are only pointers to GCs used elsewhere */
312   bitmap->drawable = None;
313   bitmap->clip_mask = None;
314   bitmap->stored_clip_gc = None;
315 #endif
316
317   if (bitmap->source_filename)
318     free(bitmap->source_filename);
319   bitmap->source_filename = NULL;
320 }
321
322 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
323                                           Bitmap *dst_bitmap)
324 {
325   if (src_bitmap == NULL || dst_bitmap == NULL)
326     return;
327
328   FreeBitmapPointers(dst_bitmap);
329
330   *dst_bitmap = *src_bitmap;
331 }
332
333 inline void FreeBitmap(Bitmap *bitmap)
334 {
335   if (bitmap == NULL)
336     return;
337
338   FreeBitmapPointers(bitmap);
339
340   free(bitmap);
341 }
342
343 inline void CloseWindow(DrawWindow *window)
344 {
345 #ifdef TARGET_X11
346   if (window->drawable)
347   {
348     XUnmapWindow(display, window->drawable);
349     XDestroyWindow(display, window->drawable);
350   }
351   if (window->gc)
352     XFreeGC(display, window->gc);
353 #endif
354 }
355
356 inline boolean DrawingDeactivated(int x, int y, int width, int height)
357 {
358   if (gfx.draw_deactivation_mask != REDRAW_NONE)
359   {
360     if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
361         x < gfx.sx + gfx.sxsize)
362       return TRUE;
363     else if ((gfx.draw_deactivation_mask & REDRAW_DOORS) &&
364              x > gfx.dx)
365     {
366       if ((gfx.draw_deactivation_mask & REDRAW_DOOR_1) &&
367           y < gfx.dy + gfx.dysize)
368         return TRUE;
369       else if ((gfx.draw_deactivation_mask & REDRAW_DOOR_2) &&
370                y > gfx.vy)
371         return TRUE;
372     }
373   }
374
375   return FALSE;
376 }
377
378 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
379                        int src_x, int src_y,
380                        int width, int height,
381                        int dst_x, int dst_y)
382 {
383   if (DrawingDeactivated(dst_x, dst_y, width, height))
384     return;
385
386 #ifdef TARGET_SDL
387   SDLCopyArea(src_bitmap, dst_bitmap,
388               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
389 #else
390   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
391             dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
392 #endif
393 }
394
395 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
396 {
397   if (DrawingDeactivated(x, y, width, height))
398     return;
399
400 #ifdef TARGET_SDL
401   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
402 #else
403   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
404 #endif
405 }
406
407 #if 0
408 #ifndef TARGET_SDL
409 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
410 #endif
411 #endif
412
413 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
414 {
415 #ifdef TARGET_X11
416   if (clip_gc)
417   {
418     bitmap->clip_gc = clip_gc;
419     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
420   }
421 #if 0
422   last_clip_gc = clip_gc;
423 #endif
424 #endif
425 }
426
427 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
428 {
429 #ifdef TARGET_X11
430   if (clip_gc)
431   {
432     bitmap->clip_gc = clip_gc;
433     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
434   }
435 #if 0
436   last_clip_gc = clip_gc;
437 #endif
438 #endif
439 }
440
441 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
442                              int src_x, int src_y,
443                              int width, int height,
444                              int dst_x, int dst_y)
445 {
446   if (DrawingDeactivated(dst_x, dst_y, width, height))
447     return;
448
449 #ifdef TARGET_SDL
450   SDLCopyArea(src_bitmap, dst_bitmap,
451               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
452 #else
453   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
454             src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
455 #endif
456 }
457
458 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
459                                 int to_x, int to_y)
460 {
461 #ifdef TARGET_SDL
462   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
463 #else
464   XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
465   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
466   XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
467 #endif
468 }
469
470 #if !defined(TARGET_X11_NATIVE)
471 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
472                      int to_x, int to_y, Pixel pixel, int line_width)
473 {
474   int x, y;
475
476   for (x=0; x<line_width; x++)
477   {
478     for (y=0; y<line_width; y++)
479     {
480       int dx = x - line_width / 2;
481       int dy = y - line_width / 2;
482
483       if ((x == 0 && y == 0) ||
484           (x == 0 && y == line_width - 1) ||
485           (x == line_width - 1 && y == 0) ||
486           (x == line_width - 1 && y == line_width - 1))
487         continue;
488
489 #if defined(TARGET_SDL)
490       SDLDrawLine(bitmap,
491                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
492 #elif defined(TARGET_ALLEGRO)
493       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
494                       to_x + dx, to_y + dy, pixel);
495 #endif
496     }
497   }
498 }
499 #endif
500
501 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
502                       Pixel pixel)
503 {
504 #if !defined(TARGET_X11_NATIVE)
505   int line_width = 4;
506   int i;
507
508   for (i=0; i<num_points - 1; i++)
509     DrawLine(bitmap, points[i].x, points[i].y,
510              points[i + 1].x, points[i + 1].y, pixel, line_width);
511
512   /*
513   SDLDrawLines(bitmap->surface, points, num_points, pixel);
514   */
515 #else
516   XSetForeground(display, bitmap->line_gc[1], pixel);
517   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
518              (XPoint *)points, num_points, CoordModeOrigin);
519   /*
520   XSetForeground(display, gc, BlackPixel(display, screen));
521   */
522 #endif
523 }
524
525 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
526 {
527 #if defined(TARGET_SDL)
528   return SDLGetPixel(bitmap, x, y);
529 #elif defined(TARGET_ALLEGRO)
530   return AllegroGetPixel(bitmap->drawable, x, y);
531 #else
532   unsigned long pixel_value;
533   XImage *pixel_image;
534
535   pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
536                           AllPlanes, ZPixmap);
537   pixel_value = XGetPixel(pixel_image, 0, 0);
538
539   XDestroyImage(pixel_image);
540
541   return pixel_value;
542 #endif
543 }
544
545 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
546                              unsigned int color_g, unsigned int color_b)
547 {
548   Pixel pixel;
549
550 #if defined(TARGET_SDL)
551   pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
552 #elif defined(TARGET_ALLEGRO)
553   pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
554 #elif defined(TARGET_X11_NATIVE)
555   XColor xcolor;
556
557   xcolor.flags = DoRed | DoGreen | DoBlue;
558   xcolor.red = (color_r << 8);
559   xcolor.green = (color_g << 8);
560   xcolor.blue = (color_b << 8);
561   XAllocColor(display, cmap, &xcolor);
562   pixel = xcolor.pixel;
563 #endif
564
565   return pixel;
566 }
567
568 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
569 {
570   unsigned int color_r = (color >> 16) & 0xff;
571   unsigned int color_g = (color >>  8) & 0xff;
572   unsigned int color_b = (color >>  0) & 0xff;
573
574   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
575 }
576
577 /* execute all pending screen drawing operations */
578 inline void FlushDisplay(void)
579 {
580 #ifndef TARGET_SDL
581   XFlush(display);
582 #endif
583 }
584
585 /* execute and wait for all pending screen drawing operations */
586 inline void SyncDisplay(void)
587 {
588 #ifndef TARGET_SDL
589   XSync(display, FALSE);
590 #endif
591 }
592
593 inline void KeyboardAutoRepeatOn(void)
594 {
595 #ifdef TARGET_SDL
596   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
597                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
598   SDL_EnableUNICODE(1);
599 #else
600   if (display)
601     XAutoRepeatOn(display);
602 #endif
603 }
604
605 inline void KeyboardAutoRepeatOff(void)
606 {
607 #ifdef TARGET_SDL
608   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
609   SDL_EnableUNICODE(0);
610 #else
611   if (display)
612     XAutoRepeatOff(display);
613 #endif
614 }
615
616 inline boolean PointerInWindow(DrawWindow *window)
617 {
618 #ifdef TARGET_SDL
619   return TRUE;
620 #else
621   Window root, child;
622   int root_x, root_y;
623   unsigned int mask;
624   int win_x, win_y;
625
626   /* if XQueryPointer() returns False, the pointer
627      is not on the same screen as the specified window */
628   return XQueryPointer(display, window->drawable, &root, &child,
629                        &root_x, &root_y, &win_x, &win_y, &mask);
630 #endif
631 }
632
633 inline boolean SetVideoMode(boolean fullscreen)
634 {
635 #ifdef TARGET_SDL
636   return SDLSetVideoMode(&backbuffer, fullscreen);
637 #else
638   boolean success = TRUE;
639
640   if (fullscreen && video.fullscreen_available)
641   {
642     Error(ERR_WARN, "fullscreen not available in X11 version");
643
644     /* display error message only once */
645     video.fullscreen_available = FALSE;
646
647     success = FALSE;
648   }
649
650   return success;
651 #endif
652 }
653
654 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
655 {
656 #ifdef TARGET_SDL
657   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
658       (!fullscreen && video.fullscreen_enabled))
659     fullscreen = SetVideoMode(fullscreen);
660 #endif
661
662   return fullscreen;
663 }
664
665 Bitmap *LoadImage(char *filename)
666 {
667   Bitmap *new_bitmap;
668
669 #if defined(TARGET_SDL)
670   new_bitmap = SDLLoadImage(filename);
671 #else
672   new_bitmap = X11LoadImage(filename);
673 #endif
674
675   if (new_bitmap)
676     new_bitmap->source_filename = getStringCopy(filename);
677
678   return new_bitmap;
679 }
680
681 Bitmap *LoadCustomImage(char *basename)
682 {
683   char *filename = getCustomImageFilename(basename);
684   Bitmap *new_bitmap;
685
686   if (filename == NULL)
687     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
688
689   if ((new_bitmap = LoadImage(filename)) == NULL)
690     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
691
692   return new_bitmap;
693 }
694
695 void ReloadCustomImage(Bitmap *bitmap, char *basename)
696 {
697   char *filename = getCustomImageFilename(basename);
698   Bitmap *new_bitmap;
699
700   if (filename == NULL)         /* (should never happen) */
701   {
702     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
703     return;
704   }
705
706   if (strcmp(filename, bitmap->source_filename) == 0)
707   {
708     /* The old and new image are the same (have the same filename and path).
709        This usually means that this image does not exist in this graphic set
710        and a fallback to the existing image is done. */
711
712     return;
713   }
714
715   if ((new_bitmap = LoadImage(filename)) == NULL)
716   {
717     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
718     return;
719   }
720
721   if (bitmap->width != new_bitmap->width ||
722       bitmap->height != new_bitmap->height)
723   {
724     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
725           filename);
726     FreeBitmap(new_bitmap);
727     return;
728   }
729
730   TransferBitmapPointers(new_bitmap, bitmap);
731   free(new_bitmap);
732 }
733
734
735 /* ========================================================================= */
736 /* audio functions                                                           */
737 /* ========================================================================= */
738
739 inline void OpenAudio(void)
740 {
741   /* always start with reliable default values */
742   audio.sound_available = FALSE;
743   audio.music_available = FALSE;
744   audio.loops_available = FALSE;
745
746   audio.sound_enabled = FALSE;
747   audio.sound_deactivated = FALSE;
748
749   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
750   audio.mixer_pid = -1;
751   audio.device_name = NULL;
752   audio.device_fd = -1;
753
754   audio.num_channels = 0;
755   audio.music_channel = 0;
756   audio.first_sound_channel = 0;
757
758 #if defined(TARGET_SDL)
759   SDLOpenAudio();
760 #elif defined(PLATFORM_UNIX)
761   UnixOpenAudio();
762 #elif defined(PLATFORM_MSDOS)
763   MSDOSOpenAudio();
764 #endif
765 }
766
767 inline void CloseAudio(void)
768 {
769 #if defined(TARGET_SDL)
770   SDLCloseAudio();
771 #elif defined(PLATFORM_UNIX)
772   UnixCloseAudio();
773 #elif defined(PLATFORM_MSDOS)
774   MSDOSCloseAudio();
775 #endif
776
777   audio.sound_enabled = FALSE;
778 }
779
780 inline void SetAudioMode(boolean enabled)
781 {
782   if (!audio.sound_available)
783     return;
784
785   audio.sound_enabled = enabled;
786 }
787
788
789 /* ========================================================================= */
790 /* event functions                                                           */
791 /* ========================================================================= */
792
793 inline void InitEventFilter(EventFilter filter_function)
794 {
795 #ifdef TARGET_SDL
796   /* set event filter to filter out certain events */
797   SDL_SetEventFilter(filter_function);
798 #endif
799 }
800
801 inline boolean PendingEvent(void)
802 {
803 #ifdef TARGET_SDL
804   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
805 #else
806   return (XPending(display) ? TRUE : FALSE);
807 #endif
808 }
809
810 inline void NextEvent(Event *event)
811 {
812 #ifdef TARGET_SDL
813   SDLNextEvent(event);
814 #else
815   XNextEvent(display, event);
816 #endif
817 }
818
819 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
820 {
821 #ifdef TARGET_SDL
822 #if 0
823   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
824          (int)event->keysym.unicode,
825          (int)event->keysym.sym,
826          (int)SDL_GetModState());
827 #endif
828
829   if (with_modifiers &&
830       event->keysym.unicode > 0x0000 &&
831       event->keysym.unicode < 0x2000)
832     return event->keysym.unicode;
833   else
834     return event->keysym.sym;
835 #else
836 #if 0
837   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
838          (int)XLookupKeysym(event, event->state),
839          (int)XLookupKeysym(event, 0));
840 #endif
841
842   if (with_modifiers)
843     return XLookupKeysym(event, event->state);
844   else
845     return XLookupKeysym(event, 0);
846 #endif
847 }
848
849 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
850 {
851   if (event->type != EVENT_CLIENTMESSAGE)
852     return FALSE;
853
854 #if defined(TARGET_SDL)
855   return TRUE;          /* the only possible message here is SDL_QUIT */
856 #elif defined(PLATFORM_UNIX)
857   if ((event->window == window->drawable) &&
858       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
859     return TRUE;
860 #endif
861
862   return FALSE;
863 }
864
865
866 /* ========================================================================= */
867 /* joystick functions                                                        */
868 /* ========================================================================= */
869
870 inline void InitJoysticks()
871 {
872   int i;
873
874 #ifdef NO_JOYSTICK
875   return;       /* joysticks generally deactivated by compile-time directive */
876 #endif
877
878   /* always start with reliable default values */
879   joystick.status = JOYSTICK_NOT_AVAILABLE;
880   for (i=0; i<MAX_PLAYERS; i++)
881     joystick.fd[i] = -1;                /* joystick device closed */
882
883 #if defined(TARGET_SDL)
884   SDLInitJoysticks();
885 #elif defined(PLATFORM_UNIX)
886   UnixInitJoysticks();
887 #elif defined(PLATFORM_MSDOS)
888   MSDOSInitJoysticks();
889 #endif
890 }
891
892 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
893 {
894 #if defined(TARGET_SDL)
895   return SDLReadJoystick(nr, x, y, b1, b2);
896 #elif defined(PLATFORM_UNIX)
897   return UnixReadJoystick(nr, x, y, b1, b2);
898 #elif defined(PLATFORM_MSDOS)
899   return MSDOSReadJoystick(nr, x, y, b1, b2);
900 #endif
901 }