sdl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * Copyright (c) 2013 Google, Inc
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. #include <errno.h>
  7. #include <linux/input.h>
  8. #include <SDL/SDL.h>
  9. #include <sound.h>
  10. #include <asm/state.h>
  11. static struct sdl_info {
  12. SDL_Surface *screen;
  13. int width;
  14. int height;
  15. int depth;
  16. int pitch;
  17. uint frequency;
  18. uint audio_pos;
  19. uint audio_size;
  20. uint8_t *audio_data;
  21. bool audio_active;
  22. bool inited;
  23. } sdl;
  24. static void sandbox_sdl_poll_events(void)
  25. {
  26. /*
  27. * We don't want to include common.h in this file since it uses
  28. * system headers. So add a declation here.
  29. */
  30. extern void reset_cpu(unsigned long addr);
  31. SDL_Event event;
  32. while (SDL_PollEvent(&event)) {
  33. switch (event.type) {
  34. case SDL_QUIT:
  35. puts("LCD window closed - quitting\n");
  36. reset_cpu(1);
  37. break;
  38. }
  39. }
  40. }
  41. static int sandbox_sdl_ensure_init(void)
  42. {
  43. if (!sdl.inited) {
  44. if (SDL_Init(0) < 0) {
  45. printf("Unable to initialize SDL: %s\n",
  46. SDL_GetError());
  47. return -EIO;
  48. }
  49. atexit(SDL_Quit);
  50. sdl.inited = true;
  51. }
  52. return 0;
  53. }
  54. int sandbox_sdl_init_display(int width, int height, int log2_bpp)
  55. {
  56. struct sandbox_state *state = state_get_current();
  57. int err;
  58. if (!width || !state->show_lcd)
  59. return 0;
  60. err = sandbox_sdl_ensure_init();
  61. if (err)
  62. return err;
  63. if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
  64. printf("Unable to initialize SDL LCD: %s\n", SDL_GetError());
  65. return -EPERM;
  66. }
  67. SDL_WM_SetCaption("U-Boot", "U-Boot");
  68. sdl.width = width;
  69. sdl.height = height;
  70. sdl.depth = 1 << log2_bpp;
  71. sdl.pitch = sdl.width * sdl.depth / 8;
  72. sdl.screen = SDL_SetVideoMode(width, height, 0, 0);
  73. sandbox_sdl_poll_events();
  74. return 0;
  75. }
  76. int sandbox_sdl_sync(void *lcd_base)
  77. {
  78. SDL_Surface *frame;
  79. frame = SDL_CreateRGBSurfaceFrom(lcd_base, sdl.width, sdl.height,
  80. sdl.depth, sdl.pitch,
  81. 0x1f << 11, 0x3f << 5, 0x1f << 0, 0);
  82. SDL_BlitSurface(frame, NULL, sdl.screen, NULL);
  83. SDL_FreeSurface(frame);
  84. SDL_UpdateRect(sdl.screen, 0, 0, 0, 0);
  85. sandbox_sdl_poll_events();
  86. return 0;
  87. }
  88. #define NONE (-1)
  89. #define NUM_SDL_CODES (SDLK_UNDO + 1)
  90. static int16_t sdl_to_keycode[NUM_SDL_CODES] = {
  91. /* 0 */
  92. NONE, NONE, NONE, NONE, NONE,
  93. NONE, NONE, NONE, KEY_BACKSPACE, KEY_TAB,
  94. NONE, NONE, NONE, KEY_ENTER, NONE,
  95. NONE, NONE, NONE, NONE, KEY_POWER, /* use PAUSE as POWER */
  96. /* 20 */
  97. NONE, NONE, NONE, NONE, NONE,
  98. NONE, NONE, KEY_ESC, NONE, NONE,
  99. NONE, NONE, KEY_SPACE, NONE, NONE,
  100. NONE, NONE, NONE, NONE, NONE,
  101. /* 40 */
  102. NONE, NONE, NONE, NONE, KEY_COMMA,
  103. KEY_MINUS, KEY_DOT, KEY_SLASH, KEY_0, KEY_1,
  104. KEY_2, KEY_3, KEY_4, KEY_5, KEY_6,
  105. KEY_7, KEY_8, KEY_9, NONE, KEY_SEMICOLON,
  106. /* 60 */
  107. NONE, KEY_EQUAL, NONE, NONE, NONE,
  108. NONE, NONE, NONE, NONE, NONE,
  109. NONE, NONE, NONE, NONE, NONE,
  110. NONE, NONE, NONE, NONE, NONE,
  111. /* 80 */
  112. NONE, NONE, NONE, NONE, NONE,
  113. NONE, NONE, NONE, NONE, NONE,
  114. NONE, NONE, KEY_BACKSLASH, NONE, NONE,
  115. NONE, KEY_GRAVE, KEY_A, KEY_B, KEY_C,
  116. /* 100 */
  117. KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
  118. KEY_I, KEY_J, KEY_K, KEY_L, KEY_M,
  119. KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
  120. KEY_S, KEY_T, KEY_U, KEY_V, KEY_W,
  121. /* 120 */
  122. KEY_X, KEY_Y, KEY_Z, NONE, NONE,
  123. NONE, NONE, KEY_DELETE, NONE, NONE,
  124. NONE, NONE, NONE, NONE, NONE,
  125. NONE, NONE, NONE, NONE, NONE,
  126. /* 140 */
  127. NONE, NONE, NONE, NONE, NONE,
  128. NONE, NONE, NONE, NONE, NONE,
  129. NONE, NONE, NONE, NONE, NONE,
  130. NONE, NONE, NONE, NONE, NONE,
  131. /* 160 */
  132. NONE, NONE, NONE, NONE, NONE,
  133. NONE, NONE, NONE, NONE, NONE,
  134. NONE, NONE, NONE, NONE, NONE,
  135. NONE, NONE, NONE, NONE, NONE,
  136. /* 180 */
  137. NONE, NONE, NONE, NONE, NONE,
  138. NONE, NONE, NONE, NONE, NONE,
  139. NONE, NONE, NONE, NONE, NONE,
  140. NONE, NONE, NONE, NONE, NONE,
  141. /* 200 */
  142. NONE, NONE, NONE, NONE, NONE,
  143. NONE, NONE, NONE, NONE, NONE,
  144. NONE, NONE, NONE, NONE, NONE,
  145. NONE, NONE, NONE, NONE, NONE,
  146. /* 220 */
  147. NONE, NONE, NONE, NONE, NONE,
  148. NONE, NONE, NONE, NONE, NONE,
  149. NONE, NONE, NONE, NONE, NONE,
  150. NONE, NONE, NONE, NONE, NONE,
  151. /* 240 */
  152. NONE, NONE, NONE, NONE, NONE,
  153. NONE, NONE, NONE, NONE, NONE,
  154. NONE, NONE, NONE, NONE, NONE,
  155. NONE, KEY_KP0, KEY_KP1, KEY_KP2, KEY_KP3,
  156. /* 260 */
  157. KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8,
  158. KEY_KP9, KEY_KPDOT, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS,
  159. KEY_KPPLUS, KEY_KPENTER, KEY_KPEQUAL, KEY_UP, KEY_DOWN,
  160. KEY_RIGHT, KEY_LEFT, KEY_INSERT, KEY_HOME, KEY_END,
  161. /* 280 */
  162. KEY_PAGEUP, KEY_PAGEDOWN, KEY_F1, KEY_F2, KEY_F3,
  163. KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8,
  164. KEY_F9, KEY_F10, KEY_F11, KEY_F12, NONE,
  165. NONE, NONE, NONE, NONE, NONE,
  166. /* 300 */
  167. KEY_NUMLOCK, KEY_CAPSLOCK, KEY_SCROLLLOCK, KEY_RIGHTSHIFT,
  168. KEY_LEFTSHIFT,
  169. KEY_RIGHTCTRL, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTALT, KEY_RIGHTMETA,
  170. KEY_LEFTMETA, NONE, KEY_FN, NONE, KEY_COMPOSE,
  171. NONE, KEY_PRINT, KEY_SYSRQ, KEY_PAUSE, NONE,
  172. /* 320 */
  173. NONE, NONE, NONE,
  174. };
  175. int sandbox_sdl_scan_keys(int key[], int max_keys)
  176. {
  177. Uint8 *keystate;
  178. int i, count;
  179. sandbox_sdl_poll_events();
  180. keystate = SDL_GetKeyState(NULL);
  181. for (i = count = 0; i < NUM_SDL_CODES; i++) {
  182. if (count >= max_keys)
  183. break;
  184. else if (keystate[i])
  185. key[count++] = sdl_to_keycode[i];
  186. }
  187. return count;
  188. }
  189. int sandbox_sdl_key_pressed(int keycode)
  190. {
  191. int key[8]; /* allow up to 8 keys to be pressed at once */
  192. int count;
  193. int i;
  194. count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
  195. for (i = 0; i < count; i++) {
  196. if (key[i] == keycode)
  197. return 0;
  198. }
  199. return -ENOENT;
  200. }
  201. void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
  202. {
  203. int avail;
  204. avail = sdl.audio_size - sdl.audio_pos;
  205. if (avail < len)
  206. len = avail;
  207. SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
  208. SDL_MIX_MAXVOLUME);
  209. sdl.audio_pos += len;
  210. /* Loop if we are at the end */
  211. if (sdl.audio_pos == sdl.audio_size)
  212. sdl.audio_pos = 0;
  213. }
  214. int sandbox_sdl_sound_init(void)
  215. {
  216. SDL_AudioSpec wanted;
  217. if (sandbox_sdl_ensure_init())
  218. return -1;
  219. if (sdl.audio_active)
  220. return 0;
  221. /*
  222. * At present all sandbox sounds crash. This is probably due to
  223. * symbol name conflicts with U-Boot. We can remove the malloc()
  224. * probles with:
  225. *
  226. * #define USE_DL_PREFIX
  227. *
  228. * and get this:
  229. *
  230. * Assertion 'e->pollfd->fd == e->fd' failed at pulse/mainloop.c:676,
  231. * function dispatch_pollfds(). Aborting.
  232. *
  233. * The right solution is probably to make U-Boot's names private or
  234. * link os.c and sdl.c against their libraries before liking with
  235. * U-Boot. TBD. For now sound is disabled.
  236. */
  237. printf("(Warning: sandbox sound disabled)\n");
  238. return 0;
  239. /* Set the audio format */
  240. wanted.freq = 22050;
  241. wanted.format = AUDIO_S16;
  242. wanted.channels = 1; /* 1 = mono, 2 = stereo */
  243. wanted.samples = 1024; /* Good low-latency value for callback */
  244. wanted.callback = sandbox_sdl_fill_audio;
  245. wanted.userdata = NULL;
  246. sdl.audio_size = sizeof(uint16_t) * wanted.freq;
  247. sdl.audio_data = malloc(sdl.audio_size);
  248. if (!sdl.audio_data) {
  249. printf("%s: Out of memory\n", __func__);
  250. return -1;
  251. }
  252. sdl.audio_pos = 0;
  253. if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
  254. printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
  255. goto err;
  256. }
  257. /* Open the audio device, forcing the desired format */
  258. if (SDL_OpenAudio(&wanted, NULL) < 0) {
  259. printf("Couldn't open audio: %s\n", SDL_GetError());
  260. goto err;
  261. }
  262. sdl.audio_active = true;
  263. return 0;
  264. err:
  265. free(sdl.audio_data);
  266. return -1;
  267. }
  268. int sandbox_sdl_sound_start(uint frequency)
  269. {
  270. if (!sdl.audio_active)
  271. return -1;
  272. sdl.frequency = frequency;
  273. sound_create_square_wave((unsigned short *)sdl.audio_data,
  274. sdl.audio_size, frequency);
  275. sdl.audio_pos = 0;
  276. SDL_PauseAudio(0);
  277. return 0;
  278. }
  279. int sandbox_sdl_sound_stop(void)
  280. {
  281. if (!sdl.audio_active)
  282. return -1;
  283. SDL_PauseAudio(1);
  284. return 0;
  285. }