问题描述
我尝试在网上搜索,但我必须注意,要找到有关 X 编程这方面的资料并不容易.
I tried searching the web, but I must note that finding materials about this aspect of X programming is not really easy.
我使用 X 和 GLX 来创建 OpenGL 上下文.我已经知道我当前的显卡驱动程序最多只支持 OpenGL API 3.3 版,但我希望我的应用程序能够尝试使用任何类型的版本创建上下文(因为它可以在其他计算机上运行).我的代码是这样的:
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 :
- 版本
- 创建上下文:
- 如果版本是3.x或4.x,使用
glXCreateContextAttribsARB
- 否则使用
glXCreateContext
- Version <- requested OpenGL Version (by example : 3.3)
- Create a context :
- if Version is 3.x or 4.x, use
glXCreateContextAttribsARB
- else use
glXCreateContext
我的代码没问题,但我想在检索 X/GLX 启动的错误的方式上更清晰,至于目前,如果我使用
glXCreateContextAttribARB
创建一个 4.4版本(记住,我的显卡最多只支持3.3),我显然得到: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
我想在我的代码中插入 X 的错误处理来处理它.X 是 C,不是 C++,异常在这个阶段不可用.这是我创建上下文的地方(我故意删除了不是直接创建上下文的内容):
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); } }
所以我的问题是如何捕获 X 错误并查询它们?
So my question is how can I catch X error and query them ?
感谢阅读:)
推荐答案
您需要使用
XSetErrorHandler
来指定错误处理程序,例如You need to use
XSetErrorHandler
to specify an error handler e.g.XSetErrorHandler(handler);
错误处理程序是
int handler(Display * d, XErrorEvent * e) { std::cerr << "Error code: " << e->error_code << std::endl; return 0; }
这篇关于如何捕捉 X 错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
- if Version is 3.x or 4.x, use
- 如果版本是3.x或4.x,使用