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

258 lines
6.5 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. }
  121. static void
  122. PrintText(const char *eventtype, const char *text)
  123. {
  124. const char *spot;
  125. char expanded[1024];
  126. expanded[0] = '\0';
  127. for ( spot = text; *spot; ++spot )
  128. {
  129. size_t length = SDL_strlen(expanded);
  130. SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
  131. }
  132. SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
  133. }
  134. void
  135. loop()
  136. {
  137. SDL_Event event;
  138. /* Check for events */
  139. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  140. while (SDL_PollEvent(&event)) {
  141. switch (event.type) {
  142. case SDL_KEYDOWN:
  143. case SDL_KEYUP:
  144. PrintKey(&event.key.keysym, (event.key.state == SDL_PRESSED) ? SDL_TRUE : SDL_FALSE, (event.key.repeat) ? SDL_TRUE : SDL_FALSE);
  145. break;
  146. case SDL_TEXTEDITING:
  147. PrintText("EDIT", event.text.text);
  148. break;
  149. case SDL_TEXTINPUT:
  150. PrintText("INPUT", event.text.text);
  151. break;
  152. case SDL_MOUSEBUTTONDOWN:
  153. /* Left button quits the app, other buttons toggles text input */
  154. if (event.button.button == SDL_BUTTON_LEFT) {
  155. done = 1;
  156. } else {
  157. if (SDL_IsTextInputActive()) {
  158. SDL_Log("Stopping text input\n");
  159. SDL_StopTextInput();
  160. } else {
  161. SDL_Log("Starting text input\n");
  162. SDL_StartTextInput();
  163. }
  164. }
  165. break;
  166. case SDL_QUIT:
  167. done = 1;
  168. break;
  169. default:
  170. break;
  171. }
  172. }
  173. #ifdef __EMSCRIPTEN__
  174. if (done) {
  175. emscripten_cancel_main_loop();
  176. }
  177. #endif
  178. }
  179. int
  180. main(int argc, char *argv[])
  181. {
  182. SDL_Window *window;
  183. SDL_Renderer *renderer;
  184. /* Enable standard application logging */
  185. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  186. /* Initialize SDL */
  187. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  188. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  189. return (1);
  190. }
  191. /* Set 640x480 video mode */
  192. window = SDL_CreateWindow("CheckKeys Test",
  193. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  194. 640, 480, 0);
  195. if (!window) {
  196. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
  197. SDL_GetError());
  198. quit(2);
  199. }
  200. /* On wayland, no window will actually show until something has
  201. actually been displayed.
  202. */
  203. renderer = SDL_CreateRenderer(window, -1, 0);
  204. SDL_RenderPresent(renderer);
  205. #if __IPHONEOS__
  206. /* Creating the context creates the view, which we need to show keyboard */
  207. SDL_GL_CreateContext(window);
  208. #endif
  209. SDL_StartTextInput();
  210. /* Print initial modifier state */
  211. SDL_PumpEvents();
  212. PrintModifierState();
  213. /* Watch keystrokes */
  214. done = 0;
  215. #ifdef __EMSCRIPTEN__
  216. emscripten_set_main_loop(loop, 0, 1);
  217. #else
  218. while (!done) {
  219. loop();
  220. }
  221. #endif
  222. SDL_Quit();
  223. return (0);
  224. }
  225. /* vi: set ts=4 sw=4 expandtab: */