blob: bef5abd039d7ef448d708a6f7104141e54c6fcc0 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass0d10f292014-02-27 13:26:17 -07002/*
3 * Copyright (c) 2013 Google, Inc
Simon Glass0d10f292014-02-27 13:26:17 -07004 */
5
6#include <errno.h>
Simon Glass3e610172018-12-10 10:37:45 -07007#include <unistd.h>
Simon Glass4b0e1212020-02-03 07:36:10 -07008#include <stdbool.h>
Simon Glass0d10f292014-02-27 13:26:17 -07009#include <linux/input.h>
Simon Glassb6f4c1c2020-02-03 07:36:12 -070010#include <SDL2/SDL.h>
Simon Glass0d10f292014-02-27 13:26:17 -070011#include <asm/state.h>
12
Simon Glass0d22f412018-12-10 10:37:35 -070013/**
14 * struct buf_info - a data buffer holding audio data
15 *
16 * @pos: Current position playing in audio buffer
17 * @size: Size of data in audio buffer (0=empty)
18 * @alloced: Allocated size of audio buffer (max size it can hold)
19 * @data: Audio data
20 */
21struct buf_info {
22 uint pos;
23 uint size;
24 uint alloced;
25 uint8_t *data;
26};
27
Simon Glass840a8682020-02-03 07:36:08 -070028/**
29 * struct sdl_info - Information about our use of the SDL library
30 *
Simon Glass840a8682020-02-03 07:36:08 -070031 * @width: Width of simulated LCD display
32 * @height: Height of simulated LCD display
Simon Glassf91de0b2020-02-03 07:36:13 -070033 * @vis_width: Visible width (may be larger to allow for scaling up)
34 * @vis_height: Visible height (may be larger to allow for scaling up)
Simon Glass840a8682020-02-03 07:36:08 -070035 * @depth: Depth of the display in bits per pixel (16 or 32)
36 * @pitch: Number of bytes per line of the display
37 * @sample_rate: Current sample rate for audio
38 * @audio_active: true if audio can be used
39 * @inited: true if this module is initialised
40 * @cur_buf: Current audio buffer being used by sandbox_sdl_fill_audio (0 or 1)
41 * @buf: The two available audio buffers. SDL can be reading from one while we
42 * are setting up the next
43 * @running: true if audio is running
Simon Glass4b0e1212020-02-03 07:36:10 -070044 * @stopping: true if audio will stop once it runs out of data
Simon Glassb6f4c1c2020-02-03 07:36:12 -070045 * @texture: SDL texture to use for U-Boot display contents
46 * @renderer: SDL renderer to use
Simon Glass840a8682020-02-03 07:36:08 -070047 */
Simon Glass0d10f292014-02-27 13:26:17 -070048static struct sdl_info {
Simon Glass0d10f292014-02-27 13:26:17 -070049 int width;
50 int height;
Simon Glassf91de0b2020-02-03 07:36:13 -070051 int vis_width;
52 int vis_height;
Simon Glass0d10f292014-02-27 13:26:17 -070053 int depth;
54 int pitch;
Simon Glass0d7e6812018-11-15 19:56:14 -070055 uint sample_rate;
Simon Glass0d10f292014-02-27 13:26:17 -070056 bool audio_active;
57 bool inited;
Simon Glass0d22f412018-12-10 10:37:35 -070058 int cur_buf;
59 struct buf_info buf[2];
Simon Glass3e610172018-12-10 10:37:45 -070060 bool running;
Simon Glass4b0e1212020-02-03 07:36:10 -070061 bool stopping;
Simon Glassb6f4c1c2020-02-03 07:36:12 -070062 SDL_Texture *texture;
63 SDL_Renderer *renderer;
Simon Glass0d10f292014-02-27 13:26:17 -070064} sdl;
65
66static void sandbox_sdl_poll_events(void)
67{
68 /*
69 * We don't want to include common.h in this file since it uses
70 * system headers. So add a declation here.
71 */
Harald Seiler6f14d5f2020-12-15 16:47:52 +010072 extern void reset_cpu(void);
Simon Glass0d10f292014-02-27 13:26:17 -070073 SDL_Event event;
74
75 while (SDL_PollEvent(&event)) {
76 switch (event.type) {
77 case SDL_QUIT:
78 puts("LCD window closed - quitting\n");
Harald Seiler6f14d5f2020-12-15 16:47:52 +010079 reset_cpu();
Simon Glass0d10f292014-02-27 13:26:17 -070080 break;
81 }
82 }
83}
84
85static int sandbox_sdl_ensure_init(void)
86{
87 if (!sdl.inited) {
88 if (SDL_Init(0) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -070089 printf("Unable to initialise SDL: %s\n",
Simon Glass0d10f292014-02-27 13:26:17 -070090 SDL_GetError());
91 return -EIO;
92 }
93
94 atexit(SDL_Quit);
95
96 sdl.inited = true;
97 }
98 return 0;
99}
100
Simon Glassf91de0b2020-02-03 07:36:13 -0700101int sandbox_sdl_init_display(int width, int height, int log2_bpp,
102 bool double_size)
Simon Glass0d10f292014-02-27 13:26:17 -0700103{
104 struct sandbox_state *state = state_get_current();
105 int err;
106
107 if (!width || !state->show_lcd)
108 return 0;
109 err = sandbox_sdl_ensure_init();
110 if (err)
111 return err;
112 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -0700113 printf("Unable to initialise SDL LCD: %s\n", SDL_GetError());
Simon Glass0d10f292014-02-27 13:26:17 -0700114 return -EPERM;
115 }
Simon Glass0d10f292014-02-27 13:26:17 -0700116 sdl.width = width;
117 sdl.height = height;
Simon Glassf91de0b2020-02-03 07:36:13 -0700118 if (double_size) {
119 sdl.vis_width = sdl.width * 2;
120 sdl.vis_height = sdl.height * 2;
121 } else {
122 sdl.vis_width = sdl.width;
123 sdl.vis_height = sdl.height;
124 }
125
Simon Glass16823eb2021-07-05 16:32:47 -0600126 if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
127 printf("Unable to init hinting: %s", SDL_GetError());
128
Simon Glass0d10f292014-02-27 13:26:17 -0700129 sdl.depth = 1 << log2_bpp;
130 sdl.pitch = sdl.width * sdl.depth / 8;
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700131 SDL_Window *screen = SDL_CreateWindow("U-Boot", SDL_WINDOWPOS_UNDEFINED,
132 SDL_WINDOWPOS_UNDEFINED,
Heinrich Schuchardt6d754862020-10-09 23:44:11 +0200133 sdl.vis_width, sdl.vis_height,
134 SDL_WINDOW_RESIZABLE);
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700135 if (!screen) {
136 printf("Unable to initialise SDL screen: %s\n",
137 SDL_GetError());
138 return -EIO;
139 }
140 if (log2_bpp != 4 && log2_bpp != 5) {
141 printf("U-Boot SDL does not support depth %d\n", log2_bpp);
142 return -EINVAL;
143 }
144 sdl.renderer = SDL_CreateRenderer(screen, -1,
145 SDL_RENDERER_ACCELERATED |
146 SDL_RENDERER_PRESENTVSYNC);
147 if (!sdl.renderer) {
148 printf("Unable to initialise SDL renderer: %s\n",
149 SDL_GetError());
150 return -EIO;
151 }
152
153 sdl.texture = SDL_CreateTexture(sdl.renderer, log2_bpp == 4 ?
154 SDL_PIXELFORMAT_RGB565 :
155 SDL_PIXELFORMAT_RGB888,
156 SDL_TEXTUREACCESS_STREAMING,
157 width, height);
158 if (!sdl.texture) {
159 printf("Unable to initialise SDL texture: %s\n",
160 SDL_GetError());
161 return -EBADF;
162 }
Simon Glass0d10f292014-02-27 13:26:17 -0700163 sandbox_sdl_poll_events();
164
165 return 0;
166}
167
168int sandbox_sdl_sync(void *lcd_base)
169{
Simon Glass24012c82021-07-05 16:32:46 -0600170 struct SDL_Rect rect;
171 int ret;
172
173 if (!sdl.texture)
174 return 0;
175 SDL_RenderClear(sdl.renderer);
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700176 SDL_UpdateTexture(sdl.texture, NULL, lcd_base, sdl.pitch);
Simon Glass24012c82021-07-05 16:32:46 -0600177 ret = SDL_RenderCopy(sdl.renderer, sdl.texture, NULL, NULL);
178 if (ret) {
179 printf("SDL copy %d: %s\n", ret, SDL_GetError());
180 return -EIO;
181 }
182
183 /*
184 * On some machines this does not appear. Draw an empty rectangle which
185 * seems to fix that.
186 */
187 rect.x = 0;
188 rect.y = 0;
189 rect.w = 0;
190 rect.h = 0;
191 SDL_RenderDrawRect(sdl.renderer, &rect);
192
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700193 SDL_RenderPresent(sdl.renderer);
Simon Glass0d10f292014-02-27 13:26:17 -0700194 sandbox_sdl_poll_events();
195
196 return 0;
197}
198
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700199static const unsigned short sdl_to_keycode[SDL_NUM_SCANCODES] = {
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200200 [SDL_SCANCODE_ESCAPE] = KEY_ESC,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700201 [SDL_SCANCODE_1] = KEY_1,
202 [SDL_SCANCODE_2] = KEY_2,
203 [SDL_SCANCODE_3] = KEY_3,
204 [SDL_SCANCODE_4] = KEY_4,
205 [SDL_SCANCODE_5] = KEY_5,
206 [SDL_SCANCODE_6] = KEY_6,
207 [SDL_SCANCODE_7] = KEY_7,
208 [SDL_SCANCODE_8] = KEY_8,
209 [SDL_SCANCODE_9] = KEY_9,
210 [SDL_SCANCODE_0] = KEY_0,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700211 [SDL_SCANCODE_MINUS] = KEY_MINUS,
212 [SDL_SCANCODE_EQUALS] = KEY_EQUAL,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200213 [SDL_SCANCODE_BACKSPACE] = KEY_BACKSPACE,
214 [SDL_SCANCODE_TAB] = KEY_TAB,
215 [SDL_SCANCODE_Q] = KEY_Q,
216 [SDL_SCANCODE_W] = KEY_W,
217 [SDL_SCANCODE_E] = KEY_E,
218 [SDL_SCANCODE_R] = KEY_R,
219 [SDL_SCANCODE_T] = KEY_T,
220 [SDL_SCANCODE_Y] = KEY_Y,
221 [SDL_SCANCODE_U] = KEY_U,
222 [SDL_SCANCODE_I] = KEY_I,
223 [SDL_SCANCODE_O] = KEY_O,
224 [SDL_SCANCODE_P] = KEY_P,
225 [SDL_SCANCODE_LEFTBRACKET] = KEY_LEFTBRACE,
226 [SDL_SCANCODE_RIGHTBRACKET] = KEY_RIGHTBRACE,
227 [SDL_SCANCODE_RETURN] = KEY_ENTER,
228 [SDL_SCANCODE_LCTRL] = KEY_LEFTCTRL,
229 [SDL_SCANCODE_A] = KEY_A,
230 [SDL_SCANCODE_S] = KEY_S,
231 [SDL_SCANCODE_D] = KEY_D,
232 [SDL_SCANCODE_F] = KEY_F,
233 [SDL_SCANCODE_G] = KEY_G,
234 [SDL_SCANCODE_H] = KEY_H,
235 [SDL_SCANCODE_J] = KEY_J,
236 [SDL_SCANCODE_K] = KEY_K,
237 [SDL_SCANCODE_L] = KEY_L,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700238 [SDL_SCANCODE_SEMICOLON] = KEY_SEMICOLON,
239 [SDL_SCANCODE_APOSTROPHE] = KEY_APOSTROPHE,
240 [SDL_SCANCODE_GRAVE] = KEY_GRAVE,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200241 [SDL_SCANCODE_LSHIFT] = KEY_LEFTSHIFT,
242 [SDL_SCANCODE_BACKSLASH] = KEY_BACKSLASH,
243 [SDL_SCANCODE_Z] = KEY_Z,
244 [SDL_SCANCODE_X] = KEY_X,
245 [SDL_SCANCODE_C] = KEY_C,
246 [SDL_SCANCODE_V] = KEY_V,
247 [SDL_SCANCODE_B] = KEY_B,
248 [SDL_SCANCODE_N] = KEY_N,
249 [SDL_SCANCODE_M] = KEY_M,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700250 [SDL_SCANCODE_COMMA] = KEY_COMMA,
251 [SDL_SCANCODE_PERIOD] = KEY_DOT,
252 [SDL_SCANCODE_SLASH] = KEY_SLASH,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200253 [SDL_SCANCODE_RSHIFT] = KEY_RIGHTSHIFT,
254 [SDL_SCANCODE_KP_MULTIPLY] = KEY_KPASTERISK,
255 [SDL_SCANCODE_LALT] = KEY_LEFTALT,
256 [SDL_SCANCODE_SPACE] = KEY_SPACE,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700257 [SDL_SCANCODE_CAPSLOCK] = KEY_CAPSLOCK,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700258 [SDL_SCANCODE_F1] = KEY_F1,
259 [SDL_SCANCODE_F2] = KEY_F2,
260 [SDL_SCANCODE_F3] = KEY_F3,
261 [SDL_SCANCODE_F4] = KEY_F4,
262 [SDL_SCANCODE_F5] = KEY_F5,
263 [SDL_SCANCODE_F6] = KEY_F6,
264 [SDL_SCANCODE_F7] = KEY_F7,
265 [SDL_SCANCODE_F8] = KEY_F8,
266 [SDL_SCANCODE_F9] = KEY_F9,
267 [SDL_SCANCODE_F10] = KEY_F10,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700268 [SDL_SCANCODE_NUMLOCKCLEAR] = KEY_NUMLOCK,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200269 [SDL_SCANCODE_SCROLLLOCK] = KEY_SCROLLLOCK,
270 [SDL_SCANCODE_KP_7] = KEY_KP7,
271 [SDL_SCANCODE_KP_8] = KEY_KP8,
272 [SDL_SCANCODE_KP_9] = KEY_KP9,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700273 [SDL_SCANCODE_KP_MINUS] = KEY_KPMINUS,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200274 [SDL_SCANCODE_KP_4] = KEY_KP4,
275 [SDL_SCANCODE_KP_5] = KEY_KP5,
276 [SDL_SCANCODE_KP_6] = KEY_KP6,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700277 [SDL_SCANCODE_KP_PLUS] = KEY_KPPLUS,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700278 [SDL_SCANCODE_KP_1] = KEY_KP1,
279 [SDL_SCANCODE_KP_2] = KEY_KP2,
280 [SDL_SCANCODE_KP_3] = KEY_KP3,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700281 [SDL_SCANCODE_KP_0] = KEY_KP0,
282 [SDL_SCANCODE_KP_PERIOD] = KEY_KPDOT,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200283 /* key 84 does not exist linux_input.h */
284 [SDL_SCANCODE_LANG5] = KEY_ZENKAKUHANKAKU,
285 [SDL_SCANCODE_NONUSBACKSLASH] = KEY_102ND,
286 [SDL_SCANCODE_F11] = KEY_F11,
287 [SDL_SCANCODE_F12] = KEY_F12,
288 [SDL_SCANCODE_INTERNATIONAL1] = KEY_RO,
289 [SDL_SCANCODE_LANG3] = KEY_KATAKANA,
290 [SDL_SCANCODE_LANG4] = KEY_HIRAGANA,
291 [SDL_SCANCODE_INTERNATIONAL4] = KEY_HENKAN,
292 [SDL_SCANCODE_INTERNATIONAL2] = KEY_KATAKANAHIRAGANA,
293 [SDL_SCANCODE_INTERNATIONAL5] = KEY_MUHENKAN,
294 /* [SDL_SCANCODE_INTERNATIONAL5] -> [KEY_KPJPCOMMA] */
295 [SDL_SCANCODE_KP_ENTER] = KEY_KPENTER,
296 [SDL_SCANCODE_RCTRL] = KEY_RIGHTCTRL,
297 [SDL_SCANCODE_KP_DIVIDE] = KEY_KPSLASH,
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700298 [SDL_SCANCODE_SYSREQ] = KEY_SYSRQ,
Heinrich Schuchardt6ce563b2020-09-29 01:41:18 +0200299 [SDL_SCANCODE_RALT] = KEY_RIGHTALT,
300 /* KEY_LINEFEED */
301 [SDL_SCANCODE_HOME] = KEY_HOME,
302 [SDL_SCANCODE_UP] = KEY_UP,
303 [SDL_SCANCODE_PAGEUP] = KEY_PAGEUP,
304 [SDL_SCANCODE_LEFT] = KEY_LEFT,
305 [SDL_SCANCODE_RIGHT] = KEY_RIGHT,
306 [SDL_SCANCODE_END] = KEY_END,
307 [SDL_SCANCODE_DOWN] = KEY_DOWN,
308 [SDL_SCANCODE_PAGEDOWN] = KEY_PAGEDOWN,
309 [SDL_SCANCODE_INSERT] = KEY_INSERT,
310 [SDL_SCANCODE_DELETE] = KEY_DELETE,
311 /* KEY_MACRO */
312 [SDL_SCANCODE_MUTE] = KEY_MUTE,
313 [SDL_SCANCODE_VOLUMEDOWN] = KEY_VOLUMEDOWN,
314 [SDL_SCANCODE_VOLUMEUP] = KEY_VOLUMEUP,
315 [SDL_SCANCODE_POWER] = KEY_POWER,
316 [SDL_SCANCODE_KP_EQUALS] = KEY_KPEQUAL,
317 [SDL_SCANCODE_KP_PLUSMINUS] = KEY_KPPLUSMINUS,
318 [SDL_SCANCODE_PAUSE] = KEY_PAUSE,
319 /* KEY_SCALE */
320 [SDL_SCANCODE_KP_COMMA] = KEY_KPCOMMA,
321 [SDL_SCANCODE_LANG1] = KEY_HANGUEL,
322 [SDL_SCANCODE_LANG2] = KEY_HANJA,
323 [SDL_SCANCODE_INTERNATIONAL3] = KEY_YEN,
324 [SDL_SCANCODE_LGUI] = KEY_LEFTMETA,
325 [SDL_SCANCODE_RGUI] = KEY_RIGHTMETA,
326 [SDL_SCANCODE_APPLICATION] = KEY_COMPOSE,
Simon Glass0d10f292014-02-27 13:26:17 -0700327};
328
329int sandbox_sdl_scan_keys(int key[], int max_keys)
330{
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700331 const Uint8 *keystate;
332 int num_keys;
Simon Glass0d10f292014-02-27 13:26:17 -0700333 int i, count;
334
335 sandbox_sdl_poll_events();
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700336 keystate = SDL_GetKeyboardState(&num_keys);
337 for (i = count = 0; i < num_keys; i++) {
338 if (count < max_keys && keystate[i]) {
339 int keycode = sdl_to_keycode[i];
340
341 if (keycode)
342 key[count++] = keycode;
343 }
Simon Glass0d10f292014-02-27 13:26:17 -0700344 }
345
346 return count;
347}
348
349int sandbox_sdl_key_pressed(int keycode)
350{
351 int key[8]; /* allow up to 8 keys to be pressed at once */
352 int count;
353 int i;
354
355 count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
356 for (i = 0; i < count; i++) {
357 if (key[i] == keycode)
358 return 0;
359 }
360
361 return -ENOENT;
362}
363
364void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
365{
Simon Glass0d22f412018-12-10 10:37:35 -0700366 struct buf_info *buf;
Simon Glass0d10f292014-02-27 13:26:17 -0700367 int avail;
Simon Glass4b0e1212020-02-03 07:36:10 -0700368 bool have_data = false;
Simon Glass0d22f412018-12-10 10:37:35 -0700369 int i;
Simon Glass0d10f292014-02-27 13:26:17 -0700370
Simon Glass0d22f412018-12-10 10:37:35 -0700371 for (i = 0; i < 2; i++) {
372 buf = &sdl.buf[sdl.cur_buf];
373 avail = buf->size - buf->pos;
374 if (avail <= 0) {
375 sdl.cur_buf = 1 - sdl.cur_buf;
376 continue;
377 }
378 if (avail > len)
379 avail = len;
Simon Glass4b0e1212020-02-03 07:36:10 -0700380 have_data = true;
Simon Glass0d10f292014-02-27 13:26:17 -0700381
Simon Glass0d22f412018-12-10 10:37:35 -0700382 SDL_MixAudio(stream, buf->data + buf->pos, avail,
383 SDL_MIX_MAXVOLUME);
384 buf->pos += avail;
385 len -= avail;
Simon Glass0d10f292014-02-27 13:26:17 -0700386
Simon Glass0d22f412018-12-10 10:37:35 -0700387 /* Move to next buffer if we are at the end */
388 if (buf->pos == buf->size)
389 buf->size = 0;
390 else
391 break;
392 }
Simon Glass4b0e1212020-02-03 07:36:10 -0700393 sdl.stopping = !have_data;
Simon Glass0d10f292014-02-27 13:26:17 -0700394}
395
Simon Glass658a8742018-12-10 10:37:50 -0700396int sandbox_sdl_sound_init(int rate, int channels)
Simon Glass0d10f292014-02-27 13:26:17 -0700397{
Simon Glassf0e2f422020-02-03 07:36:09 -0700398 SDL_AudioSpec wanted, have;
Simon Glass0d22f412018-12-10 10:37:35 -0700399 int i;
Simon Glass0d10f292014-02-27 13:26:17 -0700400
401 if (sandbox_sdl_ensure_init())
402 return -1;
403
404 if (sdl.audio_active)
405 return 0;
406
Simon Glass0d10f292014-02-27 13:26:17 -0700407 /* Set the audio format */
Simon Glass658a8742018-12-10 10:37:50 -0700408 wanted.freq = rate;
Simon Glass0d10f292014-02-27 13:26:17 -0700409 wanted.format = AUDIO_S16;
Simon Glass658a8742018-12-10 10:37:50 -0700410 wanted.channels = channels;
Simon Glass0d10f292014-02-27 13:26:17 -0700411 wanted.samples = 1024; /* Good low-latency value for callback */
412 wanted.callback = sandbox_sdl_fill_audio;
413 wanted.userdata = NULL;
414
Simon Glass0d22f412018-12-10 10:37:35 -0700415 for (i = 0; i < 2; i++) {
416 struct buf_info *buf = &sdl.buf[i];
417
418 buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels;
419 buf->data = malloc(buf->alloced);
420 if (!buf->data) {
421 printf("%s: Out of memory\n", __func__);
422 if (i == 1)
423 free(sdl.buf[0].data);
424 return -1;
425 }
426 buf->pos = 0;
427 buf->size = 0;
Simon Glass0d10f292014-02-27 13:26:17 -0700428 }
Simon Glass0d10f292014-02-27 13:26:17 -0700429
430 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -0700431 printf("Unable to initialise SDL audio: %s\n", SDL_GetError());
Simon Glass0d10f292014-02-27 13:26:17 -0700432 goto err;
433 }
434
435 /* Open the audio device, forcing the desired format */
Simon Glassf0e2f422020-02-03 07:36:09 -0700436 if (SDL_OpenAudio(&wanted, &have) < 0) {
Simon Glass0d10f292014-02-27 13:26:17 -0700437 printf("Couldn't open audio: %s\n", SDL_GetError());
438 goto err;
439 }
Simon Glassf0e2f422020-02-03 07:36:09 -0700440 if (have.format != wanted.format) {
441 printf("Couldn't select required audio format\n");
442 goto err;
443 }
Simon Glass0d10f292014-02-27 13:26:17 -0700444 sdl.audio_active = true;
Simon Glass0d7e6812018-11-15 19:56:14 -0700445 sdl.sample_rate = wanted.freq;
Simon Glass0d22f412018-12-10 10:37:35 -0700446 sdl.cur_buf = 0;
Simon Glass4b0e1212020-02-03 07:36:10 -0700447 sdl.running = false;
Simon Glass0d10f292014-02-27 13:26:17 -0700448
449 return 0;
450
451err:
Simon Glass0d22f412018-12-10 10:37:35 -0700452 for (i = 0; i < 2; i++)
453 free(sdl.buf[i].data);
Simon Glass0d10f292014-02-27 13:26:17 -0700454 return -1;
455}
456
Simon Glass3e610172018-12-10 10:37:45 -0700457int sandbox_sdl_sound_play(const void *data, uint size)
Simon Glass0d10f292014-02-27 13:26:17 -0700458{
Simon Glass3e610172018-12-10 10:37:45 -0700459 struct buf_info *buf;
Simon Glass0d22f412018-12-10 10:37:35 -0700460
Simon Glass0d10f292014-02-27 13:26:17 -0700461 if (!sdl.audio_active)
Simon Glass3e610172018-12-10 10:37:45 -0700462 return 0;
463
464 buf = &sdl.buf[0];
465 if (buf->size)
466 buf = &sdl.buf[1];
467 while (buf->size)
468 usleep(1000);
469
470 if (size > buf->alloced)
471 return -E2BIG;
472
473 memcpy(buf->data, data, size);
474 buf->size = size;
Simon Glass0d22f412018-12-10 10:37:35 -0700475 buf->pos = 0;
Simon Glass3e610172018-12-10 10:37:45 -0700476 if (!sdl.running) {
477 SDL_PauseAudio(0);
Simon Glass4b0e1212020-02-03 07:36:10 -0700478 sdl.running = true;
479 sdl.stopping = false;
Simon Glass3e610172018-12-10 10:37:45 -0700480 }
Simon Glass0d10f292014-02-27 13:26:17 -0700481
482 return 0;
483}
484
485int sandbox_sdl_sound_stop(void)
486{
Simon Glass3e610172018-12-10 10:37:45 -0700487 if (sdl.running) {
Simon Glass4b0e1212020-02-03 07:36:10 -0700488 while (!sdl.stopping)
489 SDL_Delay(100);
490
Simon Glass3e610172018-12-10 10:37:45 -0700491 SDL_PauseAudio(1);
492 sdl.running = 0;
Simon Glass4b0e1212020-02-03 07:36:10 -0700493 sdl.stopping = false;
Simon Glass3e610172018-12-10 10:37:45 -0700494 }
Simon Glass0d10f292014-02-27 13:26:17 -0700495
496 return 0;
497}