Subversion Repositories seema-scanner

Rev

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

Rev Author Line No. Line
1 jakw 1
#include "OpenGLContext.h"
2
#include <GLFW/glfw3.h>
3
 
16 jakw 4
#include <stdio.h>
5
#ifdef __unix__
1 jakw 6
    #define GLFW_EXPOSE_NATIVE_X11
7
    #define GLFW_EXPOSE_NATIVE_GLX
8
    #include <cstring>
16 jakw 9
    #include <X11/X.h>
10
 typedef XID RRCrtc;
1 jakw 11
    #include <GLFW/glfw3native.h>
12
#endif
13
 
14
struct OpenGLContext::OpenGLContextInfo{
15
    GLFWmonitor* monitor;
16
    GLFWwindow* window;
17
    OpenGLContextInfo() : monitor(NULL), window(NULL){}
18
};
19
 
20
void error_callback(int error, const char* description){
21
    std::cerr << "GLFW error code " << error << " " << description << std::endl;
22
}
23
 
24
 
25
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
26
    std::vector<ScreenInfo> ret;
27
 
28
    if (!glfwInit())
29
        std::cerr << "Could not initialize GLFW!" << std::endl;
30
    glfwSetErrorCallback(error_callback);
31
 
32
    int nScreens = 0;
33
    GLFWmonitor **monitors;
34
    monitors = glfwGetMonitors(&nScreens);
35
 
36
    for(int i=0; i<nScreens; i++){
37
 
38
        GLFWmonitor *monitor = monitors[i];
39
        const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);
40
 
41
        ScreenInfo screen;
42
        screen.resX = videoMode->width;
43
        screen.resY = videoMode->height;
44
        const char *screenName = glfwGetMonitorName(monitor);
45
        screen.name = screenName;
46
 
47
        ret.push_back(screen);
48
    }
49
 
50
    //glfwTerminate();
51
 
52
    return ret;
53
}
54
 
55
OpenGLContext::OpenGLContext(uint _screenNum) : screenNum(_screenNum), screenResX(0), screenResY(0){
56
 
57
    contextInfo = new OpenGLContextInfo();
58
 
59
    if (!glfwInit())
60
        std::cerr << "Could not initialize GLFW!" << std::endl;
61
    glfwSetErrorCallback(error_callback);
62
 
63
    int nScreens = 0;
64
    GLFWmonitor **monitors;
65
    monitors = glfwGetMonitors(&nScreens);
66
 
67
    if((unsigned int)nScreens < screenNum+1)
68
        std::cerr << "Could not open screen " << screenNum << std::endl;
69
 
70
    contextInfo->monitor = monitors[screenNum];
71
    const GLFWvidmode *videoMode = glfwGetVideoMode(contextInfo->monitor);
72
 
73
    screenResX = videoMode->width;
74
    screenResY = videoMode->height;
75
 
16 jakw 76
    // Set window hints
77
    glfwDefaultWindowHints();
78
    // highest available refresh rate
79
    glfwWindowHint(GLFW_REFRESH_RATE, 0);
80
 
1 jakw 81
    // Create fullscreen window
82
    contextInfo->window = glfwCreateWindow(screenResX, screenResY, "GLFW OpenGL Context", contextInfo->monitor, NULL);
83
 
16 jakw 84
    // make cursor invisible
85
    glfwSetInputMode(contextInfo->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
86
 
87
#ifdef a__unix__
1 jakw 88
    Display *display = glfwGetX11Display();
89
    Window window = glfwGetX11Window(contextInfo->window);
90
 
16 jakw 91
    Atom wmStateAbove = XInternAtom( display, "_NET_WM_STATE_ABOVE", 1 );
92
    if( wmStateAbove != None ) {
93
        printf( "_NET_WM_STATE_ABOVE has atom of %ld\n", (long)wmStateAbove );
94
    } else {
95
        printf( "ERROR: cannot find atom for _NET_WM_STATE_ABOVE !\n" );
96
    }
97
 
98
    Atom wmNetWmState = XInternAtom( display, "_NET_WM_STATE", 1 );
99
    if( wmNetWmState != None ) {
100
        printf( "_NET_WM_STATE has atom of %ld\n", (long)wmNetWmState );
101
    } else {
102
        printf( "ERROR: cannot find atom for _NET_WM_STATE !\n" );
103
    }
104
    // set window always on top hint
105
    if( wmStateAbove != None ) {
106
        XClientMessageEvent xclient;
107
        memset( &xclient, 0, sizeof (xclient) );
108
        //
109
        //window  = the respective client window
110
        //message_type = _NET_WM_STATE
111
        //format = 32
112
        //data.l[0] = the action, as listed below
113
        //data.l[1] = first property to alter
114
        //data.l[2] = second property to alter
115
        //data.l[3] = source indication (0-unk,1-normal app,2-pager)
116
        //other data.l[] elements = 0
117
        //
118
        xclient.type = ClientMessage;
119
        xclient.window = window; // GDK_WINDOW_XID(window);
120
        xclient.message_type = wmNetWmState; //gdk_x11_get_xatom_by_name_for_display( display, "_NET_WM_STATE" );
121
        xclient.format = 32;
122
        xclient.data.l[0] = 1; // add the state
123
        xclient.data.l[1] = wmStateAbove; //gdk_x11_atom_to_xatom_for_display (display, state1);
124
        xclient.data.l[2] = 0; //gdk_x11_atom_to_xatom_for_display (display, state2);
125
        xclient.data.l[3] = 0;
126
        xclient.data.l[4] = 0;
127
        //gdk_wmspec_change_state( FALSE, window,
128
        //  gdk_atom_intern_static_string ("_NET_WM_STATE_BELOW"),
129
        //  GDK_NONE );
130
        XSendEvent( display,
131
          //mywin - wrong, not app window, send to root window!
132
          DefaultRootWindow(display),
133
          False,
134
          SubstructureRedirectMask | SubstructureNotifyMask,
135
          (XEvent *)&xclient );
136
      }
1 jakw 137
//    // Set swap interval to 1 for standard vsync
138
//    typedef GLvoid (*glXSwapIntervalSGIFunc) (GLint);
139
//    const char *glx_extensions = glXQueryExtensionsString(display, screenNum);
140
//    if (strstr(glx_extensions, "GLX_SGI_swap_control")) {
141
//        PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
142
//        SwapIntervalSGI(1);
143
//    } else if (strstr(glx_extensions, "GLX_EXT_swap_control")) {
16 jakw 144
//        PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
145
//        SwapIntervalEXT(display, window, 1);
1 jakw 146
//    } else {
147
//        std::cerr << "OpenGLContext.Unix Error: Could not access swap interval extension!" << std::endl;
148
//    }
149
#endif
150
 
151
    glfwMakeContextCurrent(contextInfo->window);
16 jakw 152
 
1 jakw 153
    // Set vsync (swap interval 1)
154
    glfwSwapInterval(1);
155
 
156
    // Adjust gamma to one
157
    this->setGamma(1.0);
158
}
159
 
160
void OpenGLContext::setGamma(float gamma){
161
    glfwSetGamma(contextInfo->monitor, gamma);
162
}
163
 
164
void OpenGLContext::makeContextCurrent(){
165
    glfwMakeContextCurrent(contextInfo->window);
166
}
167
 
168
void OpenGLContext::flush(){
169
 
170
    // Swap buffers
171
    glfwSwapBuffers(contextInfo->window);
172
 
173
    // Poll events in order not to lock up
174
    glfwPollEvents();
175
    //glfwWaitEvents();
16 jakw 176
//    glFinish();
1 jakw 177
}
178
 
179
OpenGLContext::~OpenGLContext(){
180
 
181
    glfwDestroyWindow(contextInfo->window);
182
    glfwTerminate();
183
 
184
    delete contextInfo;
185
}
186