Re: [AD] XIM patch for Allegro 4.1.x

[ Thread Index | Date Index | More lists.liballeg.org/allegro-developers Archives ]


Er, here is the patch.
Index: src/keyboard.c
===================================================================
RCS file: /cvsroot/alleg/allegro/src/keyboard.c,v
retrieving revision 1.9
diff -u -p -r1.9 keyboard.c
--- src/keyboard.c	27 Jul 2004 10:33:22 -0000	1.9
+++ src/keyboard.c	20 Nov 2004 20:32:27 -0000
@@ -52,6 +52,29 @@ static int repeat_scan = -1;
 
 static int rate_changed = FALSE;
 
+/* Provide a default ASCII mapping for the most common keys. Keys whose
+ * mapping changes dependind on the layout aren't listed - it's up to
+ * the keyboard driver to do that.
+ */
+static int common_ascii[1 + KEY_MAX] =
+{
+   0,   'a', 'b', 'c', 'd', 'e', 'f', 'g',
+   'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
+   'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
+   'x', 'y', 'z', '0', '1', '2', '3', '4',
+   '5', '6', '7', '8', '9', '0', '1', '2',
+   '3', '4', '5', '6', '7', '8', '9', 0,
+   0,   0,   0,   0,   0,   0,   0,   0,
+   0,   0,   0,   27,  0,   0,   0,   0,
+   9,   0,   0,   13,  0,   0,   0,   0,
+   0,   0,   0,   ' ', 0,   0,   0,   0,
+   0,   0,   0,   0,   0,   0,   '/', '*',
+   '-', '+', 0,  13,   0,   0,   0,   0,
+   0,   0,   0,   0,   0,   0,   0,   '=',
+   0,   0,   0,   0,   0,   0,   0,   0,
+   0,   0,   0,   0,   0,   0,   0,   0,
+   0,   0,   0,   0,   0,   0,   0,   0
+};
 
 #define KEY_BUFFER_SIZE    64                /* character ring buffer */
 
@@ -516,8 +539,11 @@ int keyboard_needs_poll()
  */
 int scancode_to_ascii(int scancode)
 {
+   ASSERT (scancode >= 0 && scancode < KEY_MAX);
    if (keyboard_driver->scancode_to_ascii)
       return keyboard_driver->scancode_to_ascii(scancode);
+   else
+      return common_ascii[scancode];
 
    return 0;
 }
Index: include/allegro/keyboard.h
===================================================================
RCS file: /cvsroot/alleg/allegro/include/allegro/keyboard.h,v
retrieving revision 1.9
diff -u -p -r1.9 keyboard.h
--- include/allegro/keyboard.h	30 Oct 2004 14:19:53 -0000	1.9
+++ include/allegro/keyboard.h	20 Nov 2004 20:32:27 -0000
@@ -205,23 +205,31 @@ enum {
    __allegro_KEY_BACKQUOTE    = 104,  /* MacOS X */
    __allegro_KEY_SEMICOLON    = 105,  /* MacOS X */
    __allegro_KEY_COMMAND      = 106,  /* MacOS X */
+   __allegro_KEY_UNKNOWN1     = 107,
+   __allegro_KEY_UNKNOWN2     = 108,
+   __allegro_KEY_UNKNOWN3     = 109,
+   __allegro_KEY_UNKNOWN4     = 110,
+   __allegro_KEY_UNKNOWN5     = 111,
+   __allegro_KEY_UNKNOWN6     = 112,
+   __allegro_KEY_UNKNOWN7     = 113,
+   __allegro_KEY_UNKNOWN8     = 114,
+
+   __allegro_KEY_MODIFIERS    = 115,
+
+   __allegro_KEY_LSHIFT       = 115,
+   __allegro_KEY_RSHIFT       = 116,
+   __allegro_KEY_LCONTROL     = 117,
+   __allegro_KEY_RCONTROL     = 118,
+   __allegro_KEY_ALT          = 119,
+   __allegro_KEY_ALTGR        = 120,
+   __allegro_KEY_LWIN         = 121,
+   __allegro_KEY_RWIN         = 122,
+   __allegro_KEY_MENU         = 123,
+   __allegro_KEY_SCRLOCK      = 124,
+   __allegro_KEY_NUMLOCK      = 125,
+   __allegro_KEY_CAPSLOCK     = 126,
 
-   __allegro_KEY_MODIFIERS    = 107,
-
-   __allegro_KEY_LSHIFT       = 107,
-   __allegro_KEY_RSHIFT       = 108,
-   __allegro_KEY_LCONTROL     = 109,
-   __allegro_KEY_RCONTROL     = 110,
-   __allegro_KEY_ALT          = 111,
-   __allegro_KEY_ALTGR        = 112,
-   __allegro_KEY_LWIN         = 113,
-   __allegro_KEY_RWIN         = 114,
-   __allegro_KEY_MENU         = 115,
-   __allegro_KEY_SCRLOCK      = 116,
-   __allegro_KEY_NUMLOCK      = 117,
-   __allegro_KEY_CAPSLOCK     = 118,
-
-   __allegro_KEY_MAX          = 119
+   __allegro_KEY_MAX          = 127
 };
 
 #ifndef ALLEGRO_NO_KEY_DEFINES
@@ -348,6 +356,14 @@ enum {
 #define KEY_BACKQUOTE         __allegro_KEY_BACKQUOTE
 #define KEY_SEMICOLON         __allegro_KEY_SEMICOLON
 #define KEY_COMMAND           __allegro_KEY_COMMAND
+#define KEY_UNKNOWN1          __allegro_KEY_UNKNOWN1
+#define KEY_UNKNOWN2          __allegro_KEY_UNKNOWN2
+#define KEY_UNKNOWN3          __allegro_KEY_UNKNOWN3
+#define KEY_UNKNOWN4          __allegro_KEY_UNKNOWN4
+#define KEY_UNKNOWN5          __allegro_KEY_UNKNOWN5
+#define KEY_UNKNOWN6          __allegro_KEY_UNKNOWN6
+#define KEY_UNKNOWN7          __allegro_KEY_UNKNOWN7
+#define KEY_UNKNOWN8          __allegro_KEY_UNKNOWN8
 
 #define KEY_MODIFIERS         __allegro_KEY_MODIFIERS
 
Index: examples/exkeys.c
===================================================================
RCS file: /cvsroot/alleg/allegro/examples/exkeys.c,v
retrieving revision 1.16
diff -u -p -r1.16 exkeys.c
--- examples/exkeys.c	27 Oct 2004 04:05:46 -0000	1.16
+++ examples/exkeys.c	20 Nov 2004 20:32:27 -0000
@@ -8,6 +8,9 @@
  *    fifth test requires some focus to be passed. The final step
  *    shows how to use the global key array to read simultaneous
  *    keypresses.
+ *    The last method to detect key presses are keyboard callbacks.
+ *    This is demonstrated by by installing a keyboard callback,
+ *    which marks all pressed keys by drawing to a grid.
  */
 
 
@@ -45,21 +48,42 @@ char *key_names[] =
    "KEY_MINUS_PAD",  "KEY_PLUS_PAD",   "KEY_DEL_PAD",    "KEY_ENTER_PAD",
    "KEY_PRTSCR",     "KEY_PAUSE",      "KEY_ABNT_C1",    "KEY_YEN",
    "KEY_KANA",       "KEY_CONVERT",    "KEY_NOCONVERT",  "KEY_AT",
-   "KEY_CIRCUMFLEX", "KEY_COLON2",     "KEY_KANJI",
-   "KEY_EQUALS_PAD", "KEY_BACKQUOTE",  "KEY_SEMICOLON",  "KEY_COMMAND",
-   "KEY_LSHIFT",     "KEY_RSHIFT",     "KEY_LCONTROL",   "KEY_RCONTROL",
-   "KEY_ALT",        "KEY_ALTGR",      "KEY_LWIN",       "KEY_RWIN",
-   "KEY_MENU",       "KEY_SCRLOCK",    "KEY_NUMLOCK",    "KEY_CAPSLOCK",
-   "KEY_MAX"
+   "KEY_CIRCUMFLEX", "KEY_COLON2",     "KEY_KANJI",      "KEY_EQUALS_PAD",
+   "KEY_BACKQUOTE",  "KEY_SEMICOLON",  "KEY_COMMAND",    "KEY_UNKNOWN1",
+   "KEY_UNKNOWN2",   "KEY_UNKNOWN3",   "KEY_UNKNOWN4",   "KEY_UNKNOWN5",
+   "KEY_UNKNOWN6",   "KEY_UNKNOWN7",   "KEY_UNKNOWN8",   "KEY_LSHIFT",
+   "KEY_RSHIFT",     "KEY_LCONTROL",   "KEY_RCONTROL",   "KEY_ALT",
+   "KEY_ALTGR",      "KEY_LWIN",       "KEY_RWIN",       "KEY_MENU",
+   "KEY_SCRLOCK",    "KEY_NUMLOCK",    "KEY_CAPSLOCK",   "KEY_MAX"
 };
 
 
 
+/* Keyboard callback. We are very evil and draw to the screen from within
+ * the callback. Don't do this in your own programs ;)
+ */
+void keypress_handler(int scancode)
+{
+   int i, x, y, color, ascii;
+   i = scancode & 0x7f;
+   x = SCREEN_W - 40 * 8 + (i % 8) * 40;
+   y = SCREEN_H / 2 + (i / 8 - 8) * 20;
+   color = scancode & 0x80 ? makecol (255, 255, 0) : makecol (128, 0, 0);
+   rectfill (screen, x, y, x + 35, y + 15, color);
+   ascii = scancode_to_ascii (i);
+   if (ascii)
+      textprintf_ex (screen, font, x + 3, y + 3, makecol (0, 0, 0), -1,
+            "%c", ascii);
+}
+END_OF_FUNCTION(keypress_handler)
+
+
+
 /* helper function for making more room on the screen */
 void scroll(void)
 {
-   blit(screen, screen, 0, 32, 0, 24, SCREEN_W, SCREEN_H-32);
-   rectfill(screen, 0, SCREEN_H-16, SCREEN_W, SCREEN_H, makecol(255, 255, 255));
+   blit(screen, screen, 0, 32, 0, 24, SCREEN_W / 2, SCREEN_H-32);
+   rectfill(screen, 0, SCREEN_H-16, SCREEN_W / 2, SCREEN_H, makecol(255, 255, 255));
 }
 
 
@@ -73,8 +97,8 @@ int main(void)
       return 1;
    install_keyboard();
 
-   if (set_gfx_mode(GFX_AUTODETECT, 320, 200, 0, 0) != 0) {
-      if (set_gfx_mode(GFX_SAFE, 320, 200, 0, 0) != 0) {
+   if (set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0) != 0) {
+      if (set_gfx_mode(GFX_SAFE, 640, 480, 0, 0) != 0) {
 	 set_gfx_mode(GFX_TEXT, 0, 0, 0, 0);
 	 allegro_message("Unable to set any graphic mode\n%s\n", allegro_error);
 	 return 1;
@@ -83,8 +107,17 @@ int main(void)
 
    set_palette(desktop_palette);
 
-   acquire_screen();
    clear_to_color(screen, makecol(255, 255, 255));
+
+   /* Draw the initial keys grid by simulating release of every key. */
+   for (k = 0; k < KEY_MAX; k++)
+      keypress_handler (k + 0x80);
+
+   /* Install our keyboard callback. */
+   LOCK_FUNCTION(keypress_handler);
+   keyboard_lowlevel_callback = keypress_handler;
+
+   acquire_screen();
    textprintf_centre_ex(screen, font, SCREEN_W/2, 8, makecol(0, 0, 0), makecol(255, 255, 255),
 			"Driver: %s", keyboard_driver->name);
 
@@ -204,6 +237,7 @@ int main(void)
    } while (!keypressed() || (readkey() >> 8) != KEY_ESC);
 
    clear_keybuf();
+   keyboard_lowlevel_callback = NULL;
    return 0;
 }
 


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/