Implement posix app loading (#643)
- Added POSIX desktop support for SDK builds, application packaging, and integration testing. - Added POSIX filesystem partitions and improved application path handling. - Added simulator support for loading and running applications dynamically. - Improved simulator task stack handling and display startup reliability. - Improved simulator display scaling, resizing, high-DPI support, and pointer accuracy. - Fixed LVGL timers and input polling on POSIX. (fixes simulator with Linux on some Intel graphics platforms) - Standardized data paths across platforms. - Logging now works via separate task: this allows apps to write to log without it affecting their stdout (for apps that output text as relevant date for other apps, like the File Selection app) - Fixes for app stdio
This commit is contained in:
committed by
GitHub
parent
d3656bcd3d
commit
643cbc3806
@@ -15,50 +15,46 @@ constexpr auto* TAG = "SdlDisplay";
|
||||
#define GET_CONFIG(device) (static_cast<const SdlDisplayConfig*>((device)->config))
|
||||
|
||||
struct SdlDisplayInternal {
|
||||
bool initialized;
|
||||
bool init_failed;
|
||||
SDL_Window* window;
|
||||
SDL_Renderer* renderer;
|
||||
SDL_Texture* texture;
|
||||
};
|
||||
|
||||
// Only one sdl-display device exists in the simulator; sdl_input.cpp uses this to map window
|
||||
// coordinates back to the fixed logical resolution SDL_RenderSetLogicalSize() scales to the window.
|
||||
static SdlDisplayInternal* g_display_internal = nullptr;
|
||||
|
||||
SDL_Renderer* sdl_display_get_renderer(void) {
|
||||
return g_display_internal != nullptr ? g_display_internal->renderer : nullptr;
|
||||
}
|
||||
|
||||
// Re-blits the already-drawn texture at the renderer's current (possibly just-resized) scale.
|
||||
// No new pixel data needed: the window resizing doesn't change what LVGL last rendered, only how
|
||||
// large it should appear, and SDL only applies that until the next SDL_RenderPresent() call.
|
||||
void sdl_display_present_now(void) {
|
||||
if (g_display_internal == nullptr) {
|
||||
return;
|
||||
}
|
||||
SDL_RenderClear(g_display_internal->renderer);
|
||||
SDL_RenderCopy(g_display_internal->renderer, g_display_internal->texture, nullptr, nullptr);
|
||||
SDL_RenderPresent(g_display_internal->renderer);
|
||||
}
|
||||
|
||||
// region Driver lifecycle
|
||||
|
||||
static error_t start(Device* device) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
auto* internal = static_cast<SdlDisplayInternal*>(malloc(sizeof(SdlDisplayInternal)));
|
||||
if (internal == nullptr) {
|
||||
return ERROR_OUT_OF_MEMORY;
|
||||
}
|
||||
|
||||
if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
|
||||
LOG_E(TAG, "SDL_InitSubSystem failed: %s", SDL_GetError());
|
||||
free(internal);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
internal->window = SDL_CreateWindow(
|
||||
"Tactility",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
config->horizontal_resolution, config->vertical_resolution,
|
||||
SDL_WINDOW_SHOWN
|
||||
);
|
||||
internal->renderer = internal->window != nullptr
|
||||
? SDL_CreateRenderer(internal->window, -1, SDL_RENDERER_ACCELERATED)
|
||||
: nullptr;
|
||||
internal->texture = internal->renderer != nullptr
|
||||
? SDL_CreateTexture(internal->renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING,
|
||||
config->horizontal_resolution, config->vertical_resolution)
|
||||
: nullptr;
|
||||
|
||||
if (internal->window == nullptr || internal->renderer == nullptr || internal->texture == nullptr) {
|
||||
LOG_E(TAG, "Failed to create SDL window: %s", SDL_GetError());
|
||||
if (internal->texture != nullptr) SDL_DestroyTexture(internal->texture);
|
||||
if (internal->renderer != nullptr) SDL_DestroyRenderer(internal->renderer);
|
||||
if (internal->window != nullptr) SDL_DestroyWindow(internal->window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||
free(internal);
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
internal->initialized = false;
|
||||
internal->init_failed = false;
|
||||
internal->window = nullptr;
|
||||
internal->renderer = nullptr;
|
||||
internal->texture = nullptr;
|
||||
|
||||
device_set_driver_data(device, internal);
|
||||
return ERROR_NONE;
|
||||
@@ -67,10 +63,16 @@ static error_t start(Device* device) {
|
||||
static error_t stop(Device* device) {
|
||||
auto* internal = static_cast<SdlDisplayInternal*>(device_get_driver_data(device));
|
||||
|
||||
SDL_DestroyTexture(internal->texture);
|
||||
SDL_DestroyRenderer(internal->renderer);
|
||||
SDL_DestroyWindow(internal->window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||
if (internal->initialized) {
|
||||
SDL_DestroyTexture(internal->texture);
|
||||
SDL_DestroyRenderer(internal->renderer);
|
||||
SDL_DestroyWindow(internal->window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||
}
|
||||
|
||||
if (g_display_internal == internal) {
|
||||
g_display_internal = nullptr;
|
||||
}
|
||||
|
||||
free(internal);
|
||||
device_set_driver_data(device, nullptr);
|
||||
@@ -84,18 +86,83 @@ static error_t stop(Device* device) {
|
||||
static error_t sdl_display_reset(Device*) { return ERROR_NONE; }
|
||||
static error_t sdl_display_init(Device*) { return ERROR_NONE; }
|
||||
|
||||
static float sdl_display_get_dpi_scale() {
|
||||
float hdpi = 96.0f;
|
||||
if (SDL_GetDisplayDPI(0, nullptr, &hdpi, nullptr) != 0 || hdpi <= 0.0f) {
|
||||
return 1.0f;
|
||||
}
|
||||
return hdpi / 96.0f;
|
||||
}
|
||||
|
||||
static bool sdl_display_lazy_init(Device* device, SdlDisplayInternal* internal) {
|
||||
const auto* config = GET_CONFIG(device);
|
||||
|
||||
if (SDL_InitSubSystem(SDL_INIT_VIDEO) != 0) {
|
||||
LOG_E(TAG, "SDL_InitSubSystem failed: %s", SDL_GetError());
|
||||
return false;
|
||||
}
|
||||
|
||||
// Only the window's initial on-screen footprint scales here - the render/logical resolution
|
||||
// (config->horizontal_resolution/vertical_resolution, used below for the texture and
|
||||
// SDL_RenderSetLogicalSize()) is unaffected, same as any other resize the user does by hand.
|
||||
const float dpi_scale = sdl_display_get_dpi_scale();
|
||||
const int initial_width = static_cast<int>(config->horizontal_resolution * dpi_scale);
|
||||
const int initial_height = static_cast<int>(config->vertical_resolution * dpi_scale);
|
||||
|
||||
internal->window = SDL_CreateWindow(
|
||||
"Tactility",
|
||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||
initial_width, initial_height,
|
||||
SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI
|
||||
);
|
||||
internal->renderer = internal->window != nullptr
|
||||
? SDL_CreateRenderer(internal->window, -1, SDL_RENDERER_ACCELERATED)
|
||||
: nullptr;
|
||||
// Lets the window be resized freely while the renderer scales/letterboxes the fixed-resolution
|
||||
// texture below to fit - LVGL keeps rendering at horizontal_resolution x vertical_resolution.
|
||||
if (internal->renderer != nullptr) {
|
||||
SDL_RenderSetLogicalSize(internal->renderer, config->horizontal_resolution, config->vertical_resolution);
|
||||
}
|
||||
internal->texture = internal->renderer != nullptr
|
||||
? SDL_CreateTexture(internal->renderer, SDL_PIXELFORMAT_RGB565, SDL_TEXTUREACCESS_STREAMING,
|
||||
config->horizontal_resolution, config->vertical_resolution)
|
||||
: nullptr;
|
||||
|
||||
if (internal->window == nullptr || internal->renderer == nullptr || internal->texture == nullptr) {
|
||||
LOG_E(TAG, "Failed to create SDL window: %s", SDL_GetError());
|
||||
if (internal->texture != nullptr) SDL_DestroyTexture(internal->texture);
|
||||
if (internal->renderer != nullptr) SDL_DestroyRenderer(internal->renderer);
|
||||
if (internal->window != nullptr) SDL_DestroyWindow(internal->window);
|
||||
SDL_QuitSubSystem(SDL_INIT_VIDEO);
|
||||
return false;
|
||||
}
|
||||
|
||||
g_display_internal = internal;
|
||||
return true;
|
||||
}
|
||||
|
||||
static error_t sdl_display_draw_bitmap(Device* device, int32_t x_start, int32_t y_start, int32_t x_end, int32_t y_end, const void* color_data) {
|
||||
auto* internal = static_cast<SdlDisplayInternal*>(device_get_driver_data(device));
|
||||
|
||||
if (internal->init_failed) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
if (!internal->initialized) {
|
||||
if (!sdl_display_lazy_init(device, internal)) {
|
||||
internal->init_failed = true;
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
internal->initialized = true;
|
||||
}
|
||||
|
||||
SDL_Rect rect = { x_start, y_start, x_end - x_start, y_end - y_start };
|
||||
// RGB565 = 2 bytes/pixel.
|
||||
if (SDL_UpdateTexture(internal->texture, &rect, color_data, (x_end - x_start) * 2) != 0) {
|
||||
return ERROR_RESOURCE;
|
||||
}
|
||||
|
||||
SDL_RenderClear(internal->renderer);
|
||||
SDL_RenderCopy(internal->renderer, internal->texture, nullptr, nullptr);
|
||||
SDL_RenderPresent(internal->renderer);
|
||||
sdl_display_present_now();
|
||||
return ERROR_NONE;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -12,6 +14,22 @@ struct SdlDisplayConfig {
|
||||
uint16_t vertical_resolution;
|
||||
};
|
||||
|
||||
/**
|
||||
* @return the display's renderer, or NULL if the display hasn't drawn its first frame yet
|
||||
* (see sdl_display_lazy_init() in sdl_display.cpp). Used by sdl_input.cpp to map window
|
||||
* coordinates back to the fixed logical resolution the renderer scales to fit the window.
|
||||
*/
|
||||
SDL_Renderer* sdl_display_get_renderer(void);
|
||||
|
||||
/**
|
||||
* @brief Re-presents the already-drawn frame at the renderer's current scale. Call this when the
|
||||
* window is resized: the window size change alone doesn't make SDL re-blit the last frame at
|
||||
* the new scale until something calls SDL_RenderPresent() again, and LVGL won't do that on
|
||||
* its own since nothing it's tracking actually changed. No-op if the display hasn't drawn its
|
||||
* first frame yet.
|
||||
*/
|
||||
void sdl_display_present_now(void);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
#include "sdl_input.h"
|
||||
#include "sdl_display.h"
|
||||
|
||||
#include <tactility/drivers/keyboard.h>
|
||||
|
||||
@@ -34,6 +35,23 @@ void push_key(uint32_t key) {
|
||||
// all of these back to LVGL's own sentinels (CODEPOINT_ESCAPE/BACKSPACE/DELETE already equal their
|
||||
// LV_KEY_* counterpart numerically, so no translation is needed for those). Printable characters
|
||||
// arrive separately via SDL_TEXTINPUT.
|
||||
// The window can be freely resized (see sdl_display.cpp's SDL_WINDOW_RESIZABLE/
|
||||
// SDL_RenderSetLogicalSize()), so raw SDL mouse coordinates are in window-pixel space, not the
|
||||
// fixed logical resolution LVGL renders at. SDL_RenderWindowToLogical() is the renderer's own
|
||||
// inverse of that scaling, accounting for both the scale factor and any letterbox offset.
|
||||
void set_pointer_position(int32_t window_x, int32_t window_y) {
|
||||
SDL_Renderer* renderer = sdl_display_get_renderer();
|
||||
if (renderer == nullptr) {
|
||||
pointer_state.x = window_x;
|
||||
pointer_state.y = window_y;
|
||||
return;
|
||||
}
|
||||
float logical_x, logical_y;
|
||||
SDL_RenderWindowToLogical(renderer, window_x, window_y, &logical_x, &logical_y);
|
||||
pointer_state.x = static_cast<int32_t>(logical_x);
|
||||
pointer_state.y = static_cast<int32_t>(logical_y);
|
||||
}
|
||||
|
||||
uint32_t keycode_to_key(SDL_Keycode sdl_key, bool shift) {
|
||||
switch (sdl_key) {
|
||||
case SDLK_RIGHT: return CODEPOINT_ARROW_RIGHT;
|
||||
@@ -64,13 +82,17 @@ void sdl_input_pump() {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
switch (event.type) {
|
||||
case SDL_MOUSEMOTION:
|
||||
pointer_state.x = event.motion.x;
|
||||
pointer_state.y = event.motion.y;
|
||||
set_pointer_position(event.motion.x, event.motion.y);
|
||||
break;
|
||||
case SDL_MOUSEBUTTONDOWN:
|
||||
if (event.button.button == SDL_BUTTON_LEFT) {
|
||||
pointer_state.x = event.button.x;
|
||||
pointer_state.y = event.button.y;
|
||||
// event.button.x/y can be stale immediately after a window resize (an
|
||||
// SDL/X11 event-queue quirk - confirmed by comparing against a live
|
||||
// SDL_GetWindowSize() at the same instant). SDL_GetMouseState() queries the
|
||||
// OS for the current pointer position directly, sidestepping that entirely.
|
||||
int live_x, live_y;
|
||||
SDL_GetMouseState(&live_x, &live_y);
|
||||
set_pointer_position(live_x, live_y);
|
||||
pointer_state.pressed = true;
|
||||
}
|
||||
break;
|
||||
@@ -86,6 +108,14 @@ void sdl_input_pump() {
|
||||
// ASCII only (first byte of event.text.text) - sufficient for a simulator keyboard.
|
||||
push_key(static_cast<uint8_t>(event.text.text[0]));
|
||||
break;
|
||||
case SDL_WINDOWEVENT:
|
||||
// Resizing doesn't change what LVGL last rendered, only how large it should
|
||||
// appear - re-present the existing frame at the new scale immediately, rather
|
||||
// than leaving stale-looking content on screen until the next LVGL-driven flush.
|
||||
if (event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) {
|
||||
sdl_display_present_now();
|
||||
}
|
||||
break;
|
||||
case SDL_QUIT:
|
||||
exit(0);
|
||||
default:
|
||||
|
||||
@@ -29,7 +29,7 @@ static Driver* const simulator_drivers[] = {
|
||||
// These devices have no real bus to attach to (SDL has no notion of one), but every non-root
|
||||
// device is still expected to have a parent (see Device::parent) - they're parented to root once
|
||||
// it's available below.
|
||||
static const SdlDisplayConfig sdl_display_config = { 320, 240 };
|
||||
static const SdlDisplayConfig sdl_display_config = { 640, 480 };
|
||||
static Device sdl_display_device {};
|
||||
static Device sdl_pointer_device {};
|
||||
static Device sdl_keyboard_device {};
|
||||
|
||||
Reference in New Issue
Block a user