diff --git a/docs/src/details/integration/driver/index.rst b/docs/src/details/integration/driver/index.rst index b34f962f62..370f1b49ca 100644 --- a/docs/src/details/integration/driver/index.rst +++ b/docs/src/details/integration/driver/index.rst @@ -15,3 +15,4 @@ Drivers windows X11 uefi + sdl diff --git a/docs/src/details/integration/driver/sdl.rst b/docs/src/details/integration/driver/sdl.rst new file mode 100644 index 0000000000..d05879711c --- /dev/null +++ b/docs/src/details/integration/driver/sdl.rst @@ -0,0 +1,106 @@ +.. _sdl_driver: + +=============================== +SDL Driver +=============================== + +Overview +-------- + +| SDL (Simple DirectMedia Layer) provides a cross-platform way to handle graphics, input, and multimedia, making it an excellent choice for running LVGL applications on a PC. + + +Prerequisites +------------- + +Install SDL according to your platform. + Linux ``sudo apt install libsdl2-dev`` + MacOS ``brew install sdl2`` + Windows https://github.com/libsdl-org/SDL/releases + + +Configure SDL Driver +----------------------- + +1. Required linked libraries: -lSDL2 +2. Enable SDL driver support in lv_conf.h, CMake compiler definitions or KConfig. + .. code-block:: c + + #define LV_USE_SDL 1 + #define LV_SDL_INCLUDE_PATH + #define SDL_HOR_RES 400 + #define SDL_VER_RES 400 + +Basic Usage +----------- + +.. code-block:: c + + #include + #define SDL_MAIN_HANDLED /*To fix SDL's "undefined reference to WinMain" issue*/ + #include + #include "drivers/sdl/lv_sdl_mouse.h" + #include "drivers/sdl/lv_sdl_mousewheel.h" + #include "drivers/sdl/lv_sdl_keyboard.h" + + static lv_display_t *lvDisplay; + static lv_indev_t *lvMouse; + static lv_indev_t *lvMouseWheel; + static lv_indev_t *lvKeyboard; + + #if LV_USE_LOG != 0 + static void lv_log_print_g_cb(lv_log_level_t level, const char * buf) + { + LV_UNUSED(level); + LV_UNUSED(buf); + } + #endif + + int main() + { + /* initialize lvgl */ + lv_init(); + + // Workaround for sdl2 `-m32` crash + // https://bugs.launchpad.net/ubuntu/+source/libsdl2/+bug/1775067/comments/7 + #ifndef WIN32 + setenv("DBUS_FATAL_WARNINGS", "0", 1); + #endif + + /* Register the log print callback */ + #if LV_USE_LOG != 0 + lv_log_register_print_cb(lv_log_print_g_cb); + #endif + + /* Add a display + * Use the 'monitor' driver which creates window on PC's monitor to simulate a display*/ + + lvDisplay = lv_sdl_window_create(SDL_HOR_RES, SDL_VER_RES); + lvMouse = lv_sdl_mouse_create(); + lvMouseWheel = lv_sdl_mousewheel_create(); + lvKeyboard = lv_sdl_keyboard_create(); + + /* create Widgets on the screen */ + lv_demo_widgets(); + + + Uint32 lastTick = SDL_GetTicks(); + while(1) { + SDL_Delay(5); + Uint32 current = SDL_GetTicks(); + lv_tick_inc(current - lastTick); // Update the tick timer. Tick is new for LVGL 9 + lastTick = current; + lv_timer_handler(); // Update the UI- + } + + return 0; + } + +Using an IDE +------------ + +LVGL with SDL has been ported to various IDEs. + +- `Eclipse with SDL driver `__: Recommended on Linux and Mac, supports CMake as well +- `VSCode with SDL driver `__: Recommended on Linux (SDL) and Mac (SDL) +- `Generic Linux `__: CMake based project where you can easily switch between fbdev, DRM, and SDL.