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