checkkeysthreads.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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. /* Simple program: Loop, watching keystrokes
  11. Note that you need to call SDL_PollEvent() or SDL_WaitEvent() to
  12. pump the event loop and catch keystrokes.
  13. */
  14. #include <SDL3/SDL.h>
  15. #include <SDL3/SDL_main.h>
  16. #include <SDL3/SDL_test.h>
  17. #ifdef SDL_PLATFORM_EMSCRIPTEN
  18. #include <emscripten/emscripten.h>
  19. #endif
  20. #include <stdlib.h>
  21. static int done;
  22. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  23. static void
  24. quit(int rc)
  25. {
  26. SDL_Quit();
  27. /* Let 'main()' return normally */
  28. if (rc != 0) {
  29. exit(rc);
  30. }
  31. }
  32. static void
  33. print_string(char **text, size_t *maxlen, const char *fmt, ...)
  34. {
  35. int len;
  36. va_list ap;
  37. va_start(ap, fmt);
  38. len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
  39. if (len > 0) {
  40. *text += len;
  41. if (((size_t)len) < *maxlen) {
  42. *maxlen -= (size_t)len;
  43. } else {
  44. *maxlen = 0;
  45. }
  46. }
  47. va_end(ap);
  48. }
  49. static void
  50. print_modifiers(char **text, size_t *maxlen)
  51. {
  52. int mod;
  53. print_string(text, maxlen, " modifiers:");
  54. mod = SDL_GetModState();
  55. if (!mod) {
  56. print_string(text, maxlen, " (none)");
  57. return;
  58. }
  59. if (mod & SDL_KMOD_LSHIFT) {
  60. print_string(text, maxlen, " LSHIFT");
  61. }
  62. if (mod & SDL_KMOD_RSHIFT) {
  63. print_string(text, maxlen, " RSHIFT");
  64. }
  65. if (mod & SDL_KMOD_LCTRL) {
  66. print_string(text, maxlen, " LCTRL");
  67. }
  68. if (mod & SDL_KMOD_RCTRL) {
  69. print_string(text, maxlen, " RCTRL");
  70. }
  71. if (mod & SDL_KMOD_LALT) {
  72. print_string(text, maxlen, " LALT");
  73. }
  74. if (mod & SDL_KMOD_RALT) {
  75. print_string(text, maxlen, " RALT");
  76. }
  77. if (mod & SDL_KMOD_LGUI) {
  78. print_string(text, maxlen, " LGUI");
  79. }
  80. if (mod & SDL_KMOD_RGUI) {
  81. print_string(text, maxlen, " RGUI");
  82. }
  83. if (mod & SDL_KMOD_NUM) {
  84. print_string(text, maxlen, " NUM");
  85. }
  86. if (mod & SDL_KMOD_CAPS) {
  87. print_string(text, maxlen, " CAPS");
  88. }
  89. if (mod & SDL_KMOD_MODE) {
  90. print_string(text, maxlen, " MODE");
  91. }
  92. if (mod & SDL_KMOD_SCROLL) {
  93. print_string(text, maxlen, " SCROLL");
  94. }
  95. }
  96. static void
  97. PrintModifierState(void)
  98. {
  99. char message[512];
  100. char *spot;
  101. size_t left;
  102. spot = message;
  103. left = sizeof(message);
  104. print_modifiers(&spot, &left);
  105. SDL_Log("Initial state:%s\n", message);
  106. }
  107. static void
  108. PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
  109. {
  110. char message[512];
  111. char *spot;
  112. size_t left;
  113. spot = message;
  114. left = sizeof(message);
  115. /* Print the keycode, name and state */
  116. if (sym->sym) {
  117. print_string(&spot, &left,
  118. "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
  119. pressed ? "pressed " : "released",
  120. sym->scancode,
  121. SDL_GetScancodeName(sym->scancode),
  122. sym->sym, SDL_GetKeyName(sym->sym));
  123. } else {
  124. print_string(&spot, &left,
  125. "Unknown Key (scancode %d = %s) %s ",
  126. sym->scancode,
  127. SDL_GetScancodeName(sym->scancode),
  128. pressed ? "pressed " : "released");
  129. }
  130. print_modifiers(&spot, &left);
  131. if (repeat) {
  132. print_string(&spot, &left, " (repeat)");
  133. }
  134. SDL_Log("%s\n", message);
  135. }
  136. static void
  137. PrintText(const char *eventtype, const char *text)
  138. {
  139. const char *spot;
  140. char expanded[1024];
  141. expanded[0] = '\0';
  142. for (spot = text; *spot; ++spot) {
  143. size_t length = SDL_strlen(expanded);
  144. (void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
  145. }
  146. SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
  147. }
  148. static void loop(void)
  149. {
  150. SDL_Event event;
  151. /* Check for events */
  152. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  153. SDL_Log("starting loop\n");
  154. while (!done && SDL_WaitEvent(&event)) {
  155. SDL_Log("Got event type: %" SDL_PRIu32 "\n", event.type);
  156. switch (event.type) {
  157. case SDL_EVENT_KEY_DOWN:
  158. case SDL_EVENT_KEY_UP:
  159. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED), (event.key.repeat > 0));
  160. break;
  161. case SDL_EVENT_TEXT_EDITING:
  162. PrintText("EDIT", event.text.text);
  163. break;
  164. case SDL_EVENT_TEXT_INPUT:
  165. PrintText("INPUT", event.text.text);
  166. break;
  167. case SDL_EVENT_MOUSE_BUTTON_DOWN:
  168. /* Left button quits the app, other buttons toggles text input */
  169. SDL_Log("mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT);
  170. if (event.button.button == SDL_BUTTON_LEFT) {
  171. done = 1;
  172. } else {
  173. if (SDL_TextInputActive()) {
  174. SDL_Log("Stopping text input\n");
  175. SDL_StopTextInput();
  176. } else {
  177. SDL_Log("Starting text input\n");
  178. SDL_StartTextInput();
  179. }
  180. }
  181. break;
  182. case SDL_EVENT_QUIT:
  183. done = 1;
  184. break;
  185. default:
  186. break;
  187. }
  188. SDL_Log("waiting new event\n");
  189. }
  190. SDL_Log("exiting event loop\n");
  191. #ifdef SDL_PLATFORM_EMSCRIPTEN
  192. if (done) {
  193. emscripten_cancel_main_loop();
  194. }
  195. #endif
  196. }
  197. /* Very simple thread - counts 0 to 9 delaying 50ms between increments */
  198. static int SDLCALL ping_thread(void *ptr)
  199. {
  200. int cnt;
  201. SDL_Event sdlevent;
  202. SDL_memset(&sdlevent, 0, sizeof(SDL_Event));
  203. for (cnt = 0; cnt < 10; ++cnt) {
  204. SDL_Log("sending event (%d/%d) from thread.\n", cnt + 1, 10);
  205. sdlevent.type = SDL_EVENT_KEY_DOWN;
  206. sdlevent.key.keysym.sym = SDLK_1;
  207. SDL_PushEvent(&sdlevent);
  208. SDL_Delay(1000 + SDL_rand() % 1000);
  209. }
  210. return cnt;
  211. }
  212. int main(int argc, char *argv[])
  213. {
  214. SDL_Window *window;
  215. SDL_Renderer *renderer;
  216. SDL_Thread *thread;
  217. SDLTest_CommonState *state;
  218. /* Initialize test framework */
  219. state = SDLTest_CommonCreateState(argv, 0);
  220. if (!state) {
  221. return 1;
  222. }
  223. /* Enable standard application logging */
  224. SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  225. /* Parse commandline */
  226. if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
  227. return 1;
  228. }
  229. /* Initialize SDL */
  230. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  231. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  232. return 1;
  233. }
  234. /* Set 640x480 video mode */
  235. window = SDL_CreateWindow("CheckKeys Test", 640, 480, 0);
  236. if (!window) {
  237. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
  238. SDL_GetError());
  239. quit(2);
  240. }
  241. /* On wayland, no window will actually show until something has
  242. actually been displayed.
  243. */
  244. renderer = SDL_CreateRenderer(window, NULL);
  245. SDL_RenderPresent(renderer);
  246. #ifdef SDL_PLATFORM_IOS
  247. /* Creating the context creates the view, which we need to show keyboard */
  248. SDL_GL_CreateContext(window);
  249. #endif
  250. SDL_StartTextInput();
  251. /* Print initial modifier state */
  252. SDL_PumpEvents();
  253. PrintModifierState();
  254. /* Watch keystrokes */
  255. done = 0;
  256. thread = SDL_CreateThread(ping_thread, "PingThread", NULL);
  257. #ifdef SDL_PLATFORM_EMSCRIPTEN
  258. emscripten_set_main_loop(loop, 0, 1);
  259. #else
  260. while (!done) {
  261. loop();
  262. }
  263. #endif
  264. SDL_DestroyRenderer(renderer);
  265. SDL_DestroyWindow(window);
  266. SDL_WaitThread(thread, NULL);
  267. SDL_Quit();
  268. SDLTest_CommonDestroyState(state);
  269. return 0;
  270. }