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