1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
7 #include <linux/input.h>
10 #include <asm/state.h>
16 static struct sdl_info {
31 static void sandbox_sdl_poll_events(void)
34 * We don't want to include common.h in this file since it uses
35 * system headers. So add a declation here.
37 extern void reset_cpu(unsigned long addr);
40 while (SDL_PollEvent(&event)) {
43 puts("LCD window closed - quitting\n");
50 static int sandbox_sdl_ensure_init(void)
53 if (SDL_Init(0) < 0) {
54 printf("Unable to initialize SDL: %s\n",
66 int sandbox_sdl_init_display(int width, int height, int log2_bpp)
68 struct sandbox_state *state = state_get_current();
71 if (!width || !state->show_lcd)
73 err = sandbox_sdl_ensure_init();
76 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
77 printf("Unable to initialize SDL LCD: %s\n", SDL_GetError());
80 SDL_WM_SetCaption("U-Boot", "U-Boot");
84 sdl.depth = 1 << log2_bpp;
85 sdl.pitch = sdl.width * sdl.depth / 8;
86 sdl.screen = SDL_SetVideoMode(width, height, 0, 0);
87 sandbox_sdl_poll_events();
92 int sandbox_sdl_sync(void *lcd_base)
96 frame = SDL_CreateRGBSurfaceFrom(lcd_base, sdl.width, sdl.height,
98 0x1f << 11, 0x3f << 5, 0x1f << 0, 0);
99 SDL_BlitSurface(frame, NULL, sdl.screen, NULL);
100 SDL_FreeSurface(frame);
101 SDL_UpdateRect(sdl.screen, 0, 0, 0, 0);
102 sandbox_sdl_poll_events();
108 #define NUM_SDL_CODES (SDLK_UNDO + 1)
110 static int16_t sdl_to_keycode[NUM_SDL_CODES] = {
112 NONE, NONE, NONE, NONE, NONE,
113 NONE, NONE, NONE, KEY_BACKSPACE, KEY_TAB,
114 NONE, NONE, NONE, KEY_ENTER, NONE,
115 NONE, NONE, NONE, NONE, KEY_POWER, /* use PAUSE as POWER */
118 NONE, NONE, NONE, NONE, NONE,
119 NONE, NONE, KEY_ESC, NONE, NONE,
120 NONE, NONE, KEY_SPACE, NONE, NONE,
121 NONE, NONE, NONE, NONE, NONE,
124 NONE, NONE, NONE, NONE, KEY_COMMA,
125 KEY_MINUS, KEY_DOT, KEY_SLASH, KEY_0, KEY_1,
126 KEY_2, KEY_3, KEY_4, KEY_5, KEY_6,
127 KEY_7, KEY_8, KEY_9, NONE, KEY_SEMICOLON,
130 NONE, KEY_EQUAL, NONE, NONE, NONE,
131 NONE, NONE, NONE, NONE, NONE,
132 NONE, NONE, NONE, NONE, NONE,
133 NONE, NONE, NONE, NONE, NONE,
136 NONE, NONE, NONE, NONE, NONE,
137 NONE, NONE, NONE, NONE, NONE,
138 NONE, NONE, KEY_BACKSLASH, NONE, NONE,
139 NONE, KEY_GRAVE, KEY_A, KEY_B, KEY_C,
142 KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
143 KEY_I, KEY_J, KEY_K, KEY_L, KEY_M,
144 KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
145 KEY_S, KEY_T, KEY_U, KEY_V, KEY_W,
148 KEY_X, KEY_Y, KEY_Z, NONE, NONE,
149 NONE, NONE, KEY_DELETE, NONE, NONE,
150 NONE, NONE, NONE, NONE, NONE,
151 NONE, NONE, NONE, NONE, NONE,
154 NONE, NONE, NONE, NONE, NONE,
155 NONE, NONE, NONE, NONE, NONE,
156 NONE, NONE, NONE, NONE, NONE,
157 NONE, NONE, NONE, NONE, NONE,
160 NONE, NONE, NONE, NONE, NONE,
161 NONE, NONE, NONE, NONE, NONE,
162 NONE, NONE, NONE, NONE, NONE,
163 NONE, NONE, NONE, NONE, NONE,
166 NONE, NONE, NONE, NONE, NONE,
167 NONE, NONE, NONE, NONE, NONE,
168 NONE, NONE, NONE, NONE, NONE,
169 NONE, NONE, NONE, NONE, NONE,
172 NONE, NONE, NONE, NONE, NONE,
173 NONE, NONE, NONE, NONE, NONE,
174 NONE, NONE, NONE, NONE, NONE,
175 NONE, NONE, NONE, NONE, NONE,
178 NONE, NONE, NONE, NONE, NONE,
179 NONE, NONE, NONE, NONE, NONE,
180 NONE, NONE, NONE, NONE, NONE,
181 NONE, NONE, NONE, NONE, NONE,
184 NONE, NONE, NONE, NONE, NONE,
185 NONE, NONE, NONE, NONE, NONE,
186 NONE, NONE, NONE, NONE, NONE,
187 NONE, KEY_KP0, KEY_KP1, KEY_KP2, KEY_KP3,
190 KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8,
191 KEY_KP9, KEY_KPDOT, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS,
192 KEY_KPPLUS, KEY_KPENTER, KEY_KPEQUAL, KEY_UP, KEY_DOWN,
193 KEY_RIGHT, KEY_LEFT, KEY_INSERT, KEY_HOME, KEY_END,
196 KEY_PAGEUP, KEY_PAGEDOWN, KEY_F1, KEY_F2, KEY_F3,
197 KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8,
198 KEY_F9, KEY_F10, KEY_F11, KEY_F12, NONE,
199 NONE, NONE, NONE, NONE, NONE,
202 KEY_NUMLOCK, KEY_CAPSLOCK, KEY_SCROLLLOCK, KEY_RIGHTSHIFT,
204 KEY_RIGHTCTRL, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTALT, KEY_RIGHTMETA,
205 KEY_LEFTMETA, NONE, KEY_FN, NONE, KEY_COMPOSE,
206 NONE, KEY_PRINT, KEY_SYSRQ, KEY_PAUSE, NONE,
212 int sandbox_sdl_scan_keys(int key[], int max_keys)
217 sandbox_sdl_poll_events();
218 keystate = SDL_GetKeyState(NULL);
219 for (i = count = 0; i < NUM_SDL_CODES; i++) {
220 if (count >= max_keys)
222 else if (keystate[i])
223 key[count++] = sdl_to_keycode[i];
229 int sandbox_sdl_key_pressed(int keycode)
231 int key[8]; /* allow up to 8 keys to be pressed at once */
235 count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
236 for (i = 0; i < count; i++) {
237 if (key[i] == keycode)
244 void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
248 avail = sdl.audio_size - sdl.audio_pos;
252 SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
254 sdl.audio_pos += len;
256 /* Loop if we are at the end */
257 if (sdl.audio_pos == sdl.audio_size)
261 int sandbox_sdl_sound_init(void)
263 SDL_AudioSpec wanted;
265 if (sandbox_sdl_ensure_init())
268 if (sdl.audio_active)
271 /* Set the audio format */
272 wanted.freq = SAMPLE_RATE;
273 wanted.format = AUDIO_S16;
274 wanted.channels = 1; /* 1 = mono, 2 = stereo */
275 wanted.samples = 1024; /* Good low-latency value for callback */
276 wanted.callback = sandbox_sdl_fill_audio;
277 wanted.userdata = NULL;
279 sdl.audio_size = sizeof(uint16_t) * wanted.freq;
280 sdl.audio_data = malloc(sdl.audio_size);
281 if (!sdl.audio_data) {
282 printf("%s: Out of memory\n", __func__);
287 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
288 printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
292 /* Open the audio device, forcing the desired format */
293 if (SDL_OpenAudio(&wanted, NULL) < 0) {
294 printf("Couldn't open audio: %s\n", SDL_GetError());
297 sdl.audio_active = true;
298 sdl.sample_rate = wanted.freq;
303 free(sdl.audio_data);
307 int sandbox_sdl_sound_start(uint frequency)
309 if (!sdl.audio_active)
311 sdl.frequency = frequency;
312 sound_create_square_wave(sdl.sample_rate,
313 (unsigned short *)sdl.audio_data,
314 sdl.audio_size, frequency);
321 int sandbox_sdl_sound_stop(void)
323 if (!sdl.audio_active)