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