Back to home page

EIC code displayed by LXR

 
 

    


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   session(std::ostream& a_out,unsigned int a_monitor = 0)
0018   :parent(a_out,a_monitor)
0019   ,m_vinfo(0)
0020   ,m_ctx(0)
0021   ,m_colormap(0)
0022   {
0023     if(!m_display) return;
0024 
0025    {int glxMajor, glxMinor;
0026     ::glXQueryVersion(m_display,&glxMajor,&glxMinor);
0027     if(glxMajor<=0) {
0028       m_out << "toolx::X11::session::session : bad GLX-Version " << glxMajor << "." << glxMinor << std::endl;
0029       ::XCloseDisplay(m_display);
0030       m_display = 0;
0031       m_vinfo = 0;
0032       m_ctx = 0;
0033       return;
0034     }}
0035 
0036     static const int atbs_alpha[] = {
0037       GLX_RGBA,
0038       GLX_RED_SIZE,   1,
0039       GLX_GREEN_SIZE, 1,
0040       GLX_BLUE_SIZE,  1,
0041       GLX_ALPHA_SIZE, 1,
0042       GLX_DEPTH_SIZE, 1,
0043       GLX_DOUBLEBUFFER,
0044       None};
0045 
0046     //NOTE : macOS : glXChooseVisual leaks 640 bytes.
0047     m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs_alpha);
0048     if(!m_vinfo) {
0049       static const int atbs[] = {
0050         GLX_RGBA,
0051         GLX_RED_SIZE,   1,
0052         GLX_GREEN_SIZE, 1,
0053         GLX_BLUE_SIZE,  1,
0054         GLX_DEPTH_SIZE, 1,
0055         GLX_DOUBLEBUFFER,
0056         None};
0057 
0058       m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs);
0059       if(!m_vinfo) {
0060         m_out << "toolx::X11::session::session :"
0061               << " can't choose a visual on screen " <<  m_monitor << "."
0062               << std::endl;
0063         ::XCloseDisplay(m_display);
0064         m_display = 0;
0065         m_vinfo = 0;
0066         m_ctx = 0;
0067         return;
0068       }
0069     }
0070 
0071     m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_TRUE);
0072     if(!m_ctx) {
0073       m_out << "toolx::X11::session::session :"
0074             << " can't create a glX context with direct rendering."
0075             << std::endl;
0076       m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_FALSE);
0077       if(!m_ctx) {
0078         m_out << "toolx::X11::session::session :"
0079               << " can't create a glX context."
0080               << std::endl;
0081         ::XCloseDisplay(m_display);
0082         m_display = 0;
0083         m_vinfo = 0;
0084         m_ctx = 0;
0085         return;
0086       }
0087     }
0088 
0089     // It is better to create a colormap adapted to the visual.
0090     m_colormap = ::XCreateColormap(m_display,::XRootWindow(m_display,m_monitor),m_vinfo->visual,AllocNone);
0091     if(m_colormap==0L) {
0092       m_out << "toolx::X11::session::session : XCreateColormap failed." << std::endl;
0093       ::XCloseDisplay(m_display);
0094       m_display = 0;
0095       m_vinfo = 0;
0096       m_ctx = 0;
0097       return;
0098     }
0099   }
0100   virtual ~session() {
0101     if(m_display) {
0102       if(m_ctx) {
0103         ::glXDestroyContext(m_display,m_ctx);
0104         m_ctx = 0;
0105       }
0106       if(m_colormap) {
0107         ::XFreeColormap(m_display,m_colormap);
0108         m_colormap = 0;
0109       }
0110       ::XCloseDisplay(m_display);
0111       m_display = 0;
0112     }
0113     if(m_vinfo) {
0114       ::XFree(m_vinfo);
0115       m_vinfo = 0;
0116     }
0117   }
0118 protected:
0119   session(const session& a_from)
0120   :parent(a_from)
0121   ,m_vinfo(0)
0122   ,m_ctx(0)
0123   ,m_colormap(0)
0124   {}
0125   session& operator=(const session& a_from){
0126     if(&a_from==this) return *this;
0127     parent::operator=(a_from);
0128     return *this;
0129   }
0130 public:
0131   GLXContext context() const {return m_ctx;}
0132 
0133   Window create_window(const char* a_title,int a_x,int a_y,unsigned int a_width,unsigned int a_height) {
0134     if(!m_display) return 0L;
0135 
0136     XSetWindowAttributes swa;
0137     swa.event_mask = StructureNotifyMask | ExposureMask
0138        | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask
0139        | PointerMotionMask
0140        | KeyPressMask;
0141 
0142     swa.colormap = m_colormap;
0143     swa.border_pixel = 0L;
0144 
0145     Window window = ::XCreateWindow(m_display,
0146                                     ::XRootWindow(m_display,m_monitor),
0147                                     a_x,a_y,a_width,a_height,
0148                                     0,
0149                                     m_vinfo->depth,
0150                                     InputOutput,
0151                                     m_vinfo->visual,
0152                                     CWBorderPixel|CWColormap|CWEventMask,&swa);
0153 
0154     if(window==0L) {
0155       m_out << "toolx::X11::session::create_window :"
0156             << " can't create a X11 window."
0157             << std::endl;
0158       return 0L;
0159     }
0160 
0161     XTextProperty tp;
0162     ::XStringListToTextProperty((char**)&a_title,1,&tp);
0163     XSizeHints sh;
0164     sh.flags = USPosition | USSize;
0165     ::XSetWMProperties(m_display,window,&tp,&tp,0,0,&sh,0,0);
0166     ::XFree(tp.value);
0167 
0168     ::XSetWMProtocols(m_display,window,&m_WM_DELETE_WINDOW_atom,1);
0169     return window;
0170   }
0171 protected:
0172   XVisualInfo*  m_vinfo;
0173   GLXContext    m_ctx;
0174   Colormap      m_colormap;
0175 };
0176 
0177 }}
0178 
0179 #endif
0180