X11: Provide specific X error when SDL_GL_CreateContext fails.
authorJørgen P. Tjernø <jorgen@uberent.com>
Tue, 03 Jun 2014 21:13:00 -0700
changeset 8804a941cf4234c4
parent 8803 ace0e63268f3
child 8805 a5c009ff64d7
X11: Provide specific X error when SDL_GL_CreateContext fails.

This makes the X error handler used for GL context creation handle *all* errors
and provide the user with specific error messages when SDL_GL_CreateContext
fails.

CR: icculus@icculus.org
src/video/x11/SDL_x11opengl.c
     1.1 --- a/src/video/x11/SDL_x11opengl.c	Mon Jun 02 09:20:09 2014 -0700
     1.2 +++ b/src/video/x11/SDL_x11opengl.c	Tue Jun 03 21:13:00 2014 -0700
     1.3 @@ -539,24 +539,30 @@
     1.4  #endif
     1.5  static int (*handler) (Display *, XErrorEvent *) = NULL;
     1.6  static int errorBase = 0;
     1.7 +static int errorCode = 0;
     1.8  static int
     1.9  X11_GL_CreateContextErrorHandler(Display * d, XErrorEvent * e)
    1.10  {
    1.11 -    switch (e->error_code) {
    1.12 -    case BadRequest:
    1.13 -    case BadMatch:
    1.14 -    case BadValue:
    1.15 -    case BadAlloc:
    1.16 -        return (0);
    1.17 -    default:
    1.18 -        if (errorBase && 
    1.19 -            (e->error_code == errorBase + GLXBadContext ||
    1.20 -             e->error_code == errorBase + GLXBadFBConfig ||
    1.21 -             e->error_code == errorBase + GLXBadProfileARB)) {
    1.22 -            return (0);
    1.23 -        }
    1.24 -        return (handler(d, e));
    1.25 +    char *x11_error = NULL;
    1.26 +    char x11_error_locale[256];
    1.27 +
    1.28 +    errorCode = e->error_code;
    1.29 +    if (X11_XGetErrorText(d, errorCode, x11_error_locale, sizeof(x11_error_locale)) == Success)
    1.30 +    {
    1.31 +        x11_error = SDL_iconv_string("UTF-8", "", x11_error_locale, strlen(x11_error_locale));
    1.32      }
    1.33 +
    1.34 +    if (x11_error)
    1.35 +    {
    1.36 +        SDL_SetError("Could not create GL context: %s", x11_error);
    1.37 +        SDL_free(x11_error);
    1.38 +    }
    1.39 +    else
    1.40 +    {
    1.41 +        SDL_SetError("Could not create GL context: %i (Base %i)\n", errorCode, errorBase);
    1.42 +    }
    1.43 +
    1.44 +    return (0);
    1.45  }
    1.46  
    1.47  SDL_GLContext
    1.48 @@ -581,6 +587,7 @@
    1.49      /* We do this to create a clean separation between X and GLX errors. */
    1.50      X11_XSync(display, False);
    1.51      errorBase = _this->gl_data->errorBase;
    1.52 +    errorCode = Success;
    1.53      handler = X11_XSetErrorHandler(X11_GL_CreateContextErrorHandler);
    1.54      X11_XGetWindowAttributes(display, data->xwindow, &xattr);
    1.55      v.screen = screen;
    1.56 @@ -675,7 +682,9 @@
    1.57      X11_XSetErrorHandler(handler);
    1.58  
    1.59      if (!context) {
    1.60 -        SDL_SetError("Could not create GL context");
    1.61 +        if (errorCode == Success) {
    1.62 +            SDL_SetError("Could not create GL context");
    1.63 +        }
    1.64          return NULL;
    1.65      }
    1.66