2 * Copyright (c) 2013 Google, Inc
4 * SPDX-License-Identifier: GPL-2.0+
8 #include <linux/input.h>
11 #include <asm/state.h>
13 static struct sdl_info {
27 static void sandbox_sdl_poll_events(void)
30 * We don't want to include common.h in this file since it uses
31 * system headers. So add a declation here.
33 extern void reset_cpu(unsigned long addr);
36 while (SDL_PollEvent(&event)) {
39 puts("LCD window closed - quitting\n");
46 static int sandbox_sdl_ensure_init(void)
49 if (SDL_Init(0) < 0) {
50 printf("Unable to initialize SDL: %s\n",
62 int sandbox_sdl_init_display(int width, int height, int log2_bpp)
64 struct sandbox_state *state = state_get_current();
67 if (!width || !state->show_lcd)
69 err = sandbox_sdl_ensure_init();
72 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
73 printf("Unable to initialize SDL LCD: %s\n", SDL_GetError());
76 SDL_WM_SetCaption("U-Boot", "U-Boot");
80 sdl.depth = 1 << log2_bpp;
81 sdl.pitch = sdl.width * sdl.depth / 8;
82 sdl.screen = SDL_SetVideoMode(width, height, 0, 0);
83 sandbox_sdl_poll_events();
88 int sandbox_sdl_sync(void *lcd_base)
92 frame = SDL_CreateRGBSurfaceFrom(lcd_base, sdl.width, sdl.height,
94 0x1f << 11, 0x3f << 5, 0x1f << 0, 0);
95 SDL_BlitSurface(frame, NULL, sdl.screen, NULL);
96 SDL_FreeSurface(frame);
97 SDL_UpdateRect(sdl.screen, 0, 0, 0, 0);
98 sandbox_sdl_poll_events();
104 #define NUM_SDL_CODES (SDLK_UNDO + 1)
106 static int16_t sdl_to_keycode[NUM_SDL_CODES] = {
108 NONE, NONE, NONE, NONE, NONE,
109 NONE, NONE, NONE, KEY_BACKSPACE, KEY_TAB,
110 NONE, NONE, NONE, KEY_ENTER, NONE,
111 NONE, NONE, NONE, NONE, KEY_POWER, /* use PAUSE as POWER */
114 NONE, NONE, NONE, NONE, NONE,
115 NONE, NONE, KEY_ESC, NONE, NONE,
116 NONE, NONE, KEY_SPACE, NONE, NONE,
117 NONE, NONE, NONE, NONE, NONE,
120 NONE, NONE, NONE, NONE, KEY_COMMA,
121 KEY_MINUS, KEY_DOT, KEY_SLASH, KEY_0, KEY_1,
122 KEY_2, KEY_3, KEY_4, KEY_5, KEY_6,
123 KEY_7, KEY_8, KEY_9, NONE, KEY_SEMICOLON,
126 NONE, KEY_EQUAL, NONE, NONE, NONE,
127 NONE, NONE, NONE, NONE, NONE,
128 NONE, NONE, NONE, NONE, NONE,
129 NONE, NONE, NONE, NONE, NONE,
132 NONE, NONE, NONE, NONE, NONE,
133 NONE, NONE, NONE, NONE, NONE,
134 NONE, NONE, KEY_BACKSLASH, NONE, NONE,
135 NONE, KEY_GRAVE, KEY_A, KEY_B, KEY_C,
138 KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
139 KEY_I, KEY_J, KEY_K, KEY_L, KEY_M,
140 KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
141 KEY_S, KEY_T, KEY_U, KEY_V, KEY_W,
144 KEY_X, KEY_Y, KEY_Z, NONE, NONE,
145 NONE, NONE, KEY_DELETE, NONE, NONE,
146 NONE, NONE, NONE, NONE, NONE,
147 NONE, NONE, NONE, NONE, NONE,
150 NONE, NONE, NONE, NONE, NONE,
151 NONE, NONE, NONE, NONE, NONE,
152 NONE, NONE, NONE, NONE, NONE,
153 NONE, NONE, NONE, NONE, NONE,
156 NONE, NONE, NONE, NONE, NONE,
157 NONE, NONE, NONE, NONE, NONE,
158 NONE, NONE, NONE, NONE, NONE,
159 NONE, NONE, NONE, NONE, NONE,
162 NONE, NONE, NONE, NONE, NONE,
163 NONE, NONE, NONE, NONE, NONE,
164 NONE, NONE, NONE, NONE, NONE,
165 NONE, NONE, NONE, NONE, NONE,
168 NONE, NONE, NONE, NONE, NONE,
169 NONE, NONE, NONE, NONE, NONE,
170 NONE, NONE, NONE, NONE, NONE,
171 NONE, NONE, NONE, NONE, NONE,
174 NONE, NONE, NONE, NONE, NONE,
175 NONE, NONE, NONE, NONE, NONE,
176 NONE, NONE, NONE, NONE, NONE,
177 NONE, NONE, NONE, NONE, NONE,
180 NONE, NONE, NONE, NONE, NONE,
181 NONE, NONE, NONE, NONE, NONE,
182 NONE, NONE, NONE, NONE, NONE,
183 NONE, KEY_KP0, KEY_KP1, KEY_KP2, KEY_KP3,
186 KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8,
187 KEY_KP9, KEY_KPDOT, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS,
188 KEY_KPPLUS, KEY_KPENTER, KEY_KPEQUAL, KEY_UP, KEY_DOWN,
189 KEY_RIGHT, KEY_LEFT, KEY_INSERT, KEY_HOME, KEY_END,
192 KEY_PAGEUP, KEY_PAGEDOWN, KEY_F1, KEY_F2, KEY_F3,
193 KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8,
194 KEY_F9, KEY_F10, KEY_F11, KEY_F12, NONE,
195 NONE, NONE, NONE, NONE, NONE,
198 KEY_NUMLOCK, KEY_CAPSLOCK, KEY_SCROLLLOCK, KEY_RIGHTSHIFT,
200 KEY_RIGHTCTRL, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTALT, KEY_RIGHTMETA,
201 KEY_LEFTMETA, NONE, KEY_FN, NONE, KEY_COMPOSE,
202 NONE, KEY_PRINT, KEY_SYSRQ, KEY_PAUSE, NONE,
208 int sandbox_sdl_scan_keys(int key[], int max_keys)
213 sandbox_sdl_poll_events();
214 keystate = SDL_GetKeyState(NULL);
215 for (i = count = 0; i < NUM_SDL_CODES; i++) {
216 if (count >= max_keys)
218 else if (keystate[i])
219 key[count++] = sdl_to_keycode[i];
225 int sandbox_sdl_key_pressed(int keycode)
227 int key[8]; /* allow up to 8 keys to be pressed at once */
231 count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
232 for (i = 0; i < count; i++) {
233 if (key[i] == keycode)
240 void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
244 avail = sdl.audio_size - sdl.audio_pos;
248 SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
250 sdl.audio_pos += len;
252 /* Loop if we are at the end */
253 if (sdl.audio_pos == sdl.audio_size)
257 int sandbox_sdl_sound_init(void)
259 SDL_AudioSpec wanted;
261 if (sandbox_sdl_ensure_init())
264 if (sdl.audio_active)
268 * At present all sandbox sounds crash. This is probably due to
269 * symbol name conflicts with U-Boot. We can remove the malloc()
272 * #define USE_DL_PREFIX
276 * Assertion 'e->pollfd->fd == e->fd' failed at pulse/mainloop.c:676,
277 * function dispatch_pollfds(). Aborting.
279 * The right solution is probably to make U-Boot's names private or
280 * link os.c and sdl.c against their libraries before liking with
281 * U-Boot. TBD. For now sound is disabled.
283 printf("(Warning: sandbox sound disabled)\n");
286 /* Set the audio format */
288 wanted.format = AUDIO_S16;
289 wanted.channels = 1; /* 1 = mono, 2 = stereo */
290 wanted.samples = 1024; /* Good low-latency value for callback */
291 wanted.callback = sandbox_sdl_fill_audio;
292 wanted.userdata = NULL;
294 sdl.audio_size = sizeof(uint16_t) * wanted.freq;
295 sdl.audio_data = malloc(sdl.audio_size);
296 if (!sdl.audio_data) {
297 printf("%s: Out of memory\n", __func__);
302 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
303 printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
307 /* Open the audio device, forcing the desired format */
308 if (SDL_OpenAudio(&wanted, NULL) < 0) {
309 printf("Couldn't open audio: %s\n", SDL_GetError());
312 sdl.audio_active = true;
317 free(sdl.audio_data);
321 int sandbox_sdl_sound_start(uint frequency)
323 if (!sdl.audio_active)
325 sdl.frequency = frequency;
326 sound_create_square_wave((unsigned short *)sdl.audio_data,
327 sdl.audio_size, frequency);
334 int sandbox_sdl_sound_stop(void)
336 if (!sdl.audio_active)