Skip to content

Commit 69104d0

Browse files
committed
Harmony port: entrypoint multithread
1 parent 973260a commit 69104d0

File tree

3 files changed

+52
-14
lines changed

3 files changed

+52
-14
lines changed

src/core/ohos/SDL_ohos.c

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ void OHOS_windowDataFill(SDL_Window *w)
6767
w->w = wid;
6868
w->h = hei;
6969
w->internal->native_window = g_ohosNativeWindow;
70+
71+
SDL_SetWindowSize(w, wid, hei);
7072

7173
SDL_VideoDevice *_this = SDL_GetVideoDevice();
7274

@@ -224,7 +226,7 @@ static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
224226
data->func = "test";
225227
data->argCount = 0;
226228

227-
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_blocking);
229+
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_nonblocking);
228230

229231
// SDL_free(data);
230232

@@ -233,6 +235,26 @@ static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
233235
return result;
234236
}
235237

238+
typedef struct entrypoint_info_ {
239+
char* libname;
240+
char* func;
241+
} entrypoint_info;
242+
static int sdlLaunchMainInternal(void* reserved)
243+
{
244+
if (!reserved) {
245+
return -1;
246+
}
247+
entrypoint_info *data = (entrypoint_info*)reserved;
248+
void *lib = dlopen(data->libname, RTLD_LAZY);
249+
void *func = dlsym(lib, data->func);
250+
typedef int (*test)();
251+
int d = ((test)func)();
252+
dlclose(lib);
253+
SDL_free(reserved);
254+
255+
return d;
256+
}
257+
236258
static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
237259
{
238260
size_t argc = 2;
@@ -248,12 +270,12 @@ static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
248270
napi_get_value_string_utf8(env, args[1], NULL, 0, &fstringSize);
249271
char *fname = SDL_malloc(fstringSize + 1);
250272
napi_get_value_string_utf8(env, args[1], fname, fstringSize + 1, &fstringSize);
251-
252-
void *lib = dlopen(libname, RTLD_LAZY);
253-
void *func = dlsym(lib, fname);
254-
typedef int (*test)();
255-
((test)func)();
256-
dlclose(lib);
273+
274+
entrypoint_info *entry = (entrypoint_info*)SDL_malloc(sizeof(entrypoint_info));
275+
entry->func = fname;
276+
entry->libname = libname;
277+
SDL_Thread* m = SDL_CreateThread(sdlLaunchMainInternal, "SDL App Thread", entry);
278+
SDL_SetMainReady();
257279

258280
napi_value result;
259281
napi_create_int32(env, 0, &result);
@@ -278,6 +300,12 @@ static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
278300
x = (int)offsetX;
279301
y = (int)offsetY;
280302
SDL_UnlockMutex(g_ohosPageMutex);
303+
304+
napiCallbackData *data = SDL_malloc(sizeof(napiCallbackData));
305+
data->func = "onMainLaunch";
306+
data->argCount = 0;
307+
308+
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_nonblocking);
281309
}
282310
static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window)
283311
{
@@ -296,6 +324,13 @@ static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window)
296324
x = (int)offsetX;
297325
y = (int)offsetY;
298326
SDL_UnlockMutex(g_ohosPageMutex);
327+
328+
SDL_VideoDevice *_this = SDL_GetVideoDevice();
329+
SDL_Window *win = _this->windows;
330+
while (win != NULL) {
331+
OHOS_windowDataFill(win);
332+
win = win->next;
333+
}
299334
}
300335
static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
301336
{
@@ -318,6 +353,7 @@ static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
318353
static void onKeyEvent(OH_NativeXComponent *component, void *window)
319354
{
320355
OH_NativeXComponent_KeyEvent *keyEvent = NULL;
356+
SDL_Log("key!");
321357
if (OH_NativeXComponent_GetKeyEvent(component, &keyEvent) >= 0) {
322358
OH_NativeXComponent_KeyAction action;
323359
OH_NativeXComponent_KeyCode code;
@@ -349,7 +385,8 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
349385
for (int i = 0; i < touchEvent.numPoints; i++) {
350386
SDL_OHOSTouchEvent e;
351387
e.timestamp = touchEvent.timeStamp;
352-
e.deviceId = touchEvent.deviceId;
388+
// skip assertions
389+
e.deviceId = touchEvent.deviceId + 1;
353390
e.fingerId = touchEvent.touchPoints[i].id;
354391
e.area = touchEvent.touchPoints[i].size;
355392
e.x = touchEvent.touchPoints[i].x / (float)wid;
@@ -374,13 +411,13 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
374411

375412
OHOS_OnTouch(e);
376413
}
414+
415+
OHOS_UnlockPage();
377416
}
378-
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window)
379-
{
417+
// TODO mouse data
418+
static void onNativeMouse(OH_NativeXComponent *component, void *window) {
380419
onNativeTouch(component, window);
381420
}
382-
// TODO
383-
static void onNativeMouse(OH_NativeXComponent *component, void *window) {}
384421

385422
static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
386423
{
@@ -405,7 +442,6 @@ static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
405442
callback.DispatchTouchEvent = onNativeTouch;
406443
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback);
407444

408-
mouseCallback.DispatchMouseEvent = OnDispatchTouchEventCB;
409445
mouseCallback.DispatchMouseEvent = onNativeMouse;
410446
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);
411447

src/video/SDL_egl.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
* 3. This notice may not be removed or altered from any source distribution.
2020
*/
2121
#include "SDL_internal.h"
22+
#include "dynapi/SDL_dynapi_overrides.h"
2223

2324
#ifdef SDL_VIDEO_OPENGL_EGL
2425

src/video/ohos/SDL_ohosgl.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ SDL_GLContext OHOS_GLES_CreateContext(SDL_VideoDevice *_this, SDL_Window *window
1717
SDL_GLContext result;
1818

1919
OHOS_LockPage();
20-
20+
21+
OHOS_windowDataFill(window);
2122
result = SDL_EGL_CreateContext(_this, window->internal->egl_surface);
2223

2324
OHOS_UnlockPage();

0 commit comments

Comments
 (0)