Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Google, Inc |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <errno.h> |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 7 | #include <unistd.h> |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 8 | #include <stdbool.h> |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 9 | #include <linux/input.h> |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 10 | #include <SDL2/SDL.h> |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 11 | #include <asm/state.h> |
| 12 | |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 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 | |
Simon Glass | c127f19 | 2020-02-03 07:36:08 -0700 | [diff] [blame] | 28 | /** |
| 29 | * struct sdl_info - Information about our use of the SDL library |
| 30 | * |
Simon Glass | c127f19 | 2020-02-03 07:36:08 -0700 | [diff] [blame] | 31 | * @width: Width of simulated LCD display |
| 32 | * @height: Height of simulated LCD display |
Simon Glass | 6be88c7 | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 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) |
Simon Glass | c127f19 | 2020-02-03 07:36:08 -0700 | [diff] [blame] | 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 |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 44 | * @stopping: true if audio will stop once it runs out of data |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 45 | * @texture: SDL texture to use for U-Boot display contents |
| 46 | * @renderer: SDL renderer to use |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 47 | * @screen: SDL window to use |
Simon Glass | 0fe5e94 | 2021-11-19 13:23:45 -0700 | [diff] [blame] | 48 | * @src_depth: Number of bits per pixel in the source frame buffer (that we read |
| 49 | * from and render to SDL) |
Simon Glass | c127f19 | 2020-02-03 07:36:08 -0700 | [diff] [blame] | 50 | */ |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 51 | static struct sdl_info { |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 52 | int width; |
| 53 | int height; |
Simon Glass | 6be88c7 | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 54 | int vis_width; |
| 55 | int vis_height; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 56 | int depth; |
| 57 | int pitch; |
Simon Glass | 856b8f5 | 2018-11-15 19:56:14 -0700 | [diff] [blame] | 58 | uint sample_rate; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 59 | bool audio_active; |
| 60 | bool inited; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 61 | int cur_buf; |
| 62 | struct buf_info buf[2]; |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 63 | bool running; |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 64 | bool stopping; |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 65 | SDL_Texture *texture; |
| 66 | SDL_Renderer *renderer; |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 67 | SDL_Window *screen; |
Simon Glass | 0fe5e94 | 2021-11-19 13:23:45 -0700 | [diff] [blame] | 68 | int src_depth; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 69 | } sdl; |
| 70 | |
| 71 | static void sandbox_sdl_poll_events(void) |
| 72 | { |
| 73 | /* |
| 74 | * We don't want to include common.h in this file since it uses |
| 75 | * system headers. So add a declation here. |
| 76 | */ |
Harald Seiler | 35b65dd | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 77 | extern void reset_cpu(void); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 78 | SDL_Event event; |
| 79 | |
| 80 | while (SDL_PollEvent(&event)) { |
| 81 | switch (event.type) { |
| 82 | case SDL_QUIT: |
| 83 | puts("LCD window closed - quitting\n"); |
Harald Seiler | 35b65dd | 2020-12-15 16:47:52 +0100 | [diff] [blame] | 84 | reset_cpu(); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | static int sandbox_sdl_ensure_init(void) |
| 91 | { |
| 92 | if (!sdl.inited) { |
| 93 | if (SDL_Init(0) < 0) { |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 94 | printf("Unable to initialise SDL: %s\n", |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 95 | SDL_GetError()); |
| 96 | return -EIO; |
| 97 | } |
| 98 | |
| 99 | atexit(SDL_Quit); |
| 100 | |
| 101 | sdl.inited = true; |
| 102 | } |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 106 | int sandbox_sdl_remove_display(void) |
| 107 | { |
| 108 | if (!sdl.renderer) { |
| 109 | printf("SDL renderer does not exist\n"); |
| 110 | return -ENOENT; |
| 111 | } |
| 112 | |
| 113 | SDL_DestroyTexture(sdl.texture); |
| 114 | SDL_DestroyRenderer(sdl.renderer); |
| 115 | SDL_DestroyWindow(sdl.screen); |
| 116 | sdl.texture = NULL; |
| 117 | sdl.renderer = NULL; |
| 118 | sdl.screen = NULL; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Simon Glass | 6be88c7 | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 123 | int sandbox_sdl_init_display(int width, int height, int log2_bpp, |
| 124 | bool double_size) |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 125 | { |
| 126 | struct sandbox_state *state = state_get_current(); |
| 127 | int err; |
| 128 | |
| 129 | if (!width || !state->show_lcd) |
| 130 | return 0; |
| 131 | err = sandbox_sdl_ensure_init(); |
| 132 | if (err) |
| 133 | return err; |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 134 | if (sdl.renderer) |
| 135 | sandbox_sdl_remove_display(); |
| 136 | |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 137 | if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) { |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 138 | printf("Unable to initialise SDL LCD: %s\n", SDL_GetError()); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 139 | return -EPERM; |
| 140 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 141 | sdl.width = width; |
| 142 | sdl.height = height; |
Simon Glass | 6be88c7 | 2020-02-03 07:36:13 -0700 | [diff] [blame] | 143 | if (double_size) { |
| 144 | sdl.vis_width = sdl.width * 2; |
| 145 | sdl.vis_height = sdl.height * 2; |
| 146 | } else { |
| 147 | sdl.vis_width = sdl.width; |
| 148 | sdl.vis_height = sdl.height; |
| 149 | } |
| 150 | |
Simon Glass | 350b1d3 | 2021-07-05 16:32:47 -0600 | [diff] [blame] | 151 | if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) |
| 152 | printf("Unable to init hinting: %s", SDL_GetError()); |
| 153 | |
Simon Glass | 0fe5e94 | 2021-11-19 13:23:45 -0700 | [diff] [blame] | 154 | sdl.src_depth = 1 << log2_bpp; |
| 155 | if (log2_bpp != 4 && log2_bpp != 5) |
| 156 | log2_bpp = 5; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 157 | sdl.depth = 1 << log2_bpp; |
| 158 | sdl.pitch = sdl.width * sdl.depth / 8; |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 159 | sdl.screen = SDL_CreateWindow("U-Boot", SDL_WINDOWPOS_UNDEFINED, |
| 160 | SDL_WINDOWPOS_UNDEFINED, sdl.vis_width, |
| 161 | sdl.vis_height, SDL_WINDOW_RESIZABLE); |
| 162 | if (!sdl.screen) { |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 163 | printf("Unable to initialise SDL screen: %s\n", |
| 164 | SDL_GetError()); |
| 165 | return -EIO; |
| 166 | } |
Simon Glass | 250e735 | 2021-11-19 13:23:46 -0700 | [diff] [blame] | 167 | sdl.renderer = SDL_CreateRenderer(sdl.screen, -1, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 168 | SDL_RENDERER_ACCELERATED | |
| 169 | SDL_RENDERER_PRESENTVSYNC); |
| 170 | if (!sdl.renderer) { |
| 171 | printf("Unable to initialise SDL renderer: %s\n", |
| 172 | SDL_GetError()); |
| 173 | return -EIO; |
| 174 | } |
| 175 | |
| 176 | sdl.texture = SDL_CreateTexture(sdl.renderer, log2_bpp == 4 ? |
| 177 | SDL_PIXELFORMAT_RGB565 : |
| 178 | SDL_PIXELFORMAT_RGB888, |
| 179 | SDL_TEXTUREACCESS_STREAMING, |
| 180 | width, height); |
| 181 | if (!sdl.texture) { |
| 182 | printf("Unable to initialise SDL texture: %s\n", |
| 183 | SDL_GetError()); |
| 184 | return -EBADF; |
| 185 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 186 | sandbox_sdl_poll_events(); |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
Simon Glass | 0fe5e94 | 2021-11-19 13:23:45 -0700 | [diff] [blame] | 191 | static int copy_to_texture(void *lcd_base) |
| 192 | { |
| 193 | char *dest; |
| 194 | int pitch, x, y; |
| 195 | int src_pitch; |
| 196 | void *pixels; |
| 197 | char *src; |
| 198 | int ret; |
| 199 | |
| 200 | if (sdl.src_depth == sdl.depth) { |
| 201 | SDL_UpdateTexture(sdl.texture, NULL, lcd_base, sdl.pitch); |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * We only support copying from an 8bpp to a 32bpp texture since the |
| 207 | * other cases are supported directly by the texture. |
| 208 | */ |
| 209 | if (sdl.depth != 32 && sdl.src_depth != 8) { |
| 210 | printf("Need depth 32bpp for copy\n"); |
| 211 | return -EINVAL; |
| 212 | } |
| 213 | |
| 214 | ret = SDL_LockTexture(sdl.texture, NULL, &pixels, &pitch); |
| 215 | if (ret) { |
| 216 | printf("SDL lock %d: %s\n", ret, SDL_GetError()); |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* Copy the pixels one by one */ |
| 221 | src_pitch = sdl.width * sdl.src_depth / 8; |
| 222 | for (y = 0; y < sdl.height; y++) { |
| 223 | char val; |
| 224 | |
| 225 | dest = pixels + y * pitch; |
| 226 | src = lcd_base + src_pitch * y; |
| 227 | for (x = 0; x < sdl.width; x++, dest += 4) { |
| 228 | val = *src++; |
| 229 | dest[0] = val; |
| 230 | dest[1] = val; |
| 231 | dest[2] = val; |
| 232 | dest[3] = 0; |
| 233 | } |
| 234 | } |
| 235 | SDL_UnlockTexture(sdl.texture); |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 240 | int sandbox_sdl_sync(void *lcd_base) |
| 241 | { |
Simon Glass | fcb7e31 | 2021-07-05 16:32:46 -0600 | [diff] [blame] | 242 | struct SDL_Rect rect; |
| 243 | int ret; |
| 244 | |
| 245 | if (!sdl.texture) |
| 246 | return 0; |
| 247 | SDL_RenderClear(sdl.renderer); |
Simon Glass | 0fe5e94 | 2021-11-19 13:23:45 -0700 | [diff] [blame] | 248 | ret = copy_to_texture(lcd_base); |
| 249 | if (ret) { |
| 250 | printf("copy_to_texture: %d: %s\n", ret, SDL_GetError()); |
| 251 | return -EIO; |
| 252 | } |
Simon Glass | fcb7e31 | 2021-07-05 16:32:46 -0600 | [diff] [blame] | 253 | ret = SDL_RenderCopy(sdl.renderer, sdl.texture, NULL, NULL); |
| 254 | if (ret) { |
| 255 | printf("SDL copy %d: %s\n", ret, SDL_GetError()); |
| 256 | return -EIO; |
| 257 | } |
| 258 | |
| 259 | /* |
| 260 | * On some machines this does not appear. Draw an empty rectangle which |
| 261 | * seems to fix that. |
| 262 | */ |
| 263 | rect.x = 0; |
| 264 | rect.y = 0; |
| 265 | rect.w = 0; |
| 266 | rect.h = 0; |
| 267 | SDL_RenderDrawRect(sdl.renderer, &rect); |
| 268 | |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 269 | SDL_RenderPresent(sdl.renderer); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 270 | sandbox_sdl_poll_events(); |
| 271 | |
| 272 | return 0; |
| 273 | } |
| 274 | |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 275 | static const unsigned short sdl_to_keycode[SDL_NUM_SCANCODES] = { |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 276 | [SDL_SCANCODE_ESCAPE] = KEY_ESC, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 277 | [SDL_SCANCODE_1] = KEY_1, |
| 278 | [SDL_SCANCODE_2] = KEY_2, |
| 279 | [SDL_SCANCODE_3] = KEY_3, |
| 280 | [SDL_SCANCODE_4] = KEY_4, |
| 281 | [SDL_SCANCODE_5] = KEY_5, |
| 282 | [SDL_SCANCODE_6] = KEY_6, |
| 283 | [SDL_SCANCODE_7] = KEY_7, |
| 284 | [SDL_SCANCODE_8] = KEY_8, |
| 285 | [SDL_SCANCODE_9] = KEY_9, |
| 286 | [SDL_SCANCODE_0] = KEY_0, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 287 | [SDL_SCANCODE_MINUS] = KEY_MINUS, |
| 288 | [SDL_SCANCODE_EQUALS] = KEY_EQUAL, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 289 | [SDL_SCANCODE_BACKSPACE] = KEY_BACKSPACE, |
| 290 | [SDL_SCANCODE_TAB] = KEY_TAB, |
| 291 | [SDL_SCANCODE_Q] = KEY_Q, |
| 292 | [SDL_SCANCODE_W] = KEY_W, |
| 293 | [SDL_SCANCODE_E] = KEY_E, |
| 294 | [SDL_SCANCODE_R] = KEY_R, |
| 295 | [SDL_SCANCODE_T] = KEY_T, |
| 296 | [SDL_SCANCODE_Y] = KEY_Y, |
| 297 | [SDL_SCANCODE_U] = KEY_U, |
| 298 | [SDL_SCANCODE_I] = KEY_I, |
| 299 | [SDL_SCANCODE_O] = KEY_O, |
| 300 | [SDL_SCANCODE_P] = KEY_P, |
| 301 | [SDL_SCANCODE_LEFTBRACKET] = KEY_LEFTBRACE, |
| 302 | [SDL_SCANCODE_RIGHTBRACKET] = KEY_RIGHTBRACE, |
| 303 | [SDL_SCANCODE_RETURN] = KEY_ENTER, |
| 304 | [SDL_SCANCODE_LCTRL] = KEY_LEFTCTRL, |
| 305 | [SDL_SCANCODE_A] = KEY_A, |
| 306 | [SDL_SCANCODE_S] = KEY_S, |
| 307 | [SDL_SCANCODE_D] = KEY_D, |
| 308 | [SDL_SCANCODE_F] = KEY_F, |
| 309 | [SDL_SCANCODE_G] = KEY_G, |
| 310 | [SDL_SCANCODE_H] = KEY_H, |
| 311 | [SDL_SCANCODE_J] = KEY_J, |
| 312 | [SDL_SCANCODE_K] = KEY_K, |
| 313 | [SDL_SCANCODE_L] = KEY_L, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 314 | [SDL_SCANCODE_SEMICOLON] = KEY_SEMICOLON, |
| 315 | [SDL_SCANCODE_APOSTROPHE] = KEY_APOSTROPHE, |
| 316 | [SDL_SCANCODE_GRAVE] = KEY_GRAVE, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 317 | [SDL_SCANCODE_LSHIFT] = KEY_LEFTSHIFT, |
| 318 | [SDL_SCANCODE_BACKSLASH] = KEY_BACKSLASH, |
| 319 | [SDL_SCANCODE_Z] = KEY_Z, |
| 320 | [SDL_SCANCODE_X] = KEY_X, |
| 321 | [SDL_SCANCODE_C] = KEY_C, |
| 322 | [SDL_SCANCODE_V] = KEY_V, |
| 323 | [SDL_SCANCODE_B] = KEY_B, |
| 324 | [SDL_SCANCODE_N] = KEY_N, |
| 325 | [SDL_SCANCODE_M] = KEY_M, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 326 | [SDL_SCANCODE_COMMA] = KEY_COMMA, |
| 327 | [SDL_SCANCODE_PERIOD] = KEY_DOT, |
| 328 | [SDL_SCANCODE_SLASH] = KEY_SLASH, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 329 | [SDL_SCANCODE_RSHIFT] = KEY_RIGHTSHIFT, |
| 330 | [SDL_SCANCODE_KP_MULTIPLY] = KEY_KPASTERISK, |
| 331 | [SDL_SCANCODE_LALT] = KEY_LEFTALT, |
| 332 | [SDL_SCANCODE_SPACE] = KEY_SPACE, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 333 | [SDL_SCANCODE_CAPSLOCK] = KEY_CAPSLOCK, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 334 | [SDL_SCANCODE_F1] = KEY_F1, |
| 335 | [SDL_SCANCODE_F2] = KEY_F2, |
| 336 | [SDL_SCANCODE_F3] = KEY_F3, |
| 337 | [SDL_SCANCODE_F4] = KEY_F4, |
| 338 | [SDL_SCANCODE_F5] = KEY_F5, |
| 339 | [SDL_SCANCODE_F6] = KEY_F6, |
| 340 | [SDL_SCANCODE_F7] = KEY_F7, |
| 341 | [SDL_SCANCODE_F8] = KEY_F8, |
| 342 | [SDL_SCANCODE_F9] = KEY_F9, |
| 343 | [SDL_SCANCODE_F10] = KEY_F10, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 344 | [SDL_SCANCODE_NUMLOCKCLEAR] = KEY_NUMLOCK, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 345 | [SDL_SCANCODE_SCROLLLOCK] = KEY_SCROLLLOCK, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 346 | [SDL_SCANCODE_KP_7] = KEY_KP7, |
| 347 | [SDL_SCANCODE_KP_8] = KEY_KP8, |
| 348 | [SDL_SCANCODE_KP_9] = KEY_KP9, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 349 | [SDL_SCANCODE_KP_MINUS] = KEY_KPMINUS, |
| 350 | [SDL_SCANCODE_KP_4] = KEY_KP4, |
| 351 | [SDL_SCANCODE_KP_5] = KEY_KP5, |
| 352 | [SDL_SCANCODE_KP_6] = KEY_KP6, |
| 353 | [SDL_SCANCODE_KP_PLUS] = KEY_KPPLUS, |
| 354 | [SDL_SCANCODE_KP_1] = KEY_KP1, |
| 355 | [SDL_SCANCODE_KP_2] = KEY_KP2, |
| 356 | [SDL_SCANCODE_KP_3] = KEY_KP3, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 357 | [SDL_SCANCODE_KP_0] = KEY_KP0, |
| 358 | [SDL_SCANCODE_KP_PERIOD] = KEY_KPDOT, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 359 | /* key 84 does not exist linux_input.h */ |
| 360 | [SDL_SCANCODE_LANG5] = KEY_ZENKAKUHANKAKU, |
| 361 | [SDL_SCANCODE_NONUSBACKSLASH] = KEY_102ND, |
| 362 | [SDL_SCANCODE_F11] = KEY_F11, |
| 363 | [SDL_SCANCODE_F12] = KEY_F12, |
| 364 | [SDL_SCANCODE_INTERNATIONAL1] = KEY_RO, |
| 365 | [SDL_SCANCODE_LANG3] = KEY_KATAKANA, |
| 366 | [SDL_SCANCODE_LANG4] = KEY_HIRAGANA, |
| 367 | [SDL_SCANCODE_INTERNATIONAL4] = KEY_HENKAN, |
| 368 | [SDL_SCANCODE_INTERNATIONAL2] = KEY_KATAKANAHIRAGANA, |
| 369 | [SDL_SCANCODE_INTERNATIONAL5] = KEY_MUHENKAN, |
| 370 | /* [SDL_SCANCODE_INTERNATIONAL5] -> [KEY_KPJPCOMMA] */ |
| 371 | [SDL_SCANCODE_KP_ENTER] = KEY_KPENTER, |
| 372 | [SDL_SCANCODE_RCTRL] = KEY_RIGHTCTRL, |
| 373 | [SDL_SCANCODE_KP_DIVIDE] = KEY_KPSLASH, |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 374 | [SDL_SCANCODE_SYSREQ] = KEY_SYSRQ, |
Heinrich Schuchardt | c421621 | 2020-09-29 01:41:18 +0200 | [diff] [blame] | 375 | [SDL_SCANCODE_RALT] = KEY_RIGHTALT, |
| 376 | /* KEY_LINEFEED */ |
| 377 | [SDL_SCANCODE_HOME] = KEY_HOME, |
| 378 | [SDL_SCANCODE_UP] = KEY_UP, |
| 379 | [SDL_SCANCODE_PAGEUP] = KEY_PAGEUP, |
| 380 | [SDL_SCANCODE_LEFT] = KEY_LEFT, |
| 381 | [SDL_SCANCODE_RIGHT] = KEY_RIGHT, |
| 382 | [SDL_SCANCODE_END] = KEY_END, |
| 383 | [SDL_SCANCODE_DOWN] = KEY_DOWN, |
| 384 | [SDL_SCANCODE_PAGEDOWN] = KEY_PAGEDOWN, |
| 385 | [SDL_SCANCODE_INSERT] = KEY_INSERT, |
| 386 | [SDL_SCANCODE_DELETE] = KEY_DELETE, |
| 387 | /* KEY_MACRO */ |
| 388 | [SDL_SCANCODE_MUTE] = KEY_MUTE, |
| 389 | [SDL_SCANCODE_VOLUMEDOWN] = KEY_VOLUMEDOWN, |
| 390 | [SDL_SCANCODE_VOLUMEUP] = KEY_VOLUMEUP, |
| 391 | [SDL_SCANCODE_POWER] = KEY_POWER, |
| 392 | [SDL_SCANCODE_KP_EQUALS] = KEY_KPEQUAL, |
| 393 | [SDL_SCANCODE_KP_PLUSMINUS] = KEY_KPPLUSMINUS, |
| 394 | [SDL_SCANCODE_PAUSE] = KEY_PAUSE, |
| 395 | /* KEY_SCALE */ |
| 396 | [SDL_SCANCODE_KP_COMMA] = KEY_KPCOMMA, |
| 397 | [SDL_SCANCODE_LANG1] = KEY_HANGUEL, |
| 398 | [SDL_SCANCODE_LANG2] = KEY_HANJA, |
| 399 | [SDL_SCANCODE_INTERNATIONAL3] = KEY_YEN, |
| 400 | [SDL_SCANCODE_LGUI] = KEY_LEFTMETA, |
| 401 | [SDL_SCANCODE_RGUI] = KEY_RIGHTMETA, |
| 402 | [SDL_SCANCODE_APPLICATION] = KEY_COMPOSE, |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 403 | }; |
| 404 | |
| 405 | int sandbox_sdl_scan_keys(int key[], int max_keys) |
| 406 | { |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 407 | const Uint8 *keystate; |
| 408 | int num_keys; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 409 | int i, count; |
| 410 | |
| 411 | sandbox_sdl_poll_events(); |
Simon Glass | 96d0cd4 | 2020-02-03 07:36:12 -0700 | [diff] [blame] | 412 | keystate = SDL_GetKeyboardState(&num_keys); |
| 413 | for (i = count = 0; i < num_keys; i++) { |
| 414 | if (count < max_keys && keystate[i]) { |
| 415 | int keycode = sdl_to_keycode[i]; |
| 416 | |
| 417 | if (keycode) |
| 418 | key[count++] = keycode; |
| 419 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | return count; |
| 423 | } |
| 424 | |
| 425 | int sandbox_sdl_key_pressed(int keycode) |
| 426 | { |
| 427 | int key[8]; /* allow up to 8 keys to be pressed at once */ |
| 428 | int count; |
| 429 | int i; |
| 430 | |
| 431 | count = sandbox_sdl_scan_keys(key, sizeof(key) / sizeof(key[0])); |
| 432 | for (i = 0; i < count; i++) { |
| 433 | if (key[i] == keycode) |
| 434 | return 0; |
| 435 | } |
| 436 | |
| 437 | return -ENOENT; |
| 438 | } |
| 439 | |
| 440 | void sandbox_sdl_fill_audio(void *udata, Uint8 *stream, int len) |
| 441 | { |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 442 | struct buf_info *buf; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 443 | int avail; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 444 | int i; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 445 | |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 446 | for (i = 0; i < 2; i++) { |
| 447 | buf = &sdl.buf[sdl.cur_buf]; |
| 448 | avail = buf->size - buf->pos; |
| 449 | if (avail <= 0) { |
| 450 | sdl.cur_buf = 1 - sdl.cur_buf; |
| 451 | continue; |
| 452 | } |
| 453 | if (avail > len) |
| 454 | avail = len; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 455 | |
Heinrich Schuchardt | 304bc9f | 2022-12-04 20:48:57 +0100 | [diff] [blame] | 456 | memcpy(stream, buf->data + buf->pos, avail); |
| 457 | stream += avail; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 458 | buf->pos += avail; |
| 459 | len -= avail; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 460 | |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 461 | /* Move to next buffer if we are at the end */ |
| 462 | if (buf->pos == buf->size) |
| 463 | buf->size = 0; |
| 464 | else |
| 465 | break; |
| 466 | } |
Heinrich Schuchardt | 304bc9f | 2022-12-04 20:48:57 +0100 | [diff] [blame] | 467 | memset(stream, 0, len); |
| 468 | sdl.stopping = !!len; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 469 | } |
| 470 | |
Simon Glass | e221cdc | 2018-12-10 10:37:50 -0700 | [diff] [blame] | 471 | int sandbox_sdl_sound_init(int rate, int channels) |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 472 | { |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 473 | SDL_AudioSpec wanted, have; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 474 | int i; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 475 | |
| 476 | if (sandbox_sdl_ensure_init()) |
| 477 | return -1; |
| 478 | |
| 479 | if (sdl.audio_active) |
| 480 | return 0; |
| 481 | |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 482 | /* Set the audio format */ |
Simon Glass | e221cdc | 2018-12-10 10:37:50 -0700 | [diff] [blame] | 483 | wanted.freq = rate; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 484 | wanted.format = AUDIO_S16; |
Simon Glass | e221cdc | 2018-12-10 10:37:50 -0700 | [diff] [blame] | 485 | wanted.channels = channels; |
Heinrich Schuchardt | 304bc9f | 2022-12-04 20:48:57 +0100 | [diff] [blame] | 486 | wanted.samples = 960; /* Good low-latency value for callback */ |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 487 | wanted.callback = sandbox_sdl_fill_audio; |
| 488 | wanted.userdata = NULL; |
| 489 | |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 490 | for (i = 0; i < 2; i++) { |
| 491 | struct buf_info *buf = &sdl.buf[i]; |
| 492 | |
| 493 | buf->alloced = sizeof(uint16_t) * wanted.freq * wanted.channels; |
| 494 | buf->data = malloc(buf->alloced); |
| 495 | if (!buf->data) { |
| 496 | printf("%s: Out of memory\n", __func__); |
| 497 | if (i == 1) |
| 498 | free(sdl.buf[0].data); |
| 499 | return -1; |
| 500 | } |
| 501 | buf->pos = 0; |
| 502 | buf->size = 0; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 503 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 504 | |
| 505 | if (SDL_InitSubSystem(SDL_INIT_AUDIO) < 0) { |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 506 | printf("Unable to initialise SDL audio: %s\n", SDL_GetError()); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 507 | goto err; |
| 508 | } |
| 509 | |
| 510 | /* Open the audio device, forcing the desired format */ |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 511 | if (SDL_OpenAudio(&wanted, &have) < 0) { |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 512 | printf("Couldn't open audio: %s\n", SDL_GetError()); |
| 513 | goto err; |
| 514 | } |
Simon Glass | 5f736f8 | 2020-02-03 07:36:09 -0700 | [diff] [blame] | 515 | if (have.format != wanted.format) { |
| 516 | printf("Couldn't select required audio format\n"); |
| 517 | goto err; |
| 518 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 519 | sdl.audio_active = true; |
Simon Glass | 856b8f5 | 2018-11-15 19:56:14 -0700 | [diff] [blame] | 520 | sdl.sample_rate = wanted.freq; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 521 | sdl.cur_buf = 0; |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 522 | sdl.running = false; |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 523 | |
| 524 | return 0; |
| 525 | |
| 526 | err: |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 527 | for (i = 0; i < 2; i++) |
| 528 | free(sdl.buf[i].data); |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 529 | return -1; |
| 530 | } |
| 531 | |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 532 | int sandbox_sdl_sound_play(const void *data, uint size) |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 533 | { |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 534 | struct buf_info *buf; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 535 | |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 536 | if (!sdl.audio_active) |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 537 | return 0; |
| 538 | |
| 539 | buf = &sdl.buf[0]; |
| 540 | if (buf->size) |
| 541 | buf = &sdl.buf[1]; |
| 542 | while (buf->size) |
| 543 | usleep(1000); |
| 544 | |
| 545 | if (size > buf->alloced) |
| 546 | return -E2BIG; |
| 547 | |
| 548 | memcpy(buf->data, data, size); |
| 549 | buf->size = size; |
Simon Glass | e625b68 | 2018-12-10 10:37:35 -0700 | [diff] [blame] | 550 | buf->pos = 0; |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 551 | if (!sdl.running) { |
| 552 | SDL_PauseAudio(0); |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 553 | sdl.running = true; |
| 554 | sdl.stopping = false; |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 555 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 556 | |
| 557 | return 0; |
| 558 | } |
| 559 | |
| 560 | int sandbox_sdl_sound_stop(void) |
| 561 | { |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 562 | if (sdl.running) { |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 563 | while (!sdl.stopping) |
| 564 | SDL_Delay(100); |
| 565 | |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 566 | SDL_PauseAudio(1); |
| 567 | sdl.running = 0; |
Simon Glass | ac7b730 | 2020-02-03 07:36:10 -0700 | [diff] [blame] | 568 | sdl.stopping = false; |
Simon Glass | 282e29e | 2018-12-10 10:37:45 -0700 | [diff] [blame] | 569 | } |
Simon Glass | bbc09bf | 2014-02-27 13:26:17 -0700 | [diff] [blame] | 570 | |
| 571 | return 0; |
| 572 | } |