10
10
11
11
#ifdef SDL_PLATFORM_OHOS
12
12
13
- #include "napi/native_api.h"
14
- #include "SDL_ohos.h"
15
- #include <ace/xcomponent/native_interface_xcomponent.h>
16
- #include "../../video/ohos/SDL_ohosvideo.h"
13
+ #include "../../video/ohos/SDL_ohoskeyboard.h"
17
14
#include "../../video/ohos/SDL_ohostouch.h"
15
+ #include "../../video/ohos/SDL_ohosvideo.h"
18
16
#include "SDL3/SDL_mutex.h"
19
- #include "../../video/ohos/SDL_ohoskeyboard.h"
17
+ #include "SDL_ohos.h"
18
+ #include "napi/native_api.h"
19
+ #include <ace/xcomponent/native_interface_xcomponent.h>
20
20
21
21
static OHNativeWindow * g_ohosNativeWindow ;
22
22
static SDL_Mutex * g_ohosPageMutex = NULL ;
@@ -46,19 +46,19 @@ typedef struct
46
46
int i ;
47
47
long long l ;
48
48
double d ;
49
- const char * str ;
49
+ const char * str ;
50
50
} data ;
51
51
} napiCallbackArg ;
52
52
typedef struct
53
53
{
54
- const char * func ;
54
+ const char * func ;
55
55
int argCount ;
56
56
napiCallbackArg arg [16 ];
57
57
napiArgType type ;
58
58
napiCallbackArg ret ;
59
59
} napiCallbackData ;
60
60
61
- void OHOS_windowDataFill (SDL_Window * w )
61
+ void OHOS_windowDataFill (SDL_Window * w )
62
62
{
63
63
w -> internal = SDL_calloc (1 , sizeof (SDL_WindowData ));
64
64
w -> x = x ;
@@ -69,47 +69,35 @@ void OHOS_windowDataFill(SDL_Window* w)
69
69
70
70
SDL_VideoDevice * _this = SDL_GetVideoDevice ();
71
71
72
- if (_this -> windows == NULL )
73
- {
72
+ if (_this -> windows == NULL ) {
74
73
_this -> windows = w ;
75
- }
76
- else
77
- {
74
+ } else {
78
75
_this -> windows -> next = w ;
79
76
w -> prev = _this -> windows ;
80
77
}
81
78
82
79
#ifdef SDL_VIDEO_OPENGL_EGL
83
80
if (w -> flags & SDL_WINDOW_OPENGL ) {
84
81
SDL_LockMutex (g_ohosPageMutex );
85
- if (w -> internal -> egl_surface == EGL_NO_SURFACE )
86
- {
82
+ if (w -> internal -> egl_surface == EGL_NO_SURFACE ) {
87
83
w -> internal -> egl_surface = SDL_EGL_CreateSurface (_this , w , (NativeWindowType )g_ohosNativeWindow );
88
84
}
89
85
SDL_UnlockMutex (g_ohosPageMutex );
90
86
}
91
87
#endif
92
88
}
93
- void OHOS_removeWindow (SDL_Window * w )
89
+ void OHOS_removeWindow (SDL_Window * w )
94
90
{
95
91
SDL_VideoDevice * _this = SDL_GetVideoDevice ();
96
- if (_this -> windows == w )
97
- {
92
+ if (_this -> windows == w ) {
98
93
_this -> windows = _this -> windows -> next ;
99
- }
100
- else
101
- {
102
- SDL_Window * curWin = _this -> windows ;
103
- while (curWin != NULL )
104
- {
105
- if (curWin == w )
106
- {
107
- if (curWin -> next == NULL )
108
- {
94
+ } else {
95
+ SDL_Window * curWin = _this -> windows ;
96
+ while (curWin != NULL ) {
97
+ if (curWin == w ) {
98
+ if (curWin -> next == NULL ) {
109
99
curWin -> prev -> next = NULL ;
110
- }
111
- else
112
- {
100
+ } else {
113
101
curWin -> prev -> next = curWin -> next ;
114
102
curWin -> next -> prev = curWin -> prev ;
115
103
}
@@ -122,12 +110,10 @@ void OHOS_removeWindow(SDL_Window* w)
122
110
#ifdef SDL_VIDEO_OPENGL_EGL
123
111
if (w -> flags & SDL_WINDOW_OPENGL ) {
124
112
SDL_LockMutex (g_ohosPageMutex );
125
- if (w -> internal -> egl_context )
126
- {
113
+ if (w -> internal -> egl_context ) {
127
114
SDL_EGL_DestroyContext (_this , w -> internal -> egl_context );
128
115
}
129
- if (w -> internal -> egl_surface != EGL_NO_SURFACE )
130
- {
116
+ if (w -> internal -> egl_surface != EGL_NO_SURFACE ) {
131
117
SDL_EGL_DestroySurface (_this , w -> internal -> egl_surface );
132
118
}
133
119
SDL_UnlockMutex (g_ohosPageMutex );
@@ -179,62 +165,68 @@ static napi_value minus(napi_env env, napi_callback_info info)
179
165
return sum ;
180
166
}
181
167
182
- static void sdlJSCallback (napi_env env , napi_value jsCb , void * content , void * data )
168
+ static void sdlJSCallback (napi_env env , napi_value jsCb , void * content , void * data )
183
169
{
184
- napiCallbackData * ar = (napiCallbackData * ) data ;
170
+ napiCallbackData * ar = (napiCallbackData * ) data ;
185
171
186
172
napi_value callb = NULL ;
187
173
napi_get_reference_value (env , napiEnv .interface , & callb );
188
174
napi_value jsMethod = NULL ;
189
175
napi_get_named_property (env , callb , ar -> func , & jsMethod );
190
176
191
177
napi_value args [16 ];
192
- for (int i = 0 ; i < ar -> argCount ; i ++ )
193
- {
194
- switch ( ar -> arg [ i ]. type )
178
+ for (int i = 0 ; i < ar -> argCount ; i ++ ) {
179
+ switch ( ar -> arg [ i ]. type ) {
180
+ case Int :
195
181
{
196
- case Int : {
197
- napi_create_int32 (env , ar -> arg [i ].data .i , args + i );
198
- break ;
199
- }
200
- case Long : {
201
- napi_create_int64 (env , ar -> arg [i ].data .l , args + i );
202
- break ;
203
- }
204
- case Double : {
205
- napi_create_double (env , ar -> arg [i ].data .d , args + i );
206
- break ;
207
- }
208
- case String : {
209
- napi_create_string_utf8 (env , ar -> arg [i ].data .str , SDL_strlen (ar -> arg [i ].data .str ), args + i );
210
- break ;
211
- }
212
- }
213
- }
214
-
215
- napi_value v ;
216
- napi_call_function (env , NULL , jsMethod , ar -> argCount , args , & v );
217
- switch (ar -> type ) {
218
- case Int : {
219
- napi_get_value_int32 (env , v , & ar -> ret .data .i );
182
+ napi_create_int32 (env , ar -> arg [i ].data .i , args + i );
220
183
break ;
221
184
}
222
- case Long : {
223
- napi_get_value_int64 (env , v , (int64_t * ) & ar -> ret .data .l );
185
+ case Long :
186
+ {
187
+ napi_create_int64 (env , ar -> arg [i ].data .l , args + i );
224
188
break ;
225
189
}
226
- case String : {
227
- size_t stringSize = 0 ;
228
- napi_get_value_string_utf8 (env , args [1 ], NULL , 0 , & stringSize );
229
- char * value = SDL_malloc (stringSize + 1 );
230
- napi_get_value_string_utf8 (env , args [1 ], value , stringSize + 1 , & stringSize );
231
- ar -> ret .data .str = value ;
190
+ case Double :
191
+ {
192
+ napi_create_double (env , ar -> arg [i ].data .d , args + i );
232
193
break ;
233
194
}
234
- case Double : {
235
- napi_get_value_double (env , v , & ar -> ret .data .d );
195
+ case String :
196
+ {
197
+ napi_create_string_utf8 (env , ar -> arg [i ].data .str , SDL_strlen (ar -> arg [i ].data .str ), args + i );
236
198
break ;
237
199
}
200
+ }
201
+ }
202
+
203
+ napi_value v ;
204
+ napi_call_function (env , NULL , jsMethod , ar -> argCount , args , & v );
205
+ switch (ar -> type ) {
206
+ case Int :
207
+ {
208
+ napi_get_value_int32 (env , v , & ar -> ret .data .i );
209
+ break ;
210
+ }
211
+ case Long :
212
+ {
213
+ napi_get_value_int64 (env , v , (int64_t * )& ar -> ret .data .l );
214
+ break ;
215
+ }
216
+ case String :
217
+ {
218
+ size_t stringSize = 0 ;
219
+ napi_get_value_string_utf8 (env , args [1 ], NULL , 0 , & stringSize );
220
+ char * value = SDL_malloc (stringSize + 1 );
221
+ napi_get_value_string_utf8 (env , args [1 ], value , stringSize + 1 , & stringSize );
222
+ ar -> ret .data .str = value ;
223
+ break ;
224
+ }
225
+ case Double :
226
+ {
227
+ napi_get_value_double (env , v , & ar -> ret .data .d );
228
+ break ;
229
+ }
238
230
}
239
231
}
240
232
@@ -257,7 +249,7 @@ static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
257
249
data -> argCount = 0 ;
258
250
259
251
napi_call_threadsafe_function (napiEnv .func , data , napi_tsfn_nonblocking );
260
-
252
+
261
253
SDL_free (data );
262
254
263
255
napi_value result ;
@@ -273,16 +265,16 @@ static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
273
265
274
266
size_t libstringSize = 0 ;
275
267
napi_get_value_string_utf8 (env , args [0 ], NULL , 0 , & libstringSize );
276
- char * libname = SDL_malloc (libstringSize + 1 );
268
+ char * libname = SDL_malloc (libstringSize + 1 );
277
269
napi_get_value_string_utf8 (env , args [0 ], libname , libstringSize + 1 , & libstringSize );
278
270
279
271
size_t fstringSize = 0 ;
280
272
napi_get_value_string_utf8 (env , args [1 ], NULL , 0 , & fstringSize );
281
- char * fname = SDL_malloc (fstringSize + 1 );
273
+ char * fname = SDL_malloc (fstringSize + 1 );
282
274
napi_get_value_string_utf8 (env , args [1 ], fname , fstringSize + 1 , & fstringSize );
283
275
284
- void * lib = dlopen (libname , RTLD_LAZY );
285
- void * func = dlsym (lib , fname );
276
+ void * lib = dlopen (libname , RTLD_LAZY );
277
+ void * func = dlsym (lib , fname );
286
278
typedef int (* test )();
287
279
((test )func )();
288
280
dlclose (lib );
@@ -332,16 +324,13 @@ static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
332
324
{
333
325
SDL_VideoDevice * _this = SDL_GetVideoDevice ();
334
326
SDL_Window * win = _this -> windows ;
335
- while (win != NULL )
336
- {
327
+ while (win != NULL ) {
337
328
#ifdef SDL_VIDEO_OPENGL_EGL
338
329
if (win -> flags & SDL_WINDOW_OPENGL ) {
339
- if (win -> internal -> egl_context )
340
- {
330
+ if (win -> internal -> egl_context ) {
341
331
SDL_EGL_DestroyContext (_this , win -> internal -> egl_context );
342
332
}
343
- if (win -> internal -> egl_surface )
344
- {
333
+ if (win -> internal -> egl_surface ) {
345
334
SDL_EGL_DestroySurface (_this , win -> internal -> egl_surface );
346
335
}
347
336
}
@@ -352,8 +341,7 @@ static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
352
341
static void onKeyEvent (OH_NativeXComponent * component , void * window )
353
342
{
354
343
OH_NativeXComponent_KeyEvent * keyEvent = NULL ;
355
- if (OH_NativeXComponent_GetKeyEvent (component , & keyEvent ) >= 0 )
356
- {
344
+ if (OH_NativeXComponent_GetKeyEvent (component , & keyEvent ) >= 0 ) {
357
345
OH_NativeXComponent_KeyAction action ;
358
346
OH_NativeXComponent_KeyCode code ;
359
347
OH_NativeXComponent_EventSourceType sourceType ;
@@ -362,14 +350,10 @@ static void onKeyEvent(OH_NativeXComponent *component, void *window)
362
350
OH_NativeXComponent_GetKeyEventCode (keyEvent , & code );
363
351
OH_NativeXComponent_GetKeyEventSourceType (keyEvent , & sourceType );
364
352
365
- if (sourceType == OH_NATIVEXCOMPONENT_SOURCE_TYPE_KEYBOARD )
366
- {
367
- if (OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN == action )
368
- {
353
+ if (sourceType == OH_NATIVEXCOMPONENT_SOURCE_TYPE_KEYBOARD ) {
354
+ if (OH_NATIVEXCOMPONENT_KEY_ACTION_DOWN == action ) {
369
355
OHOS_OnKeyDown (code );
370
- }
371
- else if (OH_NATIVEXCOMPONENT_KEY_ACTION_UP == action )
372
- {
356
+ } else if (OH_NATIVEXCOMPONENT_KEY_ACTION_UP == action ) {
373
357
OHOS_OnKeyUp (code );
374
358
}
375
359
}
@@ -385,8 +369,7 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
385
369
OH_NativeXComponent_GetTouchEvent (component , window , & touchEvent );
386
370
OH_NativeXComponent_GetTouchPointToolType (component , 0 , & toolType );
387
371
388
- for (int i = 0 ; i < touchEvent .numPoints ; i ++ )
389
- {
372
+ for (int i = 0 ; i < touchEvent .numPoints ; i ++ ) {
390
373
SDL_OHOSTouchEvent e ;
391
374
e .timestamp = touchEvent .timeStamp ;
392
375
e .deviceId = touchEvent .deviceId ;
@@ -397,19 +380,19 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
397
380
e .p = touchEvent .touchPoints [i ].force ;
398
381
399
382
switch (touchEvent .touchPoints [i ].type ) {
400
- case OH_NATIVEXCOMPONENT_DOWN :
401
- e .type = SDL_EVENT_FINGER_DOWN ;
402
- break ;
403
- case OH_NATIVEXCOMPONENT_MOVE :
404
- e .type = SDL_EVENT_FINGER_MOTION ;
405
- break ;
406
- case OH_NATIVEXCOMPONENT_UP :
407
- e .type = SDL_EVENT_FINGER_UP ;
408
- break ;
409
- case OH_NATIVEXCOMPONENT_CANCEL :
410
- case OH_NATIVEXCOMPONENT_UNKNOWN :
411
- e .type = SDL_EVENT_FINGER_CANCELED ;
412
- break ;
383
+ case OH_NATIVEXCOMPONENT_DOWN :
384
+ e .type = SDL_EVENT_FINGER_DOWN ;
385
+ break ;
386
+ case OH_NATIVEXCOMPONENT_MOVE :
387
+ e .type = SDL_EVENT_FINGER_MOTION ;
388
+ break ;
389
+ case OH_NATIVEXCOMPONENT_UP :
390
+ e .type = SDL_EVENT_FINGER_UP ;
391
+ break ;
392
+ case OH_NATIVEXCOMPONENT_CANCEL :
393
+ case OH_NATIVEXCOMPONENT_UNKNOWN :
394
+ e .type = SDL_EVENT_FINGER_CANCELED ;
395
+ break ;
413
396
}
414
397
415
398
OHOS_OnTouch (e );
@@ -448,7 +431,7 @@ static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
448
431
449
432
mouseCallback .DispatchMouseEvent = OnDispatchTouchEventCB ;
450
433
mouseCallback .DispatchMouseEvent = onNativeMouse ;
451
- OH_NativeXComponent_RegisterMouseEventCallback (nativeXComponent , & mouseCallback );
434
+ OH_NativeXComponent_RegisterMouseEventCallback (nativeXComponent , & mouseCallback );
452
435
453
436
OH_NativeXComponent_RegisterKeyEventCallback (nativeXComponent , onKeyEvent );
454
437
0 commit comments