7cebdb8fa727a1c5e4f8330dbe02568f115e7cc2
[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_ALL)
361       return TRUE;
362     else if ((gfx.draw_deactivation_mask & REDRAW_FIELD) &&
363              x < gfx.sx + gfx.sxsize)
364       return TRUE;
365     else if ((gfx.draw_deactivation_mask & REDRAW_DOORS) &&
366              x > gfx.dx)
367     {
368       if ((gfx.draw_deactivation_mask & REDRAW_DOOR_1) &&
369           y < gfx.dy + gfx.dysize)
370         return TRUE;
371       else if ((gfx.draw_deactivation_mask & REDRAW_DOOR_2) &&
372                y > gfx.vy)
373         return TRUE;
374     }
375   }
376
377   return FALSE;
378 }
379
380 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
381                        int src_x, int src_y,
382                        int width, int height,
383                        int dst_x, int dst_y)
384 {
385   if (DrawingDeactivated(dst_x, dst_y, width, height))
386     return;
387
388 #ifdef TARGET_SDL
389   SDLCopyArea(src_bitmap, dst_bitmap,
390               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_OPAQUE);
391 #else
392   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
393             dst_bitmap->gc, src_x, src_y, width, height, dst_x, dst_y);
394 #endif
395 }
396
397 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
398 {
399   if (DrawingDeactivated(x, y, width, height))
400     return;
401
402 #ifdef TARGET_SDL
403   SDLFillRectangle(bitmap, x, y, width, height, 0x000000);
404 #else
405   XFillRectangle(display, bitmap->drawable, bitmap->gc, x, y, width, height);
406 #endif
407 }
408
409 #if 0
410 #ifndef TARGET_SDL
411 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
412 #endif
413 #endif
414
415 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
416 {
417 #ifdef TARGET_X11
418   if (clip_gc)
419   {
420     bitmap->clip_gc = clip_gc;
421     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
422   }
423 #if 0
424   last_clip_gc = clip_gc;
425 #endif
426 #endif
427 }
428
429 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
430 {
431 #ifdef TARGET_X11
432   if (clip_gc)
433   {
434     bitmap->clip_gc = clip_gc;
435     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
436   }
437 #if 0
438   last_clip_gc = clip_gc;
439 #endif
440 #endif
441 }
442
443 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
444                              int src_x, int src_y,
445                              int width, int height,
446                              int dst_x, int dst_y)
447 {
448   if (DrawingDeactivated(dst_x, dst_y, width, height))
449     return;
450
451 #ifdef TARGET_SDL
452   SDLCopyArea(src_bitmap, dst_bitmap,
453               src_x, src_y, width, height, dst_x, dst_y, SDLCOPYAREA_MASKED);
454 #else
455   XCopyArea(display, src_bitmap->drawable, dst_bitmap->drawable,
456             src_bitmap->clip_gc, src_x, src_y, width, height, dst_x, dst_y);
457 #endif
458 }
459
460 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
461                                 int to_x, int to_y)
462 {
463 #ifdef TARGET_SDL
464   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
465 #else
466   XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
467   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
468   XSetForeground(display, bitmap->gc, BlackPixel(display, screen));
469 #endif
470 }
471
472 #if !defined(TARGET_X11_NATIVE)
473 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
474                      int to_x, int to_y, Pixel pixel, int line_width)
475 {
476   int x, y;
477
478   for (x=0; x<line_width; x++)
479   {
480     for (y=0; y<line_width; y++)
481     {
482       int dx = x - line_width / 2;
483       int dy = y - line_width / 2;
484
485       if ((x == 0 && y == 0) ||
486           (x == 0 && y == line_width - 1) ||
487           (x == line_width - 1 && y == 0) ||
488           (x == line_width - 1 && y == line_width - 1))
489         continue;
490
491 #if defined(TARGET_SDL)
492       SDLDrawLine(bitmap,
493                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
494 #elif defined(TARGET_ALLEGRO)
495       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
496                       to_x + dx, to_y + dy, pixel);
497 #endif
498     }
499   }
500 }
501 #endif
502
503 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
504                       Pixel pixel)
505 {
506 #if !defined(TARGET_X11_NATIVE)
507   int line_width = 4;
508   int i;
509
510   for (i=0; i<num_points - 1; i++)
511     DrawLine(bitmap, points[i].x, points[i].y,
512              points[i + 1].x, points[i + 1].y, pixel, line_width);
513
514   /*
515   SDLDrawLines(bitmap->surface, points, num_points, pixel);
516   */
517 #else
518   XSetForeground(display, bitmap->line_gc[1], pixel);
519   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
520              (XPoint *)points, num_points, CoordModeOrigin);
521   /*
522   XSetForeground(display, gc, BlackPixel(display, screen));
523   */
524 #endif
525 }
526
527 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
528 {
529 #if defined(TARGET_SDL)
530   return SDLGetPixel(bitmap, x, y);
531 #elif defined(TARGET_ALLEGRO)
532   return AllegroGetPixel(bitmap->drawable, x, y);
533 #else
534   unsigned long pixel_value;
535   XImage *pixel_image;
536
537   pixel_image = XGetImage(display, bitmap->drawable, x, y, 1, 1,
538                           AllPlanes, ZPixmap);
539   pixel_value = XGetPixel(pixel_image, 0, 0);
540
541   XDestroyImage(pixel_image);
542
543   return pixel_value;
544 #endif
545 }
546
547 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
548                              unsigned int color_g, unsigned int color_b)
549 {
550   Pixel pixel;
551
552 #if defined(TARGET_SDL)
553   pixel = SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
554 #elif defined(TARGET_ALLEGRO)
555   pixel = AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
556 #elif defined(TARGET_X11_NATIVE)
557   XColor xcolor;
558
559   xcolor.flags = DoRed | DoGreen | DoBlue;
560   xcolor.red = (color_r << 8);
561   xcolor.green = (color_g << 8);
562   xcolor.blue = (color_b << 8);
563   XAllocColor(display, cmap, &xcolor);
564   pixel = xcolor.pixel;
565 #endif
566
567   return pixel;
568 }
569
570 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
571 {
572   unsigned int color_r = (color >> 16) & 0xff;
573   unsigned int color_g = (color >>  8) & 0xff;
574   unsigned int color_b = (color >>  0) & 0xff;
575
576   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
577 }
578
579 /* execute all pending screen drawing operations */
580 inline void FlushDisplay(void)
581 {
582 #ifndef TARGET_SDL
583   XFlush(display);
584 #endif
585 }
586
587 /* execute and wait for all pending screen drawing operations */
588 inline void SyncDisplay(void)
589 {
590 #ifndef TARGET_SDL
591   XSync(display, FALSE);
592 #endif
593 }
594
595 inline void KeyboardAutoRepeatOn(void)
596 {
597 #ifdef TARGET_SDL
598   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
599                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
600   SDL_EnableUNICODE(1);
601 #else
602   if (display)
603     XAutoRepeatOn(display);
604 #endif
605 }
606
607 inline void KeyboardAutoRepeatOff(void)
608 {
609 #ifdef TARGET_SDL
610   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
611   SDL_EnableUNICODE(0);
612 #else
613   if (display)
614     XAutoRepeatOff(display);
615 #endif
616 }
617
618 inline boolean PointerInWindow(DrawWindow *window)
619 {
620 #ifdef TARGET_SDL
621   return TRUE;
622 #else
623   Window root, child;
624   int root_x, root_y;
625   unsigned int mask;
626   int win_x, win_y;
627
628   /* if XQueryPointer() returns False, the pointer
629      is not on the same screen as the specified window */
630   return XQueryPointer(display, window->drawable, &root, &child,
631                        &root_x, &root_y, &win_x, &win_y, &mask);
632 #endif
633 }
634
635 inline boolean SetVideoMode(boolean fullscreen)
636 {
637 #ifdef TARGET_SDL
638   return SDLSetVideoMode(&backbuffer, fullscreen);
639 #else
640   boolean success = TRUE;
641
642   if (fullscreen && video.fullscreen_available)
643   {
644     Error(ERR_WARN, "fullscreen not available in X11 version");
645
646     /* display error message only once */
647     video.fullscreen_available = FALSE;
648
649     success = FALSE;
650   }
651
652   return success;
653 #endif
654 }
655
656 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
657 {
658 #ifdef TARGET_SDL
659   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
660       (!fullscreen && video.fullscreen_enabled))
661     fullscreen = SetVideoMode(fullscreen);
662 #endif
663
664   return fullscreen;
665 }
666
667 Bitmap *LoadImage(char *filename)
668 {
669   Bitmap *new_bitmap;
670
671 #if defined(TARGET_SDL)
672   new_bitmap = SDLLoadImage(filename);
673 #else
674   new_bitmap = X11LoadImage(filename);
675 #endif
676
677   if (new_bitmap)
678     new_bitmap->source_filename = getStringCopy(filename);
679
680   return new_bitmap;
681 }
682
683 Bitmap *LoadCustomImage(char *basename)
684 {
685   char *filename = getCustomImageFilename(basename);
686   Bitmap *new_bitmap;
687
688   if (filename == NULL)
689     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
690
691   if ((new_bitmap = LoadImage(filename)) == NULL)
692     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
693
694   return new_bitmap;
695 }
696
697 void ReloadCustomImage(Bitmap *bitmap, char *basename)
698 {
699   char *filename = getCustomImageFilename(basename);
700   Bitmap *new_bitmap;
701
702   if (filename == NULL)         /* (should never happen) */
703   {
704     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
705     return;
706   }
707
708   if (strcmp(filename, bitmap->source_filename) == 0)
709   {
710     /* The old and new image are the same (have the same filename and path).
711        This usually means that this image does not exist in this graphic set
712        and a fallback to the existing image is done. */
713
714     return;
715   }
716
717   if ((new_bitmap = LoadImage(filename)) == NULL)
718   {
719     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
720     return;
721   }
722
723   if (bitmap->width != new_bitmap->width ||
724       bitmap->height != new_bitmap->height)
725   {
726     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
727           filename);
728     FreeBitmap(new_bitmap);
729     return;
730   }
731
732   TransferBitmapPointers(new_bitmap, bitmap);
733   free(new_bitmap);
734 }
735
736
737 /* ========================================================================= */
738 /* audio functions                                                           */
739 /* ========================================================================= */
740
741 inline void OpenAudio(void)
742 {
743   /* always start with reliable default values */
744   audio.sound_available = FALSE;
745   audio.music_available = FALSE;
746   audio.loops_available = FALSE;
747
748   audio.sound_enabled = FALSE;
749   audio.sound_deactivated = FALSE;
750
751   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
752   audio.mixer_pid = -1;
753   audio.device_name = NULL;
754   audio.device_fd = -1;
755
756   audio.num_channels = 0;
757   audio.music_channel = 0;
758   audio.first_sound_channel = 0;
759
760 #if defined(TARGET_SDL)
761   SDLOpenAudio();
762 #elif defined(PLATFORM_UNIX)
763   UnixOpenAudio();
764 #elif defined(PLATFORM_MSDOS)
765   MSDOSOpenAudio();
766 #endif
767 }
768
769 inline void CloseAudio(void)
770 {
771 #if defined(TARGET_SDL)
772   SDLCloseAudio();
773 #elif defined(PLATFORM_UNIX)
774   UnixCloseAudio();
775 #elif defined(PLATFORM_MSDOS)
776   MSDOSCloseAudio();
777 #endif
778
779   audio.sound_enabled = FALSE;
780 }
781
782 inline void SetAudioMode(boolean enabled)
783 {
784   if (!audio.sound_available)
785     return;
786
787   audio.sound_enabled = enabled;
788 }
789
790
791 /* ========================================================================= */
792 /* event functions                                                           */
793 /* ========================================================================= */
794
795 inline void InitEventFilter(EventFilter filter_function)
796 {
797 #ifdef TARGET_SDL
798   /* set event filter to filter out certain events */
799   SDL_SetEventFilter(filter_function);
800 #endif
801 }
802
803 inline boolean PendingEvent(void)
804 {
805 #ifdef TARGET_SDL
806   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
807 #else
808   return (XPending(display) ? TRUE : FALSE);
809 #endif
810 }
811
812 inline void NextEvent(Event *event)
813 {
814 #ifdef TARGET_SDL
815   SDLNextEvent(event);
816 #else
817   XNextEvent(display, event);
818 #endif
819 }
820
821 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
822 {
823 #ifdef TARGET_SDL
824 #if 0
825   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
826          (int)event->keysym.unicode,
827          (int)event->keysym.sym,
828          (int)SDL_GetModState());
829 #endif
830
831   if (with_modifiers &&
832       event->keysym.unicode > 0x0000 &&
833       event->keysym.unicode < 0x2000)
834     return event->keysym.unicode;
835   else
836     return event->keysym.sym;
837 #else
838 #if 0
839   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
840          (int)XLookupKeysym(event, event->state),
841          (int)XLookupKeysym(event, 0));
842 #endif
843
844   if (with_modifiers)
845     return XLookupKeysym(event, event->state);
846   else
847     return XLookupKeysym(event, 0);
848 #endif
849 }
850
851 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
852 {
853   if (event->type != EVENT_CLIENTMESSAGE)
854     return FALSE;
855
856 #if defined(TARGET_SDL)
857   return TRUE;          /* the only possible message here is SDL_QUIT */
858 #elif defined(PLATFORM_UNIX)
859   if ((event->window == window->drawable) &&
860       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
861     return TRUE;
862 #endif
863
864   return FALSE;
865 }
866
867
868 /* ========================================================================= */
869 /* joystick functions                                                        */
870 /* ========================================================================= */
871
872 inline void InitJoysticks()
873 {
874   int i;
875
876 #ifdef NO_JOYSTICK
877   return;       /* joysticks generally deactivated by compile-time directive */
878 #endif
879
880   /* always start with reliable default values */
881   joystick.status = JOYSTICK_NOT_AVAILABLE;
882   for (i=0; i<MAX_PLAYERS; i++)
883     joystick.fd[i] = -1;                /* joystick device closed */
884
885 #if defined(TARGET_SDL)
886   SDLInitJoysticks();
887 #elif defined(PLATFORM_UNIX)
888   UnixInitJoysticks();
889 #elif defined(PLATFORM_MSDOS)
890   MSDOSInitJoysticks();
891 #endif
892 }
893
894 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
895 {
896 #if defined(TARGET_SDL)
897   return SDLReadJoystick(nr, x, y, b1, b2);
898 #elif defined(PLATFORM_UNIX)
899   return UnixReadJoystick(nr, x, y, b1, b2);
900 #elif defined(PLATFORM_MSDOS)
901   return MSDOSReadJoystick(nr, x, y, b1, b2);
902 #endif
903 }