Rockbox open source high quality audio player as a Music Player Daemon
mpris rockbox mpd libadwaita audio rust zig deno
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

sdl: make window resizable, enable high DPI

Tested on Linux, MacOS, and Windows.

On MacOS and Windows, we constrain the window's aspect
ratio by adjusting the size when responding to resize
events.

On Linux, I've not found a way to do so, that doesn't
result in fairly stuttery behavior and weird jumpy
behavior of the resize handle, possibly depending
on your window manager. So, black bars are displayed
around the content.
Maybe someone, at some point, finds a way.
(SDL3 seems to have SDL_SetWindowAspectRatio)

When the window is in fullscreen, black bars are
display necessarily, of course, on all systems,
unless the player GUI has exactly the same aspect
ratio as the screen...

Change-Id: I535e6617497611ea57a4c19e08e552f990859cfe

+127 -30
+5 -1
firmware/drivers/button_queue.c
··· 24 24 #include "button.h" 25 25 #ifdef HAVE_SDL 26 26 #include "button-sdl.h" 27 + #include "lcd-sdl.h" 27 28 #endif 28 29 29 30 static struct event_queue button_queue SHAREDBSS_ATTR; ··· 100 101 unsigned long curr_tick, remaining; 101 102 while(true) 102 103 { 103 - handle_sdl_events(); 104 + handle_sdl_events(); /* Includes window updates after resize events */ 104 105 queue_wait_w_tmo(&button_queue, evp, TIMEOUT_NOBLOCK); 105 106 if (evp->id != SYS_TIMEOUT || timeout == TIMEOUT_NOBLOCK) 106 107 return; ··· 117 118 } 118 119 #else 119 120 queue_wait_w_tmo(&button_queue, evp, timeout); 121 + #ifdef HAVE_SDL 122 + sdl_update_window(); /* Window may have been resized */ 123 + #endif 120 124 #endif 121 125 } 122 126 #endif /* HAVE_ADJUSTABLE_CPU_FREQ */
+12
firmware/target/hosted/sdl/button-sdl.c
··· 31 31 #include "backlight.h" 32 32 #include "system.h" 33 33 #include "button-sdl.h" 34 + #include "lcd-sdl.h" 34 35 #include "sim_tasks.h" 35 36 #include "buttonmap.h" 36 37 #include "debug.h" ··· 240 241 sdl_app_has_input_focus = 1; 241 242 else if (event->window.event == SDL_WINDOWEVENT_FOCUS_LOST) 242 243 sdl_app_has_input_focus = 0; 244 + else if(event->window.event == SDL_WINDOWEVENT_RESIZED) 245 + { 246 + sdl_window_needs_update(); 247 + #if !defined (__APPLE__) && !defined(__WIN32) 248 + static unsigned long last_tick; 249 + if (TIME_AFTER(current_tick, last_tick + HZ/20) && !button_queue_full()) 250 + button_queue_post(SDLK_UNKNOWN, 0); /* update window on main thread */ 251 + else 252 + last_tick = current_tick; 253 + #endif 254 + } 243 255 break; 244 256 case SDL_KEYDOWN: 245 257 case SDL_KEYUP:
+94 -7
firmware/target/hosted/sdl/lcd-sdl.c
··· 23 23 #include "lcd-sdl.h" 24 24 #include "sim-ui-defines.h" 25 25 #include "system.h" /* for MIN() and MAX() */ 26 + #include "misc.h" 26 27 27 28 double display_zoom = 1; 28 29 30 + static bool window_needs_update; 31 + 32 + void sdl_get_window_dimensions(int *w, int *h) 33 + { 34 + if (background) 35 + { 36 + *w = UI_WIDTH; 37 + *h = UI_HEIGHT; 38 + } 39 + else 40 + { 41 + #ifdef HAVE_REMOTE_LCD 42 + if (showremote) 43 + { 44 + *w = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH; 45 + *h = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT; 46 + } 47 + else 48 + #endif 49 + { 50 + *w = SIM_LCD_WIDTH; 51 + *h = SIM_LCD_HEIGHT; 52 + } 53 + } 54 + } 55 + 56 + static inline void sdl_render(void) 57 + { 58 + SDL_Texture *sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, gui_surface); 59 + SDL_RenderClear(sdlRenderer); 60 + SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); 61 + SDL_RenderPresent(sdlRenderer); 62 + SDL_DestroyTexture(sdlTexture); 63 + } 64 + 65 + 66 + #define SNAP_MARGIN 50 67 + int sdl_update_window(void) 68 + { 69 + if (!window_needs_update) 70 + return false; 71 + window_needs_update = false; 72 + 73 + #if defined (__APPLE__) || defined(__WIN32) /* Constrain aspect ratio */ 74 + if (!(SDL_GetWindowFlags(window) & (SDL_WINDOW_MAXIMIZED | SDL_WINDOW_FULLSCREEN))) 75 + { 76 + float aspect_ratio; 77 + int w, h; 78 + 79 + sdl_get_window_dimensions(&w, &h); 80 + aspect_ratio = (float) h / w; 81 + 82 + int original_height = h; 83 + int original_width = w; 84 + 85 + SDL_GetWindowSize(window, &w, &h); 86 + if (w != original_width || h != original_height) 87 + { 88 + if (abs(original_width - w) < SNAP_MARGIN) 89 + { 90 + w = original_width; 91 + h = original_height; 92 + } 93 + else 94 + h = w * aspect_ratio; 95 + 96 + SDL_SetWindowSize(window, w, h); 97 + } 98 + } 99 + #endif 100 + sdl_render(); 101 + return true; 102 + } 103 + 104 + void sdl_window_needs_update(void) 105 + { 106 + window_needs_update = true; 107 + 108 + /* For MacOS and Windows, we're on a main or 109 + display thread already, and can immediately 110 + update the window. 111 + On Linux, we have to defer the update, until 112 + it is handled by the main thread later. */ 113 + #if defined (__APPLE__) || defined(__WIN32) 114 + sdl_update_window(); 115 + #endif 116 + } 117 + 118 + 29 119 void sdl_update_rect(SDL_Surface *surface, int x_start, int y_start, int width, 30 120 int height, int max_x, int max_y, 31 121 unsigned long (*getpixel)(int, int)) ··· 117 207 118 208 uint8_t alpha; 119 209 if (SDL_GetSurfaceAlphaMod(surface,&alpha) == 0 && alpha < 255) 120 - SDL_FillRect(gui_surface, &dest, 0); 210 + SDL_FillRect(gui_surface, &dest, 0); /* alpha needs a black background */ 121 211 122 - SDL_BlitSurface(surface, &src, gui_surface, &dest); /* alpha needs a black background */ 212 + SDL_BlitSurface(surface, &src, gui_surface, &dest); 123 213 124 - SDL_Texture *sdlTexture = SDL_CreateTextureFromSurface(sdlRenderer, gui_surface); 125 - SDL_RenderClear(sdlRenderer); 126 - SDL_RenderCopy(sdlRenderer, sdlTexture, NULL, NULL); 127 - SDL_RenderPresent(sdlRenderer); 128 - SDL_DestroyTexture(sdlTexture); 214 + if (!sdl_update_window()) /* already calls sdl_render itself */ 215 + sdl_render(); 129 216 } 130 217 131 218 /* set a range of bitmap indices to a gradient from startcolour to endcolour */
+5
firmware/target/hosted/sdl/lcd-sdl.h
··· 28 28 /* Default display zoom level */ 29 29 extern SDL_Surface *gui_surface; 30 30 extern SDL_Renderer *sdlRenderer; 31 + extern SDL_Window *window; 32 + 33 + void sdl_get_window_dimensions(int *w, int *h); 34 + int sdl_update_window(void); 35 + void sdl_window_needs_update(void); 31 36 32 37 void sdl_update_rect(SDL_Surface *surface, int x_start, int y_start, int width, 33 38 int height, int max_x, int max_y,
+11 -22
firmware/target/hosted/sdl/system-sdl.c
··· 91 91 } 92 92 } 93 93 94 - /* Set things up */ 95 - if (background) 96 - { 97 - width = UI_WIDTH; 98 - height = UI_HEIGHT; 99 - } 100 - else 101 - { 102 - #ifdef HAVE_REMOTE_LCD 103 - if (showremote) 104 - { 105 - width = SIM_LCD_WIDTH > SIM_REMOTE_WIDTH ? SIM_LCD_WIDTH : SIM_REMOTE_WIDTH; 106 - height = SIM_LCD_HEIGHT + SIM_REMOTE_HEIGHT; 107 - } 108 - else 109 - #endif 110 - { 111 - width = SIM_LCD_WIDTH; 112 - height = SIM_LCD_HEIGHT; 113 - } 114 - } 115 - 94 + sdl_get_window_dimensions(&width, &height); 116 95 depth = LCD_DEPTH; 117 96 if (depth < 8) 118 97 depth = 16; ··· 122 101 flags |= SDL_WINDOW_FULLSCREEN; 123 102 #endif 124 103 104 + if (display_zoom == 1) 105 + flags |= SDL_WINDOW_RESIZABLE; 106 + 107 + flags |= SDL_WINDOW_ALLOW_HIGHDPI; 108 + 125 109 if ((window = SDL_CreateWindow(UI_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 126 110 width * display_zoom, height * display_zoom , flags)) == NULL) 127 111 panicf("%s", SDL_GetError()); 128 112 if ((sdlRenderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_PRESENTVSYNC)) == NULL) 129 113 panicf("%s", SDL_GetError()); 114 + 115 + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); 116 + SDL_RenderSetLogicalSize(sdlRenderer, width * display_zoom, height * display_zoom); 117 + 130 118 if ((gui_surface = SDL_CreateRGBSurface(0, width * display_zoom, height * display_zoom, depth, 131 119 0, 0, 0, 0)) == NULL) 132 120 panicf("%s", SDL_GetError()); ··· 152 140 static int sdl_event_thread(void * param) 153 141 { 154 142 #ifdef __WIN32 /* Fails on Linux and MacOS */ 143 + SDL_SetHint(SDL_HINT_WINDOWS_DPI_SCALING, "1"); 155 144 SDL_InitSubSystem(SDL_INIT_VIDEO); 156 145 sdl_window_setup(); 157 146 #endif