Subversion Repositories seema-scanner

Rev

Rev 1 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 1 Rev 16
1
#include "OpenGLContext.h"
1
#include "OpenGLContext.h"
2
#include <GLFW/glfw3.h>
2
#include <GLFW/glfw3.h>
3
 
3
 
-
 
4
#include <stdio.h>
4
#ifdef __LINUX__
5
#ifdef __unix__
5
    #define GLFW_EXPOSE_NATIVE_X11
6
    #define GLFW_EXPOSE_NATIVE_X11
6
    #define GLFW_EXPOSE_NATIVE_GLX
7
    #define GLFW_EXPOSE_NATIVE_GLX
7
    #include <cstring>
8
    #include <cstring>
-
 
9
    #include <X11/X.h>
-
 
10
 typedef XID RRCrtc;
8
    #include <GLFW/glfw3native.h>
11
    #include <GLFW/glfw3native.h>
9
#endif
12
#endif
10
 
13
 
11
struct OpenGLContext::OpenGLContextInfo{
14
struct OpenGLContext::OpenGLContextInfo{
12
    GLFWmonitor* monitor;
15
    GLFWmonitor* monitor;
13
    GLFWwindow* window;
16
    GLFWwindow* window;
14
    OpenGLContextInfo() : monitor(NULL), window(NULL){}
17
    OpenGLContextInfo() : monitor(NULL), window(NULL){}
15
};
18
};
16
 
19
 
17
void error_callback(int error, const char* description){
20
void error_callback(int error, const char* description){
18
    std::cerr << "GLFW error code " << error << " " << description << std::endl;
21
    std::cerr << "GLFW error code " << error << " " << description << std::endl;
19
}
22
}
20
 
23
 
21
 
24
 
22
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
25
std::vector<ScreenInfo> OpenGLContext::GetScreenInfo(){
23
    std::vector<ScreenInfo> ret;
26
    std::vector<ScreenInfo> ret;
24
 
27
 
25
    if (!glfwInit())
28
    if (!glfwInit())
26
        std::cerr << "Could not initialize GLFW!" << std::endl;
29
        std::cerr << "Could not initialize GLFW!" << std::endl;
27
    glfwSetErrorCallback(error_callback);
30
    glfwSetErrorCallback(error_callback);
28
 
31
 
29
    int nScreens = 0;
32
    int nScreens = 0;
30
    GLFWmonitor **monitors;
33
    GLFWmonitor **monitors;
31
    monitors = glfwGetMonitors(&nScreens);
34
    monitors = glfwGetMonitors(&nScreens);
32
 
35
 
33
    for(int i=0; i<nScreens; i++){
36
    for(int i=0; i<nScreens; i++){
34
 
37
 
35
        GLFWmonitor *monitor = monitors[i];
38
        GLFWmonitor *monitor = monitors[i];
36
        const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);
39
        const GLFWvidmode *videoMode = glfwGetVideoMode(monitor);
37
 
40
 
38
        ScreenInfo screen;
41
        ScreenInfo screen;
39
        screen.resX = videoMode->width;
42
        screen.resX = videoMode->width;
40
        screen.resY = videoMode->height;
43
        screen.resY = videoMode->height;
41
        const char *screenName = glfwGetMonitorName(monitor);
44
        const char *screenName = glfwGetMonitorName(monitor);
42
        screen.name = screenName;
45
        screen.name = screenName;
43
 
46
 
44
        ret.push_back(screen);
47
        ret.push_back(screen);
45
    }
48
    }
46
 
49
 
47
    //glfwTerminate();
50
    //glfwTerminate();
48
 
51
 
49
    return ret;
52
    return ret;
50
}
53
}
51
 
54
 
52
OpenGLContext::OpenGLContext(uint _screenNum) : screenNum(_screenNum), screenResX(0), screenResY(0){
55
OpenGLContext::OpenGLContext(uint _screenNum) : screenNum(_screenNum), screenResX(0), screenResY(0){
53
 
56
 
54
    contextInfo = new OpenGLContextInfo();
57
    contextInfo = new OpenGLContextInfo();
55
 
58
 
56
    if (!glfwInit())
59
    if (!glfwInit())
57
        std::cerr << "Could not initialize GLFW!" << std::endl;
60
        std::cerr << "Could not initialize GLFW!" << std::endl;
58
    glfwSetErrorCallback(error_callback);
61
    glfwSetErrorCallback(error_callback);
59
 
62
 
60
    int nScreens = 0;
63
    int nScreens = 0;
61
    GLFWmonitor **monitors;
64
    GLFWmonitor **monitors;
62
    monitors = glfwGetMonitors(&nScreens);
65
    monitors = glfwGetMonitors(&nScreens);
63
 
66
 
64
    if((unsigned int)nScreens < screenNum+1)
67
    if((unsigned int)nScreens < screenNum+1)
65
        std::cerr << "Could not open screen " << screenNum << std::endl;
68
        std::cerr << "Could not open screen " << screenNum << std::endl;
66
 
69
 
67
    contextInfo->monitor = monitors[screenNum];
70
    contextInfo->monitor = monitors[screenNum];
68
    const GLFWvidmode *videoMode = glfwGetVideoMode(contextInfo->monitor);
71
    const GLFWvidmode *videoMode = glfwGetVideoMode(contextInfo->monitor);
69
 
72
 
70
    screenResX = videoMode->width;
73
    screenResX = videoMode->width;
71
    screenResY = videoMode->height;
74
    screenResY = videoMode->height;
72
 
75
 
-
 
76
    // Set window hints
-
 
77
    glfwDefaultWindowHints();
-
 
78
    // highest available refresh rate
-
 
79
    glfwWindowHint(GLFW_REFRESH_RATE, 0);
-
 
80
 
73
    // Create fullscreen window
81
    // Create fullscreen window
74
    contextInfo->window = glfwCreateWindow(screenResX, screenResY, "GLFW OpenGL Context", contextInfo->monitor, NULL);
82
    contextInfo->window = glfwCreateWindow(screenResX, screenResY, "GLFW OpenGL Context", contextInfo->monitor, NULL);
75
 
83
 
-
 
84
    // make cursor invisible
-
 
85
    glfwSetInputMode(contextInfo->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
-
 
86
 
76
#ifdef __unix__
87
#ifdef a__unix__
77
    Display *display = glfwGetX11Display();
88
    Display *display = glfwGetX11Display();
78
    Window window = glfwGetX11Window(contextInfo->window);
89
    Window window = glfwGetX11Window(contextInfo->window);
79
 
90
 
-
 
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
      }
80
//    // Set swap interval to 1 for standard vsync
137
//    // Set swap interval to 1 for standard vsync
81
//    typedef GLvoid (*glXSwapIntervalSGIFunc) (GLint);
138
//    typedef GLvoid (*glXSwapIntervalSGIFunc) (GLint);
82
//    const char *glx_extensions = glXQueryExtensionsString(display, screenNum);
139
//    const char *glx_extensions = glXQueryExtensionsString(display, screenNum);
83
//    if (strstr(glx_extensions, "GLX_SGI_swap_control")) {
140
//    if (strstr(glx_extensions, "GLX_SGI_swap_control")) {
84
//        PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
141
//        PFNGLXSWAPINTERVALSGIPROC SwapIntervalSGI = (PFNGLXSWAPINTERVALSGIPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalSGI");
85
//        SwapIntervalSGI(1);
142
//        SwapIntervalSGI(1);
86
//    } else if (strstr(glx_extensions, "GLX_EXT_swap_control")) {
143
//    } else if (strstr(glx_extensions, "GLX_EXT_swap_control")) {
87
        PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
144
//        PFNGLXSWAPINTERVALEXTPROC SwapIntervalEXT = (PFNGLXSWAPINTERVALEXTPROC)glXGetProcAddressARB((const GLubyte*)"glXSwapIntervalEXT");
88
        SwapIntervalEXT(display, window, 1);
145
//        SwapIntervalEXT(display, window, 1);
89
//    } else {
146
//    } else {
90
//        std::cerr << "OpenGLContext.Unix Error: Could not access swap interval extension!" << std::endl;
147
//        std::cerr << "OpenGLContext.Unix Error: Could not access swap interval extension!" << std::endl;
91
//    }
148
//    }
92
#endif
149
#endif
93
 
150
 
94
    glfwMakeContextCurrent(contextInfo->window);
151
    glfwMakeContextCurrent(contextInfo->window);
-
 
152
 
95
    // Set vsync (swap interval 1)
153
    // Set vsync (swap interval 1)
96
    glfwSwapInterval(1);
154
    glfwSwapInterval(1);
97
 
155
 
98
    // Adjust gamma to one
156
    // Adjust gamma to one
99
    this->setGamma(1.0);
157
    this->setGamma(1.0);
100
}
158
}
101
 
159
 
102
void OpenGLContext::setGamma(float gamma){
160
void OpenGLContext::setGamma(float gamma){
103
    glfwSetGamma(contextInfo->monitor, gamma);
161
    glfwSetGamma(contextInfo->monitor, gamma);
104
}
162
}
105
 
163
 
106
void OpenGLContext::makeContextCurrent(){
164
void OpenGLContext::makeContextCurrent(){
107
    glfwMakeContextCurrent(contextInfo->window);
165
    glfwMakeContextCurrent(contextInfo->window);
108
}
166
}
109
 
167
 
110
void OpenGLContext::flush(){
168
void OpenGLContext::flush(){
111
 
169
 
112
    // Swap buffers
170
    // Swap buffers
113
    glfwSwapBuffers(contextInfo->window);
171
    glfwSwapBuffers(contextInfo->window);
114
 
172
 
115
    // Poll events in order not to lock up
173
    // Poll events in order not to lock up
116
    glfwPollEvents();
174
    glfwPollEvents();
117
    //glfwWaitEvents();
175
    //glfwWaitEvents();
118
    glFinish();
176
//    glFinish();
119
}
177
}
120
 
178
 
121
OpenGLContext::~OpenGLContext(){
179
OpenGLContext::~OpenGLContext(){
122
 
180
 
123
    glfwDestroyWindow(contextInfo->window);
181
    glfwDestroyWindow(contextInfo->window);
124
    glfwTerminate();
182
    glfwTerminate();
125
 
183
 
126
    delete contextInfo;
184
    delete contextInfo;
127
}
185
}
128
 
186
 
129
 
187