Hash :
413a2306
Author :
Date :
2021-04-04T10:47:06
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
/*
Simple DirectMedia Layer
Copyright (C) 1997-2021 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_WAYLAND
#include "SDL.h"
#include <stdlib.h> /* popen/pclose/fgets */
int
Wayland_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
{
#define ZENITY_CONST(name, str) \
const char *name = str; \
const size_t name##_len = SDL_strlen(name);
ZENITY_CONST(zenity, "zenity --question --switch --no-wrap --icon-name=dialog-")
ZENITY_CONST(title, "--title=")
ZENITY_CONST(message, "--text=")
ZENITY_CONST(extrabutton, "--extra-button=")
ZENITY_CONST(icon_info, "information")
ZENITY_CONST(icon_warn, "warning")
ZENITY_CONST(icon_error, "error")
#undef ZENITY_CONST
char *command, *output;
size_t command_len, output_len;
const char *icon;
char *tmp;
FILE *process;
int i;
/* Start by calculating the lengths of the strings. These commands can get
* pretty long, so we need to dynamically allocate this.
*/
command_len = zenity_len;
output_len = 0;
switch (messageboxdata->flags)
{
case SDL_MESSAGEBOX_ERROR:
icon = icon_error;
command_len += icon_error_len;
break;
case SDL_MESSAGEBOX_WARNING:
icon = icon_warn;
command_len += icon_warn_len;
break;
case SDL_MESSAGEBOX_INFORMATION:
default:
icon = icon_info;
command_len += icon_info_len;
break;
}
#define ADD_ARGUMENT(arg, value) \
command_len += arg + 3; /* Two " and a space */ \
if (messageboxdata->value != NULL) { \
command_len += SDL_strlen(messageboxdata->value); \
}
ADD_ARGUMENT(title_len, title)
ADD_ARGUMENT(message_len, message)
#undef ADD_ARGUMENT
for (i = 0; i < messageboxdata->numbuttons; i += 1) {
command_len += extrabutton_len + 3; /* Two " and a space */
if (messageboxdata->buttons[i].text != NULL) {
const size_t button_len = SDL_strlen(messageboxdata->buttons[i].text);
command_len += button_len;
if (button_len > output_len) {
output_len = button_len;
}
}
}
/* Don't forget null terminators! */
command_len += 1;
output_len += 1;
/* Now that we know the length of the command, allocate! */
command = (char*) SDL_malloc(command_len + output_len);
if (command == NULL) {
return SDL_OutOfMemory();
}
output = command + command_len;
command[0] = '\0';
output[0] = '\0';
/* Now we can build the command... */
SDL_strlcpy(command, zenity, command_len);
SDL_strlcat(command, icon, command_len);
#define ADD_ARGUMENT(arg, value) \
SDL_strlcat(command, " ", command_len); \
SDL_strlcat(command, arg, command_len); \
SDL_strlcat(command, "\"", command_len); \
if (value != NULL) { \
SDL_strlcat(command, value, command_len); \
} \
SDL_strlcat(command, "\"", command_len)
ADD_ARGUMENT(title, messageboxdata->title);
ADD_ARGUMENT(message, messageboxdata->message);
for (i = 0; i < messageboxdata->numbuttons; i += 1) {
ADD_ARGUMENT(extrabutton, messageboxdata->buttons[i].text);
}
#undef ADD_ARGUMENT
/* ... then run it, finally. */
process = popen(command, "r");
if (process == NULL) {
SDL_free(command);
return SDL_SetError("zenity failed to run");
}
/* At this point, if no button ID is needed, we can just bail as soon as the
* process has completed.
*/
if (buttonid == NULL) {
pclose(process);
goto end;
}
*buttonid = -1;
/* Read the result from stdout */
tmp = fgets(output, output_len, process);
pclose(process);
if ((tmp == NULL) || (*tmp == '\0') || (*tmp == '\n')) {
goto end; /* User simply closed the dialog */
}
/* It likes to add a newline... */
tmp = SDL_strrchr(output, '\n');
if (tmp != NULL) {
*tmp = '\0';
}
/* Check which button got pressed */
for (i = 0; i < messageboxdata->numbuttons; i += 1) {
if (messageboxdata->buttons[i].text != NULL) {
if (SDL_strcmp(output, messageboxdata->buttons[i].text) == 0) {
*buttonid = i;
break;
}
}
}
end:
SDL_free(command);
return 0;
}
#endif /* SDL_VIDEO_DRIVER_WAYLAND */
/* vi: set ts=4 sw=4 expandtab: */