rnd-20030403-3-src
[rocksndiamonds.git] / src / libgame / system.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2002 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * system.c                                                 *
12 ***********************************************************/
13
14 #include <string.h>
15 #include <signal.h>
16
17 #include "platform.h"
18
19 #if defined(PLATFORM_MSDOS)
20 #include <fcntl.h>
21 #endif
22
23 #include "system.h"
24 #include "sound.h"
25 #include "setup.h"
26 #include "joystick.h"
27 #include "misc.h"
28
29
30 /* ========================================================================= */
31 /* exported variables                                                        */
32 /* ========================================================================= */
33
34 struct ProgramInfo      program;
35 struct OptionInfo       options;
36 struct VideoSystemInfo  video;
37 struct AudioSystemInfo  audio;
38 struct GfxInfo          gfx;
39 struct ArtworkInfo      artwork;
40 struct JoystickInfo     joystick;
41 struct SetupInfo        setup;
42
43 LevelDirTree           *leveldir_first = NULL;
44 LevelDirTree           *leveldir_current = NULL;
45 int                     level_nr;
46
47 Display                *display = NULL;
48 Visual                 *visual = NULL;
49 int                     screen = 0;
50 Colormap                cmap = None;
51
52 DrawWindow             *window = NULL;
53 DrawBuffer             *backbuffer = NULL;
54 DrawBuffer             *drawto = NULL;
55
56 int                     button_status = MB_NOT_PRESSED;
57 boolean                 motion_status = FALSE;
58
59 int                     redraw_mask = REDRAW_NONE;
60 int                     redraw_tiles = 0;
61
62 int                     FrameCounter = 0;
63
64
65 /* ========================================================================= */
66 /* init/close functions                                                      */
67 /* ========================================================================= */
68
69 void InitProgramInfo(char *argv0,
70                      char *userdata_directory, char *program_title,
71                      char *window_title, char *icon_title,
72                      char *x11_icon_filename, char *x11_iconmask_filename,
73                      char *msdos_pointer_filename,
74                      char *cookie_prefix, char *filename_prefix,
75                      int program_version)
76 {
77   program.command_basename =
78     (strrchr(argv0, '/') ? strrchr(argv0, '/') + 1 : argv0);
79
80   program.userdata_directory = userdata_directory;
81   program.program_title = program_title;
82   program.window_title = window_title;
83   program.icon_title = icon_title;
84   program.x11_icon_filename = x11_icon_filename;
85   program.x11_iconmask_filename = x11_iconmask_filename;
86   program.msdos_pointer_filename = msdos_pointer_filename;
87
88   program.cookie_prefix = cookie_prefix;
89   program.filename_prefix = filename_prefix;
90
91   program.version_major = VERSION_MAJOR(program_version);
92   program.version_minor = VERSION_MINOR(program_version);
93   program.version_patch = VERSION_PATCH(program_version);
94 }
95
96 void InitExitFunction(void (*exit_function)(int))
97 {
98   program.exit_function = exit_function;
99
100   /* set signal handlers to custom exit function */
101   signal(SIGINT, exit_function);
102   signal(SIGTERM, exit_function);
103
104 #if defined(TARGET_SDL)
105   /* set exit function to automatically cleanup SDL stuff after exit() */
106   atexit(SDL_Quit);
107 #endif
108 }
109
110 void InitPlatformDependantStuff(void)
111 {
112 #if defined(PLATFORM_MSDOS)
113   _fmode = O_BINARY;
114   initErrorFile();
115 #endif
116
117 #if defined(TARGET_SDL)
118   if (SDL_Init(SDL_INIT_EVENTTHREAD | SDL_INIT_NOPARACHUTE) < 0)
119     Error(ERR_EXIT, "SDL_Init() failed: %s", SDL_GetError());
120 #endif
121 }
122
123 void ClosePlatformDependantStuff(void)
124 {
125 #if defined(PLATFORM_MSDOS)
126   dumpErrorFile();
127 #endif
128 }
129
130 void InitGfxFieldInfo(int sx, int sy, int sxsize, int sysize,
131                       int real_sx, int real_sy,
132                       int full_sxsize, int full_sysize,
133                       Bitmap *field_save_buffer)
134 {
135   gfx.sx = sx;
136   gfx.sy = sy;
137   gfx.sxsize = sxsize;
138   gfx.sysize = sysize;
139   gfx.real_sx = real_sx;
140   gfx.real_sy = real_sy;
141   gfx.full_sxsize = full_sxsize;
142   gfx.full_sysize = full_sysize;
143
144   gfx.field_save_buffer = field_save_buffer;
145
146   gfx.background_bitmap = NULL;
147   gfx.background_bitmap_mask = REDRAW_NONE;
148
149   SetDrawDeactivationMask(REDRAW_NONE);         /* do not deactivate drawing */
150   SetDrawBackgroundMask(REDRAW_NONE);           /* deactivate masked drawing */
151 }
152
153 void InitGfxDoor1Info(int dx, int dy, int dxsize, int dysize)
154 {
155   gfx.dx = dx;
156   gfx.dy = dy;
157   gfx.dxsize = dxsize;
158   gfx.dysize = dysize;
159 }
160
161 void InitGfxDoor2Info(int vx, int vy, int vxsize, int vysize)
162 {
163   gfx.vx = vx;
164   gfx.vy = vy;
165   gfx.vxsize = vxsize;
166   gfx.vysize = vysize;
167 }
168
169 void InitGfxScrollbufferInfo(int scrollbuffer_width, int scrollbuffer_height)
170 {
171   /* currently only used by MSDOS code to alloc VRAM buffer, if available */
172   gfx.scrollbuffer_width = scrollbuffer_width;
173   gfx.scrollbuffer_height = scrollbuffer_height;
174 }
175
176 void SetDrawDeactivationMask(int draw_deactivation_mask)
177 {
178   gfx.draw_deactivation_mask = draw_deactivation_mask;
179 }
180
181 void SetDrawBackgroundMask(int draw_background_mask)
182 {
183   gfx.draw_background_mask = draw_background_mask;
184 }
185
186 static void DrawBitmapFromTile(Bitmap *bitmap, Bitmap *tile,
187                                int dest_x, int dest_y, int width, int height)
188 {
189   int bitmap_xsize = width;
190   int bitmap_ysize = height;
191   int tile_xsize = tile->width;
192   int tile_ysize = tile->height;
193   int tile_xsteps = (bitmap_xsize + tile_xsize - 1) / tile_xsize;
194   int tile_ysteps = (bitmap_ysize + tile_ysize - 1) / tile_ysize;
195   int x, y;
196
197   for (y=0; y < tile_ysteps; y++)
198   {
199     for (x=0; x < tile_xsteps; x++)
200     {
201       int draw_x = dest_x + x * tile_xsize;
202       int draw_y = dest_y + y * tile_ysize;
203       int draw_xsize = MIN(tile_xsize, bitmap_xsize - x * tile_xsize);
204       int draw_ysize = MIN(tile_ysize, bitmap_ysize - y * tile_ysize);
205
206       BlitBitmap(tile, bitmap, 0, 0, draw_xsize, draw_ysize, draw_x, draw_y);
207     }
208   }
209 }
210
211 void SetBackgroundBitmap(Bitmap *background_bitmap_tile, int mask)
212 {
213   static Bitmap *main_bitmap_tile = NULL;
214   static Bitmap *door_bitmap_tile = NULL;
215
216   if (mask == REDRAW_FIELD)
217   {
218     if (background_bitmap_tile == main_bitmap_tile)
219       return;           /* main background tile has not changed */
220
221     main_bitmap_tile = background_bitmap_tile;
222   }
223   else if (mask == REDRAW_DOOR_1)
224   {
225     if (background_bitmap_tile == door_bitmap_tile)
226       return;   /* main background tile has not changed */
227
228     door_bitmap_tile = background_bitmap_tile;
229   }
230   else          /* should not happen */
231     return;
232
233   if (background_bitmap_tile)
234     gfx.background_bitmap_mask |= mask;
235   else
236     gfx.background_bitmap_mask &= ~mask;
237
238   if (gfx.background_bitmap == NULL)
239     gfx.background_bitmap = CreateBitmap(video.width, video.height,
240                                          DEFAULT_DEPTH);
241
242   if (background_bitmap_tile == NULL)   /* empty background requested */
243     return;
244
245   if (mask == REDRAW_FIELD)
246     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
247                        gfx.real_sx, gfx.real_sy,
248                        gfx.full_sxsize, gfx.full_sysize);
249   else
250     DrawBitmapFromTile(gfx.background_bitmap, background_bitmap_tile,
251                        gfx.dx, gfx.dy,
252                        gfx.dxsize, gfx.dysize);
253 }
254
255 void SetMainBackgroundBitmap(Bitmap *background_bitmap_tile)
256 {
257   SetBackgroundBitmap(background_bitmap_tile, REDRAW_FIELD);
258 }
259
260 void SetDoorBackgroundBitmap(Bitmap *background_bitmap_tile)
261 {
262   SetBackgroundBitmap(background_bitmap_tile, REDRAW_DOOR_1);
263 }
264
265
266 /* ========================================================================= */
267 /* video functions                                                           */
268 /* ========================================================================= */
269
270 inline static int GetRealDepth(int depth)
271 {
272   return (depth == DEFAULT_DEPTH ? video.default_depth : depth);
273 }
274
275 inline static void sysFillRectangle(Bitmap *bitmap, int x, int y,
276                                int width, int height, Pixel color)
277 {
278 #ifdef TARGET_SDL
279   SDLFillRectangle(bitmap, x, y, width, height, color);
280 #else
281   X11FillRectangle(bitmap, x, y, width, height, color);
282 #endif
283 }
284
285 inline static void sysCopyArea(Bitmap *src_bitmap, Bitmap *dst_bitmap,
286                                int src_x, int src_y, int width, int height,
287                                int dst_x, int dst_y, int mask_mode)
288 {
289 #ifdef TARGET_SDL
290   SDLCopyArea(src_bitmap, dst_bitmap,
291               src_x, src_y, width, height, dst_x, dst_y, mask_mode);
292 #else
293   X11CopyArea(src_bitmap, dst_bitmap,
294               src_x, src_y, width, height, dst_x, dst_y, mask_mode);
295 #endif
296 }
297
298 inline void InitVideoDisplay(void)
299 {
300 #if defined(TARGET_SDL)
301   SDLInitVideoDisplay();
302 #else
303   X11InitVideoDisplay();
304 #endif
305 }
306
307 inline void CloseVideoDisplay(void)
308 {
309   KeyboardAutoRepeatOn();
310
311 #if defined(TARGET_SDL)
312   SDL_QuitSubSystem(SDL_INIT_VIDEO);
313 #else
314
315   if (display)
316     XCloseDisplay(display);
317 #endif
318 }
319
320 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
321                             int width, int height, int depth,
322                             boolean fullscreen)
323 {
324   video.width = width;
325   video.height = height;
326   video.depth = GetRealDepth(depth);
327   video.fullscreen_available = FULLSCREEN_STATUS;
328   video.fullscreen_enabled = FALSE;
329
330 #ifdef TARGET_SDL
331   SDLInitVideoBuffer(backbuffer, window, fullscreen);
332 #else
333   X11InitVideoBuffer(backbuffer, window);
334 #endif
335 }
336
337 inline Bitmap *CreateBitmapStruct(void)
338 {
339 #ifdef TARGET_SDL
340   return checked_calloc(sizeof(struct SDLSurfaceInfo));
341 #else
342   return checked_calloc(sizeof(struct X11DrawableInfo));
343 #endif
344 }
345
346 inline Bitmap *CreateBitmap(int width, int height, int depth)
347 {
348   Bitmap *new_bitmap = CreateBitmapStruct();
349   int real_depth = GetRealDepth(depth);
350
351 #ifdef TARGET_SDL
352   SDL_Surface *surface_tmp, *surface_native;
353
354   if ((surface_tmp = SDL_CreateRGBSurface(SURFACE_FLAGS, width, height,
355                                           real_depth, 0, 0, 0, 0))
356       == NULL)
357     Error(ERR_EXIT, "SDL_CreateRGBSurface() failed: %s", SDL_GetError());
358
359   if ((surface_native = SDL_DisplayFormat(surface_tmp)) == NULL)
360     Error(ERR_EXIT, "SDL_DisplayFormat() failed: %s", SDL_GetError());
361
362   SDL_FreeSurface(surface_tmp);
363
364   new_bitmap->surface = surface_native;
365 #else
366   Pixmap pixmap;
367
368   if ((pixmap = XCreatePixmap(display, window->drawable,
369                               width, height, real_depth))
370       == None)
371     Error(ERR_EXIT, "cannot create pixmap");
372
373   new_bitmap->drawable = pixmap;
374
375   if (window == NULL)
376     Error(ERR_EXIT, "Window GC needed for Bitmap -- create Window first");
377
378   new_bitmap->gc = window->gc;
379
380   new_bitmap->line_gc[0] = window->line_gc[0];
381   new_bitmap->line_gc[1] = window->line_gc[1];
382 #endif
383
384   new_bitmap->width = width;
385   new_bitmap->height = height;
386
387   return new_bitmap;
388 }
389
390 inline static void FreeBitmapPointers(Bitmap *bitmap)
391 {
392   if (bitmap == NULL)
393     return;
394
395 #ifdef TARGET_SDL
396   if (bitmap->surface)
397     SDL_FreeSurface(bitmap->surface);
398   if (bitmap->surface_masked)
399     SDL_FreeSurface(bitmap->surface_masked);
400   bitmap->surface = NULL;
401   bitmap->surface_masked = NULL;
402 #else
403   /* The X11 version seems to have a memory leak here -- although
404      "XFreePixmap()" is called, the corresponding memory seems not
405      to be freed (according to "ps"). The SDL version apparently
406      does not have this problem. */
407
408   if (bitmap->drawable)
409     XFreePixmap(display, bitmap->drawable);
410   if (bitmap->clip_mask)
411     XFreePixmap(display, bitmap->clip_mask);
412   if (bitmap->stored_clip_gc)
413     XFreeGC(display, bitmap->stored_clip_gc);
414   /* the other GCs are only pointers to GCs used elsewhere */
415   bitmap->drawable = None;
416   bitmap->clip_mask = None;
417   bitmap->stored_clip_gc = None;
418 #endif
419
420   if (bitmap->source_filename)
421     free(bitmap->source_filename);
422   bitmap->source_filename = NULL;
423 }
424
425 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
426                                           Bitmap *dst_bitmap)
427 {
428   if (src_bitmap == NULL || dst_bitmap == NULL)
429     return;
430
431   FreeBitmapPointers(dst_bitmap);
432
433   *dst_bitmap = *src_bitmap;
434 }
435
436 inline void FreeBitmap(Bitmap *bitmap)
437 {
438   if (bitmap == NULL)
439     return;
440
441   FreeBitmapPointers(bitmap);
442
443   free(bitmap);
444 }
445
446 inline void CloseWindow(DrawWindow *window)
447 {
448 #ifdef TARGET_X11
449   if (window->drawable)
450   {
451     XUnmapWindow(display, window->drawable);
452     XDestroyWindow(display, window->drawable);
453   }
454   if (window->gc)
455     XFreeGC(display, window->gc);
456 #endif
457 }
458
459 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
460                                        int draw_mask)
461 {
462   if (draw_mask == REDRAW_NONE)
463     return FALSE;
464
465   if (draw_mask & REDRAW_ALL)
466     return TRUE;
467
468   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
469     return TRUE;
470
471   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
472     return TRUE;
473
474   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
475     return TRUE;
476
477   return FALSE;
478 }
479
480 inline boolean DrawingDeactivated(int x, int y, int width, int height)
481 {
482   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
483 }
484
485 inline boolean DrawingOnBackground(int x, int y)
486 {
487   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
488           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
489 }
490
491 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
492                        int src_x, int src_y, int width, int height,
493                        int dst_x, int dst_y)
494 {
495   if (DrawingDeactivated(dst_x, dst_y, width, height))
496     return;
497
498   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
499               dst_x, dst_y, BLIT_OPAQUE);
500 }
501
502 inline void FillRectangle(Bitmap *bitmap, int x, int y, int width, int height,
503                           Pixel color)
504 {
505   if (DrawingDeactivated(x, y, width, height))
506     return;
507
508   sysFillRectangle(bitmap, x, y, width, height, color);
509 }
510
511 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
512 {
513   FillRectangle(bitmap, x, y, width, height, BlackPixel(display, screen));
514 }
515
516 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
517                                        int width, int height)
518 {
519   if (DrawingOnBackground(x, y))
520     BlitBitmap(gfx.background_bitmap, bitmap, x, y, width, height, x, y);
521   else
522     ClearRectangle(bitmap, x, y, width, height);
523 }
524
525 #if 0
526 #ifndef TARGET_SDL
527 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
528 #endif
529 #endif
530
531 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
532 {
533 #ifdef TARGET_X11
534   if (clip_gc)
535   {
536     bitmap->clip_gc = clip_gc;
537     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
538   }
539 #if 0
540   last_clip_gc = clip_gc;
541 #endif
542 #endif
543 }
544
545 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
546 {
547 #ifdef TARGET_X11
548   if (clip_gc)
549   {
550     bitmap->clip_gc = clip_gc;
551     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
552   }
553 #if 0
554   last_clip_gc = clip_gc;
555 #endif
556 #endif
557 }
558
559 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
560                              int src_x, int src_y,
561                              int width, int height,
562                              int dst_x, int dst_y)
563 {
564   if (DrawingDeactivated(dst_x, dst_y, width, height))
565     return;
566
567   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
568               dst_x, dst_y, BLIT_MASKED);
569 }
570
571 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
572                                    int src_x, int src_y,
573                                    int width, int height,
574                                    int dst_x, int dst_y)
575 {
576   if (DrawingOnBackground(dst_x, dst_y))
577   {
578     /* draw background */
579     BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
580                dst_x, dst_y);
581
582     /* draw foreground */
583     SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
584                   dst_x - src_x, dst_y - src_y);
585     BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
586                      dst_x, dst_y);
587   }
588   else
589     BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
590                dst_x, dst_y);
591 }
592
593 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
594                                 int to_x, int to_y)
595 {
596 #ifdef TARGET_SDL
597   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, 0xffffff);
598 #else
599   XSetForeground(display, bitmap->gc, WhitePixel(display, screen));
600   XDrawLine(display, bitmap->drawable, bitmap->gc, from_x, from_y, to_x, to_y);
601 #endif
602 }
603
604 #if !defined(TARGET_X11_NATIVE)
605 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
606                      int to_x, int to_y, Pixel pixel, int line_width)
607 {
608   int x, y;
609
610   for (x=0; x<line_width; x++)
611   {
612     for (y=0; y<line_width; y++)
613     {
614       int dx = x - line_width / 2;
615       int dy = y - line_width / 2;
616
617       if ((x == 0 && y == 0) ||
618           (x == 0 && y == line_width - 1) ||
619           (x == line_width - 1 && y == 0) ||
620           (x == line_width - 1 && y == line_width - 1))
621         continue;
622
623 #if defined(TARGET_SDL)
624       SDLDrawLine(bitmap,
625                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
626 #elif defined(TARGET_ALLEGRO)
627       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
628                       to_x + dx, to_y + dy, pixel);
629 #endif
630     }
631   }
632 }
633 #endif
634
635 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
636                       Pixel pixel)
637 {
638 #if !defined(TARGET_X11_NATIVE)
639   int line_width = 4;
640   int i;
641
642   for (i=0; i<num_points - 1; i++)
643     DrawLine(bitmap, points[i].x, points[i].y,
644              points[i + 1].x, points[i + 1].y, pixel, line_width);
645
646   /*
647   SDLDrawLines(bitmap->surface, points, num_points, pixel);
648   */
649 #else
650   XSetForeground(display, bitmap->line_gc[1], pixel);
651   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
652              (XPoint *)points, num_points, CoordModeOrigin);
653 #endif
654 }
655
656 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
657 {
658 #if defined(TARGET_SDL)
659   return SDLGetPixel(bitmap, x, y);
660 #elif defined(TARGET_ALLEGRO)
661   return AllegroGetPixel(bitmap->drawable, x, y);
662 #else
663   return X11GetPixel(bitmap, x, y);
664 #endif
665 }
666
667 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
668                              unsigned int color_g, unsigned int color_b)
669 {
670 #if defined(TARGET_SDL)
671   return SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
672 #elif defined(TARGET_ALLEGRO)
673   return AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
674 #else
675   return X11GetPixelFromRGB(color_r, color_g, color_b);
676 #endif
677 }
678
679 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
680 {
681   unsigned int color_r = (color >> 16) & 0xff;
682   unsigned int color_g = (color >>  8) & 0xff;
683   unsigned int color_b = (color >>  0) & 0xff;
684
685   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
686 }
687
688 /* execute all pending screen drawing operations */
689 inline void FlushDisplay(void)
690 {
691 #ifndef TARGET_SDL
692   XFlush(display);
693 #endif
694 }
695
696 /* execute and wait for all pending screen drawing operations */
697 inline void SyncDisplay(void)
698 {
699 #ifndef TARGET_SDL
700   XSync(display, FALSE);
701 #endif
702 }
703
704 inline void KeyboardAutoRepeatOn(void)
705 {
706 #ifdef TARGET_SDL
707   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
708                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
709   SDL_EnableUNICODE(1);
710 #else
711   if (display)
712     XAutoRepeatOn(display);
713 #endif
714 }
715
716 inline void KeyboardAutoRepeatOff(void)
717 {
718 #ifdef TARGET_SDL
719   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
720   SDL_EnableUNICODE(0);
721 #else
722   if (display)
723     XAutoRepeatOff(display);
724 #endif
725 }
726
727 inline boolean PointerInWindow(DrawWindow *window)
728 {
729 #ifdef TARGET_SDL
730   return TRUE;
731 #else
732   Window root, child;
733   int root_x, root_y;
734   unsigned int mask;
735   int win_x, win_y;
736
737   /* if XQueryPointer() returns False, the pointer
738      is not on the same screen as the specified window */
739   return XQueryPointer(display, window->drawable, &root, &child,
740                        &root_x, &root_y, &win_x, &win_y, &mask);
741 #endif
742 }
743
744 inline boolean SetVideoMode(boolean fullscreen)
745 {
746 #ifdef TARGET_SDL
747   return SDLSetVideoMode(&backbuffer, fullscreen);
748 #else
749   boolean success = TRUE;
750
751   if (fullscreen && video.fullscreen_available)
752   {
753     Error(ERR_WARN, "fullscreen not available in X11 version");
754
755     /* display error message only once */
756     video.fullscreen_available = FALSE;
757
758     success = FALSE;
759   }
760
761   return success;
762 #endif
763 }
764
765 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
766 {
767 #ifdef TARGET_SDL
768   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
769       (!fullscreen && video.fullscreen_enabled))
770     fullscreen = SetVideoMode(fullscreen);
771 #endif
772
773   return fullscreen;
774 }
775
776 Bitmap *LoadImage(char *filename)
777 {
778   Bitmap *new_bitmap;
779
780 #if defined(TARGET_SDL)
781   new_bitmap = SDLLoadImage(filename);
782 #else
783   new_bitmap = X11LoadImage(filename);
784 #endif
785
786   if (new_bitmap)
787     new_bitmap->source_filename = getStringCopy(filename);
788
789   return new_bitmap;
790 }
791
792 Bitmap *LoadCustomImage(char *basename)
793 {
794   char *filename = getCustomImageFilename(basename);
795   Bitmap *new_bitmap;
796
797   if (filename == NULL)
798     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
799
800   if ((new_bitmap = LoadImage(filename)) == NULL)
801     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
802
803   return new_bitmap;
804 }
805
806 void ReloadCustomImage(Bitmap *bitmap, char *basename)
807 {
808   char *filename = getCustomImageFilename(basename);
809   Bitmap *new_bitmap;
810
811   if (filename == NULL)         /* (should never happen) */
812   {
813     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
814     return;
815   }
816
817   if (strcmp(filename, bitmap->source_filename) == 0)
818   {
819     /* The old and new image are the same (have the same filename and path).
820        This usually means that this image does not exist in this graphic set
821        and a fallback to the existing image is done. */
822
823     return;
824   }
825
826   if ((new_bitmap = LoadImage(filename)) == NULL)
827   {
828     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
829     return;
830   }
831
832   if (bitmap->width != new_bitmap->width ||
833       bitmap->height != new_bitmap->height)
834   {
835     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
836           filename);
837     FreeBitmap(new_bitmap);
838     return;
839   }
840
841   TransferBitmapPointers(new_bitmap, bitmap);
842   free(new_bitmap);
843 }
844
845 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
846 {
847   Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
848
849 #if defined(TARGET_SDL)
850   SDLZoomBitmap(src_bitmap, dst_bitmap);
851 #else
852   X11ZoomBitmap(src_bitmap, dst_bitmap);
853 #endif
854
855   return dst_bitmap;
856 }
857
858 void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap)
859 {
860   Bitmap *tmp_bitmap, *tmp_bitmap_2, *tmp_bitmap_8;
861   int src_width, src_height;
862   int tmp_width, tmp_height;
863
864   src_width  = src_bitmap->width;
865   src_height = src_bitmap->height;
866
867   tmp_width  = src_width;
868   tmp_height = src_height + src_height / 2;
869
870   tmp_bitmap = CreateBitmap(tmp_width, tmp_height, DEFAULT_DEPTH);
871
872   tmp_bitmap_2 = ZoomBitmap(src_bitmap, src_width / 2, src_height / 2);
873   tmp_bitmap_8 = ZoomBitmap(src_bitmap, src_width / 8, src_height / 8);
874
875   BlitBitmap(src_bitmap, tmp_bitmap, 0, 0, src_width, src_height, 0, 0);
876   BlitBitmap(tmp_bitmap_2, tmp_bitmap, 0, 0, src_width / 2, src_height / 2,
877              0, src_height);
878   BlitBitmap(tmp_bitmap_8, tmp_bitmap, 0, 0, src_width / 8, src_height / 8,
879              3 * src_width / 4, src_height);
880
881   FreeBitmap(tmp_bitmap_2);
882   FreeBitmap(tmp_bitmap_8);
883
884 #ifdef TARGET_SDL
885   src_bitmap->surface = tmp_bitmap->surface;
886   tmp_bitmap->surface = NULL;
887 #else
888   src_bitmap->drawable = tmp_bitmap->drawable;
889   tmp_bitmap->drawable = None;
890 #endif
891
892   src_bitmap->height = tmp_bitmap->height;
893
894   FreeBitmap(tmp_bitmap);
895 }
896
897
898 /* ========================================================================= */
899 /* audio functions                                                           */
900 /* ========================================================================= */
901
902 inline void OpenAudio(void)
903 {
904   /* always start with reliable default values */
905   audio.sound_available = FALSE;
906   audio.music_available = FALSE;
907   audio.loops_available = FALSE;
908
909   audio.sound_enabled = FALSE;
910   audio.sound_deactivated = FALSE;
911
912   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
913   audio.mixer_pid = 0;
914   audio.device_name = NULL;
915   audio.device_fd = -1;
916
917   audio.num_channels = 0;
918   audio.music_channel = 0;
919   audio.first_sound_channel = 0;
920
921 #if defined(TARGET_SDL)
922   SDLOpenAudio();
923 #elif defined(PLATFORM_UNIX)
924   UnixOpenAudio();
925 #elif defined(PLATFORM_MSDOS)
926   MSDOSOpenAudio();
927 #endif
928 }
929
930 inline void CloseAudio(void)
931 {
932 #if defined(TARGET_SDL)
933   SDLCloseAudio();
934 #elif defined(PLATFORM_UNIX)
935   UnixCloseAudio();
936 #elif defined(PLATFORM_MSDOS)
937   MSDOSCloseAudio();
938 #endif
939
940   audio.sound_enabled = FALSE;
941 }
942
943 inline void SetAudioMode(boolean enabled)
944 {
945   if (!audio.sound_available)
946     return;
947
948   audio.sound_enabled = enabled;
949 }
950
951
952 /* ========================================================================= */
953 /* event functions                                                           */
954 /* ========================================================================= */
955
956 inline void InitEventFilter(EventFilter filter_function)
957 {
958 #ifdef TARGET_SDL
959   /* set event filter to filter out certain events */
960   SDL_SetEventFilter(filter_function);
961 #endif
962 }
963
964 inline boolean PendingEvent(void)
965 {
966 #ifdef TARGET_SDL
967   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
968 #else
969   return (XPending(display) ? TRUE : FALSE);
970 #endif
971 }
972
973 inline void NextEvent(Event *event)
974 {
975 #ifdef TARGET_SDL
976   SDLNextEvent(event);
977 #else
978   XNextEvent(display, event);
979 #endif
980 }
981
982 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
983 {
984 #ifdef TARGET_SDL
985 #if 0
986   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
987          (int)event->keysym.unicode,
988          (int)event->keysym.sym,
989          (int)SDL_GetModState());
990 #endif
991
992   if (with_modifiers &&
993       event->keysym.unicode > 0x0000 &&
994       event->keysym.unicode < 0x2000)
995     return event->keysym.unicode;
996   else
997     return event->keysym.sym;
998 #else
999 #if 0
1000   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
1001          (int)XLookupKeysym(event, event->state),
1002          (int)XLookupKeysym(event, 0));
1003 #endif
1004
1005   if (with_modifiers)
1006     return XLookupKeysym(event, event->state);
1007   else
1008     return XLookupKeysym(event, 0);
1009 #endif
1010 }
1011
1012 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
1013 {
1014   if (event->type != EVENT_CLIENTMESSAGE)
1015     return FALSE;
1016
1017 #if defined(TARGET_SDL)
1018   return TRUE;          /* the only possible message here is SDL_QUIT */
1019 #elif defined(PLATFORM_UNIX)
1020   if ((event->window == window->drawable) &&
1021       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
1022     return TRUE;
1023 #endif
1024
1025   return FALSE;
1026 }
1027
1028
1029 /* ========================================================================= */
1030 /* joystick functions                                                        */
1031 /* ========================================================================= */
1032
1033 inline void InitJoysticks()
1034 {
1035   int i;
1036
1037 #ifdef NO_JOYSTICK
1038   return;       /* joysticks generally deactivated by compile-time directive */
1039 #endif
1040
1041   /* always start with reliable default values */
1042   joystick.status = JOYSTICK_NOT_AVAILABLE;
1043   for (i=0; i<MAX_PLAYERS; i++)
1044     joystick.fd[i] = -1;                /* joystick device closed */
1045
1046 #if defined(TARGET_SDL)
1047   SDLInitJoysticks();
1048 #elif defined(PLATFORM_UNIX)
1049   UnixInitJoysticks();
1050 #elif defined(PLATFORM_MSDOS)
1051   MSDOSInitJoysticks();
1052 #endif
1053 }
1054
1055 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1056 {
1057 #if defined(TARGET_SDL)
1058   return SDLReadJoystick(nr, x, y, b1, b2);
1059 #elif defined(PLATFORM_UNIX)
1060   return UnixReadJoystick(nr, x, y, b1, b2);
1061 #elif defined(PLATFORM_MSDOS)
1062   return MSDOSReadJoystick(nr, x, y, b1, b2);
1063 #endif
1064 }