blob: 58a9cc8168eab7f3bc813e67027ef07b447f93fc [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
33 * @depth: Depth of the display in bits per pixel (16 or 32)
34 * @pitch: Number of bytes per line of the display
35 * @sample_rate: Current sample rate for audio
36 * @audio_active: true if audio can be used
37 * @inited: true if this module is initialised
38 * @cur_buf: Current audio buffer being used by sandbox_sdl_fill_audio (0 or 1)
39 * @buf: The two available audio buffers. SDL can be reading from one while we
40 * are setting up the next
41 * @running: true if audio is running
Simon Glass4b0e1212020-02-03 07:36:10 -070042 * @stopping: true if audio will stop once it runs out of data
Simon Glassb6f4c1c2020-02-03 07:36:12 -070043 * @texture: SDL texture to use for U-Boot display contents
44 * @renderer: SDL renderer to use
Simon Glass840a8682020-02-03 07:36:08 -070045 */
Simon Glass0d10f292014-02-27 13:26:17 -070046static struct sdl_info {
Simon Glass0d10f292014-02-27 13:26:17 -070047 int width;
48 int height;
49 int depth;
50 int pitch;
Simon Glass0d7e6812018-11-15 19:56:14 -070051 uint sample_rate;
Simon Glass0d10f292014-02-27 13:26:17 -070052 bool audio_active;
53 bool inited;
Simon Glass0d22f412018-12-10 10:37:35 -070054 int cur_buf;
55 struct buf_info buf[2];
Simon Glass3e610172018-12-10 10:37:45 -070056 bool running;
Simon Glass4b0e1212020-02-03 07:36:10 -070057 bool stopping;
Simon Glassb6f4c1c2020-02-03 07:36:12 -070058 SDL_Texture *texture;
59 SDL_Renderer *renderer;
Simon Glass0d10f292014-02-27 13:26:17 -070060} sdl;
61
62static void sandbox_sdl_poll_events(void)
63{
64 /*
65 * We don't want to include common.h in this file since it uses
66 * system headers. So add a declation here.
67 */
68 extern void reset_cpu(unsigned long addr);
69 SDL_Event event;
70
71 while (SDL_PollEvent(&event)) {
72 switch (event.type) {
73 case SDL_QUIT:
74 puts("LCD window closed - quitting\n");
75 reset_cpu(1);
76 break;
77 }
78 }
79}
80
81static int sandbox_sdl_ensure_init(void)
82{
83 if (!sdl.inited) {
84 if (SDL_Init(0) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -070085 printf("Unable to initialise SDL: %s\n",
Simon Glass0d10f292014-02-27 13:26:17 -070086 SDL_GetError());
87 return -EIO;
88 }
89
90 atexit(SDL_Quit);
91
92 sdl.inited = true;
93 }
94 return 0;
95}
96
97int sandbox_sdl_init_display(int width, int height, int log2_bpp)
98{
99 struct sandbox_state *state = state_get_current();
100 int err;
101
102 if (!width || !state->show_lcd)
103 return 0;
104 err = sandbox_sdl_ensure_init();
105 if (err)
106 return err;
107 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -0700108 printf("Unable to initialise SDL LCD: %s\n", SDL_GetError());
Simon Glass0d10f292014-02-27 13:26:17 -0700109 return -EPERM;
110 }
Simon Glass0d10f292014-02-27 13:26:17 -0700111 sdl.width = width;
112 sdl.height = height;
113 sdl.depth = 1 << log2_bpp;
114 sdl.pitch = sdl.width * sdl.depth / 8;
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700115 SDL_Window *screen = SDL_CreateWindow("U-Boot", SDL_WINDOWPOS_UNDEFINED,
116 SDL_WINDOWPOS_UNDEFINED,
117 sdl.width, sdl.height, 0);
118 if (!screen) {
119 printf("Unable to initialise SDL screen: %s\n",
120 SDL_GetError());
121 return -EIO;
122 }
123 if (log2_bpp != 4 && log2_bpp != 5) {
124 printf("U-Boot SDL does not support depth %d\n", log2_bpp);
125 return -EINVAL;
126 }
127 sdl.renderer = SDL_CreateRenderer(screen, -1,
128 SDL_RENDERER_ACCELERATED |
129 SDL_RENDERER_PRESENTVSYNC);
130 if (!sdl.renderer) {
131 printf("Unable to initialise SDL renderer: %s\n",
132 SDL_GetError());
133 return -EIO;
134 }
135
136 sdl.texture = SDL_CreateTexture(sdl.renderer, log2_bpp == 4 ?
137 SDL_PIXELFORMAT_RGB565 :
138 SDL_PIXELFORMAT_RGB888,
139 SDL_TEXTUREACCESS_STREAMING,
140 width, height);
141 if (!sdl.texture) {
142 printf("Unable to initialise SDL texture: %s\n",
143 SDL_GetError());
144 return -EBADF;
145 }
Simon Glass0d10f292014-02-27 13:26:17 -0700146 sandbox_sdl_poll_events();
147
148 return 0;
149}
150
151int sandbox_sdl_sync(void *lcd_base)
152{
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700153 SDL_UpdateTexture(sdl.texture, NULL, lcd_base, sdl.pitch);
154 SDL_RenderCopy(sdl.renderer, sdl.texture, NULL, NULL);
155 SDL_RenderPresent(sdl.renderer);
Simon Glass0d10f292014-02-27 13:26:17 -0700156 sandbox_sdl_poll_events();
157
158 return 0;
159}
160
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700161static const unsigned short sdl_to_keycode[SDL_NUM_SCANCODES] = {
162 [SDL_SCANCODE_A] = KEY_A,
163 [SDL_SCANCODE_B] = KEY_B,
164 [SDL_SCANCODE_C] = KEY_C,
165 [SDL_SCANCODE_D] = KEY_D,
166 [SDL_SCANCODE_E] = KEY_E,
167 [SDL_SCANCODE_F] = KEY_F,
168 [SDL_SCANCODE_G] = KEY_G,
169 [SDL_SCANCODE_H] = KEY_H,
170 [SDL_SCANCODE_I] = KEY_I,
171 [SDL_SCANCODE_J] = KEY_J,
172 [SDL_SCANCODE_K] = KEY_K,
173 [SDL_SCANCODE_L] = KEY_L,
174 [SDL_SCANCODE_M] = KEY_M,
175 [SDL_SCANCODE_N] = KEY_N,
176 [SDL_SCANCODE_O] = KEY_O,
177 [SDL_SCANCODE_P] = KEY_P,
178 [SDL_SCANCODE_Q] = KEY_Q,
179 [SDL_SCANCODE_R] = KEY_R,
180 [SDL_SCANCODE_S] = KEY_S,
181 [SDL_SCANCODE_T] = KEY_T,
182 [SDL_SCANCODE_U] = KEY_U,
183 [SDL_SCANCODE_V] = KEY_V,
184 [SDL_SCANCODE_W] = KEY_W,
185 [SDL_SCANCODE_X] = KEY_X,
186 [SDL_SCANCODE_Y] = KEY_Y,
187 [SDL_SCANCODE_Z] = KEY_Z,
Simon Glass0d10f292014-02-27 13:26:17 -0700188
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700189 [SDL_SCANCODE_1] = KEY_1,
190 [SDL_SCANCODE_2] = KEY_2,
191 [SDL_SCANCODE_3] = KEY_3,
192 [SDL_SCANCODE_4] = KEY_4,
193 [SDL_SCANCODE_5] = KEY_5,
194 [SDL_SCANCODE_6] = KEY_6,
195 [SDL_SCANCODE_7] = KEY_7,
196 [SDL_SCANCODE_8] = KEY_8,
197 [SDL_SCANCODE_9] = KEY_9,
198 [SDL_SCANCODE_0] = KEY_0,
Simon Glass0d10f292014-02-27 13:26:17 -0700199
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700200 [SDL_SCANCODE_RETURN] = KEY_ENTER,
201 [SDL_SCANCODE_ESCAPE] = KEY_ESC,
202 [SDL_SCANCODE_BACKSPACE] = KEY_BACKSPACE,
203 [SDL_SCANCODE_TAB] = KEY_TAB,
204 [SDL_SCANCODE_SPACE] = KEY_SPACE,
Simon Glass0d10f292014-02-27 13:26:17 -0700205
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700206 [SDL_SCANCODE_MINUS] = KEY_MINUS,
207 [SDL_SCANCODE_EQUALS] = KEY_EQUAL,
208 [SDL_SCANCODE_BACKSLASH] = KEY_BACKSLASH,
209 [SDL_SCANCODE_SEMICOLON] = KEY_SEMICOLON,
210 [SDL_SCANCODE_APOSTROPHE] = KEY_APOSTROPHE,
211 [SDL_SCANCODE_GRAVE] = KEY_GRAVE,
212 [SDL_SCANCODE_COMMA] = KEY_COMMA,
213 [SDL_SCANCODE_PERIOD] = KEY_DOT,
214 [SDL_SCANCODE_SLASH] = KEY_SLASH,
Simon Glass0d10f292014-02-27 13:26:17 -0700215
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700216 [SDL_SCANCODE_CAPSLOCK] = KEY_CAPSLOCK,
Simon Glass0d10f292014-02-27 13:26:17 -0700217
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700218 [SDL_SCANCODE_F1] = KEY_F1,
219 [SDL_SCANCODE_F2] = KEY_F2,
220 [SDL_SCANCODE_F3] = KEY_F3,
221 [SDL_SCANCODE_F4] = KEY_F4,
222 [SDL_SCANCODE_F5] = KEY_F5,
223 [SDL_SCANCODE_F6] = KEY_F6,
224 [SDL_SCANCODE_F7] = KEY_F7,
225 [SDL_SCANCODE_F8] = KEY_F8,
226 [SDL_SCANCODE_F9] = KEY_F9,
227 [SDL_SCANCODE_F10] = KEY_F10,
228 [SDL_SCANCODE_F11] = KEY_F11,
229 [SDL_SCANCODE_F12] = KEY_F12,
Simon Glass0d10f292014-02-27 13:26:17 -0700230
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700231 [SDL_SCANCODE_PRINTSCREEN] = KEY_PRINT,
232 [SDL_SCANCODE_SCROLLLOCK] = KEY_SCROLLLOCK,
233 [SDL_SCANCODE_PAUSE] = KEY_PAUSE,
234 [SDL_SCANCODE_INSERT] = KEY_INSERT,
235 [SDL_SCANCODE_HOME] = KEY_HOME,
236 [SDL_SCANCODE_PAGEUP] = KEY_PAGEUP,
237 [SDL_SCANCODE_DELETE] = KEY_DELETE,
238 [SDL_SCANCODE_END] = KEY_END,
239 [SDL_SCANCODE_PAGEDOWN] = KEY_PAGEDOWN,
240 [SDL_SCANCODE_RIGHT] = KEY_RIGHT,
241 [SDL_SCANCODE_LEFT] = KEY_LEFT,
242 [SDL_SCANCODE_DOWN] = KEY_DOWN,
243 [SDL_SCANCODE_UP] = KEY_UP,
Simon Glass0d10f292014-02-27 13:26:17 -0700244
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700245 [SDL_SCANCODE_NUMLOCKCLEAR] = KEY_NUMLOCK,
246 [SDL_SCANCODE_KP_DIVIDE] = KEY_KPSLASH,
247 [SDL_SCANCODE_KP_MULTIPLY] = KEY_KPASTERISK,
248 [SDL_SCANCODE_KP_MINUS] = KEY_KPMINUS,
249 [SDL_SCANCODE_KP_PLUS] = KEY_KPPLUS,
250 [SDL_SCANCODE_KP_ENTER] = KEY_KPENTER,
251 [SDL_SCANCODE_KP_1] = KEY_KP1,
252 [SDL_SCANCODE_KP_2] = KEY_KP2,
253 [SDL_SCANCODE_KP_3] = KEY_KP3,
254 [SDL_SCANCODE_KP_4] = KEY_KP4,
255 [SDL_SCANCODE_KP_5] = KEY_KP5,
256 [SDL_SCANCODE_KP_6] = KEY_KP6,
257 [SDL_SCANCODE_KP_7] = KEY_KP7,
258 [SDL_SCANCODE_KP_8] = KEY_KP8,
259 [SDL_SCANCODE_KP_9] = KEY_KP9,
260 [SDL_SCANCODE_KP_0] = KEY_KP0,
261 [SDL_SCANCODE_KP_PERIOD] = KEY_KPDOT,
Simon Glass0d10f292014-02-27 13:26:17 -0700262
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700263 [SDL_SCANCODE_KP_EQUALS] = KEY_KPEQUAL,
264 [SDL_SCANCODE_KP_COMMA] = KEY_KPCOMMA,
Simon Glass0d10f292014-02-27 13:26:17 -0700265
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700266 [SDL_SCANCODE_SYSREQ] = KEY_SYSRQ,
Simon Glass0d10f292014-02-27 13:26:17 -0700267};
268
269int sandbox_sdl_scan_keys(int key[], int max_keys)
270{
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700271 const Uint8 *keystate;
272 int num_keys;
Simon Glass0d10f292014-02-27 13:26:17 -0700273 int i, count;
274
275 sandbox_sdl_poll_events();
Simon Glassb6f4c1c2020-02-03 07:36:12 -0700276 keystate = SDL_GetKeyboardState(&num_keys);
277 for (i = count = 0; i < num_keys; i++) {
278 if (count < max_keys && keystate[i]) {
279 int keycode = sdl_to_keycode[i];
280
281 if (keycode)
282 key[count++] = keycode;
283 }
Simon Glass0d10f292014-02-27 13:26:17 -0700284 }
285
286 return count;
287}
288
289int sandbox_sdl_key_pressed(int keycode)
290{
291 int key[8]; /* allow up to 8 keys to be pressed at once */
292 int count;
293 int i;
294
295 count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
296 for (i = 0; i < count; i++) {
297 if (key[i] == keycode)
298 return 0;
299 }
300
301 return -ENOENT;
302}
303
304void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
305{
Simon Glass0d22f412018-12-10 10:37:35 -0700306 struct buf_info *buf;
Simon Glass0d10f292014-02-27 13:26:17 -0700307 int avail;
Simon Glass4b0e1212020-02-03 07:36:10 -0700308 bool have_data = false;
Simon Glass0d22f412018-12-10 10:37:35 -0700309 int i;
Simon Glass0d10f292014-02-27 13:26:17 -0700310
Simon Glass0d22f412018-12-10 10:37:35 -0700311 for (i = 0; i < 2; i++) {
312 buf = &sdl.buf[sdl.cur_buf];
313 avail = buf->size - buf->pos;
314 if (avail <= 0) {
315 sdl.cur_buf = 1 - sdl.cur_buf;
316 continue;
317 }
318 if (avail > len)
319 avail = len;
Simon Glass4b0e1212020-02-03 07:36:10 -0700320 have_data = true;
Simon Glass0d10f292014-02-27 13:26:17 -0700321
Simon Glass0d22f412018-12-10 10:37:35 -0700322 SDL_MixAudio(stream, buf->data + buf->pos, avail,
323 SDL_MIX_MAXVOLUME);
324 buf->pos += avail;
325 len -= avail;
Simon Glass0d10f292014-02-27 13:26:17 -0700326
Simon Glass0d22f412018-12-10 10:37:35 -0700327 /* Move to next buffer if we are at the end */
328 if (buf->pos == buf->size)
329 buf->size = 0;
330 else
331 break;
332 }
Simon Glass4b0e1212020-02-03 07:36:10 -0700333 sdl.stopping = !have_data;
Simon Glass0d10f292014-02-27 13:26:17 -0700334}
335
Simon Glass658a8742018-12-10 10:37:50 -0700336int sandbox_sdl_sound_init(int rate, int channels)
Simon Glass0d10f292014-02-27 13:26:17 -0700337{
Simon Glassf0e2f422020-02-03 07:36:09 -0700338 SDL_AudioSpec wanted, have;
Simon Glass0d22f412018-12-10 10:37:35 -0700339 int i;
Simon Glass0d10f292014-02-27 13:26:17 -0700340
341 if (sandbox_sdl_ensure_init())
342 return -1;
343
344 if (sdl.audio_active)
345 return 0;
346
Simon Glass0d10f292014-02-27 13:26:17 -0700347 /* Set the audio format */
Simon Glass658a8742018-12-10 10:37:50 -0700348 wanted.freq = rate;
Simon Glass0d10f292014-02-27 13:26:17 -0700349 wanted.format = AUDIO_S16;
Simon Glass658a8742018-12-10 10:37:50 -0700350 wanted.channels = channels;
Simon Glass0d10f292014-02-27 13:26:17 -0700351 wanted.samples = 1024; /* Good low-latency value for callback */
352 wanted.callback = sandbox_sdl_fill_audio;
353 wanted.userdata = NULL;
354
Simon Glass0d22f412018-12-10 10:37:35 -0700355 for (i = 0; i < 2; i++) {
356 struct buf_info *buf = &sdl.buf[i];
357
358 buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels;
359 buf->data = malloc(buf->alloced);
360 if (!buf->data) {
361 printf("%s: Out of memory\n", __func__);
362 if (i == 1)
363 free(sdl.buf[0].data);
364 return -1;
365 }
366 buf->pos = 0;
367 buf->size = 0;
Simon Glass0d10f292014-02-27 13:26:17 -0700368 }
Simon Glass0d10f292014-02-27 13:26:17 -0700369
370 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
Simon Glassf0e2f422020-02-03 07:36:09 -0700371 printf("Unable to initialise SDL audio: %s\n", SDL_GetError());
Simon Glass0d10f292014-02-27 13:26:17 -0700372 goto err;
373 }
374
375 /* Open the audio device, forcing the desired format */
Simon Glassf0e2f422020-02-03 07:36:09 -0700376 if (SDL_OpenAudio(&wanted, &have) < 0) {
Simon Glass0d10f292014-02-27 13:26:17 -0700377 printf("Couldn't open audio: %s\n", SDL_GetError());
378 goto err;
379 }
Simon Glassf0e2f422020-02-03 07:36:09 -0700380 if (have.format != wanted.format) {
381 printf("Couldn't select required audio format\n");
382 goto err;
383 }
Simon Glass0d10f292014-02-27 13:26:17 -0700384 sdl.audio_active = true;
Simon Glass0d7e6812018-11-15 19:56:14 -0700385 sdl.sample_rate = wanted.freq;
Simon Glass0d22f412018-12-10 10:37:35 -0700386 sdl.cur_buf = 0;
Simon Glass4b0e1212020-02-03 07:36:10 -0700387 sdl.running = false;
Simon Glass0d10f292014-02-27 13:26:17 -0700388
389 return 0;
390
391err:
Simon Glass0d22f412018-12-10 10:37:35 -0700392 for (i = 0; i < 2; i++)
393 free(sdl.buf[i].data);
Simon Glass0d10f292014-02-27 13:26:17 -0700394 return -1;
395}
396
Simon Glass3e610172018-12-10 10:37:45 -0700397int sandbox_sdl_sound_play(const void *data, uint size)
Simon Glass0d10f292014-02-27 13:26:17 -0700398{
Simon Glass3e610172018-12-10 10:37:45 -0700399 struct buf_info *buf;
Simon Glass0d22f412018-12-10 10:37:35 -0700400
Simon Glass0d10f292014-02-27 13:26:17 -0700401 if (!sdl.audio_active)
Simon Glass3e610172018-12-10 10:37:45 -0700402 return 0;
403
404 buf = &sdl.buf[0];
405 if (buf->size)
406 buf = &sdl.buf[1];
407 while (buf->size)
408 usleep(1000);
409
410 if (size > buf->alloced)
411 return -E2BIG;
412
413 memcpy(buf->data, data, size);
414 buf->size = size;
Simon Glass0d22f412018-12-10 10:37:35 -0700415 buf->pos = 0;
Simon Glass3e610172018-12-10 10:37:45 -0700416 if (!sdl.running) {
417 SDL_PauseAudio(0);
Simon Glass4b0e1212020-02-03 07:36:10 -0700418 sdl.running = true;
419 sdl.stopping = false;
Simon Glass3e610172018-12-10 10:37:45 -0700420 }
Simon Glass0d10f292014-02-27 13:26:17 -0700421
422 return 0;
423}
424
425int sandbox_sdl_sound_stop(void)
426{
Simon Glass3e610172018-12-10 10:37:45 -0700427 if (sdl.running) {
Simon Glass4b0e1212020-02-03 07:36:10 -0700428 while (!sdl.stopping)
429 SDL_Delay(100);
430
Simon Glass3e610172018-12-10 10:37:45 -0700431 SDL_PauseAudio(1);
432 sdl.running = 0;
Simon Glass4b0e1212020-02-03 07:36:10 -0700433 sdl.stopping = false;
Simon Glass3e610172018-12-10 10:37:45 -0700434 }
Simon Glass0d10f292014-02-27 13:26:17 -0700435
436 return 0;
437}