src/video/x11/SDL_x11messagebox.c
author Sam Lantinga <slouken@libsdl.org>
Sun, 13 Nov 2016 22:57:41 -0800
changeset 10609 d702ecbd8ba7
parent 9998 f67cf37e9cd4
child 10737 3406a0f8b041
permissions -rw-r--r--
Patch from Sylvain to fix clang warnings
slouken@6602
     1
/*
slouken@6602
     2
  Simple DirectMedia Layer
slouken@9998
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@6602
     4
slouken@6602
     5
  This software is provided 'as-is', without any express or implied
slouken@6602
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@6602
     7
  arising from the use of this software.
slouken@6602
     8
slouken@6602
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@6602
    10
  including commercial applications, and to alter it and redistribute it
slouken@6602
    11
  freely, subject to the following restrictions:
slouken@6602
    12
slouken@6602
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@6602
    14
     claim that you wrote the original software. If you use this software
slouken@6602
    15
     in a product, an acknowledgment in the product documentation would be
slouken@6602
    16
     appreciated but is not required.
slouken@6602
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@6602
    18
     misrepresented as being the original software.
slouken@6602
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@6602
    20
*/
icculus@6723
    21
icculus@8093
    22
#include "../../SDL_internal.h"
slouken@6602
    23
slouken@6602
    24
#if SDL_VIDEO_DRIVER_X11
slouken@6602
    25
slouken@6602
    26
#include "SDL.h"
slouken@6602
    27
#include "SDL_x11video.h"
slouken@6602
    28
#include "SDL_x11dyn.h"
icculus@6726
    29
#include "SDL_assert.h"
slouken@10609
    30
#include "SDL_x11messagebox.h"
slouken@6602
    31
slouken@9733
    32
#include <X11/keysym.h>
icculus@6723
    33
#include <locale.h>
icculus@6723
    34
icculus@6726
    35
icculus@9423
    36
#define SDL_FORK_MESSAGEBOX 1
icculus@9423
    37
#define SDL_SET_LOCALE      1
icculus@6726
    38
icculus@6726
    39
#if SDL_FORK_MESSAGEBOX
icculus@6726
    40
#include <sys/types.h>
icculus@6726
    41
#include <sys/wait.h>
icculus@6726
    42
#include <unistd.h>
icculus@6726
    43
#include <errno.h>
icculus@6726
    44
#endif
icculus@6726
    45
slouken@6602
    46
#define MAX_BUTTONS             8       /* Maximum number of buttons supported */
slouken@6602
    47
#define MAX_TEXT_LINES          32      /* Maximum number of text lines supported */
slouken@6602
    48
#define MIN_BUTTON_WIDTH        64      /* Minimum button width */
slouken@6602
    49
#define MIN_DIALOG_WIDTH        200     /* Minimum dialog width */
slouken@6602
    50
#define MIN_DIALOG_HEIGHT       100     /* Minimum dialog height */
slouken@6602
    51
icculus@6723
    52
static const char g_MessageBoxFontLatin1[] = "-*-*-medium-r-normal--0-120-*-*-p-0-iso8859-1";
urkle@8103
    53
static const char g_MessageBoxFont[] = "-*-*-*-*-*-*-*-120-*-*-*-*-*-*";
slouken@6602
    54
slouken@6725
    55
static const SDL_MessageBoxColor g_default_colors[ SDL_MESSAGEBOX_COLOR_MAX ] = {
icculus@6817
    56
    { 56,  54,  53  }, /* SDL_MESSAGEBOX_COLOR_BACKGROUND, */
icculus@6817
    57
    { 209, 207, 205 }, /* SDL_MESSAGEBOX_COLOR_TEXT, */
icculus@6817
    58
    { 140, 135, 129 }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BORDER, */
icculus@6817
    59
    { 105, 102, 99  }, /* SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND, */
icculus@6817
    60
    { 205, 202, 53  }, /* SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED, */
slouken@6602
    61
};
slouken@6602
    62
slouken@6602
    63
#define SDL_MAKE_RGB( _r, _g, _b )  ( ( ( Uint32 )( _r ) << 16 ) | \
slouken@6602
    64
                                      ( ( Uint32 )( _g ) << 8 ) |  \
slouken@6602
    65
                                      ( ( Uint32 )( _b ) ) )
slouken@6602
    66
slouken@6725
    67
typedef struct SDL_MessageBoxButtonDataX11 {
slouken@6602
    68
    int x, y;                           /* Text position */
slouken@6602
    69
    int length;                         /* Text length */
slouken@6602
    70
    int text_width;                     /* Text width */
slouken@6602
    71
slouken@6602
    72
    SDL_Rect rect;                      /* Rectangle for entire button */
slouken@6602
    73
slouken@6602
    74
    const SDL_MessageBoxButtonData *buttondata;   /* Button data from caller */
slouken@6602
    75
} SDL_MessageBoxButtonDataX11;
slouken@6602
    76
slouken@6725
    77
typedef struct TextLineData {
slouken@6602
    78
    int width;                          /* Width of this text line */
slouken@6602
    79
    int length;                         /* String length of this text line */
slouken@6602
    80
    const char *text;                   /* Text for this line */
slouken@6602
    81
} TextLineData;
slouken@6602
    82
slouken@7522
    83
typedef struct SDL_MessageBoxDataX11
slouken@7522
    84
{
slouken@7522
    85
    Display *display;
slouken@7522
    86
    int screen;
slouken@6602
    87
    Window window;
icculus@9657
    88
#if SDL_VIDEO_DRIVER_X11_XDBE
icculus@9657
    89
    XdbeBackBuffer buf;
icculus@9657
    90
    SDL_bool xdbe;                      /* Whether Xdbe is present or not */
icculus@9657
    91
#endif
slouken@6603
    92
    long event_mask;
slouken@6602
    93
    Atom wm_protocols;
slouken@6602
    94
    Atom wm_delete_message;
slouken@6602
    95
slouken@6602
    96
    int dialog_width;                   /* Dialog box width. */
slouken@6602
    97
    int dialog_height;                  /* Dialog box height. */
slouken@6602
    98
slouken@7522
    99
    XFontSet font_set;                  /* for UTF-8 systems */
slouken@7522
   100
    XFontStruct *font_struct;           /* Latin1 (ASCII) fallback. */
slouken@6602
   101
    int xtext, ytext;                   /* Text position to start drawing at. */
slouken@6602
   102
    int numlines;                       /* Count of Text lines. */
slouken@6602
   103
    int text_height;                    /* Height for text lines. */
slouken@6602
   104
    TextLineData linedata[ MAX_TEXT_LINES ];
slouken@6602
   105
slouken@6602
   106
    int *pbuttonid;                     /* Pointer to user return buttonid value. */
slouken@6602
   107
slouken@6602
   108
    int button_press_index;             /* Index into buttondata/buttonpos for button which is pressed (or -1). */
slouken@6602
   109
    int mouse_over_index;               /* Index into buttondata/buttonpos for button mouse is over (or -1). */
slouken@6602
   110
slouken@6602
   111
    int numbuttons;                     /* Count of buttons. */
slouken@6602
   112
    const SDL_MessageBoxButtonData *buttondata;
slouken@6602
   113
    SDL_MessageBoxButtonDataX11 buttonpos[ MAX_BUTTONS ];
slouken@6602
   114
slouken@6603
   115
    Uint32 color[ SDL_MESSAGEBOX_COLOR_MAX ];
slouken@6602
   116
slouken@6602
   117
    const SDL_MessageBoxData *messageboxdata;
slouken@6602
   118
} SDL_MessageBoxDataX11;
slouken@6602
   119
slouken@6602
   120
/* Maximum helper for ints. */
slouken@7860
   121
static SDL_INLINE int
slouken@6602
   122
IntMax( int a, int b )
slouken@6602
   123
{
slouken@6602
   124
    return ( a > b  ) ? a : b;
slouken@6602
   125
}
slouken@6602
   126
slouken@6602
   127
/* Return width and height for a string. */
slouken@6602
   128
static void
icculus@6723
   129
GetTextWidthHeight( SDL_MessageBoxDataX11 *data, const char *str, int nbytes, int *pwidth, int *pheight )
slouken@6602
   130
{
icculus@6723
   131
    if (SDL_X11_HAVE_UTF8) {
icculus@6723
   132
        XRectangle overall_ink, overall_logical;
icculus@7827
   133
        X11_Xutf8TextExtents(data->font_set, str, nbytes, &overall_ink, &overall_logical);
icculus@6723
   134
        *pwidth = overall_logical.width;
icculus@6723
   135
        *pheight = overall_logical.height;
icculus@6723
   136
    } else {
icculus@6723
   137
        XCharStruct text_structure;
icculus@6723
   138
        int font_direction, font_ascent, font_descent;
icculus@7827
   139
        X11_XTextExtents( data->font_struct, str, nbytes,
icculus@6723
   140
                      &font_direction, &font_ascent, &font_descent,
icculus@6723
   141
                      &text_structure );
icculus@6723
   142
        *pwidth = text_structure.width;
icculus@6723
   143
        *pheight = text_structure.ascent + text_structure.descent;
icculus@6723
   144
    }
slouken@6602
   145
}
slouken@6602
   146
slouken@6602
   147
/* Return index of button if position x,y is contained therein. */
slouken@6602
   148
static int
slouken@6602
   149
GetHitButtonIndex( SDL_MessageBoxDataX11 *data, int x, int y )
slouken@6602
   150
{
slouken@6602
   151
    int i;
slouken@6602
   152
    int numbuttons = data->numbuttons;
slouken@6602
   153
    SDL_MessageBoxButtonDataX11 *buttonpos = data->buttonpos;
slouken@6602
   154
slouken@6725
   155
    for ( i = 0; i < numbuttons; i++ ) {
slouken@6602
   156
        SDL_Rect *rect = &buttonpos[ i ].rect;
slouken@6602
   157
slouken@6602
   158
        if ( ( x >= rect->x ) &&
slouken@6725
   159
             ( x <= ( rect->x + rect->w ) ) &&
slouken@6725
   160
             ( y >= rect->y ) &&
slouken@6725
   161
             ( y <= ( rect->y + rect->h ) ) ) {
slouken@6602
   162
            return i;
slouken@6602
   163
        }
slouken@6602
   164
    }
slouken@6602
   165
slouken@6602
   166
    return -1;
slouken@6602
   167
}
slouken@6602
   168
slouken@6602
   169
/* Initialize SDL_MessageBoxData structure and Display, etc. */
slouken@6602
   170
static int
slouken@6602
   171
X11_MessageBoxInit( SDL_MessageBoxDataX11 *data, const SDL_MessageBoxData * messageboxdata, int * pbuttonid )
slouken@6602
   172
{
slouken@6603
   173
    int i;
slouken@6602
   174
    int numbuttons = messageboxdata->numbuttons;
slouken@6602
   175
    const SDL_MessageBoxButtonData *buttondata = messageboxdata->buttons;
slouken@6603
   176
    const SDL_MessageBoxColor *colorhints;
slouken@6602
   177
slouken@6602
   178
    if ( numbuttons > MAX_BUTTONS ) {
icculus@7037
   179
        return SDL_SetError("Too many buttons (%d max allowed)", MAX_BUTTONS);
slouken@6602
   180
    }
slouken@6602
   181
slouken@6602
   182
    data->dialog_width = MIN_DIALOG_WIDTH;
slouken@6602
   183
    data->dialog_height = MIN_DIALOG_HEIGHT;
slouken@6602
   184
    data->messageboxdata = messageboxdata;
slouken@6602
   185
    data->buttondata = buttondata;
slouken@6602
   186
    data->numbuttons = numbuttons;
slouken@6602
   187
    data->pbuttonid = pbuttonid;
slouken@6602
   188
icculus@7827
   189
    data->display = X11_XOpenDisplay( NULL );
slouken@6602
   190
    if ( !data->display ) {
icculus@7037
   191
        return SDL_SetError("Couldn't open X11 display");
slouken@6602
   192
    }
slouken@6602
   193
icculus@6723
   194
    if (SDL_X11_HAVE_UTF8) {
icculus@6723
   195
        char **missing = NULL;
icculus@6723
   196
        int num_missing = 0;
icculus@7827
   197
        data->font_set = X11_XCreateFontSet(data->display, g_MessageBoxFont,
icculus@6723
   198
                                        &missing, &num_missing, NULL);
icculus@6723
   199
        if ( missing != NULL ) {
icculus@7827
   200
            X11_XFreeStringList(missing);
icculus@6723
   201
        }
icculus@6723
   202
        if ( data->font_set == NULL ) {
icculus@7037
   203
            return SDL_SetError("Couldn't load font %s", g_MessageBoxFont);
icculus@6723
   204
        }
icculus@6723
   205
    } else {
icculus@7827
   206
        data->font_struct = X11_XLoadQueryFont( data->display, g_MessageBoxFontLatin1 );
icculus@6723
   207
        if ( data->font_struct == NULL ) {
icculus@7037
   208
            return SDL_SetError("Couldn't load font %s", g_MessageBoxFontLatin1);
icculus@6723
   209
        }
slouken@6602
   210
    }
slouken@6602
   211
slouken@6603
   212
    if ( messageboxdata->colorScheme ) {
slouken@6603
   213
        colorhints = messageboxdata->colorScheme->colors;
slouken@6603
   214
    } else {
slouken@6603
   215
        colorhints = g_default_colors;
slouken@6603
   216
    }
slouken@6602
   217
slouken@6603
   218
    /* Convert our SDL_MessageBoxColor r,g,b values to packed RGB format. */
slouken@6603
   219
    for ( i = 0; i < SDL_MESSAGEBOX_COLOR_MAX; i++ ) {
slouken@6603
   220
        data->color[ i ] = SDL_MAKE_RGB( colorhints[ i ].r, colorhints[ i ].g, colorhints[ i ].b );
slouken@6603
   221
    }
slouken@6602
   222
slouken@6602
   223
    return 0;
slouken@6602
   224
}
slouken@6602
   225
slouken@6602
   226
/* Calculate and initialize text and button locations. */
slouken@6602
   227
static int
slouken@6602
   228
X11_MessageBoxInitPositions( SDL_MessageBoxDataX11 *data )
slouken@6602
   229
{
slouken@6602
   230
    int i;
slouken@6602
   231
    int ybuttons;
slouken@6602
   232
    int text_width_max = 0;
slouken@6602
   233
    int button_text_height = 0;
slouken@6602
   234
    int button_width = MIN_BUTTON_WIDTH;
slouken@6602
   235
    const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
slouken@6602
   236
slouken@6602
   237
    /* Go over text and break linefeeds into separate lines. */
slouken@6725
   238
    if ( messageboxdata->message && messageboxdata->message[ 0 ] ) {
slouken@6602
   239
        const char *text = messageboxdata->message;
slouken@6602
   240
        TextLineData *plinedata = data->linedata;
slouken@6602
   241
slouken@6725
   242
        for ( i = 0; i < MAX_TEXT_LINES; i++, plinedata++ ) {
slouken@6602
   243
            int height;
slouken@6602
   244
            char *lf = SDL_strchr( ( char * )text, '\n' );
slouken@6602
   245
slouken@6602
   246
            data->numlines++;
slouken@6602
   247
slouken@6602
   248
            /* Only grab length up to lf if it exists and isn't the last line. */
slouken@6602
   249
            plinedata->length = ( lf && ( i < MAX_TEXT_LINES - 1 ) ) ? ( lf - text ) : SDL_strlen( text );
slouken@6602
   250
            plinedata->text = text;
slouken@6602
   251
icculus@6723
   252
            GetTextWidthHeight( data, text, plinedata->length, &plinedata->width, &height );
slouken@6602
   253
slouken@6602
   254
            /* Text and widths are the largest we've ever seen. */
slouken@6602
   255
            data->text_height = IntMax( data->text_height, height );
slouken@6602
   256
            text_width_max = IntMax( text_width_max, plinedata->width );
slouken@6602
   257
icculus@6758
   258
            if (lf && (lf > text) && (lf[-1] == '\r')) {
icculus@6758
   259
                plinedata->length--;
icculus@6758
   260
            }
icculus@6758
   261
slouken@6602
   262
            text += plinedata->length + 1;
slouken@6602
   263
slouken@6602
   264
            /* Break if there are no more linefeeds. */
slouken@6602
   265
            if ( !lf )
slouken@6602
   266
                break;
slouken@6602
   267
        }
slouken@6602
   268
slouken@6602
   269
        /* Bump up the text height slightly. */
slouken@6602
   270
        data->text_height += 2;
slouken@6602
   271
    }
slouken@6602
   272
slouken@6602
   273
    /* Loop through all buttons and calculate the button widths and height. */
slouken@6725
   274
    for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   275
        int height;
slouken@6602
   276
slouken@6602
   277
        data->buttonpos[ i ].buttondata = &data->buttondata[ i ];
slouken@6602
   278
        data->buttonpos[ i ].length = SDL_strlen( data->buttondata[ i ].text );
slouken@6602
   279
icculus@6723
   280
        GetTextWidthHeight( data, data->buttondata[ i ].text, SDL_strlen( data->buttondata[ i ].text ),
slouken@6602
   281
                            &data->buttonpos[ i ].text_width, &height );
slouken@6602
   282
slouken@6602
   283
        button_width = IntMax( button_width, data->buttonpos[ i ].text_width );
slouken@6602
   284
        button_text_height = IntMax( button_text_height, height );
slouken@6602
   285
    }
slouken@6602
   286
slouken@6725
   287
    if ( data->numlines ) {
slouken@6602
   288
        /* x,y for this line of text. */
slouken@6602
   289
        data->xtext = data->text_height;
slouken@6602
   290
        data->ytext = data->text_height + data->text_height;
slouken@6602
   291
slouken@6602
   292
        /* Bump button y down to bottom of text. */
slouken@6602
   293
        ybuttons = 3 * data->ytext / 2 + ( data->numlines - 1 ) * data->text_height;
slouken@6602
   294
slouken@6602
   295
        /* Bump the dialog box width and height up if needed. */
slouken@6602
   296
        data->dialog_width = IntMax( data->dialog_width, 2 * data->xtext + text_width_max );
slouken@6602
   297
        data->dialog_height = IntMax( data->dialog_height, ybuttons );
slouken@6725
   298
    } else {
slouken@6602
   299
        /* Button y starts at height of button text. */
slouken@6602
   300
        ybuttons = button_text_height;
slouken@6602
   301
    }
slouken@6602
   302
slouken@6725
   303
    if ( data->numbuttons ) {
slouken@6602
   304
        int x, y;
slouken@6602
   305
        int width_of_buttons;
slouken@6602
   306
        int button_spacing = button_text_height;
slouken@6602
   307
        int button_height = 2 * button_text_height;
slouken@6602
   308
slouken@6602
   309
        /* Bump button width up a bit. */
slouken@6602
   310
        button_width += button_text_height;
slouken@6602
   311
slouken@6602
   312
        /* Get width of all buttons lined up. */
slouken@6602
   313
        width_of_buttons = data->numbuttons * button_width + ( data->numbuttons - 1 ) * button_spacing;
slouken@6602
   314
slouken@6602
   315
        /* Bump up dialog width and height if buttons are wider than text. */
slouken@6602
   316
        data->dialog_width = IntMax( data->dialog_width, width_of_buttons + 2 * button_spacing );
slouken@6602
   317
        data->dialog_height = IntMax( data->dialog_height, ybuttons + 2 * button_height );
slouken@6602
   318
slouken@6602
   319
        /* Location for first button. */
slouken@6602
   320
        x = ( data->dialog_width - width_of_buttons ) / 2;
slouken@6602
   321
        y = ybuttons + ( data->dialog_height - ybuttons - button_height ) / 2;
slouken@6602
   322
slouken@6725
   323
        for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   324
            /* Button coordinates. */
slouken@6602
   325
            data->buttonpos[ i ].rect.x = x;
slouken@6602
   326
            data->buttonpos[ i ].rect.y = y;
slouken@6602
   327
            data->buttonpos[ i ].rect.w = button_width;
slouken@6602
   328
            data->buttonpos[ i ].rect.h = button_height;
slouken@6602
   329
slouken@6602
   330
            /* Button text coordinates. */
slouken@6602
   331
            data->buttonpos[ i ].x = x + ( button_width - data->buttonpos[ i ].text_width ) / 2;
slouken@6602
   332
            data->buttonpos[ i ].y = y + ( button_height - button_text_height - 1 ) / 2 + button_text_height;
slouken@6602
   333
slouken@6602
   334
            /* Scoot over for next button. */
slouken@6615
   335
            x += button_width + button_spacing;
slouken@6602
   336
        }
slouken@6602
   337
    }
slouken@6602
   338
slouken@6602
   339
    return 0;
slouken@6602
   340
}
slouken@6602
   341
slouken@6602
   342
/* Free SDL_MessageBoxData data. */
slouken@6602
   343
static void
slouken@6602
   344
X11_MessageBoxShutdown( SDL_MessageBoxDataX11 *data )
slouken@6602
   345
{
slouken@6725
   346
    if ( data->font_set != NULL ) {
icculus@7827
   347
        X11_XFreeFontSet( data->display, data->font_set );
icculus@6723
   348
        data->font_set = NULL;
icculus@6723
   349
    }
icculus@6723
   350
slouken@6725
   351
    if ( data->font_struct != NULL ) {
icculus@7827
   352
        X11_XFreeFont( data->display, data->font_struct );
icculus@6723
   353
        data->font_struct = NULL;
slouken@6602
   354
    }
slouken@6602
   355
icculus@9657
   356
#if SDL_VIDEO_DRIVER_X11_XDBE
icculus@9657
   357
    if ( SDL_X11_HAVE_XDBE && data->xdbe ) {
icculus@9657
   358
        X11_XdbeDeallocateBackBufferName(data->display, data->buf);
icculus@9657
   359
    }
icculus@9657
   360
#endif
icculus@9657
   361
slouken@6725
   362
    if ( data->display ) {
slouken@6725
   363
        if ( data->window != None ) {
icculus@7827
   364
            X11_XWithdrawWindow( data->display, data->window, data->screen );
icculus@7827
   365
            X11_XDestroyWindow( data->display, data->window );
slouken@6602
   366
            data->window = None;
slouken@6602
   367
        }
slouken@6602
   368
icculus@7827
   369
        X11_XCloseDisplay( data->display );
slouken@6602
   370
        data->display = NULL;
slouken@6602
   371
    }
slouken@6602
   372
}
slouken@6602
   373
slouken@6602
   374
/* Create and set up our X11 dialog box indow. */
slouken@6602
   375
static int
slouken@6602
   376
X11_MessageBoxCreateWindow( SDL_MessageBoxDataX11 *data )
slouken@6602
   377
{
slouken@6602
   378
    int x, y;
slouken@6602
   379
    XSizeHints *sizehints;
slouken@6602
   380
    XSetWindowAttributes wnd_attr;
jwyatt@9698
   381
    Atom _NET_WM_WINDOW_TYPE, _NET_WM_WINDOW_TYPE_DIALOG, _NET_WM_NAME, UTF8_STRING;
slouken@6602
   382
    Display *display = data->display;
slouken@6602
   383
    SDL_WindowData *windowdata = NULL;
slouken@6602
   384
    const SDL_MessageBoxData *messageboxdata = data->messageboxdata;
slouken@6602
   385
slouken@6602
   386
    if ( messageboxdata->window ) {
slouken@7522
   387
        SDL_DisplayData *displaydata =
slouken@7522
   388
            (SDL_DisplayData *) SDL_GetDisplayForWindow(messageboxdata->window)->driverdata;
slouken@6602
   389
        windowdata = (SDL_WindowData *)messageboxdata->window->driverdata;
slouken@7522
   390
        data->screen = displaydata->screen;
slouken@7522
   391
    } else {
slouken@7522
   392
        data->screen = DefaultScreen( display );
slouken@6602
   393
    }
slouken@6602
   394
slouken@6602
   395
    data->event_mask = ExposureMask |
slouken@6725
   396
                       ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask |
slouken@6725
   397
                       StructureNotifyMask | FocusChangeMask | PointerMotionMask;
slouken@6603
   398
    wnd_attr.event_mask = data->event_mask;
slouken@6602
   399
icculus@7827
   400
    data->window = X11_XCreateWindow(
slouken@7522
   401
                       display, RootWindow(display, data->screen),
slouken@6725
   402
                       0, 0,
slouken@6725
   403
                       data->dialog_width, data->dialog_height,
slouken@6725
   404
                       0, CopyFromParent, InputOutput, CopyFromParent,
slouken@6725
   405
                       CWEventMask, &wnd_attr );
slouken@6602
   406
    if ( data->window == None ) {
icculus@7037
   407
        return SDL_SetError("Couldn't create X window");
slouken@6602
   408
    }
slouken@6602
   409
slouken@6602
   410
    if ( windowdata ) {
slouken@6602
   411
        /* http://tronche.com/gui/x/icccm/sec-4.html#WM_TRANSIENT_FOR */
icculus@7827
   412
        X11_XSetTransientForHint( display, data->window, windowdata->xwindow );
slouken@6602
   413
    }
slouken@6602
   414
icculus@7827
   415
    X11_XStoreName( display, data->window, messageboxdata->title );
jwyatt@9698
   416
    _NET_WM_NAME = X11_XInternAtom(display, "_NET_WM_NAME", False);
jwyatt@9698
   417
    UTF8_STRING = X11_XInternAtom(display, "UTF8_STRING", False);
jwyatt@9698
   418
    X11_XChangeProperty(display, data->window, _NET_WM_NAME, UTF8_STRING, 8,
jwyatt@9698
   419
                    PropModeReplace, (unsigned char *) messageboxdata->title,
jwyatt@9698
   420
                    strlen(messageboxdata->title) + 1 );
slouken@6602
   421
icculus@9339
   422
    /* Let the window manager know this is a dialog box */
icculus@9339
   423
    _NET_WM_WINDOW_TYPE = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE", False);
icculus@9339
   424
    _NET_WM_WINDOW_TYPE_DIALOG = X11_XInternAtom(display, "_NET_WM_WINDOW_TYPE_DIALOG", False);
icculus@9339
   425
    X11_XChangeProperty(display, data->window, _NET_WM_WINDOW_TYPE, XA_ATOM, 32,
icculus@9339
   426
                    PropModeReplace,
icculus@9339
   427
                    (unsigned char *)&_NET_WM_WINDOW_TYPE_DIALOG, 1);
icculus@9339
   428
slouken@6602
   429
    /* Allow the window to be deleted by the window manager */
icculus@7827
   430
    data->wm_protocols = X11_XInternAtom( display, "WM_PROTOCOLS", False );
icculus@7827
   431
    data->wm_delete_message = X11_XInternAtom( display, "WM_DELETE_WINDOW", False );
icculus@7827
   432
    X11_XSetWMProtocols( display, data->window, &data->wm_delete_message, 1 );
slouken@6602
   433
slouken@6602
   434
    if ( windowdata ) {
slouken@6602
   435
        XWindowAttributes attrib;
slouken@6602
   436
        Window dummy;
slouken@6602
   437
icculus@7827
   438
        X11_XGetWindowAttributes(display, windowdata->xwindow, &attrib);
slouken@6602
   439
        x = attrib.x + ( attrib.width - data->dialog_width ) / 2;
slouken@6602
   440
        y = attrib.y + ( attrib.height - data->dialog_height ) / 3 ;
icculus@7827
   441
        X11_XTranslateCoordinates(display, windowdata->xwindow, RootWindow(display, data->screen), x, y, &x, &y, &dummy);
slouken@6602
   442
    } else {
icculus@9801
   443
        const SDL_VideoDevice *dev = SDL_GetVideoDevice();
icculus@9801
   444
        if ((dev) && (dev->displays) && (dev->num_displays > 0)) {
icculus@9801
   445
            const SDL_VideoDisplay *dpy = &dev->displays[0];
icculus@9801
   446
            const SDL_DisplayData *dpydata = (SDL_DisplayData *) dpy->driverdata;
icculus@9801
   447
            x = dpydata->x + (( dpy->current_mode.w - data->dialog_width ) / 2);
icculus@9801
   448
            y = dpydata->y + (( dpy->current_mode.h - data->dialog_height ) / 3);
icculus@9801
   449
        } else {   /* oh well. This will misposition on a multi-head setup. Init first next time. */
icculus@9801
   450
            x = ( DisplayWidth( display, data->screen ) - data->dialog_width ) / 2;
icculus@9801
   451
            y = ( DisplayHeight( display, data->screen ) - data->dialog_height ) / 3 ;
icculus@9801
   452
        }
slouken@6602
   453
    }
icculus@7827
   454
    X11_XMoveWindow( display, data->window, x, y );
slouken@6602
   455
icculus@7827
   456
    sizehints = X11_XAllocSizeHints();
slouken@6602
   457
    if ( sizehints ) {
slouken@6602
   458
        sizehints->flags = USPosition | USSize | PMaxSize | PMinSize;
slouken@6602
   459
        sizehints->x = x;
slouken@6602
   460
        sizehints->y = y;
slouken@6602
   461
        sizehints->width = data->dialog_width;
slouken@6602
   462
        sizehints->height = data->dialog_height;
slouken@6602
   463
slouken@6602
   464
        sizehints->min_width = sizehints->max_width = data->dialog_width;
slouken@6602
   465
        sizehints->min_height = sizehints->max_height = data->dialog_height;
slouken@6602
   466
icculus@7827
   467
        X11_XSetWMNormalHints( display, data->window, sizehints );
slouken@6602
   468
icculus@7827
   469
        X11_XFree( sizehints );
slouken@6602
   470
    }
slouken@6602
   471
icculus@7827
   472
    X11_XMapRaised( display, data->window );
icculus@9657
   473
icculus@9657
   474
#if SDL_VIDEO_DRIVER_X11_XDBE
icculus@9657
   475
    /* Initialise a back buffer for double buffering */
icculus@9657
   476
    if (SDL_X11_HAVE_XDBE) {
icculus@9657
   477
        int xdbe_major, xdbe_minor;
icculus@9657
   478
        if (X11_XdbeQueryExtension(display, &xdbe_major, &xdbe_minor) != 0) {
icculus@9657
   479
            data->xdbe = SDL_TRUE;
icculus@9657
   480
            data->buf = X11_XdbeAllocateBackBufferName(display, data->window, XdbeUndefined);
icculus@9657
   481
        } else {
icculus@9657
   482
            data->xdbe = SDL_FALSE;
icculus@9657
   483
        }
icculus@9657
   484
    }
icculus@9657
   485
#endif
icculus@9657
   486
slouken@6602
   487
    return 0;
slouken@6602
   488
}
slouken@6602
   489
slouken@6602
   490
/* Draw our message box. */
slouken@6602
   491
static void
slouken@6602
   492
X11_MessageBoxDraw( SDL_MessageBoxDataX11 *data, GC ctx )
slouken@6602
   493
{
slouken@6602
   494
    int i;
icculus@9657
   495
    Drawable window = data->window;
slouken@6602
   496
    Display *display = data->display;
slouken@6602
   497
icculus@9657
   498
#if SDL_VIDEO_DRIVER_X11_XDBE
icculus@9657
   499
    if (SDL_X11_HAVE_XDBE && data->xdbe) {
icculus@9657
   500
        window = data->buf;
icculus@9657
   501
        X11_XdbeBeginIdiom(data->display);
icculus@9657
   502
    }
icculus@9657
   503
#endif
icculus@9657
   504
icculus@7827
   505
    X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ] );
icculus@7827
   506
    X11_XFillRectangle( display, window, ctx, 0, 0, data->dialog_width, data->dialog_height );
slouken@6602
   507
icculus@7827
   508
    X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_TEXT ] );
slouken@6725
   509
    for ( i = 0; i < data->numlines; i++ ) {
slouken@6602
   510
        TextLineData *plinedata = &data->linedata[ i ];
slouken@6602
   511
icculus@6723
   512
        if (SDL_X11_HAVE_UTF8) {
icculus@7827
   513
            X11_Xutf8DrawString( display, window, data->font_set, ctx,
icculus@6723
   514
                             data->xtext, data->ytext + i * data->text_height,
icculus@6723
   515
                             plinedata->text, plinedata->length );
icculus@6723
   516
        } else {
icculus@7827
   517
            X11_XDrawString( display, window, ctx,
icculus@6723
   518
                         data->xtext, data->ytext + i * data->text_height,
icculus@6723
   519
                         plinedata->text, plinedata->length );
icculus@6723
   520
        }
slouken@6602
   521
    }
slouken@6602
   522
slouken@6725
   523
    for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   524
        SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ i ];
slouken@6602
   525
        const SDL_MessageBoxButtonData *buttondata = buttondatax11->buttondata;
slouken@6602
   526
        int border = ( buttondata->flags & SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT ) ? 2 : 0;
slouken@6602
   527
        int offset = ( ( data->mouse_over_index == i ) && ( data->button_press_index == data->mouse_over_index ) ) ? 1 : 0;
slouken@6602
   528
icculus@7827
   529
        X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_BACKGROUND ] );
icculus@7827
   530
        X11_XFillRectangle( display, window, ctx,
slouken@6602
   531
                        buttondatax11->rect.x - border, buttondatax11->rect.y - border,
slouken@6602
   532
                        buttondatax11->rect.w + 2 * border, buttondatax11->rect.h + 2 * border );
slouken@6602
   533
icculus@7827
   534
        X11_XSetForeground( display, ctx, data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_BORDER ] );
icculus@7827
   535
        X11_XDrawRectangle( display, window, ctx,
slouken@6602
   536
                        buttondatax11->rect.x, buttondatax11->rect.y,
slouken@6602
   537
                        buttondatax11->rect.w, buttondatax11->rect.h );
slouken@6602
   538
icculus@7827
   539
        X11_XSetForeground( display, ctx, ( data->mouse_over_index == i ) ?
slouken@6602
   540
                        data->color[ SDL_MESSAGEBOX_COLOR_BUTTON_SELECTED ] :
slouken@6603
   541
                        data->color[ SDL_MESSAGEBOX_COLOR_TEXT ] );
icculus@6723
   542
icculus@6723
   543
        if (SDL_X11_HAVE_UTF8) {
icculus@7827
   544
            X11_Xutf8DrawString( display, window, data->font_set, ctx,
icculus@6723
   545
                             buttondatax11->x + offset,
icculus@6723
   546
                             buttondatax11->y + offset,
icculus@6723
   547
                             buttondata->text, buttondatax11->length );
icculus@6723
   548
        } else {
icculus@7827
   549
            X11_XDrawString( display, window, ctx,
icculus@6723
   550
                         buttondatax11->x + offset, buttondatax11->y + offset,
icculus@6723
   551
                         buttondata->text, buttondatax11->length );
icculus@6723
   552
        }
slouken@6602
   553
    }
icculus@9657
   554
icculus@9657
   555
#if SDL_VIDEO_DRIVER_X11_XDBE
icculus@9657
   556
    if (SDL_X11_HAVE_XDBE && data->xdbe) {
icculus@9657
   557
        XdbeSwapInfo swap_info;
icculus@9657
   558
        swap_info.swap_window = data->window;
icculus@9657
   559
        swap_info.swap_action = XdbeUndefined;
icculus@9657
   560
        X11_XdbeSwapBuffers(data->display, &swap_info, 1);
icculus@9657
   561
        X11_XdbeEndIdiom(data->display);
icculus@9657
   562
    }
icculus@9657
   563
#endif
slouken@6602
   564
}
slouken@6602
   565
icculus@9695
   566
static Bool
icculus@9695
   567
X11_MessageBoxEventTest(Display *display, XEvent *event, XPointer arg)
icculus@9695
   568
{
icculus@9695
   569
    const SDL_MessageBoxDataX11 *data = (const SDL_MessageBoxDataX11 *) arg;
icculus@9695
   570
    return ((event->xany.display == data->display) && (event->xany.window == data->window)) ? True : False;
icculus@9695
   571
}
icculus@9695
   572
slouken@6602
   573
/* Loop and handle message box event messages until something kills it. */
slouken@6602
   574
static int
slouken@6602
   575
X11_MessageBoxLoop( SDL_MessageBoxDataX11 *data )
slouken@6602
   576
{
slouken@6602
   577
    GC ctx;
slouken@6602
   578
    XGCValues ctx_vals;
slouken@6602
   579
    SDL_bool close_dialog = SDL_FALSE;
slouken@6602
   580
    SDL_bool has_focus = SDL_TRUE;
slouken@6602
   581
    KeySym last_key_pressed = XK_VoidSymbol;
icculus@6723
   582
    unsigned long gcflags = GCForeground | GCBackground;
slouken@6602
   583
icculus@6723
   584
    SDL_zero(ctx_vals);
slouken@6602
   585
    ctx_vals.foreground = data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ];
slouken@6602
   586
    ctx_vals.background = data->color[ SDL_MESSAGEBOX_COLOR_BACKGROUND ];
slouken@6602
   587
icculus@6723
   588
    if (!SDL_X11_HAVE_UTF8) {
icculus@6723
   589
        gcflags |= GCFont;
icculus@6723
   590
        ctx_vals.font = data->font_struct->fid;
icculus@6723
   591
    }
icculus@6723
   592
icculus@7827
   593
    ctx = X11_XCreateGC( data->display, data->window, gcflags, &ctx_vals );
slouken@6602
   594
    if ( ctx == None ) {
icculus@7037
   595
        return SDL_SetError("Couldn't create graphics context");
slouken@6602
   596
    }
slouken@6602
   597
slouken@6602
   598
    data->button_press_index = -1;  /* Reset what button is currently depressed. */
slouken@6602
   599
    data->mouse_over_index = -1;    /* Reset what button the mouse is over. */
slouken@6602
   600
slouken@6602
   601
    while( !close_dialog ) {
slouken@6602
   602
        XEvent e;
slouken@6602
   603
        SDL_bool draw = SDL_TRUE;
slouken@6602
   604
icculus@9695
   605
        /* can't use XWindowEvent() because it can't handle ClientMessage events. */
icculus@9695
   606
        /* can't use XNextEvent() because we only want events for this window. */
icculus@9695
   607
        X11_XIfEvent( data->display, &e, X11_MessageBoxEventTest, (XPointer) data );
slouken@6602
   608
icculus@7827
   609
        /* If X11_XFilterEvent returns True, then some input method has filtered the
slouken@6602
   610
           event, and the client should discard the event. */
icculus@7827
   611
        if ( ( e.type != Expose ) && X11_XFilterEvent( &e, None ) )
slouken@6602
   612
            continue;
slouken@6602
   613
slouken@6602
   614
        switch( e.type ) {
slouken@6602
   615
        case Expose:
slouken@6602
   616
            if ( e.xexpose.count > 0 ) {
slouken@6602
   617
                draw = SDL_FALSE;
slouken@6602
   618
            }
slouken@6602
   619
            break;
slouken@6602
   620
slouken@6602
   621
        case FocusIn:
slouken@6602
   622
            /* Got focus. */
slouken@6602
   623
            has_focus = SDL_TRUE;
slouken@6602
   624
            break;
slouken@6602
   625
slouken@6602
   626
        case FocusOut:
slouken@6602
   627
            /* lost focus. Reset button and mouse info. */
slouken@6602
   628
            has_focus = SDL_FALSE;
slouken@6602
   629
            data->button_press_index = -1;
slouken@6602
   630
            data->mouse_over_index = -1;
slouken@6602
   631
            break;
slouken@6602
   632
slouken@6602
   633
        case MotionNotify:
slouken@6602
   634
            if ( has_focus ) {
slouken@6602
   635
                /* Mouse moved... */
icculus@9657
   636
                const int previndex = data->mouse_over_index;
slouken@6602
   637
                data->mouse_over_index = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
jorgen@8132
   638
                if (data->mouse_over_index == previndex) {
jorgen@8132
   639
                    draw = SDL_FALSE;
jorgen@8132
   640
                }
slouken@6602
   641
            }
slouken@6602
   642
            break;
slouken@6602
   643
slouken@6602
   644
        case ClientMessage:
slouken@6602
   645
            if ( e.xclient.message_type == data->wm_protocols &&
slouken@6602
   646
                 e.xclient.format == 32 &&
slouken@6602
   647
                 e.xclient.data.l[ 0 ] == data->wm_delete_message ) {
slouken@6602
   648
                close_dialog = SDL_TRUE;
slouken@6602
   649
            }
slouken@6602
   650
            break;
slouken@6602
   651
slouken@6602
   652
        case KeyPress:
slouken@6602
   653
            /* Store key press - we make sure in key release that we got both. */
icculus@7827
   654
            last_key_pressed = X11_XLookupKeysym( &e.xkey, 0 );
slouken@6602
   655
            break;
slouken@6602
   656
slouken@6725
   657
        case KeyRelease: {
slouken@6602
   658
            Uint32 mask = 0;
icculus@7827
   659
            KeySym key = X11_XLookupKeysym( &e.xkey, 0 );
slouken@6602
   660
slouken@6602
   661
            /* If this is a key release for something we didn't get the key down for, then bail. */
slouken@6602
   662
            if ( key != last_key_pressed )
slouken@6602
   663
                break;
slouken@6602
   664
slouken@6602
   665
            if ( key == XK_Escape )
slouken@6602
   666
                mask = SDL_MESSAGEBOX_BUTTON_ESCAPEKEY_DEFAULT;
slouken@6602
   667
            else if ( ( key == XK_Return ) || ( key == XK_KP_Enter ) )
slouken@6602
   668
                mask = SDL_MESSAGEBOX_BUTTON_RETURNKEY_DEFAULT;
slouken@6602
   669
slouken@6602
   670
            if ( mask ) {
slouken@6602
   671
                int i;
slouken@6602
   672
slouken@6602
   673
                /* Look for first button with this mask set, and return it if found. */
slouken@6602
   674
                for ( i = 0; i < data->numbuttons; i++ ) {
slouken@6602
   675
                    SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ i ];
slouken@6602
   676
slouken@6602
   677
                    if ( buttondatax11->buttondata->flags & mask ) {
slouken@6602
   678
                        *data->pbuttonid = buttondatax11->buttondata->buttonid;
slouken@6602
   679
                        close_dialog = SDL_TRUE;
slouken@6602
   680
                        break;
slouken@6602
   681
                    }
slouken@6602
   682
                }
slouken@6602
   683
            }
slouken@6602
   684
            break;
slouken@6602
   685
        }
slouken@6602
   686
slouken@6602
   687
        case ButtonPress:
slouken@6602
   688
            data->button_press_index = -1;
slouken@6602
   689
            if ( e.xbutton.button == Button1 ) {
slouken@6602
   690
                /* Find index of button they clicked on. */
slouken@6602
   691
                data->button_press_index = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
slouken@6602
   692
            }
slouken@6602
   693
            break;
slouken@6602
   694
slouken@6602
   695
        case ButtonRelease:
slouken@6602
   696
            /* If button is released over the same button that was clicked down on, then return it. */
slouken@6602
   697
            if ( ( e.xbutton.button == Button1 ) && ( data->button_press_index >= 0 ) ) {
slouken@6602
   698
                int button = GetHitButtonIndex( data, e.xbutton.x, e.xbutton.y );
slouken@6602
   699
slouken@6602
   700
                if ( data->button_press_index == button ) {
slouken@6602
   701
                    SDL_MessageBoxButtonDataX11 *buttondatax11 = &data->buttonpos[ button ];
slouken@6602
   702
slouken@6602
   703
                    *data->pbuttonid = buttondatax11->buttondata->buttonid;
slouken@6602
   704
                    close_dialog = SDL_TRUE;
slouken@6602
   705
                }
slouken@6602
   706
            }
slouken@6602
   707
            data->button_press_index = -1;
slouken@6602
   708
            break;
slouken@6602
   709
        }
slouken@6602
   710
slouken@6602
   711
        if ( draw ) {
slouken@6602
   712
            /* Draw our dialog box. */
slouken@6602
   713
            X11_MessageBoxDraw( data, ctx );
slouken@6602
   714
        }
slouken@6602
   715
    }
slouken@6602
   716
icculus@7827
   717
    X11_XFreeGC( data->display, ctx );
slouken@6602
   718
    return 0;
slouken@6602
   719
}
slouken@6602
   720
icculus@6726
   721
static int
icculus@6726
   722
X11_ShowMessageBoxImpl(const SDL_MessageBoxData *messageboxdata, int *buttonid)
slouken@6602
   723
{
slouken@6602
   724
    int ret;
slouken@6602
   725
    SDL_MessageBoxDataX11 data;
mikesart@6835
   726
#if SDL_SET_LOCALE
icculus@6724
   727
    char *origlocale;
mikesart@6835
   728
#endif
slouken@6602
   729
icculus@6723
   730
    SDL_zero(data);
icculus@6723
   731
slouken@6602
   732
    if ( !SDL_X11_LoadSymbols() )
slouken@6602
   733
        return -1;
slouken@6602
   734
mikesart@6835
   735
#if SDL_SET_LOCALE
icculus@6724
   736
    origlocale = setlocale(LC_ALL, NULL);
icculus@6724
   737
    if (origlocale != NULL) {
icculus@6724
   738
        origlocale = SDL_strdup(origlocale);
icculus@6724
   739
        if (origlocale == NULL) {
icculus@7037
   740
            return SDL_OutOfMemory();
icculus@6724
   741
        }
icculus@6724
   742
        setlocale(LC_ALL, "");
icculus@6724
   743
    }
mikesart@6835
   744
#endif
icculus@6724
   745
slouken@6602
   746
    /* This code could get called from multiple threads maybe? */
icculus@7827
   747
    X11_XInitThreads();
slouken@6602
   748
slouken@6602
   749
    /* Initialize the return buttonid value to -1 (for error or dialogbox closed). */
slouken@6602
   750
    *buttonid = -1;
slouken@6602
   751
slouken@6602
   752
    /* Init and display the message box. */
slouken@6602
   753
    ret = X11_MessageBoxInit( &data, messageboxdata, buttonid );
slouken@6602
   754
    if ( ret != -1 ) {
slouken@6602
   755
        ret = X11_MessageBoxInitPositions( &data );
slouken@6602
   756
        if ( ret != -1 ) {
slouken@6602
   757
            ret = X11_MessageBoxCreateWindow( &data );
slouken@6602
   758
            if ( ret != -1 ) {
slouken@6602
   759
                ret = X11_MessageBoxLoop( &data );
slouken@6602
   760
            }
slouken@6602
   761
        }
slouken@6602
   762
    }
slouken@6602
   763
slouken@6602
   764
    X11_MessageBoxShutdown( &data );
icculus@6724
   765
mikesart@6835
   766
#if SDL_SET_LOCALE
icculus@6724
   767
    if (origlocale) {
icculus@6724
   768
        setlocale(LC_ALL, origlocale);
icculus@6724
   769
        SDL_free(origlocale);
icculus@6724
   770
    }
mikesart@6835
   771
#endif
icculus@6724
   772
slouken@6602
   773
    return ret;
slouken@6602
   774
}
slouken@6602
   775
icculus@6726
   776
/* Display an x11 message box. */
icculus@6726
   777
int
icculus@6726
   778
X11_ShowMessageBox(const SDL_MessageBoxData *messageboxdata, int *buttonid)
icculus@6726
   779
{
icculus@6726
   780
#if SDL_FORK_MESSAGEBOX
icculus@6726
   781
    /* Use a child process to protect against setlocale(). Annoying. */
icculus@6726
   782
    pid_t pid;
icculus@6726
   783
    int fds[2];
icculus@6726
   784
    int status = 0;
icculus@6726
   785
icculus@6726
   786
    if (pipe(fds) == -1) {
icculus@6726
   787
        return X11_ShowMessageBoxImpl(messageboxdata, buttonid); /* oh well. */
icculus@6726
   788
    }
icculus@6726
   789
icculus@6726
   790
    pid = fork();
icculus@6726
   791
    if (pid == -1) {  /* failed */
icculus@6726
   792
        close(fds[0]);
icculus@6726
   793
        close(fds[1]);
icculus@6726
   794
        return X11_ShowMessageBoxImpl(messageboxdata, buttonid); /* oh well. */
icculus@6726
   795
    } else if (pid == 0) {  /* we're the child */
icculus@6726
   796
        int exitcode = 0;
icculus@6726
   797
        close(fds[0]);
icculus@6726
   798
        status = X11_ShowMessageBoxImpl(messageboxdata, buttonid);
icculus@6726
   799
        if (write(fds[1], &status, sizeof (int)) != sizeof (int))
icculus@6726
   800
            exitcode = 1;
icculus@6726
   801
        else if (write(fds[1], buttonid, sizeof (int)) != sizeof (int))
icculus@6726
   802
            exitcode = 1;
icculus@6726
   803
        close(fds[1]);
icculus@6726
   804
        _exit(exitcode);  /* don't run atexit() stuff, static destructors, etc. */
icculus@6726
   805
    } else {  /* we're the parent */
icculus@6726
   806
        pid_t rc;
icculus@6726
   807
        close(fds[1]);
icculus@6726
   808
        do {
icculus@6726
   809
            rc = waitpid(pid, &status, 0);
icculus@6726
   810
        } while ((rc == -1) && (errno == EINTR));
icculus@6726
   811
icculus@6726
   812
        SDL_assert(rc == pid);  /* not sure what to do if this fails. */
icculus@6726
   813
icculus@6726
   814
        if ((rc == -1) || (!WIFEXITED(status)) || (WEXITSTATUS(status) != 0)) {
icculus@7037
   815
            return SDL_SetError("msgbox child process failed");
icculus@6726
   816
        }
icculus@6726
   817
icculus@6726
   818
        if (read(fds[0], &status, sizeof (int)) != sizeof (int))
icculus@6726
   819
            status = -1;
icculus@6726
   820
        else if (read(fds[0], buttonid, sizeof (int)) != sizeof (int))
icculus@6726
   821
            status = -1;
icculus@6726
   822
        close(fds[0]);
icculus@6726
   823
icculus@6726
   824
        return status;
icculus@6726
   825
    }
icculus@6726
   826
#else
icculus@6726
   827
    return X11_ShowMessageBoxImpl(messageboxdata, buttonid);
icculus@6726
   828
#endif
icculus@6726
   829
}
slouken@6602
   830
#endif /* SDL_VIDEO_DRIVER_X11 */
slouken@6602
   831
slouken@6602
   832
/* vi: set ts=4 sw=4 expandtab: */