testwm2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /*
  2. Copyright (C) 1997-2019 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. int i;
  45. SDL_Event event;
  46. /* Check for events */
  47. while (SDL_PollEvent(&event)) {
  48. SDLTest_CommonEvent(state, &event, &done);
  49. if (event.type == SDL_WINDOWEVENT) {
  50. if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
  51. SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
  52. if (window) {
  53. SDL_Log("Window %d resized to %dx%d\n",
  54. event.window.windowID,
  55. event.window.data1,
  56. event.window.data2);
  57. }
  58. }
  59. if (event.window.event == SDL_WINDOWEVENT_MOVED) {
  60. SDL_Window *window = SDL_GetWindowFromID(event.window.windowID);
  61. if (window) {
  62. SDL_Log("Window %d moved to %d,%d (display %s)\n",
  63. event.window.windowID,
  64. event.window.data1,
  65. event.window.data2,
  66. SDL_GetDisplayName(SDL_GetWindowDisplayIndex(window)));
  67. }
  68. }
  69. }
  70. if (event.type == SDL_KEYUP) {
  71. SDL_bool updateCursor = SDL_FALSE;
  72. if (event.key.keysym.sym == SDLK_LEFT) {
  73. --system_cursor;
  74. if (system_cursor < 0) {
  75. system_cursor = SDL_NUM_SYSTEM_CURSORS - 1;
  76. }
  77. updateCursor = SDL_TRUE;
  78. } else if (event.key.keysym.sym == SDLK_RIGHT) {
  79. ++system_cursor;
  80. if (system_cursor >= SDL_NUM_SYSTEM_CURSORS) {
  81. system_cursor = 0;
  82. }
  83. updateCursor = SDL_TRUE;
  84. }
  85. if (updateCursor) {
  86. SDL_Log("Changing cursor to \"%s\"", cursorNames[system_cursor]);
  87. SDL_FreeCursor(cursor);
  88. cursor = SDL_CreateSystemCursor((SDL_SystemCursor)system_cursor);
  89. SDL_SetCursor(cursor);
  90. }
  91. }
  92. }
  93. for (i = 0; i < state->num_windows; ++i) {
  94. SDL_Renderer *renderer = state->renderers[i];
  95. SDL_RenderClear(renderer);
  96. SDL_RenderPresent(renderer);
  97. }
  98. #ifdef __EMSCRIPTEN__
  99. if (done) {
  100. emscripten_cancel_main_loop();
  101. }
  102. #endif
  103. }
  104. int
  105. main(int argc, char *argv[])
  106. {
  107. int i;
  108. /* Enable standard application logging */
  109. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  110. SDL_assert(SDL_arraysize(cursorNames) == SDL_NUM_SYSTEM_CURSORS);
  111. /* Initialize test framework */
  112. state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
  113. if (!state) {
  114. return 1;
  115. }
  116. if (!SDLTest_CommonDefaultArgs(state, argc, argv) || !SDLTest_CommonInit(state)) {
  117. SDLTest_CommonQuit(state);
  118. return 1;
  119. }
  120. SDL_EventState(SDL_DROPFILE, SDL_ENABLE);
  121. SDL_EventState(SDL_DROPTEXT, SDL_ENABLE);
  122. for (i = 0; i < state->num_windows; ++i) {
  123. SDL_Renderer *renderer = state->renderers[i];
  124. SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
  125. SDL_RenderClear(renderer);
  126. }
  127. /* Main render loop */
  128. done = 0;
  129. #ifdef __EMSCRIPTEN__
  130. emscripten_set_main_loop(loop, 0, 1);
  131. #else
  132. while (!done) {
  133. loop();
  134. }
  135. #endif
  136. SDL_FreeCursor(cursor);
  137. quit(0);
  138. /* keep the compiler happy ... */
  139. return(0);
  140. }
  141. /* vi: set ts=4 sw=4 expandtab: */