From 39910421016671276db1e45d6723443b5a3cf682 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Sun, 7 Apr 2024 15:33:51 +0800 Subject: [PATCH 01/11] codecheck --- src/core/ohos/SDL_ohos.cpp | 85 +++++++++++++++++---------------- src/core/ohos/SDL_ohos.h | 21 ++++---- src/video/ohos/SDL_ohosgl.c | 2 +- src/video/ohos/SDL_ohoswindow.c | 2 +- 4 files changed, 59 insertions(+), 51 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index d6dd40669..bc2a33536 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -43,6 +43,9 @@ extern "C" { #ifdef __cplusplus } #endif +#define OHOS_DELAY_FIFTY 50 +#define OHOS_DELAY_TEN 10 +#define OHOS_START_ARGS_INDEX 2 using namespace std; using namespace OHOS::SDL; @@ -55,34 +58,32 @@ static SDL_atomic_t bQuit; /* Lock / Unlock Mutex */ void -OHOS_PageMutex_Lock() -{ +OhosPageMutexLock() +{ SDL_LockMutex(OHOS_PageMutex); } void -OHOS_PageMutex_Unlock() -{ +OhosPageMutexUnlock() +{ SDL_UnlockMutex(OHOS_PageMutex); } /* Lock the Mutex when the Activity is in its 'Running' state */ void -OHOS_PageMutex_Lock_Running() +OhosPageMutexLockRunning() { int pauseSignaled = 0; int resumeSignaled = 0; - + retry: - SDL_LockMutex(OHOS_PageMutex); - pauseSignaled = SDL_SemValue(OHOS_PauseSem); resumeSignaled = SDL_SemValue(OHOS_ResumeSem); if (pauseSignaled > resumeSignaled) { SDL_UnlockMutex(OHOS_PageMutex); - SDL_Delay(50); + SDL_Delay(OHOS_DELAY_FIFTY); goto retry; } } @@ -99,7 +100,8 @@ OHOS_GetDisplayOrientation() return displayOrientation; } -void OHOS_NAPI_SetWindowResize(int x, int y, int w, int h) { +void OHOS_NAPI_SetWindowResize(int x, int y, int w, int h) +{ cJSON *root = cJSON_CreateObject(); if (root == NULL) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "Error creating JSON object."); @@ -111,7 +113,7 @@ void OHOS_NAPI_SetWindowResize(int x, int y, int w, int h) { cJSON_AddNumberToObject(root, "w", w); cJSON_AddNumberToObject(root, "h", h); napi_status status = napi_call_threadsafe_function(napiCallback->tsfn, root, napi_tsfn_nonblocking); - if(status != napi_ok){ + if(status != napi_ok) { cJSON_free(root); } } @@ -140,7 +142,7 @@ OHOS_NAPI_RequestPermission(const char *permission) { /* Wait for any pending request on another thread */ while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) { - SDL_Delay(10); + SDL_Delay(OHOS_DELAY_TEN); } SDL_AtomicSet(&bPermissionRequestPending, SDL_TRUE); @@ -157,7 +159,7 @@ OHOS_NAPI_RequestPermission(const char *permission) } /* Wait for the request to complete */ while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) { - SDL_Delay(10); + SDL_Delay(OHOS_DELAY_TEN); } return bPermissionRequestResult; } @@ -262,7 +264,7 @@ OHOS_NAPI_SetOrientation(int w, int h, int resizable, const char *hint) } int -OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y) +OHOS_CreateCustomCursor(SDL_Surface *surface, int hotX, int hotY) { cJSON *root = cJSON_CreateObject(); if (root == NULL) { @@ -270,12 +272,12 @@ OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y) return -1; } cJSON_AddNumberToObject(root, OHOS_TS_CALLBACK_TYPE, NAPI_CALLBACK_CREATE_CUSTOMCURSOR); - cJSON_AddNumberToObject(root, "hot_x", hot_x); - cJSON_AddNumberToObject(root, "hot_y", hot_y); + cJSON_AddNumberToObject(root, "hot_x", hotX); + cJSON_AddNumberToObject(root, "hot_y", hotY); cJSON_AddNumberToObject(root, "BytesPerPixel", surface->format->BytesPerPixel); cJSON_AddNumberToObject(root, "w", surface->w); cJSON_AddNumberToObject(root, "h", surface->h); - size_t bufferSize = surface->w * surface->h * surface->format->BytesPerPixel; + size_t bufferSize = static_cast(surface->w * surface->h * surface->format->BytesPerPixel); void *buff = SDL_malloc(bufferSize); SDL_memcpy(buff, surface->pixels, bufferSize); long long surfacepixel = (long long)buff; @@ -303,7 +305,7 @@ OHOS_SetCustomCursor(int cursorID) if (status != napi_ok) { cJSON_free(root); } - return SDL_FALSE; + return SDL_FALSE; } SDL_bool @@ -327,10 +329,10 @@ OHOS_SetSystemCursor(int cursorID) } /* Relative mouse support */ -SDL_bool +SDL_bool OHOS_SupportsRelativeMouse(void) { - return SDL_TRUE; + return SDL_TRUE; } SDL_bool @@ -346,7 +348,7 @@ SDLNapi::OHOS_SetResourceManager(napi_env env, napi_callback_info info) napi_value args[2]; napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); size_t len = 0; - napi_get_value_string_utf8(env, args[0], gPath,0,&len); + napi_get_value_string_utf8(env, args[0], gPath, 0, &len); if (gPath != nullptr) { delete gPath; @@ -376,10 +378,10 @@ SDLNapi::OHOS_NativeSetScreenResolution(napi_env env, napi_callback_info info) double rate; napi_get_value_int32(env, args[0], &surfaceWidth); napi_get_value_int32(env, args[1], &surfaceHeight); - napi_get_value_int32(env, args[2], &deviceWidth); - napi_get_value_int32(env, args[3], &deviceHeight); - napi_get_value_int32(env, args[4], &format); - napi_get_value_double(env, args[5], &rate); + napi_get_value_int32(env, args[OHOS_INDEX_TWO], &deviceWidth); + napi_get_value_int32(env, args[OHOS_INDEX_THREE], &deviceHeight); + napi_get_value_int32(env, args[OHOS_INDEX_FOUR], &format); + napi_get_value_double(env, args[OHOS_INDEX_FIVE], &rate); SDL_LockMutex(OHOS_PageMutex); OHOS_SetScreenResolution(deviceWidth, deviceHeight, format, rate); SDL_UnlockMutex(OHOS_PageMutex); @@ -402,17 +404,18 @@ SDLNapi::OHOS_TextInput(napi_env env, napi_callback_info info) { size_t requireArgc = 2; size_t argc = 2; - napi_value args[2] = {nullptr}; - char* inputBuffer = nullptr;//存储atkts传过来的文本框的内容 + napi_value args[2] = {nullptr}; + char* inputBuffer = nullptr; napi_get_cb_info(env, info, &argc, args , nullptr, nullptr); napi_valuetype valuetype0; napi_typeof(env, args[0], &valuetype0); napi_valuetype valuetype1; - napi_typeof(env, args[1], &valuetype1); + napi_typeof(env, args[1], &valuetype1); - int bufSize;//atkts传过来的文本框的内容的长度 + // atkts传过来的文本框的内容的长度 + int bufSize; napi_get_value_int32(env, args[0], &bufSize); size_t stringLength = bufSize + 1; size_t length; @@ -421,23 +424,21 @@ SDLNapi::OHOS_TextInput(napi_env env, napi_callback_info info) napi_get_value_string_utf8(env, args[1], inputBuffer, stringLength, &length); - //SDL_SendKeyboardText(inputBuffer); - SDL_Event keyEvent; keyEvent.type = SDL_KEYDOWN; keyEvent.key.keysym.sym = SDLK_RETURN; SDL_PushEvent(&keyEvent); SDL_Event event; - memset(&event, 0, sizeof(SDL_Event)); // 清空event结构体 + SDL_memset(&event, 0, sizeof(SDL_Event)); // 清空event结构体 event.type = SDL_TEXTINPUT; - strcpy(event.text.text, inputBuffer); + SDL_strlcpy(event.text.text, inputBuffer, sizeof(inputBuffer)); SDL_PushEvent(&event); // 推送事件到事件队列 - delete inputBuffer; + delete inputBuffer; return nullptr; } @@ -473,7 +474,8 @@ SDLNapi::OHOS_OnNativeKeyboardFocusLost(napi_env env, napi_callback_info info) } static void -OHOS_NativeQuit(void) { +OHOS_NativeQuit(void) +{ const char *str; if (OHOS_PageMutex) { SDL_DestroyMutex(OHOS_PageMutex); @@ -569,7 +571,8 @@ SDLNapi::OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info info) bool focus; napi_get_value_bool(env, args[0], &focus); if (OHOS_Window) { - SDL_SendWindowEvent(OHOS_Window, (focus = SDL_TRUE ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST), 0, 0); + SDL_SendWindowEvent(OHOS_Window, + (focus = SDL_TRUE ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST), 0, 0); } return nullptr; } @@ -591,7 +594,7 @@ OHOS_NAPI_Init(napi_env env, napi_callback_info info) if (napiCallback == nullptr) { napiCallback = std::make_unique(); } - OHOS_NAPI_NativeSetup(); + OHOS_NAPI_NativeSetup(); napiCallback->env = env; size_t argc = 1; napi_value args[1] = {nullptr}; @@ -610,7 +613,7 @@ static napi_value SDLAppEntry(napi_env env, napi_callback_info info) { char *library_file; - + size_t buffer_size; napi_value argv[10]; @@ -729,13 +732,13 @@ SDLNapi::Init(napi_env env, napi_value exports) }; napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); OHOS_XcomponentExport(env, exports); - return exports; + return exports; } EXTERN_C_START static napi_value SDLNapiInit(napi_env env, napi_value exports) { - return SDLNapi::Init(env, exports); + return SDLNapi::Init(env, exports); } EXTERN_C_END @@ -750,7 +753,7 @@ napi_module OHOSNapiModule = { }; extern "C" __attribute__((constructor)) void RegisterEntryModule(void) -{ +{ napi_module_register(&OHOSNapiModule); } diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index a8afda1c5..d8d6e392e 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -13,10 +13,10 @@ * limitations under the License. */ -#include "../../SDL_internal.h" +#ifndef SDL_OHOS_H_ +#define SDL_OHOS_H_ -#ifndef SDL_ohos_h_ -#define SDL_ohos_h_ +#include "../../SDL_internal.h" /* Set up for C function definitions, even when using C++ */ #include "SDL_ohosfile.h" @@ -34,12 +34,17 @@ extern "C" { #include "SDL_video.h" #include "../../SDL_internal.h" +#define OHOS_INDEX_TWO 2 +#define OHOS_INDEX_THREE 3 +#define OHOS_INDEX_FOUR 4 +#define OHOS_INDEX_FIVE 5 + extern SDL_DisplayOrientation displayOrientation; extern SDL_atomic_t bPermissionRequestPending; extern SDL_bool bPermissionRequestResult; /* Cursor support */ -extern int OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y); +extern int OHOS_CreateCustomCursor(SDL_Surface *surface, int hotX, int hoY); extern SDL_bool OHOS_SetCustomCursor(int cursorID); extern SDL_bool OHOS_SetSystemCursor(int cursorID); @@ -47,13 +52,13 @@ extern SDL_bool OHOS_SetSystemCursor(int cursorID); extern SDL_bool OHOS_SupportsRelativeMouse(void); extern SDL_bool OHOS_SetRelativeMouseEnabled(SDL_bool enabled); -extern void OHOS_PageMutex_Lock(void); -extern void OHOS_PageMutex_Unlock(void); -extern void OHOS_PageMutex_Lock_Running(void); +extern void OhosPageMutexLock(void); +extern void OhosPageMutexUnlock(void); +extern void OhosPageMutexLockRunning(void); extern void OHOS_SetDisplayOrientation(int orientation); extern SDL_DisplayOrientation OHOS_GetDisplayOrientation(); -extern void OHOS_NAPI_ShowTextInput(int x ,int y, int w, int h); +extern void OHOS_NAPI_ShowTextInput(int x, int y, int w, int h); extern SDL_bool OHOS_NAPI_RequestPermission(const char *Permission); extern void OHOS_NAPI_HideTextInput(int a); extern void OHOS_NAPI_ShouldMinimizeOnFocusLoss(int a); diff --git a/src/video/ohos/SDL_ohosgl.c b/src/video/ohos/SDL_ohosgl.c index 2b116dd18..ce7e6c3f4 100644 --- a/src/video/ohos/SDL_ohosgl.c +++ b/src/video/ohos/SDL_ohosgl.c @@ -44,7 +44,7 @@ OHOS_GLES_CreateContext(_THIS, SDL_Window * window) { SDL_GLContext ret; - OHOS_PageMutex_Lock_Running(); + OhosPageMutexLockRunning(); ret = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface); diff --git a/src/video/ohos/SDL_ohoswindow.c b/src/video/ohos/SDL_ohoswindow.c index bb7dad90a..f5fae2b39 100644 --- a/src/video/ohos/SDL_ohoswindow.c +++ b/src/video/ohos/SDL_ohoswindow.c @@ -226,7 +226,7 @@ SetupWindowData(_THIS, SDL_Window *window, SDL_Window *w) { SDL_WindowData *data; unsigned int delaytime = 0; - OHOS_PageMutex_Lock_Running(); + OhosPageMutexLockRunning(); /* Allocate the window data */ window->flags = w->flags; window->x = w->x; -- Gitee From 1eb0fe8cd55b8e2702fa8210cf724e8d4913179f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Sun, 7 Apr 2024 15:52:59 +0800 Subject: [PATCH 02/11] 1 --- src/core/ohos/SDL_ohos.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index bc2a33536..5a3266a01 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -29,10 +29,10 @@ extern "C" { #include #include "cJSON.h" #include "SDL.h" -#include "../video/../../video/SDL_sysvideo.h" -#include "../events/../../events/SDL_windowevents_c.h" -#include "../events/../../events/SDL_events_c.h" -#include "../events/../../events/SDL_keyboard_c.h" +#include "../../video/SDL_sysvideo.h" +#include "../../events/SDL_windowevents_c.h" +#include "../../events/SDL_events_c.h" +#include "../../events/SDL_keyboard_c.h" #include "../../video/ohos/SDL_ohosvideo.h" #include "../../video/ohos/SDL_ohoskeyboard.h" #include "../../audio/ohos/SDL_ohosaudio.h" -- Gitee From 78215c2b7a196c1722b2a380ee23e53b466ae172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Sun, 7 Apr 2024 16:10:42 +0800 Subject: [PATCH 03/11] codecheck --- src/core/ohos/SDL_ohos.h | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index d8d6e392e..1bf49ee6a 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -28,11 +28,9 @@ extern "C" { #endif #include "SDL_system.h" -#include #include "SDL_audio.h" #include "SDL_rect.h" #include "SDL_video.h" -#include "../../SDL_internal.h" #define OHOS_INDEX_TWO 2 #define OHOS_INDEX_THREE 3 -- Gitee From 36d79c0cb7b0f8acbb7786bf56c1ced4cc246370 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Sun, 7 Apr 2024 16:14:01 +0800 Subject: [PATCH 04/11] 1 --- src/core/ohos/SDL_ohos.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index 1bf49ee6a..d8d6e392e 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -28,9 +28,11 @@ extern "C" { #endif #include "SDL_system.h" +#include #include "SDL_audio.h" #include "SDL_rect.h" #include "SDL_video.h" +#include "../../SDL_internal.h" #define OHOS_INDEX_TWO 2 #define OHOS_INDEX_THREE 3 -- Gitee From 50acc3776cfcba13d5fb3553d279af7099feeea9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 10:59:19 +0800 Subject: [PATCH 05/11] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E8=A7=84=E8=8C=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/core/ohos/SDL_ohos.cpp | 6 +++--- src/video/ohos/SDL_ohosgl.c | 2 +- src/video/ohos/SDL_ohoswindow.c | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 5a3266a01..b76374250 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -58,20 +58,20 @@ static SDL_atomic_t bQuit; /* Lock / Unlock Mutex */ void -OhosPageMutexLock() +OHOS_PAGEMUTEX_Lock() { SDL_LockMutex(OHOS_PageMutex); } void -OhosPageMutexUnlock() +OHOS_PAGEMUTEX_Unlock() { SDL_UnlockMutex(OHOS_PageMutex); } /* Lock the Mutex when the Activity is in its 'Running' state */ void -OhosPageMutexLockRunning() +OHOS_PAGEMUTEX_LockRunning() { int pauseSignaled = 0; int resumeSignaled = 0; diff --git a/src/video/ohos/SDL_ohosgl.c b/src/video/ohos/SDL_ohosgl.c index ce7e6c3f4..737322f42 100644 --- a/src/video/ohos/SDL_ohosgl.c +++ b/src/video/ohos/SDL_ohosgl.c @@ -44,7 +44,7 @@ OHOS_GLES_CreateContext(_THIS, SDL_Window * window) { SDL_GLContext ret; - OhosPageMutexLockRunning(); + OHOS_PAGEMUTEX_LockRunning(); ret = SDL_EGL_CreateContext(_this, ((SDL_WindowData *) window->driverdata)->egl_surface); diff --git a/src/video/ohos/SDL_ohoswindow.c b/src/video/ohos/SDL_ohoswindow.c index f5fae2b39..833d9da08 100644 --- a/src/video/ohos/SDL_ohoswindow.c +++ b/src/video/ohos/SDL_ohoswindow.c @@ -226,7 +226,7 @@ SetupWindowData(_THIS, SDL_Window *window, SDL_Window *w) { SDL_WindowData *data; unsigned int delaytime = 0; - OhosPageMutexLockRunning(); + OHOS_PAGEMUTEX_LockRunning(); /* Allocate the window data */ window->flags = w->flags; window->x = w->x; -- Gitee From ea541d9698148816f7bcb21461c4e607de0cb762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 14:54:51 +0800 Subject: [PATCH 06/11] 1 --- src/core/ohos/SDL_ohos.cpp | 141 +++++++++++++++++++++++------ src/core/ohos/SDL_ohosthreadsafe.h | 7 ++ 2 files changed, 119 insertions(+), 29 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index b76374250..4f4403228 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -609,54 +609,111 @@ OHOS_NAPI_Init(napi_env env, napi_callback_info info) return nullptr; } -static napi_value -SDLAppEntry(napi_env env, napi_callback_info info) -{ - char *library_file; - +static int +OHOS_NAPI_GetInfo(napi_env &env, napi_callback_info &info, napi_value *argv, char **library_file, + char **function_name) { + napi_status status; + napi_valuetype valuetype; size_t buffer_size; - - napi_value argv[10]; size_t argc = 10; + + status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():failed to obtained argument!"); + return argc; + } + + status = napi_typeof(env, argv[0], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); + return argc; + } + napi_get_value_string_utf8(env, argv[0], nullptr, 0, &buffer_size); + *library_file = new char[buffer_size]; + napi_get_value_string_utf8(env, argv[0], *library_file, buffer_size + 1, &buffer_size); + + status = napi_typeof(env, argv[1], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); + SDL_free(library_file); + return argc; + } + napi_get_value_string_utf8(env, argv[1], nullptr, 0, &buffer_size); + *function_name = new char[buffer_size]; + napi_get_value_string_utf8(env, argv[1], *function_name, buffer_size + 1, &buffer_size); + return argc; +} + +static int +OHOS_NAPI_SetArgs(napi_env& env, char **argvs, int &argcs, size_t &argc, napi_value* argv) { napi_status status; napi_valuetype valuetype; - - if (OHOS_IsThreadRun() == SDL_TRUE) { - return nullptr; + size_t buffer_size; + int i = 2; + + argvs[argcs++] = SDL_strdup("SDL_main"); + for (i = 2; i < argc; ++i) { + char *arg = NULL; + status = napi_typeof(env, argv[i], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); + break; + } + napi_get_value_string_utf8(env, argv[i], nullptr, 0, &buffer_size); + arg = new char[buffer_size + 1]; + SDL_memset(arg, 0, buffer_size + 1); + napi_get_value_string_utf8(env, argv[i], arg, buffer_size + 1, &buffer_size); + if (!arg) { + delete[] arg; + arg = SDL_strdup(""); + } + argvs[argcs++] = arg; } + return i; +} + +static int +OHOS_NAPI_GetInfo(napi_env &env, napi_callback_info &info, napi_value *argv, char **library_file, + char **function_name) { + napi_status status; + napi_valuetype valuetype; + size_t buffer_size; + size_t argc = 10; status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (status != napi_ok) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():failed to obtained argument!"); - return nullptr; + return argc; } status = napi_typeof(env, argv[0], &valuetype); if (status != napi_ok || valuetype != napi_string) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); - return nullptr; + return argc; } napi_get_value_string_utf8(env, argv[0], nullptr, 0, &buffer_size); - library_file = new char[buffer_size]; - napi_get_value_string_utf8(env, argv[0], library_file, buffer_size + 1, &buffer_size); + *library_file = new char[buffer_size]; + napi_get_value_string_utf8(env, argv[0], *library_file, buffer_size + 1, &buffer_size); - char *function_name; status = napi_typeof(env, argv[1], &valuetype); if (status != napi_ok || valuetype != napi_string) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); SDL_free(library_file); - return nullptr; + return argc; } napi_get_value_string_utf8(env, argv[1], nullptr, 0, &buffer_size); - function_name = new char[buffer_size]; - napi_get_value_string_utf8(env, argv[1], function_name, buffer_size + 1, &buffer_size); - - char **argvs; - int argcs = 0; - int i; - bool isstack; + *function_name = new char[buffer_size]; + napi_get_value_string_utf8(env, argv[1], *function_name, buffer_size + 1, &buffer_size); + return argc; +} - argvs = SDL_small_alloc(char *, argc, &isstack); +static int +OHOS_NAPI_SetArgs(napi_env& env, char **argvs, int &argcs, size_t &argc, napi_value* argv) { + napi_status status; + napi_valuetype valuetype; + size_t buffer_size; + int i = 2; + argvs[argcs++] = SDL_strdup("SDL_main"); for (i = 2; i < argc; ++i) { char *arg = NULL; @@ -675,7 +732,28 @@ SDLAppEntry(napi_env env, napi_callback_info info) } argvs[argcs++] = arg; } - + return i; +} + +static napi_value +OHOS_NAPI_SDLAppEntry(napi_env env, napi_callback_info info) { + char *library_file; + char *function_name; + napi_value argv[10]; + size_t argc = 10; + char **argvs; + int argcs = 0; + bool isstack; + int i; + + if (OHOS_IsThreadRun() == SDL_TRUE) { + return nullptr; + } + + argc = OHOS_NAPI_GetInfo(env, info, argv, &library_file, &function_name); + argvs = SDL_small_alloc(char *, argc, &isstack); + i = OHOS_NAPI_SetArgs(env, argvs, argcs, argc, argv); + bool isRunThread = true; OhosSDLEntryInfo *entry = NULL; if (i == argc) { @@ -691,7 +769,7 @@ SDLAppEntry(napi_env env, napi_callback_info info) isRunThread = false; } } - + // Not run SDL thread succuss then free memory, if run SDL thread succuss, SDL thread deal the memory. if (!isRunThread) { for (i = 0; i < argcs; ++i) { @@ -700,9 +778,14 @@ SDLAppEntry(napi_env env, napi_callback_info info) SDL_small_free(argvs, isstack); SDL_free(info); SDL_free(function_name); - SDL_free(library_file); + if (library_file != nullptr) { + dlclose(library_file); + SDL_free(library_file); + library_file = nullptr; + } + SDL_free(entry); } - + return nullptr; } @@ -710,7 +793,7 @@ napi_value SDLNapi::Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = { - {"sdlAppEntry", nullptr, SDLAppEntry, nullptr, nullptr, nullptr, napi_default, nullptr}, + {"sdlAppEntry", nullptr, OHOS_NAPI_SDLAppEntry, nullptr, nullptr, nullptr, napi_default, nullptr}, {"nativeSetScreenResolution", nullptr, OHOS_NativeSetScreenResolution, nullptr, nullptr, nullptr, napi_default, nullptr}, {"onNativeResize", nullptr, OHOS_OnNativeResize, nullptr, nullptr, nullptr, napi_default, nullptr}, diff --git a/src/core/ohos/SDL_ohosthreadsafe.h b/src/core/ohos/SDL_ohosthreadsafe.h index 26b52d088..79d198a9e 100644 --- a/src/core/ohos/SDL_ohosthreadsafe.h +++ b/src/core/ohos/SDL_ohosthreadsafe.h @@ -53,6 +53,13 @@ typedef struct { int argcs; char *functionName; char *libraryFile; + void SetValue(char **argvs, int argcs, char *functionName, char *libraryFile) + { + this->argvs = argvs; + this->argcs = argcs; + this->functionName = functionName; + this->libraryFile = libraryFile; + } } OhosSDLEntryInfo; extern std::unique_ptr napiCallback; -- Gitee From f64414f95f55ce51752b832ff70baba80e097818 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 15:16:03 +0800 Subject: [PATCH 07/11] 1 --- src/core/ohos/SDL_ohos.cpp | 63 -------------------------------------- 1 file changed, 63 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 4f4403228..e8b552b40 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -672,69 +672,6 @@ OHOS_NAPI_SetArgs(napi_env& env, char **argvs, int &argcs, size_t &argc, napi_va return i; } -static int -OHOS_NAPI_GetInfo(napi_env &env, napi_callback_info &info, napi_value *argv, char **library_file, - char **function_name) { - napi_status status; - napi_valuetype valuetype; - size_t buffer_size; - size_t argc = 10; - - status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); - if (status != napi_ok) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():failed to obtained argument!"); - return argc; - } - - status = napi_typeof(env, argv[0], &valuetype); - if (status != napi_ok || valuetype != napi_string) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); - return argc; - } - napi_get_value_string_utf8(env, argv[0], nullptr, 0, &buffer_size); - *library_file = new char[buffer_size]; - napi_get_value_string_utf8(env, argv[0], *library_file, buffer_size + 1, &buffer_size); - - status = napi_typeof(env, argv[1], &valuetype); - if (status != napi_ok || valuetype != napi_string) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); - SDL_free(library_file); - return argc; - } - napi_get_value_string_utf8(env, argv[1], nullptr, 0, &buffer_size); - *function_name = new char[buffer_size]; - napi_get_value_string_utf8(env, argv[1], *function_name, buffer_size + 1, &buffer_size); - return argc; -} - -static int -OHOS_NAPI_SetArgs(napi_env& env, char **argvs, int &argcs, size_t &argc, napi_value* argv) { - napi_status status; - napi_valuetype valuetype; - size_t buffer_size; - int i = 2; - - argvs[argcs++] = SDL_strdup("SDL_main"); - for (i = 2; i < argc; ++i) { - char *arg = NULL; - status = napi_typeof(env, argv[i], &valuetype); - if (status != napi_ok || valuetype != napi_string) { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); - break; - } - napi_get_value_string_utf8(env, argv[i], nullptr, 0, &buffer_size); - arg = new char[buffer_size + 1]; - SDL_memset(arg, 0, buffer_size + 1); - napi_get_value_string_utf8(env, argv[i], arg, buffer_size + 1, &buffer_size); - if (!arg) { - delete[] arg; - arg = SDL_strdup(""); - } - argvs[argcs++] = arg; - } - return i; -} - static napi_value OHOS_NAPI_SDLAppEntry(napi_env env, napi_callback_info info) { char *library_file; -- Gitee From 7e845859ee6b52c353349d5dff40a86823ae5aeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 15:19:06 +0800 Subject: [PATCH 08/11] 1 --- src/core/ohos/SDL_ohos.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index d8d6e392e..80de2be05 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -52,9 +52,9 @@ extern SDL_bool OHOS_SetSystemCursor(int cursorID); extern SDL_bool OHOS_SupportsRelativeMouse(void); extern SDL_bool OHOS_SetRelativeMouseEnabled(SDL_bool enabled); -extern void OhosPageMutexLock(void); -extern void OhosPageMutexUnlock(void); -extern void OhosPageMutexLockRunning(void); +extern void OHOS_PAGEMUTEX_Lock(void); +extern void OHOS_PAGEMUTEX_Unlock(void); +extern void OHOS_PAGEMUTEX_LockRunning(void); extern void OHOS_SetDisplayOrientation(int orientation); extern SDL_DisplayOrientation OHOS_GetDisplayOrientation(); -- Gitee From 46bd5a2142b35b87f149af5e56661f43217a1062 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 15:22:44 +0800 Subject: [PATCH 09/11] 1 --- src/core/ohos/SDL_ohosthreadsafe.h | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/core/ohos/SDL_ohosthreadsafe.h b/src/core/ohos/SDL_ohosthreadsafe.h index 79d198a9e..26b52d088 100644 --- a/src/core/ohos/SDL_ohosthreadsafe.h +++ b/src/core/ohos/SDL_ohosthreadsafe.h @@ -53,13 +53,6 @@ typedef struct { int argcs; char *functionName; char *libraryFile; - void SetValue(char **argvs, int argcs, char *functionName, char *libraryFile) - { - this->argvs = argvs; - this->argcs = argcs; - this->functionName = functionName; - this->libraryFile = libraryFile; - } } OhosSDLEntryInfo; extern std::unique_ptr napiCallback; -- Gitee From 0b5768e53e5369a042c0389793f3703613f81526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 15:44:25 +0800 Subject: [PATCH 10/11] 1 --- src/core/ohos/SDL_ohos.cpp | 20 +++++++++++++------- src/core/ohos/SDL_ohos.h | 5 ----- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index e8b552b40..729651adf 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -46,6 +46,12 @@ extern "C" { #define OHOS_DELAY_FIFTY 50 #define OHOS_DELAY_TEN 10 #define OHOS_START_ARGS_INDEX 2 +#define OHOS_INDEX_ARG0 0 +#define OHOS_INDEX_ARG1 1 +#define OHOS_INDEX_ARG2 2 +#define OHOS_INDEX_ARG3 3 +#define OHOS_INDEX_ARG4 4 +#define OHOS_INDEX_ARG5 5 using namespace std; using namespace OHOS::SDL; @@ -345,7 +351,7 @@ napi_value SDLNapi::OHOS_SetResourceManager(napi_env env, napi_callback_info info) { size_t argc = 2; - napi_value args[2]; + napi_value args[OHOS_INDEX_ARG2]; napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); size_t len = 0; napi_get_value_string_utf8(env, args[0], gPath, 0, &len); @@ -376,12 +382,12 @@ SDLNapi::OHOS_NativeSetScreenResolution(napi_env env, napi_callback_info info) int deviceHeight; int format; double rate; - napi_get_value_int32(env, args[0], &surfaceWidth); - napi_get_value_int32(env, args[1], &surfaceHeight); - napi_get_value_int32(env, args[OHOS_INDEX_TWO], &deviceWidth); - napi_get_value_int32(env, args[OHOS_INDEX_THREE], &deviceHeight); - napi_get_value_int32(env, args[OHOS_INDEX_FOUR], &format); - napi_get_value_double(env, args[OHOS_INDEX_FIVE], &rate); + napi_get_value_int32(env, args[OHOS_INDEX_ARG0], &surfaceWidth); + napi_get_value_int32(env, args[OHOS_INDEX_ARG1], &surfaceHeight); + napi_get_value_int32(env, args[OHOS_INDEX_ARG2], &deviceWidth); + napi_get_value_int32(env, args[OHOS_INDEX_ARG3], &deviceHeight); + napi_get_value_int32(env, args[OHOS_INDEX_ARG4], &format); + napi_get_value_double(env, args[OHOS_INDEX_ARG5], &rate); SDL_LockMutex(OHOS_PageMutex); OHOS_SetScreenResolution(deviceWidth, deviceHeight, format, rate); SDL_UnlockMutex(OHOS_PageMutex); diff --git a/src/core/ohos/SDL_ohos.h b/src/core/ohos/SDL_ohos.h index 80de2be05..40152bfc2 100644 --- a/src/core/ohos/SDL_ohos.h +++ b/src/core/ohos/SDL_ohos.h @@ -34,11 +34,6 @@ extern "C" { #include "SDL_video.h" #include "../../SDL_internal.h" -#define OHOS_INDEX_TWO 2 -#define OHOS_INDEX_THREE 3 -#define OHOS_INDEX_FOUR 4 -#define OHOS_INDEX_FIVE 5 - extern SDL_DisplayOrientation displayOrientation; extern SDL_atomic_t bPermissionRequestPending; extern SDL_bool bPermissionRequestResult; -- Gitee From e1132d791d42824326905ca27dac938d868d2077 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=91=A3=E6=AD=A3=E4=B8=9C?= <1796342412@qq.com> Date: Mon, 8 Apr 2024 17:29:51 +0800 Subject: [PATCH 11/11] 1 --- src/core/ohos/SDL_ohos.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 729651adf..7821ca146 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -577,8 +577,8 @@ SDLNapi::OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info info) bool focus; napi_get_value_bool(env, args[0], &focus); if (OHOS_Window) { - SDL_SendWindowEvent(OHOS_Window, - (focus = SDL_TRUE ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST), 0, 0); + SDL_SendWindowEvent(OHOS_Window, + (focus = SDL_TRUE ? SDL_WINDOWEVENT_FOCUS_GAINED : SDL_WINDOWEVENT_FOCUS_LOST), 0, 0); } return nullptr; } @@ -626,13 +626,13 @@ OHOS_NAPI_GetInfo(napi_env &env, napi_callback_info &info, napi_value *argv, cha status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); if (status != napi_ok) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():failed to obtained argument!"); - return argc; + return -1; } status = napi_typeof(env, argv[0], &valuetype); if (status != napi_ok || valuetype != napi_string) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); - return argc; + return -1; } napi_get_value_string_utf8(env, argv[0], nullptr, 0, &buffer_size); *library_file = new char[buffer_size]; @@ -642,7 +642,7 @@ OHOS_NAPI_GetInfo(napi_env &env, napi_callback_info &info, napi_value *argv, cha if (status != napi_ok || valuetype != napi_string) { SDL_LogError(SDL_LOG_CATEGORY_ERROR, "SDLAppEntry():invalid type of argument!"); SDL_free(library_file); - return argc; + return -1; } napi_get_value_string_utf8(env, argv[1], nullptr, 0, &buffer_size); *function_name = new char[buffer_size]; @@ -694,6 +694,8 @@ OHOS_NAPI_SDLAppEntry(napi_env env, napi_callback_info info) { } argc = OHOS_NAPI_GetInfo(env, info, argv, &library_file, &function_name); + if (argc == -1) + return nullptr; argvs = SDL_small_alloc(char *, argc, &isstack); i = OHOS_NAPI_SetArgs(env, argvs, argcs, argc, argv); -- Gitee