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