test/testwm2.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 19 Nov 2012 20:38:52 -0800
changeset 6677 c3202c29e352
parent 5535 96594ac5fd1a
child 6785 c094c438c0a6
permissions -rw-r--r--
Added code to testwm2 to test the system cursor support
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 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@1933
    16
#include "common.h"
slouken@1895
    17
slouken@1933
    18
static 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@1933
    24
    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
    SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
slouken@6677
    46
slouken@1895
    47
    int i, done;
slouken@1895
    48
    SDL_Event event;
slouken@6677
    49
    int system_cursor = -1;
slouken@6677
    50
    SDL_Cursor *cursor = NULL;
slouken@1895
    51
slouken@1933
    52
    /* Initialize test framework */
slouken@1933
    53
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@1933
    54
    if (!state) {
slouken@1933
    55
        return 1;
slouken@1933
    56
    }
slouken@1933
    57
    state->skip_renderer = SDL_TRUE;
slouken@1933
    58
    for (i = 1; i < argc;) {
slouken@1933
    59
        int consumed;
slouken@1933
    60
slouken@1933
    61
        consumed = CommonArg(state, i);
slouken@1933
    62
        if (consumed == 0) {
slouken@1933
    63
            consumed = -1;
slouken@1933
    64
        }
slouken@1933
    65
        if (consumed < 0) {
slouken@1985
    66
            fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
slouken@1933
    67
            quit(1);
slouken@1933
    68
        }
slouken@1933
    69
        i += consumed;
slouken@1933
    70
    }
slouken@1933
    71
    if (!CommonInit(state)) {
slouken@1933
    72
        quit(2);
slouken@1895
    73
    }
slouken@1895
    74
slouken@1933
    75
    /* Main render loop */
slouken@1895
    76
    done = 0;
slouken@1895
    77
    while (!done) {
slouken@1895
    78
        /* Check for events */
slouken@1895
    79
        while (SDL_PollEvent(&event)) {
slouken@1933
    80
            CommonEvent(state, &event, &done);
slouken@5246
    81
slouken@5246
    82
            if (event.type == SDL_WINDOWEVENT) {
slouken@5246
    83
                if (event.window.event == SDL_WINDOWEVENT_MOVED) {
slouken@5246
    84
                    SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
slouken@5246
    85
                    if (window) {
slouken@5246
    86
                        printf("Window %d moved to %d,%d (display %d)\n",
slouken@5246
    87
                            event.window.windowID,
slouken@5246
    88
                            event.window.data1,
slouken@5246
    89
                            event.window.data2,
slouken@5246
    90
                            SDL_GetWindowDisplay(window));
slouken@5246
    91
                    }
slouken@5246
    92
                }
slouken@5246
    93
            }
slouken@6677
    94
            if (event.type == SDL_KEYUP) {
slouken@6677
    95
                SDL_bool updateCursor = SDL_FALSE;
slouken@6677
    96
slouken@6677
    97
                if (event.key.keysym.sym == SDLK_LEFT) {
slouken@6677
    98
                    --system_cursor;
slouken@6677
    99
                    if (system_cursor < 0) {
slouken@6677
   100
                        system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
slouken@6677
   101
                    }
slouken@6677
   102
                    updateCursor = SDL_TRUE;
slouken@6677
   103
                } else if (event.key.keysym.sym == SDLK_RIGHT) {
slouken@6677
   104
                    ++system_cursor;
slouken@6677
   105
                    if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
slouken@6677
   106
                        system_cursor = 0;
slouken@6677
   107
                    }
slouken@6677
   108
                    updateCursor = SDL_TRUE;
slouken@6677
   109
                }
slouken@6677
   110
                if (updateCursor) {
slouken@6677
   111
                    SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
slouken@6677
   112
                    SDL_FreeCursor(cursor);
slouken@6677
   113
                    cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
slouken@6677
   114
                    SDL_SetCursor(cursor);
slouken@6677
   115
                }
slouken@6677
   116
            }
slouken@1895
   117
        }
slouken@1895
   118
    }
slouken@6677
   119
    SDL_FreeCursor(cursor);
slouken@6677
   120
slouken@1895
   121
    quit(0);
slouken@3338
   122
	// keep the compiler happy ...
slouken@3338
   123
	return(0);
slouken@1895
   124
}
slouken@1895
   125
slouken@1895
   126
/* vi: set ts=4 sw=4 expandtab: */