test/checkkeys.c
changeset 5461 94f742ce580a
parent 5218 572a73d71b5f
child 5535 96594ac5fd1a
--- a/test/checkkeys.c	Wed Mar 09 15:51:51 2011 -0800
+++ b/test/checkkeys.c	Wed Mar 09 17:38:12 2011 -0800
@@ -19,92 +19,124 @@
 }
 
 static void
-print_modifiers(void)
+print_string(char **text, size_t *maxlen, const char *fmt, ...)
+{
+    int len;
+    va_list ap;
+
+    va_start(ap, fmt);
+    len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
+    if (len > 0) {
+        *text += len;
+        if (len < *maxlen) {
+            *maxlen -= len;
+        } else {
+            *maxlen = 0;
+        }
+    }
+    va_end(ap);
+}
+
+static void
+print_modifiers(char **text, size_t *maxlen)
 {
     int mod;
-    printf(" modifiers:");
+    print_string(text, maxlen, " modifiers:");
     mod = SDL_GetModState();
     if (!mod) {
-        printf(" (none)");
+        print_string(text, maxlen, " (none)");
         return;
     }
     if (mod & KMOD_LSHIFT)
-        printf(" LSHIFT");
+        print_string(text, maxlen, " LSHIFT");
     if (mod & KMOD_RSHIFT)
-        printf(" RSHIFT");
+        print_string(text, maxlen, " RSHIFT");
     if (mod & KMOD_LCTRL)
-        printf(" LCTRL");
+        print_string(text, maxlen, " LCTRL");
     if (mod & KMOD_RCTRL)
-        printf(" RCTRL");
+        print_string(text, maxlen, " RCTRL");
     if (mod & KMOD_LALT)
-        printf(" LALT");
+        print_string(text, maxlen, " LALT");
     if (mod & KMOD_RALT)
-        printf(" RALT");
+        print_string(text, maxlen, " RALT");
     if (mod & KMOD_LGUI)
-        printf(" LGUI");
+        print_string(text, maxlen, " LGUI");
     if (mod & KMOD_RGUI)
-        printf(" RGUI");
+        print_string(text, maxlen, " RGUI");
     if (mod & KMOD_NUM)
-        printf(" NUM");
+        print_string(text, maxlen, " NUM");
     if (mod & KMOD_CAPS)
-        printf(" CAPS");
+        print_string(text, maxlen, " CAPS");
     if (mod & KMOD_MODE)
-        printf(" MODE");
+        print_string(text, maxlen, " MODE");
 }
 
 static void
 PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
 {
+    char message[512];
+    char *spot;
+    size_t left;
+
+    spot = message;
+    left = sizeof(message);
+
     /* Print the keycode, name and state */
     if (sym->sym) {
-        printf("Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
-               pressed ? "pressed " : "released",
-               sym->scancode,
-               SDL_GetScancodeName(sym->scancode),
-               sym->sym, SDL_GetKeyName(sym->sym));
+        print_string(&spot, &left,
+                "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
+                pressed ? "pressed " : "released",
+                sym->scancode,
+                SDL_GetScancodeName(sym->scancode),
+                sym->sym, SDL_GetKeyName(sym->sym));
     } else {
-        printf("Unknown Key (scancode %d = %s) %s ",
-               sym->scancode,
-               SDL_GetScancodeName(sym->scancode),
-               pressed ? "pressed" : "released");
+        print_string(&spot, &left,
+                "Unknown Key (scancode %d = %s) %s ",
+                sym->scancode,
+                SDL_GetScancodeName(sym->scancode),
+                pressed ? "pressed" : "released");
     }
 
     /* Print the translated character, if one exists */
     if (sym->unicode) {
         /* Is it a control-character? */
         if (sym->unicode < ' ') {
-            printf(" (^%c)", sym->unicode + '@');
+            print_string(&spot, &left, " (^%c)", sym->unicode + '@');
         } else {
 #ifdef UNICODE
-            printf(" (%c)", sym->unicode);
+            print_string(&spot, &left, " (%c)", sym->unicode);
 #else
             /* This is a Latin-1 program, so only show 8-bits */
             if (!(sym->unicode & 0xFF00))
-                printf(" (%c)", sym->unicode);
+                print_string(&spot, &left, " (%c)", sym->unicode);
             else
-                printf(" (0x%X)", sym->unicode);
+                print_string(&spot, &left, " (0x%X)", sym->unicode);
 #endif
         }
     }
-    print_modifiers();
+    print_modifiers(&spot, &left);
     if (repeat) {
-        printf(" (repeat)");
+        print_string(&spot, &left, " (repeat)");
     }
-    printf("\n");
+    SDL_Log("%s", message);
 }
 
 static void
 PrintText(char *text)
 {
-    printf("Text: %s\n", text);
+    SDL_Log("Text: %s", text);
 }
 
+#if __IPHONEOS__
+extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
+#endif
+
 int
 main(int argc, char *argv[])
 {
+    SDL_Window *window;
     SDL_Event event;
     int done;
-    Uint32 videoflags;
 
     /* Initialize SDL */
     if (SDL_Init(SDL_INIT_VIDEO) < 0) {
@@ -112,24 +144,23 @@
         return (1);
     }
 
-    videoflags = SDL_SWSURFACE;
-    while (argc > 1) {
-        --argc;
-        if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
-            videoflags |= SDL_FULLSCREEN;
-        } else {
-            fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
-            quit(1);
-        }
-    }
-
     /* Set 640x480 video mode */
-    if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
-        fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
+    window = SDL_CreateWindow("CheckKeys Test",
+                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
+                              640, 480, 0);
+    if (!window) {
+        fprintf(stderr, "Couldn't create 640x480 window: %s\n",
                 SDL_GetError());
         quit(2);
     }
 
+#if __IPHONEOS__
+    /* Creating the context creates the view, which we need to show keyboard */
+    SDL_GL_CreateContext(window);
+    SDL_iPhoneKeyboardShow(window);
+#endif
+    
+
     /* Watch keystrokes */
     done = 0;
     while (!done) {
@@ -156,3 +187,5 @@
     SDL_Quit();
     return (0);
 }
+
+/* vi: set ts=4 sw=4 expandtab: */