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
     1 /*
     2   Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 
    13 #include <stdlib.h>
    14 #include <stdio.h>
    15 
    16 #include "common.h"
    17 
    18 static CommonState *state;
    19 
    20 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    21 static void
    22 quit(int rc)
    23 {
    24     CommonQuit(state);
    25     exit(rc);
    26 }
    27 
    28 int
    29 main(int argc, char *argv[])
    30 {
    31     static const char *cursorNames[] = {
    32         "arrow",
    33         "ibeam",
    34         "wait",
    35         "crosshair",
    36         "waitarrow",
    37         "sizeNWSE",
    38         "sizeNESW",
    39         "sizeWE",
    40         "sizeNS",
    41         "sizeALL",
    42         "NO",
    43         "hand",
    44     };
    45     SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
    46 
    47     int i, done;
    48     SDL_Event event;
    49     int system_cursor = -1;
    50     SDL_Cursor *cursor = NULL;
    51 
    52     /* Initialize test framework */
    53     state = CommonCreateState(argv, SDL_INIT_VIDEO);
    54     if (!state) {
    55         return 1;
    56     }
    57     state->skip_renderer = SDL_TRUE;
    58     for (i = 1; i < argc;) {
    59         int consumed;
    60 
    61         consumed = CommonArg(state, i);
    62         if (consumed == 0) {
    63             consumed = -1;
    64         }
    65         if (consumed < 0) {
    66             fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
    67             quit(1);
    68         }
    69         i += consumed;
    70     }
    71     if (!CommonInit(state)) {
    72         quit(2);
    73     }
    74 
    75     /* Main render loop */
    76     done = 0;
    77     while (!done) {
    78         /* Check for events */
    79         while (SDL_PollEvent(&event)) {
    80             CommonEvent(state, &event, &done);
    81 
    82             if (event.type == SDL_WINDOWEVENT) {
    83                 if (event.window.event == SDL_WINDOWEVENT_MOVED) {
    84                     SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
    85                     if (window) {
    86                         printf("Window %d moved to %d,%d (display %d)\n",
    87                             event.window.windowID,
    88                             event.window.data1,
    89                             event.window.data2,
    90                             SDL_GetWindowDisplay(window));
    91                     }
    92                 }
    93             }
    94             if (event.type == SDL_KEYUP) {
    95                 SDL_bool updateCursor = SDL_FALSE;
    96 
    97                 if (event.key.keysym.sym == SDLK_LEFT) {
    98                     --system_cursor;
    99                     if (system_cursor < 0) {
   100                         system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
   101                     }
   102                     updateCursor = SDL_TRUE;
   103                 } else if (event.key.keysym.sym == SDLK_RIGHT) {
   104                     ++system_cursor;
   105                     if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
   106                         system_cursor = 0;
   107                     }
   108                     updateCursor = SDL_TRUE;
   109                 }
   110                 if (updateCursor) {
   111                     SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
   112                     SDL_FreeCursor(cursor);
   113                     cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
   114                     SDL_SetCursor(cursor);
   115                 }
   116             }
   117         }
   118     }
   119     SDL_FreeCursor(cursor);
   120 
   121     quit(0);
   122 	// keep the compiler happy ...
   123 	return(0);
   124 }
   125 
   126 /* vi: set ts=4 sw=4 expandtab: */