checkkeysthreads.c 8.2 KB

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