checkkeysthreads.c 8.1 KB

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