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