From 69d65257bb2122e721019095a2824cfa3167686b Mon Sep 17 00:00:00 2001 From: bjsylvia Date: Tue, 14 Nov 2023 15:26:28 +0800 Subject: [PATCH] feat(sdl): support indev LV_EVENT_DELETE (#4792) Signed-off-by: XiaoweiYan Signed-off-by: YanXiaowei Co-authored-by: XiaoweiYan --- src/dev/sdl/lv_sdl_keyboard.c | 15 +++++++++++++++ src/dev/sdl/lv_sdl_mouse.c | 15 +++++++++++++++ src/dev/sdl/lv_sdl_mousewheel.c | 15 +++++++++++++++ src/indev/lv_indev.c | 2 +- 4 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/dev/sdl/lv_sdl_keyboard.c b/src/dev/sdl/lv_sdl_keyboard.c index 0d803be6b..52ca7b90b 100644 --- a/src/dev/sdl/lv_sdl_keyboard.c +++ b/src/dev/sdl/lv_sdl_keyboard.c @@ -31,6 +31,7 @@ typedef struct { **********************/ static void sdl_keyboard_read(lv_indev_t * indev, lv_indev_data_t * data); static uint32_t keycode_to_ctrl_key(SDL_Keycode sdl_key); +static void release_indev_cb(lv_event_t * e); /********************** * STATIC VARIABLES @@ -57,6 +58,8 @@ lv_indev_t * lv_sdl_keyboard_create(void) lv_indev_set_read_cb(indev, sdl_keyboard_read); lv_indev_set_driver_data(indev, dsc); + lv_indev_add_event(indev, release_indev_cb, LV_EVENT_DELETE, indev); + return indev; } @@ -86,6 +89,18 @@ static void sdl_keyboard_read(lv_indev_t * indev, lv_indev_data_t * data) } } +static void release_indev_cb(lv_event_t * e) +{ + lv_indev_t * indev = (lv_indev_t *) lv_event_get_user_data(e); + lv_sdl_keyboard_t * dev = lv_indev_get_driver_data(indev); + if(dev) { + lv_indev_set_driver_data(indev, NULL); + lv_indev_set_read_cb(indev, NULL); + lv_free(dev); + LV_LOG_INFO("done"); + } +} + void _lv_sdl_keyboard_handler(SDL_Event * event) { uint32_t win_id = UINT32_MAX; diff --git a/src/dev/sdl/lv_sdl_mouse.c b/src/dev/sdl/lv_sdl_mouse.c index 47b34fb57..27856b31d 100644 --- a/src/dev/sdl/lv_sdl_mouse.c +++ b/src/dev/sdl/lv_sdl_mouse.c @@ -25,6 +25,7 @@ * STATIC PROTOTYPES **********************/ static void sdl_mouse_read(lv_indev_t * indev, lv_indev_data_t * data); +static void release_indev_cb(lv_event_t * e); /********************** * STATIC VARIABLES @@ -57,6 +58,8 @@ lv_indev_t * lv_sdl_mouse_create(void) lv_indev_set_read_cb(indev, sdl_mouse_read); lv_indev_set_driver_data(indev, dsc); + lv_indev_add_event(indev, release_indev_cb, LV_EVENT_DELETE, indev); + return indev; } @@ -74,6 +77,18 @@ static void sdl_mouse_read(lv_indev_t * indev, lv_indev_data_t * data) data->state = dsc->left_button_down ? LV_INDEV_STATE_PRESSED : LV_INDEV_STATE_RELEASED; } +static void release_indev_cb(lv_event_t * e) +{ + lv_indev_t * indev = (lv_indev_t *) lv_event_get_user_data(e); + lv_sdl_mouse_t * dsc = lv_indev_get_driver_data(indev); + if(dsc) { + lv_indev_set_driver_data(indev, NULL); + lv_indev_set_read_cb(indev, NULL); + lv_free(dsc); + LV_LOG_INFO("done"); + } +} + void _lv_sdl_mouse_handler(SDL_Event * event) { uint32_t win_id = UINT32_MAX; diff --git a/src/dev/sdl/lv_sdl_mousewheel.c b/src/dev/sdl/lv_sdl_mousewheel.c index ca687ac82..dd0216635 100644 --- a/src/dev/sdl/lv_sdl_mousewheel.c +++ b/src/dev/sdl/lv_sdl_mousewheel.c @@ -22,6 +22,7 @@ * STATIC PROTOTYPES **********************/ static void sdl_mousewheel_read(lv_indev_t * indev, lv_indev_data_t * data); +static void release_indev_cb(lv_event_t * e); /********************** * STATIC VARIABLES @@ -52,6 +53,8 @@ lv_indev_t * lv_sdl_mousewheel_create(void) lv_indev_set_read_cb(indev, sdl_mousewheel_read); lv_indev_set_driver_data(indev, dsc); + lv_indev_add_event(indev, release_indev_cb, LV_EVENT_DELETE, indev); + return indev; } @@ -68,6 +71,18 @@ static void sdl_mousewheel_read(lv_indev_t * indev, lv_indev_data_t * data) dsc->diff = 0; } +static void release_indev_cb(lv_event_t * e) +{ + lv_indev_t * indev = (lv_indev_t *) lv_event_get_user_data(e); + lv_sdl_mousewheel_t * dsc = lv_indev_get_driver_data(indev); + if(dsc) { + lv_indev_set_driver_data(indev, NULL); + lv_indev_set_read_cb(indev, NULL); + lv_free(dsc); + LV_LOG_INFO("done"); + } +} + void _lv_sdl_mousewheel_handler(SDL_Event * event) { uint32_t win_id = UINT32_MAX; diff --git a/src/indev/lv_indev.c b/src/indev/lv_indev.c index 948e85b38..202327b36 100644 --- a/src/indev/lv_indev.c +++ b/src/indev/lv_indev.c @@ -272,7 +272,7 @@ void lv_indev_set_type(lv_indev_t * indev, lv_indev_type_t indev_type) indev->reset_query = 1; } -void lv_indev_set_read_cb(lv_indev_t * indev, lv_indev_read_cb_t read_cb) +void lv_indev_set_read_cb(lv_indev_t * indev, lv_indev_read_cb_t read_cb) { if(indev == NULL) return;