diff --git a/src/core/ohos/SDL_ohos.c b/src/core/ohos/SDL_ohos.c index e7a41b9067..a9c0af9738 100644 --- a/src/core/ohos/SDL_ohos.c +++ b/src/core/ohos/SDL_ohos.c @@ -1,3 +1,4 @@ +#include "SDL3/SDL_events.h" #include "SDL_internal.h" #include #include @@ -14,6 +15,7 @@ #include "SDL_ohos.h" #include #include "../../video/ohos/SDL_ohosvideo.h" +#include "../../video/ohos/SDL_ohostouch.h" #include "SDL3/SDL_mutex.h" #include "../../video/ohos/SDL_ohoskeyboard.h" @@ -355,10 +357,51 @@ static void onKeyEvent(OH_NativeXComponent *component, void *window) } } +static void onNativeTouch(OH_NativeXComponent *component, void *window) +{ + OH_NativeXComponent_TouchEvent touchEvent; + OH_NativeXComponent_TouchPointToolType toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN; + + OHOS_LockPage(); + OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent); + OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType); + + for (int i = 0; i < touchEvent.numPoints; i++) + { + SDL_OHOSTouchEvent e; + e.timestamp = touchEvent.timeStamp; + e.deviceId = touchEvent.deviceId; + e.fingerId = touchEvent.touchPoints[i].id; + e.area = touchEvent.touchPoints[i].size; + e.x = touchEvent.touchPoints[i].x / (float)wid; + e.y = touchEvent.touchPoints[i].y / (float)hei; + e.p = touchEvent.touchPoints[i].force; + + switch (touchEvent.touchPoints[i].type) { + case OH_NATIVEXCOMPONENT_DOWN: + e.type = SDL_EVENT_FINGER_DOWN; + break; + case OH_NATIVEXCOMPONENT_MOVE: + e.type = SDL_EVENT_FINGER_MOTION; + break; + case OH_NATIVEXCOMPONENT_UP: + e.type = SDL_EVENT_FINGER_UP; + break; + case OH_NATIVEXCOMPONENT_CANCEL: + case OH_NATIVEXCOMPONENT_UNKNOWN: + e.type = SDL_EVENT_FINGER_CANCELED; + break; + } + + OHOS_OnTouch(e); + } +} +static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) +{ + onNativeTouch(component, window); +} // TODO -static void onNativeTouch(OH_NativeXComponent *component, void *window) {} static void onNativeMouse(OH_NativeXComponent *component, void *window) {} -static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) {} static void OnHoverEvent(OH_NativeXComponent *component, bool isHover) {} static void OnFocusEvent(OH_NativeXComponent *component, void *window) {} static void OnBlurEvent(OH_NativeXComponent *component, void *window) {} diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index f95941cd35..e8adb2777b 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -1,7 +1,6 @@ #ifndef SDL_OHOS_H #define SDL_OHOS_H -#include "SDL3/SDL_mutex.h" #include "SDL3/SDL_video.h" #include "video/SDL_sysvideo.h" #include @@ -19,4 +18,15 @@ typedef struct SDL_VideoData { int isPausing; } SDL_VideoData; +typedef struct SDL_OHOSTouchEvent { + int64_t deviceId; + int32_t fingerId; + int type; + float x; + float y; + float p; + float area; + int64_t timestamp; +} SDL_OHOSTouchEvent; + #endif diff --git a/src/video/ohos/SDL_ohostouch.c b/src/video/ohos/SDL_ohostouch.c new file mode 100644 index 0000000000..673c28730c --- /dev/null +++ b/src/video/ohos/SDL_ohostouch.c @@ -0,0 +1,31 @@ +#include "../../core/ohos/SDL_ohos.h" +#include "SDL3/SDL_events.h" +#include "SDL3/SDL_touch.h" +#include "events/SDL_events_c.h" +void OHOS_OnTouch(SDL_OHOSTouchEvent event) +{ + if (SDL_AddTouch(event.deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0) + { + SDL_Log("Cannot add touch"); + return; + } + + switch (event.type) { + case SDL_EVENT_FINGER_DOWN: { + SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_DOWN, event.x, event.y, event.p); + break; + } + case SDL_EVENT_FINGER_MOTION: { + SDL_SendTouchMotion(event.timestamp, event.deviceId, event.fingerId, NULL, event.x, event.y, event.p); + break; + } + case SDL_EVENT_FINGER_UP: { + SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_UP, event.x, event.y, event.p); + break; + } + case SDL_EVENT_FINGER_CANCELED: { + SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_CANCELED, event.x, event.y, event.p); + break; + } + } +} diff --git a/src/video/ohos/SDL_ohostouch.h b/src/video/ohos/SDL_ohostouch.h new file mode 100644 index 0000000000..41dc9c6240 --- /dev/null +++ b/src/video/ohos/SDL_ohostouch.h @@ -0,0 +1,9 @@ +#ifndef SDL_OHOSTOUCH_H +#define SDL_OHOSTOUCH_H + +#include "SDL_internal.h" +#include "../../core/ohos/SDL_ohos.h" + +void OHOS_OnTouch(SDL_OHOSTouchEvent event); + +#endif