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 10.5.p1 ] [ 10.6 ] [ 10.6.p1 ] [ 10.6.p2 ] [ 10.6.p3 ] [ 10.7 ] [ 10.7.p1 ] [ 10.7.p2 ] [ 10.7.p3 ] [ 10.7.p4 ] [ 11.0 ] [ 11.0.p1 ] [ 11.0.p2 ] [ 11.0.p3, ] [ 11.0.p4 ] [ 11.1 ] [ 11.1.1 ] [ 11.1.2 ] [ 11.1.3 ] [ 11.2 ] [ 11.2.1 ] [ 11.2.2 ] [ 11.3.0 ]

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