diff --git a/CMakeLists.txt b/CMakeLists.txt index 67db094c6a0bb88cec4aaf0c4b970d7290526664..e795add83e84b32d2944c7417e11eec81ee44013 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2188,15 +2188,11 @@ if(SDL_SHARED) OUTPUT_NAME "SDL2-${LT_RELEASE}") elseif(UNIX AND NOT ANDROID) set_target_properties(SDL2 PROPERTIES - VERSION ${LT_VERSION} - SOVERSION ${LT_REVISION} #TODO change onlytest lead can't find so. #OUTPUT_NAME "SDL2-${LT_RELEASE}") OUTPUT_NAME "SDL2") else() set_target_properties(SDL2 PROPERTIES - VERSION ${SDL_VERSION} - SOVERSION ${LT_REVISION} OUTPUT_NAME "SDL2") endif() if(MSVC AND NOT LIBC) diff --git a/ohos-project/entry/build-profile.json5 b/ohos-project/entry/build-profile.json5 index ce374e052cf6b2fcf14d6f0d9b1989f4862a3d01..3e2566ad23d931002be4e27122bb677aba107de2 100644 --- a/ohos-project/entry/build-profile.json5 +++ b/ohos-project/entry/build-profile.json5 @@ -7,7 +7,7 @@ ] }, "externalNativeOptions": { - "path": "./src/main/cpp/SDL/CMakeLists.txt", + "path": "./src/main/cpp/CMakeLists.txt", "arguments": "-v -DOHOS_STL=c++_shared -DSDL_TESTS=ON", "abiFilters": [ "arm64-v8a", diff --git a/ohos-project/entry/src/main/cpp/CMakeLists.txt b/ohos-project/entry/src/main/cpp/CMakeLists.txt new file mode 100644 index 0000000000000000000000000000000000000000..ce4b162af4a7e9c151b2eca6415c9bdd1e73b551 --- /dev/null +++ b/ohos-project/entry/src/main/cpp/CMakeLists.txt @@ -0,0 +1,23 @@ +# the minimum version of CMake. +cmake_minimum_required(VERSION 3.4.1) +project(entry) + +set(env ${CMAKE_CURRENT_SOURCE_DIR}) +set(NATIVE_INCLUDE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/SDL/include) +set(NATIVE_ENTRY_PATH ${CMAKE_CURRENT_SOURCE_DIR}/application) + + +add_subdirectory(SDL) +include_directories(${NATIVE_INCLUDE_PATH}) + +link_directories(${CMAKE_LIBRARY_OUTPUT_DIRECTORY}) + +file(GLOB my_files "${NATIVE_ENTRY_PATH}/*.c" "${NATIVE_ENTRY_PATH}/*.h") + +add_library(entry SHARED ${my_files}) + +add_dependencies(entry SDL2) + +target_link_libraries(entry PUBLIC libSDL2d.so) + + diff --git a/ohos-project/entry/src/main/cpp/SDL/src/core/ohos/SDL_ohos.cpp b/ohos-project/entry/src/main/cpp/SDL/src/core/ohos/SDL_ohos.cpp deleted file mode 100644 index 45d2ca1a1d16bfd8c47be95db26d61a19ea888d2..0000000000000000000000000000000000000000 --- a/ohos-project/entry/src/main/cpp/SDL/src/core/ohos/SDL_ohos.cpp +++ /dev/null @@ -1,714 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License,Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "SDL_napi.h" -#ifdef __cplusplus -extern "C" { -#endif -#include -#include -#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/ohos/SDL_ohosvideo.h" -#include "SDL_ohos.h" -#include "SDL_quit.h" -#include "SDL_ohos.h" -#ifdef __cplusplus -} -#endif - -#include -#include -#include - -using namespace std; -using namespace OHOS::SDL; - -SDL_DisplayOrientation displayOrientation; - -SDL_atomic_t bPermissionRequestPending; -SDL_bool bPermissionRequestResult; - -SDL_RWops *gCtx = nullptr; -char *gPath = nullptr; - -const char *SDL_OHOSGetInternalStoragePath() -{ - return gPath; -} - -int OHOS_FileOpen(SDL_RWops *ctx, const char *fileName, const char *mode) -{ - gCtx->hidden.ohosio.fileName = (char *)fileName; - gCtx->hidden.ohosio.mode = (char *)mode; - gCtx->hidden.ohosio.position = 0; - - NativeResourceManager *nativeResourceManager = static_cast(gCtx->hidden.ohosio.nativeResourceManager); - RawFile *rawFile = OH_ResourceManager_OpenRawFile(nativeResourceManager, fileName); - - if (!rawFile) { - return -1; - } - - gCtx->hidden.ohosio.fileNameRef = rawFile; - - long rawFileSize = OH_ResourceManager_GetRawFileSize(rawFile); - gCtx->hidden.ohosio.size = rawFileSize; - - RawFileDescriptor descriptor; - bool result = OH_ResourceManager_GetRawFileDescriptor(rawFile, descriptor); - gCtx->hidden.ohosio.fd = descriptor.fd; - gCtx->hidden.ohosio.fileDescriptorRef = static_cast(&descriptor); - - long rawFileOffset = OH_ResourceManager_GetRawFileOffset(rawFile); - gCtx->hidden.ohosio.offset = rawFileOffset; - ctx = gCtx; - - /* Seek to the correct offset in the file. */ - int position = OH_ResourceManager_SeekRawFile(rawFile, gCtx->hidden.ohosio.offset, SEEK_SET); - - return 0; -} - -Sint64 OHOS_FileSize(SDL_RWops *ctx) -{ - return gCtx->hidden.ohosio.size; -} - -Sint64 OHOS_FileSeek(SDL_RWops *ctx, Sint64 offset, int whence) -{ - if (gCtx->hidden.ohosio.nativeResourceManager) { - switch (whence) { - case RW_SEEK_SET: - if (gCtx->hidden.ohosio.size != -1 /* UNKNOWN_LENGTH */ && offset > gCtx->hidden.ohosio.size) - offset = gCtx->hidden.ohosio.size; - offset += gCtx->hidden.ohosio.offset; - break; - case RW_SEEK_CUR: - offset += gCtx->hidden.ohosio.position; - if (gCtx->hidden.ohosio.size != -1 /* UNKNOWN_LENGTH */ && offset > gCtx->hidden.ohosio.size) - offset = gCtx->hidden.ohosio.size; - offset += gCtx->hidden.ohosio.offset; - break; - case RW_SEEK_END: - offset = gCtx->hidden.ohosio.offset + gCtx->hidden.ohosio.size + offset; - break; - default: - return SDL_SetError("Unknown value for 'whence'"); - } - - RawFile *rawFile = static_cast(gCtx->hidden.ohosio.fileNameRef); - int ret = OH_ResourceManager_SeekRawFile(rawFile, offset, SEEK_SET); - if (ret == -1) - { - return -1; - } - - if (ret == 0) - { - ret = offset; - } - - gCtx->hidden.ohosio.position = ret - gCtx->hidden.ohosio.offset; - } else { - Sint64 newPosition; - Sint64 movement; - - switch (whence) { - case RW_SEEK_SET: - newPosition = offset; - break; - case RW_SEEK_CUR: - newPosition = gCtx->hidden.ohosio.position + offset; - break; - case RW_SEEK_END: - newPosition = gCtx->hidden.ohosio.size + offset; - break; - default: - return SDL_SetError("Unknown value for 'whence'"); - } - - /* Validate the new position */ - if (newPosition < 0) { - return SDL_Error(SDL_EFSEEK); - } - if (newPosition > gCtx->hidden.ohosio.size) { - newPosition = gCtx->hidden.ohosio.size; - } - - movement = newPosition - gCtx->hidden.ohosio.position; - if (movement > 0) { - unsigned char buffer[4096]; - - /* The easy case where we're seeking forwards */ - while (movement > 0) { - Sint64 amount = sizeof(buffer); - size_t result; - if (amount > movement) { - amount = movement; - } - result = OHOS_FileRead(gCtx, buffer, 1, (size_t)amount); - if (result <= 0) { - /* Failed to read/skip the required amount, so fail */ - return -1; - } - - movement -= result; - } - - } else if (movement < 0) { - /* We can't seek backwards so we have to reopen the file and seek */ - /* forwards which obviously isn't very efficient */ - OHOS_FileClose(ctx, SDL_FALSE); - OHOS_FileOpen(ctx, gCtx->hidden.ohosio.fileName, gCtx->hidden.ohosio.mode); - OHOS_FileSeek(ctx, newPosition, RW_SEEK_SET); - } - } - - return gCtx->hidden.ohosio.position; -} - -size_t OHOS_FileRead(SDL_RWops *ctx, void *buffer, size_t size, size_t maxnum) -{ - if (gCtx->hidden.ohosio.nativeResourceManager) { - size_t bytesMax = size * maxnum; - size_t result; - if (gCtx->hidden.ohosio.size != -1 /* UNKNOWN_LENGTH */ && - gCtx->hidden.ohosio.position + bytesMax > gCtx->hidden.ohosio.size) { - bytesMax = gCtx->hidden.ohosio.size - gCtx->hidden.ohosio.position; - } - - RawFile *rawFile = static_cast(gCtx->hidden.ohosio.fileNameRef); - result = OH_ResourceManager_ReadRawFile(rawFile, buffer, bytesMax); - - if (result > 0) { - gCtx->hidden.ohosio.position += result; - return result / size; - } - return 0; - } else { - long bytesRemaining = size * maxnum; - long bytesMax = gCtx->hidden.ohosio.size - gCtx->hidden.ohosio.position; - int bytesRead = 0; - - /* Don't read more bytes than those that remain in the file, otherwise we get an exception */ - if (bytesRemaining > bytesMax) - bytesRemaining = bytesMax; - unsigned char byteBuffer[bytesRemaining]; - while (bytesRemaining > 0) { - RawFile *rawFile = static_cast(gCtx->hidden.ohosio.fileNameRef); - int result = OH_ResourceManager_ReadRawFile(rawFile, byteBuffer, bytesRemaining); - if (result < 0) { - break; - } - - bytesRemaining -= result; - bytesRead += result; - gCtx->hidden.ohosio.position += result; - } - return bytesRead / size; - } -} - -size_t OHOS_FileWrite(SDL_RWops *ctx, const void *buffer, size_t size, size_t num) -{ - SDL_SetError("Cannot write to OHOS package filesystem"); - return 0; -} - -int OHOS_FileClose(SDL_RWops *ctx, SDL_bool release) -{ - int result = 0; - - if (ctx) { - OHOS_CloseResourceManager(); - - if (release) { - SDL_FreeRW(ctx); - } - } - - return result; -} - -void OHOS_CloseResourceManager() -{ - RawFile * rawFile = static_cast(gCtx->hidden.ohosio.fileNameRef); - if (rawFile) { - OH_ResourceManager_CloseRawFile(rawFile); - } - - RawFileDescriptor *descriptor = static_cast(gCtx->hidden.ohosio.fileDescriptorRef); - if (descriptor) { - OH_ResourceManager_ReleaseRawFileDescriptor(*descriptor); - } -} - -/* Lock / Unlock Mutex */ -void OHOS_PageMutex_Lock() -{ - SDL_LockMutex(OHOS_PageMutex); -} - -void OHOS_PageMutex_Unlock() -{ - SDL_UnlockMutex(OHOS_PageMutex); -} - -/* Lock the Mutex when the Activity is in its 'Running' state */ -void OHOS_PageMutex_Lock_Running() -{ - 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); - goto retry; - } -} - -void OHOS_SetDisplayOrientation(int orientation) -{ - displayOrientation = (SDL_DisplayOrientation)orientation; -} - -SDL_DisplayOrientation OHOS_GetDisplayOrientation() -{ - return displayOrientation; -} - -void OHOS_NAPI_ShowTextInput(int x, int y, int w, int h) // 左上角的x轴坐标,左上角的y轴坐标,矩形宽度,矩形高度 -{ - size_t argc = 4; - napi_value args[1] = {nullptr}; - napi_value argv[4] = {nullptr}; - - napi_create_int32(napiCallback->env, x, &argv[0]); - napi_create_int32(napiCallback->env, y, &argv[1]); - napi_create_int32(napiCallback->env, w, &argv[2]); - napi_create_int32(napiCallback->env, h, &argv[3]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "showTextInput", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 4, argv, nullptr); -} - -SDL_bool OHOS_NAPI_RequestPermission(const char *permission) { - - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Napi begin to request permission."); - /* Wait for any pending request on another thread */ - while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) { - SDL_Delay(10); - } - SDL_AtomicSet(&bPermissionRequestPending, SDL_TRUE); - - napi_value argv[1] = {nullptr}; - napi_create_string_utf8(napiCallback->env, permission, NAPI_AUTO_LENGTH, &argv[0]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "requestPermission", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); - - /* Wait for the request to complete */ - while (SDL_AtomicGet(&bPermissionRequestPending) == SDL_TRUE) { - SDL_Delay(10); - } - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Napi request permission over."); - return bPermissionRequestResult; -} - -void OHOS_NAPI_HideTextInput(int a) { - size_t argc = 1; - napi_value argv[1] = {nullptr}; - napi_create_int32(napiCallback->env, a, &argv[0]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "hideTextInput", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); -} - -void OHOS_NAPI_ShouldMinimizeOnFocusLoss(int a) { - size_t argc = 1; - napi_value argv[1] = {nullptr}; - napi_create_int32(napiCallback->env, a, &argv[0]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "shouldMinimizeOnFocusLoss", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); -} - -void OHOS_NAPI_SetTitle(const char *title) { - size_t argc = 1; - napi_value args[1] = {nullptr}; - napi_value argv[1] = {nullptr}; - napi_create_string_utf8(napiCallback->env, title, NAPI_AUTO_LENGTH, &argv[0]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "setTitle", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); -} - -void OHOS_NAPI_SetWindowStyle(SDL_bool fullscreen) -{ - size_t argc = 1; - napi_value args[1] = {nullptr}; - napi_value argv[1] = {nullptr}; - - napi_get_boolean(napiCallback->env, fullscreen, &argv[0]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "setWindowStyle", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); -} - -// OHOS_NAPI_SetOrientation -void OHOS_NAPI_SetOrientation(int w, int h, int resizable, const char *hint) -{ - size_t argc = 4; - napi_value args[4] = {nullptr}; - napi_value argv[4] = {nullptr}; - napi_create_int32(napiCallback->env, w, &argv[0]); - napi_create_int32(napiCallback->env, h, &argv[1]); - napi_create_int32(napiCallback->env, resizable, &argv[2]); - napi_create_string_utf8(napiCallback->env, hint, NAPI_AUTO_LENGTH, &argv[3]); - - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, - &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "setOrientation", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 4, argv, nullptr); -} - -void OHOS_NAPI_SetPointer(int cursorID) -{ - napi_value argv[1] = {nullptr}; - napi_create_int32(napiCallback->env, cursorID, &argv[0]); - napi_value callback = nullptr; - napi_get_reference_value(napiCallback->env, napiCallback->callbackRef, &callback); - napi_value jsMethod; - napi_get_named_property(napiCallback->env, callback, "setPointer", &jsMethod); - napi_call_function(napiCallback->env, nullptr, jsMethod, 1, argv, nullptr); -} - -int OHOS_CreateCustomCursor(SDL_Surface *surface, int hot_x, int hot_y) -{ - return -1; -} - -SDL_bool OHOS_SetCustomCursor(int cursorID) -{ - return SDL_FALSE; -} - -SDL_bool OHOS_SetSystemCursor(int cursorID) -{ - OHOS_NAPI_SetPointer(cursorID); - return SDL_TRUE; -} - -/* Relative mouse support */ -SDL_bool OHOS_SupportsRelativeMouse(void) -{ - return SDL_TRUE; -} - -SDL_bool OHOS_SetRelativeMouseEnabled(SDL_bool enabled) -{ - return SDL_TRUE; -} - -napi_value SDLNapi::OHOS_SetResourceManager(napi_env env, napi_callback_info info) -{ - size_t argc = 2; - 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); - - if (gPath != nullptr) { - delete gPath; - gPath = nullptr; - } - - gPath = new char[len + 1]; - napi_get_value_string_utf8(env, args[0], gPath, len + 1, &len); - - gCtx = SDL_AllocRW(); - NativeResourceManager *nativeResourceManager = OH_ResourceManager_InitNativeResourceManager(env, args[1]); - gCtx->hidden.ohosio.nativeResourceManager = nativeResourceManager; - return nullptr; -} - -napi_value SDLNapi::OHOS_NativeSetScreenResolution(napi_env env, napi_callback_info info) -{ - size_t argc = 6; - napi_value args[6]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - int surfaceWidth; - int surfaceHeight; - int deviceWidth; - 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[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); - SDL_LockMutex(OHOS_PageMutex); - OHOS_SetScreenResolution(format, rate); - SDL_UnlockMutex(OHOS_PageMutex); - return nullptr; -} - -napi_value SDLNapi::OHOS_OnNativeResize(napi_env env, napi_callback_info info) -{ - SDL_LockMutex(OHOS_PageMutex); - if (OHOS_Window) { - OHOS_SendResize(OHOS_Window); - } - SDL_UnlockMutex(OHOS_PageMutex); - return nullptr; -} - -napi_value SDLNapi::OHOS_KeyDown(napi_env env, napi_callback_info info) -{ - int keycode; - size_t argc = 1; - napi_value args[1]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - napi_get_value_int32(env, args[0], &keycode); - return nullptr; -} - -napi_value SDLNapi::OHOS_KeyUp(napi_env env, napi_callback_info info) -{ - int keycode; - size_t argc = 1; - napi_value args[1]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - napi_get_value_int32(env, args[0], &keycode); - return nullptr; -} - -napi_value SDLNapi::OHOS_OnNativeKeyboardFocusLost(napi_env env, napi_callback_info info) -{ - SDL_StopTextInput(); - return nullptr; -} - -napi_value SDLNapi::OHOS_NativeSendQuit(napi_env env, napi_callback_info info) -{ - SDL_FlushEvents(SDL_FIRSTEVENT, SDL_LASTEVENT); - napi_value sum = 0; - SDL_SendQuit(); - SDL_SendAppEvent(SDL_APP_TERMINATING); - while (SDL_SemTryWait(OHOS_PauseSem) == 0) { - } - SDL_SemPost(OHOS_ResumeSem); - return nullptr; -} - -napi_value SDLNapi::OHOS_NativeQuit(napi_env env, napi_callback_info info) -{ - const char *str; - if (OHOS_PageMutex) { - SDL_DestroyMutex(OHOS_PageMutex); - OHOS_PageMutex = nullptr; - } - - if (OHOS_PauseSem) { - SDL_DestroySemaphore(OHOS_PauseSem); - OHOS_PauseSem = nullptr; - } - - if (OHOS_ResumeSem) { - SDL_DestroySemaphore(OHOS_ResumeSem); - OHOS_ResumeSem = nullptr; - } - - str = SDL_GetError(); - if (str && str[0]) { - } else { - } - return nullptr; -} - -napi_value SDLNapi::OHOS_NativeResume(napi_env env, napi_callback_info info) -{ - SDL_SemPost(OHOS_ResumeSem); - return nullptr; -} - -napi_value SDLNapi::OHOS_NativePause(napi_env env, napi_callback_info info) -{ - SDL_SemPost(OHOS_PauseSem); - return nullptr; -} - -napi_value SDLNapi::OHOS_NativePermissionResult(napi_env env, napi_callback_info info) -{ - size_t argc = 1; - napi_value args[1]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - bool result; - napi_get_value_bool(env, args[0], &result); - bPermissionRequestResult = result ? SDL_TRUE : SDL_FALSE; - SDL_AtomicSet(&bPermissionRequestPending, SDL_FALSE); - return nullptr; -} - -napi_value SDLNapi::OHOS_OnNativeOrientationChanged(napi_env env, napi_callback_info info) -{ - int orientation; - size_t argc = 1; - napi_value args[1]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - napi_get_value_int32(env, args[0], &orientation); - SDL_LockMutex(OHOS_PageMutex); - OHOS_SetDisplayOrientation(orientation); - if (OHOS_Window) { - SDL_VideoDisplay *display = SDL_GetDisplay(0); - SDL_SendDisplayEvent(display, SDL_DISPLAYEVENT_ORIENTATION, orientation); - } - SDL_UnlockMutex(OHOS_PageMutex); - return nullptr; -} - -napi_value SDLNapi::OHOS_NativeSetupNAPI(napi_env env, napi_callback_info info) -{ - SDL_AtomicSet(&bPermissionRequestPending, SDL_FALSE); - return nullptr; -} - -napi_value SDLNapi::OHOS_NAPI_RegisterCallback(napi_env env, napi_callback_info info) -{ - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "SDl begin to register callback."); - if (napiCallback == nullptr) { - napiCallback = std::make_unique(); - } - napiCallback->env = env; - size_t argc = 1; - napi_value args[1] = {nullptr}; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - napi_create_reference(env, args[0], 1, &napiCallback->callbackRef); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "SDl register callback over."); - return nullptr; -} - -napi_value SDLNapi::OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info info) -{ - size_t argc = 1; - napi_value args[1]; - napi_get_cb_info(env, info, &argc, args, nullptr, nullptr); - 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); - } - return nullptr; -} - -extern int main(int argc, char *argv[]); -static napi_value SDLAppEntry(napi_env env, napi_callback_info info) -{ - SDL_setenv("SDL_VIDEO_GL_DRIVER", "libGLESv3.so", 1); - SDL_setenv("SDL_VIDEO_EGL_DRIVER", "libEGL.so", 1); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Begin to enter sdl main fuc."); - //TODO pare info arg - char *argvs[4] = {"SDLTest_CommonCreateState\0", "--blend", "0", "--trackmem"}; - main(3,argvs); - return nullptr; -} - -napi_value SDLNapi::Init(napi_env env, napi_value exports) -{ - napi_property_descriptor desc[] = { - {"sdlAppEntry", nullptr, SDLAppEntry, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"registerCallback", nullptr, OHOS_NAPI_RegisterCallback, 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}, - {"keyDown", nullptr, OHOS_KeyDown, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"keyUp", nullptr, OHOS_KeyUp, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"onNativeKeyboardFocusLost", nullptr, OHOS_OnNativeKeyboardFocusLost, nullptr, nullptr, nullptr, napi_default, - nullptr}, - {"nativeSendQuit", nullptr, OHOS_NativeSendQuit, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"nativeResume", nullptr, OHOS_NativeResume, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"nativePause", nullptr, OHOS_NativePause, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"nativePermissionResult", nullptr, OHOS_NativePermissionResult, nullptr, nullptr, nullptr, napi_default, - nullptr}, - {"onNativeOrientationChanged", nullptr, OHOS_OnNativeOrientationChanged, nullptr, nullptr, nullptr, - napi_default, nullptr}, - {"nativeSetupNAPI", nullptr, OHOS_NativeSetupNAPI, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"setResourceManager", nullptr, OHOS_SetResourceManager, nullptr, nullptr, nullptr, napi_default, nullptr}, - {"onNativeFocusChanged", nullptr, OHOS_OnNativeFocusChanged, nullptr, nullptr, nullptr, napi_default, nullptr} - }; - napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc); - OHOS_XcomponentExport(env, exports); - return exports; -} - -EXTERN_C_START -static napi_value SDLNapiInit(napi_env env, napi_value exports) -{ - return SDLNapi::Init(env, exports); -} -EXTERN_C_END - -napi_module OHOSNapiModule = { - .nm_version = 1, - .nm_flags = 0, - .nm_filename = nullptr, - .nm_register_func = SDLNapiInit, - .nm_modname = "SDLNapi", - .nm_priv = ((void *)0), - .reserved = {0}, -}; - -extern "C" __attribute__((constructor)) void RegisterEntryModule(void) -{ - napi_module_register(&OHOSNapiModule); -} \ No newline at end of file diff --git a/src/main/ohos/testdraw2.c b/ohos-project/entry/src/main/cpp/application/testdraw2.c similarity index 61% rename from src/main/ohos/testdraw2.c rename to ohos-project/entry/src/main/cpp/application/testdraw2.c index 945698b743a474fab457a7e641e3fbed9793855e..cbe932e8c1194c14e0f6ae5c6a6a88b9345e0b9d 100644 --- a/src/main/ohos/testdraw2.c +++ b/ohos-project/entry/src/main/cpp/application/testdraw2.c @@ -13,12 +13,8 @@ * limitations under the License. */ -#include "../../core/ohos/SDL_ohos.h" -#include #include "SDL.h" -int main2(int argc, char *argv[]) { - SDL_bool ret; - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Test request permission begin."); - ret = OHOS_NAPI_RequestPermission("ohos.permission.MICROPHONE"); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Test request permission over, ret = %{public}d", ret); +int main(int argc, char *argv[]) { + SDL_Log("entry"); + return 0; } \ No newline at end of file diff --git a/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts b/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts index 6fbc3fc6f287e1ee19a43ea4fc7dd4d25fd9e722..0b6d6181b0910c9848c878c45ebff9aad367152b 100644 --- a/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts +++ b/ohos-project/entry/src/main/ets/workers/SDLAppWork.ts @@ -156,7 +156,7 @@ let callbackRef: NapiCallback = new ArkNapiCallback(); workerPort.onmessage = function (event: MessageEvents) { sdl.nativeSetupNAPI(); sdl.registerCallback(callbackRef); - sdl.sdlAppEntry(); + sdl.sdlAppEntry("libentry.so","main"); } /** diff --git a/src/core/ohos/SDL_ohos.cpp b/src/core/ohos/SDL_ohos.cpp index 690ff73097194407f3580e5651582013239c6f97..9c3425c136423da47dc298d0c30041a9cd6a0943 100644 --- a/src/core/ohos/SDL_ohos.cpp +++ b/src/core/ohos/SDL_ohos.cpp @@ -19,6 +19,7 @@ extern "C" { #endif #include #include +#include #include "SDL.h" #include "../video/../../video/SDL_sysvideo.h" #include "../events/../../events/SDL_windowevents_c.h" @@ -734,20 +735,98 @@ napi_value SDLNapi::OHOS_OnNativeFocusChanged(napi_env env, napi_callback_info i return nullptr; } -extern int main(int argc, char *argv[]); +typedef int (*SDL_main_func)(int argc, char *argv[]); static napi_value SDLAppEntry(napi_env env, napi_callback_info info) { - SDL_setenv("SDL_VIDEO_GL_DRIVER", "libGLESv3.so", 1); - SDL_setenv("SDL_VIDEO_EGL_DRIVER", "libEGL.so", 1); - OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Begin to enter sdl main fuc."); - //TODO pare info arg - //char *argvs[4] = {"SDLTest_CommonCreateState\0", "--blend", "0", "--trackmem"}; + char *library_file; + void *library_handle; + size_t buffer_size; - char *argvs[3] = {"--font", "0", "--trackmem"}; - main(3,argvs); + napi_value argv[10]; + size_t argc = 10; + napi_status status; + napi_valuetype valuetype; + + status = napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr); + if (status != napi_ok) { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry():failed to obtained argument!"); + return nullptr; + } + status = napi_typeof(env, argv[0], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry():invalid type of argument!"); + return nullptr; + } + 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_handle = dlopen(library_file, RTLD_GLOBAL); + + if (library_handle) { + SDL_main_func SDL_main; + char *function_name; + + status = napi_typeof(env, argv[1], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry():invalid type of argument!"); + SDL_free(library_file); + return nullptr; + } + 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); + SDL_main = (SDL_main_func)dlsym(library_handle, function_name); + + if (SDL_main) { + char** argvs; + int argcs = 0; + int i; + bool isstack; + + SDL_setenv("SDL_VIDEO_GL_DRIVER", "libGLESv3.so", 1); + SDL_setenv("SDL_VIDEO_EGL_DRIVER", "libEGL.so", 1); + //TODO pare info arg + argvs = SDL_small_alloc(char *, argc , &isstack); + argvs[argcs++] = SDL_strdup("app_process"); + for (i = 2; i < argc; ++i) { + char *arg = NULL; + status = napi_typeof(env, argv[i], &valuetype); + if (status != napi_ok || valuetype != napi_string) { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry():invalid type of argument!"); + break; + } + napi_get_value_string_utf8(env, argv[i], nullptr, 0, &buffer_size); + arg = new char[buffer_size]; + napi_get_value_string_utf8(env, argv[i], arg, buffer_size+1, &buffer_size); + if (!arg) { + arg = SDL_strdup(""); + } + argvs[argcs++] = arg; + } + if(i == argc) { + OH_LOG_Print(LOG_APP, LOG_INFO, 0xFF00, "EGLAPP", "Begin to enter sdl main fuc."); + argvs[argcs] = NULL; + SDL_main(argcs, argvs); + } + for (i = 0; i < argcs; ++i) { + SDL_free(argvs[i]); + } + SDL_small_free(argvs, isstack); + } + else{ + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry(): Couldn't find function %{public}s in library %{public}s", function_name, library_file); + } + SDL_free(function_name); + dlclose(library_handle); + } else { + OH_LOG_Print(LOG_APP, LOG_ERROR, 0xFF00, "EGLAPP", "SDLAppEntry():ERROR: %{public}s", dlerror()); + } + SDL_free(library_file); return nullptr; } + napi_value SDLNapi::Init(napi_env env, napi_value exports) { napi_property_descriptor desc[] = {