Skip to content

Commit c951070

Browse files
committed
Harmony port: touch event
1 parent 56ccf29 commit c951070

File tree

4 files changed

+96
-3
lines changed

4 files changed

+96
-3
lines changed

src/core/ohos/SDL_ohos.c

Lines changed: 45 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "SDL3/SDL_events.h"
12
#include "SDL_internal.h"
23
#include <EGL/egl.h>
34
#include <EGL/eglplatform.h>
@@ -14,6 +15,7 @@
1415
#include "SDL_ohos.h"
1516
#include <ace/xcomponent/native_interface_xcomponent.h>
1617
#include "../../video/ohos/SDL_ohosvideo.h"
18+
#include "../../video/ohos/SDL_ohostouch.h"
1719
#include "SDL3/SDL_mutex.h"
1820
#include "../../video/ohos/SDL_ohoskeyboard.h"
1921

@@ -355,10 +357,51 @@ static void onKeyEvent(OH_NativeXComponent *component, void *window)
355357
}
356358
}
357359

360+
static void onNativeTouch(OH_NativeXComponent *component, void *window)
361+
{
362+
OH_NativeXComponent_TouchEvent touchEvent;
363+
OH_NativeXComponent_TouchPointToolType toolType = OH_NATIVEXCOMPONENT_TOOL_TYPE_UNKNOWN;
364+
365+
OHOS_LockPage();
366+
OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent);
367+
OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType);
368+
369+
for (int i = 0; i < touchEvent.numPoints; i++)
370+
{
371+
SDL_OHOSTouchEvent e;
372+
e.timestamp = touchEvent.timeStamp;
373+
e.deviceId = touchEvent.deviceId;
374+
e.fingerId = touchEvent.touchPoints[i].id;
375+
e.area = touchEvent.touchPoints[i].size;
376+
e.x = touchEvent.touchPoints[i].x / (float)wid;
377+
e.y = touchEvent.touchPoints[i].y / (float)hei;
378+
e.p = touchEvent.touchPoints[i].force;
379+
380+
switch (touchEvent.touchPoints[i].type) {
381+
case OH_NATIVEXCOMPONENT_DOWN:
382+
e.type = SDL_EVENT_FINGER_DOWN;
383+
break;
384+
case OH_NATIVEXCOMPONENT_MOVE:
385+
e.type = SDL_EVENT_FINGER_MOTION;
386+
break;
387+
case OH_NATIVEXCOMPONENT_UP:
388+
e.type = SDL_EVENT_FINGER_UP;
389+
break;
390+
case OH_NATIVEXCOMPONENT_CANCEL:
391+
case OH_NATIVEXCOMPONENT_UNKNOWN:
392+
e.type = SDL_EVENT_FINGER_CANCELED;
393+
break;
394+
}
395+
396+
OHOS_OnTouch(e);
397+
}
398+
}
399+
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window)
400+
{
401+
onNativeTouch(component, window);
402+
}
358403
// TODO
359-
static void onNativeTouch(OH_NativeXComponent *component, void *window) {}
360404
static void onNativeMouse(OH_NativeXComponent *component, void *window) {}
361-
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) {}
362405
static void OnHoverEvent(OH_NativeXComponent *component, bool isHover) {}
363406
static void OnFocusEvent(OH_NativeXComponent *component, void *window) {}
364407
static void OnBlurEvent(OH_NativeXComponent *component, void *window) {}

src/core/ohos/SDL_ohos.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#ifndef SDL_OHOS_H
22
#define SDL_OHOS_H
33

4-
#include "SDL3/SDL_mutex.h"
54
#include "SDL3/SDL_video.h"
65
#include "video/SDL_sysvideo.h"
76
#include <native_window/external_window.h>
@@ -19,4 +18,15 @@ typedef struct SDL_VideoData {
1918
int isPausing;
2019
} SDL_VideoData;
2120

21+
typedef struct SDL_OHOSTouchEvent {
22+
int64_t deviceId;
23+
int32_t fingerId;
24+
int type;
25+
float x;
26+
float y;
27+
float p;
28+
float area;
29+
int64_t timestamp;
30+
} SDL_OHOSTouchEvent;
31+
2232
#endif

src/video/ohos/SDL_ohostouch.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "../../core/ohos/SDL_ohos.h"
2+
#include "SDL3/SDL_events.h"
3+
#include "SDL3/SDL_touch.h"
4+
#include "events/SDL_events_c.h"
5+
void OHOS_OnTouch(SDL_OHOSTouchEvent event)
6+
{
7+
if (SDL_AddTouch(event.deviceId, SDL_TOUCH_DEVICE_DIRECT, "") < 0)
8+
{
9+
SDL_Log("Cannot add touch");
10+
return;
11+
}
12+
13+
switch (event.type) {
14+
case SDL_EVENT_FINGER_DOWN: {
15+
SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_DOWN, event.x, event.y, event.p);
16+
break;
17+
}
18+
case SDL_EVENT_FINGER_MOTION: {
19+
SDL_SendTouchMotion(event.timestamp, event.deviceId, event.fingerId, NULL, event.x, event.y, event.p);
20+
break;
21+
}
22+
case SDL_EVENT_FINGER_UP: {
23+
SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_UP, event.x, event.y, event.p);
24+
break;
25+
}
26+
case SDL_EVENT_FINGER_CANCELED: {
27+
SDL_SendTouch(event.timestamp, event.deviceId, event.fingerId, NULL, SDL_EVENT_FINGER_CANCELED, event.x, event.y, event.p);
28+
break;
29+
}
30+
}
31+
}

src/video/ohos/SDL_ohostouch.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#ifndef SDL_OHOSTOUCH_H
2+
#define SDL_OHOSTOUCH_H
3+
4+
#include "SDL_internal.h"
5+
#include "../../core/ohos/SDL_ohos.h"
6+
7+
void OHOS_OnTouch(SDL_OHOSTouchEvent event);
8+
9+
#endif

0 commit comments

Comments
 (0)