1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2013 Google, Inc
4  */
5 
6 #include <errno.h>
7 #include <unistd.h>
8 #include <stdbool.h>
9 #include <linux/input.h>
10 #include <SDL2/SDL.h>
11 #include <asm/state.h>
12 
13 /**
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  */
21 struct buf_info {
22 	uint pos;
23 	uint size;
24 	uint alloced;
25 	uint8_t *data;
26 };
27 
28 /**
29  * struct sdl_info - Information about our use of the SDL library
30  *
31  * @width: Width of simulated LCD display
32  * @height: Height of simulated LCD display
33  * @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)
35  * @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
44  * @stopping: true if audio will stop once it runs out of data
45  * @texture: SDL texture to use for U-Boot display contents
46  * @renderer: SDL renderer to use
47  */
48 static struct sdl_info {
49 	int width;
50 	int height;
51 	int vis_width;
52 	int vis_height;
53 	int depth;
54 	int pitch;
55 	uint sample_rate;
56 	bool audio_active;
57 	bool inited;
58 	int cur_buf;
59 	struct buf_info buf[2];
60 	bool running;
61 	bool stopping;
62 	SDL_Texture *texture;
63 	SDL_Renderer *renderer;
64 } sdl;
65 
sandbox_sdl_poll_events(void)66 static 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 	 */
72 	extern void reset_cpu(void);
73 	SDL_Event event;
74 
75 	while (SDL_PollEvent(&event)) {
76 		switch (event.type) {
77 		case SDL_QUIT:
78 			puts("LCD window closed - quitting\n");
79 			reset_cpu();
80 			break;
81 		}
82 	}
83 }
84 
sandbox_sdl_ensure_init(void)85 static int sandbox_sdl_ensure_init(void)
86 {
87 	if (!sdl.inited) {
88 		if (SDL_Init(0) < 0) {
89 			printf("Unable to initialise SDL: %s\n",
90 			       SDL_GetError());
91 			return -EIO;
92 		}
93 
94 		atexit(SDL_Quit);
95 
96 		sdl.inited = true;
97 	}
98 	return 0;
99 }
100 
sandbox_sdl_init_display(int width,int height,int log2_bpp,bool double_size)101 int sandbox_sdl_init_display(int width, int height, int log2_bpp,
102 			     bool double_size)
103 {
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) {
113 		printf("Unable to initialise SDL LCD: %s\n", SDL_GetError());
114 		return -EPERM;
115 	}
116 	sdl.width = width;
117 	sdl.height = height;
118 	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 
126 	sdl.depth = 1 << log2_bpp;
127 	sdl.pitch = sdl.width * sdl.depth / 8;
128 	SDL_Window *screen = SDL_CreateWindow("U-Boot", SDL_WINDOWPOS_UNDEFINED,
129 					      SDL_WINDOWPOS_UNDEFINED,
130 					      sdl.vis_width, sdl.vis_height,
131 					      SDL_WINDOW_RESIZABLE);
132 	if (!screen) {
133 		printf("Unable to initialise SDL screen: %s\n",
134 		       SDL_GetError());
135 		return -EIO;
136 	}
137 	if (log2_bpp != 4 && log2_bpp != 5) {
138 		printf("U-Boot SDL does not support depth %d\n", log2_bpp);
139 		return -EINVAL;
140 	}
141 	sdl.renderer = SDL_CreateRenderer(screen, -1,
142 					  SDL_RENDERER_ACCELERATED |
143 					  SDL_RENDERER_PRESENTVSYNC);
144 	if (!sdl.renderer) {
145 		printf("Unable to initialise SDL renderer: %s\n",
146 		       SDL_GetError());
147 		return -EIO;
148 	}
149 
150 	sdl.texture = SDL_CreateTexture(sdl.renderer, log2_bpp == 4 ?
151 					SDL_PIXELFORMAT_RGB565 :
152 					SDL_PIXELFORMAT_RGB888,
153 					SDL_TEXTUREACCESS_STREAMING,
154 					width, height);
155 	if (!sdl.texture) {
156 		printf("Unable to initialise SDL texture: %s\n",
157 		       SDL_GetError());
158 		return -EBADF;
159 	}
160 	sandbox_sdl_poll_events();
161 
162 	return 0;
163 }
164 
sandbox_sdl_sync(void * lcd_base)165 int sandbox_sdl_sync(void *lcd_base)
166 {
167 	SDL_UpdateTexture(sdl.texture, NULL, lcd_base, sdl.pitch);
168 	SDL_RenderCopy(sdl.renderer, sdl.texture, NULL, NULL);
169 	SDL_RenderPresent(sdl.renderer);
170 	sandbox_sdl_poll_events();
171 
172 	return 0;
173 }
174 
175 static const unsigned short sdl_to_keycode[SDL_NUM_SCANCODES] = {
176 	[SDL_SCANCODE_ESCAPE]	= KEY_ESC,
177 	[SDL_SCANCODE_1]	= KEY_1,
178 	[SDL_SCANCODE_2]	= KEY_2,
179 	[SDL_SCANCODE_3]	= KEY_3,
180 	[SDL_SCANCODE_4]	= KEY_4,
181 	[SDL_SCANCODE_5]	= KEY_5,
182 	[SDL_SCANCODE_6]	= KEY_6,
183 	[SDL_SCANCODE_7]	= KEY_7,
184 	[SDL_SCANCODE_8]	= KEY_8,
185 	[SDL_SCANCODE_9]	= KEY_9,
186 	[SDL_SCANCODE_0]	= KEY_0,
187 	[SDL_SCANCODE_MINUS]	= KEY_MINUS,
188 	[SDL_SCANCODE_EQUALS]	= KEY_EQUAL,
189 	[SDL_SCANCODE_BACKSPACE]	= KEY_BACKSPACE,
190 	[SDL_SCANCODE_TAB]	= KEY_TAB,
191 	[SDL_SCANCODE_Q]	= KEY_Q,
192 	[SDL_SCANCODE_W]	= KEY_W,
193 	[SDL_SCANCODE_E]	= KEY_E,
194 	[SDL_SCANCODE_R]	= KEY_R,
195 	[SDL_SCANCODE_T]	= KEY_T,
196 	[SDL_SCANCODE_Y]	= KEY_Y,
197 	[SDL_SCANCODE_U]	= KEY_U,
198 	[SDL_SCANCODE_I]	= KEY_I,
199 	[SDL_SCANCODE_O]	= KEY_O,
200 	[SDL_SCANCODE_P]	= KEY_P,
201 	[SDL_SCANCODE_LEFTBRACKET]	= KEY_LEFTBRACE,
202 	[SDL_SCANCODE_RIGHTBRACKET]	= KEY_RIGHTBRACE,
203 	[SDL_SCANCODE_RETURN]	= KEY_ENTER,
204 	[SDL_SCANCODE_LCTRL]	= KEY_LEFTCTRL,
205 	[SDL_SCANCODE_A]	= KEY_A,
206 	[SDL_SCANCODE_S]	= KEY_S,
207 	[SDL_SCANCODE_D]	= KEY_D,
208 	[SDL_SCANCODE_F]	= KEY_F,
209 	[SDL_SCANCODE_G]	= KEY_G,
210 	[SDL_SCANCODE_H]	= KEY_H,
211 	[SDL_SCANCODE_J]	= KEY_J,
212 	[SDL_SCANCODE_K]	= KEY_K,
213 	[SDL_SCANCODE_L]	= KEY_L,
214 	[SDL_SCANCODE_SEMICOLON]	= KEY_SEMICOLON,
215 	[SDL_SCANCODE_APOSTROPHE]	= KEY_APOSTROPHE,
216 	[SDL_SCANCODE_GRAVE]	= KEY_GRAVE,
217 	[SDL_SCANCODE_LSHIFT]	= KEY_LEFTSHIFT,
218 	[SDL_SCANCODE_BACKSLASH]	= KEY_BACKSLASH,
219 	[SDL_SCANCODE_Z]	= KEY_Z,
220 	[SDL_SCANCODE_X]	= KEY_X,
221 	[SDL_SCANCODE_C]	= KEY_C,
222 	[SDL_SCANCODE_V]	= KEY_V,
223 	[SDL_SCANCODE_B]	= KEY_B,
224 	[SDL_SCANCODE_N]	= KEY_N,
225 	[SDL_SCANCODE_M]	= KEY_M,
226 	[SDL_SCANCODE_COMMA]	= KEY_COMMA,
227 	[SDL_SCANCODE_PERIOD]	= KEY_DOT,
228 	[SDL_SCANCODE_SLASH]	= KEY_SLASH,
229 	[SDL_SCANCODE_RSHIFT]	= KEY_RIGHTSHIFT,
230 	[SDL_SCANCODE_KP_MULTIPLY] = KEY_KPASTERISK,
231 	[SDL_SCANCODE_LALT]	= KEY_LEFTALT,
232 	[SDL_SCANCODE_SPACE]	= KEY_SPACE,
233 	[SDL_SCANCODE_CAPSLOCK]	= KEY_CAPSLOCK,
234 	[SDL_SCANCODE_F1]	= KEY_F1,
235 	[SDL_SCANCODE_F2]	= KEY_F2,
236 	[SDL_SCANCODE_F3]	= KEY_F3,
237 	[SDL_SCANCODE_F4]	= KEY_F4,
238 	[SDL_SCANCODE_F5]	= KEY_F5,
239 	[SDL_SCANCODE_F6]	= KEY_F6,
240 	[SDL_SCANCODE_F7]	= KEY_F7,
241 	[SDL_SCANCODE_F8]	= KEY_F8,
242 	[SDL_SCANCODE_F9]	= KEY_F9,
243 	[SDL_SCANCODE_F10]	= KEY_F10,
244 	[SDL_SCANCODE_NUMLOCKCLEAR]	= KEY_NUMLOCK,
245 	[SDL_SCANCODE_SCROLLLOCK]	= KEY_SCROLLLOCK,
246 	[SDL_SCANCODE_KP_7]	= KEY_KP7,
247 	[SDL_SCANCODE_KP_8]	= KEY_KP8,
248 	[SDL_SCANCODE_KP_9]	= KEY_KP9,
249 	[SDL_SCANCODE_KP_MINUS]	= KEY_KPMINUS,
250 	[SDL_SCANCODE_KP_4]	= KEY_KP4,
251 	[SDL_SCANCODE_KP_5]	= KEY_KP5,
252 	[SDL_SCANCODE_KP_6]	= KEY_KP6,
253 	[SDL_SCANCODE_KP_PLUS]	= KEY_KPPLUS,
254 	[SDL_SCANCODE_KP_1]	= KEY_KP1,
255 	[SDL_SCANCODE_KP_2]	= KEY_KP2,
256 	[SDL_SCANCODE_KP_3]	= KEY_KP3,
257 	[SDL_SCANCODE_KP_0]	= KEY_KP0,
258 	[SDL_SCANCODE_KP_PERIOD]	= KEY_KPDOT,
259 	/* key 84 does not exist linux_input.h */
260 	[SDL_SCANCODE_LANG5]	=  KEY_ZENKAKUHANKAKU,
261 	[SDL_SCANCODE_NONUSBACKSLASH]	= KEY_102ND,
262 	[SDL_SCANCODE_F11]	= KEY_F11,
263 	[SDL_SCANCODE_F12]	= KEY_F12,
264 	[SDL_SCANCODE_INTERNATIONAL1]	= KEY_RO,
265 	[SDL_SCANCODE_LANG3]	= KEY_KATAKANA,
266 	[SDL_SCANCODE_LANG4]	= KEY_HIRAGANA,
267 	[SDL_SCANCODE_INTERNATIONAL4] = KEY_HENKAN,
268 	[SDL_SCANCODE_INTERNATIONAL2] = KEY_KATAKANAHIRAGANA,
269 	[SDL_SCANCODE_INTERNATIONAL5] = KEY_MUHENKAN,
270 	/* [SDL_SCANCODE_INTERNATIONAL5] -> [KEY_KPJPCOMMA] */
271 	[SDL_SCANCODE_KP_ENTER]	= KEY_KPENTER,
272 	[SDL_SCANCODE_RCTRL]	= KEY_RIGHTCTRL,
273 	[SDL_SCANCODE_KP_DIVIDE] = KEY_KPSLASH,
274 	[SDL_SCANCODE_SYSREQ]	= KEY_SYSRQ,
275 	[SDL_SCANCODE_RALT]	= KEY_RIGHTALT,
276 	/* KEY_LINEFEED */
277 	[SDL_SCANCODE_HOME]	= KEY_HOME,
278 	[SDL_SCANCODE_UP]	= KEY_UP,
279 	[SDL_SCANCODE_PAGEUP]	= KEY_PAGEUP,
280 	[SDL_SCANCODE_LEFT]	= KEY_LEFT,
281 	[SDL_SCANCODE_RIGHT]	= KEY_RIGHT,
282 	[SDL_SCANCODE_END]	= KEY_END,
283 	[SDL_SCANCODE_DOWN]	= KEY_DOWN,
284 	[SDL_SCANCODE_PAGEDOWN]	= KEY_PAGEDOWN,
285 	[SDL_SCANCODE_INSERT]	= KEY_INSERT,
286 	[SDL_SCANCODE_DELETE]	= KEY_DELETE,
287 	/* KEY_MACRO */
288 	[SDL_SCANCODE_MUTE]	= KEY_MUTE,
289 	[SDL_SCANCODE_VOLUMEDOWN]	= KEY_VOLUMEDOWN,
290 	[SDL_SCANCODE_VOLUMEUP]	= KEY_VOLUMEUP,
291 	[SDL_SCANCODE_POWER]	= KEY_POWER,
292 	[SDL_SCANCODE_KP_EQUALS]	= KEY_KPEQUAL,
293 	[SDL_SCANCODE_KP_PLUSMINUS]	= KEY_KPPLUSMINUS,
294 	[SDL_SCANCODE_PAUSE]	= KEY_PAUSE,
295 	/* KEY_SCALE */
296 	[SDL_SCANCODE_KP_COMMA] = KEY_KPCOMMA,
297 	[SDL_SCANCODE_LANG1]	= KEY_HANGUEL,
298 	[SDL_SCANCODE_LANG2]	= KEY_HANJA,
299 	[SDL_SCANCODE_INTERNATIONAL3]	= KEY_YEN,
300 	[SDL_SCANCODE_LGUI]	= KEY_LEFTMETA,
301 	[SDL_SCANCODE_RGUI]	= KEY_RIGHTMETA,
302 	[SDL_SCANCODE_APPLICATION] = KEY_COMPOSE,
303 };
304 
sandbox_sdl_scan_keys(int key[],int max_keys)305 int sandbox_sdl_scan_keys(int key[], int max_keys)
306 {
307 	const Uint8 *keystate;
308 	int num_keys;
309 	int i, count;
310 
311 	sandbox_sdl_poll_events();
312 	keystate = SDL_GetKeyboardState(&num_keys);
313 	for (i = count = 0; i < num_keys; i++) {
314 		if (count < max_keys && keystate[i]) {
315 			int keycode = sdl_to_keycode[i];
316 
317 			if (keycode)
318 				key[count++] = keycode;
319 		}
320 	}
321 
322 	return count;
323 }
324 
sandbox_sdl_key_pressed(int keycode)325 int sandbox_sdl_key_pressed(int keycode)
326 {
327 	int key[8];	/* allow up to 8 keys to be pressed at once */
328 	int count;
329 	int i;
330 
331 	count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
332 	for (i = 0; i < count; i++) {
333 		if (key[i] == keycode)
334 			return 0;
335 	}
336 
337 	return -ENOENT;
338 }
339 
sandbox_sdl_fill_audio(void * udata,Uint8 * stream,int len)340 void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
341 {
342 	struct buf_info *buf;
343 	int avail;
344 	bool have_data = false;
345 	int i;
346 
347 	for (i = 0; i < 2; i++) {
348 		buf = &sdl.buf[sdl.cur_buf];
349 		avail = buf->size - buf->pos;
350 		if (avail <= 0) {
351 			sdl.cur_buf = 1 - sdl.cur_buf;
352 			continue;
353 		}
354 		if (avail > len)
355 			avail = len;
356 		have_data = true;
357 
358 		SDL_MixAudio(stream, buf->data + buf->pos, avail,
359 			     SDL_MIX_MAXVOLUME);
360 		buf->pos += avail;
361 		len -= avail;
362 
363 		/* Move to next buffer if we are at the end */
364 		if (buf->pos == buf->size)
365 			buf->size = 0;
366 		else
367 			break;
368 	}
369 	sdl.stopping = !have_data;
370 }
371 
sandbox_sdl_sound_init(int rate,int channels)372 int sandbox_sdl_sound_init(int rate, int channels)
373 {
374 	SDL_AudioSpec wanted, have;
375 	int i;
376 
377 	if (sandbox_sdl_ensure_init())
378 		return -1;
379 
380 	if (sdl.audio_active)
381 		return 0;
382 
383 	/* Set the audio format */
384 	wanted.freq = rate;
385 	wanted.format = AUDIO_S16;
386 	wanted.channels = channels;
387 	wanted.samples = 1024;  /* Good low-latency value for callback */
388 	wanted.callback = sandbox_sdl_fill_audio;
389 	wanted.userdata = NULL;
390 
391 	for (i = 0; i < 2; i++) {
392 		struct buf_info *buf = &sdl.buf[i];
393 
394 		buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels;
395 		buf->data = malloc(buf->alloced);
396 		if (!buf->data) {
397 			printf("%s: Out of memory\n", __func__);
398 			if (i == 1)
399 				free(sdl.buf[0].data);
400 			return -1;
401 		}
402 		buf->pos = 0;
403 		buf->size = 0;
404 	}
405 
406 	if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
407 		printf("Unable to initialise SDL audio: %s\n", SDL_GetError());
408 		goto err;
409 	}
410 
411 	/* Open the audio device, forcing the desired format */
412 	if (SDL_OpenAudio(&wanted, &have) < 0) {
413 		printf("Couldn't open audio: %s\n", SDL_GetError());
414 		goto err;
415 	}
416 	if (have.format != wanted.format) {
417 		printf("Couldn't select required audio format\n");
418 		goto err;
419 	}
420 	sdl.audio_active = true;
421 	sdl.sample_rate = wanted.freq;
422 	sdl.cur_buf = 0;
423 	sdl.running = false;
424 
425 	return 0;
426 
427 err:
428 	for (i = 0; i < 2; i++)
429 		free(sdl.buf[i].data);
430 	return -1;
431 }
432 
sandbox_sdl_sound_play(const void * data,uint size)433 int sandbox_sdl_sound_play(const void *data, uint size)
434 {
435 	struct buf_info *buf;
436 
437 	if (!sdl.audio_active)
438 		return 0;
439 
440 	buf = &sdl.buf[0];
441 	if (buf->size)
442 		buf = &sdl.buf[1];
443 	while (buf->size)
444 		usleep(1000);
445 
446 	if (size > buf->alloced)
447 		return -E2BIG;
448 
449 	memcpy(buf->data, data, size);
450 	buf->size = size;
451 	buf->pos = 0;
452 	if (!sdl.running) {
453 		SDL_PauseAudio(0);
454 		sdl.running = true;
455 		sdl.stopping = false;
456 	}
457 
458 	return 0;
459 }
460 
sandbox_sdl_sound_stop(void)461 int sandbox_sdl_sound_stop(void)
462 {
463 	if (sdl.running) {
464 		while (!sdl.stopping)
465 			SDL_Delay(100);
466 
467 		SDL_PauseAudio(1);
468 		sdl.running = 0;
469 		sdl.stopping = false;
470 	}
471 
472 	return 0;
473 }
474