🛠️🐜 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.

285 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: %d\n", event.type); fflush(stderr);
  145. switch (event.type) {
  146. case SDL_KEYDOWN:
  147. case SDL_KEYUP:
  148. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
  149. break;
  150. case SDL_TEXTEDITING:
  151. PrintText("EDIT", event.text.text);
  152. break;
  153. case SDL_TEXTINPUT:
  154. PrintText("INPUT", event.text.text);
  155. break;
  156. case SDL_MOUSEBUTTONDOWN:
  157. /* Left button quits the app, other buttons toggles text input */
  158. fprintf(stderr, "mouse button down button: %d (LEFT=%d)\n", event.button.button, SDL_BUTTON_LEFT); fflush(stderr);
  159. if (event.button.button == SDL_BUTTON_LEFT) {
  160. done = 1;
  161. } else {
  162. if (SDL_IsTextInputActive()) {
  163. SDL_Log("Stopping text input\n");
  164. SDL_StopTextInput();
  165. } else {
  166. SDL_Log("Starting text input\n");
  167. SDL_StartTextInput();
  168. }
  169. }
  170. break;
  171. case SDL_QUIT:
  172. done = 1;
  173. break;
  174. default:
  175. break;
  176. }
  177. fprintf(stderr, "waiting new event\n"); fflush(stderr);
  178. }
  179. fprintf(stderr, "exiting event loop\n"); fflush(stderr);
  180. #ifdef __EMSCRIPTEN__
  181. if (done) {
  182. emscripten_cancel_main_loop();
  183. }
  184. #endif
  185. }
  186. /* Very simple thread - counts 0 to 9 delaying 50ms between increments */
  187. static int SDLCALL ping_thread(void *ptr)
  188. {
  189. int cnt;
  190. SDL_Event sdlevent;
  191. SDL_memset(&sdlevent, 0 , sizeof(SDL_Event));
  192. for (cnt = 0; cnt < 10; ++cnt) {
  193. fprintf(stderr, "sending event (%d/%d) from thread.\n", cnt + 1, 10); fflush(stderr);
  194. sdlevent.type = SDL_KEYDOWN;
  195. sdlevent.key.keysym.sym = SDLK_1;
  196. SDL_PushEvent(&sdlevent);
  197. SDL_Delay(1000 + rand() % 1000);
  198. }
  199. return cnt;
  200. }
  201. int
  202. main(int argc, char *argv[])
  203. {
  204. SDL_Window *window;
  205. SDL_Renderer *renderer;
  206. SDL_Thread *thread;
  207. /* Enable standard application logging */
  208. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  209. /* Initialize SDL */
  210. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  211. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  212. return (1);
  213. }
  214. /* Set 640x480 video mode */
  215. window = SDL_CreateWindow("CheckKeys Test",
  216. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  217. 640, 480, 0);
  218. if (!window) {
  219. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
  220. SDL_GetError());
  221. quit(2);
  222. }
  223. /* On wayland, no window will actually show until something has
  224. actually been displayed.
  225. */
  226. renderer = SDL_CreateRenderer(window, -1, 0);
  227. SDL_RenderPresent(renderer);
  228. #if __IPHONEOS__
  229. /* Creating the context creates the view, which we need to show keyboard */
  230. SDL_GL_CreateContext(window);
  231. #endif
  232. SDL_StartTextInput();
  233. /* Print initial modifier state */
  234. SDL_PumpEvents();
  235. PrintModifierState();
  236. /* Watch keystrokes */
  237. done = 0;
  238. thread = SDL_CreateThread(ping_thread, "PingThread", (void *)NULL);
  239. #ifdef __EMSCRIPTEN__
  240. emscripten_set_main_loop(loop, 0, 1);
  241. #else
  242. while (!done) {
  243. loop();
  244. }
  245. #endif
  246. SDL_WaitThread(thread, NULL);
  247. SDL_Quit();
  248. return (0);
  249. }
  250. /* vi: set ts=4 sw=4 expandtab: */