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

298 lines
7.8 KiB

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