Warning, /include/Geant4/toolx/X11/session is written in an unsupported language. File is not indexed.
0001 // Copyright (C) 2010, Guy Barrand. All rights reserved.
0002 // See the file tools.license for terms.
0003
0004 #ifndef toolx_X11_session
0005 #define toolx_X11_session
0006
0007 #include "base_session"
0008
0009 #include <GL/glx.h>
0010
0011 namespace toolx {
0012 namespace X11 {
0013
0014 class session : public base_session {
0015 typedef base_session parent;
0016 public:
0017 //virtual bool make_current(Window a_window) const {
0018 // if(!m_display) return false;
0019 // if(::glXMakeCurrent(m_display,a_window,m_ctx)==False) {
0020 // m_out << "toolx::X11::session::make_current : glXMakeCurrent failed." << std::endl;
0021 // return false;
0022 // }
0023 // return true;
0024 //}
0025 //virtual bool swap_buffers(Window a_window) const {
0026 // if(!m_display) return false;
0027 // ::glXSwapBuffers(m_display,a_window);
0028 // return true;
0029 //}
0030 public:
0031 session(std::ostream& a_out,unsigned int a_monitor = 0)
0032 :parent(a_out,a_monitor)
0033 ,m_vinfo(0)
0034 ,m_ctx(0)
0035 ,m_colormap(0)
0036 {
0037 if(!m_display) return;
0038
0039 {int glxMajor, glxMinor;
0040 ::glXQueryVersion(m_display,&glxMajor,&glxMinor);
0041 if(glxMajor<=0) {
0042 m_out << "toolx::X11::session::session : bad GLX-Version " << glxMajor << "." << glxMinor << std::endl;
0043 ::XCloseDisplay(m_display);
0044 m_display = 0;
0045 m_vinfo = 0;
0046 m_ctx = 0;
0047 return;
0048 }}
0049
0050 static const int atbs_alpha[] = {
0051 GLX_RGBA,
0052 GLX_RED_SIZE, 1,
0053 GLX_GREEN_SIZE, 1,
0054 GLX_BLUE_SIZE, 1,
0055 GLX_ALPHA_SIZE, 1,
0056 GLX_DEPTH_SIZE, 1,
0057 GLX_DOUBLEBUFFER,
0058 None};
0059
0060 //NOTE : macOS : glXChooseVisual leaks 640 bytes.
0061 m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs_alpha);
0062 if(!m_vinfo) {
0063 //m_out << "toolx::X11::session::initialize :"
0064 // << " can't get a visual with alpha on screen " << m_monitor << ". Try without alpha..."
0065 // << std::endl;
0066 //m_out << "toolx::X11::session::initialize :"
0067 // << " DefaultScreen(m_display): " << DefaultScreen(m_display) << "."
0068 // << std::endl;
0069
0070 static const int atbs[] = {
0071 GLX_RGBA,
0072 GLX_RED_SIZE, 1,
0073 GLX_GREEN_SIZE, 1,
0074 GLX_BLUE_SIZE, 1,
0075 GLX_DEPTH_SIZE, 1,
0076 GLX_DOUBLEBUFFER,
0077 None};
0078
0079 m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs);
0080 if(!m_vinfo) {
0081 m_out << "toolx::X11::session::session :"
0082 << " can't choose a visual on screen " << m_monitor << "."
0083 << std::endl;
0084 ::XCloseDisplay(m_display);
0085 m_display = 0;
0086 m_vinfo = 0;
0087 m_ctx = 0;
0088 return;
0089 }
0090 }
0091
0092 m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_TRUE);
0093 if(!m_ctx) {
0094 m_out << "toolx::X11::session::session :"
0095 << " can't create a glX context with direct rendering."
0096 << std::endl;
0097 m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_FALSE);
0098 if(!m_ctx) {
0099 m_out << "toolx::X11::session::session :"
0100 << " can't create a glX context."
0101 << std::endl;
0102 ::XCloseDisplay(m_display);
0103 m_display = 0;
0104 m_vinfo = 0;
0105 m_ctx = 0;
0106 return;
0107 }
0108 //} else {
0109 //m_out << "toolx::X11::session::session :"
0110 // << " glX context with direct rendering created."
0111 // << std::endl;
0112 }
0113
0114 // It is better to create a colormap adapted to the visual.
0115 m_colormap = ::XCreateColormap(m_display,::XRootWindow(m_display,m_monitor),m_vinfo->visual,AllocNone);
0116 //m_colormap = ::XDefaultColormap(m_display,m_monitor);
0117 if(m_colormap==0L) {
0118 m_out << "toolx::X11::session::session : XCreateColormap failed." << std::endl;
0119 ::XCloseDisplay(m_display);
0120 m_display = 0;
0121 m_vinfo = 0;
0122 m_ctx = 0;
0123 return;
0124 }
0125 }
0126 virtual ~session() {
0127 if(m_display) {
0128 if(m_ctx) {
0129 ::glXDestroyContext(m_display,m_ctx);
0130 m_ctx = 0;
0131 }
0132 if(m_colormap) {
0133 ::XFreeColormap(m_display,m_colormap);
0134 m_colormap = 0;
0135 }
0136 ::XCloseDisplay(m_display);
0137 m_display = 0;
0138 }
0139 if(m_vinfo) {
0140 ::XFree(m_vinfo);
0141 m_vinfo = 0;
0142 }
0143 //std::cout << "debug : ~session" << std::endl;
0144 }
0145 protected:
0146 session(const session& a_from)
0147 :parent(a_from)
0148 ,m_vinfo(0)
0149 ,m_ctx(0)
0150 ,m_colormap(0)
0151 {}
0152 session& operator=(const session& a_from){
0153 if(&a_from==this) return *this;
0154 parent::operator=(a_from);
0155 return *this;
0156 }
0157 public:
0158 GLXContext context() const {return m_ctx;}
0159
0160 Window create_window(const char* a_title,int a_x,int a_y,unsigned int a_width,unsigned int a_height) {
0161 if(!m_display) return 0L;
0162
0163 XSetWindowAttributes swa;
0164 swa.event_mask = StructureNotifyMask | ExposureMask
0165 | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
0166 | PointerMotionMask
0167 | KeyPressMask;
0168
0169 swa.colormap = m_colormap;
0170 swa.border_pixel = 0L;
0171
0172 Window window = ::XCreateWindow(m_display,
0173 ::XRootWindow(m_display,m_monitor),
0174 a_x,a_y,a_width,a_height,
0175 0,
0176 m_vinfo->depth,
0177 InputOutput,
0178 m_vinfo->visual,
0179 CWBorderPixel|CWColormap|CWEventMask,&swa);
0180
0181 if(window==0L) {
0182 m_out << "toolx::X11::session::create_window :"
0183 << " can't create a X11 window."
0184 << std::endl;
0185 return 0L;
0186 }
0187
0188 XTextProperty tp;
0189 ::XStringListToTextProperty((char**)&a_title,1,&tp);
0190 XSizeHints sh;
0191 sh.flags = USPosition | USSize;
0192 ::XSetWMProperties(m_display,window,&tp,&tp,0,0,&sh,0,0);
0193 ::XFree(tp.value);
0194
0195 ::XSetWMProtocols(m_display,window,&m_WM_DELETE_WINDOW_atom,1);
0196 return window;
0197 }
0198 protected:
0199 XVisualInfo* m_vinfo;
0200 GLXContext m_ctx;
0201 Colormap m_colormap;
0202 };
0203
0204 }}
0205
0206 #endif
0207