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

301 lines
11 KiB

  1. /*
  2. * keyboard.c
  3. * written by Holmes Futrell
  4. * use however you want
  5. */
  6. #include "SDL.h"
  7. #include "common.h"
  8. #define TEST_INPUT_RECT
  9. #define GLYPH_SIZE_IMAGE 16 /* size of glyphs (characters) in the bitmap font file */
  10. #define GLYPH_SIZE_SCREEN 32 /* size of glyphs (characters) as shown on the screen */
  11. #define MAX_CHARS 1024
  12. static SDL_Texture *texture; /* texture where we'll hold our font */
  13. static SDL_Renderer *renderer;
  14. static int numChars = 0; /* number of characters we've typed so far */
  15. static SDL_Color bg_color = { 50, 50, 100, 255 }; /* color of background */
  16. static int glyphs[MAX_CHARS];
  17. /* this structure maps a scancode to an index in our bitmap font.
  18. it also contains data about under which modifiers the mapping is valid
  19. (for example, we don't want shift + 1 to produce the character '1',
  20. but rather the character '!')
  21. */
  22. typedef struct
  23. {
  24. SDL_Scancode scancode; /* scancode of the key we want to map */
  25. int allow_no_mod; /* is the map valid if the key has no modifiers? */
  26. SDL_Keymod mod; /* what modifiers are allowed for the mapping */
  27. int index; /* what index in the font does the scancode map to */
  28. } fontMapping;
  29. #define TABLE_SIZE 51 /* size of our table which maps keys and modifiers to font indices */
  30. /* Below is the table that defines the mapping between scancodes and modifiers to indices in the
  31. bitmap font. As an example, then line '{ SDL_SCANCODE_A, 1, KMOD_SHIFT, 33 }' means, map
  32. the key A (which has scancode SDL_SCANCODE_A) to index 33 in the font (which is a picture of an A),
  33. The '1' means that the mapping is valid even if there are no modifiers, and KMOD_SHIFT means the
  34. mapping is also valid if the user is holding shift.
  35. */
  36. fontMapping map[TABLE_SIZE] = {
  37. {SDL_SCANCODE_A, 1, KMOD_SHIFT, 33}, /* A */
  38. {SDL_SCANCODE_B, 1, KMOD_SHIFT, 34}, /* B */
  39. {SDL_SCANCODE_C, 1, KMOD_SHIFT, 35}, /* C */
  40. {SDL_SCANCODE_D, 1, KMOD_SHIFT, 36}, /* D */
  41. {SDL_SCANCODE_E, 1, KMOD_SHIFT, 37}, /* E */
  42. {SDL_SCANCODE_F, 1, KMOD_SHIFT, 38}, /* F */
  43. {SDL_SCANCODE_G, 1, KMOD_SHIFT, 39}, /* G */
  44. {SDL_SCANCODE_H, 1, KMOD_SHIFT, 40}, /* H */
  45. {SDL_SCANCODE_I, 1, KMOD_SHIFT, 41}, /* I */
  46. {SDL_SCANCODE_J, 1, KMOD_SHIFT, 42}, /* J */
  47. {SDL_SCANCODE_K, 1, KMOD_SHIFT, 43}, /* K */
  48. {SDL_SCANCODE_L, 1, KMOD_SHIFT, 44}, /* L */
  49. {SDL_SCANCODE_M, 1, KMOD_SHIFT, 45}, /* M */
  50. {SDL_SCANCODE_N, 1, KMOD_SHIFT, 46}, /* N */
  51. {SDL_SCANCODE_O, 1, KMOD_SHIFT, 47}, /* O */
  52. {SDL_SCANCODE_P, 1, KMOD_SHIFT, 48}, /* P */
  53. {SDL_SCANCODE_Q, 1, KMOD_SHIFT, 49}, /* Q */
  54. {SDL_SCANCODE_R, 1, KMOD_SHIFT, 50}, /* R */
  55. {SDL_SCANCODE_S, 1, KMOD_SHIFT, 51}, /* S */
  56. {SDL_SCANCODE_T, 1, KMOD_SHIFT, 52}, /* T */
  57. {SDL_SCANCODE_U, 1, KMOD_SHIFT, 53}, /* U */
  58. {SDL_SCANCODE_V, 1, KMOD_SHIFT, 54}, /* V */
  59. {SDL_SCANCODE_W, 1, KMOD_SHIFT, 55}, /* W */
  60. {SDL_SCANCODE_X, 1, KMOD_SHIFT, 56}, /* X */
  61. {SDL_SCANCODE_Y, 1, KMOD_SHIFT, 57}, /* Y */
  62. {SDL_SCANCODE_Z, 1, KMOD_SHIFT, 58}, /* Z */
  63. {SDL_SCANCODE_0, 1, 0, 16}, /* 0 */
  64. {SDL_SCANCODE_1, 1, 0, 17}, /* 1 */
  65. {SDL_SCANCODE_2, 1, 0, 18}, /* 2 */
  66. {SDL_SCANCODE_3, 1, 0, 19}, /* 3 */
  67. {SDL_SCANCODE_4, 1, 0, 20}, /* 4 */
  68. {SDL_SCANCODE_5, 1, 0, 21}, /* 5 */
  69. {SDL_SCANCODE_6, 1, 0, 22}, /* 6 */
  70. {SDL_SCANCODE_7, 1, 0, 23}, /* 7 */
  71. {SDL_SCANCODE_8, 1, 0, 24}, /* 8 */
  72. {SDL_SCANCODE_9, 1, 0, 25}, /* 9 */
  73. {SDL_SCANCODE_SPACE, 1, 0, 0}, /* ' ' */
  74. {SDL_SCANCODE_1, 0, KMOD_SHIFT, 1}, /* ! */
  75. {SDL_SCANCODE_SLASH, 0, KMOD_SHIFT, 31}, /* ? */
  76. {SDL_SCANCODE_SLASH, 1, 0, 15}, /* / */
  77. {SDL_SCANCODE_COMMA, 1, 0, 12}, /* , */
  78. {SDL_SCANCODE_SEMICOLON, 1, 0, 27}, /* ; */
  79. {SDL_SCANCODE_SEMICOLON, 0, KMOD_SHIFT, 26}, /* : */
  80. {SDL_SCANCODE_PERIOD, 1, 0, 14}, /* . */
  81. {SDL_SCANCODE_MINUS, 1, 0, 13}, /* - */
  82. {SDL_SCANCODE_EQUALS, 0, KMOD_SHIFT, 11}, /* = */
  83. {SDL_SCANCODE_APOSTROPHE, 1, 0, 7}, /* ' */
  84. {SDL_SCANCODE_APOSTROPHE, 0, KMOD_SHIFT, 2}, /* " */
  85. {SDL_SCANCODE_5, 0, KMOD_SHIFT, 5}, /* % */
  86. };
  87. /*
  88. This function maps an SDL_KeySym to an index in the bitmap font.
  89. It does so by scanning through the font mapping table one entry
  90. at a time.
  91. If a match is found (scancode and allowed modifiers), the proper
  92. index is returned.
  93. If there is no entry for the key, -1 is returned
  94. */
  95. int
  96. keyToGlyphIndex(SDL_Keysym key)
  97. {
  98. int i, index = -1;
  99. for (i = 0; i < TABLE_SIZE; i++) {
  100. fontMapping compare = map[i];
  101. if (key.scancode == compare.scancode) {
  102. /* if this entry is valid with no key mod and we have no keymod, or if
  103. the key's modifiers are allowed modifiers for that mapping */
  104. if ((compare.allow_no_mod && key.mod == 0)
  105. || (key.mod & compare.mod)) {
  106. index = compare.index;
  107. break;
  108. }
  109. }
  110. }
  111. return index;
  112. }
  113. /*
  114. This function returns and x,y position for a given character number.
  115. It is used for positioning each character of text
  116. */
  117. void
  118. getPositionForCharNumber(int n, int *x, int *y)
  119. {
  120. int renderW, renderH;
  121. SDL_RenderGetLogicalSize(renderer, &renderW, &renderH);
  122. int x_padding = 16; /* padding space on left and right side of screen */
  123. int y_padding = 32; /* padding space at top of screen */
  124. /* figure out the number of characters that can fit horizontally across the screen */
  125. int max_x_chars = (renderW - 2 * x_padding) / GLYPH_SIZE_SCREEN;
  126. int line_separation = 5; /* pixels between each line */
  127. *x = (n % max_x_chars) * GLYPH_SIZE_SCREEN + x_padding;
  128. #ifdef TEST_INPUT_RECT
  129. *y = renderH - GLYPH_SIZE_SCREEN;
  130. #else
  131. *y = (n / max_x_chars) * (GLYPH_SIZE_SCREEN + line_separation) + y_padding;
  132. #endif
  133. }
  134. void
  135. drawGlyph(int glyph, int positionIndex)
  136. {
  137. int x, y;
  138. getPositionForCharNumber(positionIndex, &x, &y);
  139. SDL_Rect srcRect = { GLYPH_SIZE_IMAGE * glyph, 0, GLYPH_SIZE_IMAGE, GLYPH_SIZE_IMAGE };
  140. SDL_Rect dstRect = { x, y, GLYPH_SIZE_SCREEN, GLYPH_SIZE_SCREEN };
  141. SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
  142. }
  143. /* this function loads our font into an SDL_Texture and returns the SDL_Texture */
  144. SDL_Texture*
  145. loadFont(void)
  146. {
  147. SDL_Surface *surface = SDL_LoadBMP("kromasky_16x16.bmp");
  148. if (!surface) {
  149. printf("Error loading bitmap: %s\n", SDL_GetError());
  150. return 0;
  151. } else {
  152. /* set the transparent color for the bitmap font (hot pink) */
  153. SDL_SetColorKey(surface, 1, SDL_MapRGB(surface->format, 238, 0, 252));
  154. /* now we convert the surface to our desired pixel format */
  155. int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
  156. Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
  157. int bpp; /* bits per pixel for desired format */
  158. SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask,
  159. &Amask);
  160. SDL_Surface *converted =
  161. SDL_CreateRGBSurface(0, surface->w, surface->h, bpp, Rmask, Gmask,
  162. Bmask, Amask);
  163. SDL_BlitSurface(surface, NULL, converted, NULL);
  164. /* create our texture */
  165. texture = SDL_CreateTextureFromSurface(renderer, converted);
  166. if (texture == 0) {
  167. printf("texture creation failed: %s\n", SDL_GetError());
  168. } else {
  169. /* set blend mode for our texture */
  170. SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
  171. }
  172. SDL_FreeSurface(surface);
  173. SDL_FreeSurface(converted);
  174. return texture;
  175. }
  176. }
  177. void
  178. draw()
  179. {
  180. SDL_SetRenderDrawColor(renderer, bg_color.r, bg_color.g, bg_color.b, bg_color.a);
  181. SDL_RenderClear(renderer);
  182. for (int i = 0; i < numChars; i++) {
  183. drawGlyph(glyphs[i], i);
  184. }
  185. drawGlyph(29, numChars); /* cursor is at index 29 in the bitmap font */
  186. SDL_RenderPresent(renderer);
  187. }
  188. int
  189. main(int argc, char *argv[])
  190. {
  191. SDL_Window *window;
  192. SDL_Event event; /* last event received */
  193. SDL_Scancode scancode; /* scancode of last key we pushed */
  194. int width;
  195. int height;
  196. int done;
  197. SDL_Rect textrect;
  198. if (SDL_Init(SDL_INIT_VIDEO) < 0) {
  199. printf("Error initializing SDL: %s", SDL_GetError());
  200. }
  201. /* create window */
  202. window = SDL_CreateWindow("iOS keyboard test", 0, 0, 0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
  203. /* create renderer */
  204. renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC);
  205. SDL_GetWindowSize(window, &width, &height);
  206. SDL_RenderSetLogicalSize(renderer, width, height);
  207. /* load up our font */
  208. loadFont();
  209. /* Show onscreen keyboard */
  210. #ifdef TEST_INPUT_RECT
  211. textrect.x = 0;
  212. textrect.y = height - GLYPH_SIZE_IMAGE;
  213. textrect.w = width;
  214. textrect.h = GLYPH_SIZE_IMAGE;
  215. SDL_SetTextInputRect(&textrect);
  216. #endif
  217. SDL_StartTextInput();
  218. done = 0;
  219. while (!done) {
  220. while (SDL_PollEvent(&event)) {
  221. switch (event.type) {
  222. case SDL_QUIT:
  223. done = 1;
  224. break;
  225. case SDL_WINDOWEVENT:
  226. if (event.window.event == SDL_WINDOWEVENT_RESIZED) {
  227. width = event.window.data1;
  228. height = event.window.data2;
  229. SDL_RenderSetLogicalSize(renderer, width, height);
  230. #ifdef TEST_INPUT_RECT
  231. textrect.x = 0;
  232. textrect.y = height - GLYPH_SIZE_IMAGE;
  233. textrect.w = width;
  234. textrect.h = GLYPH_SIZE_IMAGE;
  235. SDL_SetTextInputRect(&textrect);
  236. #endif
  237. }
  238. break;
  239. case SDL_KEYDOWN:
  240. if (event.key.keysym.scancode == SDL_SCANCODE_BACKSPACE) {
  241. if (numChars > 0) {
  242. numChars--;
  243. }
  244. } else if (numChars + 1 < MAX_CHARS) {
  245. int index = keyToGlyphIndex(event.key.keysym);
  246. if (index >= 0) {
  247. glyphs[numChars++] = index;
  248. }
  249. }
  250. break;
  251. case SDL_MOUSEBUTTONUP:
  252. /* mouse up toggles onscreen keyboard visibility */
  253. if (SDL_IsTextInputActive()) {
  254. SDL_StopTextInput();
  255. } else {
  256. SDL_StartTextInput();
  257. }
  258. break;
  259. }
  260. }
  261. draw();
  262. SDL_Delay(15);
  263. }
  264. SDL_DestroyTexture(texture);
  265. SDL_DestroyRenderer(renderer);
  266. SDL_DestroyWindow(window);
  267. SDL_Quit();
  268. return 0;
  269. }