1.Fixed a memory leak inside XInput2 code
authorDimitris Zenios <dimitris.zenios@gmail.com>
Thu, 31 May 2012 19:23:30 +0300
changeset 631849b2cb56db6e
parent 6317 df009b78a763
child 6319 698c98b83cbb
1.Fixed a memory leak inside XInput2 code
2.Replaced XKeycodeToKeysym with XkbKeycodeToKeysym since XKeycodeToKeysym is deprecated in newer X11 version
3.Rewrote testime.c since it was disabled after SDL_compat.c removal
4.Take into account common arguments also in testrelative.c
configure.in
include/SDL_config.h.in
include/SDL_config_macosx.h
src/video/x11/SDL_x11dyn.h
src/video/x11/SDL_x11events.c
src/video/x11/SDL_x11keyboard.c
src/video/x11/SDL_x11sym.h
src/video/x11/SDL_x11xinput2.c
test/Makefile.in
test/testime.c
test/testrelative.c
     1.1 --- a/configure.in	Thu May 31 09:06:47 2012 -0400
     1.2 +++ b/configure.in	Thu May 31 19:23:30 2012 +0300
     1.3 @@ -1111,6 +1111,7 @@
     1.4              AC_MSG_RESULT($have_const_param_XextAddDisplay)
     1.5  
     1.6              AC_CHECK_LIB(X11, XGetEventData, AC_DEFINE(SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS, 1, [Have XGenericEvent]))
     1.7 +            AC_CHECK_LIB(X11, XkbKeycodeToKeysym, AC_DEFINE(SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM, 1, [Have XkbKeycodeToKeysym]))
     1.8  
     1.9              AC_ARG_ENABLE(video-x11-xcursor,
    1.10  AC_HELP_STRING([--enable-video-x11-xcursor], [enable X11 Xcursor support [[default=yes]]]),
     2.1 --- a/include/SDL_config.h.in	Thu May 31 09:06:47 2012 -0400
     2.2 +++ b/include/SDL_config.h.in	Thu May 31 19:23:30 2012 +0300
     2.3 @@ -271,6 +271,7 @@
     2.4  #undef SDL_VIDEO_DRIVER_X11_XVIDMODE
     2.5  #undef SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS
     2.6  #undef SDL_VIDEO_DRIVER_X11_CONST_PARAM_XEXTADDDISPLAY
     2.7 +#undef SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     2.8  
     2.9  #undef SDL_VIDEO_RENDER_D3D
    2.10  #undef SDL_VIDEO_RENDER_OGL
     3.1 --- a/include/SDL_config_macosx.h	Thu May 31 09:06:47 2012 -0400
     3.2 +++ b/include/SDL_config_macosx.h	Thu May 31 19:23:30 2012 +0300
     3.3 @@ -146,6 +146,7 @@
     3.4  #define SDL_VIDEO_DRIVER_X11_XSHAPE 1
     3.5  #define SDL_VIDEO_DRIVER_X11_XVIDMODE 1
     3.6  #define SDL_VIDEO_DRIVER_X11_SUPPORTS_GENERIC_EVENTS 1
     3.7 +#define SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM 1
     3.8  
     3.9  #ifndef SDL_VIDEO_RENDER_OGL
    3.10  #define SDL_VIDEO_RENDER_OGL	1
     4.1 --- a/src/video/x11/SDL_x11dyn.h	Thu May 31 09:06:47 2012 -0400
     4.2 +++ b/src/video/x11/SDL_x11dyn.h	Thu May 31 19:23:30 2012 +0300
     4.3 @@ -27,6 +27,10 @@
     4.4  #include <X11/Xutil.h>
     4.5  #include <X11/Xatom.h>
     4.6  
     4.7 +#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     4.8 +#include <X11/XKBlib.h>
     4.9 +#endif
    4.10 +
    4.11  /* Apparently some X11 systems can't include this multiple times... */
    4.12  #ifndef SDL_INCLUDED_XLIBINT_H
    4.13  #define SDL_INCLUDED_XLIBINT_H 1
     5.1 --- a/src/video/x11/SDL_x11events.c	Thu May 31 09:06:47 2012 -0400
     5.2 +++ b/src/video/x11/SDL_x11events.c	Thu May 31 19:23:30 2012 +0300
     5.3 @@ -265,7 +265,11 @@
     5.4              if (videodata->key_layout[keycode] == SDL_SCANCODE_UNKNOWN) {
     5.5                  int min_keycode, max_keycode;
     5.6                  XDisplayKeycodes(display, &min_keycode, &max_keycode);
     5.7 +#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     5.8 +                keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
     5.9 +#else
    5.10                  keysym = XKeycodeToKeysym(display, keycode, 0);
    5.11 +#endif
    5.12                  fprintf(stderr,
    5.13                          "The key you just pressed is not recognized by SDL. To help get this fixed, please report this to the SDL mailing list <sdl@libsdl.org> X11 KeyCode %d (%d), X11 KeySym 0x%lX (%s).\n",
    5.14                          keycode, keycode - min_keycode, keysym,
    5.15 @@ -563,7 +567,7 @@
    5.16              XResetScreenSaver(data->display);
    5.17              data->screensaver_activity = now;
    5.18          }
    5.19 -    }
    5.20 +    }   
    5.21  
    5.22      /* Keep processing pending events */
    5.23      while (X11_Pending(data->display)) {
     6.1 --- a/src/video/x11/SDL_x11keyboard.c	Thu May 31 09:06:47 2012 -0400
     6.2 +++ b/src/video/x11/SDL_x11keyboard.c	Thu May 31 19:23:30 2012 +0300
     6.3 @@ -151,7 +151,11 @@
     6.4      unsigned int ucs4;
     6.5      int i;
     6.6  
     6.7 +#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     6.8 +    keysym = XkbKeycodeToKeysym(display, keycode, 0, 0);
     6.9 +#else
    6.10      keysym = XKeycodeToKeysym(display, keycode, 0);
    6.11 +#endif
    6.12      if (keysym == NoSymbol) {
    6.13          return SDLK_UNKNOWN;
    6.14      }
    6.15 @@ -232,7 +236,11 @@
    6.16          SDL_GetDefaultKeymap(keymap);
    6.17          for (i = min_keycode; i <= max_keycode; ++i) {
    6.18              KeySym sym;
    6.19 +#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
    6.20 +            sym = XkbKeycodeToKeysym(data->display, i, 0, 0);
    6.21 +#else
    6.22              sym = XKeycodeToKeysym(data->display, i, 0);
    6.23 +#endif
    6.24              if (sym != NoSymbol) {
    6.25                  SDL_Keycode key;
    6.26                  printf("code = %d, sym = 0x%X (%s) ", i - min_keycode,
     7.1 --- a/src/video/x11/SDL_x11sym.h	Thu May 31 09:06:47 2012 -0400
     7.2 +++ b/src/video/x11/SDL_x11sym.h	Thu May 31 19:23:30 2012 +0300
     7.3 @@ -132,6 +132,14 @@
     7.4  SDL_X11_SYM(void,XFreeEventData,(Display* a,XGenericEventCookie* b),(a,b),)    
     7.5  #endif
     7.6  
     7.7 +#if SDL_VIDEO_DRIVER_X11_HAS_XKBKEYCODETOKEYSYM
     7.8 +#if NeedWidePrototypes
     7.9 +SDL_X11_SYM(KeySym,XkbKeycodeToKeysym,(Display* a,unsigned int b,int c,int d),(a,b,c,d),return)
    7.10 +#else
    7.11 +SDL_X11_SYM(KeySym,XkbKeycodeToKeysym,(Display* a,KeyCode b,int c,int d),(a,b,c,d),return)
    7.12 +#endif
    7.13 +#endif
    7.14 +
    7.15  #if NeedWidePrototypes
    7.16  SDL_X11_SYM(KeySym,XKeycodeToKeysym,(Display* a,unsigned int b,int c),(a,b,c),return)
    7.17  #else
     8.1 --- a/src/video/x11/SDL_x11xinput2.c	Thu May 31 09:06:47 2012 -0400
     8.2 +++ b/src/video/x11/SDL_x11xinput2.c	Thu May 31 19:23:30 2012 +0300
     8.3 @@ -210,6 +210,7 @@
     8.4              }
     8.5          }
     8.6      }
     8.7 +    XIFreeDeviceInfo(info);
     8.8  #endif
     8.9  }
    8.10  
     9.1 --- a/test/Makefile.in	Thu May 31 09:06:47 2012 -0400
     9.2 +++ b/test/Makefile.in	Thu May 31 19:23:30 2012 +0300
     9.3 @@ -105,8 +105,8 @@
     9.4  testiconv$(EXE): $(srcdir)/testiconv.c
     9.5  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
     9.6  
     9.7 -testime$(EXE): $(srcdir)/testime.c
     9.8 -	$(CC) -o $@ $? $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
     9.9 +testime$(EXE): $(srcdir)/testime.c $(srcdir)/common.c
    9.10 +	$(CC) -o $@ $(srcdir)/testime.c $(srcdir)/common.c $(CFLAGS) $(LIBS) @SDL_TTF_LIB@
    9.11  
    9.12  testjoystick$(EXE): $(srcdir)/testjoystick.c
    9.13  	$(CC) -o $@ $? $(CFLAGS) $(LIBS)
    10.1 --- a/test/testime.c	Thu May 31 09:06:47 2012 -0400
    10.2 +++ b/test/testime.c	Thu May 31 19:23:30 2012 +0300
    10.3 @@ -11,15 +11,6 @@
    10.4  */
    10.5  /* A simple program to test the Input Method support in the SDL library (2.0+) */
    10.6  
    10.7 -#if 1 /* FIXME: Rework this using the 2.0 API */
    10.8 -#include <stdio.h>
    10.9 -
   10.10 -int main(int argc, char *argv[])
   10.11 -{
   10.12 -    printf("FIXME\n");
   10.13 -    return 0;
   10.14 -}
   10.15 -#else
   10.16  #include <stdlib.h>
   10.17  #include <stdio.h>
   10.18  #include <string.h>
   10.19 @@ -29,20 +20,22 @@
   10.20  #include "SDL_ttf.h"
   10.21  #endif
   10.22  
   10.23 +#include "common.h"
   10.24 +
   10.25  #define DEFAULT_PTSIZE  30
   10.26  #define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
   10.27  #define MAX_TEXT_LENGTH 256
   10.28  
   10.29 -SDL_Surface *screen;
   10.30 -
   10.31 +static CommonState *state;
   10.32 +static SDL_Rect textRect, markedRect;
   10.33 +static SDL_Color lineColor = {0,0,0,0};
   10.34 +static SDL_Color backColor = {255,255,255,0};
   10.35 +static SDL_Color textColor = {0,0,0,0};
   10.36 +static char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
   10.37 +static int cursor = 0;
   10.38  #ifdef HAVE_SDL_TTF
   10.39 -TTF_Font *font;
   10.40 +static TTF_Font *font;
   10.41  #endif
   10.42 -SDL_Rect textRect, markedRect;
   10.43 -Uint32 lineColor, backColor;
   10.44 -SDL_Color textColor = { 0, 0, 0 };
   10.45 -char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
   10.46 -int cursor = 0;
   10.47  
   10.48  size_t utf8_length(unsigned char c)
   10.49  {
   10.50 @@ -87,24 +80,150 @@
   10.51  
   10.52  void usage()
   10.53  {
   10.54 -    printf("usage: testime [--font fontfile] [--fullscreen]\n");
   10.55 +    printf("usage: testime [--font fontfile]\n");
   10.56      exit(0);
   10.57  }
   10.58  
   10.59 -void InitVideo(int argc, char *argv[])
   10.60 +void InitInput()
   10.61  {
   10.62 -    int width = 640, height = 480;
   10.63 -    int flags = SDL_HWSURFACE;
   10.64 +
   10.65 +    /* Prepare a rect for text input */
   10.66 +    textRect.x = textRect.y = 100;
   10.67 +    textRect.w = DEFAULT_WINDOW_WIDTH - 2 * textRect.x;
   10.68 +    textRect.h = 50;
   10.69 +
   10.70 +    text[0] = 0;
   10.71 +    markedRect = textRect;
   10.72 +    markedText[0] = 0;
   10.73 +
   10.74 +    SDL_StartTextInput();
   10.75 +}
   10.76 +
   10.77 +void CleanupVideo()
   10.78 +{
   10.79 +    SDL_StopTextInput();
   10.80 +#ifdef HAVE_SDL_TTF
   10.81 +    TTF_CloseFont(font);
   10.82 +    TTF_Quit();
   10.83 +#endif
   10.84 +}
   10.85 +
   10.86 +
   10.87 +void _Redraw(SDL_Renderer * renderer) {
   10.88 +    int w = 0, h = textRect.h;
   10.89 +    SDL_Rect cursorRect, underlineRect;
   10.90 +
   10.91 +    SDL_SetRenderDrawColor(renderer, 255,255,255,255);
   10.92 +    SDL_RenderFillRect(renderer,&textRect);
   10.93 +
   10.94 +#ifdef HAVE_SDL_TTF
   10.95 +    if (*text)
   10.96 +    {
   10.97 +        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, textColor);
   10.98 +        SDL_Rect dest = {textRect.x, textRect.y, textSur->w, textSur->h };
   10.99 +
  10.100 +        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
  10.101 +        SDL_FreeSurface(textSur);
  10.102 +
  10.103 +        SDL_RenderCopy(renderer,texture,NULL,&dest);
  10.104 +        SDL_DestroyTexture(texture);
  10.105 +        TTF_SizeUTF8(font, text, &w, &h);
  10.106 +    }
  10.107 +#endif
  10.108 +
  10.109 +    markedRect.x = textRect.x + w;
  10.110 +    markedRect.w = textRect.w - w;
  10.111 +    if (markedRect.w < 0)
  10.112 +    {
  10.113 +        // Stop text input because we cannot hold any more characters
  10.114 +        SDL_StopTextInput();
  10.115 +        return;
  10.116 +    }
  10.117 +    else
  10.118 +    {
  10.119 +        SDL_StartTextInput();
  10.120 +    }
  10.121 +
  10.122 +    cursorRect = markedRect;
  10.123 +    cursorRect.w = 2;
  10.124 +    cursorRect.h = h;
  10.125 +
  10.126 +    SDL_SetRenderDrawColor(renderer, 255,255,255,255);
  10.127 +    SDL_RenderFillRect(renderer,&markedRect);
  10.128 +
  10.129 +    if (markedText[0])
  10.130 +    {
  10.131 +#ifdef HAVE_SDL_TTF
  10.132 +        if (cursor)
  10.133 +        {
  10.134 +            char *p = utf8_advance(markedText, cursor);
  10.135 +            char c = 0;
  10.136 +            if (!p)
  10.137 +                p = &markedText[strlen(markedText)];
  10.138 +
  10.139 +            c = *p;
  10.140 +            *p = 0;
  10.141 +            TTF_SizeUTF8(font, markedText, &w, 0);
  10.142 +            cursorRect.x += w;
  10.143 +            *p = c;
  10.144 +        }
  10.145 +        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, markedText, textColor);
  10.146 +        SDL_Rect dest = {markedRect.x, markedRect.y, textSur->w, textSur->h };
  10.147 +        TTF_SizeUTF8(font, markedText, &w, &h);
  10.148 +        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,textSur);
  10.149 +        SDL_FreeSurface(textSur);
  10.150 +
  10.151 +        SDL_RenderCopy(renderer,texture,NULL,&dest);
  10.152 +        SDL_DestroyTexture(texture);
  10.153 +#endif
  10.154 +
  10.155 +        underlineRect = markedRect;
  10.156 +        underlineRect.y += (h - 2);
  10.157 +        underlineRect.h = 2;
  10.158 +        underlineRect.w = w;
  10.159 +
  10.160 +        SDL_SetRenderDrawColor(renderer, 0,0,0,0);
  10.161 +        SDL_RenderFillRect(renderer,&markedRect);
  10.162 +    }
  10.163 +
  10.164 +    SDL_SetRenderDrawColor(renderer, 0,0,0,0);
  10.165 +    SDL_RenderFillRect(renderer,&cursorRect);
  10.166 +
  10.167 +    SDL_SetTextInputRect(&markedRect);
  10.168 +}
  10.169 +
  10.170 +void Redraw() {
  10.171 +    int i;
  10.172 +    for (i = 0; i < state->num_windows; ++i) {
  10.173 +        SDL_Renderer *renderer = state->renderers[i];
  10.174 +        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
  10.175 +        SDL_RenderClear(renderer);
  10.176 +
  10.177 +        _Redraw(renderer);
  10.178 +
  10.179 +        SDL_RenderPresent(renderer);
  10.180 +    }
  10.181 +}
  10.182 +
  10.183 +int main(int argc, char *argv[]) {
  10.184 +    int i, done;
  10.185 +    SDL_Event event;
  10.186      const char *fontname = DEFAULT_FONT;
  10.187 -    int fullscreen = 0;
  10.188  
  10.189 +    /* Initialize test framework */
  10.190 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
  10.191 +    if (!state) {
  10.192 +        return 1;
  10.193 +    }
  10.194 +    for (i = 1; i < argc;i++) {
  10.195 +        CommonArg(state, i);
  10.196 +    }
  10.197      for (argc--, argv++; argc > 0; argc--, argv++)
  10.198      {
  10.199 -        if (strcmp(argv[0], "--help") == 0)
  10.200 +        if (strcmp(argv[0], "--help") == 0) {
  10.201              usage();
  10.202 -
  10.203 -        else if (strcmp(argv[0], "--fullscreen") == 0)
  10.204 -            fullscreen = 1;
  10.205 +            return 0;
  10.206 +        }
  10.207  
  10.208          else if (strcmp(argv[0], "--font") == 0)
  10.209          {
  10.210 @@ -113,17 +232,17 @@
  10.211  
  10.212              if (argc > 0)
  10.213                  fontname = argv[0];
  10.214 -            else
  10.215 +            else {
  10.216                  usage();
  10.217 +                return 0;
  10.218 +            }
  10.219          }
  10.220      }
  10.221 +    
  10.222 +    if (!CommonInit(state)) {
  10.223 +        return 2;
  10.224 +    }
  10.225  
  10.226 -    SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
  10.227 -    if (SDL_Init(SDL_INIT_VIDEO) < 0)
  10.228 -    {
  10.229 -        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
  10.230 -        exit(-1);
  10.231 -    }
  10.232  
  10.233  #ifdef HAVE_SDL_TTF
  10.234      /* Initialize fonts */
  10.235 @@ -140,258 +259,111 @@
  10.236      printf("Using font: %s\n", fontname);
  10.237      atexit(SDL_Quit);
  10.238  
  10.239 -    if (fullscreen)
  10.240 -    {
  10.241 -        /* Use the desktop mode */
  10.242 -        width = 0;
  10.243 -        height = 0;
  10.244 -        flags |= SDL_FULLSCREEN;
  10.245 +    InitInput();
  10.246 +    /* Create the windows and initialize the renderers */
  10.247 +    for (i = 0; i < state->num_windows; ++i) {
  10.248 +        SDL_Renderer *renderer = state->renderers[i];
  10.249 +        SDL_SetRenderDrawBlendMode(renderer, SDL_BLENDMODE_NONE);
  10.250 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  10.251 +        SDL_RenderClear(renderer);
  10.252      }
  10.253 +    Redraw();    
  10.254 +    /* Main render loop */
  10.255 +    done = 0;
  10.256 +    while (!done) {
  10.257 +        /* Check for events */
  10.258 +        while (SDL_PollEvent(&event)) {
  10.259 +            CommonEvent(state, &event, &done);
  10.260 +            switch(event.type) {
  10.261 +                case SDL_KEYDOWN: {
  10.262 +                    switch (event.key.keysym.sym)
  10.263 +                    {
  10.264 +                        case SDLK_RETURN:
  10.265 +                             text[0]=0x00;
  10.266 +                             Redraw();
  10.267 +                             break;
  10.268 +                        case SDLK_BACKSPACE:
  10.269 +                             {
  10.270 +                                 int textlen=SDL_strlen(text);
  10.271  
  10.272 -    /* Create window */
  10.273 -    screen = SDL_SetVideoMode(width, height, 32, flags);
  10.274 -    if (screen == NULL)
  10.275 -    {
  10.276 -        fprintf(stderr, "Unable to set %dx%d video: %s\n",
  10.277 -                width, height, SDL_GetError());
  10.278 -        exit(-1);
  10.279 +                                 do {
  10.280 +                                     if (textlen==0)
  10.281 +                                     {
  10.282 +                                         break;
  10.283 +                                     }
  10.284 +                                     if ((text[textlen-1] & 0x80) == 0x00)
  10.285 +                                     {
  10.286 +                                         /* One byte */
  10.287 +                                         text[textlen-1]=0x00;
  10.288 +                                         break;
  10.289 +                                     }
  10.290 +                                     if ((text[textlen-1] & 0xC0) == 0x80)
  10.291 +                                     {
  10.292 +                                         /* Byte from the multibyte sequence */
  10.293 +                                         text[textlen-1]=0x00;
  10.294 +                                         textlen--;
  10.295 +                                     }
  10.296 +                                     if ((text[textlen-1] & 0xC0) == 0xC0)
  10.297 +                                     {
  10.298 +                                         /* First byte of multibyte sequence */
  10.299 +                                         text[textlen-1]=0x00;
  10.300 +                                         break;
  10.301 +                                     }
  10.302 +                                 } while(1);
  10.303 +
  10.304 +                                 Redraw();
  10.305 +                             }
  10.306 +                             break;
  10.307 +                    }
  10.308 +
  10.309 +                    if (done)
  10.310 +                    {
  10.311 +                        break;
  10.312 +                    }
  10.313 +
  10.314 +                    fprintf(stderr,
  10.315 +                            "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
  10.316 +                            event.key.keysym.scancode,
  10.317 +                            SDL_GetScancodeName(event.key.keysym.scancode),
  10.318 +                            event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
  10.319 +                    break;
  10.320 +
  10.321 +                case SDL_TEXTINPUT:
  10.322 +                    if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
  10.323 +                        markedRect.w < 0)
  10.324 +                        break;
  10.325 +
  10.326 +                    fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
  10.327 +
  10.328 +                    if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
  10.329 +                        SDL_strlcat(text, event.text.text, sizeof(text));
  10.330 +
  10.331 +                    fprintf(stderr, "text inputed: %s\n", text);
  10.332 +
  10.333 +                    // After text inputed, we can clear up markedText because it
  10.334 +                    // is committed
  10.335 +                    markedText[0] = 0;
  10.336 +                    Redraw();
  10.337 +                    break;
  10.338 +
  10.339 +                case SDL_TEXTEDITING:
  10.340 +                    fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
  10.341 +                            event.edit.text, event.edit.start, event.edit.length);
  10.342 +
  10.343 +                    strcpy(markedText, event.edit.text);
  10.344 +                    cursor = event.edit.start;
  10.345 +                    Redraw();
  10.346 +                    break;
  10.347 +                }
  10.348 +                break;
  10.349 +                    
  10.350 +            }
  10.351 +        }
  10.352      }
  10.353 +    CleanupVideo();
  10.354 +    CommonQuit(state);
  10.355 +    return 0;
  10.356  }
  10.357  
  10.358 -void CleanupVideo()
  10.359 -{
  10.360 -    SDL_StopTextInput();
  10.361 -#ifdef HAVE_SDL_TTF
  10.362 -    TTF_CloseFont(font);
  10.363 -    TTF_Quit();
  10.364 -#endif
  10.365 -}
  10.366 -
  10.367 -void InitInput()
  10.368 -{
  10.369 -    backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
  10.370 -    lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
  10.371 -
  10.372 -    /* Prepare a rect for text input */
  10.373 -    textRect.x = textRect.y = 100;
  10.374 -    textRect.w = screen->w - 2 * textRect.x;
  10.375 -    textRect.h = 50;
  10.376 -
  10.377 -    text[0] = 0;
  10.378 -    markedRect = textRect;
  10.379 -    markedText[0] = 0;
  10.380 -
  10.381 -    SDL_StartTextInput();
  10.382 -}
  10.383 -
  10.384 -#ifdef HAVE_SDL_TTF
  10.385 -static void RenderText(SDL_Surface *sur,
  10.386 -                        TTF_Font *font,
  10.387 -                        const char *text,
  10.388 -                        int x, int y,
  10.389 -                        SDL_Color color)
  10.390 -{
  10.391 -    if (text && *text) {
  10.392 -        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
  10.393 -        SDL_Rect dest = { x, y, textSur->w, textSur->h };
  10.394 -
  10.395 -        SDL_BlitSurface(textSur, NULL, sur, &dest);
  10.396 -        SDL_FreeSurface(textSur);
  10.397 -    }
  10.398 -}
  10.399 -#endif
  10.400 -
  10.401 -void Redraw()
  10.402 -{
  10.403 -    int w = 0, h = textRect.h;
  10.404 -    SDL_Rect cursorRect, underlineRect;
  10.405 -
  10.406 -    SDL_FillRect(screen, &textRect, backColor);
  10.407 -
  10.408 -#ifdef HAVE_SDL_TTF
  10.409 -    if (*text)
  10.410 -    {
  10.411 -        RenderText(screen, font, text, textRect.x, textRect.y, textColor);
  10.412 -        TTF_SizeUTF8(font, text, &w, &h);
  10.413 -    }
  10.414 -#endif
  10.415 -
  10.416 -    markedRect.x = textRect.x + w;
  10.417 -    markedRect.w = textRect.w - w;
  10.418 -    if (markedRect.w < 0)
  10.419 -    {
  10.420 -        SDL_Flip(screen);
  10.421 -        // Stop text input because we cannot hold any more characters
  10.422 -        SDL_StopTextInput();
  10.423 -        return;
  10.424 -    }
  10.425 -    else
  10.426 -    {
  10.427 -        SDL_StartTextInput();
  10.428 -    }
  10.429 -
  10.430 -    cursorRect = markedRect;
  10.431 -    cursorRect.w = 2;
  10.432 -    cursorRect.h = h;
  10.433 -
  10.434 -    SDL_FillRect(screen, &markedRect, backColor);
  10.435 -    if (markedText[0])
  10.436 -    {
  10.437 -#ifdef HAVE_SDL_TTF
  10.438 -        if (cursor)
  10.439 -        {
  10.440 -            char *p = utf8_advance(markedText, cursor);
  10.441 -            char c = 0;
  10.442 -            if (!p)
  10.443 -                p = &markedText[strlen(markedText)];
  10.444 -
  10.445 -            c = *p;
  10.446 -            *p = 0;
  10.447 -            TTF_SizeUTF8(font, markedText, &w, 0);
  10.448 -            cursorRect.x += w;
  10.449 -            *p = c;
  10.450 -        }
  10.451 -        RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
  10.452 -        TTF_SizeUTF8(font, markedText, &w, &h);
  10.453 -#endif
  10.454 -
  10.455 -        underlineRect = markedRect;
  10.456 -        underlineRect.y += (h - 2);
  10.457 -        underlineRect.h = 2;
  10.458 -        underlineRect.w = w;
  10.459 -
  10.460 -        SDL_FillRect(screen, &underlineRect, lineColor);
  10.461 -    }
  10.462 -
  10.463 -    SDL_FillRect(screen, &cursorRect, lineColor);
  10.464 -
  10.465 -    SDL_Flip(screen);
  10.466 -
  10.467 -    SDL_SetTextInputRect(&markedRect);
  10.468 -}
  10.469 -
  10.470 -void
  10.471 -HotKey_ToggleFullScreen(void)
  10.472 -{
  10.473 -    SDL_Surface *screen;
  10.474 -
  10.475 -    screen = SDL_GetVideoSurface();
  10.476 -    if (SDL_WM_ToggleFullScreen(screen)) {
  10.477 -        printf("Toggled fullscreen mode - now %s\n",
  10.478 -               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
  10.479 -    } else {
  10.480 -        printf("Unable to toggle fullscreen mode\n");
  10.481 -    }
  10.482 -}
  10.483 -
  10.484 -int main(int argc, char *argv[])
  10.485 -{
  10.486 -    SDL_Event event;
  10.487 -    int done = 0;
  10.488 -
  10.489 -    InitVideo(argc, argv);
  10.490 -    InitInput();
  10.491 -    Redraw();
  10.492 -
  10.493 -    while (! done && SDL_WaitEvent(&event))
  10.494 -    {
  10.495 -        switch (event.type)
  10.496 -        {
  10.497 -        case SDL_KEYDOWN:
  10.498 -            switch (event.key.keysym.sym)
  10.499 -            {
  10.500 -                case SDLK_ESCAPE:
  10.501 -                     done = 1;
  10.502 -                     break;
  10.503 -                case SDLK_RETURN:
  10.504 -                     text[0]=0x00;
  10.505 -                     Redraw();
  10.506 -                     break;
  10.507 -                case SDLK_BACKSPACE:
  10.508 -                     {
  10.509 -                         int textlen=SDL_strlen(text);
  10.510 -
  10.511 -                         do {
  10.512 -                             if (textlen==0)
  10.513 -                             {
  10.514 -                                 break;
  10.515 -                             }
  10.516 -                             if ((text[textlen-1] & 0x80) == 0x00)
  10.517 -                             {
  10.518 -                                 /* One byte */
  10.519 -                                 text[textlen-1]=0x00;
  10.520 -                                 break;
  10.521 -                             }
  10.522 -                             if ((text[textlen-1] & 0xC0) == 0x80)
  10.523 -                             {
  10.524 -                                 /* Byte from the multibyte sequence */
  10.525 -                                 text[textlen-1]=0x00;
  10.526 -                                 textlen--;
  10.527 -                             }
  10.528 -                             if ((text[textlen-1] & 0xC0) == 0xC0)
  10.529 -                             {
  10.530 -                                 /* First byte of multibyte sequence */
  10.531 -                                 text[textlen-1]=0x00;
  10.532 -                                 break;
  10.533 -                             }
  10.534 -                         } while(1);
  10.535 -
  10.536 -                         Redraw();
  10.537 -                     }
  10.538 -                     break;
  10.539 -            }
  10.540 -
  10.541 -            if (done)
  10.542 -            {
  10.543 -                break;
  10.544 -            }
  10.545 -
  10.546 -            fprintf(stderr,
  10.547 -                    "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
  10.548 -                    event.key.keysym.scancode,
  10.549 -                    SDL_GetScancodeName(event.key.keysym.scancode),
  10.550 -                    event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
  10.551 -            break;
  10.552 -
  10.553 -        case SDL_TEXTINPUT:
  10.554 -            if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
  10.555 -                markedRect.w < 0)
  10.556 -                break;
  10.557 -
  10.558 -            fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
  10.559 -
  10.560 -            if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
  10.561 -                SDL_strlcat(text, event.text.text, sizeof(text));
  10.562 -
  10.563 -            fprintf(stderr, "text inputed: %s\n", text);
  10.564 -
  10.565 -            // After text inputed, we can clear up markedText because it
  10.566 -            // is committed
  10.567 -            markedText[0] = 0;
  10.568 -            Redraw();
  10.569 -            break;
  10.570 -
  10.571 -        case SDL_TEXTEDITING:
  10.572 -            fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
  10.573 -                    event.edit.text, event.edit.start, event.edit.length);
  10.574 -
  10.575 -            strcpy(markedText, event.edit.text);
  10.576 -            cursor = event.edit.start;
  10.577 -            Redraw();
  10.578 -            break;
  10.579 -
  10.580 -        case SDL_QUIT:
  10.581 -            done = 1;
  10.582 -            break;
  10.583 -
  10.584 -        default:
  10.585 -            break;
  10.586 -        }
  10.587 -    }
  10.588 -
  10.589 -    CleanupVideo();
  10.590 -    return 0;
  10.591 -}
  10.592 -#endif
  10.593  
  10.594  /* vi: set ts=4 sw=4 expandtab: */
    11.1 --- a/test/testrelative.c	Thu May 31 09:06:47 2012 -0400
    11.2 +++ b/test/testrelative.c	Thu May 31 19:23:30 2012 +0300
    11.3 @@ -41,6 +41,9 @@
    11.4      if (!state) {
    11.5          return 1;
    11.6      }
    11.7 +    for (i = 1; i < argc;i++) {
    11.8 +        CommonArg(state, i);
    11.9 +    }
   11.10      if (!CommonInit(state)) {
   11.11          return 2;
   11.12      }