lvglをlinux上で手軽に確認

# 目的

実機環境やクロスコンパイル環境を用意せず
Linux上で手軽に [LVGL](https://lvgl.io/) の動作確認をする


# 確認した環境

- fedora(rawhide: 20250304) + xfce




# 確認したcommit id

| repository | commit id |
|-|-|
| [lv_port_linux](https://github.com/lvgl/lv_port_linux.git) |`b5b3c41998820768792eedc619f9836435c8d354` | 
| [lvgl](https://github.com/lvgl/lvgl.git) | `5c5ae5d2bc9b3626bfed246a8533a1b1a3aeba20`  |

# 手順

## 事前準備

SDLを利用する場合

```shell
sudo yum install -y libevdev-devel.x86_64 SDL2_image-devel.x86_64
```

fbdevなどを使用するひとはrpm無いので自分でコンパイルする必要あると思う

## ダウンロード

```shell
git clone --recursive --depth=1 https://github.com/lvgl/lv_port_linux.git
```

## パッチ

以下を適当な名前(例えばsdl2.patch)で保存して

<details>
<summary>パッチ</summary>

```diff
diff --git a/Makefile b/Makefile
index 7ccd1a6..3ae9f5e 100644
--- a/Makefile
+++ b/Makefile
@@ -11,8 +11,8 @@ WARNINGS		:= -Wall -Wshadow -Wundef -Wmissing-prototypes -Wno-discarded-qualifie
 					-Wsizeof-pointer-memaccess -Wno-format-nonliteral -Wno-cast-qual -Wunreachable-code -Wno-switch-default -Wreturn-type -Wmultichar -Wformat-security \
 					-Wno-ignored-qualifiers -Wno-error=pedantic -Wno-sign-compare -Wno-error=missing-prototypes -Wdouble-promotion -Wclobbered -Wdeprecated -Wempty-body \
 					-Wshift-negative-value -Wstack-usage=2048 -Wno-unused-value -std=gnu99
-CFLAGS 			?= -O3 -g0 -I$(LVGL_DIR)/ $(WARNINGS)
-LDFLAGS 		?= -lm -lstdc++
+CFLAGS 			?= -O3 -g0 -I$(LVGL_DIR)/ -Isrc/lib $(WARNINGS)
+LDFLAGS 		?= -lm -lstdc++ -lSDL2 -lSDL2_image
 BIN 			= main
 BUILD_DIR 		= ./build
 BUILD_OBJ_DIR 	= $(BUILD_DIR)/obj
@@ -22,11 +22,10 @@ prefix 			?= /usr
 bindir 			?= $(prefix)/bin
 
 #Collect the files to compile
-MAINSRC          = ./main.c
+MAINSRC          =
 
 include $(LVGL_DIR)/lvgl/lvgl.mk
-
-CSRCS 			+=$(LVGL_DIR)/mouse_cursor_icon.c 
+CSRCS			+= $(shell find src -type f -name '*.c')
 
 OBJEXT 			?= .o
 
diff --git a/lv_conf.h b/lv_conf.h
index adb7053..911a738 100644
--- a/lv_conf.h
+++ b/lv_conf.h
@@ -299,7 +299,7 @@
 #define LV_USE_DRAW_DAVE2D 0
 
 /** Draw using cached SDL textures*/
-#define LV_USE_DRAW_SDL 0
+#define LV_USE_DRAW_SDL 1
 
 /** Use VG-Lite GPU. */
 #define LV_USE_DRAW_VG_LITE 0
@@ -1142,7 +1142,7 @@
  *==================*/
 
 /** Use SDL to open window on PC and handle mouse and keyboard. */
-#define LV_USE_SDL              0
+#define LV_USE_SDL              1
 #if LV_USE_SDL
     #define LV_SDL_INCLUDE_PATH     <SDL2/SDL.h>
     #define LV_SDL_RENDER_MODE      LV_DISPLAY_RENDER_MODE_DIRECT   /**< LV_DISPLAY_RENDER_MODE_DIRECT is recommended for best performance */
@@ -1172,7 +1172,7 @@
 #endif
 
 /** Driver for /dev/fb */
-#define LV_USE_LINUX_FBDEV      1
+#define LV_USE_LINUX_FBDEV      0
 #if LV_USE_LINUX_FBDEV
     #define LV_LINUX_FBDEV_BSD           0
     #define LV_LINUX_FBDEV_RENDER_MODE   LV_DISPLAY_RENDER_MODE_PARTIAL
Submodule lvgl contains modified content
diff --git a/lvgl/env_support/cmake/custom.cmake b/lvgl/env_support/cmake/custom.cmake
index c87e78d23..0572e19f6 100644
--- a/lvgl/env_support/cmake/custom.cmake
+++ b/lvgl/env_support/cmake/custom.cmake
@@ -86,7 +86,7 @@ if(NOT "${ret}" STREQUAL "0")
 endif()
 
 # This will set all CONFIG_LV_USE_* variables in cmake
-include(${CMAKE_BINARY_DIR}/lv_conf.cmake)
+include(${CMAKE_BINARY_DIR}/lvgl/lv_conf.cmake)
 
 # Add definition of LV_CONF_PATH only if needed
 # Do not redefine it if already defined in tests/CMakeLists.txt
diff --git a/src/lib/display_backends/fbdev.c b/src/lib/display_backends/fbdev.c
index 500e0c9..f980cbd 100644
--- a/src/lib/display_backends/fbdev.c
+++ b/src/lib/display_backends/fbdev.c
@@ -20,7 +20,7 @@
 #include <stdbool.h>
 
 #include "lvgl/lvgl.h"
-#if LV_USE_FBDEV
+#if LV_USE_FBDEV || LV_USE_LINUX_FBDEV
 #include "simulator_util.h"
 #include "backends.h"
 
```

</details>

パッチをあてる

```shell
patch -p1 < sdl2.patch
```


## build

```
make
```

## 実行

```shell
./build/bin/main
```

## 修正と確認

src/main.cを修正することで
exampleなども表示出来る

公式の[Simple window](https://docs.lvgl.io/master/examples.html#window)を表示したい場合の差分

```diff
diff --git a/src/main.c b/src/main.c
index 90909d0..ce0e33a 100644
--- a/src/main.c
+++ b/src/main.c
@@ -124,6 +124,8 @@ static void configure_simulator(int argc, char **argv)
     }
 }
 
+extern void lv_example_win_1(void);
+
 /**
  * @brief entry point
  * @description start a demo
@@ -151,8 +153,9 @@ int main(int argc, char **argv)
 #endif
 
     /*Create a Demo*/
-    lv_demo_widgets();
-    lv_demo_widgets_start_slideshow();
+    lv_example_win_1();
+//    lv_demo_widgets();
+//    lv_demo_widgets_start_slideshow();
 
     /* Enter the run loop of the selected backend */
     driver_backends_run_loop();

```

もう一回 `make` 打って `build/bin/main` 実行すると

こんな感じで表示される

![image.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/3786381/5cf5df04-f5b6-4a75-9568-c937c59d8c95.png)

# その他

windowsでも [VcXsrv](https://vcxsrv.com/) や [Xming](https://sourceforge.net/projects/xming/) 使えば表示出来るのかもしれない(未確認)