rnd-19990205-1
[rocksndiamonds.git] / src / 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 #ifdef MSDOS
16
17 #include "main.h"
18 #include "misc.h"
19 #include "tools.h"
20 #include "sound.h"
21 #include "files.h"
22 #include "joystick.h"
23 #include "image.h"
24 #include "pcx.h"
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_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   reserve_voices(MAX_SOUNDS_PLAYING, 0);
92   if (install_sound(DIGI_AUTODETECT, MIDI_NONE, NULL) == -1)
93     if (install_sound(DIGI_SB, MIDI_NONE, NULL) == -1)
94       sound_status = SOUND_OFF;
95 }
96
97 static boolean hide_mouse(Display *display, int x, int y,
98                           unsigned int width, unsigned int height)
99 {
100   if (mouse_x + display->mouse_ptr->w < x || mouse_x > x + width)
101     return FALSE;
102   if (mouse_y + display->mouse_ptr->h < y || mouse_y > y + height)
103     return FALSE;
104
105   show_mouse(NULL);
106
107   return TRUE;
108 }
109
110 static void unhide_mouse(Display *display)
111 {
112   if (mouse_installed)
113     show_mouse(video_bitmap);
114 }
115
116 static KeySym ScancodeToKeySym(byte scancode)
117 {
118   switch(scancode)
119   {
120     case KEY_ESC:               return XK_Escape;
121     case KEY_1:                 return XK_1;
122     case KEY_2:                 return XK_2;
123     case KEY_3:                 return XK_3;
124     case KEY_4:                 return XK_4;
125     case KEY_5:                 return XK_5;
126     case KEY_6:                 return XK_6;
127     case KEY_7:                 return XK_7;
128     case KEY_8:                 return XK_8;
129     case KEY_9:                 return XK_9;
130     case KEY_0:                 return XK_0;
131     case KEY_MINUS:             return XK_minus;
132     case KEY_EQUALS:            return XK_equal;
133     case KEY_BACKSPACE:         return XK_BackSpace;
134     case KEY_TAB:               return XK_Tab;
135     case KEY_Q:                 return XK_q;
136     case KEY_W:                 return XK_w;
137     case KEY_E:                 return XK_e;
138     case KEY_R:                 return XK_r;
139     case KEY_T:                 return XK_t;
140     case KEY_Y:                 return XK_y;
141     case KEY_U:                 return XK_u;
142     case KEY_I:                 return XK_i;
143     case KEY_O:                 return XK_o;
144     case KEY_P:                 return XK_p;
145     case KEY_OPENBRACE:         return XK_braceleft;
146     case KEY_CLOSEBRACE:        return XK_braceright;
147     case KEY_ENTER:             return XK_Return;
148     case KEY_LCONTROL:          return XK_Control_L;
149     case KEY_A:                 return XK_a;
150     case KEY_S:                 return XK_s;
151     case KEY_D:                 return XK_d;
152     case KEY_F:                 return XK_f;
153     case KEY_G:                 return XK_g;
154     case KEY_H:                 return XK_h;
155     case KEY_J:                 return XK_j;
156     case KEY_K:                 return XK_k;
157     case KEY_L:                 return XK_l;
158     case KEY_COLON:             return XK_colon;
159     case KEY_QUOTE:             return XK_apostrophe;
160     case KEY_TILDE:             return XK_asciitilde;
161     case KEY_LSHIFT:            return XK_Shift_L;
162     case KEY_BACKSLASH:         return XK_backslash;
163     case KEY_Z:                 return XK_z;
164     case KEY_X:                 return XK_x;
165     case KEY_C:                 return XK_c;
166     case KEY_V:                 return XK_v;
167     case KEY_B:                 return XK_b;
168     case KEY_N:                 return XK_n;
169     case KEY_M:                 return XK_m;
170     case KEY_COMMA:             return XK_comma;
171     case KEY_STOP:              return XK_period;
172     case KEY_SLASH:             return XK_slash;
173     case KEY_RSHIFT:            return XK_Shift_R;
174     case KEY_ASTERISK:          return XK_KP_Multiply;
175     case KEY_ALT:               return XK_Alt_L;
176     case KEY_SPACE:             return XK_space;
177     case KEY_CAPSLOCK:          return XK_Caps_Lock;
178     case KEY_F1:                return XK_F1;
179     case KEY_F2:                return XK_F2;
180     case KEY_F3:                return XK_F3;
181     case KEY_F4:                return XK_F4;
182     case KEY_F5:                return XK_F5;
183     case KEY_F6:                return XK_F6;
184     case KEY_F7:                return XK_F7;
185     case KEY_F8:                return XK_F8;
186     case KEY_F9:                return XK_F9;
187     case KEY_F10:               return XK_F10;
188     case KEY_NUMLOCK:           return XK_Num_Lock;
189     case KEY_SCRLOCK:           return XK_Scroll_Lock;
190     case KEY_HOME:              return XK_Home;
191     case KEY_UP:                return XK_Up;
192     case KEY_PGUP:              return XK_Page_Up;
193     case KEY_MINUS_PAD:         return XK_KP_Subtract;
194     case KEY_LEFT:              return XK_Left;
195     case KEY_5_PAD:             return XK_KP_5;
196     case KEY_RIGHT:             return XK_Right;
197     case KEY_PLUS_PAD:          return XK_KP_Add;
198     case KEY_END:               return XK_End;
199     case KEY_DOWN:              return XK_Down;
200     case KEY_PGDN:              return XK_Page_Down;
201     case KEY_INSERT:            return XK_Insert;
202     case KEY_DEL:               return XK_Delete;
203     case KEY_PRTSCR:            return XK_Print;
204     case KEY_F11:               return XK_F11;
205     case KEY_F12:               return XK_F12;
206     case KEY_LWIN:              return XK_Meta_L;
207     case KEY_RWIN:              return XK_Meta_R;
208     case KEY_MENU:              return XK_Menu;
209     case KEY_PAD:               return XK_VoidSymbol;
210     case KEY_RCONTROL:          return XK_Control_R;
211     case KEY_ALTGR:             return XK_Alt_R;
212     case KEY_SLASH2:            return XK_KP_Divide;
213     case KEY_PAUSE:             return XK_Pause;
214
215     case NEW_KEY_BACKSLASH:     return XK_backslash;
216     case NEW_KEY_1_PAD:         return XK_KP_1;
217     case NEW_KEY_2_PAD:         return XK_KP_2;
218     case NEW_KEY_3_PAD:         return XK_KP_3;
219     case NEW_KEY_4_PAD:         return XK_KP_4;
220     case NEW_KEY_5_PAD:         return XK_KP_5;
221     case NEW_KEY_6_PAD:         return XK_KP_6;
222     case NEW_KEY_7_PAD:         return XK_KP_7;
223     case NEW_KEY_8_PAD:         return XK_KP_8;
224     case NEW_KEY_9_PAD:         return XK_KP_9;
225     case NEW_KEY_0_PAD:         return XK_KP_0;
226     case NEW_KEY_STOP_PAD:      return XK_KP_Separator;
227     case NEW_KEY_EQUALS_PAD:    return XK_KP_Equal;
228     case NEW_KEY_SLASH_PAD:     return XK_KP_Divide;
229     case NEW_KEY_ASTERISK_PAD:  return XK_KP_Multiply;
230     case NEW_KEY_ENTER_PAD:     return XK_KP_Enter;
231
232     default:                    return XK_VoidSymbol;
233   }
234 }
235
236 void XMapWindow(Display *display, Window window)
237 {
238   int x, y;
239   unsigned int width, height;
240   boolean mouse_off;
241
242   x = display->screens[display->default_screen].x;
243   y = display->screens[display->default_screen].y;
244   width = display->screens[display->default_screen].width;
245   height = display->screens[display->default_screen].height;
246
247   mouse_off = hide_mouse(display, x, y, width, height);
248   blit((BITMAP *)window, video_bitmap, 0, 0, x, y, width, height);
249
250   if (mouse_off)
251     unhide_mouse(display);
252 }
253
254 static unsigned long AllocColorCell(int r, int g, int b)
255 {
256   byte pixel_mapping = 0;
257   int i;
258
259   r >>= 10;
260   g >>= 10;
261   b >>= 10;
262
263   /* try to use existing colors from the global colormap */
264   for (i=0; i<global_colormap_entries_used; i++)
265   {
266     if (r == global_colormap[i].r &&
267         g == global_colormap[i].g &&
268         b == global_colormap[i].b)              /* color found */
269     {
270       pixel_mapping = i;
271       break;
272     }
273   }
274
275   if (i == global_colormap_entries_used)        /* color not found */
276   {
277     if (global_colormap_entries_used < MAX_COLORS)
278       global_colormap_entries_used++;
279
280     global_colormap[i].r = r;
281     global_colormap[i].g = g;
282     global_colormap[i].b = b;
283
284     pixel_mapping = i;
285   }
286
287   return pixel_mapping;
288 }
289
290 Display *XOpenDisplay(char *display_name)
291 {
292   Screen *screen;
293   Display *display;
294   BITMAP *mouse_bitmap = NULL;
295   char *filename;
296
297   filename = getPath3(options.base_directory, GRAPHICS_DIRECTORY,
298                       MOUSE_FILENAME);
299
300   mouse_bitmap = Read_PCX_to_AllegroBitmap(filename);
301   free(filename);
302
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_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   display->screens[display->default_screen].video_bitmap = video_bitmap;
345   display->screens[display->default_screen].x = x;
346   display->screens[display->default_screen].y = y;
347   display->screens[display->default_screen].width = XRES;
348   display->screens[display->default_screen].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(display->screens[display->default_screen].x + 1,
358                   display->screens[display->default_screen].y + 1,
359                   display->screens[display->default_screen].x + WIN_XSIZE + 1,
360                   display->screens[display->default_screen].y + WIN_YSIZE + 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 += display->screens[display->default_screen].x;
430     y += display->screens[display->default_screen].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 == FXSIZE && height == FYSIZE)
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 += display->screens[display->default_screen].x;
474     src_y += display->screens[display->default_screen].y;
475   }
476
477   if ((BITMAP *)dest == video_bitmap)
478   {
479     dest_x += display->screens[display->default_screen].x;
480     dest_y += display->screens[display->default_screen].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     return NULL;
520
521   clear(bitmap);
522
523   /* try to use existing colors from the global colormap */
524   for (i=0; i<MAX_COLORS; i++)
525   {
526
527 #if 0
528     int r, g, b;
529 #endif
530
531     if (!image->rgb.color_used[i])
532       continue;
533
534
535 #if 0
536     r = image->rgb.red[i] >> 10;
537     g = image->rgb.green[i] >> 10;
538     b = image->rgb.blue[i] >> 10;
539
540     for (j=0; j<global_colormap_entries_used; j++)
541     {
542       if (r == global_colormap[j].r &&
543           g == global_colormap[j].g &&
544           b == global_colormap[j].b)            /* color found */
545       {
546         pixel_mapping[i] = j;
547         break;
548       }
549     }
550
551     if (j == global_colormap_entries_used)      /* color not found */
552     {
553       if (global_colormap_entries_used < MAX_COLORS)
554         global_colormap_entries_used++;
555
556       global_colormap[j].r = r;
557       global_colormap[j].g = g;
558       global_colormap[j].b = b;
559
560       pixel_mapping[i] = j;
561     }
562 #else
563     pixel_mapping[i] = AllocColorCell(image->rgb.red[i],
564                                       image->rgb.green[i],
565                                       image->rgb.blue[i]);
566 #endif
567
568   }
569
570   /* copy bitmap data */
571   for (y=0; y<image->height; y++)
572     for (x=0; x<image->width; x++)
573       putpixel(bitmap, x, y, pixel_mapping[*src_ptr++]);
574
575   return bitmap;
576 }
577
578 static BITMAP *Read_PCX_to_AllegroBitmap(char *filename)
579 {
580   BITMAP *bitmap;
581   Image *image;
582
583   /* read the graphic file in PCX format to internal image structure */
584   if ((image = Read_PCX_to_Image(filename)) == NULL)
585   {
586     Error(ERR_RETURN, "Read_PCX_to_Image failed");
587     return NULL;
588   }
589
590   /* convert internal image structure to allegro bitmap structure */
591   if ((bitmap = Image_to_AllegroBitmap(image)) == NULL)
592   {
593     Error(ERR_RETURN, "Image_to_AllegroBitmap failed");
594     return NULL;
595   }
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 PCX_FileInvalid;
609
610   *pixmap = (Pixmap)bitmap;
611   *pixmap_mask = (Pixmap)bitmap;
612
613   return PCX_Success;
614 }
615
616 int XpmReadFileToPixmap(Display *display, Drawable d, char *filename,
617                         Pixmap *pixmap_return, Pixmap *shapemask_return,
618                         XpmAttributes *attributes)
619 {
620   BITMAP *bitmap;
621
622   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
623     return XpmOpenFailed;
624
625   *pixmap_return = (Pixmap)bitmap;
626
627   return XpmSuccess;
628 }
629
630 int XReadBitmapFile(Display *display, Drawable d, char *filename,
631                     unsigned int *width_return, unsigned int *height_return,
632                     Pixmap *bitmap_return,
633                     int *x_hot_return, int *y_hot_return)
634 {
635   BITMAP *bitmap;
636
637   if ((bitmap = Read_PCX_to_AllegroBitmap(filename)) == NULL)
638     return BitmapOpenFailed;
639
640   *width_return = bitmap->w;
641   *height_return = bitmap->h;
642   *x_hot_return = -1;
643   *y_hot_return = -1;
644   *bitmap_return = (Pixmap)bitmap;
645
646   return BitmapSuccess;
647 }
648
649 void XFreePixmap(Display *display, Pixmap pixmap)
650 {
651   if (pixmap != DUMMY_MASK &&
652       (is_memory_bitmap((BITMAP *)pixmap) ||
653        is_screen_bitmap((BITMAP *)pixmap)))
654     destroy_bitmap((BITMAP *)pixmap);
655 }
656
657 void XFreeGC(Display *display, GC gc)
658 {
659   XGCValues *gcv;
660
661   gcv = (XGCValues *)gc;
662   if (gcv)
663     free(gcv);
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 - display->screens[display->default_screen].x;
834     xmotion->y = mouse_y - display->screens[display->default_screen].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 - display->screens[display->default_screen].x;
853         xbutton->y = mouse_y - display->screens[display->default_screen].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 += display->screens[display->default_screen].x;
890     y1 += display->screens[display->default_screen].y;
891     x2 += display->screens[display->default_screen].x;
892     y2 += display->screens[display->default_screen].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 Bool XQueryPointer(Display *display, Window window,
912                    Window *root, Window *child, int *root_x, int *root_y,
913                    int *win_x, int *win_y, unsigned int *mask)
914 {
915   *win_x = mouse_x - display->screens[display->default_screen].x;
916   *win_y = mouse_y - display->screens[display->default_screen].y;
917
918   return True;
919 }
920
921 void XAutoRepeatOn(Display *display)
922 {
923   keyboard_auto_repeat = TRUE;
924   clear_keybuf();
925 }
926
927 void XAutoRepeatOff(Display *display)
928 {
929   keyboard_auto_repeat = FALSE;
930 }
931
932 void NetworkServer(int port, int serveronly)
933 {
934   Error(ERR_WARN, "networking not supported in DOS version");
935 }
936
937 #endif /* MSDOS */