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