From 1dd3c6de9073d8b5c68e8601276fd05f9facfdc3 Mon Sep 17 00:00:00 2001 From: Hannah_GBS <7504779+Hannah-GBS@users.noreply.github.com> Date: Wed, 4 Jun 2025 10:49:32 +0100 Subject: [PATCH] [sdl] Add config for SDL window flags (#8998) Co-authored-by: Clyde Stubbs <2366188+clydebarrow@users.noreply.github.com> --- esphome/components/sdl/display.py | 40 ++++++++++++++++++++++++++ esphome/components/sdl/sdl_esphome.cpp | 4 +-- esphome/components/sdl/sdl_esphome.h | 8 ++++++ 3 files changed, 50 insertions(+), 2 deletions(-) diff --git a/esphome/components/sdl/display.py b/esphome/components/sdl/display.py index 66709cc834..ae8b0fd43a 100644 --- a/esphome/components/sdl/display.py +++ b/esphome/components/sdl/display.py @@ -8,16 +8,28 @@ from esphome.const import ( CONF_HEIGHT, CONF_ID, CONF_LAMBDA, + CONF_POSITION, CONF_WIDTH, + CONF_X, + CONF_Y, PLATFORM_HOST, ) sdl_ns = cg.esphome_ns.namespace("sdl") Sdl = sdl_ns.class_("Sdl", display.Display, cg.Component) +sdl_window_flags = cg.global_ns.enum("SDL_WindowFlags") CONF_SDL_OPTIONS = "sdl_options" CONF_SDL_ID = "sdl_id" +CONF_WINDOW_OPTIONS = "window_options" +WINDOW_OPTIONS = ( + "borderless", + "always_on_top", + "fullscreen", + "skip_taskbar", + "resizable", +) def get_sdl_options(value): @@ -29,6 +41,10 @@ def get_sdl_options(value): raise cv.Invalid("Unable to run sdl2-config - have you installed sdl2?") from e +def get_window_options(): + return {cv.Optional(option, default=False): cv.boolean for option in WINDOW_OPTIONS} + + CONFIG_SCHEMA = cv.All( display.FULL_DISPLAY_SCHEMA.extend( cv.Schema( @@ -44,6 +60,17 @@ CONFIG_SCHEMA = cv.All( } ), ), + cv.Optional(CONF_WINDOW_OPTIONS): cv.Schema( + { + cv.Optional(CONF_POSITION): cv.Schema( + { + cv.Required(CONF_X): cv.int_, + cv.Required(CONF_Y): cv.int_, + } + ), + **get_window_options(), + } + ), } ) ), @@ -65,6 +92,19 @@ async def to_code(config): (width, height) = dimensions cg.add(var.set_dimensions(width, height)) + if window_options := config.get(CONF_WINDOW_OPTIONS): + create_flags = 0 + for option in WINDOW_OPTIONS: + value = window_options.get(option, False) + if value: + create_flags = create_flags | getattr( + sdl_window_flags, "SDL_WINDOW_" + option.upper() + ) + cg.add(var.set_window_options(create_flags)) + + if position := window_options.get(CONF_POSITION): + cg.add(var.set_position(position[CONF_X], position[CONF_Y])) + if lamb := config.get(CONF_LAMBDA): lambda_ = await cg.process_lambda( lamb, [(display.DisplayRef, "it")], return_type=cg.void diff --git a/esphome/components/sdl/sdl_esphome.cpp b/esphome/components/sdl/sdl_esphome.cpp index 42dfe687e9..e55bff58fe 100644 --- a/esphome/components/sdl/sdl_esphome.cpp +++ b/esphome/components/sdl/sdl_esphome.cpp @@ -8,8 +8,8 @@ namespace sdl { void Sdl::setup() { ESP_LOGD(TAG, "Starting setup"); SDL_Init(SDL_INIT_VIDEO); - this->window_ = SDL_CreateWindow(App.get_name().c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, - this->width_, this->height_, SDL_WINDOW_RESIZABLE); + this->window_ = SDL_CreateWindow(App.get_name().c_str(), this->pos_x_, this->pos_y_, this->width_, this->height_, + this->window_options_); this->renderer_ = SDL_CreateRenderer(this->window_, -1, SDL_RENDERER_SOFTWARE); SDL_RenderSetLogicalSize(this->renderer_, this->width_, this->height_); this->texture_ = diff --git a/esphome/components/sdl/sdl_esphome.h b/esphome/components/sdl/sdl_esphome.h index 39ea3ed417..bf5fde1428 100644 --- a/esphome/components/sdl/sdl_esphome.h +++ b/esphome/components/sdl/sdl_esphome.h @@ -28,6 +28,11 @@ class Sdl : public display::Display { this->width_ = width; this->height_ = height; } + void set_window_options(uint32_t window_options) { this->window_options_ = window_options; } + void set_position(uint16_t pos_x, uint16_t pos_y) { + this->pos_x_ = pos_x; + this->pos_y_ = pos_y; + } int get_width() override { return this->width_; } int get_height() override { return this->height_; } float get_setup_priority() const override { return setup_priority::HARDWARE; } @@ -49,6 +54,9 @@ class Sdl : public display::Display { void redraw_(SDL_Rect &rect); int width_{}; int height_{}; + uint32_t window_options_{0}; + int pos_x_{SDL_WINDOWPOS_UNDEFINED}; + int pos_y_{SDL_WINDOWPOS_UNDEFINED}; SDL_Renderer *renderer_{}; SDL_Window *window_{}; SDL_Texture *texture_{};