Harmony port: window fetching

This commit is contained in:
Jack253-png 2025-05-30 20:21:35 +08:00
parent 8f0b9c1151
commit 1c48676f4f
No known key found for this signature in database
GPG key ID: 51EA61206B02D886
5 changed files with 147 additions and 2 deletions

View file

@ -1510,6 +1510,7 @@ elseif(OHOS)
set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "")
sdl_link_dependency(OHOS_LIBS LIBS ace_napi.z hilog_ndk.z ace_ndk.z rawfile.z pixelmap_ndk.z)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/core/ohos/*.c")
set(SDL_LOADSO_DLOPEN 1)
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/loadso/dlopen/*.c")

View file

@ -265,6 +265,7 @@
#cmakedefine SDL_AUDIO_DRIVER_JACK 1
#cmakedefine SDL_AUDIO_DRIVER_JACK_DYNAMIC @SDL_AUDIO_DRIVER_JACK_DYNAMIC@
#cmakedefine SDL_AUDIO_DRIVER_NETBSD 1
#cmakedefine SDL_VIDEO_DRIVER_OHOS 1
#cmakedefine SDL_AUDIO_DRIVER_OSS 1
#cmakedefine SDL_AUDIO_DRIVER_PIPEWIRE 1
#cmakedefine SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC @SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC@
@ -391,6 +392,7 @@
#cmakedefine SDL_VIDEO_DRIVER_N3DS 1
#cmakedefine SDL_VIDEO_DRIVER_NGAGE 1
#cmakedefine SDL_VIDEO_DRIVER_OFFSCREEN 1
#cmakedefine SDL_VIDEO_DRIVER_OHOS 1
#cmakedefine SDL_VIDEO_DRIVER_PS2 1
#cmakedefine SDL_VIDEO_DRIVER_PSP 1
#cmakedefine SDL_VIDEO_DRIVER_RISCOS 1

View file

@ -36,6 +36,10 @@
#include <android/log.h>
#endif
#ifdef SDL_PLATFORM_OHOS
#include <hilog/log.h>
#endif
#include "stdlib/SDL_vacopy.h"
// The size of the stack buffer to use for rendering log messages.
@ -120,6 +124,19 @@ static int SDL_android_priority[] = {
SDL_COMPILE_TIME_ASSERT(android_priority, SDL_arraysize(SDL_android_priority) == SDL_LOG_PRIORITY_COUNT);
#endif // SDL_PLATFORM_ANDROID
#ifdef SDL_PLATFORM_OHOS
static int SDL_ohos_priority[] = {
LOG_DEBUG,
LOG_DEBUG,
LOG_DEBUG,
LOG_DEBUG,
LOG_INFO,
LOG_WARN,
LOG_ERROR,
LOG_FATAL
};
#endif
static void SDLCALL SDL_LoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
{
SDL_ResetLogPriorities();
@ -556,7 +573,7 @@ void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_ST
va_end(ap);
}
#ifdef SDL_PLATFORM_ANDROID
#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OHOS)
static const char *GetCategoryPrefix(int category)
{
if (category < SDL_LOG_CATEGORY_RESERVED2) {
@ -567,7 +584,7 @@ static const char *GetCategoryPrefix(int category)
}
return "CUSTOM";
}
#endif // SDL_PLATFORM_ANDROID
#endif
void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
{
@ -751,6 +768,13 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
__android_log_write(SDL_android_priority[priority], tag, message);
}
#elif defined(SDL_PLATFORM_OHOS)
{
char tag[32];
SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
OH_LOG_Print(LOG_APP, SDL_ohos_priority[priority], 0, tag, "%{public}s", message);
}
#elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
/* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
*/

106
src/core/ohos/SDL_ohos.c Normal file
View file

@ -0,0 +1,106 @@
#include "SDL_internal.h"
#ifdef SDL_PLATFORM_OHOS
#include "napi/native_api.h"
#include "SDL_ohos.h"
#include <ace/xcomponent/native_interface_xcomponent.h>
OHNativeWindow *nativeWindow;
SDL_Mutex *g_ohosPageMutex = NULL;
static OH_NativeXComponent_Callback callback;
static OH_NativeXComponent_MouseEvent_Callback mouseCallback;
static napi_value minus(napi_env env, napi_callback_info info)
{
size_t argc = 2;
napi_value args[2] = { NULL };
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
napi_valuetype valuetype0;
napi_typeof(env, args[0], &valuetype0);
napi_valuetype valuetype1;
napi_typeof(env, args[1], &valuetype1);
double value0;
napi_get_value_double(env, args[0], &value0);
double value1;
napi_get_value_double(env, args[1], &value1);
napi_value sum;
napi_create_double(env, value0 - value1, &sum);
return sum;
}
static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
{
nativeWindow = (OHNativeWindow *)window;
SDL_Log("Native Window: %p", nativeWindow);
}
static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) {}
static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {}
static void onKeyEvent(OH_NativeXComponent *component, void *window) {}
static void onNativeTouch(OH_NativeXComponent *component, void *window) {}
static void onNativeMouse(OH_NativeXComponent *component, void *window) {}
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) {}
void OnHoverEvent(OH_NativeXComponent *component, bool isHover) {}
void OnFocusEvent(OH_NativeXComponent *component, void *window) {}
void OnBlurEvent(OH_NativeXComponent *component, void *window) {}
static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
{
napi_property_descriptor desc[] = {
{ "minus", NULL, minus, NULL, NULL, NULL, napi_default, NULL }
};
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
napi_value exportInstance = NULL;
if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
return exports;
}
OH_NativeXComponent *nativeXComponent;
if (napi_unwrap(env, exportInstance, (void **)(&nativeXComponent)) != napi_ok) {
return exports;
}
callback.OnSurfaceCreated = OnSurfaceCreatedCB;
callback.OnSurfaceChanged = OnSurfaceChangedCB;
callback.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
callback.DispatchTouchEvent = onNativeTouch;
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback);
mouseCallback.DispatchMouseEvent = OnDispatchTouchEventCB;
mouseCallback.DispatchMouseEvent = onNativeMouse;
mouseCallback.DispatchHoverEvent = OnHoverEvent;
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);
OH_NativeXComponent_RegisterKeyEventCallback(nativeXComponent, onKeyEvent);
OH_NativeXComponent_RegisterFocusEventCallback(nativeXComponent, OnFocusEvent);
OH_NativeXComponent_RegisterBlurEventCallback(nativeXComponent, OnBlurEvent);
g_ohosPageMutex = SDL_CreateMutex();
return exports;
}
napi_module OHOS_NAPI_Module = {
.nm_version = 1,
.nm_flags = 0,
.nm_filename = NULL,
.nm_register_func = SDL_OHOS_NAPI_Init,
.nm_modname = "SDL3",
.nm_priv = ((void *)0),
.reserved = { 0 },
};
__attribute__((constructor)) void RegisterEntryModule(void)
{
napi_module_register(&OHOS_NAPI_Module);
}
#endif

12
src/core/ohos/SDL_ohos.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef SDL_OHOS_H
#define SDL_OHOS_H
#include "SDL3/SDL_mutex.h"
#include "video/SDL_sysvideo.h"
#include <native_window/external_window.h>
extern SDL_Mutex *g_ohosPageMutex;
void SDL_OHOS_SetDisplayOrientation(int orientation);
extern OHNativeWindow *nativeWindow;
#endif