1
0

testwm2.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. Copyright (C) 1997-2016 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. #ifdef __EMSCRIPTEN__
  93. if (done) {
  94. emscripten_cancel_main_loop();
  95. }
  96. #endif
  97. }
  98. int
  99. main(int argc, char *argv[])
  100. {
  101. int i;
  102. /* Enable standard application logging */
  103. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  104. SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
  105. /* Initialize test framework */
  106. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  107. if (!state) {
  108. return 1;
  109. }
  110. state->skip_renderer = SDL_TRUE;
  111. for (i = 1; i < argc;) {
  112. int consumed;
  113. consumed = SDLTest_CommonArg(state, i);
  114. if (consumed == 0) {
  115. consumed = -1;
  116. }
  117. if (consumed < 0) {
  118. SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
  119. quit(1);
  120. }
  121. i += consumed;
  122. }
  123. if (!SDLTest_CommonInit(state)) {
  124. quit(2);
  125. }
  126. /* Main render loop */
  127. done = 0;
  128. #ifdef __EMSCRIPTEN__
  129. emscripten_set_main_loop(loop, 0, 1);
  130. #else
  131. while (!done) {
  132. loop();
  133. }
  134. #endif
  135. SDL_FreeCursor(cursor);
  136. quit(0);
  137. /* keep the compiler happy ... */
  138. return(0);
  139. }
  140. /* vi: set ts=4 sw=4 expandtab: */