Skip to content

Commit a649753

Browse files
committed
Harmony port: format
1 parent 29c6d18 commit a649753

File tree

6 files changed

+150
-177
lines changed

6 files changed

+150
-177
lines changed

src/core/ohos/SDL_ohos.c

Lines changed: 93 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
#ifdef SDL_PLATFORM_OHOS
1212

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"
1714
#include "../../video/ohos/SDL_ohostouch.h"
15+
#include "../../video/ohos/SDL_ohosvideo.h"
1816
#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>
2020

2121
static OHNativeWindow *g_ohosNativeWindow;
2222
static SDL_Mutex *g_ohosPageMutex = NULL;
@@ -46,19 +46,19 @@ typedef struct
4646
int i;
4747
long long l;
4848
double d;
49-
const char* str;
49+
const char *str;
5050
} data;
5151
} napiCallbackArg;
5252
typedef struct
5353
{
54-
const char* func;
54+
const char *func;
5555
int argCount;
5656
napiCallbackArg arg[16];
5757
napiArgType type;
5858
napiCallbackArg ret;
5959
} napiCallbackData;
6060

61-
void OHOS_windowDataFill(SDL_Window* w)
61+
void OHOS_windowDataFill(SDL_Window *w)
6262
{
6363
w->internal = SDL_calloc(1, sizeof(SDL_WindowData));
6464
w->x = x;
@@ -69,47 +69,35 @@ void OHOS_windowDataFill(SDL_Window* w)
6969

7070
SDL_VideoDevice *_this = SDL_GetVideoDevice();
7171

72-
if (_this->windows == NULL)
73-
{
72+
if (_this->windows == NULL) {
7473
_this->windows = w;
75-
}
76-
else
77-
{
74+
} else {
7875
_this->windows->next = w;
7976
w->prev = _this->windows;
8077
}
8178

8279
#ifdef SDL_VIDEO_OPENGL_EGL
8380
if (w->flags & SDL_WINDOW_OPENGL) {
8481
SDL_LockMutex(g_ohosPageMutex);
85-
if (w->internal->egl_surface == EGL_NO_SURFACE)
86-
{
82+
if (w->internal->egl_surface == EGL_NO_SURFACE) {
8783
w->internal->egl_surface = SDL_EGL_CreateSurface(_this, w, (NativeWindowType)g_ohosNativeWindow);
8884
}
8985
SDL_UnlockMutex(g_ohosPageMutex);
9086
}
9187
#endif
9288
}
93-
void OHOS_removeWindow(SDL_Window* w)
89+
void OHOS_removeWindow(SDL_Window *w)
9490
{
9591
SDL_VideoDevice *_this = SDL_GetVideoDevice();
96-
if (_this->windows == w)
97-
{
92+
if (_this->windows == w) {
9893
_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) {
10999
curWin->prev->next = NULL;
110-
}
111-
else
112-
{
100+
} else {
113101
curWin->prev->next = curWin->next;
114102
curWin->next->prev = curWin->prev;
115103
}
@@ -122,12 +110,10 @@ void OHOS_removeWindow(SDL_Window* w)
122110
#ifdef SDL_VIDEO_OPENGL_EGL
123111
if (w->flags & SDL_WINDOW_OPENGL) {
124112
SDL_LockMutex(g_ohosPageMutex);
125-
if (w->internal->egl_context)
126-
{
113+
if (w->internal->egl_context) {
127114
SDL_EGL_DestroyContext(_this, w->internal->egl_context);
128115
}
129-
if (w->internal->egl_surface != EGL_NO_SURFACE)
130-
{
116+
if (w->internal->egl_surface != EGL_NO_SURFACE) {
131117
SDL_EGL_DestroySurface(_this, w->internal->egl_surface);
132118
}
133119
SDL_UnlockMutex(g_ohosPageMutex);
@@ -179,62 +165,68 @@ static napi_value minus(napi_env env, napi_callback_info info)
179165
return sum;
180166
}
181167

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)
183169
{
184-
napiCallbackData* ar = (napiCallbackData*) data;
170+
napiCallbackData *ar = (napiCallbackData *)data;
185171

186172
napi_value callb = NULL;
187173
napi_get_reference_value(env, napiEnv.interface, &callb);
188174
napi_value jsMethod = NULL;
189175
napi_get_named_property(env, callb, ar->func, &jsMethod);
190176

191177
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:
195181
{
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);
220183
break;
221184
}
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);
224188
break;
225189
}
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);
232193
break;
233194
}
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);
236198
break;
237199
}
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+
}
238230
}
239231
}
240232

@@ -257,7 +249,7 @@ static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
257249
data->argCount = 0;
258250

259251
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_nonblocking);
260-
252+
261253
SDL_free(data);
262254

263255
napi_value result;
@@ -273,16 +265,16 @@ static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
273265

274266
size_t libstringSize = 0;
275267
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);
277269
napi_get_value_string_utf8(env, args[0], libname, libstringSize + 1, &libstringSize);
278270

279271
size_t fstringSize = 0;
280272
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);
282274
napi_get_value_string_utf8(env, args[1], fname, fstringSize + 1, &fstringSize);
283275

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);
286278
typedef int (*test)();
287279
((test)func)();
288280
dlclose(lib);
@@ -332,16 +324,13 @@ static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
332324
{
333325
SDL_VideoDevice *_this = SDL_GetVideoDevice();
334326
SDL_Window *win = _this->windows;
335-
while (win != NULL)
336-
{
327+
while (win != NULL) {
337328
#ifdef SDL_VIDEO_OPENGL_EGL
338329
if (win->flags & SDL_WINDOW_OPENGL) {
339-
if (win->internal->egl_context)
340-
{
330+
if (win->internal->egl_context) {
341331
SDL_EGL_DestroyContext(_this, win->internal->egl_context);
342332
}
343-
if (win->internal->egl_surface)
344-
{
333+
if (win->internal->egl_surface) {
345334
SDL_EGL_DestroySurface(_this, win->internal->egl_surface);
346335
}
347336
}
@@ -352,8 +341,7 @@ static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)
352341
static void onKeyEvent(OH_NativeXComponent *component, void *window)
353342
{
354343
OH_NativeXComponent_KeyEvent *keyEvent = NULL;
355-
if (OH_NativeXComponent_GetKeyEvent(component, &keyEvent) >= 0)
356-
{
344+
if (OH_NativeXComponent_GetKeyEvent(component, &keyEvent) >= 0) {
357345
OH_NativeXComponent_KeyAction action;
358346
OH_NativeXComponent_KeyCode code;
359347
OH_NativeXComponent_EventSourceType sourceType;
@@ -362,14 +350,10 @@ static void onKeyEvent(OH_NativeXComponent *component, void *window)
362350
OH_NativeXComponent_GetKeyEventCode(keyEvent, &code);
363351
OH_NativeXComponent_GetKeyEventSourceType(keyEvent, &sourceType);
364352

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) {
369355
OHOS_OnKeyDown(code);
370-
}
371-
else if (OH_NATIVEXCOMPONENT_KEY_ACTION_UP == action)
372-
{
356+
} else if (OH_NATIVEXCOMPONENT_KEY_ACTION_UP == action) {
373357
OHOS_OnKeyUp(code);
374358
}
375359
}
@@ -385,8 +369,7 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
385369
OH_NativeXComponent_GetTouchEvent(component, window, &touchEvent);
386370
OH_NativeXComponent_GetTouchPointToolType(component, 0, &toolType);
387371

388-
for (int i = 0; i < touchEvent.numPoints; i++)
389-
{
372+
for (int i = 0; i < touchEvent.numPoints; i++) {
390373
SDL_OHOSTouchEvent e;
391374
e.timestamp = touchEvent.timeStamp;
392375
e.deviceId = touchEvent.deviceId;
@@ -397,19 +380,19 @@ static void onNativeTouch(OH_NativeXComponent *component, void *window)
397380
e.p = touchEvent.touchPoints[i].force;
398381

399382
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;
413396
}
414397

415398
OHOS_OnTouch(e);
@@ -448,7 +431,7 @@ static napi_value SDL_OHOS_NAPI_Init(napi_env env, napi_value exports)
448431

449432
mouseCallback.DispatchMouseEvent = OnDispatchTouchEventCB;
450433
mouseCallback.DispatchMouseEvent = onNativeMouse;
451-
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);
434+
OH_NativeXComponent_RegisterMouseEventCallback(nativeXComponent, &mouseCallback);
452435

453436
OH_NativeXComponent_RegisterKeyEventCallback(nativeXComponent, onKeyEvent);
454437

src/video/ohos/SDL_ohosgl.c

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
#include "SDL_internal.h"
22
#ifdef SDL_VIDEO_DRIVER_OHOS
3-
#include "SDL_ohosvideo.h"
43
#include "../../core/ohos/SDL_ohos.h"
4+
#include "SDL_ohosvideo.h"
55

66
bool OHOS_GLES_MakeCurrent(SDL_VideoDevice *_this, SDL_Window *window, SDL_GLContext context)
77
{
8-
if (window && context)
9-
{
8+
if (window && context) {
109
return SDL_EGL_MakeCurrent(_this, window->internal->egl_surface, context);
11-
}
12-
else
13-
{
10+
} else {
1411
return SDL_EGL_MakeCurrent(_this, NULL, NULL);
1512
}
1613
}

0 commit comments

Comments
 (0)