rnd-20010115-1-src
[rocksndiamonds.git] / src / libgame / msdos.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2001 Artsoft Entertainment                      *
5 *               Holger Schemel                             *
6 *               Detmolder Strasse 189                      *
7 *               33604 Bielefeld                            *
8 *               Germany                                    *
9 *               e-mail: info@artsoft.org                   *
10 *----------------------------------------------------------*
11 * msdos.c                                                  *
12 ***********************************************************/
13
14 #include "system.h"
15
16
17 #if defined(PLATFORM_MSDOS)
18
19 #include "sound.h"
20 #include "misc.h"
21 #include "pcx.h"
22
23 #define AllegroDefaultScreen() (display->screens[display->default_screen])
24
25 /* allegro driver declarations */
26 DECLARE_GFX_DRIVER_LIST(GFX_DRIVER_VBEAF GFX_DRIVER_VESA2L GFX_DRIVER_VESA1)
27 DECLARE_COLOR_DEPTH_LIST(COLOR_DEPTH_8)
28 DECLARE_DIGI_DRIVER_LIST(DIGI_DRIVER_SB)
29 DECLARE_MIDI_DRIVER_LIST()
30 DECLARE_JOYSTICK_DRIVER_LIST(JOYSTICK_DRIVER_STANDARD)
31
32 /* allegro global variables */
33 extern volatile int key_shifts;
34 extern int num_joysticks;
35 extern JOYSTICK_INFO joy[];
36 extern int i_love_bill;
37
38 /* internal variables of msdos.c */
39 static boolean keyboard_auto_repeat = TRUE;
40 static int key_press_state[MAX_SCANCODES];
41 static XEvent event_buffer[MAX_EVENT_BUFFER];
42 static int pending_events;
43 static boolean joystick_event;
44 static boolean mouse_installed = FALSE;
45 static int last_mouse_pos;
46 static int last_mouse_b;
47 static int last_joystick_state;
48 static BITMAP* video_bitmap;
49
50 static RGB global_colormap[MAX_COLORS];
51 static int global_colormap_entries_used = 0;
52
53 boolean wait_for_vsync;
54
55 /*
56 extern int playing_sounds;
57 extern struct SoundControl playlist[MAX_SOUNDS_PLAYING];
58 extern struct SoundControl emptySoundControl;
59 */
60
61 static BITMAP *Read_PCX_to_AllegroBitmap(char *);
62
63 static void allegro_init_drivers()
64 {
65   int i;
66
67   for (i=0; i<MAX_EVENT_BUFFER; i++)
68     event_buffer[i].type = 0;
69
70   for (i=0; i<MAX_SCANCODES; i++)
71     key_press_state[i] = KeyReleaseMask;
72
73   last_mouse_pos = mouse_pos;
74   last_mouse_b = 0;
75
76   pending_events = 0;
77   clear_keybuf();
78
79   /* enable Windows friendly timer mode (already default under Windows) */
80   i_love_bill = TRUE;
81
82   install_keyboard();
83   install_timer();
84   if (install_mouse() > 0)
85     mouse_installed = TRUE;
86
87   last_joystick_state = 0;
88   joystick_event = FALSE;
89 }
90
91 static boolean allegro_init_audio()
92 {
93   reserve_voices(MAX_SOUNDS_PLAYING, 0);
94
95   if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL) == -1)
96     if (install_sound(DIGI_SB, MIDI_NONE, NULL) == -1)
97       return FALSE;
98
99   return TRUE;
100 }
101
102 static boolean hide_mouse(Display *display, int x, int y,
103                           unsigned int width, unsigned int height)
104 {
105   if (mouse_x + display->mouse_ptr->w < x || mouse_x > x + width)
106     return FALSE;
107   if (mouse_y + display->mouse_ptr->h < y || mouse_y > y + height)
108     return FALSE;
109
110   show_mouse(NULL);
111
112   return TRUE;
113 }
114
115 static void unhide_mouse(Display *display)
116 {
117   if (mouse_installed)
118     show_mouse(video_bitmap);
119 }
120
121 static KeySym ScancodeToKeySym(byte scancode)
122 {
123   switch(scancode)
124   {
125     case KEY_ESC:               return XK_Escape;
126     case KEY_1:                 return XK_1;
127     case KEY_2:                 return XK_2;
128     case KEY_3:                 return XK_3;
129     case KEY_4:                 return XK_4;
130     case KEY_5:                 return XK_5;
131     case KEY_6:                 return XK_6;
132     case KEY_7:                 return XK_7;
133     case KEY_8:                 return XK_8;
134     case KEY_9:                 return XK_9;
135     case KEY_0:                 return XK_0;
136     case KEY_MINUS:             return XK_minus;
137     case KEY_EQUALS:            return XK_equal;
138     case KEY_BACKSPACE:         return XK_BackSpace;
139     case KEY_TAB:               return XK_Tab;
140     case KEY_Q:                 return XK_q;
141     case KEY_W:                 return XK_w;
142     case KEY_E:                 return XK_e;
143     case KEY_R:                 return XK_r;
144     case KEY_T:                 return XK_t;
145     case KEY_Y:                 return XK_y;
146     case KEY_U:                 return XK_u;
147     case KEY_I:                 return XK_i;
148     case KEY_O:                 return XK_o;
149     case KEY_P:                 return XK_p;
150     case KEY_OPENBRACE:         return XK_braceleft;
151     case KEY_CLOSEBRACE:        return XK_braceright;
152     case KEY_ENTER:             return XK_Return;
153     case KEY_LCONTROL:          return XK_Control_L;
154     case KEY_A:                 return XK_a;
155     case KEY_S:                 return XK_s;
156     case KEY_D:                 return XK_d;
157     case KEY_F:                 return XK_f;
158     case KEY_G:                 return XK_g;
159     case KEY_H:                 return XK_h;
160     case KEY_J:                 return XK_j;
161     case KEY_K:                 return XK_k;
162     case KEY_L:                 return XK_l;
163     case KEY_COLON:             return XK_colon;
164     case KEY_QUOTE:             return XK_apostrophe;
165     case KEY_TILDE:             return XK_asciitilde;
166     case KEY_LSHIFT:            return XK_Shift_L;
167     case KEY_BACKSLASH:         return XK_backslash;
168     case KEY_Z:                 return XK_z;
169     case KEY_X:                 return XK_x;
170     case KEY_C:                 return XK_c;
171     case KEY_V:                 return XK_v;
172     case KEY_B:                 return XK_b;
173     case KEY_N:                 return XK_n;
174     case KEY_M:                 return XK_m;
175     case KEY_COMMA:             return XK_comma;
176     case KEY_STOP:              return XK_period;
177     case KEY_SLASH:             return XK_slash;
178     case KEY_RSHIFT:            return XK_Shift_R;
179     case KEY_ASTERISK:          return XK_KP_Multiply;
180     case KEY_ALT:               return XK_Alt_L;
181     case KEY_SPACE:             return XK_space;
182     case KEY_CAPSLOCK:          return XK_Caps_Lock;
183     case KEY_F1:                return XK_F1;
184     case KEY_F2:                return XK_F2;
185     case KEY_F3:                return XK_F3;
186     case KEY_F4:                return XK_F4;
187     case KEY_F5:                return XK_F5;
188     case KEY_F6:                return XK_F6;
189     case KEY_F7:                return XK_F7;
190     case KEY_F8:                return XK_F8;
191     case KEY_F9:                return XK_F9;
192     case KEY_F10:               return XK_F10;
193     case KEY_NUMLOCK:           return XK_Num_Lock;
194     case KEY_SCRLOCK:           return XK_Scroll_Lock;
195     case KEY_HOME:              return XK_Home;
196     case KEY_UP:                return XK_Up;
197     case KEY_PGUP:              return XK_Page_Up;
198     case KEY_MINUS_PAD:         return XK_KP_Subtract;
199     case KEY_LEFT:              return XK_Left;
200     case KEY_5_PAD:             return XK_KP_5;
201     case KEY_RIGHT:             return XK_Right;
202     case KEY_PLUS_PAD:          return XK_KP_Add;
203     case KEY_END:               return XK_End;
204     case KEY_DOWN:              return XK_Down;
205     case KEY_PGDN:              return XK_Page_Down;
206     case KEY_INSERT:            return XK_Insert;
207     case KEY_DEL:               return XK_Delete;
208     case KEY_PRTSCR:            return XK_Print;
209     case KEY_F11:               return XK_F11;
210     case KEY_F12:               return XK_F12;
211     case KEY_LWIN:              return XK_Meta_L;
212     case KEY_RWIN:              return XK_Meta_R;
213     case KEY_MENU:              return XK_Menu;
214     case KEY_PAD:               return XK_VoidSymbol;
215     case KEY_RCONTROL:          return XK_Control_R;
216     case KEY_ALTGR:             return XK_Alt_R;
217     case KEY_SLASH2:            return XK_KP_Divide;
218     case KEY_PAUSE:             return XK_Pause;
219
220     case NEW_KEY_BACKSLASH:     return XK_backslash;
221     case NEW_KEY_1_PAD:         return XK_KP_1;
222     case NEW_KEY_2_PAD:         return XK_KP_2;
223     case NEW_KEY_3_PAD:         return XK_KP_3;
224     case NEW_KEY_4_PAD:         return XK_KP_4;
225     case NEW_KEY_5_PAD:         return XK_KP_5;
226     case NEW_KEY_6_PAD:         return XK_KP_6;
227     case NEW_KEY_7_PAD:         return XK_KP_7;
228     case NEW_KEY_8_PAD:         return XK_KP_8;
229     case NEW_KEY_9_PAD:         return XK_KP_9;
230     case NEW_KEY_0_PAD:         return XK_KP_0;
231     case NEW_KEY_STOP_PAD:      return XK_KP_Separator;
232     case NEW_KEY_EQUALS_PAD:    return XK_KP_Equal;
233     case NEW_KEY_SLASH_PAD:     return XK_KP_Divide;
234     case NEW_KEY_ASTERISK_PAD:  return XK_KP_Multiply;
235     case NEW_KEY_ENTER_PAD:     return XK_KP_Enter;
236
237     default:                    return XK_VoidSymbol;
238   }
239 }
240
241 Pixel AllegroAllocColorCell(int r, int g, int b)
242 {
243   byte pixel_mapping = 0;
244   int i;
245
246   r >>= 10;
247   g >>= 10;
248   b >>= 10;
249
250   /* try to use existing colors from the global colormap */
251   for (i=0; i<global_colormap_entries_used; i++)
252   {
253     if (r == global_colormap[i].r &&
254         g == global_colormap[i].g &&
255         b == global_colormap[i].b)              /* color found */
256     {
257       pixel_mapping = i;
258       break;
259     }
260   }
261
262   if (i == global_colormap_entries_used)        /* color not found */
263   {
264     if (global_colormap_entries_used < MAX_COLORS)
265       global_colormap_entries_used++;
266
267     i = global_colormap_entries_used - 1;
268
269     global_colormap[i].r = r;
270     global_colormap[i].g = g;
271     global_colormap[i].b = b;
272
273     set_palette(global_colormap);
274
275     pixel_mapping = i;
276   }
277
278   return pixel_mapping;
279 }
280
281 void XMapWindow(Display *display, Window window)
282 {
283   int x, y;
284   unsigned int width, height;
285   boolean mouse_off;
286
287   x = AllegroDefaultScreen().x;
288   y = AllegroDefaultScreen().y;
289   width = AllegroDefaultScreen().width;
290   height = AllegroDefaultScreen().height;
291
292   mouse_off = hide_mouse(display, x, y, width, height);
293   blit((BITMAP *)window, video_bitmap, 0, 0, x, y, width, height);
294
295   if (mouse_off)
296     unhide_mouse(display);
297 }
298
299 Display *XOpenDisplay(char *display_name)
300 {
301   Screen *screen;
302   Display *display;
303   BITMAP *mouse_bitmap = NULL;
304
305   mouse_bitmap = Read_PCX_to_AllegroBitmap(program.msdos_pointer_filename);
306   if (mouse_bitmap == NULL)
307     return NULL;
308
309   screen = malloc(sizeof(Screen));
310   display = malloc(sizeof(Display));
311
312   screen[0].cmap = 0;
313   screen[0].root = 0;
314 #if 0
315   screen[0].white_pixel = 0xFF;
316   screen[0].black_pixel = 0x00;
317 #else
318   screen[0].white_pixel = AllegroAllocColorCell(0xFFFF, 0xFFFF, 0xFFFF);
319   screen[0].black_pixel = AllegroAllocColorCell(0x0000, 0x0000, 0x0000);
320 #endif
321   screen[0].video_bitmap = NULL;
322
323   display->default_screen = 0;
324   display->screens = screen;
325   display->mouse_ptr = mouse_bitmap;
326
327   allegro_init();
328   allegro_init_drivers();
329   set_color_depth(8);
330
331   /* force Windows 95 to switch to fullscreen mode */
332   set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);
333   rest(200);
334   set_gfx_mode(GFX_AUTODETECT, XRES, YRES, 0, 0);
335
336   return display;
337 }
338
339 Window XCreateSimpleWindow(Display *display, Window parent, int x, int y,
340                            unsigned int width, unsigned int height,
341                            unsigned int border_width, unsigned long border,
342                            unsigned long background)
343 {
344   video_bitmap = create_video_bitmap(XRES, YRES);
345   clear_to_color(video_bitmap, background);
346
347   AllegroDefaultScreen().video_bitmap = video_bitmap;
348   AllegroDefaultScreen().x = x;
349   AllegroDefaultScreen().y = y;
350   AllegroDefaultScreen().width = XRES;
351   AllegroDefaultScreen().height = YRES;
352
353   set_mouse_sprite(display->mouse_ptr);
354
355 #if 0
356   set_mouse_sprite_focus(1, 1);
357 #endif
358
359   set_mouse_speed(1, 1);
360   set_mouse_range(AllegroDefaultScreen().x + 1,
361                   AllegroDefaultScreen().y + 1,
362                   AllegroDefaultScreen().x + video.width + 1,
363                   AllegroDefaultScreen().y + video.height + 1);
364
365   show_video_bitmap(video_bitmap);
366
367   return (Window)video_bitmap;
368 }
369
370 Status XStringListToTextProperty(char **list, int count,
371                                  XTextProperty *text_prop_return)
372 {
373   char *string;
374
375   if (count >= 1)
376   {
377     string = malloc(strlen(list[0] + 1));
378     strcpy(string, list[0]);
379     text_prop_return->value = (unsigned char *)string;
380     return 1;
381   }
382   else
383     text_prop_return = NULL;
384
385   return 0;
386 }
387
388 void XFree(void *data)
389 {
390   if (data)
391     free(data);
392 }
393
394 GC XCreateGC(Display *display, Drawable d, unsigned long value_mask,
395              XGCValues *values)
396 {
397   XGCValues *gcv;
398   gcv = malloc(sizeof(XGCValues));
399   gcv->foreground = values->foreground;
400   gcv->background = values->background;
401   gcv->graphics_exposures = values->graphics_exposures;
402   gcv->clip_mask = values->clip_mask;
403   gcv->clip_x_origin = values->clip_x_origin;
404   gcv->clip_y_origin = values->clip_y_origin;
405   gcv->value_mask = value_mask;
406   return (GC)gcv;
407 }
408
409 void XSetClipMask(Display *display, GC gc, Pixmap pixmap)
410 {
411   XGCValues *gcv = (XGCValues *)gc;
412
413   gcv->clip_mask = pixmap;
414   gcv->value_mask |= GCClipMask;
415 }
416
417 void XSetClipOrigin(Display *display, GC gc, int x, int y)
418 {
419   XGCValues *gcv = (XGCValues *)gc;
420
421   gcv->clip_x_origin = x;
422   gcv->clip_x_origin = y;
423 }
424
425 void XFillRectangle(Display *display, Drawable d, GC gc, int x, int y,
426                     unsigned int width, unsigned int height)
427 {
428   boolean mouse_off = FALSE;
429
430   if ((BITMAP *)d == video_bitmap)
431   {
432     x += AllegroDefaultScreen().x;
433     y += AllegroDefaultScreen().y;
434     freeze_mouse_flag = TRUE;
435     mouse_off = hide_mouse(display, x, y, width, height);
436   }
437
438   rectfill((BITMAP *)d, x, y, x + width - 1, y + height - 1,
439            ((XGCValues *)gc)->foreground);
440
441   if (mouse_off)
442     unhide_mouse(display);
443
444   freeze_mouse_flag = FALSE;
445 }
446
447 Pixmap XCreatePixmap(Display *display, Drawable d, unsigned int width,
448                      unsigned int height, unsigned int depth)
449 {
450   BITMAP *bitmap = NULL;
451
452   if (gfx_capabilities & GFX_HW_VRAM_BLIT &&
453       width  == gfx.scrollbuffer_width && height == gfx.scrollbuffer_height)
454     bitmap = create_video_bitmap(width, height);
455
456   if (bitmap == NULL)
457     bitmap = create_bitmap(width, height);
458
459   return (Pixmap)bitmap;
460 }
461
462 void XSync(Display *display, Bool discard_events)
463 {
464   wait_for_vsync = TRUE;
465 }
466
467 inline void XCopyArea(Display *display, Drawable src, Drawable dest, GC gc,
468                       int src_x, int src_y,
469                       unsigned int width, unsigned int height,
470                       int dest_x, int dest_y)
471 {
472   boolean mouse_off = FALSE;
473
474   if ((BITMAP *)src == video_bitmap)
475   {
476     src_x += AllegroDefaultScreen().x;
477     src_y += AllegroDefaultScreen().y;
478   }
479
480   if ((BITMAP *)dest == video_bitmap)
481   {
482     dest_x += AllegroDefaultScreen().x;
483     dest_y += AllegroDefaultScreen().y;
484     freeze_mouse_flag = TRUE;
485     mouse_off = hide_mouse(display, dest_x, dest_y, width, height);
486   }
487
488   if (wait_for_vsync)
489   {
490     wait_for_vsync = FALSE;
491     vsync();
492   }
493
494   if (((XGCValues *)gc)->value_mask & GCClipMask)
495     masked_blit((BITMAP *)src, (BITMAP *)dest, src_x, src_y, dest_x, dest_y,
496                 width, height);
497   else
498     blit((BITMAP *)src, (BITMAP *)dest, src_x, src_y, dest_x, dest_y,
499          width, height);
500
501   if (mouse_off)
502     unhide_mouse(display);
503
504   freeze_mouse_flag = FALSE;
505 }
506
507 static BITMAP *Image_to_AllegroBitmap(Image *image)
508 {
509   BITMAP *bitmap;
510   byte *src_ptr = image->data;
511   byte pixel_mapping[MAX_COLORS];
512   unsigned int depth = 8;
513
514 #if 0
515   int i, j, x, y;
516 #else
517   int i, x, y;
518 #endif
519
520   /* allocate new allegro bitmap structure */
521   if ((bitmap = create_bitmap_ex(depth, image->width, image->height)) == NULL)
522   {
523     errno_pcx = PCX_NoMemory;
524     return NULL;
525   }
526
527   clear(bitmap);
528
529   /* try to use existing colors from the global colormap */
530   for (i=0; i<MAX_COLORS; i++)
531   {
532
533 #if 0
534     int r, g, b;
535 #endif
536
537     if (!image->rgb.color_used[i])
538       continue;
539
540
541 #if 0
542     r = image->rgb.red[i] >> 10;
543     g = image->rgb.green[i] >> 10;
544     b = image->rgb.blue[i] >> 10;
545
546     for (j=0; j<global_colormap_entries_used; j++)
547     {
548       if (r == global_colormap[j].r &&
549           g == global_colormap[j].g &&
550           b == global_colormap[j].b)            /* color found */
551       {
552         pixel_mapping[i] = j;
553         break;
554       }
555     }
556
557     if (j == global_colormap_entries_used)      /* color not found */
558     {
559       if (global_colormap_entries_used < MAX_COLORS)
560         global_colormap_entries_used++;
561
562       global_colormap[j].r = r;
563       global_colormap[j].g = g;
564       global_colormap[j].b = b;
565
566       pixel_mapping[i] = j;
567     }
568 #else
569     pixel_mapping[i] = AllegroAllocColorCell(image->rgb.red[i],
570                                              image->rgb.green[i],
571                                              image->rgb.blue[i]);
572 #endif
573
574   }
575
576   /* copy bitmap data */
577   for (y=0; y<image->height; y++)
578     for (x=0; x<image->width; x++)
579       putpixel(bitmap, x, y, pixel_mapping[*src_ptr++]);
580
581   return bitmap;
582 }
583
584 static BITMAP *Read_PCX_to_AllegroBitmap(char *filename)
585 {
586   BITMAP *bitmap;
587   Image *image;
588
589   /* read the graphic file in PCX format to internal image structure */
590   if ((image = Read_PCX_to_Image(filename)) == NULL)
591     return NULL;
592
593   /* convert internal image structure to allegro bitmap structure */
594   if ((bitmap = Image_to_AllegroBitmap(image)) == NULL)
595     return NULL;
596
597   set_palette(global_colormap);
598
599   return bitmap;
600 }
601
602 int Read_PCX_to_Pixmap(Display *display, Window window, GC gc, char *filename,
603                        Pixmap *pixmap, Pixmap *pixmap_mask)
604 {
605   BITMAP *bitmap;
606
607   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
608     return errno_pcx;
609
610   *pixmap = (Pixmap)bitmap;
611 #if 0
612   *pixmap_mask = (Pixmap)bitmap;
613   /* !!! two pointers on same bitmap => second free() fails !!! */
614 #else
615   /* pixmap_mask will never be used in Allegro (which uses masked_blit()),
616      so use non-NULL dummy pointer to empty Pixmap */
617   /*
618   *pixmap_mask = (Pixmap)checked_calloc(sizeof(Pixmap));
619   */
620   *pixmap_mask = (Pixmap)DUMMY_MASK;
621 #endif
622
623   return PCX_Success;
624 }
625
626 int XReadBitmapFile(Display *display, Drawable d, char *filename,
627                     unsigned int *width_return, unsigned int *height_return,
628                     Pixmap *bitmap_return,
629                     int *x_hot_return, int *y_hot_return)
630 {
631   BITMAP *bitmap;
632
633   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
634     return BitmapOpenFailed;
635
636   *width_return = bitmap->w;
637   *height_return = bitmap->h;
638   *x_hot_return = -1;
639   *y_hot_return = -1;
640   *bitmap_return = (Pixmap)bitmap;
641
642   return BitmapSuccess;
643 }
644
645 void XFreePixmap(Display *display, Pixmap pixmap)
646 {
647   if (pixmap != DUMMY_MASK &&
648       (is_memory_bitmap((BITMAP *)pixmap) ||
649        is_screen_bitmap((BITMAP *)pixmap)))
650     destroy_bitmap((BITMAP *)pixmap);
651 }
652
653 void XFreeGC(Display *display, GC gc)
654 {
655   XGCValues *gcv;
656
657   gcv = (XGCValues *)gc;
658   if (gcv)
659     free(gcv);
660 }
661
662 void XUnmapWindow(Display *display, Window window)
663 {
664 }
665
666 void XCloseDisplay(Display *display)
667 {
668   BITMAP *bitmap = video_bitmap;
669
670   if (is_screen_bitmap(bitmap))
671     destroy_bitmap(bitmap);
672
673   if (display->screens)
674     free(display->screens);
675
676   if (display)
677     free(display);
678
679   /* return to text mode (or DOS box on Windows screen) */
680   set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
681 }
682
683 void XNextEvent(Display *display, XEvent *event_return)
684 {
685   while (!pending_events)
686     XPending(display);
687
688   memcpy(event_return, &event_buffer[pending_events], sizeof(XEvent));
689   pending_events--;
690 }
691
692 static void NewKeyEvent(int key_press_state, KeySym keysym)
693 {
694   XKeyEvent *xkey;
695
696   if (pending_events >= MAX_EVENT_BUFFER)
697     return;
698
699   pending_events++;
700   xkey = (XKeyEvent *)&event_buffer[pending_events];
701   xkey->type = key_press_state;
702   xkey->state = (unsigned int)keysym;
703 }
704
705 #define HANDLE_RAW_KB_ALL_KEYS                  0
706 #define HANDLE_RAW_KB_MODIFIER_KEYS_ONLY        1
707
708 static int modifier_scancode[] =
709 {
710   KEY_LSHIFT,
711   KEY_RSHIFT,
712   KEY_LCONTROL,
713   KEY_RCONTROL,
714   KEY_ALT,
715   KEY_ALTGR,
716   KEY_LWIN,
717   KEY_RWIN,
718   KEY_CAPSLOCK,
719   KEY_NUMLOCK,
720   KEY_SCRLOCK,
721   -1
722 };
723
724 static void HandleKeyboardRaw(int mode)
725 {
726   int i;
727
728   for (i=0; i<MAX_SCANCODES; i++)
729   {
730     int scancode, new_state, event_type;
731     char key_pressed;
732
733     if (mode == HANDLE_RAW_KB_MODIFIER_KEYS_ONLY)
734     {
735       if ((scancode = modifier_scancode[i]) == -1)
736         return;
737     }
738     else
739       scancode = i;
740
741     key_pressed = key[scancode];
742     new_state = (key_pressed ? KeyPressMask : KeyReleaseMask);
743     event_type = (key_pressed ? KeyPress : KeyRelease);
744
745     if (key_press_state[i] == new_state)        /* state not changed */
746       continue;
747
748     key_press_state[i] = new_state;
749
750     NewKeyEvent(event_type, ScancodeToKeySym(scancode));
751   }
752 }
753
754 static void HandleKeyboardEvent()
755 {
756   if (keypressed())
757   {
758     int key_info = readkey();
759     int scancode = (key_info >> 8);
760     int ascii = (key_info & 0xff);
761     KeySym keysym = ScancodeToKeySym(scancode);
762
763     if (scancode == KEY_PAD)
764     {
765       /* keys on the numeric keypad return just scancode 'KEY_PAD'
766          for some reason, so we must handle them separately */
767
768       if (ascii >= '0' && ascii <= '9')
769         keysym = XK_KP_0 + (KeySym)(ascii - '0');
770       else if (ascii == '.')
771         keysym = XK_KP_Separator;
772     }
773     else if (ascii >= ' ' && ascii <= 'Z')
774       keysym = XK_space + (KeySym)(ascii - ' ');
775     else if (ascii == '^')
776       keysym = XK_asciicircum;
777     else if (ascii == '_')
778       keysym = XK_underscore;
779     else if (ascii == 'Ä')
780       keysym = XK_Adiaeresis;
781     else if (ascii == 'Ö')
782       keysym = XK_Odiaeresis;
783     else if (ascii == 'Ü')
784       keysym = XK_Udiaeresis;
785     else if (ascii == 'ä')
786       keysym = XK_adiaeresis;
787     else if (ascii == 'ö')
788       keysym = XK_odiaeresis;
789     else if (ascii == 'ü')
790       keysym = XK_udiaeresis;
791     else if (ascii == 'ß')
792       keysym = XK_ssharp;
793
794     NewKeyEvent(KeyPress, keysym);
795   }
796   else if (key_shifts & (KB_SHIFT_FLAG | KB_CTRL_FLAG | KB_ALT_FLAG))
797   {
798     /* the allegro function keypressed() does not give us single pressed
799        modifier keys, so we must detect them with the internal global
800        allegro variable 'key_shifts' and then handle them separately */
801
802     HandleKeyboardRaw(HANDLE_RAW_KB_MODIFIER_KEYS_ONLY);
803   }
804 }
805
806 int XPending(Display *display)
807 {
808   XButtonEvent *xbutton;
809   XMotionEvent *xmotion;
810   int i;
811
812   /* When using 'HandleKeyboardRaw()', keyboard input is also stored in
813      the allegro keyboard input buffer and would be available a second
814      time by calling 'HandleKeyboardEvent()'. To avoid double keyboard
815      events, the allegro function 'clear_keybuf()' must be called each
816      time when switching from calling 'HandleKeyboardRaw()' to calling
817      'HandleKeyboardEvent()' to get keyboard input, which is actually
818      done by 'XAutoRepeatOn()' which sets keyboard_auto_repeat to TRUE. */
819
820   /* keyboard event */
821   if (keyboard_auto_repeat)
822     HandleKeyboardEvent();
823   else
824     HandleKeyboardRaw(HANDLE_RAW_KB_ALL_KEYS);
825
826   /* mouse motion event */
827   if (mouse_pos != last_mouse_pos)
828   {
829     last_mouse_pos = mouse_pos;
830     pending_events++;
831     xmotion = (XMotionEvent *)&event_buffer[pending_events];
832     xmotion->type = MotionNotify;
833     xmotion->x = mouse_x - AllegroDefaultScreen().x;
834     xmotion->y = mouse_y - AllegroDefaultScreen().y;
835   }
836
837   /* mouse button event */
838   if (mouse_b != last_mouse_b)
839   {
840     for (i=0; i<3; i++)         /* check all three mouse buttons */
841     {
842       int bitmask = (1 << i);
843
844       if ((last_mouse_b & bitmask) != (mouse_b & bitmask))
845       {
846         int mapping[3] = { 1, 3, 2 };
847
848         pending_events++;
849         xbutton = (XButtonEvent *)&event_buffer[pending_events];
850         xbutton->type = (mouse_b & bitmask ? ButtonPress : ButtonRelease);
851         xbutton->button = mapping[i];
852         xbutton->x = mouse_x - AllegroDefaultScreen().x;
853         xbutton->y = mouse_y - AllegroDefaultScreen().y;
854       }
855     }
856     last_mouse_b = mouse_b;
857   }
858
859   return pending_events;
860 }
861
862 KeySym XLookupKeysym(XKeyEvent *key_event, int index)
863 {
864   return key_event->state;
865 }
866
867 int XLookupString(XKeyEvent *key_event, char *buffer, int buffer_size,
868                   KeySym *key, XComposeStatus *compose)
869 {
870   *key = key_event->state;
871   return 0;
872 }
873
874 void XSetForeground(Display *display, GC gc, unsigned long pixel)
875 {
876   XGCValues *gcv = (XGCValues *)gc;
877
878   gcv->foreground = pixel;
879 }
880
881 void XDrawLine(Display *display, Drawable d, GC gc,
882                int x1, int y1, int x2, int y2)
883 {
884   XGCValues *gcv = (XGCValues *)gc;
885   boolean mouse_off = FALSE;
886
887   if ((BITMAP *)d == video_bitmap)
888   {
889     x1 += AllegroDefaultScreen().x;
890     y1 += AllegroDefaultScreen().y;
891     x2 += AllegroDefaultScreen().x;
892     y2 += AllegroDefaultScreen().y;
893     freeze_mouse_flag = TRUE;
894     mouse_off = hide_mouse(display, MIN(x1, x2), MIN(y1, y2),
895                            MAX(x1, x2) - MIN(x1, x2),
896                            MAX(y1, y2) - MIN(y1, y2));
897   }
898
899   line((BITMAP *)d, x1, y1, x2, y2, gcv->foreground);
900
901   if (mouse_off)
902     unhide_mouse(display);
903
904   freeze_mouse_flag = FALSE;
905 }
906
907 void XDestroyImage(XImage *ximage)
908 {
909 }
910
911 void XDestroyWindow(Display *display, Window window)
912 {
913 }
914
915 Bool XQueryPointer(Display *display, Window window,
916                    Window *root, Window *child, int *root_x, int *root_y,
917                    int *win_x, int *win_y, unsigned int *mask)
918 {
919   *win_x = mouse_x - AllegroDefaultScreen().x;
920   *win_y = mouse_y - AllegroDefaultScreen().y;
921
922   return True;
923 }
924
925 void XAutoRepeatOn(Display *display)
926 {
927   keyboard_auto_repeat = TRUE;
928   clear_keybuf();
929 }
930
931 void XAutoRepeatOff(Display *display)
932 {
933   keyboard_auto_repeat = FALSE;
934 }
935
936 void AllegroDrawLine(Drawable d, int from_x, int from_y, int to_x, int to_y,
937                      Pixel color)
938 {
939   boolean mouse_off = FALSE;
940
941   if ((BITMAP *)d == video_bitmap)
942   {
943     int dx = AllegroDefaultScreen().x;
944     int dy = AllegroDefaultScreen().y;
945     int x1, y1, x2, y2;
946
947     from_x += dx;
948     from_y += dy;
949     to_x += dx;
950     to_y += dy;
951
952     x1 = (from_x < to_x ? from_x : to_x);
953     y1 = (from_y < to_y ? from_y : to_y);
954     x2 = (from_x < to_x ? to_x : from_x);
955     y2 = (from_y < to_y ? to_y : from_y);
956
957     freeze_mouse_flag = TRUE;
958     mouse_off = hide_mouse(display, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
959   }
960
961   line((BITMAP *)d, from_x, from_y, to_x, to_y, color);
962
963   if (mouse_off)
964     unhide_mouse(display);
965
966   freeze_mouse_flag = FALSE;
967 }
968
969 Pixel AllegroGetPixel(Drawable d, int x, int y)
970 {
971   return getpixel((BITMAP *)d, x, y);
972 }
973
974 void MSDOSOpenAudio(void)
975 {
976   if (allegro_init_audio())
977   {
978     audio.sound_available = TRUE;
979     audio.music_available = TRUE;
980     audio.loops_available = TRUE;
981     audio.sound_enabled = TRUE;
982   }
983
984   InitPlaylist();
985 }
986
987 void MSDOSCloseAudio(void)
988 {
989   /* nothing to be done here */
990 }
991
992 void NetworkServer(int port, int serveronly)
993 {
994   Error(ERR_WARN, "networking not supported in DOS version");
995 }
996
997 #endif /* PLATFORM_MSDOS */