fixed: auto_ptr -> unique_ptr
[opensg.git] / Source / WindowSystem / X / OSGXWindow.cpp
blobd59ea13babf9656f04d9e7d772b2295098ebcaa3
1 /*---------------------------------------------------------------------------*\
2 * OpenSG *
3 * *
4 * *
5 * Copyright (C) 2000-2002 by the OpenSG Forum *
6 * *
7 * www.opensg.org *
8 * *
9 * contact: dirk@opensg.org, gerrit.voss@vossg.org, jbehr@zgdv.de *
10 * *
11 \*---------------------------------------------------------------------------*/
12 /*---------------------------------------------------------------------------*\
13 * License *
14 * *
15 * This library is free software; you can redistribute it and/or modify it *
16 * under the terms of the GNU Library General Public License as published *
17 * by the Free Software Foundation, version 2. *
18 * *
19 * This library is distributed in the hope that it will be useful, but *
20 * WITHOUT ANY WARRANTY; without even the implied warranty of *
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
22 * Library General Public License for more details. *
23 * *
24 * You should have received a copy of the GNU Library General Public *
25 * License along with this library; if not, write to the Free Software *
26 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *
27 * *
28 \*---------------------------------------------------------------------------*/
29 /*---------------------------------------------------------------------------*\
30 * Changes *
31 * *
32 * *
33 * *
34 * *
35 * *
36 * *
37 \*---------------------------------------------------------------------------*/
39 //---------------------------------------------------------------------------
40 // Includes
41 //---------------------------------------------------------------------------
43 #include <cstdlib>
44 #include <cstdio>
46 #include "OSGConfig.h"
48 #define OSG_COMPILEWINDOWXINST
50 #include "OSGGLU.h"
51 #include "OSGGLEXT.h"
52 #include "OSGGLFuncProtos.h"
53 #include "OSGGLXFuncProtos.h"
55 #include "OSGXWindow.h"
58 OSG_USING_NAMESPACE
60 // Documentation for this class is emited in the
61 // OSGXWindowBase.cpp file.
62 // To modify it, please change the .fcd file (OSGXWindow.fcd) and
63 // regenerate the base file.
65 /*----------------------- constructors & destructors ----------------------*/
67 //! Constructor
68 XWindow::XWindow(void) :
69 Inherited()
73 //! Copy Constructor
74 XWindow::XWindow(const XWindow &source) :
75 Inherited(source)
79 //! Destructor
80 XWindow::~XWindow(void)
84 /*----------------------------- class specific ----------------------------*/
86 //! initialize the static features of the class, e.g. action callbacks
87 void XWindow::initMethod(InitPhase ePhase)
89 Inherited::initMethod(ePhase);
92 //! react to field changes
93 void XWindow::changed(ConstFieldMaskArg whichField,
94 UInt32 origin,
95 BitVector details)
97 Inherited::changed(whichField, origin, details);
100 //! output the instance for debug purposes
101 void XWindow::dump( UInt32 ,
102 const BitVector ) const
104 SLOG << "Dump XWindow NI" << std::endl;
107 #ifdef OSG_DEBUG_OLD_C_CASTS
108 #ifdef DisplayString
109 #undef DisplayString
110 #endif
111 #define DisplayString(dpy)((reinterpret_cast<_XPrivDisplay>(dpy))->display_name)
112 #ifdef ScreenOfDisplay
113 #undef ScreenOfDisplay
114 #endif
115 #ifdef DefaultScreen
116 #undef DefaultScreen
117 #endif
118 #define ScreenOfDisplay(dpy, scr)(&(_XPrivDisplay(dpy))->screens[scr])
119 #define DefaultScreen(dpy) ((_XPrivDisplay(dpy))->default_screen)
120 #endif
122 void XWindow::classicInit(void)
124 XVisualInfo *vi, visInfo;
125 XWindowAttributes winAttr;
127 XGetWindowAttributes(getDisplay(), getWindow(), &winAttr);
129 // get the existing glWidget's visual-id
130 memset(&visInfo, 0, sizeof(XVisualInfo));
132 visInfo.visualid = XVisualIDFromVisual(winAttr.visual);
134 // get new display-variable
135 if(getDisplay() == NULL)
137 setDisplay(XOpenDisplay(DisplayString(getDisplay())));
140 // get a visual for the glx context
141 int nvis;
143 vi = XGetVisualInfo(getDisplay(), VisualIDMask, &visInfo, &nvis);
145 // is the visual GL-capable ?
146 int useGL;
147 glXGetConfig( getDisplay(),
148 vi,
149 GLX_USE_GL,
150 &useGL );
152 if (!useGL)
154 SFATAL << "Visual is not OpenGL-capable!" << std::endl;
155 exit(0);
158 // create the new context
159 this->setContext(glXCreateContext(getDisplay(), vi, None, GL_TRUE));
161 XFree(vi);
164 /*! Init the window: create the context and setup the OpenGL.
166 void XWindow::init(GLInitFunctor oFunc)
168 if(_sfFbConfigId.getValue() == -1)
170 classicInit();
172 else
174 OSGGETGLFUNCBYNAME_EXT(glxChooseFBConfig,
175 osgGlxChooseFBConfig,
176 "glXChooseFBConfig",
177 this);
179 OSG_ASSERT(osgGlxChooseFBConfig != NULL);
181 int iMatching;
183 int fbAttr[] =
185 GLX_FBCONFIG_ID, _sfFbConfigId.getValue(),
186 None
189 GLXFBConfig *fbConfigs =
190 osgGlxChooseFBConfig( getDisplay(),
191 DefaultScreen(getDisplay()),
192 fbAttr,
193 &iMatching);
195 if(iMatching <= 0)
197 fprintf(stderr, "no valid fbconfig %d\n",
198 _sfFbConfigId.getValue());
200 exit(0);
204 OSGGETGLFUNCBYNAME_EXT(glxCreateContextAttribsARB,
205 osgGlxCreateContextAttribsARB,
206 "glXCreateContextAttribsARB",
207 this);
209 if(osgGlxCreateContextAttribsARB != NULL)
211 std::vector<int> ctxAttr;
213 if(getRequestMajor() > 0)
215 ctxAttr.push_back(GLX_CONTEXT_MAJOR_VERSION_ARB);
216 ctxAttr.push_back(getRequestMajor());
217 ctxAttr.push_back(GLX_CONTEXT_MINOR_VERSION_ARB);
218 ctxAttr.push_back(getRequestMinor());
221 if(getContextFlags() != 0)
223 ctxAttr.push_back(GLX_CONTEXT_FLAGS_ARB);
224 ctxAttr.push_back(getContextFlags() );
227 ctxAttr.push_back(None);
229 this->setContext(osgGlxCreateContextAttribsARB( getDisplay(),
230 fbConfigs[0],
231 None,
232 GL_TRUE,
233 &(ctxAttr.front())));
235 if(getContext() == NULL)
237 FWARNING(("Could not create context, requested version "
238 "%d.%d might not be supported (guessing)\n",
239 getRequestMajor(),
240 getRequestMinor()));
242 exit(0);
245 XFree(fbConfigs);
247 else
249 classicInit();
253 Inherited::init(oFunc);
256 void XWindow::terminate(void)
258 Inherited::doTerminate();
260 if(getDisplay() != NULL && getContext() != NULL)
262 this->doDeactivate();
264 glXDestroyContext(getDisplay(), getContext());
266 setContext(NULL);
270 // activate the window: bind the OGL context
271 void XWindow::doActivate(void)
273 Bool res;
275 #ifdef OSG_DEBUG
276 if(getDisplay() == NULL)
278 SWARNING << "XWindow::doActivate: Display is NULL, can not activate context."
279 << std::endl;
282 if(getWindow() == 0)
284 SWARNING << "XWindow::doActivate: Window is NULL, can not activate "
285 << "context."
286 << std::endl;
289 if(getContext() == NULL)
291 SWARNING << "XWindow::doActivate: Context is NULL, can not activate "
292 << "context."
293 << std::endl;
295 #endif
297 res = glXMakeCurrent(getDisplay(), getWindow(), getContext());
299 if(res != True)
301 FWARNING(("XWindow::activate: makeCurrent failed!\n"));
302 glErr("XWindow::activate");
303 glErr("XWindow::activate");
304 glErr("XWindow::activate");
308 // activate the window: bind the OGL context
309 void XWindow::doDeactivate(void)
311 glXMakeCurrent(getDisplay(), None, NULL);
314 // swap front and back buffers
315 bool XWindow::doSwap(void)
317 glXSwapBuffers(getDisplay(), getWindow());
318 return true;
321 bool XWindow::hasContext(void)
323 return (this->getContext() != NULL);
326 void XWindow::onDestroy(UInt32 uiContainerId)
328 Inherited::onDestroy(uiContainerId);
332 #include "OSGSField.ins"
333 #include "OSGMField.ins"
335 #if defined(OSG_TMPL_STATIC_MEMBER_NEEDS_FUNCTION_INSTANTIATION) || \
336 defined(OSG_TMPL_STATIC_MEMBER_NEEDS_CLASS_INSTANTIATION )
338 #include "OSGSFieldFuncs.ins"
339 #include "OSGMFieldFuncs.ins"
340 #endif
342 OSG_BEGIN_NAMESPACE
344 DataType FieldTraits<DisplayP, 2>::_type("DisplayP", NULL);
345 #if ( !defined(__GNUC__) || !defined(__linux) || ( !defined(__ia64) && !defined(__x86_64) && !defined(_ARCH_PPC64)) ) && (!defined(_MIPS_SZPTR) || _MIPS_SZPTR != 64)
346 DataType FieldTraits<X11Window, 2>::_type("X11Window", NULL);
347 #endif
348 DataType FieldTraits<GLXContext, 2>::_type("GLXContext", NULL);
350 OSG_FIELD_DLLEXPORT_DEF2(SField, DisplayP, 2)
351 OSG_FIELD_DLLEXPORT_DEF2(MField, DisplayP, 2)
352 #if ( !defined(__GNUC__) || !defined(__linux) || ( !defined(__ia64) && !defined(__x86_64) && !defined(_ARCH_PPC64)) ) && (!defined(_MIPS_SZPTR) || _MIPS_SZPTR != 64)
353 OSG_FIELD_DLLEXPORT_DEF2(SField, X11Window, 2)
354 OSG_FIELD_DLLEXPORT_DEF2(MField, X11Window, 2)
355 #endif
356 OSG_FIELD_DLLEXPORT_DEF2(SField, GLXContext, 2)
357 OSG_FIELD_DLLEXPORT_DEF2(MField, GLXContext, 2)
359 OSG_END_NAMESPACE