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 :
glXCreateContextAttribsARB
glXCreateContext
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 :)
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.
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.
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 {...}
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With