testwm2.c 4.2 KB

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