rnd-20040129-2-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_cursor_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_cursor_filename = msdos_cursor_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 InitPlatformDependentStuff(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 ClosePlatformDependentStuff(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 #if defined(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 #if defined(TARGET_SDL)
290   SDLCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
291               dst_x, dst_y, mask_mode);
292 #else
293   X11CopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
294               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   if (display)
315     XCloseDisplay(display);
316 #endif
317 }
318
319 inline void InitVideoBuffer(DrawBuffer **backbuffer, DrawWindow **window,
320                             int width, int height, int depth,
321                             boolean fullscreen)
322 {
323   video.width = width;
324   video.height = height;
325   video.depth = GetRealDepth(depth);
326   video.fullscreen_available = FULLSCREEN_STATUS;
327   video.fullscreen_enabled = FALSE;
328
329 #if defined(TARGET_SDL)
330   SDLInitVideoBuffer(backbuffer, window, fullscreen);
331 #else
332   X11InitVideoBuffer(backbuffer, window);
333 #endif
334 }
335
336 inline Bitmap *CreateBitmapStruct(void)
337 {
338 #if defined(TARGET_SDL)
339   return checked_calloc(sizeof(struct SDLSurfaceInfo));
340 #else
341   return checked_calloc(sizeof(struct X11DrawableInfo));
342 #endif
343 }
344
345 inline Bitmap *CreateBitmap(int width, int height, int depth)
346 {
347   Bitmap *new_bitmap = CreateBitmapStruct();
348   int real_depth = GetRealDepth(depth);
349
350 #if defined(TARGET_SDL)
351   SDLCreateBitmapContent(new_bitmap, width, height, real_depth);
352 #else
353   X11CreateBitmapContent(new_bitmap, width, height, real_depth);
354 #endif
355
356   new_bitmap->width = width;
357   new_bitmap->height = height;
358
359   return new_bitmap;
360 }
361
362 inline static void FreeBitmapPointers(Bitmap *bitmap)
363 {
364   if (bitmap == NULL)
365     return;
366
367 #if defined(TARGET_SDL)
368   SDLFreeBitmapPointers(bitmap);
369 #else
370   X11FreeBitmapPointers(bitmap);
371 #endif
372
373   checked_free(bitmap->source_filename);
374   bitmap->source_filename = NULL;
375 }
376
377 inline static void TransferBitmapPointers(Bitmap *src_bitmap,
378                                           Bitmap *dst_bitmap)
379 {
380   if (src_bitmap == NULL || dst_bitmap == NULL)
381     return;
382
383   FreeBitmapPointers(dst_bitmap);
384
385   *dst_bitmap = *src_bitmap;
386 }
387
388 inline void FreeBitmap(Bitmap *bitmap)
389 {
390   if (bitmap == NULL)
391     return;
392
393   FreeBitmapPointers(bitmap);
394
395   free(bitmap);
396 }
397
398 inline void CloseWindow(DrawWindow *window)
399 {
400 #if defined(TARGET_X11)
401   if (window->drawable)
402   {
403     XUnmapWindow(display, window->drawable);
404     XDestroyWindow(display, window->drawable);
405   }
406   if (window->gc)
407     XFreeGC(display, window->gc);
408 #endif
409 }
410
411 static inline boolean CheckDrawingArea(int x, int y, int width, int height,
412                                        int draw_mask)
413 {
414   if (draw_mask == REDRAW_NONE)
415     return FALSE;
416
417   if (draw_mask & REDRAW_ALL)
418     return TRUE;
419
420   if ((draw_mask & REDRAW_FIELD) && x < gfx.real_sx + gfx.full_sxsize)
421     return TRUE;
422
423   if ((draw_mask & REDRAW_DOOR_1) && x >= gfx.dx && y < gfx.dy + gfx.dysize)
424     return TRUE;
425
426   if ((draw_mask & REDRAW_DOOR_2) && x >= gfx.dx && y >= gfx.vy)
427     return TRUE;
428
429   return FALSE;
430 }
431
432 inline boolean DrawingDeactivated(int x, int y, int width, int height)
433 {
434   return CheckDrawingArea(x, y, width, height, gfx.draw_deactivation_mask);
435 }
436
437 inline boolean DrawingOnBackground(int x, int y)
438 {
439   return ((gfx.draw_background_mask & gfx.background_bitmap_mask) &&
440           CheckDrawingArea(x, y, 1, 1, gfx.draw_background_mask));
441 }
442
443 inline void BlitBitmap(Bitmap *src_bitmap, Bitmap *dst_bitmap,
444                        int src_x, int src_y, int width, int height,
445                        int dst_x, int dst_y)
446 {
447   if (DrawingDeactivated(dst_x, dst_y, width, height))
448     return;
449
450   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
451               dst_x, dst_y, BLIT_OPAQUE);
452 }
453
454 inline void FillRectangle(Bitmap *bitmap, int x, int y, int width, int height,
455                           Pixel color)
456 {
457   if (DrawingDeactivated(x, y, width, height))
458     return;
459
460   sysFillRectangle(bitmap, x, y, width, height, color);
461 }
462
463 inline void ClearRectangle(Bitmap *bitmap, int x, int y, int width, int height)
464 {
465   FillRectangle(bitmap, x, y, width, height, BLACK_PIXEL);
466 }
467
468 inline void ClearRectangleOnBackground(Bitmap *bitmap, int x, int y,
469                                        int width, int height)
470 {
471   if (DrawingOnBackground(x, y))
472     BlitBitmap(gfx.background_bitmap, bitmap, x, y, width, height, x, y);
473   else
474     ClearRectangle(bitmap, x, y, width, height);
475 }
476
477 #if 0
478 #ifndef TARGET_SDL
479 static GC last_clip_gc = 0;     /* needed for XCopyArea() through clip mask */
480 #endif
481 #endif
482
483 inline void SetClipMask(Bitmap *bitmap, GC clip_gc, Pixmap clip_pixmap)
484 {
485 #if defined(TARGET_X11)
486   if (clip_gc)
487   {
488     bitmap->clip_gc = clip_gc;
489     XSetClipMask(display, bitmap->clip_gc, clip_pixmap);
490   }
491 #if 0
492   last_clip_gc = clip_gc;
493 #endif
494 #endif
495 }
496
497 inline void SetClipOrigin(Bitmap *bitmap, GC clip_gc, int clip_x, int clip_y)
498 {
499 #if defined(TARGET_X11)
500   if (clip_gc)
501   {
502     bitmap->clip_gc = clip_gc;
503     XSetClipOrigin(display, bitmap->clip_gc, clip_x, clip_y);
504   }
505 #if 0
506   last_clip_gc = clip_gc;
507 #endif
508 #endif
509 }
510
511 inline void BlitBitmapMasked(Bitmap *src_bitmap, Bitmap *dst_bitmap,
512                              int src_x, int src_y,
513                              int width, int height,
514                              int dst_x, int dst_y)
515 {
516   if (DrawingDeactivated(dst_x, dst_y, width, height))
517     return;
518
519   sysCopyArea(src_bitmap, dst_bitmap, src_x, src_y, width, height,
520               dst_x, dst_y, BLIT_MASKED);
521 }
522
523 inline void BlitBitmapOnBackground(Bitmap *src_bitmap, Bitmap *dst_bitmap,
524                                    int src_x, int src_y,
525                                    int width, int height,
526                                    int dst_x, int dst_y)
527 {
528   if (DrawingOnBackground(dst_x, dst_y))
529   {
530     /* draw background */
531     BlitBitmap(gfx.background_bitmap, dst_bitmap, dst_x, dst_y, width, height,
532                dst_x, dst_y);
533
534     /* draw foreground */
535     SetClipOrigin(src_bitmap, src_bitmap->stored_clip_gc,
536                   dst_x - src_x, dst_y - src_y);
537     BlitBitmapMasked(src_bitmap, dst_bitmap, src_x, src_y, width, height,
538                      dst_x, dst_y);
539   }
540   else
541     BlitBitmap(src_bitmap, dst_bitmap, src_x, src_y, width, height,
542                dst_x, dst_y);
543 }
544
545 inline void DrawSimpleWhiteLine(Bitmap *bitmap, int from_x, int from_y,
546                                 int to_x, int to_y)
547 {
548 #if defined(TARGET_SDL)
549   SDLDrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
550 #else
551   X11DrawSimpleLine(bitmap, from_x, from_y, to_x, to_y, WHITE_PIXEL);
552 #endif
553 }
554
555 #if !defined(TARGET_X11_NATIVE)
556 inline void DrawLine(Bitmap *bitmap, int from_x, int from_y,
557                      int to_x, int to_y, Pixel pixel, int line_width)
558 {
559   int x, y;
560
561   for (x = 0; x < line_width; x++)
562   {
563     for (y = 0; y < line_width; y++)
564     {
565       int dx = x - line_width / 2;
566       int dy = y - line_width / 2;
567
568       if ((x == 0 && y == 0) ||
569           (x == 0 && y == line_width - 1) ||
570           (x == line_width - 1 && y == 0) ||
571           (x == line_width - 1 && y == line_width - 1))
572         continue;
573
574 #if defined(TARGET_SDL)
575       SDLDrawLine(bitmap,
576                   from_x + dx, from_y + dy, to_x + dx, to_y + dy, pixel);
577 #elif defined(TARGET_ALLEGRO)
578       AllegroDrawLine(bitmap->drawable, from_x + dx, from_y + dy,
579                       to_x + dx, to_y + dy, pixel);
580 #endif
581     }
582   }
583 }
584 #endif
585
586 inline void DrawLines(Bitmap *bitmap, struct XY *points, int num_points,
587                       Pixel pixel)
588 {
589 #if !defined(TARGET_X11_NATIVE)
590   int line_width = 4;
591   int i;
592
593   for (i = 0; i < num_points - 1; i++)
594     DrawLine(bitmap, points[i].x, points[i].y,
595              points[i + 1].x, points[i + 1].y, pixel, line_width);
596
597   /*
598   SDLDrawLines(bitmap->surface, points, num_points, pixel);
599   */
600 #else
601   XSetForeground(display, bitmap->line_gc[1], pixel);
602   XDrawLines(display, bitmap->drawable, bitmap->line_gc[1],
603              (XPoint *)points, num_points, CoordModeOrigin);
604 #endif
605 }
606
607 inline Pixel GetPixel(Bitmap *bitmap, int x, int y)
608 {
609   if (x < 0 || x >= bitmap->width ||
610       y < 0 || y >= bitmap->height)
611     return BLACK_PIXEL;
612
613 #if defined(TARGET_SDL)
614   return SDLGetPixel(bitmap, x, y);
615 #elif defined(TARGET_ALLEGRO)
616   return AllegroGetPixel(bitmap->drawable, x, y);
617 #else
618   return X11GetPixel(bitmap, x, y);
619 #endif
620 }
621
622 inline Pixel GetPixelFromRGB(Bitmap *bitmap, unsigned int color_r,
623                              unsigned int color_g, unsigned int color_b)
624 {
625 #if defined(TARGET_SDL)
626   return SDL_MapRGB(bitmap->surface->format, color_r, color_g, color_b);
627 #elif defined(TARGET_ALLEGRO)
628   return AllegroAllocColorCell(color_r << 8, color_g << 8, color_b << 8);
629 #else
630   return X11GetPixelFromRGB(color_r, color_g, color_b);
631 #endif
632 }
633
634 inline Pixel GetPixelFromRGBcompact(Bitmap *bitmap, unsigned int color)
635 {
636   unsigned int color_r = (color >> 16) & 0xff;
637   unsigned int color_g = (color >>  8) & 0xff;
638   unsigned int color_b = (color >>  0) & 0xff;
639
640   return GetPixelFromRGB(bitmap, color_r, color_g, color_b);
641 }
642
643 /* execute all pending screen drawing operations */
644 inline void FlushDisplay(void)
645 {
646 #ifndef TARGET_SDL
647   XFlush(display);
648 #endif
649 }
650
651 /* execute and wait for all pending screen drawing operations */
652 inline void SyncDisplay(void)
653 {
654 #ifndef TARGET_SDL
655   XSync(display, FALSE);
656 #endif
657 }
658
659 inline void KeyboardAutoRepeatOn(void)
660 {
661 #if defined(TARGET_SDL)
662   SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY / 2,
663                       SDL_DEFAULT_REPEAT_INTERVAL / 2);
664   SDL_EnableUNICODE(1);
665 #else
666   if (display)
667     XAutoRepeatOn(display);
668 #endif
669 }
670
671 inline void KeyboardAutoRepeatOff(void)
672 {
673 #if defined(TARGET_SDL)
674   SDL_EnableKeyRepeat(0, SDL_DEFAULT_REPEAT_INTERVAL);
675   SDL_EnableUNICODE(0);
676 #else
677   if (display)
678     XAutoRepeatOff(display);
679 #endif
680 }
681
682 inline boolean PointerInWindow(DrawWindow *window)
683 {
684 #if defined(TARGET_SDL)
685   return TRUE;
686 #else
687   Window root, child;
688   int root_x, root_y;
689   unsigned int mask;
690   int win_x, win_y;
691
692   /* if XQueryPointer() returns False, the pointer
693      is not on the same screen as the specified window */
694   return XQueryPointer(display, window->drawable, &root, &child,
695                        &root_x, &root_y, &win_x, &win_y, &mask);
696 #endif
697 }
698
699 inline boolean SetVideoMode(boolean fullscreen)
700 {
701 #if defined(TARGET_SDL)
702   return SDLSetVideoMode(&backbuffer, fullscreen);
703 #else
704   boolean success = TRUE;
705
706   if (fullscreen && video.fullscreen_available)
707   {
708     Error(ERR_WARN, "fullscreen not available in X11 version");
709
710     /* display error message only once */
711     video.fullscreen_available = FALSE;
712
713     success = FALSE;
714   }
715
716   return success;
717 #endif
718 }
719
720 inline boolean ChangeVideoModeIfNeeded(boolean fullscreen)
721 {
722 #if defined(TARGET_SDL)
723   if ((fullscreen && !video.fullscreen_enabled && video.fullscreen_available)||
724       (!fullscreen && video.fullscreen_enabled))
725     fullscreen = SetVideoMode(fullscreen);
726 #endif
727
728   return fullscreen;
729 }
730
731 Bitmap *LoadImage(char *filename)
732 {
733   Bitmap *new_bitmap;
734
735 #if defined(TARGET_SDL)
736   new_bitmap = SDLLoadImage(filename);
737 #else
738   new_bitmap = X11LoadImage(filename);
739 #endif
740
741   if (new_bitmap)
742     new_bitmap->source_filename = getStringCopy(filename);
743
744   return new_bitmap;
745 }
746
747 Bitmap *LoadCustomImage(char *basename)
748 {
749   char *filename = getCustomImageFilename(basename);
750   Bitmap *new_bitmap;
751
752   if (filename == NULL)
753     Error(ERR_EXIT, "LoadCustomImage(): cannot find file '%s'", basename);
754
755   if ((new_bitmap = LoadImage(filename)) == NULL)
756     Error(ERR_EXIT, "LoadImage() failed: %s", GetError());
757
758   return new_bitmap;
759 }
760
761 void ReloadCustomImage(Bitmap *bitmap, char *basename)
762 {
763   char *filename = getCustomImageFilename(basename);
764   Bitmap *new_bitmap;
765
766   if (filename == NULL)         /* (should never happen) */
767   {
768     Error(ERR_WARN, "ReloadCustomImage(): cannot find file '%s'", basename);
769     return;
770   }
771
772   if (strcmp(filename, bitmap->source_filename) == 0)
773   {
774     /* The old and new image are the same (have the same filename and path).
775        This usually means that this image does not exist in this graphic set
776        and a fallback to the existing image is done. */
777
778     return;
779   }
780
781   if ((new_bitmap = LoadImage(filename)) == NULL)
782   {
783     Error(ERR_WARN, "LoadImage() failed: %s", GetError());
784     return;
785   }
786
787   if (bitmap->width != new_bitmap->width ||
788       bitmap->height != new_bitmap->height)
789   {
790     Error(ERR_WARN, "ReloadCustomImage: new image '%s' has wrong dimensions",
791           filename);
792     FreeBitmap(new_bitmap);
793     return;
794   }
795
796   TransferBitmapPointers(new_bitmap, bitmap);
797   free(new_bitmap);
798 }
799
800 Bitmap *ZoomBitmap(Bitmap *src_bitmap, int zoom_width, int zoom_height)
801 {
802   Bitmap *dst_bitmap = CreateBitmap(zoom_width, zoom_height, DEFAULT_DEPTH);
803
804 #if defined(TARGET_SDL)
805   SDLZoomBitmap(src_bitmap, dst_bitmap);
806 #else
807   X11ZoomBitmap(src_bitmap, dst_bitmap);
808 #endif
809
810   return dst_bitmap;
811 }
812
813 void CreateBitmapWithSmallBitmaps(Bitmap *src_bitmap)
814 {
815   Bitmap *tmp_bitmap, *tmp_bitmap_2, *tmp_bitmap_8;
816   int src_width, src_height;
817   int tmp_width, tmp_height;
818
819   src_width  = src_bitmap->width;
820   src_height = src_bitmap->height;
821
822   tmp_width  = src_width;
823   tmp_height = src_height + (src_height + 1) / 2;     /* prevent odd height */
824
825   tmp_bitmap = CreateBitmap(tmp_width, tmp_height, DEFAULT_DEPTH);
826
827   tmp_bitmap_2 = ZoomBitmap(src_bitmap, src_width / 2, src_height / 2);
828   tmp_bitmap_8 = ZoomBitmap(src_bitmap, src_width / 8, src_height / 8);
829
830   BlitBitmap(src_bitmap, tmp_bitmap, 0, 0, src_width, src_height, 0, 0);
831   BlitBitmap(tmp_bitmap_2, tmp_bitmap, 0, 0, src_width / 2, src_height / 2,
832              0, src_height);
833   BlitBitmap(tmp_bitmap_8, tmp_bitmap, 0, 0, src_width / 8, src_height / 8,
834              3 * src_width / 4, src_height);
835
836   FreeBitmap(tmp_bitmap_2);
837   FreeBitmap(tmp_bitmap_8);
838
839 #if defined(TARGET_SDL)
840   /* !!! what about the old src_bitmap->surface ??? FIX ME !!! */
841   src_bitmap->surface = tmp_bitmap->surface;
842   tmp_bitmap->surface = NULL;
843 #else
844   /* !!! see above !!! */
845   src_bitmap->drawable = tmp_bitmap->drawable;
846   tmp_bitmap->drawable = None;
847 #endif
848
849   src_bitmap->height = tmp_bitmap->height;
850
851   FreeBitmap(tmp_bitmap);
852 }
853
854
855 /* ------------------------------------------------------------------------- */
856 /* mouse pointer functions                                                   */
857 /* ------------------------------------------------------------------------- */
858
859 #if !defined(PLATFORM_MSDOS)
860 /* XPM */
861 static const char *cursor_image_playfield[] =
862 {
863   /* width height num_colors chars_per_pixel */
864   "    16    16        3            1",
865
866   /* colors */
867   "X c #000000",
868   ". c #ffffff",
869   "  c None",
870
871 #if 1
872   /* some people complained about a "white dot" on the screen and thought it
873      was a graphical error... OK, let's just remove the whole pointer :-) */
874
875   /* pixels */
876   "                ",
877   "                ",
878   "                ",
879   "                ",
880   "                ",
881   "                ",
882   "                ",
883   "                ",
884   "                ",
885   "                ",
886   "                ",
887   "                ",
888   "                ",
889   "                ",
890   "                ",
891   "                ",
892
893   /* hot spot */
894   "0,0"
895 #else
896   /* pixels */
897   " X              ",
898   "X.X             ",
899   " X              ",
900   "                ",
901   "                ",
902   "                ",
903   "                ",
904   "                ",
905   "                ",
906   "                ",
907   "                ",
908   "                ",
909   "                ",
910   "                ",
911   "                ",
912   "                ",
913
914   /* hot spot */
915   "1,1"
916 #endif
917 };
918
919 #if defined(TARGET_SDL)
920 static const int cursor_bit_order = BIT_ORDER_MSB;
921 #elif defined(TARGET_X11_NATIVE)
922 static const int cursor_bit_order = BIT_ORDER_LSB;
923 #endif
924
925 static struct MouseCursorInfo *get_cursor_from_image(const char **image)
926 {
927   struct MouseCursorInfo *cursor;
928   boolean bit_order_msb = (cursor_bit_order == BIT_ORDER_MSB);
929   int header_lines = 4;
930   int x, y, i;
931
932   cursor = checked_calloc(sizeof(struct MouseCursorInfo));
933
934   sscanf(image[0], " %d %d ", &cursor->width, &cursor->height);
935
936   i = -1;
937   for (y = 0; y < cursor->width; y++)
938   {
939     for (x = 0; x < cursor->height; x++)
940     {
941       int bit_nr = x % 8;
942       int bit_mask = 0x01 << (bit_order_msb ? 7 - bit_nr : bit_nr );
943
944       if (bit_nr == 0)
945       {
946         i++;
947         cursor->data[i] = cursor->mask[i] = 0;
948       }
949
950       switch (image[header_lines + y][x])
951       {
952         case 'X':
953           cursor->data[i] |= bit_mask;
954           cursor->mask[i] |= bit_mask;
955           break;
956
957         case '.':
958           cursor->mask[i] |= bit_mask;
959           break;
960
961         case ' ':
962           break;
963       }
964     }
965   }
966
967   sscanf(image[header_lines + y], "%d,%d", &cursor->hot_x, &cursor->hot_y);
968
969   return cursor;
970 }
971 #endif  /* !PLATFORM_MSDOS */
972
973 void SetMouseCursor(int mode)
974 {
975 #if !defined(PLATFORM_MSDOS)
976   static struct MouseCursorInfo *cursor_playfield = NULL;
977
978   if (cursor_playfield == NULL)
979     cursor_playfield = get_cursor_from_image(cursor_image_playfield);
980
981 #if defined(TARGET_SDL)
982   SDLSetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
983 #elif defined(TARGET_X11_NATIVE)
984   X11SetMouseCursor(mode == CURSOR_PLAYFIELD ? cursor_playfield : NULL);
985 #endif
986 #endif
987 }
988
989
990 /* ========================================================================= */
991 /* audio functions                                                           */
992 /* ========================================================================= */
993
994 inline void OpenAudio(void)
995 {
996   /* always start with reliable default values */
997   audio.sound_available = FALSE;
998   audio.music_available = FALSE;
999   audio.loops_available = FALSE;
1000
1001   audio.sound_enabled = FALSE;
1002   audio.sound_deactivated = FALSE;
1003
1004   audio.mixer_pipe[0] = audio.mixer_pipe[1] = 0;
1005   audio.mixer_pid = 0;
1006   audio.device_name = NULL;
1007   audio.device_fd = -1;
1008
1009   audio.num_channels = 0;
1010   audio.music_channel = 0;
1011   audio.first_sound_channel = 0;
1012
1013 #if defined(TARGET_SDL)
1014   SDLOpenAudio();
1015 #elif defined(PLATFORM_UNIX)
1016   UnixOpenAudio();
1017 #elif defined(PLATFORM_MSDOS)
1018   MSDOSOpenAudio();
1019 #endif
1020 }
1021
1022 inline void CloseAudio(void)
1023 {
1024 #if defined(TARGET_SDL)
1025   SDLCloseAudio();
1026 #elif defined(PLATFORM_UNIX)
1027   UnixCloseAudio();
1028 #elif defined(PLATFORM_MSDOS)
1029   MSDOSCloseAudio();
1030 #endif
1031
1032   audio.sound_enabled = FALSE;
1033 }
1034
1035 inline void SetAudioMode(boolean enabled)
1036 {
1037   if (!audio.sound_available)
1038     return;
1039
1040   audio.sound_enabled = enabled;
1041 }
1042
1043
1044 /* ========================================================================= */
1045 /* event functions                                                           */
1046 /* ========================================================================= */
1047
1048 inline void InitEventFilter(EventFilter filter_function)
1049 {
1050 #if defined(TARGET_SDL)
1051   /* set event filter to filter out certain events */
1052   SDL_SetEventFilter(filter_function);
1053 #endif
1054 }
1055
1056 inline boolean PendingEvent(void)
1057 {
1058 #if defined(TARGET_SDL)
1059   return (SDL_PollEvent(NULL) ? TRUE : FALSE);
1060 #else
1061   return (XPending(display) ? TRUE : FALSE);
1062 #endif
1063 }
1064
1065 inline void NextEvent(Event *event)
1066 {
1067 #if defined(TARGET_SDL)
1068   SDLNextEvent(event);
1069 #else
1070   XNextEvent(display, event);
1071 #endif
1072 }
1073
1074 inline Key GetEventKey(KeyEvent *event, boolean with_modifiers)
1075 {
1076 #if defined(TARGET_SDL)
1077 #if 0
1078   printf("unicode == '%d', sym == '%d', mod == '0x%04x'\n",
1079          (int)event->keysym.unicode,
1080          (int)event->keysym.sym,
1081          (int)SDL_GetModState());
1082 #endif
1083
1084   if (with_modifiers &&
1085       event->keysym.unicode > 0x0000 &&
1086       event->keysym.unicode < 0x2000)
1087     return event->keysym.unicode;
1088   else
1089     return event->keysym.sym;
1090 #else
1091 #if 0
1092   printf("with modifiers == '0x%04x', without modifiers == '0x%04x'\n",
1093          (int)XLookupKeysym(event, event->state),
1094          (int)XLookupKeysym(event, 0));
1095 #endif
1096
1097   if (with_modifiers)
1098     return XLookupKeysym(event, event->state);
1099   else
1100     return XLookupKeysym(event, 0);
1101 #endif
1102 }
1103
1104 inline KeyMod HandleKeyModState(Key key, int key_status)
1105 {
1106   static KeyMod current_modifiers = KMOD_None;
1107
1108 #if !defined(TARGET_SDL)
1109   if (key != KSYM_UNDEFINED)    /* new key => check for modifier key change */
1110   {
1111     KeyMod new_modifier = KMOD_None;
1112
1113     switch(key)
1114     {
1115       case KSYM_Shift_L:
1116         new_modifier = KMOD_Shift_L;
1117         break;
1118       case KSYM_Shift_R:
1119         new_modifier = KMOD_Shift_R;
1120         break;
1121       case KSYM_Control_L:
1122         new_modifier = KMOD_Control_L;
1123         break;
1124       case KSYM_Control_R:
1125         new_modifier = KMOD_Control_R;
1126         break;
1127       case KSYM_Meta_L:
1128         new_modifier = KMOD_Meta_L;
1129         break;
1130       case KSYM_Meta_R:
1131         new_modifier = KMOD_Meta_R;
1132         break;
1133       case KSYM_Alt_L:
1134         new_modifier = KMOD_Alt_L;
1135         break;
1136       case KSYM_Alt_R:
1137         new_modifier = KMOD_Alt_R;
1138         break;
1139       default:
1140         break;
1141     }
1142
1143     if (key_status == KEY_PRESSED)
1144       current_modifiers |= new_modifier;
1145     else
1146       current_modifiers &= ~new_modifier;
1147   }
1148 #endif
1149
1150   return current_modifiers;
1151 }
1152
1153 inline KeyMod GetKeyModState()
1154 {
1155 #if defined(TARGET_SDL)
1156   return (KeyMod)SDL_GetModState();
1157 #else
1158   return HandleKeyModState(KSYM_UNDEFINED, 0);
1159 #endif
1160 }
1161
1162 inline boolean CheckCloseWindowEvent(ClientMessageEvent *event)
1163 {
1164   if (event->type != EVENT_CLIENTMESSAGE)
1165     return FALSE;
1166
1167 #if defined(TARGET_SDL)
1168   return TRUE;          /* the only possible message here is SDL_QUIT */
1169 #elif defined(PLATFORM_UNIX)
1170   if ((event->window == window->drawable) &&
1171       (event->data.l[0] == XInternAtom(display, "WM_DELETE_WINDOW", FALSE)))
1172     return TRUE;
1173 #endif
1174
1175   return FALSE;
1176 }
1177
1178
1179 /* ========================================================================= */
1180 /* joystick functions                                                        */
1181 /* ========================================================================= */
1182
1183 inline void InitJoysticks()
1184 {
1185   int i;
1186
1187 #if defined(NO_JOYSTICK)
1188   return;       /* joysticks generally deactivated by compile-time directive */
1189 #endif
1190
1191   /* always start with reliable default values */
1192   joystick.status = JOYSTICK_NOT_AVAILABLE;
1193   for (i = 0; i < MAX_PLAYERS; i++)
1194     joystick.fd[i] = -1;                /* joystick device closed */
1195
1196 #if defined(TARGET_SDL)
1197   SDLInitJoysticks();
1198 #elif defined(PLATFORM_UNIX)
1199   UnixInitJoysticks();
1200 #elif defined(PLATFORM_MSDOS)
1201   MSDOSInitJoysticks();
1202 #endif
1203 }
1204
1205 inline boolean ReadJoystick(int nr, int *x, int *y, boolean *b1, boolean *b2)
1206 {
1207 #if defined(TARGET_SDL)
1208   return SDLReadJoystick(nr, x, y, b1, b2);
1209 #elif defined(PLATFORM_UNIX)
1210   return UnixReadJoystick(nr, x, y, b1, b2);
1211 #elif defined(PLATFORM_MSDOS)
1212   return MSDOSReadJoystick(nr, x, y, b1, b2);
1213 #endif
1214 }