1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
7 #include <linux/input.h>
10 #include <asm/state.h>
12 static struct sdl_info {
26 static void sandbox_sdl_poll_events(void)
29 * We don't want to include common.h in this file since it uses
30 * system headers. So add a declation here.
32 extern void reset_cpu(unsigned long addr);
35 while (SDL_PollEvent(&event)) {
38 puts("LCD window closed - quitting\n");
45 static int sandbox_sdl_ensure_init(void)
48 if (SDL_Init(0) < 0) {
49 printf("Unable to initialize SDL: %s\n",
61 int sandbox_sdl_init_display(int width, int height, int log2_bpp)
63 struct sandbox_state *state = state_get_current();
66 if (!width || !state->show_lcd)
68 err = sandbox_sdl_ensure_init();
71 if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
72 printf("Unable to initialize SDL LCD: %s\n", SDL_GetError());
75 SDL_WM_SetCaption("U-Boot", "U-Boot");
79 sdl.depth = 1 << log2_bpp;
80 sdl.pitch = sdl.width * sdl.depth / 8;
81 sdl.screen = SDL_SetVideoMode(width, height, 0, 0);
82 sandbox_sdl_poll_events();
87 int sandbox_sdl_sync(void *lcd_base)
91 frame = SDL_CreateRGBSurfaceFrom(lcd_base, sdl.width, sdl.height,
93 0x1f << 11, 0x3f << 5, 0x1f << 0, 0);
94 SDL_BlitSurface(frame, NULL, sdl.screen, NULL);
95 SDL_FreeSurface(frame);
96 SDL_UpdateRect(sdl.screen, 0, 0, 0, 0);
97 sandbox_sdl_poll_events();
103 #define NUM_SDL_CODES (SDLK_UNDO + 1)
105 static int16_t sdl_to_keycode[NUM_SDL_CODES] = {
107 NONE, NONE, NONE, NONE, NONE,
108 NONE, NONE, NONE, KEY_BACKSPACE, KEY_TAB,
109 NONE, NONE, NONE, KEY_ENTER, NONE,
110 NONE, NONE, NONE, NONE, KEY_POWER, /* use PAUSE as POWER */
113 NONE, NONE, NONE, NONE, NONE,
114 NONE, NONE, KEY_ESC, NONE, NONE,
115 NONE, NONE, KEY_SPACE, NONE, NONE,
116 NONE, NONE, NONE, NONE, NONE,
119 NONE, NONE, NONE, NONE, KEY_COMMA,
120 KEY_MINUS, KEY_DOT, KEY_SLASH, KEY_0, KEY_1,
121 KEY_2, KEY_3, KEY_4, KEY_5, KEY_6,
122 KEY_7, KEY_8, KEY_9, NONE, KEY_SEMICOLON,
125 NONE, KEY_EQUAL, NONE, NONE, NONE,
126 NONE, NONE, NONE, NONE, NONE,
127 NONE, NONE, NONE, NONE, NONE,
128 NONE, NONE, NONE, NONE, NONE,
131 NONE, NONE, NONE, NONE, NONE,
132 NONE, NONE, NONE, NONE, NONE,
133 NONE, NONE, KEY_BACKSLASH, NONE, NONE,
134 NONE, KEY_GRAVE, KEY_A, KEY_B, KEY_C,
137 KEY_D, KEY_E, KEY_F, KEY_G, KEY_H,
138 KEY_I, KEY_J, KEY_K, KEY_L, KEY_M,
139 KEY_N, KEY_O, KEY_P, KEY_Q, KEY_R,
140 KEY_S, KEY_T, KEY_U, KEY_V, KEY_W,
143 KEY_X, KEY_Y, KEY_Z, NONE, NONE,
144 NONE, NONE, KEY_DELETE, NONE, NONE,
145 NONE, NONE, NONE, NONE, NONE,
146 NONE, NONE, NONE, NONE, NONE,
149 NONE, NONE, NONE, NONE, NONE,
150 NONE, NONE, NONE, NONE, NONE,
151 NONE, NONE, NONE, NONE, NONE,
152 NONE, NONE, NONE, NONE, NONE,
155 NONE, NONE, NONE, NONE, NONE,
156 NONE, NONE, NONE, NONE, NONE,
157 NONE, NONE, NONE, NONE, NONE,
158 NONE, NONE, NONE, NONE, NONE,
161 NONE, NONE, NONE, NONE, NONE,
162 NONE, NONE, NONE, NONE, NONE,
163 NONE, NONE, NONE, NONE, NONE,
164 NONE, NONE, NONE, NONE, NONE,
167 NONE, NONE, NONE, NONE, NONE,
168 NONE, NONE, NONE, NONE, NONE,
169 NONE, NONE, NONE, NONE, NONE,
170 NONE, NONE, NONE, NONE, NONE,
173 NONE, NONE, NONE, NONE, NONE,
174 NONE, NONE, NONE, NONE, NONE,
175 NONE, NONE, NONE, NONE, NONE,
176 NONE, NONE, NONE, NONE, NONE,
179 NONE, NONE, NONE, NONE, NONE,
180 NONE, NONE, NONE, NONE, NONE,
181 NONE, NONE, NONE, NONE, NONE,
182 NONE, KEY_KP0, KEY_KP1, KEY_KP2, KEY_KP3,
185 KEY_KP4, KEY_KP5, KEY_KP6, KEY_KP7, KEY_KP8,
186 KEY_KP9, KEY_KPDOT, KEY_KPSLASH, KEY_KPASTERISK, KEY_KPMINUS,
187 KEY_KPPLUS, KEY_KPENTER, KEY_KPEQUAL, KEY_UP, KEY_DOWN,
188 KEY_RIGHT, KEY_LEFT, KEY_INSERT, KEY_HOME, KEY_END,
191 KEY_PAGEUP, KEY_PAGEDOWN, KEY_F1, KEY_F2, KEY_F3,
192 KEY_F4, KEY_F5, KEY_F6, KEY_F7, KEY_F8,
193 KEY_F9, KEY_F10, KEY_F11, KEY_F12, NONE,
194 NONE, NONE, NONE, NONE, NONE,
197 KEY_NUMLOCK, KEY_CAPSLOCK, KEY_SCROLLLOCK, KEY_RIGHTSHIFT,
199 KEY_RIGHTCTRL, KEY_LEFTCTRL, KEY_RIGHTALT, KEY_LEFTALT, KEY_RIGHTMETA,
200 KEY_LEFTMETA, NONE, KEY_FN, NONE, KEY_COMPOSE,
201 NONE, KEY_PRINT, KEY_SYSRQ, KEY_PAUSE, NONE,
207 int sandbox_sdl_scan_keys(int key[], int max_keys)
212 sandbox_sdl_poll_events();
213 keystate = SDL_GetKeyState(NULL);
214 for (i = count = 0; i < NUM_SDL_CODES; i++) {
215 if (count >= max_keys)
217 else if (keystate[i])
218 key[count++] = sdl_to_keycode[i];
224 int sandbox_sdl_key_pressed(int keycode)
226 int key[8]; /* allow up to 8 keys to be pressed at once */
230 count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0]));
231 for (i = 0; i < count; i++) {
232 if (key[i] == keycode)
239 void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len)
243 avail = sdl.audio_size - sdl.audio_pos;
247 SDL_MixAudio(stream, sdl.audio_data + sdl.audio_pos, len,
249 sdl.audio_pos += len;
251 /* Loop if we are at the end */
252 if (sdl.audio_pos == sdl.audio_size)
256 int sandbox_sdl_sound_init(void)
258 SDL_AudioSpec wanted;
260 if (sandbox_sdl_ensure_init())
263 if (sdl.audio_active)
267 * At present all sandbox sounds crash. This is probably due to
268 * symbol name conflicts with U-Boot. We can remove the malloc()
271 * #define USE_DL_PREFIX
275 * Assertion 'e->pollfd->fd == e->fd' failed at pulse/mainloop.c:676,
276 * function dispatch_pollfds(). Aborting.
278 * The right solution is probably to make U-Boot's names private or
279 * link os.c and sdl.c against their libraries before liking with
280 * U-Boot. TBD. For now sound is disabled.
282 printf("(Warning: sandbox sound disabled)\n");
285 /* Set the audio format */
287 wanted.format = AUDIO_S16;
288 wanted.channels = 1; /* 1 = mono, 2 = stereo */
289 wanted.samples = 1024; /* Good low-latency value for callback */
290 wanted.callback = sandbox_sdl_fill_audio;
291 wanted.userdata = NULL;
293 sdl.audio_size = sizeof(uint16_t) * wanted.freq;
294 sdl.audio_data = malloc(sdl.audio_size);
295 if (!sdl.audio_data) {
296 printf("%s: Out of memory\n", __func__);
301 if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) {
302 printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
306 /* Open the audio device, forcing the desired format */
307 if (SDL_OpenAudio(&wanted, NULL) < 0) {
308 printf("Couldn't open audio: %s\n", SDL_GetError());
311 sdl.audio_active = true;
316 free(sdl.audio_data);
320 int sandbox_sdl_sound_start(uint frequency)
322 if (!sdl.audio_active)
324 sdl.frequency = frequency;
325 sound_create_square_wave((unsigned short *)sdl.audio_data,
326 sdl.audio_size, frequency);
333 int sandbox_sdl_sound_stop(void)
335 if (!sdl.audio_active)