testwm2.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
#include <stdlib.h>
#include <stdio.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL_test_common.h"
static SDLTest_CommonState *state;
int done;
static const char *cursorNames[] = {
"arrow",
"ibeam",
"wait",
"crosshair",
"waitarrow",
"sizeNWSE",
"sizeNESW",
"sizeWE",
"sizeNS",
"sizeALL",
"NO",
"hand",
};
int system_cursor = -1;
SDL_Cursor *cursor = NULL;
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDLTest_CommonQuit(state);
exit(rc);
}
void
loop()
{
SDL_Event event;
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
if (event.type == SDL_WINDOWEVENT) {
if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
if (window) {
SDL_Log("Window %d resized to %dx%d\n",
event.window.windowID,
event.window.data1,
event.window.data2);
}
}
if (event.window.event == SDL_WINDOWEVENT_MOVED) {
SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
if (window) {
SDL_Log("Window %d moved to %d,%d (display %s)\n",
event.window.windowID,
event.window.data1,
event.window.data2,
SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
}
}
}
if (event.type == SDL_KEYUP) {
SDL_bool updateCursor = SDL_FALSE;
if (event.key.keysym.sym == SDLK_LEFT) {
--system_cursor;
if (system_cursor < 0) {
system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
}
updateCursor = SDL_TRUE;
} else if (event.key.keysym.sym == SDLK_RIGHT) {
++system_cursor;
if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
system_cursor = 0;
}
updateCursor = SDL_TRUE;
}
if (updateCursor) {
SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
SDL_FreeCursor(cursor);
cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
SDL_SetCursor(cursor);
}
}
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
int
main(int argc, char *argv[])
{
int i;
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
state->skip_renderer = SDL_TRUE;
for (i = 1; i < argc;) {
int consumed;
consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
consumed = -1;
}
if (consumed < 0) {
SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
quit(1);
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
quit(2);
}
/* Main render loop */
done = 0;
#ifdef __EMSCRIPTEN__
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
loop();
}
#endif
SDL_FreeCursor(cursor);
quit(0);
/* keep the compiler happy ... */
return(0);
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:128
SDL_GetWindowDisplayIndex
#define SDL_GetWindowDisplayIndex
Definition: SDL_dynapi_overrides.h:514
SDLTest_CommonState::skip_renderer
SDL_bool skip_renderer
Definition: SDL_test_common.h:83
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:36
window
SDL_Window * window
Definition: testautomation_render.c:26
SDL_CreateSystemCursor
#define SDL_CreateSystemCursor
Definition: SDL_dynapi_overrides.h:259
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:83
SDL_Cursor
Definition: SDL_mouse_c.h:30
SDL_GetDisplayName
#define SDL_GetDisplayName
Definition: SDL_dynapi_overrides.h:506
cursorNames
static const char * cursorNames[]
Definition: testwm2.c:25
SDLK_RIGHT
Definition: SDL_keycode.h:148
SDL_SetCursor
#define SDL_SetCursor
Definition: SDL_dynapi_overrides.h:260
SDL_test_common.h
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:645
state
static SDLTest_CommonState * state
Definition: testwm2.c:22
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDL_Event::window
SDL_WindowEvent window
Definition: SDL_events.h:525
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:37
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1295
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_KEYUP
Definition: SDL_events.h:94
SDL_WindowEvent::data2
Sint32 data2
Definition: SDL_events.h:181
loop
void loop()
Definition: testwm2.c:51
SDL_WINDOWEVENT
Definition: SDL_events.h:89
SDL_SystemCursor
SDL_SystemCursor
Cursor types for SDL_CreateSystemCursor.
Definition: SDL_mouse.h:46
SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
SDL_WindowEvent::event
Uint8 event
Definition: SDL_events.h:176
SDL_KeyboardEvent::keysym
SDL_Keysym keysym
Definition: SDL_events.h:196
cursor
SDL_Cursor * cursor
Definition: testwm2.c:40
SDL_GetWindowFromID
#define SDL_GetWindowFromID
Definition: SDL_dynapi_overrides.h:521
SDL_TRUE
Definition: SDL_stdinc.h:129
SDLTest_CommonUsage
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
Definition: SDL_test_common.c:451
SDLTest_CommonState
Definition: SDL_test_common.h:51
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:42
SDL_assert
#define SDL_assert(condition)
Definition: SDL_assert.h:167
main
int main(int argc, char *argv[])
Definition: testwm2.c:111
SDL_LogSetPriority
#define SDL_LogSetPriority
Definition: SDL_dynapi_overrides.h:242
SDLK_LEFT
Definition: SDL_keycode.h:149
NULL
#define NULL
Definition: begin_code.h:143
done
int done
Definition: testwm2.c:23
SDL_NUM_SYSTEM_CURSORS
Definition: SDL_mouse.h:60
SDL_Event::key
SDL_KeyboardEvent key
Definition: SDL_events.h:526
quit
static void quit(int rc)
Definition: testwm2.c:44
SDL_Window
The type used to identify a window.
Definition: SDL_sysvideo.h:71
SDL_FALSE
Definition: SDL_stdinc.h:128
SDL_Keysym::sym
SDL_Keycode sym
Definition: SDL_keyboard.h:50
SDL_arraysize
#define SDL_arraysize(array)
Definition: SDL_stdinc.h:93
SDL_Event
General event structure.
Definition: SDL_events.h:521
SDL_WINDOWEVENT_RESIZED
Definition: SDL_video.h:145
system_cursor
int system_cursor
Definition: testwm2.c:39
SDL_FreeCursor
#define SDL_FreeCursor
Definition: SDL_dynapi_overrides.h:263
SDL_WindowEvent::data1
Sint32 data1
Definition: SDL_events.h:180
SDL_WINDOWEVENT_MOVED
Definition: SDL_video.h:143
SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_WindowEvent::windowID
Uint32 windowID
Definition: SDL_events.h:175
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1560
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:77
SDL_Event::type
Uint32 type
Definition: SDL_events.h:523