Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to catch X errors?

Tags:

c

unix

glx

xlib

I tried searching the web, but I must note that finding materials about this aspect of X programming is not really easy.

I use X with GLX to create OpenGL contexts. I already know my current graphics card driver only supports up to OpenGL API version 3.3, but I want my application to be able to try to create a context with any kind of version (as it could run on other computers). My code goes like this :

  • Version <- requested OpenGL Version (by example : 3.3)
  • Create a context :
    • if Version is 3.x or 4.x, use glXCreateContextAttribsARB
    • else use glXCreateContext
  • If creating the context failed, go down one version (3.3 becomes 3.2, or 3.0 becomes 2.1, ...)
  • Stop if a context is created or if couldn't even using the minimum OpenGL version.

My code is OK, but I'd like to be more clean on the way I retrieve the errors launched by X/GLX, as for the moment, if I use glXCreateContextAttribARB to create a 4.4 version (remember, my graphics card only supports up to 3.3), I obviously get :

X Error of failed request:  BadMatch (invalid parameter attributes)
  Major opcode of failed request:  153 (GLX)
  Minor opcode of failed request:  34 ()
  Serial number of failed request:  33
  Current serial number in output stream:  34

I'd like to insert error handling for X in my code to handle it. X being C, not C++, exceptions are not usable at this stage. Here is where I create the context (I intentionally removed what does not directly is context creation) :

// Notes :
// mGLXContext : The GLX context we want to create
// vDepthSize, vAntialiasingLevel, vStencilSize are here to customize mGLXContext
// vTryVersion : a pointer to the API version {major, minor} we want to create
// vSharedContext : a pointer to an other (existing) context for data sharing.
// mXDisplay : the X Display


// Get the proc
const GLubyte* vProcName = reinterpret_cast<const GLubyte*>("glXCreateContextAttribsARB");
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = 
    reinterpret_cast<PFNGLXCREATECONTEXTATTRIBSARBPROC>(glXGetProcAddress(vProcName));

if(glXCreateContextAttribsARB) {

    // Create the FB attributes
    int vFBAttributes[] = {
        GLX_DEPTH_SIZE, (int)(vDepthSize),
        GLX_STENCIL_SIZE, (int)(vStencilSize),
        GLX_SAMPLE_BUFFERS, vAntialiasingLevel > 0,
        GLX_SAMPLES, (int)(vAntialiasingLevel),
        GLX_RED_SIZE, 8,
        GLX_GREEN_SIZE, 8,
        GLX_BLUE_SIZE, 8,
        GLX_ALPHA_SIZE, pDepth == 32 ? 8 : 0,
        GLX_DOUBLEBUFFER, True,
        GLX_X_RENDERABLE, True,
        GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
        GLX_RENDER_TYPE, GLX_RGBA_BIT,
        GLX_CONFIG_CAVEAT, GLX_NONE,
        None
    };


    // Get the FB configs
    int vNbXConfigs = 0;
    ::GLXFBConfig* vGLXFBConfig = glXChooseFBConfig(mXDisplay, DefaultScreen(mXDisplay), vFBAttributes, &vNbXConfigs);

    if(vGLXFBConfig && vNbXConfigs) {

        // Create the context attributes
        int vAttributes[] = {
            GLX_CONTEXT_MAJOR_VERSION_ARB, static_cast<int>(vTryVersion->major),
            GLX_CONTEXT_MINOR_VERSION_ARB, static_cast<int>(vTryVersion->minor),
            0, 0
        };

        // Create the context : Error is generated by GLX here
        mGLXContext = glXCreateContextAttribsARB(mXDisplay, vGLXFBConfig[0], vSharedContext, true, vAttributes);

    }
}

So my question is how can I catch X error and query them ?

Thank you for reading :)

like image 763
Oragon Efreet Avatar asked Nov 20 '13 21:11

Oragon Efreet


People also ask

How do you handle errors?

There are four ways to handle errors in Swift. You can propagate the error from a function to the code that calls that function, handle the error using a do - catch statement, handle the error as an optional value, or assert that the error will not occur.

How do you handle JavaScript errors?

JavaScript provides error-handling mechanism to catch runtime errors using try-catch-finally block, similar to other languages like Java or C#. try: wrap suspicious code that may throw an error in try block. catch: write code to do something in catch block when an error occurs.

What is error handling in Swift?

Steps For Error Handling in Swift Create an enum that represents the types of errors. Create a throwing function using the throws keyword. Call the function using the try keyword. Wrap the code with try in the do {...} block and add the catch {...}


1 Answers

You need to use XSetErrorHandler to specify an error handler e.g.

XSetErrorHandler(handler);

The error handler is

int handler(Display * d, XErrorEvent * e)
{
    std::cerr << "Error code: " << e->error_code << std::endl;
    return 0;
}
like image 179
parkydr Avatar answered Oct 03 '22 15:10

parkydr