test/checkkeys.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 11220 54ea94875b88
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@5535
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@0
    12
slouken@0
    13
/* Simple program:  Loop, watching keystrokes
slouken@7191
    14
   Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
slouken@0
    15
   pump the event loop and catch keystrokes.
slouken@0
    16
*/
slouken@0
    17
slouken@0
    18
#include <stdio.h>
slouken@0
    19
#include <stdlib.h>
slouken@0
    20
#include <string.h>
slouken@0
    21
icculus@9278
    22
#ifdef __EMSCRIPTEN__
icculus@9278
    23
#include <emscripten/emscripten.h>
icculus@9278
    24
#endif
icculus@9278
    25
slouken@0
    26
#include "SDL.h"
slouken@0
    27
icculus@9278
    28
int done;
icculus@9278
    29
icculus@1151
    30
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    31
static void
slouken@1895
    32
quit(int rc)
icculus@1151
    33
{
slouken@1895
    34
    SDL_Quit();
slouken@1895
    35
    exit(rc);
icculus@1151
    36
}
icculus@1151
    37
slouken@1895
    38
static void
slouken@5461
    39
print_string(char **text, size_t *maxlen, const char *fmt, ...)
slouken@5461
    40
{
slouken@5461
    41
    int len;
slouken@5461
    42
    va_list ap;
slouken@5461
    43
slouken@5461
    44
    va_start(ap, fmt);
slouken@5461
    45
    len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
slouken@5461
    46
    if (len > 0) {
slouken@5461
    47
        *text += len;
icculus@5879
    48
        if ( ((size_t) len) < *maxlen ) {
icculus@5879
    49
            *maxlen -= (size_t) len;
slouken@5461
    50
        } else {
slouken@5461
    51
            *maxlen = 0;
slouken@5461
    52
        }
slouken@5461
    53
    }
slouken@5461
    54
    va_end(ap);
slouken@5461
    55
}
slouken@5461
    56
slouken@5461
    57
static void
slouken@5461
    58
print_modifiers(char **text, size_t *maxlen)
slouken@0
    59
{
slouken@1895
    60
    int mod;
slouken@5461
    61
    print_string(text, maxlen, " modifiers:");
slouken@1895
    62
    mod = SDL_GetModState();
slouken@1895
    63
    if (!mod) {
slouken@5461
    64
        print_string(text, maxlen, " (none)");
slouken@1895
    65
        return;
slouken@1895
    66
    }
slouken@1895
    67
    if (mod & KMOD_LSHIFT)
slouken@5461
    68
        print_string(text, maxlen, " LSHIFT");
slouken@1895
    69
    if (mod & KMOD_RSHIFT)
slouken@5461
    70
        print_string(text, maxlen, " RSHIFT");
slouken@1895
    71
    if (mod & KMOD_LCTRL)
slouken@5461
    72
        print_string(text, maxlen, " LCTRL");
slouken@1895
    73
    if (mod & KMOD_RCTRL)
slouken@5461
    74
        print_string(text, maxlen, " RCTRL");
slouken@1895
    75
    if (mod & KMOD_LALT)
slouken@5461
    76
        print_string(text, maxlen, " LALT");
slouken@1895
    77
    if (mod & KMOD_RALT)
slouken@5461
    78
        print_string(text, maxlen, " RALT");
slouken@2303
    79
    if (mod & KMOD_LGUI)
slouken@5461
    80
        print_string(text, maxlen, " LGUI");
slouken@2303
    81
    if (mod & KMOD_RGUI)
slouken@5461
    82
        print_string(text, maxlen, " RGUI");
slouken@1895
    83
    if (mod & KMOD_NUM)
slouken@5461
    84
        print_string(text, maxlen, " NUM");
slouken@1895
    85
    if (mod & KMOD_CAPS)
slouken@5461
    86
        print_string(text, maxlen, " CAPS");
slouken@1895
    87
    if (mod & KMOD_MODE)
slouken@5461
    88
        print_string(text, maxlen, " MODE");
slouken@0
    89
}
slouken@0
    90
slouken@1895
    91
static void
slouken@9667
    92
PrintModifierState()
slouken@9667
    93
{
slouken@9667
    94
    char message[512];
slouken@9667
    95
    char *spot;
slouken@9667
    96
    size_t left;
slouken@9667
    97
slouken@9667
    98
    spot = message;
slouken@9667
    99
    left = sizeof(message);
slouken@9667
   100
slouken@9667
   101
    print_modifiers(&spot, &left);
slouken@9667
   102
    SDL_Log("Initial state:%s\n", message);
slouken@9667
   103
}
slouken@9667
   104
slouken@9667
   105
static void
slouken@5218
   106
PrintKey(SDL_Keysym * sym, SDL_bool pressed, SDL_bool repeat)
slouken@0
   107
{
slouken@5461
   108
    char message[512];
slouken@5461
   109
    char *spot;
slouken@5461
   110
    size_t left;
slouken@5461
   111
slouken@5461
   112
    spot = message;
slouken@5461
   113
    left = sizeof(message);
slouken@5461
   114
slouken@1895
   115
    /* Print the keycode, name and state */
slouken@1895
   116
    if (sym->sym) {
slouken@5461
   117
        print_string(&spot, &left,
slouken@5461
   118
                "Key %s:  scancode %d = %s, keycode 0x%08X = %s ",
slouken@5461
   119
                pressed ? "pressed " : "released",
slouken@5461
   120
                sym->scancode,
slouken@5461
   121
                SDL_GetScancodeName(sym->scancode),
slouken@5461
   122
                sym->sym, SDL_GetKeyName(sym->sym));
slouken@1895
   123
    } else {
slouken@5461
   124
        print_string(&spot, &left,
slouken@5461
   125
                "Unknown Key (scancode %d = %s) %s ",
slouken@5461
   126
                sym->scancode,
slouken@5461
   127
                SDL_GetScancodeName(sym->scancode),
jorgen@6922
   128
                pressed ? "pressed " : "released");
slouken@1895
   129
    }
slouken@5461
   130
    print_modifiers(&spot, &left);
slouken@4560
   131
    if (repeat) {
slouken@5461
   132
        print_string(&spot, &left, " (repeat)");
slouken@4560
   133
    }
jorgen@6922
   134
    SDL_Log("%s\n", message);
slouken@0
   135
}
slouken@0
   136
bob@2300
   137
static void
icculus@9581
   138
PrintText(char *eventtype, char *text)
bob@2300
   139
{
aschiffler@7639
   140
    char *spot, expanded[1024];
slouken@7317
   141
slouken@7317
   142
    expanded[0] = '\0';
slouken@7317
   143
    for ( spot = text; *spot; ++spot )
slouken@7317
   144
    {
slouken@7317
   145
        size_t length = SDL_strlen(expanded);
aschiffler@7639
   146
        SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
slouken@7317
   147
    }
icculus@9581
   148
    SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
bob@2300
   149
}
bob@2300
   150
icculus@9278
   151
void
icculus@9278
   152
loop()
icculus@9278
   153
{
icculus@9278
   154
    SDL_Event event;
icculus@9278
   155
    /* Check for events */
icculus@9278
   156
    /*SDL_WaitEvent(&event); emscripten does not like waiting*/
icculus@9278
   157
icculus@9278
   158
    while (SDL_PollEvent(&event)) {
icculus@9278
   159
        switch (event.type) {
icculus@9278
   160
        case SDL_KEYDOWN:
icculus@9580
   161
        case SDL_KEYUP:
philipp@9922
   162
            PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
icculus@9278
   163
            break;
icculus@9581
   164
        case SDL_TEXTEDITING:
icculus@9581
   165
            PrintText("EDIT", event.text.text);
icculus@9581
   166
            break;
icculus@9278
   167
        case SDL_TEXTINPUT:
icculus@9581
   168
            PrintText("INPUT", event.text.text);
icculus@9278
   169
            break;
icculus@9278
   170
        case SDL_MOUSEBUTTONDOWN:
slouken@11220
   171
            /* Left button quits the app, other buttons toggles text input */
slouken@11220
   172
            if (event.button.button == SDL_BUTTON_LEFT) {
slouken@11220
   173
                done = 1;
slouken@11220
   174
            } else {
slouken@11220
   175
                if (SDL_IsTextInputActive()) {
slouken@11220
   176
                    SDL_Log("Stopping text input\n");
slouken@11220
   177
                    SDL_StopTextInput();
slouken@11220
   178
                } else {
slouken@11220
   179
                    SDL_Log("Starting text input\n");
slouken@11220
   180
                    SDL_StartTextInput();
slouken@11220
   181
                }
slouken@11220
   182
            }
slouken@11220
   183
            break;
icculus@9278
   184
        case SDL_QUIT:
icculus@9278
   185
            done = 1;
icculus@9278
   186
            break;
icculus@9278
   187
        default:
icculus@9278
   188
            break;
icculus@9278
   189
        }
icculus@9278
   190
    }
philipp@9607
   191
#ifdef __EMSCRIPTEN__
philipp@9607
   192
    if (done) {
philipp@9607
   193
        emscripten_cancel_main_loop();
philipp@9607
   194
    }
philipp@9607
   195
#endif
icculus@9278
   196
}
icculus@9278
   197
slouken@1895
   198
int
slouken@1895
   199
main(int argc, char *argv[])
slouken@0
   200
{
slouken@5461
   201
    SDL_Window *window;
philipp@9922
   202
philipp@9922
   203
    /* Enable standard application logging */
philipp@9922
   204
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
slouken@0
   205
slouken@1895
   206
    /* Initialize SDL */
slouken@1895
   207
    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
philipp@9922
   208
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
slouken@1895
   209
        return (1);
slouken@1895
   210
    }
slouken@0
   211
slouken@1895
   212
    /* Set 640x480 video mode */
slouken@5461
   213
    window = SDL_CreateWindow("CheckKeys Test",
slouken@5461
   214
                              SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
slouken@5461
   215
                              640, 480, 0);
slouken@5461
   216
    if (!window) {
aschiffler@7639
   217
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
slouken@1895
   218
                SDL_GetError());
slouken@1895
   219
        quit(2);
slouken@1895
   220
    }
slouken@0
   221
slouken@5461
   222
#if __IPHONEOS__
slouken@5461
   223
    /* Creating the context creates the view, which we need to show keyboard */
slouken@5461
   224
    SDL_GL_CreateContext(window);
slouken@5461
   225
#endif
slouken@6392
   226
slouken@6654
   227
    SDL_StartTextInput();
slouken@5461
   228
slouken@9667
   229
    /* Print initial modifier state */
slouken@9667
   230
    SDL_PumpEvents();
slouken@9667
   231
    PrintModifierState();
slouken@9667
   232
slouken@1895
   233
    /* Watch keystrokes */
slouken@1895
   234
    done = 0;
icculus@9278
   235
icculus@9278
   236
#ifdef __EMSCRIPTEN__
icculus@9278
   237
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   238
#else
slouken@1895
   239
    while (!done) {
icculus@9278
   240
        loop();
slouken@1895
   241
    }
icculus@9278
   242
#endif
icculus@1151
   243
slouken@1895
   244
    SDL_Quit();
slouken@1895
   245
    return (0);
slouken@0
   246
}
slouken@5461
   247
slouken@5461
   248
/* vi: set ts=4 sw=4 expandtab: */