mirror of
https://github.com/love2d/megasource.git
synced 2026-08-20 20:50:52 +02:00
update SDL3 to latest commit
This commit is contained in:
@@ -40,20 +40,20 @@
|
||||
#include <errno.h>
|
||||
#endif
|
||||
|
||||
#define MAX_BUTTONS 8 /* Maximum number of buttons supported */
|
||||
#define MIN_BUTTON_WIDTH 64 /* Minimum button width */
|
||||
#define MIN_DIALOG_WIDTH 200 /* Minimum dialog width */
|
||||
#define MIN_DIALOG_HEIGHT 100 /* Minimum dialog height */
|
||||
#define MAX_BUTTONS 8 // Maximum number of buttons supported
|
||||
#define MIN_BUTTON_WIDTH 64 // Minimum button width
|
||||
#define MIN_DIALOG_WIDTH 200 // Minimum dialog width
|
||||
#define MIN_DIALOG_HEIGHT 100 // Minimum dialog height
|
||||
|
||||
static const char g_MessageBoxFontLatin1[] = "-*-*-medium-r-normal--0-120-*-*-p-0-iso8859-1";
|
||||
static const char g_MessageBoxFont[] = "-*-*-medium-r-normal--*-120-*-*-*-*-*-*";
|
||||
|
||||
static const SDL_MessageBoxColor g_default_colors[SDL_MESSAGEBOX_COLOR_MAX] = {
|
||||
{ 56, 54, 53 }, /* SDL_MESSAGEBOX_COLOR_BACKGROUND, */
|
||||
{ 209, 207, 205 }, /* SDL_MESSAGEBOX_COLOR_TEXT, */
|
||||
{ 140, 135, 129 }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BORDER, */
|
||||
{ 105, 102, 99 }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND, */
|
||||
{ 205, 202, 53 }, /* SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED, */
|
||||
static const SDL_MessageBoxColor g_default_colors[SDL_MESSAGEBOX_COLOR_COUNT] = {
|
||||
{ 56, 54, 53 }, // SDL_MESSAGEBOX_COLOR_BACKGROUND,
|
||||
{ 209, 207, 205 }, // SDL_MESSAGEBOX_COLOR_TEXT,
|
||||
{ 140, 135, 129 }, // SDL_MESSAGEBOX_COLOR_BUTTON_BORDER,
|
||||
{ 105, 102, 99 }, // SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND,
|
||||
{ 205, 202, 53 }, // SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED,
|
||||
};
|
||||
|
||||
#define SDL_MAKE_RGB(_r, _g, _b) (((Uint32)(_r) << 16) | \
|
||||
@@ -62,20 +62,20 @@ static const SDL_MessageBoxColor g_default_colors[SDL_MESSAGEBOX_COLOR_MAX] = {
|
||||
|
||||
typedef struct SDL_MessageBoxButtonDataX11
|
||||
{
|
||||
int x, y; /* Text position */
|
||||
int length; /* Text length */
|
||||
int text_width; /* Text width */
|
||||
int x, y; // Text position
|
||||
int length; // Text length
|
||||
int text_width; // Text width
|
||||
|
||||
SDL_Rect rect; /* Rectangle for entire button */
|
||||
SDL_Rect rect; // Rectangle for entire button
|
||||
|
||||
const SDL_MessageBoxButtonData *buttondata; /* Button data from caller */
|
||||
const SDL_MessageBoxButtonData *buttondata; // Button data from caller
|
||||
} SDL_MessageBoxButtonDataX11;
|
||||
|
||||
typedef struct TextLineData
|
||||
{
|
||||
int width; /* Width of this text line */
|
||||
int length; /* String length of this text line */
|
||||
const char *text; /* Text for this line */
|
||||
int width; // Width of this text line
|
||||
int length; // String length of this text line
|
||||
const char *text; // Text for this line
|
||||
} TextLineData;
|
||||
|
||||
typedef struct SDL_MessageBoxDataX11
|
||||
@@ -85,51 +85,54 @@ typedef struct SDL_MessageBoxDataX11
|
||||
Window window;
|
||||
#ifdef SDL_VIDEO_DRIVER_X11_XDBE
|
||||
XdbeBackBuffer buf;
|
||||
SDL_bool xdbe; /* Whether Xdbe is present or not */
|
||||
bool xdbe; // Whether Xdbe is present or not
|
||||
#endif
|
||||
long event_mask;
|
||||
Atom wm_protocols;
|
||||
Atom wm_delete_message;
|
||||
|
||||
int dialog_width; /* Dialog box width. */
|
||||
int dialog_height; /* Dialog box height. */
|
||||
int dialog_width; // Dialog box width.
|
||||
int dialog_height; // Dialog box height.
|
||||
|
||||
XFontSet font_set; /* for UTF-8 systems */
|
||||
XFontStruct *font_struct; /* Latin1 (ASCII) fallback. */
|
||||
int xtext, ytext; /* Text position to start drawing at. */
|
||||
int numlines; /* Count of Text lines. */
|
||||
int text_height; /* Height for text lines. */
|
||||
XFontSet font_set; // for UTF-8 systems
|
||||
XFontStruct *font_struct; // Latin1 (ASCII) fallback.
|
||||
int xtext, ytext; // Text position to start drawing at.
|
||||
int numlines; // Count of Text lines.
|
||||
int text_height; // Height for text lines.
|
||||
TextLineData *linedata;
|
||||
|
||||
int *pbuttonid; /* Pointer to user return buttonID value. */
|
||||
int *pbuttonid; // Pointer to user return buttonID value.
|
||||
|
||||
int button_press_index; /* Index into buttondata/buttonpos for button which is pressed (or -1). */
|
||||
int mouse_over_index; /* Index into buttondata/buttonpos for button mouse is over (or -1). */
|
||||
int button_press_index; // Index into buttondata/buttonpos for button which is pressed (or -1).
|
||||
int mouse_over_index; // Index into buttondata/buttonpos for button mouse is over (or -1).
|
||||
|
||||
int numbuttons; /* Count of buttons. */
|
||||
int numbuttons; // Count of buttons.
|
||||
const SDL_MessageBoxButtonData *buttondata;
|
||||
SDL_MessageBoxButtonDataX11 buttonpos[MAX_BUTTONS];
|
||||
|
||||
Uint32 color[SDL_MESSAGEBOX_COLOR_MAX];
|
||||
Uint32 color[SDL_MESSAGEBOX_COLOR_COUNT];
|
||||
|
||||
const SDL_MessageBoxData *messageboxdata;
|
||||
} SDL_MessageBoxDataX11;
|
||||
|
||||
/* Maximum helper for ints. */
|
||||
// Maximum helper for ints.
|
||||
static SDL_INLINE int IntMax(int a, int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
/* Return width and height for a string. */
|
||||
// Return width and height for a string.
|
||||
static void GetTextWidthHeight(SDL_MessageBoxDataX11 *data, const char *str, int nbytes, int *pwidth, int *pheight)
|
||||
{
|
||||
#ifdef X_HAVE_UTF8_STRING
|
||||
if (SDL_X11_HAVE_UTF8) {
|
||||
XRectangle overall_ink, overall_logical;
|
||||
X11_Xutf8TextExtents(data->font_set, str, nbytes, &overall_ink, &overall_logical);
|
||||
*pwidth = overall_logical.width;
|
||||
*pheight = overall_logical.height;
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
XCharStruct text_structure;
|
||||
int font_direction, font_ascent, font_descent;
|
||||
X11_XTextExtents(data->font_struct, str, nbytes,
|
||||
@@ -140,7 +143,7 @@ static void GetTextWidthHeight(SDL_MessageBoxDataX11 *data, const char *str, int
|
||||
}
|
||||
}
|
||||
|
||||
/* Return index of button if position x,y is contained therein. */
|
||||
// Return index of button if position x,y is contained therein.
|
||||
static int GetHitButtonIndex(SDL_MessageBoxDataX11 *data, int x, int y)
|
||||
{
|
||||
int i;
|
||||
@@ -161,8 +164,8 @@ static int GetHitButtonIndex(SDL_MessageBoxDataX11 *data, int x, int y)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Initialize SDL_MessageBoxData structure and Display, etc. */
|
||||
static int X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBoxData *messageboxdata, int *pbuttonid)
|
||||
// Initialize SDL_MessageBoxData structure and Display, etc.
|
||||
static bool X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBoxData *messageboxdata, int *pbuttonid)
|
||||
{
|
||||
int i;
|
||||
int numbuttons = messageboxdata->numbuttons;
|
||||
@@ -185,6 +188,7 @@ static int X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBoxD
|
||||
return SDL_SetError("Couldn't open X11 display");
|
||||
}
|
||||
|
||||
#ifdef X_HAVE_UTF8_STRING
|
||||
if (SDL_X11_HAVE_UTF8) {
|
||||
char **missing = NULL;
|
||||
int num_missing = 0;
|
||||
@@ -196,7 +200,9 @@ static int X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBoxD
|
||||
if (!data->font_set) {
|
||||
return SDL_SetError("Couldn't load font %s", g_MessageBoxFont);
|
||||
}
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
data->font_struct = X11_XLoadQueryFont(data->display, g_MessageBoxFontLatin1);
|
||||
if (!data->font_struct) {
|
||||
return SDL_SetError("Couldn't load font %s", g_MessageBoxFontLatin1);
|
||||
@@ -209,27 +215,27 @@ static int X11_MessageBoxInit(SDL_MessageBoxDataX11 *data, const SDL_MessageBoxD
|
||||
colorhints = g_default_colors;
|
||||
}
|
||||
|
||||
/* Convert our SDL_MessageBoxColor r,g,b values to packed RGB format. */
|
||||
for (i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; i++) {
|
||||
// Convert our SDL_MessageBoxColor r,g,b values to packed RGB format.
|
||||
for (i = 0; i < SDL_MESSAGEBOX_COLOR_COUNT; i++) {
|
||||
data->color[i] = SDL_MAKE_RGB(colorhints[i].r, colorhints[i].g, colorhints[i].b);
|
||||
}
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int CountLinesOfText(const char *text)
|
||||
{
|
||||
int retval = 0;
|
||||
int result = 0;
|
||||
while (text && *text) {
|
||||
const char *lf = SDL_strchr(text, '\n');
|
||||
retval++; /* even without an endline, this counts as a line. */
|
||||
result++; // even without an endline, this counts as a line.
|
||||
text = lf ? lf + 1 : NULL;
|
||||
}
|
||||
return retval;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Calculate and initialize text and button locations. */
|
||||
static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
// Calculate and initialize text and button locations.
|
||||
static bool X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
{
|
||||
int i;
|
||||
int ybuttons;
|
||||
@@ -238,14 +244,14 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
int button_width = MIN_BUTTON_WIDTH;
|
||||
const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
|
||||
|
||||
/* Go over text and break linefeeds into separate lines. */
|
||||
// Go over text and break linefeeds into separate lines.
|
||||
if (messageboxdata && messageboxdata->message[0]) {
|
||||
const char *text = messageboxdata->message;
|
||||
const int linecount = CountLinesOfText(text);
|
||||
TextLineData *plinedata = (TextLineData *)SDL_malloc(sizeof(TextLineData) * linecount);
|
||||
|
||||
if (!plinedata) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
data->linedata = plinedata;
|
||||
@@ -260,7 +266,7 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
|
||||
GetTextWidthHeight(data, text, length, &plinedata->width, &height);
|
||||
|
||||
/* Text and widths are the largest we've ever seen. */
|
||||
// Text and widths are the largest we've ever seen.
|
||||
data->text_height = IntMax(data->text_height, height);
|
||||
text_width_max = IntMax(text_width_max, plinedata->width);
|
||||
|
||||
@@ -271,17 +277,17 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
|
||||
text += length + 1;
|
||||
|
||||
/* Break if there are no more linefeeds. */
|
||||
// Break if there are no more linefeeds.
|
||||
if (!lf) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* Bump up the text height slightly. */
|
||||
// Bump up the text height slightly.
|
||||
data->text_height += 2;
|
||||
}
|
||||
|
||||
/* Loop through all buttons and calculate the button widths and height. */
|
||||
// Loop through all buttons and calculate the button widths and height.
|
||||
for (i = 0; i < data->numbuttons; i++) {
|
||||
int height;
|
||||
|
||||
@@ -296,18 +302,18 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
}
|
||||
|
||||
if (data->numlines) {
|
||||
/* x,y for this line of text. */
|
||||
// x,y for this line of text.
|
||||
data->xtext = data->text_height;
|
||||
data->ytext = data->text_height + data->text_height;
|
||||
|
||||
/* Bump button y down to bottom of text. */
|
||||
// Bump button y down to bottom of text.
|
||||
ybuttons = 3 * data->ytext / 2 + (data->numlines - 1) * data->text_height;
|
||||
|
||||
/* Bump the dialog box width and height up if needed. */
|
||||
// Bump the dialog box width and height up if needed.
|
||||
data->dialog_width = IntMax(data->dialog_width, 2 * data->xtext + text_width_max);
|
||||
data->dialog_height = IntMax(data->dialog_height, ybuttons);
|
||||
} else {
|
||||
/* Button y starts at height of button text. */
|
||||
// Button y starts at height of button text.
|
||||
ybuttons = button_text_height;
|
||||
}
|
||||
|
||||
@@ -317,17 +323,17 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
int button_spacing = button_text_height;
|
||||
int button_height = 2 * button_text_height;
|
||||
|
||||
/* Bump button width up a bit. */
|
||||
// Bump button width up a bit.
|
||||
button_width += button_text_height;
|
||||
|
||||
/* Get width of all buttons lined up. */
|
||||
// Get width of all buttons lined up.
|
||||
width_of_buttons = data->numbuttons * button_width + (data->numbuttons - 1) * button_spacing;
|
||||
|
||||
/* Bump up dialog width and height if buttons are wider than text. */
|
||||
// Bump up dialog width and height if buttons are wider than text.
|
||||
data->dialog_width = IntMax(data->dialog_width, width_of_buttons + 2 * button_spacing);
|
||||
data->dialog_height = IntMax(data->dialog_height, ybuttons + 2 * button_height);
|
||||
|
||||
/* Location for first button. */
|
||||
// Location for first button.
|
||||
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
|
||||
x = data->dialog_width - (data->dialog_width - width_of_buttons) / 2 - (button_width + button_spacing);
|
||||
} else {
|
||||
@@ -336,17 +342,17 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
y = ybuttons + (data->dialog_height - ybuttons - button_height) / 2;
|
||||
|
||||
for (i = 0; i < data->numbuttons; i++) {
|
||||
/* Button coordinates. */
|
||||
// Button coordinates.
|
||||
data->buttonpos[i].rect.x = x;
|
||||
data->buttonpos[i].rect.y = y;
|
||||
data->buttonpos[i].rect.w = button_width;
|
||||
data->buttonpos[i].rect.h = button_height;
|
||||
|
||||
/* Button text coordinates. */
|
||||
// Button text coordinates.
|
||||
data->buttonpos[i].x = x + (button_width - data->buttonpos[i].text_width) / 2;
|
||||
data->buttonpos[i].y = y + (button_height - button_text_height - 1) / 2 + button_text_height;
|
||||
|
||||
/* Scoot over for next button. */
|
||||
// Scoot over for next button.
|
||||
if (messageboxdata->flags & SDL_MESSAGEBOX_BUTTONS_RIGHT_TO_LEFT) {
|
||||
x -= button_width + button_spacing;
|
||||
} else {
|
||||
@@ -355,10 +361,10 @@ static int X11_MessageBoxInitPositions(SDL_MessageBoxDataX11 *data)
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Free SDL_MessageBoxData data. */
|
||||
// Free SDL_MessageBoxData data.
|
||||
static void X11_MessageBoxShutdown(SDL_MessageBoxDataX11 *data)
|
||||
{
|
||||
if (data->font_set) {
|
||||
@@ -391,8 +397,8 @@ static void X11_MessageBoxShutdown(SDL_MessageBoxDataX11 *data)
|
||||
SDL_free(data->linedata);
|
||||
}
|
||||
|
||||
/* Create and set up our X11 dialog box indow. */
|
||||
static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
// Create and set up our X11 dialog box indow.
|
||||
static bool X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
{
|
||||
int x, y;
|
||||
XSizeHints *sizehints;
|
||||
@@ -404,7 +410,7 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
|
||||
if (messageboxdata->window) {
|
||||
SDL_DisplayData *displaydata = SDL_GetDisplayDriverDataForWindow(messageboxdata->window);
|
||||
windowdata = messageboxdata->window->driverdata;
|
||||
windowdata = messageboxdata->window->internal;
|
||||
data->screen = displaydata->screen;
|
||||
} else {
|
||||
data->screen = DefaultScreen(display);
|
||||
@@ -429,8 +435,8 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
Atom _NET_WM_STATE = X11_XInternAtom(display, "_NET_WM_STATE", False);
|
||||
Atom stateatoms[16];
|
||||
size_t statecount = 0;
|
||||
/* Set some message-boxy window states when attached to a parent window... */
|
||||
/* we skip the taskbar since this will pop to the front when the parent window is clicked in the taskbar, etc */
|
||||
// Set some message-boxy window states when attached to a parent window...
|
||||
// we skip the taskbar since this will pop to the front when the parent window is clicked in the taskbar, etc
|
||||
stateatoms[statecount++] = X11_XInternAtom(display, "_NET_WM_STATE_SKIP_TASKBAR", False);
|
||||
stateatoms[statecount++] = X11_XInternAtom(display, "_NET_WM_STATE_SKIP_PAGER", False);
|
||||
stateatoms[statecount++] = X11_XInternAtom(display, "_NET_WM_STATE_FOCUSED", False);
|
||||
@@ -439,20 +445,20 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
X11_XChangeProperty(display, data->window, _NET_WM_STATE, XA_ATOM, 32,
|
||||
PropModeReplace, (unsigned char *)stateatoms, statecount);
|
||||
|
||||
/* http://tronche.com/gui/x/icccm/sec-4.html#WM_TRANSIENT_FOR */
|
||||
// http://tronche.com/gui/x/icccm/sec-4.html#WM_TRANSIENT_FOR
|
||||
X11_XSetTransientForHint(display, data->window, windowdata->xwindow);
|
||||
}
|
||||
|
||||
SDL_X11_SetWindowTitle(display, data->window, (char *)messageboxdata->title);
|
||||
|
||||
/* Let the window manager know this is a dialog box */
|
||||
// Let the window manager know this is a dialog box
|
||||
_NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
|
||||
_NET_WM_WINDOW_TYPE_DIALOG = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
|
||||
X11_XChangeProperty(display, data->window, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
|
||||
PropModeReplace,
|
||||
(unsigned char *)&_NET_WM_WINDOW_TYPE_DIALOG, 1);
|
||||
|
||||
/* Allow the window to be deleted by the window manager */
|
||||
// Allow the window to be deleted by the window manager
|
||||
data->wm_protocols = X11_XInternAtom(display, "WM_PROTOCOLS", False);
|
||||
data->wm_delete_message = X11_XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
X11_XSetWMProtocols(display, data->window, &data->wm_delete_message, 1);
|
||||
@@ -469,10 +475,10 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
const SDL_VideoDevice *dev = SDL_GetVideoDevice();
|
||||
if (dev && dev->displays && dev->num_displays > 0) {
|
||||
const SDL_VideoDisplay *dpy = dev->displays[0];
|
||||
const SDL_DisplayData *dpydata = dpy->driverdata;
|
||||
const SDL_DisplayData *dpydata = dpy->internal;
|
||||
x = dpydata->x + ((dpy->current_mode->w - data->dialog_width) / 2);
|
||||
y = dpydata->y + ((dpy->current_mode->h - data->dialog_height) / 3);
|
||||
} else { /* oh well. This will misposition on a multi-head setup. Init first next time. */
|
||||
} else { // oh well. This will misposition on a multi-head setup. Init first next time.
|
||||
x = (DisplayWidth(display, data->screen) - data->dialog_width) / 2;
|
||||
y = (DisplayHeight(display, data->screen) - data->dialog_height) / 3;
|
||||
}
|
||||
@@ -498,22 +504,22 @@ static int X11_MessageBoxCreateWindow(SDL_MessageBoxDataX11 *data)
|
||||
X11_XMapRaised(display, data->window);
|
||||
|
||||
#ifdef SDL_VIDEO_DRIVER_X11_XDBE
|
||||
/* Initialise a back buffer for double buffering */
|
||||
// Initialise a back buffer for double buffering
|
||||
if (SDL_X11_HAVE_XDBE) {
|
||||
int xdbe_major, xdbe_minor;
|
||||
if (X11_XdbeQueryExtension(display, &xdbe_major, &xdbe_minor) != 0) {
|
||||
data->xdbe = SDL_TRUE;
|
||||
data->xdbe = true;
|
||||
data->buf = X11_XdbeAllocateBackBufferName(display, data->window, XdbeUndefined);
|
||||
} else {
|
||||
data->xdbe = SDL_FALSE;
|
||||
data->xdbe = false;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
/* Draw our message box. */
|
||||
// Draw our message box.
|
||||
static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
|
||||
{
|
||||
int i;
|
||||
@@ -534,11 +540,14 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
|
||||
for (i = 0; i < data->numlines; i++) {
|
||||
TextLineData *plinedata = &data->linedata[i];
|
||||
|
||||
#ifdef X_HAVE_UTF8_STRING
|
||||
if (SDL_X11_HAVE_UTF8) {
|
||||
X11_Xutf8DrawString(display, window, data->font_set, ctx,
|
||||
data->xtext, data->ytext + i * data->text_height,
|
||||
plinedata->text, plinedata->length);
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
X11_XDrawString(display, window, ctx,
|
||||
data->xtext, data->ytext + i * data->text_height,
|
||||
plinedata->text, plinedata->length);
|
||||
@@ -563,12 +572,15 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
|
||||
|
||||
X11_XSetForeground(display, ctx, (data->mouse_over_index == i) ? data->color[SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED] : data->color[SDL_MESSAGEBOX_COLOR_TEXT]);
|
||||
|
||||
#ifdef X_HAVE_UTF8_STRING
|
||||
if (SDL_X11_HAVE_UTF8) {
|
||||
X11_Xutf8DrawString(display, window, data->font_set, ctx,
|
||||
buttondatax11->x + offset,
|
||||
buttondatax11->y + offset,
|
||||
buttondata->text, buttondatax11->length);
|
||||
} else {
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
X11_XDrawString(display, window, ctx,
|
||||
buttondatax11->x + offset, buttondatax11->y + offset,
|
||||
buttondata->text, buttondatax11->length);
|
||||
@@ -586,28 +598,33 @@ static void X11_MessageBoxDraw(SDL_MessageBoxDataX11 *data, GC ctx)
|
||||
#endif
|
||||
}
|
||||
|
||||
/* NOLINTNEXTLINE(readability-non-const-parameter): cannot make XPointer a const pointer due to typedef */
|
||||
// NOLINTNEXTLINE(readability-non-const-parameter): cannot make XPointer a const pointer due to typedef
|
||||
static Bool X11_MessageBoxEventTest(Display *display, XEvent *event, XPointer arg)
|
||||
{
|
||||
const SDL_MessageBoxDataX11 *data = (const SDL_MessageBoxDataX11 *)arg;
|
||||
return ((event->xany.display == data->display) && (event->xany.window == data->window)) ? True : False;
|
||||
}
|
||||
|
||||
/* Loop and handle message box event messages until something kills it. */
|
||||
static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
// Loop and handle message box event messages until something kills it.
|
||||
static bool X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
{
|
||||
GC ctx;
|
||||
XGCValues ctx_vals;
|
||||
SDL_bool close_dialog = SDL_FALSE;
|
||||
SDL_bool has_focus = SDL_TRUE;
|
||||
bool close_dialog = false;
|
||||
bool has_focus = true;
|
||||
KeySym last_key_pressed = XK_VoidSymbol;
|
||||
unsigned long gcflags = GCForeground | GCBackground;
|
||||
#ifdef X_HAVE_UTF8_STRING
|
||||
const int have_utf8 = SDL_X11_HAVE_UTF8;
|
||||
#else
|
||||
const int have_utf8 = 0;
|
||||
#endif
|
||||
|
||||
SDL_zero(ctx_vals);
|
||||
ctx_vals.foreground = data->color[SDL_MESSAGEBOX_COLOR_BACKGROUND];
|
||||
ctx_vals.background = data->color[SDL_MESSAGEBOX_COLOR_BACKGROUND];
|
||||
|
||||
if (!SDL_X11_HAVE_UTF8) {
|
||||
if (!have_utf8) {
|
||||
gcflags |= GCFont;
|
||||
ctx_vals.font = data->font_struct->fid;
|
||||
}
|
||||
@@ -617,15 +634,15 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
return SDL_SetError("Couldn't create graphics context");
|
||||
}
|
||||
|
||||
data->button_press_index = -1; /* Reset what button is currently depressed. */
|
||||
data->mouse_over_index = -1; /* Reset what button the mouse is over. */
|
||||
data->button_press_index = -1; // Reset what button is currently depressed.
|
||||
data->mouse_over_index = -1; // Reset what button the mouse is over.
|
||||
|
||||
while (!close_dialog) {
|
||||
XEvent e;
|
||||
SDL_bool draw = SDL_TRUE;
|
||||
bool draw = true;
|
||||
|
||||
/* can't use XWindowEvent() because it can't handle ClientMessage events. */
|
||||
/* can't use XNextEvent() because we only want events for this window. */
|
||||
// can't use XWindowEvent() because it can't handle ClientMessage events.
|
||||
// can't use XNextEvent() because we only want events for this window.
|
||||
X11_XIfEvent(data->display, &e, X11_MessageBoxEventTest, (XPointer)data);
|
||||
|
||||
/* If X11_XFilterEvent returns True, then some input method has filtered the
|
||||
@@ -637,29 +654,29 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
switch (e.type) {
|
||||
case Expose:
|
||||
if (e.xexpose.count > 0) {
|
||||
draw = SDL_FALSE;
|
||||
draw = false;
|
||||
}
|
||||
break;
|
||||
|
||||
case FocusIn:
|
||||
/* Got focus. */
|
||||
has_focus = SDL_TRUE;
|
||||
// Got focus.
|
||||
has_focus = true;
|
||||
break;
|
||||
|
||||
case FocusOut:
|
||||
/* lost focus. Reset button and mouse info. */
|
||||
has_focus = SDL_FALSE;
|
||||
// lost focus. Reset button and mouse info.
|
||||
has_focus = false;
|
||||
data->button_press_index = -1;
|
||||
data->mouse_over_index = -1;
|
||||
break;
|
||||
|
||||
case MotionNotify:
|
||||
if (has_focus) {
|
||||
/* Mouse moved... */
|
||||
// Mouse moved...
|
||||
const int previndex = data->mouse_over_index;
|
||||
data->mouse_over_index = GetHitButtonIndex(data, e.xbutton.x, e.xbutton.y);
|
||||
if (data->mouse_over_index == previndex) {
|
||||
draw = SDL_FALSE;
|
||||
draw = false;
|
||||
}
|
||||
}
|
||||
break;
|
||||
@@ -668,12 +685,12 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
if (e.xclient.message_type == data->wm_protocols &&
|
||||
e.xclient.format == 32 &&
|
||||
e.xclient.data.l[0] == data->wm_delete_message) {
|
||||
close_dialog = SDL_TRUE;
|
||||
close_dialog = true;
|
||||
}
|
||||
break;
|
||||
|
||||
case KeyPress:
|
||||
/* Store key press - we make sure in key release that we got both. */
|
||||
// Store key press - we make sure in key release that we got both.
|
||||
last_key_pressed = X11_XLookupKeysym(&e.xkey, 0);
|
||||
break;
|
||||
|
||||
@@ -682,7 +699,7 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
Uint32 mask = 0;
|
||||
KeySym key = X11_XLookupKeysym(&e.xkey, 0);
|
||||
|
||||
/* If this is a key release for something we didn't get the key down for, then bail. */
|
||||
// If this is a key release for something we didn't get the key down for, then bail.
|
||||
if (key != last_key_pressed) {
|
||||
break;
|
||||
}
|
||||
@@ -696,13 +713,13 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
if (mask) {
|
||||
int i;
|
||||
|
||||
/* Look for first button with this mask set, and return it if found. */
|
||||
// Look for first button with this mask set, and return it if found.
|
||||
for (i = 0; i < data->numbuttons; i++) {
|
||||
SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[i];
|
||||
|
||||
if (buttondatax11->buttondata->flags & mask) {
|
||||
*data->pbuttonid = buttondatax11->buttondata->buttonID;
|
||||
close_dialog = SDL_TRUE;
|
||||
close_dialog = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -713,13 +730,13 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
case ButtonPress:
|
||||
data->button_press_index = -1;
|
||||
if (e.xbutton.button == Button1) {
|
||||
/* Find index of button they clicked on. */
|
||||
// Find index of button they clicked on.
|
||||
data->button_press_index = GetHitButtonIndex(data, e.xbutton.x, e.xbutton.y);
|
||||
}
|
||||
break;
|
||||
|
||||
case ButtonRelease:
|
||||
/* If button is released over the same button that was clicked down on, then return it. */
|
||||
// If button is released over the same button that was clicked down on, then return it.
|
||||
if ((e.xbutton.button == Button1) && (data->button_press_index >= 0)) {
|
||||
int button = GetHitButtonIndex(data, e.xbutton.x, e.xbutton.y);
|
||||
|
||||
@@ -727,7 +744,7 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[button];
|
||||
|
||||
*data->pbuttonid = buttondatax11->buttondata->buttonID;
|
||||
close_dialog = SDL_TRUE;
|
||||
close_dialog = true;
|
||||
}
|
||||
}
|
||||
data->button_press_index = -1;
|
||||
@@ -735,18 +752,18 @@ static int X11_MessageBoxLoop(SDL_MessageBoxDataX11 *data)
|
||||
}
|
||||
|
||||
if (draw) {
|
||||
/* Draw our dialog box. */
|
||||
// Draw our dialog box.
|
||||
X11_MessageBoxDraw(data, ctx);
|
||||
}
|
||||
}
|
||||
|
||||
X11_XFreeGC(data->display, ctx);
|
||||
return 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
static int X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonID)
|
||||
static bool X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonID)
|
||||
{
|
||||
int ret;
|
||||
bool result = false;
|
||||
SDL_MessageBoxDataX11 data;
|
||||
#if SDL_SET_LOCALE
|
||||
char *origlocale;
|
||||
@@ -755,7 +772,7 @@ static int X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int
|
||||
SDL_zero(data);
|
||||
|
||||
if (!SDL_X11_LoadSymbols()) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
|
||||
#if SDL_SET_LOCALE
|
||||
@@ -763,28 +780,23 @@ static int X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int
|
||||
if (origlocale) {
|
||||
origlocale = SDL_strdup(origlocale);
|
||||
if (!origlocale) {
|
||||
return -1;
|
||||
return false;
|
||||
}
|
||||
(void)setlocale(LC_ALL, "");
|
||||
}
|
||||
#endif
|
||||
|
||||
/* This code could get called from multiple threads maybe? */
|
||||
// This code could get called from multiple threads maybe?
|
||||
X11_XInitThreads();
|
||||
|
||||
/* Initialize the return buttonID value to -1 (for error or dialogbox closed). */
|
||||
// Initialize the return buttonID value to -1 (for error or dialogbox closed).
|
||||
*buttonID = -1;
|
||||
|
||||
/* Init and display the message box. */
|
||||
ret = X11_MessageBoxInit(&data, messageboxdata, buttonID);
|
||||
if (ret != -1) {
|
||||
ret = X11_MessageBoxInitPositions(&data);
|
||||
if (ret != -1) {
|
||||
ret = X11_MessageBoxCreateWindow(&data);
|
||||
if (ret != -1) {
|
||||
ret = X11_MessageBoxLoop(&data);
|
||||
}
|
||||
}
|
||||
// Init and display the message box.
|
||||
if (X11_MessageBoxInit(&data, messageboxdata, buttonID) &&
|
||||
X11_MessageBoxInitPositions(&data) &&
|
||||
X11_MessageBoxCreateWindow(&data)) {
|
||||
result = X11_MessageBoxLoop(&data);
|
||||
}
|
||||
|
||||
X11_MessageBoxShutdown(&data);
|
||||
@@ -796,60 +808,61 @@ static int X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int
|
||||
}
|
||||
#endif
|
||||
|
||||
return ret;
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Display an x11 message box. */
|
||||
int X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
|
||||
// Display an x11 message box.
|
||||
bool X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonID)
|
||||
{
|
||||
#if SDL_FORK_MESSAGEBOX
|
||||
/* Use a child process to protect against setlocale(). Annoying. */
|
||||
// Use a child process to protect against setlocale(). Annoying.
|
||||
pid_t pid;
|
||||
int fds[2];
|
||||
int status = 0;
|
||||
bool result = true;
|
||||
|
||||
if (pipe(fds) == -1) {
|
||||
return X11_ShowMessageBoxImpl(messageboxdata, buttonID); /* oh well. */
|
||||
return X11_ShowMessageBoxImpl(messageboxdata, buttonID); // oh well.
|
||||
}
|
||||
|
||||
pid = fork();
|
||||
if (pid == -1) { /* failed */
|
||||
if (pid == -1) { // failed
|
||||
close(fds[0]);
|
||||
close(fds[1]);
|
||||
return X11_ShowMessageBoxImpl(messageboxdata, buttonID); /* oh well. */
|
||||
} else if (pid == 0) { /* we're the child */
|
||||
return X11_ShowMessageBoxImpl(messageboxdata, buttonID); // oh well.
|
||||
} else if (pid == 0) { // we're the child
|
||||
int exitcode = 0;
|
||||
close(fds[0]);
|
||||
status = X11_ShowMessageBoxImpl(messageboxdata, buttonID);
|
||||
if (write(fds[1], &status, sizeof(int)) != sizeof(int)) {
|
||||
result = X11_ShowMessageBoxImpl(messageboxdata, buttonID);
|
||||
if (write(fds[1], &result, sizeof(result)) != sizeof(result)) {
|
||||
exitcode = 1;
|
||||
} else if (write(fds[1], buttonID, sizeof(int)) != sizeof(int)) {
|
||||
} else if (write(fds[1], buttonID, sizeof(*buttonID)) != sizeof(*buttonID)) {
|
||||
exitcode = 1;
|
||||
}
|
||||
close(fds[1]);
|
||||
_exit(exitcode); /* don't run atexit() stuff, static destructors, etc. */
|
||||
} else { /* we're the parent */
|
||||
_exit(exitcode); // don't run atexit() stuff, static destructors, etc.
|
||||
} else { // we're the parent
|
||||
pid_t rc;
|
||||
close(fds[1]);
|
||||
do {
|
||||
rc = waitpid(pid, &status, 0);
|
||||
} while ((rc == -1) && (errno == EINTR));
|
||||
|
||||
SDL_assert(rc == pid); /* not sure what to do if this fails. */
|
||||
SDL_assert(rc == pid); // not sure what to do if this fails.
|
||||
|
||||
if ((rc == -1) || (!WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) {
|
||||
status = SDL_SetError("msgbox child process failed");
|
||||
} else if ((read(fds[0], &status, sizeof(int)) != sizeof(int)) ||
|
||||
(read(fds[0], buttonID, sizeof(int)) != sizeof(int))) {
|
||||
status = SDL_SetError("read from msgbox child process failed");
|
||||
result = SDL_SetError("msgbox child process failed");
|
||||
} else if ((read(fds[0], &result, sizeof(result)) != sizeof(result)) ||
|
||||
(read(fds[0], buttonID, sizeof(*buttonID)) != sizeof(*buttonID))) {
|
||||
result = SDL_SetError("read from msgbox child process failed");
|
||||
*buttonID = 0;
|
||||
}
|
||||
close(fds[0]);
|
||||
|
||||
return status;
|
||||
return result;
|
||||
}
|
||||
#else
|
||||
return X11_ShowMessageBoxImpl(messageboxdata, buttonID);
|
||||
#endif
|
||||
}
|
||||
#endif /* SDL_VIDEO_DRIVER_X11 */
|
||||
#endif // SDL_VIDEO_DRIVER_X11
|
||||
|
||||
Reference in New Issue
Block a user