I desperately need access to some of fltk's native handles so that I may 
fashion proper cooperation between fltk and various other libraries that I am 
using.

I am attempting to initialize the handles I need myself in the manner that is 
described in the documentation here:

http://www.fltk.org/doc-1.3/index.html

----------- Quote ----------

"Changing the Display, Screen, or X Visual
FLTK uses only a single display, screen, X visual, and X colormap. This greatly 
simplifies its internal structure and makes it much smaller and faster. You can 
change which it uses by setting global variables before the first 
Fl_Window::show() is called. You may also want to call Fl::visual(), which is a 
portable interface to get a full color and/or double buffered visual."

-----------------------------

In the constructor of my display window I attempt to initialize the display, 
screen, visual and colormap.  I normally do this before any windows are shown 
and only once.  However, the code below doesn't do this and only provides a 
means to reproduce the following error which is causing much wailing and 
gnashing of teeth:

----------- Error ----------

X Error of failed request:  BadAtom (invalid Atom parameter)
  Major opcode of failed request:  18 (X_ChangeProperty)
  Atom id in failed request:  0x0
  Serial number of failed request:  21
  Current serial number in output stream:  32
Press [Enter] to close the terminal ...

-----------------------------

----------- Code ----------

#include <fltk/run.h>
#include <fltk/GlWindow.h>
#include <iostream>
#include <string>

#include <GL/glx.h>
#define XWindow ::Window
# include <fltk/x.h>
//
#define GLX_CONTEXT_MAJOR_VERSION_ARB       0x2091
#define GLX_CONTEXT_MINOR_VERSION_ARB       0x2092
typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, 
GLXContext, Bool, const int*);

void Terminate(const std::string &message)
{
        throw message;
};

struct OglDisplayWindow : public fltk::GlWindow
{
        OglDisplayWindow(
                int X,
                int Y,
                int W,
                int H,
                const char *L);

        virtual ~OglDisplayWindow();

        virtual void draw();

        virtual int handle(int);

        typedef GLXContext OGLCONTEXT;
private:
        static Display *_disp_ptr;
        static XVisualInfo *_vis_ptr;
        static GLXFBConfig *_fbconfig_ptr;
        static int _fbconfigID;
        static int _screenID;
        static Colormap _colorMap;
};

Display* OglDisplayWindow::_disp_ptr = 0;
XVisualInfo* OglDisplayWindow::_vis_ptr = 0;
GLXFBConfig* OglDisplayWindow::_fbconfig_ptr = 0;
int OglDisplayWindow::_fbconfigID = 0;
int OglDisplayWindow::_screenID = 0;
Colormap OglDisplayWindow::_colorMap = Colormap();

OglDisplayWindow::OglDisplayWindow(
        int X,
        int Y,
        int W,
        int H,
        const char *L) :
        fltk::GlWindow(X,Y,W,H,L)
{
        struct Utility
        {
                static void InitOsNativeResources()
                {
                        //Interacting with the X library
                        
//////////////////////////////////////////////////////////////////////////
                        _disp_ptr = XOpenDisplay(0);

                        if (!_disp_ptr)
                        {
                                Terminate("OglDisplayWindow::draw - Failed to 
open X display.\n");
                        }//end if

                        _screenID = DefaultScreen(_disp_ptr);

                        // Get a matching FB config
                        static int visual_attribs[] = {
                                GLX_X_RENDERABLE,True,
                                GLX_DRAWABLE_TYPE,GLX_WINDOW_BIT,
                                GLX_RENDER_TYPE,GLX_RGBA_BIT,
                                GLX_X_VISUAL_TYPE,GLX_TRUE_COLOR,
                                GLX_RED_SIZE,8,
                                GLX_GREEN_SIZE,8,
                                GLX_BLUE_SIZE,8,
                                GLX_ALPHA_SIZE,8,
                                GLX_DEPTH_SIZE,24,
                                GLX_STENCIL_SIZE,8,
                                GLX_DOUBLEBUFFER,True,
                                //GLX_SAMPLE_BUFFERS  , 1,
                                //GLX_SAMPLES         , 4,
                                None
                        };

                        //Getting matching framebuffer configs
                        int fbcount;
                        _fbconfig_ptr = glXChooseFBConfig(
                                _disp_ptr,
                                _screenID,
                                visual_attribs,
                                &fbcount);

                        if (!_fbconfig_ptr)
                        {
                                Terminate("OglDisplayWindow::draw - Failed to 
retrieve a framebuffer config.\n");
                        }//end if

                        //"Found matching FB configs

                        // Pick the FB config/visual with the most samples per 
pixel

                        //Getting XVisualInfo
                        int best_fbc = -1;
                        int worst_fbc = -1;
                        int best_num_samp = -1;
                        int worst_num_samp = 999;

                        int samp_buf;
                        int samples;
                        for (int i(0);i < fbcount;++i)
                        {
                                _vis_ptr = glXGetVisualFromFBConfig(
                                        _disp_ptr,
                                        _fbconfig_ptr[i]);

                                if (_vis_ptr)
                                {
                                        samp_buf = 0;
                                        samples = 0;
                                        glXGetFBConfigAttrib(
                                                _disp_ptr,
                                                _fbconfig_ptr[i],
                                                GLX_SAMPLE_BUFFERS,
                                                &samp_buf);

                                        glXGetFBConfigAttrib(
                                                _disp_ptr,
                                                _fbconfig_ptr[i],
                                                GLX_SAMPLES,
                                                &samples);

                                        if ((best_fbc < 0 || samp_buf) && 
samples > best_num_samp)
                                        {
                                                best_fbc = i;
                                                best_num_samp = samples;
                                        }//end if
                                        if (worst_fbc < 0 || !samp_buf || 
samples < worst_num_samp)
                                        {
                                                worst_fbc = i;
                                                worst_num_samp = samples;
                                        }//end if
                                }//end if

                                XFree(_vis_ptr);
                        }//end for

                        //Use the best matching visual
                        _fbconfigID = best_fbc;

                        _vis_ptr = glXGetVisualFromFBConfig(
                                _disp_ptr,
                                _fbconfig_ptr[_fbconfigID]);

                        //Creating colormap
                        _colorMap = XCreateColormap(
                                _disp_ptr,
                                RootWindow(_disp_ptr,_vis_ptr->screen),
                                _vis_ptr->visual,
                                AllocNone);

                        //Interacting with FLTK library
                        
//////////////////////////////////////////////////////////////////////////
                        fltk::xdisplay = _disp_ptr;
                        fltk::xscreen = _screenID;
                        fltk::xvisual = _vis_ptr;
                        fltk::xcolormap = _colorMap;

                        std::cout<<"Os handles initialized."<<std::endl;
                }//end function
        };

        Utility::InitOsNativeResources();
}//end function

OglDisplayWindow::~OglDisplayWindow()
{
        XFree(_fbconfig_ptr); //I am assuming fltk will free the other 
resources after I pass them to the library...wrong?
}//end function

void OglDisplayWindow::draw()
{
        if (!this->valid())
        {
                this->make_current();
        }//end if
}//end function

int OglDisplayWindow::handle(int msg)
{
        return fltk::GlWindow::handle(msg);
}//end function

int main(void)
{
        try
        {
                OglDisplayWindow displayWin(
                        100,
                        100,
                        100,
                        100,
                        "Debug Code");

                displayWin.show();
                while (displayWin.visible())
                {
                        fltk::check();
                        displayWin.flush();
                }//end while
        }
        catch (const std::string &message)
        {
                std::cerr << message << std::endl;
        }//end try
}//end function
_______________________________________________
fltk mailing list
[email protected]
http://lists.easysw.com/mailman/listinfo/fltk

Reply via email to