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