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

313 lines
8.2 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. #include "SDL_test_font.h"
  22. static SDL_Window *window;
  23. static SDL_Renderer *renderer;
  24. static SDLTest_TextWindow *textwin;
  25. static int done;
  26. /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
  27. static void
  28. quit(int rc)
  29. {
  30. SDL_Quit();
  31. exit(rc);
  32. }
  33. static void
  34. print_string(char **text, size_t *maxlen, const char *fmt, ...)
  35. {
  36. int len;
  37. va_list ap;
  38. va_start(ap, fmt);
  39. len = SDL_vsnprintf(*text, *maxlen, fmt, ap);
  40. if (len > 0) {
  41. *text += len;
  42. if (((size_t)len) < *maxlen) {
  43. *maxlen -= (size_t)len;
  44. } else {
  45. *maxlen = 0;
  46. }
  47. }
  48. va_end(ap);
  49. }
  50. static void
  51. print_modifiers(char **text, size_t *maxlen)
  52. {
  53. int mod;
  54. print_string(text, maxlen, " modifiers:");
  55. mod = SDL_GetModState();
  56. if (!mod) {
  57. print_string(text, maxlen, " (none)");
  58. return;
  59. }
  60. if (mod & KMOD_LSHIFT) {
  61. print_string(text, maxlen, " LSHIFT");
  62. }
  63. if (mod & KMOD_RSHIFT) {
  64. print_string(text, maxlen, " RSHIFT");
  65. }
  66. if (mod & KMOD_LCTRL) {
  67. print_string(text, maxlen, " LCTRL");
  68. }
  69. if (mod & KMOD_RCTRL) {
  70. print_string(text, maxlen, " RCTRL");
  71. }
  72. if (mod & KMOD_LALT) {
  73. print_string(text, maxlen, " LALT");
  74. }
  75. if (mod & KMOD_RALT) {
  76. print_string(text, maxlen, " RALT");
  77. }
  78. if (mod & KMOD_LGUI) {
  79. print_string(text, maxlen, " LGUI");
  80. }
  81. if (mod & KMOD_RGUI) {
  82. print_string(text, maxlen, " RGUI");
  83. }
  84. if (mod & KMOD_NUM) {
  85. print_string(text, maxlen, " NUM");
  86. }
  87. if (mod & KMOD_CAPS) {
  88. print_string(text, maxlen, " CAPS");
  89. }
  90. if (mod & KMOD_MODE) {
  91. print_string(text, maxlen, " MODE");
  92. }
  93. if (mod & KMOD_SCROLL) {
  94. print_string(text, maxlen, " SCROLL");
  95. }
  96. }
  97. static void
  98. PrintModifierState()
  99. {
  100. char message[512];
  101. char *spot;
  102. size_t left;
  103. spot = message;
  104. left = sizeof(message);
  105. print_modifiers(&spot, &left);
  106. SDL_Log("Initial state:%s\n", message);
  107. }
  108. static void
  109. PrintKey(SDL_Keysym *sym, SDL_bool pressed, SDL_bool repeat)
  110. {
  111. char message[512];
  112. char *spot;
  113. size_t left;
  114. spot = message;
  115. left = sizeof(message);
  116. /* Print the keycode, name and state */
  117. if (sym->sym) {
  118. print_string(&spot, &left,
  119. "Key %s: scancode %d = %s, keycode 0x%08X = %s ",
  120. pressed ? "pressed " : "released",
  121. sym->scancode,
  122. SDL_GetScancodeName(sym->scancode),
  123. sym->sym, SDL_GetKeyName(sym->sym));
  124. } else {
  125. print_string(&spot, &left,
  126. "Unknown Key (scancode %d = %s) %s ",
  127. sym->scancode,
  128. SDL_GetScancodeName(sym->scancode),
  129. pressed ? "pressed " : "released");
  130. }
  131. print_modifiers(&spot, &left);
  132. if (repeat) {
  133. print_string(&spot, &left, " (repeat)");
  134. }
  135. SDL_Log("%s\n", message);
  136. }
  137. static void
  138. PrintText(const char *eventtype, const char *text)
  139. {
  140. const char *spot;
  141. char expanded[1024];
  142. expanded[0] = '\0';
  143. for (spot = text; *spot; ++spot) {
  144. size_t length = SDL_strlen(expanded);
  145. (void)SDL_snprintf(expanded + length, sizeof(expanded) - length, "\\x%.2x", (unsigned char)*spot);
  146. }
  147. SDL_Log("%s Text (%s): \"%s%s\"\n", eventtype, expanded, *text == '"' ? "\\" : "", text);
  148. }
  149. void loop()
  150. {
  151. SDL_Event event;
  152. /* Check for events */
  153. /*SDL_WaitEvent(&event); emscripten does not like waiting*/
  154. while (SDL_PollEvent(&event)) {
  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. if (event.type == SDL_KEYDOWN) {
  160. switch (event.key.keysym.sym) {
  161. case SDLK_BACKSPACE:
  162. SDLTest_TextWindowAddText(textwin, "\b");
  163. break;
  164. case SDLK_RETURN:
  165. SDLTest_TextWindowAddText(textwin, "\n");
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. break;
  172. case SDL_TEXTEDITING:
  173. PrintText("EDIT", event.edit.text);
  174. break;
  175. case SDL_TEXTEDITING_EXT:
  176. PrintText("EDIT_EXT", event.editExt.text);
  177. SDL_free(event.editExt.text);
  178. break;
  179. case SDL_TEXTINPUT:
  180. PrintText("INPUT", event.text.text);
  181. SDLTest_TextWindowAddText(textwin, "%s", event.text.text);
  182. break;
  183. case SDL_FINGERDOWN:
  184. if (SDL_IsTextInputActive()) {
  185. SDL_Log("Stopping text input\n");
  186. SDL_StopTextInput();
  187. } else {
  188. SDL_Log("Starting text input\n");
  189. SDL_StartTextInput();
  190. }
  191. break;
  192. case SDL_MOUSEBUTTONDOWN:
  193. /* Left button quits the app, other buttons toggles text input */
  194. if (event.button.button == SDL_BUTTON_LEFT) {
  195. done = 1;
  196. } else {
  197. if (SDL_IsTextInputActive()) {
  198. SDL_Log("Stopping text input\n");
  199. SDL_StopTextInput();
  200. } else {
  201. SDL_Log("Starting text input\n");
  202. SDL_StartTextInput();
  203. }
  204. }
  205. break;
  206. case SDL_QUIT:
  207. done = 1;
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
  214. SDL_RenderClear(renderer);
  215. SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
  216. SDLTest_TextWindowDisplay(textwin, renderer);
  217. SDL_RenderPresent(renderer);
  218. /* Slow down framerate */
  219. SDL_Delay(100);
  220. #ifdef __EMSCRIPTEN__
  221. if (done) {
  222. emscripten_cancel_main_loop();
  223. }
  224. #endif
  225. }
  226. int main(int argc, char *argv[])
  227. {
  228. /* Enable standard application logging */
  229. SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
  230. /* Disable mouse emulation */
  231. SDL_SetHint(SDL_HINT_TOUCH_MOUSE_EVENTS, "0");
  232. /* Enable extended text editing events */
  233. SDL_SetHint(SDL_HINT_IME_SUPPORT_EXTENDED_TEXT, "1");
  234. /* Initialize SDL */
  235. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  236. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s\n", SDL_GetError());
  237. return 1;
  238. }
  239. /* Set 640x480 video mode */
  240. window = SDL_CreateWindow("CheckKeys Test",
  241. SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
  242. 640, 480, 0);
  243. if (window == NULL) {
  244. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create 640x480 window: %s\n",
  245. SDL_GetError());
  246. quit(2);
  247. }
  248. renderer = SDL_CreateRenderer(window, -1, 0);
  249. if (renderer == NULL) {
  250. SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create renderer: %s\n",
  251. SDL_GetError());
  252. quit(2);
  253. }
  254. textwin = SDLTest_TextWindowCreate(0, 0, 640, 480);
  255. #if __IPHONEOS__
  256. /* Creating the context creates the view, which we need to show keyboard */
  257. SDL_GL_CreateContext(window);
  258. #endif
  259. SDL_StartTextInput();
  260. /* Print initial modifier state */
  261. SDL_PumpEvents();
  262. PrintModifierState();
  263. /* Watch keystrokes */
  264. done = 0;
  265. #ifdef __EMSCRIPTEN__
  266. emscripten_set_main_loop(loop, 0, 1);
  267. #else
  268. while (!done) {
  269. loop();
  270. }
  271. #endif
  272. SDL_Quit();
  273. return 0;
  274. }
  275. /* vi: set ts=4 sw=4 expandtab: */