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