Subversion Repositories seema-scanner

Rev

Rev 1 | Blame | Compare with Previous | Last modification | View Log | RSS feed

#include "OpenGLContext.h"
#include <GLFW/glfw3.h>

#include <stdio.h>
#ifdef __unix__
    #define GLFW_EXPOSE_NATIVE_X11
    #define GLFW_EXPOSE_NATIVE_GLX
    #include <cstring>
    #include <X11/X.h>
 typedef XID RRCrtc;
    #include <GLFW/glfw3native.h>
#endif

struct OpenGLContext::OpenGLContextInfo{
    GLFWmonitor* monitor;
    GLFWwindow* window;
    OpenGLContextInfo() : monitor(NULL), window(NULL){}
};

void error_callback(int error, const char* description){
    std::cerr << "GLFW error code " << error << " " << description << std::endl;
}


std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
    std::vector<ScreenInfo> ret;

    if (!glfwInit())
        std::cerr << "Could not initialize GLFW!" << std::endl;
    glfwSetErrorCallback(error_callback);

    int nScreens = 0;
    GLFWmonitor **monitors;
    monitors = glfwGetMonitors(&nScreens);

    for(int i=0; i<nScreens; i++){

        GLFWmonitor *monitor = monitors[i];
        const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);

        ScreenInfo screen;
        screen.resX = videoMode->width;
        screen.resY = videoMode->height;
        const char *screenName = glfwGetMonitorName(monitor);
        screen.name = screenName;

        ret.push_back(screen);
    }

    //glfwTerminate();

    return ret;
}

OpenGLContext::OpenGLContext(uint _screenNum) : screenNum(_screenNum), screenResX(0), screenResY(0){

    contextInfo = new OpenGLContextInfo();

    if (!glfwInit())
        std::cerr << "Could not initialize GLFW!" << std::endl;
    glfwSetErrorCallback(error_callback);

    int nScreens = 0;
    GLFWmonitor **monitors;
    monitors = glfwGetMonitors(&nScreens);

    if((unsigned int)nScreens < screenNum+1)
        std::cerr << "Could not open screen " << screenNum << std::endl;

    contextInfo->monitor = monitors[screenNum];
    const GLFWvidmode *videoMode = glfwGetVideoMode(contextInfo->monitor);

    screenResX = videoMode->width;
    screenResY = videoMode->height;

    // Set window hints
    glfwDefaultWindowHints();
    // highest available refresh rate
    glfwWindowHint(GLFW_REFRESH_RATE, 0);

    // Create fullscreen window
    contextInfo->window = glfwCreateWindow(screenResX, screenResY, "GLFW OpenGL Context", contextInfo->monitor, NULL);

    // make cursor invisible
    glfwSetInputMode(contextInfo->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);

#ifdef a__unix__
    Display *display = glfwGetX11Display();
    Window window = glfwGetX11Window(contextInfo->window);

    Atom wmStateAbove = XInternAtom( display, "_NET_WM_STATE_ABOVE", 1 );
    if( wmStateAbove != None ) {
        printf( "_NET_WM_STATE_ABOVE has atom of %ld\n", (long)wmStateAbove );
    } else {
        printf( "ERROR: cannot find atom for _NET_WM_STATE_ABOVE !\n" );
    }

    Atom wmNetWmState = XInternAtom( display, "_NET_WM_STATE", 1 );
    if( wmNetWmState != None ) {
        printf( "_NET_WM_STATE has atom of %ld\n", (long)wmNetWmState );
    } else {
        printf( "ERROR: cannot find atom for _NET_WM_STATE !\n" );
    }
    // set window always on top hint
    if( wmStateAbove != None ) {
        XClientMessageEvent xclient;
        memset( &xclient, 0, sizeof (xclient) );
        //
        //window  = the respective client window
        //message_type = _NET_WM_STATE
        //format = 32
        //data.l[0] = the action, as listed below
        //data.l[1] = first property to alter
        //data.l[2] = second property to alter
        //data.l[3] = source indication (0-unk,1-normal app,2-pager)
        //other data.l[] elements = 0
        //
        xclient.type = ClientMessage;
        xclient.window = window; // GDK_WINDOW_XID(window);
        xclient.message_type = wmNetWmState; //gdk_x11_get_xatom_by_name_for_display( display, "_NET_WM_STATE" );
        xclient.format = 32;
        xclient.data.l[0] = 1; // add the state
        xclient.data.l[1] = wmStateAbove; //gdk_x11_atom_to_xatom_for_display (display, state1);
        xclient.data.l[2] = 0; //gdk_x11_atom_to_xatom_for_display (display, state2);
        xclient.data.l[3] = 0;
        xclient.data.l[4] = 0;
        //gdk_wmspec_change_state( FALSE, window,
        //  gdk_atom_intern_static_string ("_NET_WM_STATE_BELOW"),
        //  GDK_NONE );
        XSendEvent( display,
          //mywin - wrong, not app window, send to root window!
          DefaultRootWindow(display),
          False,
          SubstructureRedirectMask | SubstructureNotifyMask,
          (XEvent *)&xclient );
      }
//    // Set swap interval to 1 for standard vsync
//    typedef GLvoid (*glXSwapIntervalSGIFunc) (GLint);
//    const char *glx_extensions = glXQueryExtensionsString(display, screenNum);
//    if (strstr(glx_extensions, "GLX_SGI_swap_control")) {
//        PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
//        SwapIntervalSGI(1);
//    } else if (strstr(glx_extensions, "GLX_EXT_swap_control")) {
//        PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
//        SwapIntervalEXT(display, window, 1);
//    } else {
//        std::cerr << "OpenGLContext.Unix Error: Could not access swap interval extension!" << std::endl;
//    }
#endif

    glfwMakeContextCurrent(contextInfo->window);

    // Set vsync (swap interval 1)
    glfwSwapInterval(1);

    // Adjust gamma to one
    this->setGamma(1.0);
}

void OpenGLContext::setGamma(float gamma){
    glfwSetGamma(contextInfo->monitor, gamma);
}

void OpenGLContext::makeContextCurrent(){
    glfwMakeContextCurrent(contextInfo->window);
}

void OpenGLContext::flush(){

    // Swap buffers
    glfwSwapBuffers(contextInfo->window);

    // Poll events in order not to lock up
    glfwPollEvents();
    //glfwWaitEvents();
//    glFinish();
}

OpenGLContext::~OpenGLContext(){

    glfwDestroyWindow(contextInfo->window);
    glfwTerminate();

    delete contextInfo;
}