Skip to content

Commit 2155440

Browse files
committed
Harmony port: window fetching
1 parent b10346e commit 2155440

File tree

5 files changed

+147
-2
lines changed

5 files changed

+147
-2
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,6 +1510,7 @@ elseif(OHOS)
15101510
set(CMAKE_CXX_COMPILER_EXTERNAL_TOOLCHAIN "")
15111511

15121512
sdl_link_dependency(OHOS_LIBS LIBS ace_napi.z hilog_ndk.z ace_ndk.z rawfile.z pixelmap_ndk.z)
1513+
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/core/ohos/*.c")
15131514

15141515
set(SDL_LOADSO_DLOPEN 1)
15151516
sdl_glob_sources("${SDL3_SOURCE_DIR}/src/loadso/dlopen/*.c")

include/build_config/SDL_build_config.h.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@
265265
#cmakedefine SDL_AUDIO_DRIVER_JACK 1
266266
#cmakedefine SDL_AUDIO_DRIVER_JACK_DYNAMIC @SDL_AUDIO_DRIVER_JACK_DYNAMIC@
267267
#cmakedefine SDL_AUDIO_DRIVER_NETBSD 1
268+
#cmakedefine SDL_VIDEO_DRIVER_OHOS 1
268269
#cmakedefine SDL_AUDIO_DRIVER_OSS 1
269270
#cmakedefine SDL_AUDIO_DRIVER_PIPEWIRE 1
270271
#cmakedefine SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC @SDL_AUDIO_DRIVER_PIPEWIRE_DYNAMIC@
@@ -391,6 +392,7 @@
391392
#cmakedefine SDL_VIDEO_DRIVER_N3DS 1
392393
#cmakedefine SDL_VIDEO_DRIVER_NGAGE 1
393394
#cmakedefine SDL_VIDEO_DRIVER_OFFSCREEN 1
395+
#cmakedefine SDL_VIDEO_DRIVER_OHOS 1
394396
#cmakedefine SDL_VIDEO_DRIVER_PS2 1
395397
#cmakedefine SDL_VIDEO_DRIVER_PSP 1
396398
#cmakedefine SDL_VIDEO_DRIVER_RISCOS 1

src/SDL_log.c

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@
3636
#include <android/log.h>
3737
#endif
3838

39+
#ifdef SDL_PLATFORM_OHOS
40+
#include <hilog/log.h>
41+
#endif
42+
3943
#include "stdlib/SDL_vacopy.h"
4044

4145
// The size of the stack buffer to use for rendering log messages.
@@ -120,6 +124,19 @@ static int SDL_android_priority[] = {
120124
SDL_COMPILE_TIME_ASSERT(android_priority, SDL_arraysize(SDL_android_priority) == SDL_LOG_PRIORITY_COUNT);
121125
#endif // SDL_PLATFORM_ANDROID
122126

127+
#ifdef SDL_PLATFORM_OHOS
128+
static int SDL_ohos_priority[] = {
129+
LOG_DEBUG,
130+
LOG_DEBUG,
131+
LOG_DEBUG,
132+
LOG_DEBUG,
133+
LOG_INFO,
134+
LOG_WARN,
135+
LOG_ERROR,
136+
LOG_FATAL
137+
};
138+
#endif
139+
123140
static void SDLCALL SDL_LoggingChanged(void *userdata, const char *name, const char *oldValue, const char *hint)
124141
{
125142
SDL_ResetLogPriorities();
@@ -556,7 +573,7 @@ void SDL_LogMessage(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_ST
556573
va_end(ap);
557574
}
558575

559-
#ifdef SDL_PLATFORM_ANDROID
576+
#if defined(SDL_PLATFORM_ANDROID) || defined(SDL_PLATFORM_OHOS)
560577
static const char *GetCategoryPrefix(int category)
561578
{
562579
if (category < SDL_LOG_CATEGORY_RESERVED2) {
@@ -567,7 +584,7 @@ static const char *GetCategoryPrefix(int category)
567584
}
568585
return "CUSTOM";
569586
}
570-
#endif // SDL_PLATFORM_ANDROID
587+
#endif
571588

572589
void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_STRING const char *fmt, va_list ap)
573590
{
@@ -751,6 +768,13 @@ static void SDLCALL SDL_LogOutput(void *userdata, int category, SDL_LogPriority
751768
SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
752769
__android_log_write(SDL_android_priority[priority], tag, message);
753770
}
771+
#elif defined(SDL_PLATFORM_OHOS)
772+
{
773+
char tag[32];
774+
775+
SDL_snprintf(tag, SDL_arraysize(tag), "SDL/%s", GetCategoryPrefix(category));
776+
OH_LOG_Print(LOG_APP, SDL_ohos_priority[priority], 0, tag, "%{public}s", message);
777+
}
754778
#elif defined(SDL_PLATFORM_APPLE) && (defined(SDL_VIDEO_DRIVER_COCOA) || defined(SDL_VIDEO_DRIVER_UIKIT))
755779
/* Technically we don't need Cocoa/UIKit, but that's where this function is defined for now.
756780
*/

src/core/ohos/SDL_ohos.c

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
#include "SDL_internal.h"
2+
3+
#ifdef SDL_PLATFORM_OHOS
4+
5+
#include "napi/native_api.h"
6+
#include "SDL_ohos.h"
7+
#include <ace/xcomponent/native_interface_xcomponent.h>
8+
9+
OHNativeWindow *nativeWindow;
10+
SDL_Mutex *g_ohosPageMutex = NULL;
11+
static OH_NativeXComponent_Callback callback;
12+
static OH_NativeXComponent_MouseEvent_Callback mouseCallback;
13+
14+
static napi_value minus(napi_env env, napi_callback_info info)
15+
{
16+
size_t argc = 2;
17+
napi_value args[2] = { NULL };
18+
19+
napi_get_cb_info(env, info, &argc, args, NULL, NULL);
20+
21+
napi_valuetype valuetype0;
22+
napi_typeof(env, args[0], &valuetype0);
23+
24+
napi_valuetype valuetype1;
25+
napi_typeof(env, args[1], &valuetype1);
26+
27+
double value0;
28+
napi_get_value_double(env, args[0], &value0);
29+
30+
double value1;
31+
napi_get_value_double(env, args[1], &value1);
32+
33+
napi_value sum;
34+
napi_create_double(env, value0 - value1, &sum);
35+
36+
return sum;
37+
}
38+
39+
static void OnSurfaceCreatedCB(OH_NativeXComponent *component, void *window)
40+
{
41+
nativeWindow = (OHNativeWindow *)window;
42+
43+
SDL_Log("Native Window: %p", nativeWindow);
44+
}
45+
static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window) {}
46+
static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window) {}
47+
static void onKeyEvent(OH_NativeXComponent *component, void *window) {}
48+
static void onNativeTouch(OH_NativeXComponent *component, void *window) {}
49+
static void onNativeMouse(OH_NativeXComponent *component, void *window) {}
50+
static void OnDispatchTouchEventCB(OH_NativeXComponent *component, void *window) {}
51+
void OnHoverEvent(OH_NativeXComponent *component, bool isHover) {}
52+
void OnFocusEvent(OH_NativeXComponent *component, void *window) {}
53+
void OnBlurEvent(OH_NativeXComponent *component, void *window) {}
54+
55+
static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
56+
{
57+
napi_property_descriptor desc[] = {
58+
{ "minus", NULL, minus, NULL, NULL, NULL, napi_default, NULL }
59+
};
60+
napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
61+
62+
napi_value exportInstance = NULL;
63+
if (napi_get_named_property(env, exports, OH_NATIVE_XCOMPONENT_OBJ, &exportInstance) != napi_ok) {
64+
return exports;
65+
}
66+
OH_NativeXComponent *nativeXComponent;
67+
if (napi_unwrap(env, exportInstance, (void **)(&nativeXComponent)) != napi_ok) {
68+
return exports;
69+
}
70+
71+
callback.OnSurfaceCreated = OnSurfaceCreatedCB;
72+
callback.OnSurfaceChanged = OnSurfaceChangedCB;
73+
callback.OnSurfaceDestroyed = OnSurfaceDestroyedCB;
74+
callback.DispatchTouchEvent = onNativeTouch;
75+
OH_NativeXComponent_RegisterCallback(nativeXComponent, &callback);
76+
77+
mouseCallback.DispatchMouseEvent = OnDispatchTouchEventCB;
78+
mouseCallback.DispatchMouseEvent = onNativeMouse;
79+
mouseCallback.DispatchHoverEvent = OnHoverEvent;
80+
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);
81+
82+
OH_NativeXComponent_RegisterKeyEventCallback(nativeXComponent, onKeyEvent);
83+
OH_NativeXComponent_RegisterFocusEventCallback(nativeXComponent, OnFocusEvent);
84+
OH_NativeXComponent_RegisterBlurEventCallback(nativeXComponent, OnBlurEvent);
85+
86+
g_ohosPageMutex = SDL_CreateMutex();
87+
88+
return exports;
89+
}
90+
91+
napi_module OHOS_NAPI_Module = {
92+
.nm_version = 1,
93+
.nm_flags = 0,
94+
.nm_filename = NULL,
95+
.nm_register_func = SDL_OHOS_NAPI_Init,
96+
.nm_modname = "SDL3",
97+
.nm_priv = ((void *)0),
98+
.reserved = { 0 },
99+
};
100+
101+
__attribute__((constructor)) void RegisterEntryModule(void)
102+
{
103+
napi_module_register(&OHOS_NAPI_Module);
104+
}
105+
106+
#endif

src/core/ohos/SDL_ohos.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef SDL_OHOS_H
2+
#define SDL_OHOS_H
3+
4+
#include "SDL3/SDL_mutex.h"
5+
#include "video/SDL_sysvideo.h"
6+
#include <native_window/external_window.h>
7+
8+
extern SDL_Mutex *g_ohosPageMutex;
9+
void SDL_OHOS_SetDisplayOrientation(int orientation);
10+
extern OHNativeWindow *nativeWindow;
11+
12+
#endif

0 commit comments

Comments
 (0)