🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

286 lines
7.6 KiB

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