From 991ee18148b815a95c8a59a91eb0c9263311aa7a Mon Sep 17 00:00:00 2001 From: lixianjing Date: Thu, 28 Nov 2024 18:25:25 +0800 Subject: [PATCH] forward SDL_TouchFingerEvent to window(touch_event_t) --- docs/changes.md | 1 + src/base/events.c | 26 +++++++++++ src/base/events.h | 84 +++++++++++++++++++++++++++++++++++ src/main_loop/main_loop_sdl.c | 47 +++++++++++++++++++- 4 files changed, 157 insertions(+), 1 deletion(-) diff --git a/docs/changes.md b/docs/changes.md index f999e0c620..55489b6673 100644 --- a/docs/changes.md +++ b/docs/changes.md @@ -3,6 +3,7 @@ 2024/11/28 * 增加函数 bitmap\_set\_dirty/bitmap\_is\_dirty * 重构bitmap,用bitmap\_init\_ex 实现bitmap\_create\_ex + * 将SDL_TouchFingerEvent转发到top windows 2024/11/26 * 增加tab\_button\_group删除tab\_button和对应page的函数(感谢智明提供补丁) diff --git a/src/base/events.c b/src/base/events.c index fbed5a2de4..c21262b99e 100644 --- a/src/base/events.c +++ b/src/base/events.c @@ -487,6 +487,32 @@ event_t* system_event_init(system_event_t* event, void* target, void* sdl_event) return (event_t*)event; } +touch_event_t* touch_event_cast(event_t* event) { + return_value_if_fail(event != NULL, NULL); + return_value_if_fail( + event->type == EVT_TOUCH_DOWN || event->type == EVT_TOUCH_UP || event->type == EVT_TOUCH_MOVE, + NULL); + return_value_if_fail(event->size == sizeof(touch_event_t), NULL); + + return (touch_event_t*)event; +} + +event_t* touch_event_init(touch_event_t* event, uint32_t type, void* target, int64_t touch_id, + int64_t finger_id, float x, float y, float pressure) { + return_value_if_fail(event != NULL, NULL); + memset(event, 0x00, sizeof(touch_event_t)); + + event->e = event_init(type, target); + event->e.size = sizeof(*event); + event->touch_id = touch_id; + event->finger_id = finger_id; + event->x = x; + event->y = y; + event->pressure = pressure; + + return (event_t*)event; +} + ui_load_event_t* ui_load_event_cast(event_t* event) { return_value_if_fail(event != NULL, NULL); return_value_if_fail(event->type == EVT_UI_LOAD, NULL); diff --git a/src/base/events.h b/src/base/events.h index 14b53eb420..914642164b 100644 --- a/src/base/events.h +++ b/src/base/events.h @@ -535,6 +535,21 @@ typedef enum _event_type_t { * UI加载完成事件(event_t)。 */ EVT_UI_LOAD, + /** + * @const EVT_TOUCH_DOWN + * 触摸按下事件名(touch_event_t)。 + */ + EVT_TOUCH_DOWN, + /** + * @const EVT_TOUCH_MOVE + * 触摸移动事件名(touch_event_t)。 + */ + EVT_TOUCH_MOVE, + /** + * @const EVT_TOUCH_UP + * 触摸抬起事件名(touch_event_t)。 + */ + EVT_TOUCH_UP, /** * @const EVT_REQ_START * event queue其它请求编号起始值。 @@ -1309,6 +1324,75 @@ system_event_t* system_event_cast(event_t* event); */ event_t* system_event_init(system_event_t* event, void* target, void* sdl_event); +/** + * @class touch_event_t + * @annotation ["scriptable"] + * @parent event_t + * 多点触摸事件(目前主要对接 SDL_TouchFingerEvent(SDL_FINGERMOTION/SDL_FINGERDOWN/SDL_FINGERUP))。 + */ +typedef struct _touch_event_t { + event_t e; + + /** + * @property {int64_t} touch_id + * @annotation ["readable", "scriptable"] + * 触摸ID。 + */ + int64_t touch_id; + /** + * @property {int64_t} finger_id + * @annotation ["readable", "scriptable"] + * 手指ID。 + */ + int64_t finger_id; + /** + * @property {float} x + * @annotation ["readable", "scriptable"] + * x坐标。 + */ + float x; + /** + * @property {float} y + * @annotation ["readable", "scriptable"] + * y坐标。 + */ + float y; + /** + * @property {float} pressure + * @annotation ["readable", "scriptable"] + * 压力。 + */ + float pressure; + +} touch_event_t; + +/** + * @method touch_event_cast + * @annotation ["cast", "scriptable"] + * 把event对象转touch_event_t对象。 + * @param {event_t*} event event对象。 + * + * @return {touch_event_t*} event 对象。 + */ +touch_event_t* touch_event_cast(event_t* event); + +/** + * @method touch_event_init + * 初始化事件。 + * @param {touch_event_t*} event event对象。 + * @param {uint32_t} type 事件类型。 + * @param {void*} target 事件目标。 + * @param {int64_t} touch_id 触摸ID。 + * @param {int64_t} finger_id 手指ID。 + * @param {float} x x坐标。 + * @param {float} y y坐标。 + * @param {float} pressure 压力。 + * + * @return {event_t*} event对象。 + */ +event_t* touch_event_init(touch_event_t* event, uint32_t type, void* target, int64_t touch_id, + int64_t finger_id, float x, float y, float pressure); + /** * @class ui_load_event_t * @annotation ["scriptable"] diff --git a/src/main_loop/main_loop_sdl.c b/src/main_loop/main_loop_sdl.c index 1bb83429b7..28e9e649a1 100644 --- a/src/main_loop/main_loop_sdl.c +++ b/src/main_loop/main_loop_sdl.c @@ -1,4 +1,4 @@ -/** +/** * File: main_loop_sdl.c * Author: AWTK Develop Team * Brief: sdl2 implemented main_loop interface @@ -118,6 +118,45 @@ static ret_t main_loop_sdl2_dispatch_multi_gesture_event(main_loop_simple_t* loo return RET_OK; } +static ret_t main_loop_sdl2_dispatch_touch_event(main_loop_simple_t* loop, SDL_Event* sdl_event) { + event_t* e = NULL; + touch_event_t event; + int type = EVT_TOUCH_DOWN; + widget_t* widget = loop->base.wm; + SDL_TouchFingerEvent* finger_event = (SDL_TouchFingerEvent*)sdl_event; + + memset(&event, 0x00, sizeof(event)); + switch (sdl_event->type) { + case SDL_FINGERDOWN: { + type = EVT_TOUCH_DOWN; + break; + } + case SDL_FINGERUP: { + type = EVT_TOUCH_UP; + break; + } + case SDL_FINGERMOTION: { + type = EVT_TOUCH_MOVE; + break; + } + default: + break; + } + + e = touch_event_init(&event, type, widget, finger_event->touchId, finger_event->fingerId, + finger_event->x, finger_event->y, finger_event->pressure); + + if (e != NULL) { + widget_t* win = window_manager_get_top_window(widget); + widget_dispatch(win, e); + } + + log_debug("touch event: type=%d touch_id=% " PRId64 " finger_id=%" PRId64 " x=%f y=%f\n", type, + event.touch_id, event.finger_id, event.x, event.y); + + return RET_OK; +} + static ret_t main_loop_sdl2_dispatch_mouse_event(main_loop_simple_t* loop, SDL_Event* sdl_event) { key_event_t key_event; pointer_event_t event; @@ -317,6 +356,12 @@ ret_t main_loop_sdl2_dispatch(main_loop_simple_t* loop) { ret = main_loop_sdl2_dispatch_mouse_event(loop, &event); break; } + case SDL_FINGERDOWN: + case SDL_FINGERUP: + case SDL_FINGERMOTION: { + ret = main_loop_sdl2_dispatch_touch_event(loop, &event); + break; + } case SDL_TEXTINPUT: { ret = main_loop_sdl2_dispatch_text_input(loop, &event); break;