Xcode-iOS/Demos/src/keyboard.c
author Sam Lantinga
Fri, 25 May 2012 18:21:22 -0400
changeset 6310 f830e73e2acf
parent 6159 fb6f363032fd
child 6392 fa7eb111f994
permissions -rw-r--r--
Fixed compiling the keyboad iOS demo
slouken@3277
     1
/*
slouken@3277
     2
 *	keyboard.c
slouken@3277
     3
 *	written by Holmes Futrell
slouken@3277
     4
 *	use however you want
slouken@3277
     5
 */
slouken@3277
     6
slouken@3277
     7
#import "SDL.h"
slouken@3277
     8
#import "common.h"
slouken@3277
     9
slouken@3277
    10
#define GLYPH_SIZE_IMAGE 16     /* size of glyphs (characters) in the bitmap font file */
slouken@3277
    11
#define GLYPH_SIZE_SCREEN 32    /* size of glyphs (characters) as shown on the screen */
slouken@3277
    12
slouken@3685
    13
static SDL_Texture *texture; /* texture where we'll hold our font */
slouken@3277
    14
slouken@3277
    15
/* iPhone SDL addition keyboard related function definitions */
slouken@3685
    16
extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardShow(SDL_Window * window);
slouken@3685
    17
extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardHide(SDL_Window * window);
slouken@3685
    18
extern DECLSPEC SDL_bool SDLCALL SDL_iPhoneKeyboardIsShown(SDL_Window *
slouken@3685
    19
                                                           window);
slouken@3685
    20
extern DECLSPEC int SDLCALL SDL_iPhoneKeyboardToggle(SDL_Window * window);
slouken@3277
    21
slouken@3277
    22
/* function declarations */
slouken@3277
    23
void cleanup(void);
slouken@3277
    24
void drawBlank(int x, int y);
slouken@3277
    25
slouken@5364
    26
static SDL_Renderer *renderer;
slouken@3277
    27
static int numChars = 0;        /* number of characters we've typed so far */
slouken@3277
    28
static SDL_bool lastCharWasColon = 0;   /* we use this to detect sequences such as :) */
slouken@3277
    29
static SDL_Color bg_color = { 50, 50, 100, 255 };       /* color of background */
slouken@3277
    30
slouken@3277
    31
/* this structure maps a scancode to an index in our bitmap font.
slouken@3277
    32
   it also contains data about under which modifiers the mapping is valid
slouken@3277
    33
   (for example, we don't want shift + 1 to produce the character '1',
slouken@3277
    34
   but rather the character '!')
slouken@3277
    35
*/
slouken@3277
    36
typedef struct
slouken@3277
    37
{
slouken@6310
    38
    SDL_Scancode scancode;      /* scancode of the key we want to map */
slouken@3277
    39
    int allow_no_mod;           /* is the map valid if the key has no modifiers? */
slouken@6310
    40
    SDL_Keymod mod;             /* what modifiers are allowed for the mapping */
slouken@3277
    41
    int index;                  /* what index in the font does the scancode map to */
slouken@3277
    42
} fontMapping;
slouken@3277
    43
slouken@3277
    44
#define TABLE_SIZE 51           /* size of our table which maps keys and modifiers to font indices */
slouken@3277
    45
slouken@3277
    46
/* Below is the table that defines the mapping between scancodes and modifiers to indices in the
slouken@3277
    47
   bitmap font.  As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
slouken@3277
    48
   the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
slouken@3277
    49
   The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
slouken@3277
    50
   mapping is also valid if the user is holding shift.
slouken@3277
    51
*/
slouken@3277
    52
fontMapping map[TABLE_SIZE] = {
slouken@3277
    53
slouken@3277
    54
    {SDL_SCANCODE_A, 1, KMOD_SHIFT, 33},        /* A */
slouken@3277
    55
    {SDL_SCANCODE_B, 1, KMOD_SHIFT, 34},        /* B */
slouken@3277
    56
    {SDL_SCANCODE_C, 1, KMOD_SHIFT, 35},        /* C */
slouken@3277
    57
    {SDL_SCANCODE_D, 1, KMOD_SHIFT, 36},        /* D */
slouken@3277
    58
    {SDL_SCANCODE_E, 1, KMOD_SHIFT, 37},        /* E */
slouken@3277
    59
    {SDL_SCANCODE_F, 1, KMOD_SHIFT, 38},        /* F */
slouken@3277
    60
    {SDL_SCANCODE_G, 1, KMOD_SHIFT, 39},        /* G */
slouken@3277
    61
    {SDL_SCANCODE_H, 1, KMOD_SHIFT, 40},        /* H */
slouken@3277
    62
    {SDL_SCANCODE_I, 1, KMOD_SHIFT, 41},        /* I */
slouken@3277
    63
    {SDL_SCANCODE_J, 1, KMOD_SHIFT, 42},        /* J */
slouken@3277
    64
    {SDL_SCANCODE_K, 1, KMOD_SHIFT, 43},        /* K */
slouken@3277
    65
    {SDL_SCANCODE_L, 1, KMOD_SHIFT, 44},        /* L */
slouken@3277
    66
    {SDL_SCANCODE_M, 1, KMOD_SHIFT, 45},        /* M */
slouken@3277
    67
    {SDL_SCANCODE_N, 1, KMOD_SHIFT, 46},        /* N */
slouken@3277
    68
    {SDL_SCANCODE_O, 1, KMOD_SHIFT, 47},        /* O */
slouken@3277
    69
    {SDL_SCANCODE_P, 1, KMOD_SHIFT, 48},        /* P */
slouken@3277
    70
    {SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49},        /* Q */
slouken@3277
    71
    {SDL_SCANCODE_R, 1, KMOD_SHIFT, 50},        /* R */
slouken@3277
    72
    {SDL_SCANCODE_S, 1, KMOD_SHIFT, 51},        /* S */
slouken@3277
    73
    {SDL_SCANCODE_T, 1, KMOD_SHIFT, 52},        /* T */
slouken@3277
    74
    {SDL_SCANCODE_U, 1, KMOD_SHIFT, 53},        /* U */
slouken@3277
    75
    {SDL_SCANCODE_V, 1, KMOD_SHIFT, 54},        /* V */
slouken@3277
    76
    {SDL_SCANCODE_W, 1, KMOD_SHIFT, 55},        /* W */
slouken@3277
    77
    {SDL_SCANCODE_X, 1, KMOD_SHIFT, 56},        /* X */
slouken@3277
    78
    {SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57},        /* Y */
slouken@3277
    79
    {SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58},        /* Z */
slouken@3277
    80
    {SDL_SCANCODE_0, 1, 0, 16}, /* 0 */
slouken@3277
    81
    {SDL_SCANCODE_1, 1, 0, 17}, /* 1 */
slouken@3277
    82
    {SDL_SCANCODE_2, 1, 0, 18}, /* 2 */
slouken@3277
    83
    {SDL_SCANCODE_3, 1, 0, 19}, /* 3 */
slouken@3277
    84
    {SDL_SCANCODE_4, 1, 0, 20}, /* 4 */
slouken@3277
    85
    {SDL_SCANCODE_5, 1, 0, 21}, /* 5 */
slouken@3277
    86
    {SDL_SCANCODE_6, 1, 0, 22}, /* 6 */
slouken@3277
    87
    {SDL_SCANCODE_7, 1, 0, 23}, /* 7 */
slouken@3277
    88
    {SDL_SCANCODE_8, 1, 0, 24}, /* 8 */
slouken@3277
    89
    {SDL_SCANCODE_9, 1, 0, 25}, /* 9 */
slouken@3277
    90
    {SDL_SCANCODE_SPACE, 1, 0, 0},      /*' ' */
slouken@3277
    91
    {SDL_SCANCODE_1, 0, KMOD_SHIFT, 1}, /* ! */
slouken@3277
    92
    {SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31},    /* ? */
slouken@3277
    93
    {SDL_SCANCODE_SLASH, 1, 0, 15},     /* / */
slouken@3277
    94
    {SDL_SCANCODE_COMMA, 1, 0, 12},     /* , */
slouken@3277
    95
    {SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */
slouken@3277
    96
    {SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26},        /* : */
slouken@3277
    97
    {SDL_SCANCODE_PERIOD, 1, 0, 14},    /* . */
slouken@3277
    98
    {SDL_SCANCODE_MINUS, 1, 0, 13},     /* - */
slouken@3277
    99
    {SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11},   /* = */
slouken@3277
   100
    {SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */
slouken@3277
   101
    {SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2},        /* " */
slouken@3277
   102
    {SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */
slouken@3277
   103
slouken@3277
   104
};
slouken@3277
   105
slouken@3277
   106
/*
krogoway@5081
   107
	This function maps an SDL_KeySym to an index in the bitmap font.
slouken@3277
   108
	It does so by scanning through the font mapping table one entry
slouken@3277
   109
	at a time.
slouken@3277
   110
 
slouken@3277
   111
	If a match is found (scancode and allowed modifiers), the proper
slouken@3277
   112
	index is returned.
slouken@3277
   113
 
slouken@3277
   114
	If there is no entry for the key, -1 is returned
slouken@3277
   115
*/
slouken@3277
   116
int
slouken@6310
   117
keyToIndex(SDL_Keysym key)
slouken@3277
   118
{
slouken@3277
   119
    int i, index = -1;
slouken@3277
   120
    for (i = 0; i < TABLE_SIZE; i++) {
slouken@3277
   121
        fontMapping compare = map[i];
slouken@3277
   122
        if (key.scancode == compare.scancode) {
slouken@3277
   123
            /* if this entry is valid with no key mod and we have no keymod, or if
slouken@3277
   124
               the key's modifiers are allowed modifiers for that mapping */
slouken@3277
   125
            if ((compare.allow_no_mod && key.mod == 0)
slouken@3277
   126
                || (key.mod & compare.mod)) {
slouken@3277
   127
                index = compare.index;
slouken@3277
   128
                break;
slouken@3277
   129
            }
slouken@3277
   130
        }
slouken@3277
   131
    }
slouken@3277
   132
    return index;
slouken@3277
   133
}
slouken@3277
   134
slouken@3277
   135
/* 
slouken@3277
   136
	This function returns and x,y position for a given character number.
slouken@3277
   137
    It is used for positioning each character of text
slouken@3277
   138
*/
slouken@3277
   139
void
slouken@3277
   140
getPositionForCharNumber(int n, int *x, int *y)
slouken@3277
   141
{
slouken@3277
   142
    int x_padding = 16;         /* padding space on left and right side of screen */
slouken@3277
   143
    int y_padding = 32;         /* padding space at top of screen */
slouken@3277
   144
    /* figure out the number of characters that can fit horizontally across the screen */
slouken@3277
   145
    int max_x_chars = (SCREEN_WIDTH - 2 * x_padding) / GLYPH_SIZE_SCREEN;
slouken@3277
   146
    int line_separation = 5;    /* pixels between each line */
slouken@3277
   147
    *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
slouken@3277
   148
    *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) +
slouken@3277
   149
        y_padding;
slouken@3277
   150
}
slouken@3277
   151
slouken@3277
   152
void
slouken@3277
   153
drawIndex(int index)
slouken@3277
   154
{
slouken@3277
   155
    int x, y;
slouken@3277
   156
    getPositionForCharNumber(numChars, &x, &y);
slouken@3277
   157
    SDL_Rect srcRect =
slouken@3277
   158
        { GLYPH_SIZE_IMAGE * index, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
slouken@3277
   159
    SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
slouken@3277
   160
    drawBlank(x, y);
slouken@5364
   161
    SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
slouken@3277
   162
}
slouken@3277
   163
slouken@3277
   164
/*  draws the cursor icon at the current end position of the text */
slouken@3277
   165
void
slouken@3277
   166
drawCursor(void)
slouken@3277
   167
{
slouken@3277
   168
    drawIndex(29);              /* cursor is at index 29 in the bitmap font */
slouken@3277
   169
}
slouken@3277
   170
slouken@3277
   171
/* paints over a glyph sized region with the background color
slouken@3277
   172
   in effect it erases the area
slouken@3277
   173
*/
slouken@3277
   174
void
slouken@3277
   175
drawBlank(int x, int y)
slouken@3277
   176
{
slouken@3277
   177
    SDL_Rect rect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
slouken@5364
   178
    SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b,
slouken@3277
   179
                           bg_color.unused);
slouken@5364
   180
    SDL_RenderFillRect(renderer, &rect);
slouken@3277
   181
}
slouken@3277
   182
slouken@3277
   183
/* moves backwards one character, erasing the last one put down */
slouken@3277
   184
void
slouken@3277
   185
backspace(void)
slouken@3277
   186
{
slouken@3277
   187
    int x, y;
slouken@3277
   188
    if (numChars > 0) {
slouken@3277
   189
        getPositionForCharNumber(numChars, &x, &y);
slouken@3277
   190
        drawBlank(x, y);
slouken@3277
   191
        numChars--;
slouken@3277
   192
        getPositionForCharNumber(numChars, &x, &y);
slouken@3277
   193
        drawBlank(x, y);
slouken@3277
   194
        drawCursor();
slouken@3277
   195
    }
slouken@3277
   196
}
slouken@3277
   197
slouken@3685
   198
/* this function loads our font into an SDL_Texture and returns the SDL_Texture  */
slouken@3685
   199
SDL_Texture*
slouken@3277
   200
loadFont(void)
slouken@3277
   201
{
slouken@3277
   202
slouken@3277
   203
    SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
slouken@3277
   204
slouken@3277
   205
    if (!surface) {
slouken@3277
   206
        printf("Error loading bitmap: %s\n", SDL_GetError());
slouken@3277
   207
        return 0;
slouken@3277
   208
    } else {
slouken@3277
   209
        /* set the transparent color for the bitmap font (hot pink) */
slouken@3277
   210
        SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
slouken@3277
   211
        /* now we convert the surface to our desired pixel format */
slouken@3277
   212
        int format = SDL_PIXELFORMAT_ABGR8888;  /* desired texture format */
slouken@3277
   213
        Uint32 Rmask, Gmask, Bmask, Amask;      /* masks for desired format */
slouken@3277
   214
        int bpp;                /* bits per pixel for desired format */
slouken@3277
   215
        SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
slouken@3277
   216
                                   &Amask);
slouken@3277
   217
        SDL_Surface *converted =
slouken@3277
   218
            SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
slouken@3277
   219
                                 Bmask, Amask);
slouken@3277
   220
        SDL_BlitSurface(surface, NULL, converted, NULL);
slouken@3277
   221
        /* create our texture */
slouken@3685
   222
        texture =
slouken@5364
   223
            SDL_CreateTextureFromSurface(renderer, converted);
slouken@3685
   224
        if (texture == 0) {
slouken@3277
   225
            printf("texture creation failed: %s\n", SDL_GetError());
slouken@3277
   226
        } else {
slouken@3277
   227
            /* set blend mode for our texture */
slouken@3685
   228
            SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
slouken@3277
   229
        }
slouken@3277
   230
        SDL_FreeSurface(surface);
slouken@3277
   231
        SDL_FreeSurface(converted);
slouken@3685
   232
        return texture;
slouken@3277
   233
    }
slouken@3277
   234
}
slouken@3277
   235
slouken@3277
   236
int
slouken@3277
   237
main(int argc, char *argv[])
slouken@3277
   238
{
slouken@3277
   239
slouken@3277
   240
    int index;                  /* index of last key we pushed in the bitmap font */
slouken@3685
   241
    SDL_Window *window;
slouken@3277
   242
    SDL_Event event;            /* last event received */
slouken@6310
   243
    SDL_Keymod mod;             /* key modifiers of last key we pushed */
slouken@6310
   244
    SDL_Scancode scancode;      /* scancode of last key we pushed */
slouken@3277
   245
slouken@3277
   246
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
slouken@3277
   247
        printf("Error initializing SDL: %s", SDL_GetError());
slouken@3277
   248
    }
slouken@3277
   249
    /* create window */
slouken@3685
   250
    window = SDL_CreateWindow("iPhone keyboard test", 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 0);
slouken@3277
   251
    /* create renderer */
slouken@5364
   252
    renderer = SDL_CreateRenderer(window, -1, 0);
slouken@3277
   253
slouken@3277
   254
    /* load up our font */
slouken@3277
   255
    loadFont();
slouken@3277
   256
slouken@3277
   257
    /* draw the background, we'll just paint over it */
slouken@5364
   258
    SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b,
slouken@3277
   259
                           bg_color.unused);
slouken@5364
   260
    SDL_RenderFillRect(renderer, NULL);
slouken@5364
   261
    SDL_RenderPresent(renderer);
slouken@3277
   262
slouken@3277
   263
    int done = 0;
slouken@3277
   264
    /* loop till we get SDL_Quit */
slouken@3277
   265
    while (SDL_WaitEvent(&event)) {
slouken@3277
   266
        switch (event.type) {
slouken@3277
   267
        case SDL_QUIT:
slouken@3277
   268
            done = 1;
slouken@3277
   269
            break;
slouken@3277
   270
        case SDL_KEYDOWN:
slouken@3277
   271
            index = keyToIndex(event.key.keysym);
slouken@3277
   272
            scancode = event.key.keysym.scancode;
slouken@3277
   273
            mod = event.key.keysym.mod;
slouken@3277
   274
            if (scancode == SDL_SCANCODE_DELETE) {
slouken@3277
   275
                /* if user hit delete, delete the last character */
slouken@3277
   276
                backspace();
slouken@3277
   277
                lastCharWasColon = 0;
slouken@3277
   278
            } else if (lastCharWasColon && scancode == SDL_SCANCODE_0
slouken@3277
   279
                       && (mod & KMOD_SHIFT)) {
slouken@3277
   280
                /* if our last key was a colon and this one is a close paren, the make a hoppy face */
slouken@3277
   281
                backspace();
slouken@3277
   282
                drawIndex(32);  /* index for happy face */
slouken@3277
   283
                numChars++;
slouken@3277
   284
                drawCursor();
slouken@3277
   285
                lastCharWasColon = 0;
slouken@3277
   286
            } else if (index != -1) {
slouken@3277
   287
                /* if we aren't doing a happy face, then just draw the normal character */
slouken@3277
   288
                drawIndex(index);
slouken@3277
   289
                numChars++;
slouken@3277
   290
                drawCursor();
slouken@3277
   291
                lastCharWasColon =
slouken@3277
   292
                    (event.key.keysym.scancode == SDL_SCANCODE_SEMICOLON
slouken@3277
   293
                     && (event.key.keysym.mod & KMOD_SHIFT));
slouken@3277
   294
            }
slouken@3277
   295
            /* check if the key was a colon */
slouken@3277
   296
            /* draw our updates to the screen */
slouken@5364
   297
            SDL_RenderPresent(renderer);
slouken@3277
   298
            break;
slouken@3277
   299
#ifdef __IPHONEOS__
slouken@3277
   300
        case SDL_MOUSEBUTTONUP:
slouken@3277
   301
            /*      mouse up toggles onscreen keyboard visibility
slouken@3277
   302
               this function is available ONLY on iPhone OS
slouken@3277
   303
             */
slouken@3685
   304
            SDL_iPhoneKeyboardToggle(window);
slouken@3277
   305
            break;
slouken@3277
   306
#endif
slouken@3277
   307
        }
slouken@3277
   308
    }
slouken@3277
   309
    cleanup();
slouken@3277
   310
    return 0;
slouken@3277
   311
}
slouken@3277
   312
slouken@3277
   313
/* clean up after ourselves like a good kiddy */
slouken@3277
   314
void
slouken@3277
   315
cleanup(void)
slouken@3277
   316
{
slouken@3685
   317
    SDL_DestroyTexture(texture);
slouken@3277
   318
    SDL_Quit();
slouken@3277
   319
}