sdl.c 7.4 KB

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