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