rnd-20001204-3-src
[rocksndiamonds.git] / src / libgame / msdos.c
1 /***********************************************************
2 * Artsoft Retro-Game Library                               *
3 *----------------------------------------------------------*
4 * (c) 1994-2000 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 void XMapWindow(Display *display, Window window)
242 {
243   int x, y;
244   unsigned int width, height;
245   boolean mouse_off;
246
247   x = AllegroDefaultScreen().x;
248   y = AllegroDefaultScreen().y;
249   width = AllegroDefaultScreen().width;
250   height = AllegroDefaultScreen().height;
251
252   mouse_off = hide_mouse(display, x, y, width, height);
253   blit((BITMAP *)window, video_bitmap, 0, 0, x, y, width, height);
254
255   if (mouse_off)
256     unhide_mouse(display);
257 }
258
259 static unsigned long AllocColorCell(int r, int g, int b)
260 {
261   byte pixel_mapping = 0;
262   int i;
263
264   r >>= 10;
265   g >>= 10;
266   b >>= 10;
267
268   /* try to use existing colors from the global colormap */
269   for (i=0; i<global_colormap_entries_used; i++)
270   {
271     if (r == global_colormap[i].r &&
272         g == global_colormap[i].g &&
273         b == global_colormap[i].b)              /* color found */
274     {
275       pixel_mapping = i;
276       break;
277     }
278   }
279
280   if (i == global_colormap_entries_used)        /* color not found */
281   {
282     if (global_colormap_entries_used < MAX_COLORS)
283       global_colormap_entries_used++;
284
285     global_colormap[i].r = r;
286     global_colormap[i].g = g;
287     global_colormap[i].b = b;
288
289     pixel_mapping = i;
290   }
291
292   return pixel_mapping;
293 }
294
295 Display *XOpenDisplay(char *display_name)
296 {
297   Screen *screen;
298   Display *display;
299   BITMAP *mouse_bitmap = NULL;
300
301   mouse_bitmap = Read_PCX_to_AllegroBitmap(program.msdos_pointer_filename);
302   if (mouse_bitmap == NULL)
303     return NULL;
304
305   screen = malloc(sizeof(Screen));
306   display = malloc(sizeof(Display));
307
308   screen[0].cmap = 0;
309   screen[0].root = 0;
310 #if 0
311   screen[0].white_pixel = 0xFF;
312   screen[0].black_pixel = 0x00;
313 #else
314   screen[0].white_pixel = AllocColorCell(0xFFFF, 0xFFFF, 0xFFFF);
315   screen[0].black_pixel = AllocColorCell(0x0000, 0x0000, 0x0000);
316 #endif
317   screen[0].video_bitmap = NULL;
318
319   display->default_screen = 0;
320   display->screens = screen;
321   display->mouse_ptr = mouse_bitmap;
322
323   allegro_init();
324   allegro_init_drivers();
325   set_color_depth(8);
326
327   /* force Windows 95 to switch to fullscreen mode */
328   set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0);
329   rest(200);
330   set_gfx_mode(GFX_AUTODETECT, XRES, YRES, 0, 0);
331
332   return display;
333 }
334
335 Window XCreateSimpleWindow(Display *display, Window parent, int x, int y,
336                            unsigned int width, unsigned int height,
337                            unsigned int border_width, unsigned long border,
338                            unsigned long background)
339 {
340   video_bitmap = create_video_bitmap(XRES, YRES);
341   clear_to_color(video_bitmap, background);
342
343   AllegroDefaultScreen().video_bitmap = video_bitmap;
344   AllegroDefaultScreen().x = x;
345   AllegroDefaultScreen().y = y;
346   AllegroDefaultScreen().width = XRES;
347   AllegroDefaultScreen().height = YRES;
348
349   set_mouse_sprite(display->mouse_ptr);
350
351 #if 0
352   set_mouse_sprite_focus(1, 1);
353 #endif
354
355   set_mouse_speed(1, 1);
356   set_mouse_range(AllegroDefaultScreen().x + 1,
357                   AllegroDefaultScreen().y + 1,
358                   AllegroDefaultScreen().x + video.width + 1,
359                   AllegroDefaultScreen().y + video.height + 1);
360
361   show_video_bitmap(video_bitmap);
362
363   return (Window)video_bitmap;
364 }
365
366 Status XStringListToTextProperty(char **list, int count,
367                                  XTextProperty *text_prop_return)
368 {
369   char *string;
370
371   if (count >= 1)
372   {
373     string = malloc(strlen(list[0] + 1));
374     strcpy(string, list[0]);
375     text_prop_return->value = (unsigned char *)string;
376     return 1;
377   }
378   else
379     text_prop_return = NULL;
380
381   return 0;
382 }
383
384 void XFree(void *data)
385 {
386   if (data)
387     free(data);
388 }
389
390 GC XCreateGC(Display *display, Drawable d, unsigned long value_mask,
391              XGCValues *values)
392 {
393   XGCValues *gcv;
394   gcv = malloc(sizeof(XGCValues));
395   gcv->foreground = values->foreground;
396   gcv->background = values->background;
397   gcv->graphics_exposures = values->graphics_exposures;
398   gcv->clip_mask = values->clip_mask;
399   gcv->clip_x_origin = values->clip_x_origin;
400   gcv->clip_y_origin = values->clip_y_origin;
401   gcv->value_mask = value_mask;
402   return (GC)gcv;
403 }
404
405 void XSetClipMask(Display *display, GC gc, Pixmap pixmap)
406 {
407   XGCValues *gcv = (XGCValues *)gc;
408
409   gcv->clip_mask = pixmap;
410   gcv->value_mask |= GCClipMask;
411 }
412
413 void XSetClipOrigin(Display *display, GC gc, int x, int y)
414 {
415   XGCValues *gcv = (XGCValues *)gc;
416
417   gcv->clip_x_origin = x;
418   gcv->clip_x_origin = y;
419 }
420
421 void XFillRectangle(Display *display, Drawable d, GC gc, int x, int y,
422                     unsigned int width, unsigned int height)
423 {
424   boolean mouse_off = FALSE;
425
426   if ((BITMAP *)d == video_bitmap)
427   {
428     x += AllegroDefaultScreen().x;
429     y += AllegroDefaultScreen().y;
430     freeze_mouse_flag = TRUE;
431     mouse_off = hide_mouse(display, x, y, width, height);
432   }
433
434   rectfill((BITMAP *)d, x, y, x + width - 1, y + height - 1,
435            ((XGCValues *)gc)->foreground);
436
437   if (mouse_off)
438     unhide_mouse(display);
439
440   freeze_mouse_flag = FALSE;
441 }
442
443 Pixmap XCreatePixmap(Display *display, Drawable d, unsigned int width,
444                      unsigned int height, unsigned int depth)
445 {
446   BITMAP *bitmap = NULL;
447
448   if (gfx_capabilities & GFX_HW_VRAM_BLIT &&
449       width  == gfx.scrollbuffer_width && height == gfx.scrollbuffer_height)
450     bitmap = create_video_bitmap(width, height);
451
452   if (bitmap == NULL)
453     bitmap = create_bitmap(width, height);
454
455   return (Pixmap)bitmap;
456 }
457
458 void XSync(Display *display, Bool discard_events)
459 {
460   wait_for_vsync = TRUE;
461 }
462
463 inline void XCopyArea(Display *display, Drawable src, Drawable dest, GC gc,
464                       int src_x, int src_y,
465                       unsigned int width, unsigned int height,
466                       int dest_x, int dest_y)
467 {
468   boolean mouse_off = FALSE;
469
470   if ((BITMAP *)src == video_bitmap)
471   {
472     src_x += AllegroDefaultScreen().x;
473     src_y += AllegroDefaultScreen().y;
474   }
475
476   if ((BITMAP *)dest == video_bitmap)
477   {
478     dest_x += AllegroDefaultScreen().x;
479     dest_y += AllegroDefaultScreen().y;
480     freeze_mouse_flag = TRUE;
481     mouse_off = hide_mouse(display, dest_x, dest_y, width, height);
482   }
483
484   if (wait_for_vsync)
485   {
486     wait_for_vsync = FALSE;
487     vsync();
488   }
489
490   if (((XGCValues *)gc)->value_mask & GCClipMask)
491     masked_blit((BITMAP *)src, (BITMAP *)dest, src_x, src_y, dest_x, dest_y,
492                 width, height);
493   else
494     blit((BITMAP *)src, (BITMAP *)dest, src_x, src_y, dest_x, dest_y,
495          width, height);
496
497   if (mouse_off)
498     unhide_mouse(display);
499
500   freeze_mouse_flag = FALSE;
501 }
502
503 static BITMAP *Image_to_AllegroBitmap(Image *image)
504 {
505   BITMAP *bitmap;
506   byte *src_ptr = image->data;
507   byte pixel_mapping[MAX_COLORS];
508   unsigned int depth = 8;
509
510 #if 0
511   int i, j, x, y;
512 #else
513   int i, x, y;
514 #endif
515
516   /* allocate new allegro bitmap structure */
517   if ((bitmap = create_bitmap_ex(depth, image->width, image->height)) == NULL)
518   {
519     errno_pcx = PCX_NoMemory;
520     return NULL;
521   }
522
523   clear(bitmap);
524
525   /* try to use existing colors from the global colormap */
526   for (i=0; i<MAX_COLORS; i++)
527   {
528
529 #if 0
530     int r, g, b;
531 #endif
532
533     if (!image->rgb.color_used[i])
534       continue;
535
536
537 #if 0
538     r = image->rgb.red[i] >> 10;
539     g = image->rgb.green[i] >> 10;
540     b = image->rgb.blue[i] >> 10;
541
542     for (j=0; j<global_colormap_entries_used; j++)
543     {
544       if (r == global_colormap[j].r &&
545           g == global_colormap[j].g &&
546           b == global_colormap[j].b)            /* color found */
547       {
548         pixel_mapping[i] = j;
549         break;
550       }
551     }
552
553     if (j == global_colormap_entries_used)      /* color not found */
554     {
555       if (global_colormap_entries_used < MAX_COLORS)
556         global_colormap_entries_used++;
557
558       global_colormap[j].r = r;
559       global_colormap[j].g = g;
560       global_colormap[j].b = b;
561
562       pixel_mapping[i] = j;
563     }
564 #else
565     pixel_mapping[i] = AllocColorCell(image->rgb.red[i],
566                                       image->rgb.green[i],
567                                       image->rgb.blue[i]);
568 #endif
569
570   }
571
572   /* copy bitmap data */
573   for (y=0; y<image->height; y++)
574     for (x=0; x<image->width; x++)
575       putpixel(bitmap, x, y, pixel_mapping[*src_ptr++]);
576
577   return bitmap;
578 }
579
580 static BITMAP *Read_PCX_to_AllegroBitmap(char *filename)
581 {
582   BITMAP *bitmap;
583   Image *image;
584
585   /* read the graphic file in PCX format to internal image structure */
586   if ((image = Read_PCX_to_Image(filename)) == NULL)
587     return NULL;
588
589   /* convert internal image structure to allegro bitmap structure */
590   if ((bitmap = Image_to_AllegroBitmap(image)) == NULL)
591     return NULL;
592
593   set_palette(global_colormap);
594
595   return bitmap;
596 }
597
598 int Read_PCX_to_Pixmap(Display *display, Window window, GC gc, char *filename,
599                        Pixmap *pixmap, Pixmap *pixmap_mask)
600 {
601   BITMAP *bitmap;
602
603   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
604     return errno_pcx;
605
606   *pixmap = (Pixmap)bitmap;
607   *pixmap_mask = (Pixmap)bitmap;
608
609   return PCX_Success;
610 }
611
612 int XReadBitmapFile(Display *display, Drawable d, char *filename,
613                     unsigned int *width_return, unsigned int *height_return,
614                     Pixmap *bitmap_return,
615                     int *x_hot_return, int *y_hot_return)
616 {
617   BITMAP *bitmap;
618
619   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
620     return BitmapOpenFailed;
621
622   *width_return = bitmap->w;
623   *height_return = bitmap->h;
624   *x_hot_return = -1;
625   *y_hot_return = -1;
626   *bitmap_return = (Pixmap)bitmap;
627
628   return BitmapSuccess;
629 }
630
631 void XFreePixmap(Display *display, Pixmap pixmap)
632 {
633   if (pixmap != DUMMY_MASK &&
634       (is_memory_bitmap((BITMAP *)pixmap) ||
635        is_screen_bitmap((BITMAP *)pixmap)))
636     destroy_bitmap((BITMAP *)pixmap);
637 }
638
639 void XFreeGC(Display *display, GC gc)
640 {
641   XGCValues *gcv;
642
643   gcv = (XGCValues *)gc;
644   if (gcv)
645     free(gcv);
646 }
647
648 void XUnmapWindow(Display *display, Window window)
649 {
650 }
651
652 void XCloseDisplay(Display *display)
653 {
654   BITMAP *bitmap = video_bitmap;
655
656   if (is_screen_bitmap(bitmap))
657     destroy_bitmap(bitmap);
658
659   if (display->screens)
660     free(display->screens);
661
662   if (display)
663     free(display);
664
665   /* return to text mode (or DOS box on Windows screen) */
666   set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
667 }
668
669 void XNextEvent(Display *display, XEvent *event_return)
670 {
671   while (!pending_events)
672     XPending(display);
673
674   memcpy(event_return, &event_buffer[pending_events], sizeof(XEvent));
675   pending_events--;
676 }
677
678 static void NewKeyEvent(int key_press_state, KeySym keysym)
679 {
680   XKeyEvent *xkey;
681
682   if (pending_events >= MAX_EVENT_BUFFER)
683     return;
684
685   pending_events++;
686   xkey = (XKeyEvent *)&event_buffer[pending_events];
687   xkey->type = key_press_state;
688   xkey->state = (unsigned int)keysym;
689 }
690
691 #define HANDLE_RAW_KB_ALL_KEYS                  0
692 #define HANDLE_RAW_KB_MODIFIER_KEYS_ONLY        1
693
694 static int modifier_scancode[] =
695 {
696   KEY_LSHIFT,
697   KEY_RSHIFT,
698   KEY_LCONTROL,
699   KEY_RCONTROL,
700   KEY_ALT,
701   KEY_ALTGR,
702   KEY_LWIN,
703   KEY_RWIN,
704   KEY_CAPSLOCK,
705   KEY_NUMLOCK,
706   KEY_SCRLOCK,
707   -1
708 };
709
710 static void HandleKeyboardRaw(int mode)
711 {
712   int i;
713
714   for (i=0; i<MAX_SCANCODES; i++)
715   {
716     int scancode, new_state, event_type;
717     char key_pressed;
718
719     if (mode == HANDLE_RAW_KB_MODIFIER_KEYS_ONLY)
720     {
721       if ((scancode = modifier_scancode[i]) == -1)
722         return;
723     }
724     else
725       scancode = i;
726
727     key_pressed = key[scancode];
728     new_state = (key_pressed ? KeyPressMask : KeyReleaseMask);
729     event_type = (key_pressed ? KeyPress : KeyRelease);
730
731     if (key_press_state[i] == new_state)        /* state not changed */
732       continue;
733
734     key_press_state[i] = new_state;
735
736     NewKeyEvent(event_type, ScancodeToKeySym(scancode));
737   }
738 }
739
740 static void HandleKeyboardEvent()
741 {
742   if (keypressed())
743   {
744     int key_info = readkey();
745     int scancode = (key_info >> 8);
746     int ascii = (key_info & 0xff);
747     KeySym keysym = ScancodeToKeySym(scancode);
748
749     if (scancode == KEY_PAD)
750     {
751       /* keys on the numeric keypad return just scancode 'KEY_PAD'
752          for some reason, so we must handle them separately */
753
754       if (ascii >= '0' && ascii <= '9')
755         keysym = XK_KP_0 + (KeySym)(ascii - '0');
756       else if (ascii == '.')
757         keysym = XK_KP_Separator;
758     }
759     else if (ascii >= ' ' && ascii <= 'Z')
760       keysym = XK_space + (KeySym)(ascii - ' ');
761     else if (ascii == '^')
762       keysym = XK_asciicircum;
763     else if (ascii == '_')
764       keysym = XK_underscore;
765     else if (ascii == 'Ä')
766       keysym = XK_Adiaeresis;
767     else if (ascii == 'Ö')
768       keysym = XK_Odiaeresis;
769     else if (ascii == 'Ü')
770       keysym = XK_Udiaeresis;
771     else if (ascii == 'ä')
772       keysym = XK_adiaeresis;
773     else if (ascii == 'ö')
774       keysym = XK_odiaeresis;
775     else if (ascii == 'ü')
776       keysym = XK_udiaeresis;
777     else if (ascii == 'ß')
778       keysym = XK_ssharp;
779
780     NewKeyEvent(KeyPress, keysym);
781   }
782   else if (key_shifts & (KB_SHIFT_FLAG | KB_CTRL_FLAG | KB_ALT_FLAG))
783   {
784     /* the allegro function keypressed() does not give us single pressed
785        modifier keys, so we must detect them with the internal global
786        allegro variable 'key_shifts' and then handle them separately */
787
788     HandleKeyboardRaw(HANDLE_RAW_KB_MODIFIER_KEYS_ONLY);
789   }
790 }
791
792 int XPending(Display *display)
793 {
794   XButtonEvent *xbutton;
795   XMotionEvent *xmotion;
796   int i;
797
798   /* When using 'HandleKeyboardRaw()', keyboard input is also stored in
799      the allegro keyboard input buffer and would be available a second
800      time by calling 'HandleKeyboardEvent()'. To avoid double keyboard
801      events, the allegro function 'clear_keybuf()' must be called each
802      time when switching from calling 'HandleKeyboardRaw()' to calling
803      'HandleKeyboardEvent()' to get keyboard input, which is actually
804      done by 'XAutoRepeatOn()' which sets keyboard_auto_repeat to TRUE. */
805
806   /* keyboard event */
807   if (keyboard_auto_repeat)
808     HandleKeyboardEvent();
809   else
810     HandleKeyboardRaw(HANDLE_RAW_KB_ALL_KEYS);
811
812   /* mouse motion event */
813   if (mouse_pos != last_mouse_pos)
814   {
815     last_mouse_pos = mouse_pos;
816     pending_events++;
817     xmotion = (XMotionEvent *)&event_buffer[pending_events];
818     xmotion->type = MotionNotify;
819     xmotion->x = mouse_x - AllegroDefaultScreen().x;
820     xmotion->y = mouse_y - AllegroDefaultScreen().y;
821   }
822
823   /* mouse button event */
824   if (mouse_b != last_mouse_b)
825   {
826     for (i=0; i<3; i++)         /* check all three mouse buttons */
827     {
828       int bitmask = (1 << i);
829
830       if ((last_mouse_b & bitmask) != (mouse_b & bitmask))
831       {
832         int mapping[3] = { 1, 3, 2 };
833
834         pending_events++;
835         xbutton = (XButtonEvent *)&event_buffer[pending_events];
836         xbutton->type = (mouse_b & bitmask ? ButtonPress : ButtonRelease);
837         xbutton->button = mapping[i];
838         xbutton->x = mouse_x - AllegroDefaultScreen().x;
839         xbutton->y = mouse_y - AllegroDefaultScreen().y;
840       }
841     }
842     last_mouse_b = mouse_b;
843   }
844
845   return pending_events;
846 }
847
848 KeySym XLookupKeysym(XKeyEvent *key_event, int index)
849 {
850   return key_event->state;
851 }
852
853 int XLookupString(XKeyEvent *key_event, char *buffer, int buffer_size,
854                   KeySym *key, XComposeStatus *compose)
855 {
856   *key = key_event->state;
857   return 0;
858 }
859
860 void XSetForeground(Display *display, GC gc, unsigned long pixel)
861 {
862   XGCValues *gcv = (XGCValues *)gc;
863
864   gcv->foreground = pixel;
865 }
866
867 void XDrawLine(Display *display, Drawable d, GC gc,
868                int x1, int y1, int x2, int y2)
869 {
870   XGCValues *gcv = (XGCValues *)gc;
871   boolean mouse_off = FALSE;
872
873   if ((BITMAP *)d == video_bitmap)
874   {
875     x1 += AllegroDefaultScreen().x;
876     y1 += AllegroDefaultScreen().y;
877     x2 += AllegroDefaultScreen().x;
878     y2 += AllegroDefaultScreen().y;
879     freeze_mouse_flag = TRUE;
880     mouse_off = hide_mouse(display, MIN(x1, x2), MIN(y1, y2),
881                            MAX(x1, x2) - MIN(x1, x2),
882                            MAX(y1, y2) - MIN(y1, y2));
883   }
884
885   line((BITMAP *)d, x1, y1, x2, y2, gcv->foreground);
886
887   if (mouse_off)
888     unhide_mouse(display);
889
890   freeze_mouse_flag = FALSE;
891 }
892
893 void XDestroyImage(XImage *ximage)
894 {
895 }
896
897 void XDestroyWindow(Display *display, Window window)
898 {
899 }
900
901 Bool XQueryPointer(Display *display, Window window,
902                    Window *root, Window *child, int *root_x, int *root_y,
903                    int *win_x, int *win_y, unsigned int *mask)
904 {
905   *win_x = mouse_x - AllegroDefaultScreen().x;
906   *win_y = mouse_y - AllegroDefaultScreen().y;
907
908   return True;
909 }
910
911 void XAutoRepeatOn(Display *display)
912 {
913   keyboard_auto_repeat = TRUE;
914   clear_keybuf();
915 }
916
917 void XAutoRepeatOff(Display *display)
918 {
919   keyboard_auto_repeat = FALSE;
920 }
921
922 void AllegroDrawLine(Drawable d, int from_x, int from_y, int to_x, int to_y,
923                      Pixel color)
924 {
925   boolean mouse_off = FALSE;
926
927   if ((BITMAP *)d == video_bitmap)
928   {
929     int dx = AllegroDefaultScreen().x;
930     int dy = AllegroDefaultScreen().y;
931     int x1, y1, x2, y2;
932
933     from_x += dx;
934     from_y += dy;
935     to_x += dx;
936     to_y += dy;
937
938     x1 = (from_x < to_x ? from_x : to_x);
939     y1 = (from_y < to_y ? from_y : to_y);
940     x2 = (from_x < to_x ? to_x : from_x);
941     y2 = (from_y < to_y ? to_y : from_y);
942
943     freeze_mouse_flag = TRUE;
944     mouse_off = hide_mouse(display, x1, y1, x2 - x1 + 1, y2 - y1 + 1);
945   }
946
947   line((BITMAP *)d, from_x, from_y, to_x, to_y, color);
948
949   if (mouse_off)
950     unhide_mouse(display);
951
952   freeze_mouse_flag = FALSE;
953 }
954
955 Bool MSDOSOpenAudio(void)
956 {
957   return allegro_init_audio();
958 }
959
960 void MSDOSCloseAudio(void)
961 {
962   /* nothing to be done here */
963 }
964
965 void NetworkServer(int port, int serveronly)
966 {
967   Error(ERR_WARN, "networking not supported in DOS version");
968 }
969
970 #endif /* PLATFORM_MSDOS */