Commit c8abc88cd0e4cc1ec9aee6757d9199321a436fe0

uyjulian 2021-01-29T01:26:35

Add support for message box

diff --git a/src/render/vitagxm/SDL_render_vita_gxm_tools.c b/src/render/vitagxm/SDL_render_vita_gxm_tools.c
index f410f08..e98d826 100644
--- a/src/render/vitagxm/SDL_render_vita_gxm_tools.c
+++ b/src/render/vitagxm/SDL_render_vita_gxm_tools.c
@@ -32,6 +32,7 @@
 #include <psp2/gxm.h>
 #include <psp2/types.h>
 #include <psp2/kernel/sysmem.h>
+#include <psp2/message_dialog.h>
 
 #include <stdio.h>
 #include <string.h>
@@ -1178,6 +1179,91 @@ gxm_texture_set_filters(gxm_texture *texture, SceGxmTextureFilter min_filter, Sc
     sceGxmTextureSetMagFilter(&texture->gxm_tex, mag_filter);
 }
 
+static unsigned int back_buffer_index_for_common_dialog = 0;
+static unsigned int front_buffer_index_for_common_dialog = 0;
+struct
+{
+    VITA_GXM_DisplayData displayData;
+    SceGxmSyncObject* sync;
+    SceGxmColorSurface surf;
+    SceUID uid;
+} buffer_for_common_dialog[VITA_GXM_BUFFERS];
+
+void gxm_minimal_init_for_common_dialog(void)
+{
+    SceGxmInitializeParams initializeParams;
+    SDL_zero(initializeParams);
+    initializeParams.flags                          = 0;
+    initializeParams.displayQueueMaxPendingCount    = VITA_GXM_PENDING_SWAPS;
+    initializeParams.displayQueueCallback           = display_callback;
+    initializeParams.displayQueueCallbackDataSize   = sizeof(VITA_GXM_DisplayData);
+    initializeParams.parameterBufferSize            = SCE_GXM_DEFAULT_PARAMETER_BUFFER_SIZE;
+    sceGxmInitialize(&initializeParams);
+}
+
+void gxm_minimal_term_for_common_dialog(void)
+{
+    sceGxmTerminate();
+}
+
+void gxm_init_for_common_dialog(void)
+{
+    for (int i = 0; i < VITA_GXM_BUFFERS; i += 1)
+    {
+        buffer_for_common_dialog[i].displayData.wait_vblank = SDL_TRUE;
+        buffer_for_common_dialog[i].displayData.address = mem_gpu_alloc(
+            SCE_KERNEL_MEMBLOCK_TYPE_USER_CDRAM_RW,
+            4 * VITA_GXM_SCREEN_STRIDE * VITA_GXM_SCREEN_HEIGHT,
+            SCE_GXM_COLOR_SURFACE_ALIGNMENT,
+            SCE_GXM_MEMORY_ATTRIB_READ | SCE_GXM_MEMORY_ATTRIB_WRITE,
+            &buffer_for_common_dialog[i].uid);
+        sceGxmColorSurfaceInit(
+            &buffer_for_common_dialog[i].surf,
+            VITA_GXM_PIXEL_FORMAT,
+            SCE_GXM_COLOR_SURFACE_LINEAR,
+            SCE_GXM_COLOR_SURFACE_SCALE_NONE,
+            SCE_GXM_OUTPUT_REGISTER_SIZE_32BIT,
+            VITA_GXM_SCREEN_WIDTH,
+            VITA_GXM_SCREEN_HEIGHT,
+            VITA_GXM_SCREEN_STRIDE,
+            buffer_for_common_dialog[i].displayData.address
+        );
+        sceGxmSyncObjectCreate(&buffer_for_common_dialog[i].sync);
+    }
+    sceGxmDisplayQueueFinish();
+}
+
+void gxm_swap_for_common_dialog(void)
+{
+    SceCommonDialogUpdateParam updateParam;
+    SDL_zero(updateParam);
+    updateParam.renderTarget.colorFormat    = VITA_GXM_PIXEL_FORMAT;
+    updateParam.renderTarget.surfaceType    = SCE_GXM_COLOR_SURFACE_LINEAR;
+    updateParam.renderTarget.width          = VITA_GXM_SCREEN_WIDTH;
+    updateParam.renderTarget.height         = VITA_GXM_SCREEN_HEIGHT;
+    updateParam.renderTarget.strideInPixels = VITA_GXM_SCREEN_STRIDE;
+
+    updateParam.renderTarget.colorSurfaceData = buffer_for_common_dialog[back_buffer_index_for_common_dialog].displayData.address;
+
+    updateParam.displaySyncObject = buffer_for_common_dialog[back_buffer_index_for_common_dialog].sync;
+    SDL_memset(buffer_for_common_dialog[back_buffer_index_for_common_dialog].displayData.address, 0, 4 * VITA_GXM_SCREEN_STRIDE * VITA_GXM_SCREEN_HEIGHT);
+    sceCommonDialogUpdate(&updateParam);
+
+    sceGxmDisplayQueueAddEntry(buffer_for_common_dialog[front_buffer_index_for_common_dialog].sync, buffer_for_common_dialog[back_buffer_index_for_common_dialog].sync, &buffer_for_common_dialog[back_buffer_index_for_common_dialog].displayData);
+    front_buffer_index_for_common_dialog = back_buffer_index_for_common_dialog;
+    back_buffer_index_for_common_dialog = (back_buffer_index_for_common_dialog + 1) % VITA_GXM_BUFFERS;
+}
+
+void gxm_term_for_common_dialog(void)
+{
+    sceGxmDisplayQueueFinish();
+    for (int i = 0; i < VITA_GXM_BUFFERS; i += 1)
+    {
+        mem_gpu_free(buffer_for_common_dialog[i].uid);
+        sceGxmSyncObjectDestroy(buffer_for_common_dialog[i].sync);
+    }
+}
+
 #endif /* SDL_VIDEO_RENDER_VITA_GXM */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/render/vitagxm/SDL_render_vita_gxm_tools.h b/src/render/vitagxm/SDL_render_vita_gxm_tools.h
index c1dfc6c..c103b00 100644
--- a/src/render/vitagxm/SDL_render_vita_gxm_tools.h
+++ b/src/render/vitagxm/SDL_render_vita_gxm_tools.h
@@ -59,6 +59,12 @@ unsigned int gxm_texture_get_height(const gxm_texture *texture);
 unsigned int gxm_texture_get_stride(const gxm_texture *texture);
 void *gxm_texture_get_datap(const gxm_texture *texture);
 
+void gxm_minimal_init_for_common_dialog(void);
+void gxm_minimal_term_for_common_dialog(void);
+void gxm_init_for_common_dialog(void);
+void gxm_swap_for_common_dialog(void);
+void gxm_term_for_common_dialog(void);
+
 #endif /* SDL_RENDER_VITA_GXM_TOOLS_H */
 
 /* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index e262a19..62da102 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -4048,6 +4048,9 @@ SDL_IsScreenKeyboardShown(SDL_Window *window)
 #if SDL_VIDEO_DRIVER_OS2
 #include "os2/SDL_os2messagebox.h"
 #endif
+#if SDL_VIDEO_DRIVER_VITA
+#include "vita/SDL_vitamessagebox.h"
+#endif
 
 #if SDL_VIDEO_DRIVER_WINDOWS || SDL_VIDEO_DRIVER_WINRT || SDL_VIDEO_DRIVER_COCOA || SDL_VIDEO_DRIVER_UIKIT || SDL_VIDEO_DRIVER_X11 || SDL_VIDEO_DRIVER_HAIKU || SDL_VIDEO_DRIVER_OS2
 static SDL_bool SDL_MessageboxValidForDriver(const SDL_MessageBoxData *messageboxdata, SDL_SYSWM_TYPE drivertype)
@@ -4162,6 +4165,12 @@ SDL_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
         retval = 0;
     }
 #endif
+#if SDL_VIDEO_DRIVER_VITA
+    if (retval == -1 &&
+        VITA_ShowMessageBox(messageboxdata, buttonid) == 0) {
+        retval = 0;
+    }
+#endif
     if (retval == -1) {
         SDL_SetError("No message system available");
     }
diff --git a/src/video/vita/SDL_vitamessagebox.c b/src/video/vita/SDL_vitamessagebox.c
new file mode 100644
index 0000000..65d4bad
--- /dev/null
+++ b/src/video/vita/SDL_vitamessagebox.c
@@ -0,0 +1,147 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+#include "../../SDL_internal.h"
+
+#if SDL_VIDEO_DRIVER_VITA
+
+#include "SDL_vitavideo.h"
+#include "SDL_vitamessagebox.h"
+#include <psp2/message_dialog.h>
+
+#if SDL_VIDEO_RENDER_VITA_GXM
+#include "../../render/vitagxm/SDL_render_vita_gxm_tools.h"
+#endif /* SDL_VIDEO_RENDER_VITA_GXM */
+
+int VITA_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
+{
+    SceCommonDialogConfigParam commonDialogConfigParam;
+    SceMsgDialogParam param;
+    SceMsgDialogUserMessageParam msgParam;
+    SceMsgDialogButtonsParam buttonParam;
+    SceDisplayFrameBuf dispparam;
+    
+    SceMsgDialogResult dialog_result;
+    SceCommonDialogErrorCode init_result;
+    SDL_bool setup_minimal_gxm = SDL_FALSE;
+#if !SDL_VIDEO_RENDER_VITA_GXM
+    {
+        return -1;
+    }
+#endif
+#if SDL_VIDEO_RENDER_VITA_GXM
+    if (messageboxdata->numbuttons > 3)
+    {
+        return -1;
+    }
+    SDL_zero(commonDialogConfigParam);
+    sceCommonDialogSetConfigParam(&commonDialogConfigParam);
+    SDL_zero(param);
+    sceMsgDialogParamInit(&param);
+    param.mode = SCE_MSG_DIALOG_MODE_USER_MSG;
+    SDL_zero(msgParam);
+    msgParam.msg = (const SceChar8*)messageboxdata->message;
+    SDL_zero(buttonParam);
+    if (messageboxdata->numbuttons == 3)
+    {
+        msgParam.buttonType = SCE_MSG_DIALOG_BUTTON_TYPE_3BUTTONS;
+        msgParam.buttonParam = &buttonParam;
+        buttonParam.msg1 = messageboxdata->buttons[0].text;
+        buttonParam.msg2 = messageboxdata->buttons[1].text;
+        buttonParam.msg3 = messageboxdata->buttons[2].text;
+    }
+    else if (messageboxdata->numbuttons == 2)
+    {
+        msgParam.buttonType = SCE_MSG_DIALOG_BUTTON_TYPE_YESNO;
+    }
+    else if (messageboxdata->numbuttons == 1)
+    {
+        msgParam.buttonType = SCE_MSG_DIALOG_BUTTON_TYPE_OK;
+    }
+    param.userMsgParam = &msgParam;
+
+    dispparam.size = sizeof(dispparam);
+
+    init_result = sceMsgDialogInit(&param);
+    
+    // Setup display if it hasn't been initialized before
+    if (init_result == SCE_COMMON_DIALOG_ERROR_GXM_IS_UNINITIALIZED)
+    {
+        gxm_minimal_init_for_common_dialog();
+        init_result = sceMsgDialogInit(&param);
+        setup_minimal_gxm = SDL_TRUE;
+    }
+
+    gxm_init_for_common_dialog();
+
+    if (init_result >= 0)
+    {
+        while (sceMsgDialogGetStatus() == SCE_COMMON_DIALOG_STATUS_RUNNING)
+        {
+            gxm_swap_for_common_dialog();
+        }
+        SDL_zero(dialog_result);
+        sceMsgDialogGetResult(&dialog_result);
+
+        if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_BUTTON1)
+        {
+            *buttonid = messageboxdata->buttons[0].buttonid;
+        }
+        else if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_BUTTON2)
+        {
+            *buttonid = messageboxdata->buttons[1].buttonid;
+        }
+        else if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_BUTTON3)
+        {
+            *buttonid = messageboxdata->buttons[2].buttonid;
+        }
+        else if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_YES)
+        {
+            *buttonid = messageboxdata->buttons[0].buttonid;
+        }
+        else if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_NO)
+        {
+            *buttonid = messageboxdata->buttons[1].buttonid;
+        }
+        else if (dialog_result.buttonId == SCE_MSG_DIALOG_BUTTON_ID_OK)
+        {
+            *buttonid = messageboxdata->buttons[0].buttonid;
+        }
+        sceMsgDialogTerm();
+    }
+    else
+    {
+        return -1;
+    }
+
+    gxm_term_for_common_dialog();
+
+    if (setup_minimal_gxm)
+    {
+        gxm_minimal_term_for_common_dialog();
+    }
+
+    return 0;
+#endif
+}
+
+#endif /* SDL_VIDEO_DRIVER_VITA */
+
+/* vi: set ts=4 sw=4 expandtab: */
diff --git a/src/video/vita/SDL_vitamessagebox.h b/src/video/vita/SDL_vitamessagebox.h
new file mode 100644
index 0000000..65ce73b
--- /dev/null
+++ b/src/video/vita/SDL_vitamessagebox.h
@@ -0,0 +1,33 @@
+/*
+  Simple DirectMedia Layer
+  Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
+
+  This software is provided 'as-is', without any express or implied
+  warranty.  In no event will the authors be held liable for any damages
+  arising from the use of this software.
+
+  Permission is granted to anyone to use this software for any purpose,
+  including commercial applications, and to alter it and redistribute it
+  freely, subject to the following restrictions:
+
+  1. The origin of this software must not be misrepresented; you must not
+     claim that you wrote the original software. If you use this software
+     in a product, an acknowledgment in the product documentation would be
+     appreciated but is not required.
+  2. Altered source versions must be plainly marked as such, and must not be
+     misrepresented as being the original software.
+  3. This notice may not be removed or altered from any source distribution.
+*/
+
+#ifndef SDL_vitamessagebox_h_
+#define SDL_vitamessagebox_h_
+
+#if SDL_VIDEO_DRIVER_VITA
+
+extern int VITA_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid);
+
+#endif /* SDL_VIDEO_DRIVER_VITA */
+
+#endif /* SDL_vitamessagebox_h_ */
+
+/* vi: set ts=4 sw=4 expandtab: */