keyboard.c 12 KB

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