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