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