test/testwm2.c
author Alex Baines <alex@abaines.me.uk>
Tue, 19 Aug 2014 23:31:50 +0100
changeset 9097 56d712662a82
parent 8149 681eb46b8ac4
child 9278 8900afb78a19
permissions -rw-r--r--
Add a SDL_IM_INTERNAL_EDITING event to make IMs like iBus render editing text in its own UI instead of sending TEXTEDITING events.
This is useful for applications that handle TEXTINPUT events but not TEXTEDITING events.
slouken@5535
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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@1895
    12
slouken@1933
    13
#include <stdlib.h>
slouken@1933
    14
#include <stdio.h>
slouken@1895
    15
slouken@6785
    16
#include "SDL_test_common.h"
slouken@1895
    17
slouken@6785
    18
static SDLTest_CommonState *state;
slouken@1895
    19
slouken@1895
    20
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@1895
    21
static void
slouken@1895
    22
quit(int rc)
slouken@1895
    23
{
slouken@6785
    24
    SDLTest_CommonQuit(state);
slouken@1895
    25
    exit(rc);
slouken@1895
    26
}
slouken@1895
    27
slouken@1895
    28
int
slouken@1895
    29
main(int argc, char *argv[])
slouken@1895
    30
{
slouken@6677
    31
    static const char *cursorNames[] = {
slouken@6677
    32
        "arrow",
slouken@6677
    33
        "ibeam",
slouken@6677
    34
        "wait",
slouken@6677
    35
        "crosshair",
slouken@6677
    36
        "waitarrow",
slouken@6677
    37
        "sizeNWSE",
slouken@6677
    38
        "sizeNESW",
slouken@6677
    39
        "sizeWE",
slouken@6677
    40
        "sizeNS",
slouken@6677
    41
        "sizeALL",
slouken@6677
    42
        "NO",
slouken@6677
    43
        "hand",
slouken@6677
    44
    };
slouken@6677
    45
slouken@1895
    46
    int i, done;
slouken@1895
    47
    SDL_Event event;
slouken@6677
    48
    int system_cursor = -1;
slouken@6677
    49
    SDL_Cursor *cursor = NULL;
slouken@1895
    50
aschiffler@7639
    51
	/* Enable standard application logging */
aschiffler@7639
    52
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
aschiffler@7639
    53
icculus@7034
    54
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
icculus@7034
    55
slouken@1933
    56
    /* Initialize test framework */
slouken@6785
    57
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1933
    58
    if (!state) {
slouken@1933
    59
        return 1;
slouken@1933
    60
    }
slouken@1933
    61
    state->skip_renderer = SDL_TRUE;
slouken@1933
    62
    for (i = 1; i < argc;) {
slouken@1933
    63
        int consumed;
slouken@1933
    64
slouken@6785
    65
        consumed = SDLTest_CommonArg(state, i);
slouken@1933
    66
        if (consumed == 0) {
slouken@1933
    67
            consumed = -1;
slouken@1933
    68
        }
slouken@1933
    69
        if (consumed < 0) {
aschiffler@7639
    70
            SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
slouken@1933
    71
            quit(1);
slouken@1933
    72
        }
slouken@1933
    73
        i += consumed;
slouken@1933
    74
    }
slouken@6785
    75
    if (!SDLTest_CommonInit(state)) {
slouken@1933
    76
        quit(2);
slouken@1895
    77
    }
slouken@1895
    78
slouken@1933
    79
    /* Main render loop */
slouken@1895
    80
    done = 0;
slouken@1895
    81
    while (!done) {
slouken@1895
    82
        /* Check for events */
slouken@1895
    83
        while (SDL_PollEvent(&event)) {
slouken@6785
    84
            SDLTest_CommonEvent(state, &event, &done);
slouken@5246
    85
slouken@5246
    86
            if (event.type == SDL_WINDOWEVENT) {
slouken@6788
    87
                if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
slouken@6788
    88
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@6788
    89
                    if (window) {
aschiffler@7639
    90
                        SDL_Log("Window %d resized to %dx%d\n",
slouken@6788
    91
                            event.window.windowID,
slouken@6788
    92
                            event.window.data1,
slouken@6788
    93
                            event.window.data2);
slouken@6788
    94
                    }
slouken@6788
    95
                }
slouken@5246
    96
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    97
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    98
                    if (window) {
aschiffler@7639
    99
                        SDL_Log("Window %d moved to %d,%d (display %s)\n",
slouken@5246
   100
                            event.window.windowID,
slouken@5246
   101
                            event.window.data1,
slouken@5246
   102
                            event.window.data2,
slouken@6787
   103
                            SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
slouken@5246
   104
                    }
slouken@5246
   105
                }
slouken@5246
   106
            }
slouken@6677
   107
            if (event.type == SDL_KEYUP) {
slouken@6677
   108
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
   109
slouken@6677
   110
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
   111
                    --system_cursor;
slouken@6677
   112
                    if (system_cursor < 0) {
slouken@6677
   113
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
   114
                    }
slouken@6677
   115
                    updateCursor = SDL_TRUE;
slouken@6677
   116
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
   117
                    ++system_cursor;
slouken@6677
   118
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
   119
                        system_cursor = 0;
slouken@6677
   120
                    }
slouken@6677
   121
                    updateCursor = SDL_TRUE;
slouken@6677
   122
                }
slouken@6677
   123
                if (updateCursor) {
slouken@6677
   124
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
   125
                    SDL_FreeCursor(cursor);
slouken@6677
   126
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
   127
                    SDL_SetCursor(cursor);
slouken@6677
   128
                }
slouken@6677
   129
            }
slouken@1895
   130
        }
slouken@1895
   131
    }
slouken@6677
   132
    SDL_FreeCursor(cursor);
slouken@6677
   133
slouken@1895
   134
    quit(0);
gabomdq@7663
   135
    /* keep the compiler happy ... */
slouken@7191
   136
    return(0);
slouken@1895
   137
}
slouken@1895
   138
slouken@1895
   139
/* vi: set ts=4 sw=4 expandtab: */