test/testime.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 22 Aug 2010 12:39:27 -0700
changeset 4763 518d1679d2d0
parent 4638 08d22c54a21f
parent 4757 140be6839185
child 5150 1435f8a6425c
permissions -rw-r--r--
Merged Daniel's Google Summer of Code work from SDL-gsoc2010_IME
slouken@3280
     1
/* A simple program to test the Input Method support in the SDL library (1.3+) */
slouken@3338
     2
slouken@3280
     3
#include <stdlib.h>
slouken@3280
     4
#include <stdio.h>
slouken@3280
     5
#include <string.h>
slouken@3280
     6
slouken@3280
     7
#include "SDL.h"
slouken@3280
     8
#ifdef HAVE_SDL_TTF
slouken@3280
     9
#include "SDL_ttf.h"
slouken@3280
    10
#endif
slouken@3338
    11
slouken@3280
    12
#define DEFAULT_PTSIZE  30
lestat@3478
    13
#ifdef __QNXNTO__
lestat@3478
    14
    #define DEFAULT_FONT    "/usr/photon/font_repository/tt0003m_.ttf"
lestat@3478
    15
#else
lestat@3478
    16
    #define DEFAULT_FONT    "/System/Library/Fonts/华文细黑.ttf"
lestat@3478
    17
#endif
slouken@3280
    18
#define MAX_TEXT_LENGTH 256
slouken@3280
    19
slouken@3280
    20
SDL_Surface *screen;
slouken@3280
    21
slouken@3280
    22
#ifdef HAVE_SDL_TTF
slouken@3280
    23
TTF_Font *font;
slouken@3280
    24
#endif
slouken@3280
    25
SDL_Rect textRect, markedRect;
slouken@3280
    26
Uint32 lineColor, backColor;
slouken@3280
    27
SDL_Color textColor = { 0, 0, 0 };
dewyatt@4756
    28
char text[MAX_TEXT_LENGTH], markedText[SDL_TEXTEDITINGEVENT_TEXT_SIZE];
dewyatt@4757
    29
int cursor = 0;
dewyatt@4757
    30
dewyatt@4757
    31
size_t utf8_length(unsigned char c)
dewyatt@4757
    32
{
dewyatt@4757
    33
    c = (unsigned char)(0xff & c);
dewyatt@4757
    34
    if (c < 0x80)
dewyatt@4757
    35
        return 1;
dewyatt@4757
    36
    else if ((c >> 5) ==0x6)
dewyatt@4757
    37
        return 2;
dewyatt@4757
    38
    else if ((c >> 4) == 0xe)
dewyatt@4757
    39
        return 3;
dewyatt@4757
    40
    else if ((c >> 3) == 0x1e)
dewyatt@4757
    41
        return 4;
dewyatt@4757
    42
    else
dewyatt@4757
    43
        return 0;
dewyatt@4757
    44
}
dewyatt@4757
    45
dewyatt@4757
    46
char *utf8_next(char *p)
dewyatt@4757
    47
{
dewyatt@4757
    48
    size_t len = utf8_length(*p);
dewyatt@4757
    49
    size_t i = 0;
dewyatt@4757
    50
    if (!len)
dewyatt@4757
    51
        return 0;
dewyatt@4757
    52
dewyatt@4757
    53
    for (; i < len; ++i)
dewyatt@4757
    54
    {
dewyatt@4757
    55
        ++p;
dewyatt@4757
    56
        if (!*p)
dewyatt@4757
    57
            return 0;
dewyatt@4757
    58
    }
dewyatt@4757
    59
    return p;
dewyatt@4757
    60
}
dewyatt@4757
    61
dewyatt@4757
    62
char *utf8_advance(char *p, size_t distance)
dewyatt@4757
    63
{
dewyatt@4757
    64
    size_t i = 0;
dewyatt@4757
    65
    for (; i < distance && p; ++i)
dewyatt@4757
    66
    {
dewyatt@4757
    67
        p = utf8_next(p);
dewyatt@4757
    68
    }
dewyatt@4757
    69
    return p;
dewyatt@4757
    70
}
slouken@3280
    71
slouken@3280
    72
void usage()
slouken@3280
    73
{
slouken@3280
    74
    printf("usage: testime [--font fontfile] [--fullscreen]\n");
slouken@3280
    75
    exit(0);
slouken@3280
    76
}
slouken@3280
    77
slouken@3280
    78
void InitVideo(int argc, char *argv[])
slouken@3280
    79
{
lestat@3478
    80
    int width = 640, height = 480;
slouken@3280
    81
    int flags = SDL_HWSURFACE;
slouken@3280
    82
    const char *fontname = DEFAULT_FONT;
slouken@3280
    83
    int fullscreen = 0;
slouken@3280
    84
slouken@3280
    85
    for (argc--, argv++; argc > 0; argc--, argv++)
slouken@3280
    86
    {
slouken@3280
    87
        if (strcmp(argv[0], "--help") == 0)
slouken@3280
    88
            usage();
slouken@3280
    89
slouken@3280
    90
        else if (strcmp(argv[0], "--fullscreen") == 0)
slouken@3280
    91
            fullscreen = 1;
slouken@3280
    92
slouken@3280
    93
        else if (strcmp(argv[0], "--font") == 0)
slouken@3280
    94
        {
slouken@3280
    95
            argc--;
slouken@3280
    96
            argv++;
slouken@3280
    97
slouken@3280
    98
            if (argc > 0)
slouken@3280
    99
                fontname = argv[0];
slouken@3280
   100
            else
slouken@3280
   101
                usage();
slouken@3280
   102
        }
slouken@3280
   103
    }
slouken@3280
   104
icculus@3581
   105
    SDL_setenv("SDL_VIDEO_WINDOW_POS", "center", 1);
slouken@3280
   106
    if (SDL_Init(SDL_INIT_VIDEO) < 0)
slouken@3280
   107
    {
bob@3281
   108
        fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError());
slouken@3280
   109
        exit(-1);
slouken@3280
   110
    }
slouken@3280
   111
slouken@3280
   112
#ifdef HAVE_SDL_TTF
slouken@3280
   113
    /* Initialize fonts */
slouken@3280
   114
    TTF_Init();
slouken@3280
   115
slouken@3280
   116
    font = TTF_OpenFont(fontname, DEFAULT_PTSIZE);
slouken@3280
   117
    if (! font)
slouken@3280
   118
    {
bob@3281
   119
        fprintf(stderr, "Failed to find font: %s\n", TTF_GetError());
slouken@3280
   120
        exit(-1);
slouken@3280
   121
    }
slouken@3280
   122
#endif
slouken@3280
   123
slouken@3280
   124
    printf("Using font: %s\n", fontname);
slouken@3280
   125
    atexit(SDL_Quit);
slouken@3280
   126
slouken@3280
   127
    if (fullscreen)
slouken@3280
   128
    {
slouken@3280
   129
        SDL_DisplayMode mode;
slouken@3280
   130
        SDL_GetDesktopDisplayMode(&mode);
slouken@3280
   131
slouken@3280
   132
        width = mode.w;
slouken@3280
   133
        height = mode.h;
slouken@3280
   134
        fprintf(stderr, "%dx%d\n", width, height);
slouken@3280
   135
        flags |= SDL_FULLSCREEN;
slouken@3280
   136
    }
slouken@3280
   137
slouken@3280
   138
    /* Create window */
slouken@3280
   139
    screen = SDL_SetVideoMode(width, height, 32, flags);
slouken@3280
   140
    if (screen == NULL)
slouken@3280
   141
    {
slouken@3280
   142
        fprintf(stderr, "Unable to set %dx%d video: %s\n",
slouken@3280
   143
                width, height, SDL_GetError());
slouken@3280
   144
        exit(-1);
slouken@3280
   145
    }
slouken@3280
   146
}
slouken@3280
   147
slouken@3280
   148
void CleanupVideo()
slouken@3280
   149
{
slouken@3280
   150
    SDL_StopTextInput();
slouken@3280
   151
#ifdef HAVE_SDL_TTF
slouken@3280
   152
    TTF_CloseFont(font);
slouken@3280
   153
    TTF_Quit();
slouken@3280
   154
#endif
slouken@3280
   155
}
slouken@3280
   156
slouken@3280
   157
void InitInput()
slouken@3280
   158
{
slouken@3280
   159
    backColor = SDL_MapRGB(screen->format, 0xFF, 0xFF, 0xFF);
slouken@3280
   160
    lineColor = SDL_MapRGB(screen->format, 0x0, 0x0, 0x0);
slouken@3280
   161
slouken@3280
   162
    /* Prepare a rect for text input */
slouken@3280
   163
    textRect.x = textRect.y = 100;
slouken@3280
   164
    textRect.w = screen->w - 2 * textRect.x;
slouken@3280
   165
    textRect.h = 50;
slouken@3280
   166
slouken@3280
   167
    text[0] = 0;
slouken@3280
   168
    markedRect = textRect;
dewyatt@4756
   169
    markedText[0] = 0;
slouken@3280
   170
slouken@3280
   171
    SDL_StartTextInput();
slouken@3280
   172
}
slouken@3280
   173
slouken@3280
   174
#ifdef HAVE_SDL_TTF
slouken@3280
   175
static void RenderText(SDL_Surface *sur,
slouken@3280
   176
                        TTF_Font *font,
slouken@3280
   177
                        const char *text,
slouken@3280
   178
                        int x, int y,
slouken@3280
   179
                        SDL_Color color)
slouken@3280
   180
{
slouken@4638
   181
    if (text && *text) {
gzjjgod@4637
   182
        SDL_Surface *textSur = TTF_RenderUTF8_Blended(font, text, color);
gzjjgod@4637
   183
        SDL_Rect dest = { x, y, textSur->w, textSur->h };
slouken@3280
   184
gzjjgod@4637
   185
        SDL_BlitSurface(textSur, NULL, sur, &dest);
gzjjgod@4637
   186
        SDL_FreeSurface(textSur);
gzjjgod@4637
   187
    }
slouken@3280
   188
}
slouken@3280
   189
#endif
slouken@3280
   190
slouken@3280
   191
void Redraw()
slouken@3280
   192
{
slouken@3280
   193
    int w = 0, h = textRect.h;
slouken@3280
   194
    SDL_Rect cursorRect, underlineRect;
slouken@3280
   195
slouken@3280
   196
    SDL_FillRect(screen, &textRect, backColor);
slouken@3280
   197
slouken@3280
   198
#ifdef HAVE_SDL_TTF
slouken@4638
   199
    if (*text)
slouken@3280
   200
    {
slouken@3280
   201
        RenderText(screen, font, text, textRect.x, textRect.y, textColor);
slouken@3280
   202
        TTF_SizeUTF8(font, text, &w, &h);
slouken@3280
   203
    }
slouken@3280
   204
#endif
slouken@3280
   205
slouken@3280
   206
    markedRect.x = textRect.x + w;
slouken@3280
   207
    markedRect.w = textRect.w - w;
slouken@3280
   208
    if (markedRect.w < 0)
slouken@3280
   209
    {
slouken@3280
   210
        SDL_Flip(screen);
slouken@3280
   211
        // Stop text input because we cannot hold any more characters
slouken@3280
   212
        SDL_StopTextInput();
slouken@3280
   213
        return;
slouken@3280
   214
    }
lestat@3478
   215
    else
lestat@3478
   216
    {
lestat@3478
   217
        SDL_StartTextInput();
lestat@3478
   218
    }
slouken@3280
   219
slouken@3280
   220
    cursorRect = markedRect;
slouken@3280
   221
    cursorRect.w = 2;
slouken@3280
   222
    cursorRect.h = h;
slouken@3280
   223
slouken@3280
   224
    SDL_FillRect(screen, &markedRect, backColor);
dewyatt@4756
   225
    if (markedText[0])
slouken@3280
   226
    {
slouken@3280
   227
#ifdef HAVE_SDL_TTF
dewyatt@4757
   228
        if (cursor)
dewyatt@4757
   229
        {
dewyatt@4757
   230
            char *p = utf8_advance(markedText, cursor);
dewyatt@4757
   231
            char c = 0;
dewyatt@4757
   232
            if (!p)
dewyatt@4757
   233
                p = &markedText[strlen(markedText)];
dewyatt@4757
   234
dewyatt@4757
   235
            c = *p;
dewyatt@4757
   236
            *p = 0;
dewyatt@4757
   237
            TTF_SizeUTF8(font, markedText, &w, 0);
dewyatt@4757
   238
            cursorRect.x += w;
dewyatt@4757
   239
            *p = c;
dewyatt@4757
   240
        }
slouken@3280
   241
        RenderText(screen, font, markedText, markedRect.x, markedRect.y, textColor);
slouken@3280
   242
        TTF_SizeUTF8(font, markedText, &w, &h);
slouken@3280
   243
#endif
slouken@3280
   244
slouken@3280
   245
        underlineRect = markedRect;
slouken@3280
   246
        underlineRect.y += (h - 2);
slouken@3280
   247
        underlineRect.h = 2;
slouken@3280
   248
        underlineRect.w = w;
slouken@3280
   249
slouken@3280
   250
        SDL_FillRect(screen, &underlineRect, lineColor);
slouken@3280
   251
    }
slouken@3280
   252
slouken@3280
   253
    SDL_FillRect(screen, &cursorRect, lineColor);
slouken@3280
   254
slouken@3280
   255
    SDL_Flip(screen);
slouken@3280
   256
slouken@3280
   257
    SDL_SetTextInputRect(&markedRect);
slouken@3280
   258
}
slouken@3280
   259
slouken@3280
   260
void
slouken@3280
   261
HotKey_ToggleFullScreen(void)
slouken@3280
   262
{
slouken@3280
   263
    SDL_Surface *screen;
slouken@3280
   264
slouken@3280
   265
    screen = SDL_GetVideoSurface();
slouken@3280
   266
    if (SDL_WM_ToggleFullScreen(screen)) {
slouken@3280
   267
        printf("Toggled fullscreen mode - now %s\n",
slouken@3280
   268
               (screen->flags & SDL_FULLSCREEN) ? "fullscreen" : "windowed");
slouken@3280
   269
    } else {
slouken@3280
   270
        printf("Unable to toggle fullscreen mode\n");
slouken@3280
   271
    }
slouken@3280
   272
}
slouken@3280
   273
slouken@3280
   274
int main(int argc, char *argv[])
slouken@3280
   275
{
slouken@3338
   276
    SDL_Event event;
slouken@3338
   277
    int done = 0;
slouken@3338
   278
slouken@3280
   279
    InitVideo(argc, argv);
slouken@3280
   280
    InitInput();
slouken@3280
   281
    Redraw();
slouken@3280
   282
slouken@3280
   283
    while (! done && SDL_WaitEvent(&event))
slouken@3280
   284
    {
slouken@3280
   285
        switch (event.type)
slouken@3280
   286
        {
slouken@3280
   287
        case SDL_KEYDOWN:
lestat@3478
   288
            switch (event.key.keysym.sym)
lestat@3478
   289
            {
lestat@3478
   290
                case SDLK_ESCAPE:
lestat@3478
   291
                     done = 1;
lestat@3478
   292
                     break;
lestat@3478
   293
                case SDLK_RETURN:
lestat@3478
   294
                     text[0]=0x00;
lestat@3478
   295
                     Redraw();
lestat@3478
   296
                     break;
lestat@3478
   297
                case SDLK_BACKSPACE:
lestat@3478
   298
                     {
lestat@3478
   299
                         int textlen=SDL_strlen(text);
lestat@3478
   300
lestat@3478
   301
                         do {
lestat@3478
   302
                             if (textlen==0)
lestat@3478
   303
                             {
lestat@3478
   304
                                 break;
lestat@3478
   305
                             }
lestat@3478
   306
                             if ((text[textlen-1] & 0x80) == 0x00)
lestat@3478
   307
                             {
lestat@3478
   308
                                 /* One byte */
lestat@3478
   309
                                 text[textlen-1]=0x00;
lestat@3478
   310
                                 break;
lestat@3478
   311
                             }
lestat@3478
   312
                             if ((text[textlen-1] & 0xC0) == 0x80)
lestat@3478
   313
                             {
lestat@3478
   314
                                 /* Byte from the multibyte sequence */
lestat@3478
   315
                                 text[textlen-1]=0x00;
lestat@3478
   316
                                 textlen--;
lestat@3478
   317
                             }
lestat@3478
   318
                             if ((text[textlen-1] & 0xC0) == 0xC0)
lestat@3478
   319
                             {
lestat@3478
   320
                                 /* First byte of multibyte sequence */
lestat@3478
   321
                                 text[textlen-1]=0x00;
lestat@3478
   322
                                 break;
lestat@3478
   323
                             }
lestat@3478
   324
                         } while(1);
lestat@3478
   325
lestat@3478
   326
                         Redraw();
lestat@3478
   327
                     }
lestat@3478
   328
                     break;
lestat@3478
   329
            }
lestat@3478
   330
lestat@3478
   331
            if (done)
lestat@3478
   332
            {
slouken@3280
   333
                break;
slouken@3280
   334
            }
slouken@3280
   335
slouken@3280
   336
            fprintf(stderr,
slouken@4465
   337
                    "Keyboard: scancode 0x%08X = %s, keycode 0x%08X = %s\n",
slouken@4465
   338
                    event.key.keysym.scancode,
slouken@3280
   339
                    SDL_GetScancodeName(event.key.keysym.scancode),
slouken@3280
   340
                    event.key.keysym.sym, SDL_GetKeyName(event.key.keysym.sym));
slouken@3280
   341
            break;
slouken@3280
   342
slouken@3280
   343
        case SDL_TEXTINPUT:
lestat@3478
   344
            if (SDL_strlen(event.text.text) == 0 || event.text.text[0] == '\n' ||
slouken@3280
   345
                markedRect.w < 0)
slouken@3280
   346
                break;
slouken@3280
   347
slouken@4465
   348
            fprintf(stderr, "Keyboard: text input \"%s\"\n", event.text.text);
slouken@3280
   349
lestat@3478
   350
            if (SDL_strlen(text) + SDL_strlen(event.text.text) < sizeof(text))
slouken@4763
   351
                SDL_strlcat(text, event.text.text, sizeof(text));
slouken@3280
   352
slouken@3280
   353
            fprintf(stderr, "text inputed: %s\n", text);
slouken@3280
   354
slouken@3280
   355
            // After text inputed, we can clear up markedText because it
slouken@3280
   356
            // is committed
dewyatt@4756
   357
            markedText[0] = 0;
slouken@3280
   358
            Redraw();
slouken@3280
   359
            break;
slouken@3280
   360
slouken@3280
   361
        case SDL_TEXTEDITING:
slouken@3280
   362
            fprintf(stderr, "text editing \"%s\", selected range (%d, %d)\n",
slouken@3280
   363
                    event.edit.text, event.edit.start, event.edit.length);
slouken@3280
   364
dewyatt@4756
   365
            strcpy(markedText, event.edit.text);
dewyatt@4757
   366
            cursor = event.edit.start;
slouken@3280
   367
            Redraw();
slouken@3280
   368
            break;
slouken@3280
   369
slouken@3280
   370
        case SDL_QUIT:
slouken@3280
   371
            done = 1;
slouken@3280
   372
            break;
slouken@3280
   373
slouken@3280
   374
        default:
slouken@3280
   375
            break;
slouken@3280
   376
        }
slouken@3280
   377
    }
slouken@3280
   378
slouken@3280
   379
    CleanupVideo();
slouken@3280
   380
    return 0;
slouken@3280
   381
}