Skip to content

Commit 56e42de

Browse files
committed
Harmony port: dialog
1 parent 44497fc commit 56e42de

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

src/core/ohos/SDL_ohos.c

Lines changed: 41 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ typedef enum
4242
typedef struct
4343
{
4444
napiArgType type;
45+
bool enabled;
4546
union
4647
{
4748
int i;
@@ -59,16 +60,21 @@ typedef struct
5960
napiCallbackArg ret;
6061
} napiCallbackData;
6162

62-
void OHOS_windowDataFill(SDL_Window *w)
63+
void OHOS_windowUpdateAttributes(SDL_Window *w)
6364
{
64-
w->internal = SDL_calloc(1, sizeof(SDL_WindowData));
6565
w->x = x;
6666
w->y = y;
6767
w->w = wid;
6868
w->h = hei;
69-
w->internal->native_window = g_ohosNativeWindow;
7069

7170
SDL_SetWindowSize(w, wid, hei);
71+
}
72+
73+
void OHOS_windowDataFill(SDL_Window *w)
74+
{
75+
w->internal = SDL_calloc(1, sizeof(SDL_WindowData));
76+
OHOS_windowUpdateAttributes(w);
77+
w->internal->native_window = g_ohosNativeWindow;
7278

7379
SDL_VideoDevice *_this = SDL_GetVideoDevice();
7480

@@ -153,7 +159,11 @@ static void sdlJSCallback(napi_env env, napi_value jsCb, void *content, void *da
153159
napi_get_named_property(env, callb, ar->func, &jsMethod);
154160

155161
napi_value args[16];
162+
SDL_Log("[SDL] calling js function %s with %d args", ar->func, ar->argCount);
156163
for (int i = 0; i < ar->argCount; i++) {
164+
if (!ar->arg[i].enabled) {
165+
continue;
166+
}
157167
switch (ar->arg[i].type) {
158168
case Int:
159169
{
@@ -172,7 +182,12 @@ static void sdlJSCallback(napi_env env, napi_value jsCb, void *content, void *da
172182
}
173183
case String:
174184
{
175-
napi_create_string_utf8(env, ar->arg[i].data.str, SDL_strlen(ar->arg[i].data.str), args + i);
185+
const char* p = ar->arg[i].data.str; int l = 0;
186+
while (*p) {
187+
l++;
188+
p++;
189+
}
190+
napi_create_string_utf8(env, ar->arg[i].data.str, l, args + i);
176191
break;
177192
}
178193
}
@@ -208,6 +223,22 @@ static void sdlJSCallback(napi_env env, napi_value jsCb, void *content, void *da
208223
}
209224
}
210225

226+
void OHOS_MessageBox(const char* title, const char* message)
227+
{
228+
napiCallbackData *data = SDL_malloc(sizeof(napiCallbackData));
229+
SDL_memset(data, 0, sizeof(napiCallbackData));
230+
data->func = "showDialog";
231+
data->argCount = 2;
232+
data->arg[0].type = String;
233+
data->arg[0].enabled = true;
234+
data->arg[0].data.str = title;
235+
data->arg[1].type = String;
236+
data->arg[1].enabled = true;
237+
data->arg[1].data.str = message;
238+
239+
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_nonblocking);
240+
}
241+
211242
static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
212243
{
213244
napiEnv.env = env;
@@ -222,14 +253,6 @@ static napi_value sdlCallbackInit(napi_env env, napi_callback_info info)
222253
napi_create_string_utf8(env, "SDLThreadSafe", NAPI_AUTO_LENGTH, &resName);
223254
napi_create_threadsafe_function(env, args[0], NULL, resName, 0, 1, NULL, NULL, NULL, sdlJSCallback, &napiEnv.func);
224255

225-
napiCallbackData *data = SDL_malloc(sizeof(napiCallbackData));
226-
data->func = "test";
227-
data->argCount = 0;
228-
229-
napi_call_threadsafe_function(napiEnv.func, data, napi_tsfn_nonblocking);
230-
231-
// SDL_free(data);
232-
233256
napi_value result;
234257
napi_create_int32(env, 0, &result);
235258
return result;
@@ -255,6 +278,8 @@ static int sdlLaunchMainInternal(void* reserved)
255278
return d;
256279
}
257280

281+
static SDL_Thread *mainThread;
282+
258283
static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
259284
{
260285
size_t argc = 2;
@@ -274,7 +299,7 @@ static napi_value sdlLaunchMain(napi_env env, napi_callback_info info)
274299
entrypoint_info *entry = (entrypoint_info*)SDL_malloc(sizeof(entrypoint_info));
275300
entry->func = fname;
276301
entry->libname = libname;
277-
SDL_Thread* m = SDL_CreateThread(sdlLaunchMainInternal, "SDL App Thread", entry);
302+
mainThread = SDL_CreateThread(sdlLaunchMainInternal, "SDL App Thread", entry);
278303
SDL_SetMainReady();
279304

280305
napi_value result;
@@ -326,10 +351,9 @@ static void OnSurfaceChangedCB(OH_NativeXComponent *component, void *window)
326351
SDL_UnlockMutex(g_ohosPageMutex);
327352

328353
SDL_VideoDevice *_this = SDL_GetVideoDevice();
329-
SDL_Window *win = _this->windows;
330-
while (win != NULL) {
331-
OHOS_windowDataFill(win);
332-
win = win->next;
354+
if (_this && _this->windows) {
355+
SDL_Window *win = _this->windows;
356+
OHOS_windowUpdateAttributes(win);
333357
}
334358
}
335359
static void OnSurfaceDestroyedCB(OH_NativeXComponent *component, void *window)

src/core/ohos/SDL_ohos.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ void OHOS_UnlockPage();
1212
int OHOS_FetchWidth();
1313
int OHOS_FetchHeight();
1414

15+
void OHOS_MessageBox(const char* title, const char* message);
16+
1517
typedef struct SDL_VideoData {
1618
SDL_Rect textRect;
1719
int isPaused;

src/video/ohos/SDL_ohosvideo.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,21 @@ static SDL_VideoDevice *OHOS_CreateDevice(void)
7070

7171
return device;
7272
}
73+
bool OHOS_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
74+
{
75+
int length1 = SDL_strlen(messageboxdata->title) + 1;
76+
char* titlecopy = (char*)SDL_malloc(length1);
77+
SDL_memcpy(titlecopy, messageboxdata->title, length1);
78+
length1 = SDL_strlen(messageboxdata->message) + 1;
79+
char* messagecopy = (char*)SDL_malloc(length1);
80+
SDL_memcpy(messagecopy, messageboxdata->message, length1);
81+
OHOS_MessageBox(titlecopy, messagecopy);
82+
return true;
83+
}
7384
VideoBootStrap OHOS_bootstrap = {
7485
"ohos", "OpenHarmony video driver",
7586
OHOS_CreateDevice,
76-
NULL,
87+
OHOS_ShowMessageBox,
7788
false
7889
};
7990
#endif

0 commit comments

Comments
 (0)