test/checkkeys.c
changeset 5461 94f742ce580a
parent 5218 572a73d71b5f
child 5535 96594ac5fd1a
     1.1 --- a/test/checkkeys.c	Wed Mar 09 15:51:51 2011 -0800
     1.2 +++ b/test/checkkeys.c	Wed Mar 09 17:38:12 2011 -0800
     1.3 @@ -19,92 +19,124 @@
     1.4  }
     1.5  
     1.6  static void
     1.7 -print_modifiers(void)
     1.8 +print_string(char **text, size_t *maxlen, const char *fmt, ...)
     1.9 +{
    1.10 +    int len;
    1.11 +    va_list ap;
    1.12 +
    1.13 +    va_start(ap, fmt);
    1.14 +    len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
    1.15 +    if (len > 0) {
    1.16 +        *text += len;
    1.17 +        if (len < *maxlen) {
    1.18 +            *maxlen -= len;
    1.19 +        } else {
    1.20 +            *maxlen = 0;
    1.21 +        }
    1.22 +    }
    1.23 +    va_end(ap);
    1.24 +}
    1.25 +
    1.26 +static void
    1.27 +print_modifiers(char **text, size_t *maxlen)
    1.28  {
    1.29      int mod;
    1.30 -    printf(" modifiers:");
    1.31 +    print_string(text, maxlen, " modifiers:");
    1.32      mod = SDL_GetModState();
    1.33      if (!mod) {
    1.34 -        printf(" (none)");
    1.35 +        print_string(text, maxlen, " (none)");
    1.36          return;
    1.37      }
    1.38      if (mod & KMOD_LSHIFT)
    1.39 -        printf(" LSHIFT");
    1.40 +        print_string(text, maxlen, " LSHIFT");
    1.41      if (mod & KMOD_RSHIFT)
    1.42 -        printf(" RSHIFT");
    1.43 +        print_string(text, maxlen, " RSHIFT");
    1.44      if (mod & KMOD_LCTRL)
    1.45 -        printf(" LCTRL");
    1.46 +        print_string(text, maxlen, " LCTRL");
    1.47      if (mod & KMOD_RCTRL)
    1.48 -        printf(" RCTRL");
    1.49 +        print_string(text, maxlen, " RCTRL");
    1.50      if (mod & KMOD_LALT)
    1.51 -        printf(" LALT");
    1.52 +        print_string(text, maxlen, " LALT");
    1.53      if (mod & KMOD_RALT)
    1.54 -        printf(" RALT");
    1.55 +        print_string(text, maxlen, " RALT");
    1.56      if (mod & KMOD_LGUI)
    1.57 -        printf(" LGUI");
    1.58 +        print_string(text, maxlen, " LGUI");
    1.59      if (mod & KMOD_RGUI)
    1.60 -        printf(" RGUI");
    1.61 +        print_string(text, maxlen, " RGUI");
    1.62      if (mod & KMOD_NUM)
    1.63 -        printf(" NUM");
    1.64 +        print_string(text, maxlen, " NUM");
    1.65      if (mod & KMOD_CAPS)
    1.66 -        printf(" CAPS");
    1.67 +        print_string(text, maxlen, " CAPS");
    1.68      if (mod & KMOD_MODE)
    1.69 -        printf(" MODE");
    1.70 +        print_string(text, maxlen, " MODE");
    1.71  }
    1.72  
    1.73  static void
    1.74  PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
    1.75  {
    1.76 +    char message[512];
    1.77 +    char *spot;
    1.78 +    size_t left;
    1.79 +
    1.80 +    spot = message;
    1.81 +    left = sizeof(message);
    1.82 +
    1.83      /* Print the keycode, name and state */
    1.84      if (sym->sym) {
    1.85 -        printf("Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    1.86 -               pressed ? "pressed " : "released",
    1.87 -               sym->scancode,
    1.88 -               SDL_GetScancodeName(sym->scancode),
    1.89 -               sym->sym, SDL_GetKeyName(sym->sym));
    1.90 +        print_string(&spot, &left,
    1.91 +                "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
    1.92 +                pressed ? "pressed " : "released",
    1.93 +                sym->scancode,
    1.94 +                SDL_GetScancodeName(sym->scancode),
    1.95 +                sym->sym, SDL_GetKeyName(sym->sym));
    1.96      } else {
    1.97 -        printf("Unknown Key (scancode %d = %s) %s ",
    1.98 -               sym->scancode,
    1.99 -               SDL_GetScancodeName(sym->scancode),
   1.100 -               pressed ? "pressed" : "released");
   1.101 +        print_string(&spot, &left,
   1.102 +                "Unknown Key (scancode %d = %s) %s ",
   1.103 +                sym->scancode,
   1.104 +                SDL_GetScancodeName(sym->scancode),
   1.105 +                pressed ? "pressed" : "released");
   1.106      }
   1.107  
   1.108      /* Print the translated character, if one exists */
   1.109      if (sym->unicode) {
   1.110          /* Is it a control-character? */
   1.111          if (sym->unicode < ' ') {
   1.112 -            printf(" (^%c)", sym->unicode + '@');
   1.113 +            print_string(&spot, &left, " (^%c)", sym->unicode + '@');
   1.114          } else {
   1.115  #ifdef UNICODE
   1.116 -            printf(" (%c)", sym->unicode);
   1.117 +            print_string(&spot, &left, " (%c)", sym->unicode);
   1.118  #else
   1.119              /* This is a Latin-1 program, so only show 8-bits */
   1.120              if (!(sym->unicode & 0xFF00))
   1.121 -                printf(" (%c)", sym->unicode);
   1.122 +                print_string(&spot, &left, " (%c)", sym->unicode);
   1.123              else
   1.124 -                printf(" (0x%X)", sym->unicode);
   1.125 +                print_string(&spot, &left, " (0x%X)", sym->unicode);
   1.126  #endif
   1.127          }
   1.128      }
   1.129 -    print_modifiers();
   1.130 +    print_modifiers(&spot, &left);
   1.131      if (repeat) {
   1.132 -        printf(" (repeat)");
   1.133 +        print_string(&spot, &left, " (repeat)");
   1.134      }
   1.135 -    printf("\n");
   1.136 +    SDL_Log("%s", message);
   1.137  }
   1.138  
   1.139  static void
   1.140  PrintText(char *text)
   1.141  {
   1.142 -    printf("Text: %s\n", text);
   1.143 +    SDL_Log("Text: %s", text);
   1.144  }
   1.145  
   1.146 +#if __IPHONEOS__
   1.147 +extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
   1.148 +#endif
   1.149 +
   1.150  int
   1.151  main(int argc, char *argv[])
   1.152  {
   1.153 +    SDL_Window *window;
   1.154      SDL_Event event;
   1.155      int done;
   1.156 -    Uint32 videoflags;
   1.157  
   1.158      /* Initialize SDL */
   1.159      if (SDL_Init(SDL_INIT_VIDEO) < 0) {
   1.160 @@ -112,24 +144,23 @@
   1.161          return (1);
   1.162      }
   1.163  
   1.164 -    videoflags = SDL_SWSURFACE;
   1.165 -    while (argc > 1) {
   1.166 -        --argc;
   1.167 -        if (argv[argc] && !strcmp(argv[argc], "-fullscreen")) {
   1.168 -            videoflags |= SDL_FULLSCREEN;
   1.169 -        } else {
   1.170 -            fprintf(stderr, "Usage: %s [-fullscreen]\n", argv[0]);
   1.171 -            quit(1);
   1.172 -        }
   1.173 -    }
   1.174 -
   1.175      /* Set 640x480 video mode */
   1.176 -    if (SDL_SetVideoMode(640, 480, 0, videoflags) == NULL) {
   1.177 -        fprintf(stderr, "Couldn't set 640x480 video mode: %s\n",
   1.178 +    window = SDL_CreateWindow("CheckKeys Test",
   1.179 +                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
   1.180 +                              640, 480, 0);
   1.181 +    if (!window) {
   1.182 +        fprintf(stderr, "Couldn't create 640x480 window: %s\n",
   1.183                  SDL_GetError());
   1.184          quit(2);
   1.185      }
   1.186  
   1.187 +#if __IPHONEOS__
   1.188 +    /* Creating the context creates the view, which we need to show keyboard */
   1.189 +    SDL_GL_CreateContext(window);
   1.190 +    SDL_iPhoneKeyboardShow(window);
   1.191 +#endif
   1.192 +    
   1.193 +
   1.194      /* Watch keystrokes */
   1.195      done = 0;
   1.196      while (!done) {
   1.197 @@ -156,3 +187,5 @@
   1.198      SDL_Quit();
   1.199      return (0);
   1.200  }
   1.201 +
   1.202 +/* vi: set ts=4 sw=4 expandtab: */