]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - arch/sandbox/cpu/sdl.c
1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2013 Google, Inc
8 #include <linux/input.h>
11 #include <asm/state.h>
18 * struct buf_info - a data buffer holding audio data
20 * @pos: Current position playing in audio buffer
21 * @size: Size of data in audio buffer (0=empty)
22 * @alloced: Allocated size of audio buffer (max size it can hold)
32 static struct sdl_info
{
43 struct buf_info buf
[2];
47 static void sandbox_sdl_poll_events(void)
50 * We don't want to include common.h in this file since it uses
51 * system headers. So add a declation here.
53 extern void reset_cpu(unsigned long addr
);
56 while (SDL_PollEvent(&event
)) {
59 puts("LCD window closed - quitting\n");
66 static int sandbox_sdl_ensure_init(void)
69 if (SDL_Init(0) < 0) {
70 printf("Unable to initialize SDL: %s\n",
82 int sandbox_sdl_init_display(int width
, int height
, int log2_bpp
)
84 struct sandbox_state
*state
= state_get_current();
87 if (!width
|| !state
->show_lcd
)
89 err
= sandbox_sdl_ensure_init();
92 if (SDL_InitSubSystem(SDL_INIT_VIDEO
) < 0) {
93 printf("Unable to initialize SDL LCD: %s\n", SDL_GetError());
96 SDL_WM_SetCaption("U-Boot", "U-Boot");
100 sdl
.depth
= 1 << log2_bpp
;
101 sdl
.pitch
= sdl
.width
* sdl
.depth
/ 8;
102 sdl
.screen
= SDL_SetVideoMode(width
, height
, 0, 0);
103 sandbox_sdl_poll_events();
108 int sandbox_sdl_sync(void *lcd_base
)
112 frame
= SDL_CreateRGBSurfaceFrom(lcd_base
, sdl
.width
, sdl
.height
,
113 sdl
.depth
, sdl
.pitch
,
114 0x1f << 11, 0x3f << 5, 0x1f << 0, 0);
115 SDL_BlitSurface(frame
, NULL
, sdl
.screen
, NULL
);
116 SDL_FreeSurface(frame
);
117 SDL_UpdateRect(sdl
.screen
, 0, 0, 0, 0);
118 sandbox_sdl_poll_events();
124 #define NUM_SDL_CODES (SDLK_UNDO + 1)
126 static int16_t sdl_to_keycode
[NUM_SDL_CODES
] = {
128 NONE
, NONE
, NONE
, NONE
, NONE
,
129 NONE
, NONE
, NONE
, KEY_BACKSPACE
, KEY_TAB
,
130 NONE
, NONE
, NONE
, KEY_ENTER
, NONE
,
131 NONE
, NONE
, NONE
, NONE
, KEY_POWER
, /* use PAUSE as POWER */
134 NONE
, NONE
, NONE
, NONE
, NONE
,
135 NONE
, NONE
, KEY_ESC
, NONE
, NONE
,
136 NONE
, NONE
, KEY_SPACE
, NONE
, NONE
,
137 NONE
, NONE
, NONE
, NONE
, NONE
,
140 NONE
, NONE
, NONE
, NONE
, KEY_COMMA
,
141 KEY_MINUS
, KEY_DOT
, KEY_SLASH
, KEY_0
, KEY_1
,
142 KEY_2
, KEY_3
, KEY_4
, KEY_5
, KEY_6
,
143 KEY_7
, KEY_8
, KEY_9
, NONE
, KEY_SEMICOLON
,
146 NONE
, KEY_EQUAL
, NONE
, NONE
, NONE
,
147 NONE
, NONE
, NONE
, NONE
, NONE
,
148 NONE
, NONE
, NONE
, NONE
, NONE
,
149 NONE
, NONE
, NONE
, NONE
, NONE
,
152 NONE
, NONE
, NONE
, NONE
, NONE
,
153 NONE
, NONE
, NONE
, NONE
, NONE
,
154 NONE
, NONE
, KEY_BACKSLASH
, NONE
, NONE
,
155 NONE
, KEY_GRAVE
, KEY_A
, KEY_B
, KEY_C
,
158 KEY_D
, KEY_E
, KEY_F
, KEY_G
, KEY_H
,
159 KEY_I
, KEY_J
, KEY_K
, KEY_L
, KEY_M
,
160 KEY_N
, KEY_O
, KEY_P
, KEY_Q
, KEY_R
,
161 KEY_S
, KEY_T
, KEY_U
, KEY_V
, KEY_W
,
164 KEY_X
, KEY_Y
, KEY_Z
, NONE
, NONE
,
165 NONE
, NONE
, KEY_DELETE
, NONE
, NONE
,
166 NONE
, NONE
, NONE
, NONE
, NONE
,
167 NONE
, NONE
, NONE
, NONE
, NONE
,
170 NONE
, NONE
, NONE
, NONE
, NONE
,
171 NONE
, NONE
, NONE
, NONE
, NONE
,
172 NONE
, NONE
, NONE
, NONE
, NONE
,
173 NONE
, NONE
, NONE
, NONE
, NONE
,
176 NONE
, NONE
, NONE
, NONE
, NONE
,
177 NONE
, NONE
, NONE
, NONE
, NONE
,
178 NONE
, NONE
, NONE
, NONE
, NONE
,
179 NONE
, NONE
, NONE
, NONE
, NONE
,
182 NONE
, NONE
, NONE
, NONE
, NONE
,
183 NONE
, NONE
, NONE
, NONE
, NONE
,
184 NONE
, NONE
, NONE
, NONE
, NONE
,
185 NONE
, NONE
, NONE
, NONE
, NONE
,
188 NONE
, NONE
, NONE
, NONE
, NONE
,
189 NONE
, NONE
, NONE
, NONE
, NONE
,
190 NONE
, NONE
, NONE
, NONE
, NONE
,
191 NONE
, NONE
, NONE
, NONE
, NONE
,
194 NONE
, NONE
, NONE
, NONE
, NONE
,
195 NONE
, NONE
, NONE
, NONE
, NONE
,
196 NONE
, NONE
, NONE
, NONE
, NONE
,
197 NONE
, NONE
, NONE
, NONE
, NONE
,
200 NONE
, NONE
, NONE
, NONE
, NONE
,
201 NONE
, NONE
, NONE
, NONE
, NONE
,
202 NONE
, NONE
, NONE
, NONE
, NONE
,
203 NONE
, KEY_KP0
, KEY_KP1
, KEY_KP2
, KEY_KP3
,
206 KEY_KP4
, KEY_KP5
, KEY_KP6
, KEY_KP7
, KEY_KP8
,
207 KEY_KP9
, KEY_KPDOT
, KEY_KPSLASH
, KEY_KPASTERISK
, KEY_KPMINUS
,
208 KEY_KPPLUS
, KEY_KPENTER
, KEY_KPEQUAL
, KEY_UP
, KEY_DOWN
,
209 KEY_RIGHT
, KEY_LEFT
, KEY_INSERT
, KEY_HOME
, KEY_END
,
212 KEY_PAGEUP
, KEY_PAGEDOWN
, KEY_F1
, KEY_F2
, KEY_F3
,
213 KEY_F4
, KEY_F5
, KEY_F6
, KEY_F7
, KEY_F8
,
214 KEY_F9
, KEY_F10
, KEY_F11
, KEY_F12
, NONE
,
215 NONE
, NONE
, NONE
, NONE
, NONE
,
218 KEY_NUMLOCK
, KEY_CAPSLOCK
, KEY_SCROLLLOCK
, KEY_RIGHTSHIFT
,
220 KEY_RIGHTCTRL
, KEY_LEFTCTRL
, KEY_RIGHTALT
, KEY_LEFTALT
, KEY_RIGHTMETA
,
221 KEY_LEFTMETA
, NONE
, KEY_FN
, NONE
, KEY_COMPOSE
,
222 NONE
, KEY_PRINT
, KEY_SYSRQ
, KEY_PAUSE
, NONE
,
228 int sandbox_sdl_scan_keys(int key
[], int max_keys
)
233 sandbox_sdl_poll_events();
234 keystate
= SDL_GetKeyState(NULL
);
235 for (i
= count
= 0; i
< NUM_SDL_CODES
; i
++) {
236 if (count
>= max_keys
)
238 else if (keystate
[i
])
239 key
[count
++] = sdl_to_keycode
[i
];
245 int sandbox_sdl_key_pressed(int keycode
)
247 int key
[8]; /* allow up to 8 keys to be pressed at once */
251 count
= sandbox_sdl_scan_keys(key
, sizeof(key
) / sizeof(key
[0]));
252 for (i
= 0; i
< count
; i
++) {
253 if (key
[i
] == keycode
)
260 void sandbox_sdl_fill_audio(void *udata
, Uint8
*stream
, int len
)
262 struct buf_info
*buf
;
266 for (i
= 0; i
< 2; i
++) {
267 buf
= &sdl
.buf
[sdl
.cur_buf
];
268 avail
= buf
->size
- buf
->pos
;
270 sdl
.cur_buf
= 1 - sdl
.cur_buf
;
276 SDL_MixAudio(stream
, buf
->data
+ buf
->pos
, avail
,
281 /* Move to next buffer if we are at the end */
282 if (buf
->pos
== buf
->size
)
289 int sandbox_sdl_sound_init(void)
291 SDL_AudioSpec wanted
;
294 if (sandbox_sdl_ensure_init())
297 if (sdl
.audio_active
)
300 /* Set the audio format */
301 wanted
.freq
= SAMPLE_RATE
;
302 wanted
.format
= AUDIO_S16
;
303 wanted
.channels
= 1; /* 1 = mono, 2 = stereo */
304 wanted
.samples
= 1024; /* Good low-latency value for callback */
305 wanted
.callback
= sandbox_sdl_fill_audio
;
306 wanted
.userdata
= NULL
;
308 for (i
= 0; i
< 2; i
++) {
309 struct buf_info
*buf
= &sdl
.buf
[i
];
311 buf
->alloced
= sizeof(uint16_t) * wanted
.freq
* wanted
.channels
;
312 buf
->data
= malloc(buf
->alloced
);
314 printf("%s: Out of memory\n", __func__
);
316 free(sdl
.buf
[0].data
);
323 if (SDL_InitSubSystem(SDL_INIT_AUDIO
) < 0) {
324 printf("Unable to initialize SDL audio: %s\n", SDL_GetError());
328 /* Open the audio device, forcing the desired format */
329 if (SDL_OpenAudio(&wanted
, NULL
) < 0) {
330 printf("Couldn't open audio: %s\n", SDL_GetError());
333 sdl
.audio_active
= true;
334 sdl
.sample_rate
= wanted
.freq
;
341 for (i
= 0; i
< 2; i
++)
342 free(sdl
.buf
[i
].data
);
346 int sandbox_sdl_sound_play(const void *data
, uint size
)
348 struct buf_info
*buf
;
350 if (!sdl
.audio_active
)
359 if (size
> buf
->alloced
)
362 memcpy(buf
->data
, data
, size
);
373 int sandbox_sdl_sound_stop(void)