Geant4 Cross Reference |
1 // Copyright (C) 2010, Guy Barrand. All rights 1 // Copyright (C) 2010, Guy Barrand. All rights reserved. 2 // See the file tools.license for terms. 2 // See the file tools.license for terms. 3 3 4 #ifndef toolx_X11_session 4 #ifndef toolx_X11_session 5 #define toolx_X11_session 5 #define toolx_X11_session 6 6 7 #include "base_session" 7 #include "base_session" 8 8 9 #include <GL/glx.h> 9 #include <GL/glx.h> 10 10 11 namespace toolx { 11 namespace toolx { 12 namespace X11 { 12 namespace X11 { 13 13 14 class session : public base_session { 14 class session : public base_session { 15 typedef base_session parent; 15 typedef base_session parent; 16 public: 16 public: 17 //virtual bool make_current(Window a_window) 17 //virtual bool make_current(Window a_window) const { 18 // if(!m_display) return false; 18 // if(!m_display) return false; 19 // if(::glXMakeCurrent(m_display,a_window,m 19 // if(::glXMakeCurrent(m_display,a_window,m_ctx)==False) { 20 // m_out << "toolx::X11::session::make_cu 20 // m_out << "toolx::X11::session::make_current : glXMakeCurrent failed." << std::endl; 21 // return false; 21 // return false; 22 // } 22 // } 23 // return true; 23 // return true; 24 //} 24 //} 25 //virtual bool swap_buffers(Window a_window) 25 //virtual bool swap_buffers(Window a_window) const { 26 // if(!m_display) return false; 26 // if(!m_display) return false; 27 // ::glXSwapBuffers(m_display,a_window); 27 // ::glXSwapBuffers(m_display,a_window); 28 // return true; 28 // return true; 29 //} 29 //} 30 public: 30 public: 31 session(std::ostream& a_out,unsigned int a_m 31 session(std::ostream& a_out,unsigned int a_monitor = 0) 32 :parent(a_out,a_monitor) 32 :parent(a_out,a_monitor) 33 ,m_vinfo(0) 33 ,m_vinfo(0) 34 ,m_ctx(0) 34 ,m_ctx(0) 35 ,m_colormap(0) 35 ,m_colormap(0) 36 { 36 { 37 if(!m_display) return; 37 if(!m_display) return; 38 38 39 {int glxMajor, glxMinor; 39 {int glxMajor, glxMinor; 40 ::glXQueryVersion(m_display,&glxMajor,&glx 40 ::glXQueryVersion(m_display,&glxMajor,&glxMinor); 41 if(glxMajor<=0) { 41 if(glxMajor<=0) { 42 m_out << "toolx::X11::session::session : 42 m_out << "toolx::X11::session::session : bad GLX-Version " << glxMajor << "." << glxMinor << std::endl; 43 ::XCloseDisplay(m_display); 43 ::XCloseDisplay(m_display); 44 m_display = 0; 44 m_display = 0; 45 m_vinfo = 0; 45 m_vinfo = 0; 46 m_ctx = 0; 46 m_ctx = 0; 47 return; 47 return; 48 }} 48 }} 49 49 50 static const int atbs_alpha[] = { 50 static const int atbs_alpha[] = { 51 GLX_RGBA, 51 GLX_RGBA, 52 GLX_RED_SIZE, 1, 52 GLX_RED_SIZE, 1, 53 GLX_GREEN_SIZE, 1, 53 GLX_GREEN_SIZE, 1, 54 GLX_BLUE_SIZE, 1, 54 GLX_BLUE_SIZE, 1, 55 GLX_ALPHA_SIZE, 1, 55 GLX_ALPHA_SIZE, 1, 56 GLX_DEPTH_SIZE, 1, 56 GLX_DEPTH_SIZE, 1, 57 GLX_DOUBLEBUFFER, 57 GLX_DOUBLEBUFFER, 58 None}; 58 None}; 59 59 60 //NOTE : macOS : glXChooseVisual leaks 640 60 //NOTE : macOS : glXChooseVisual leaks 640 bytes. 61 m_vinfo = ::glXChooseVisual(m_display,m_mo 61 m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs_alpha); 62 if(!m_vinfo) { 62 if(!m_vinfo) { 63 //m_out << "toolx::X11::session::initial 63 //m_out << "toolx::X11::session::initialize :" 64 // << " can't get a visual with alp 64 // << " can't get a visual with alpha on screen " << m_monitor << ". Try without alpha..." 65 // << std::endl; 65 // << std::endl; 66 //m_out << "toolx::X11::session::initial 66 //m_out << "toolx::X11::session::initialize :" 67 // << " DefaultScreen(m_display): " 67 // << " DefaultScreen(m_display): " << DefaultScreen(m_display) << "." 68 // << std::endl; 68 // << std::endl; 69 69 70 static const int atbs[] = { 70 static const int atbs[] = { 71 GLX_RGBA, 71 GLX_RGBA, 72 GLX_RED_SIZE, 1, 72 GLX_RED_SIZE, 1, 73 GLX_GREEN_SIZE, 1, 73 GLX_GREEN_SIZE, 1, 74 GLX_BLUE_SIZE, 1, 74 GLX_BLUE_SIZE, 1, 75 GLX_DEPTH_SIZE, 1, 75 GLX_DEPTH_SIZE, 1, 76 GLX_DOUBLEBUFFER, 76 GLX_DOUBLEBUFFER, 77 None}; 77 None}; 78 78 79 m_vinfo = ::glXChooseVisual(m_display,m_ 79 m_vinfo = ::glXChooseVisual(m_display,m_monitor,(int*)atbs); 80 if(!m_vinfo) { 80 if(!m_vinfo) { 81 m_out << "toolx::X11::session::session 81 m_out << "toolx::X11::session::session :" 82 << " can't choose a visual on sc 82 << " can't choose a visual on screen " << m_monitor << "." 83 << std::endl; 83 << std::endl; 84 ::XCloseDisplay(m_display); 84 ::XCloseDisplay(m_display); 85 m_display = 0; 85 m_display = 0; 86 m_vinfo = 0; 86 m_vinfo = 0; 87 m_ctx = 0; 87 m_ctx = 0; 88 return; 88 return; 89 } 89 } 90 } 90 } 91 91 92 m_ctx = ::glXCreateContext(m_display,m_vin 92 m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_TRUE); 93 if(!m_ctx) { 93 if(!m_ctx) { 94 m_out << "toolx::X11::session::session : 94 m_out << "toolx::X11::session::session :" 95 << " can't create a glX context wi 95 << " can't create a glX context with direct rendering." 96 << std::endl; 96 << std::endl; 97 m_ctx = ::glXCreateContext(m_display,m_v 97 m_ctx = ::glXCreateContext(m_display,m_vinfo,NULL,GL_FALSE); 98 if(!m_ctx) { 98 if(!m_ctx) { 99 m_out << "toolx::X11::session::session 99 m_out << "toolx::X11::session::session :" 100 << " can't create a glX context. 100 << " can't create a glX context." 101 << std::endl; 101 << std::endl; 102 ::XCloseDisplay(m_display); 102 ::XCloseDisplay(m_display); 103 m_display = 0; 103 m_display = 0; 104 m_vinfo = 0; 104 m_vinfo = 0; 105 m_ctx = 0; 105 m_ctx = 0; 106 return; 106 return; 107 } 107 } 108 //} else { 108 //} else { 109 //m_out << "toolx::X11::session::session 109 //m_out << "toolx::X11::session::session :" 110 // << " glX context with direct ren 110 // << " glX context with direct rendering created." 111 // << std::endl; 111 // << std::endl; 112 } 112 } 113 113 114 // It is better to create a colormap adapt 114 // It is better to create a colormap adapted to the visual. 115 m_colormap = ::XCreateColormap(m_display,: 115 m_colormap = ::XCreateColormap(m_display,::XRootWindow(m_display,m_monitor),m_vinfo->visual,AllocNone); 116 //m_colormap = ::XDefaultColormap(m_displa 116 //m_colormap = ::XDefaultColormap(m_display,m_monitor); 117 if(m_colormap==0L) { 117 if(m_colormap==0L) { 118 m_out << "toolx::X11::session::session : 118 m_out << "toolx::X11::session::session : XCreateColormap failed." << std::endl; 119 ::XCloseDisplay(m_display); 119 ::XCloseDisplay(m_display); 120 m_display = 0; 120 m_display = 0; 121 m_vinfo = 0; 121 m_vinfo = 0; 122 m_ctx = 0; 122 m_ctx = 0; 123 return; 123 return; 124 } 124 } 125 } 125 } 126 virtual ~session() { 126 virtual ~session() { 127 if(m_display) { 127 if(m_display) { 128 if(m_ctx) { 128 if(m_ctx) { 129 ::glXDestroyContext(m_display,m_ctx); 129 ::glXDestroyContext(m_display,m_ctx); 130 m_ctx = 0; 130 m_ctx = 0; 131 } 131 } 132 if(m_colormap) { 132 if(m_colormap) { 133 ::XFreeColormap(m_display,m_colormap); 133 ::XFreeColormap(m_display,m_colormap); 134 m_colormap = 0; 134 m_colormap = 0; 135 } 135 } 136 ::XCloseDisplay(m_display); 136 ::XCloseDisplay(m_display); 137 m_display = 0; 137 m_display = 0; 138 } 138 } 139 if(m_vinfo) { 139 if(m_vinfo) { 140 ::XFree(m_vinfo); 140 ::XFree(m_vinfo); 141 m_vinfo = 0; 141 m_vinfo = 0; 142 } 142 } 143 //std::cout << "debug : ~session" << std:: 143 //std::cout << "debug : ~session" << std::endl; 144 } 144 } 145 protected: 145 protected: 146 session(const session& a_from) 146 session(const session& a_from) 147 :parent(a_from) 147 :parent(a_from) 148 ,m_vinfo(0) 148 ,m_vinfo(0) 149 ,m_ctx(0) 149 ,m_ctx(0) 150 ,m_colormap(0) 150 ,m_colormap(0) 151 {} 151 {} 152 session& operator=(const session& a_from){ 152 session& operator=(const session& a_from){ 153 if(&a_from==this) return *this; 153 if(&a_from==this) return *this; 154 parent::operator=(a_from); 154 parent::operator=(a_from); 155 return *this; 155 return *this; 156 } 156 } 157 public: 157 public: 158 GLXContext context() const {return m_ctx;} 158 GLXContext context() const {return m_ctx;} 159 159 160 Window create_window(const char* a_title,int 160 Window create_window(const char* a_title,int a_x,int a_y,unsigned int a_width,unsigned int a_height) { 161 if(!m_display) return 0L; 161 if(!m_display) return 0L; 162 162 163 XSetWindowAttributes swa; 163 XSetWindowAttributes swa; 164 swa.event_mask = StructureNotifyMask | Exp 164 swa.event_mask = StructureNotifyMask | ExposureMask 165 | ButtonPressMask | ButtonReleaseMask | 165 | ButtonPressMask | ButtonReleaseMask | ButtonMotionMask 166 | PointerMotionMask 166 | PointerMotionMask 167 | KeyPressMask; 167 | KeyPressMask; 168 168 169 swa.colormap = m_colormap; 169 swa.colormap = m_colormap; 170 swa.border_pixel = 0L; 170 swa.border_pixel = 0L; 171 171 172 Window window = ::XCreateWindow(m_display, 172 Window window = ::XCreateWindow(m_display, 173 ::XRootWin 173 ::XRootWindow(m_display,m_monitor), 174 a_x,a_y,a_ 174 a_x,a_y,a_width,a_height, 175 0, 175 0, 176 m_vinfo->d 176 m_vinfo->depth, 177 InputOutpu 177 InputOutput, 178 m_vinfo->v 178 m_vinfo->visual, 179 CWBorderPixel|CWColormap|CWEventMa 179 CWBorderPixel|CWColormap|CWEventMask,&swa); 180 180 181 if(window==0L) { 181 if(window==0L) { 182 m_out << "toolx::X11::session::create_wi 182 m_out << "toolx::X11::session::create_window :" 183 << " can't create a X11 window." 183 << " can't create a X11 window." 184 << std::endl; 184 << std::endl; 185 return 0L; 185 return 0L; 186 } 186 } 187 187 188 XTextProperty tp; 188 XTextProperty tp; 189 ::XStringListToTextProperty((char**)&a_tit 189 ::XStringListToTextProperty((char**)&a_title,1,&tp); 190 XSizeHints sh; 190 XSizeHints sh; 191 sh.flags = USPosition | USSize; 191 sh.flags = USPosition | USSize; 192 ::XSetWMProperties(m_display,window,&tp,&t 192 ::XSetWMProperties(m_display,window,&tp,&tp,0,0,&sh,0,0); 193 ::XFree(tp.value); 193 ::XFree(tp.value); 194 194 195 ::XSetWMProtocols(m_display,window,&m_WM_D 195 ::XSetWMProtocols(m_display,window,&m_WM_DELETE_WINDOW_atom,1); 196 return window; 196 return window; 197 } 197 } 198 protected: 198 protected: 199 XVisualInfo* m_vinfo; 199 XVisualInfo* m_vinfo; 200 GLXContext m_ctx; 200 GLXContext m_ctx; 201 Colormap m_colormap; 201 Colormap m_colormap; 202 }; 202 }; 203 203 204 }} 204 }} 205 205 206 #endif 206 #endif 207 207