Browse Source

Now slop will just fallback to the xshape extension if opengl fails.

naelstrof 7 years ago
parent
commit
5fbbd48916
14 changed files with 563 additions and 574 deletions
  1. 42
    71
      CMakeLists.txt
  2. 246
    0
      src/glrectangle.cpp
  3. 66
    0
      src/glrectangle.hpp
  4. 0
    116
      src/glslop.cpp
  5. 0
    4
      src/main.cpp
  6. 5
    238
      src/rectangle.cpp
  7. 6
    36
      src/rectangle.hpp
  8. 169
    0
      src/slop.cpp
  9. 13
    0
      src/slop.hpp
  10. 2
    2
      src/slopstates.cpp
  11. 2
    2
      src/slopstates.hpp
  12. 8
    8
      src/xshaperectangle.cpp
  13. 4
    3
      src/xshaperectangle.hpp
  14. 0
    94
      src/xslop.cpp

+ 42
- 71
CMakeLists.txt View File

5
     set(CMAKE_INSTALL_PREFIX "/usr")
5
     set(CMAKE_INSTALL_PREFIX "/usr")
6
 endif()
6
 endif()
7
 
7
 
8
-if ( NOT SLOP_LEGACY_MODE )
9
-    set(SLOP_LEGACY_MODE FALSE CACHE BOOL "Legacy mode requires the XShape extension, is laggy, isn't guaranteed to be invisible on shutdown, isn't guaranteed to support opacity, doesn't support custom shaders, and requires C++11 for the sleep_for functions. Though it still might be desired since it doesn't require OpenGL to use. Your choice <:o)")
10
-endif()
11
-
12
 project(slop)
8
 project(slop)
13
 
9
 
14
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/")
10
 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/")
20
 # The names have to be unique unfortunately.
16
 # The names have to be unique unfortunately.
21
 set(EXECUTABLE_NAME "slop")
17
 set(EXECUTABLE_NAME "slop")
22
 set(LIBRARY_NAME "slopy")
18
 set(LIBRARY_NAME "slopy")
23
-set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/modules" )
24
-
25
-if ( SLOP_LEGACY_MODE )
26
-    add_definitions(-DSLOP_LEGACY_MODE=true)
27
-    add_library(${LIBRARY_NAME} SHARED  src/mouse.cpp
28
-                                        src/keyboard.cpp
29
-                                        src/x.cpp
30
-                                        src/slopstates.cpp
31
-                                        src/windowhelper.cpp
32
-                                        src/resource.cpp
33
-                                        src/xslop.cpp
34
-                                        src/xshaperectangle.cpp)
35
-    set_property(TARGET ${LIBRARY_NAME} PROPERTY CXX_STANDARD 11)
36
-    set_property(TARGET ${LIBRARY_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
37
 
19
 
38
-    add_executable(${EXECUTABLE_NAME} src/options.cpp
39
-                                      src/main.cpp)
20
+add_library(${LIBRARY_NAME} SHARED  src/mouse.cpp
21
+                                    src/keyboard.cpp
22
+                                    src/x.cpp
23
+                                    src/slopstates.cpp
24
+                                    src/framebuffer.cpp
25
+                                    src/windowhelper.cpp
26
+                                    src/resource.cpp
27
+                                    src/gl_core_3_3.c
28
+                                    src/shader.cpp
29
+                                    src/window.cpp
30
+                                    src/slop.cpp
31
+                                    src/rectangle.cpp
32
+                                    src/xshaperectangle.cpp
33
+                                    src/glrectangle.cpp)
34
+
35
+set_property(TARGET ${LIBRARY_NAME} PROPERTY CXX_STANDARD 11)
36
+set_property(TARGET ${LIBRARY_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
37
+
38
+add_executable(${EXECUTABLE_NAME} src/options.cpp
39
+                                  src/main.cpp)
40
 
40
 
41
-    # Detect and add cmake modules
42
-    find_package(GLM REQUIRED)
43
-    find_package(X11 REQUIRED)
44
-    find_package(XExt REQUIRED)
45
-    include_directories(${X11_INCLUDE_DIR}
46
-                        ${GLM_INCLUDE_DIR}
47
-                        ${XEXT_INCLUDE_DIR})
48
-    target_link_libraries(${EXECUTABLE_NAME} ${X11_LIBRARIES}
49
-                                             ${GLM_LIBRARIES}
50
-                                             ${XEXT_LIBRARIES}
51
-                                             ${LIBRARY_NAME})
52
-    # Install targets
53
-    install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
54
-    install( TARGETS ${LIBRARY_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" )
55
-    install( FILES ${CMAKE_SOURCE_DIR}/src/slop.hpp DESTINATION "${CMAKE_INSTALL_PREFIX}/include" )
56
-else()
57
-    add_library(${LIBRARY_NAME} SHARED  src/mouse.cpp
58
-                                        src/keyboard.cpp
59
-                                        src/x.cpp
60
-                                        src/slopstates.cpp
61
-                                        src/framebuffer.cpp
62
-                                        src/windowhelper.cpp
63
-                                        src/resource.cpp
64
-                                        src/gl_core_3_3.c
65
-                                        src/shader.cpp
66
-                                        src/window.cpp
67
-                                        src/glslop.cpp
68
-                                        src/rectangle.cpp)
69
-
70
-    add_executable(${EXECUTABLE_NAME} src/options.cpp
71
-                                      src/main.cpp)
72
-
73
-    # Detect and add cmake modules
74
-    find_package(GLM REQUIRED)
75
-    find_package(X11 REQUIRED)
76
-    find_package(GLX REQUIRED)
77
-    find_package(OpenGL REQUIRED)
78
-    include_directories(${X11_INCLUDE_DIR}
79
-                        ${GLM_INCLUDE_DIR}
80
-                        ${GLX_INCLUDE_DIR}
81
-                        ${OPENGL_INCLUDE_DIR})
82
-    target_link_libraries(${EXECUTABLE_NAME} ${X11_LIBRARIES}
83
-                                             ${GLM_LIBRARIES}
84
-                                             ${OPENGL_LIBRARIES}
85
-                                             ${GLX_LIBRARY}
86
-                                             ${LIBRARY_NAME})
87
-    # Install targets
88
-    install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
89
-    install( TARGETS ${LIBRARY_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" )
90
-    install( FILES ${CMAKE_SOURCE_DIR}/src/slop.hpp DESTINATION "${CMAKE_INSTALL_PREFIX}/include" )
91
-endif()
41
+set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/modules" )
42
+find_package(GLM REQUIRED)
43
+find_package(X11 REQUIRED)
44
+find_package(XExt REQUIRED)
45
+find_package(GLX REQUIRED)
46
+find_package(OpenGL REQUIRED)
47
+include_directories(${X11_INCLUDE_DIR}
48
+                    ${GLM_INCLUDE_DIR}
49
+                    ${XEXT_INCLUDE_DIR}
50
+                    ${GLX_INCLUDE_DIR}
51
+                    ${OPENGL_INCLUDE_DIR})
52
+
53
+target_link_libraries(${EXECUTABLE_NAME} ${X11_LIBRARIES}
54
+                                         ${GLM_LIBRARIES}
55
+                                         ${OPENGL_LIBRARIES}
56
+                                         ${GLX_LIBRARY}
57
+                                         ${XEXT_LIBRARIES}
58
+                                         ${LIBRARY_NAME})
59
+
60
+install( TARGETS ${EXECUTABLE_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" )
61
+install( TARGETS ${LIBRARY_NAME} DESTINATION "${CMAKE_INSTALL_PREFIX}/lib" )
62
+install( FILES ${CMAKE_SOURCE_DIR}/src/slop.hpp DESTINATION "${CMAKE_INSTALL_PREFIX}/include" )

+ 246
- 0
src/glrectangle.cpp View File

1
+#include "glrectangle.hpp"
2
+
3
+GLRectangle::GLRectangle( glm::vec2 p1, glm::vec2 p2, float border, float padding, glm::vec4 color, bool highlight ) {
4
+    this->color = color;
5
+    this->border = border;
6
+    this->padding = padding;
7
+    this->highlight = highlight;
8
+    // Find each corner of the rectangle
9
+    ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
10
+    bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
11
+    ur = glm::vec2( glm::max( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
12
+    br = glm::vec2( glm::max( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
13
+    // Offset the inner corners by the padding.
14
+    ul = ul + glm::vec2(-padding,padding);
15
+    bl = bl + glm::vec2(-padding,-padding);
16
+    ur = ur + glm::vec2(padding,padding);
17
+    br = br + glm::vec2(padding,-padding);
18
+    // Create the outer corners by offsetting the inner by the bordersize
19
+    oul = ul + glm::vec2(-border,border);
20
+    obl = bl + glm::vec2(-border,-border);
21
+    our = ur + glm::vec2(border,border);
22
+    obr = br + glm::vec2(border,-border);
23
+    generateBuffers();
24
+    std::string vert = "#version 130\nin vec2 position;\nuniform mat4 projection;\nvoid main() {\ngl_Position = projection*vec4(position,0,1);\n }";
25
+    std::string frag = "#version 130\nuniform vec4 color;\nout vec4 outColor;\nvoid main() {\noutColor = color;\n}";
26
+
27
+    shader = new Shader( vert, frag, false );
28
+}
29
+
30
+void GLRectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
31
+    // Find each corner of the rectangle
32
+    ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
33
+    bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
34
+    ur = glm::vec2( glm::max( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
35
+    br = glm::vec2( glm::max( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
36
+    // Offset the inner corners by the padding.
37
+    ul = ul + glm::vec2(-padding,padding);
38
+    bl = bl + glm::vec2(-padding,-padding);
39
+    ur = ur + glm::vec2(padding,padding);
40
+    br = br + glm::vec2(padding,-padding);
41
+    // Create the outer corners by offsetting the inner by the bordersize
42
+    oul = ul + glm::vec2(-border,border);
43
+    obl = bl + glm::vec2(-border,-border);
44
+    our = ur + glm::vec2(border,border);
45
+    obr = br + glm::vec2(border,-border);
46
+    generateBuffers();
47
+}
48
+
49
+void GLRectangle::generateBuffers() {
50
+    std::vector<glm::vec2> rectangle_verts;
51
+    std::vector<glm::vec2> rectangle_uvs;
52
+    std::vector<glm::vec2> corner_verts;
53
+    std::vector<glm::vec2> corner_uvs;
54
+    std::vector<glm::vec2> center_verts;
55
+    std::vector<glm::vec2> center_uvs;
56
+
57
+    // Top left corner
58
+    corner_verts.push_back( oul );
59
+    corner_uvs.push_back( glm::vec2(0, 1) );
60
+    corner_verts.push_back( ul );
61
+    corner_uvs.push_back( glm::vec2(1, 0) );
62
+    corner_verts.push_back( ul + glm::vec2( -border, 0 ) );
63
+    corner_uvs.push_back( glm::vec2(0, 0) );
64
+
65
+    corner_verts.push_back( oul );
66
+    corner_uvs.push_back( glm::vec2(0, 1) );
67
+    corner_verts.push_back( ul + glm::vec2( 0, border ) );
68
+    corner_uvs.push_back( glm::vec2(1, 1) );
69
+    corner_verts.push_back( ul );
70
+    corner_uvs.push_back( glm::vec2(1, 0) );
71
+
72
+    // Top right corner
73
+    corner_verts.push_back( ur + glm::vec2( 0, border ) );
74
+    corner_uvs.push_back( glm::vec2(0, 1) );
75
+    corner_verts.push_back( ur + glm::vec2( border, 0 ) );
76
+    corner_uvs.push_back( glm::vec2(1, 0) );
77
+    corner_verts.push_back( ur );
78
+    corner_uvs.push_back( glm::vec2(0, 0) );
79
+
80
+    corner_verts.push_back( ur + glm::vec2( 0, border ) );
81
+    corner_uvs.push_back( glm::vec2(0, 1) );
82
+    corner_verts.push_back( our );
83
+    corner_uvs.push_back( glm::vec2(1, 1) );
84
+    corner_verts.push_back( ur + glm::vec2( border, 0 ) );
85
+    corner_uvs.push_back( glm::vec2(1, 0) );
86
+
87
+    // Bottom left corner
88
+    corner_verts.push_back( bl + glm::vec2( -border, 0 ) );
89
+    corner_uvs.push_back( glm::vec2(0, 1) );
90
+    corner_verts.push_back( obl + glm::vec2( border, 0 ) );
91
+    corner_uvs.push_back( glm::vec2(1, 0) );
92
+    corner_verts.push_back( obl );
93
+    corner_uvs.push_back( glm::vec2(0, 0) );
94
+
95
+    corner_verts.push_back( bl + glm::vec2( -border, 0 ) );
96
+    corner_uvs.push_back( glm::vec2(0, 1) );
97
+    corner_verts.push_back( bl );
98
+    corner_uvs.push_back( glm::vec2(1, 1) );
99
+    corner_verts.push_back( bl + glm::vec2( 0, -border ) );
100
+    corner_uvs.push_back( glm::vec2(1, 0) );
101
+
102
+    // Bottom right corner
103
+    corner_verts.push_back( br );
104
+    corner_uvs.push_back( glm::vec2(0, 1) );
105
+    corner_verts.push_back( obr );
106
+    corner_uvs.push_back( glm::vec2(1, 0) );
107
+    corner_verts.push_back( br + glm::vec2( 0, -border ) );
108
+    corner_uvs.push_back( glm::vec2(0, 0) );
109
+
110
+    corner_verts.push_back( br );
111
+    corner_uvs.push_back( glm::vec2(0, 1) );
112
+    corner_verts.push_back( br + glm::vec2( border, 0 ) );
113
+    corner_uvs.push_back( glm::vec2(1, 1) );
114
+    corner_verts.push_back( obr );
115
+    corner_uvs.push_back( glm::vec2(1, 0) );
116
+
117
+    // Left GLRectangle
118
+    rectangle_verts.push_back( ul + glm::vec2( -border, 0 ) );
119
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
120
+    rectangle_verts.push_back( bl );
121
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
122
+    rectangle_verts.push_back( bl + glm::vec2( -border, 0 ) );
123
+    rectangle_uvs.push_back( glm::vec2(0, 0) );
124
+
125
+    rectangle_verts.push_back( ul + glm::vec2( -border, 0 ) );
126
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
127
+    rectangle_verts.push_back( ul );
128
+    rectangle_uvs.push_back( glm::vec2(1, 1) );
129
+    rectangle_verts.push_back( bl );
130
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
131
+
132
+    // Right GLRectangle
133
+    rectangle_verts.push_back( ur );
134
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
135
+    rectangle_verts.push_back( br + glm::vec2( border, 0 ) );
136
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
137
+    rectangle_verts.push_back( br );
138
+    rectangle_uvs.push_back( glm::vec2(0, 0) );
139
+
140
+    rectangle_verts.push_back( ur );
141
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
142
+    rectangle_verts.push_back( ur + glm::vec2( border, 0 ) );
143
+    rectangle_uvs.push_back( glm::vec2(1, 1) );
144
+    rectangle_verts.push_back( br + glm::vec2( border, 0 ) );
145
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
146
+
147
+    // Top GLRectangle
148
+    rectangle_verts.push_back( ul + glm::vec2( 0, border ) );
149
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
150
+    rectangle_verts.push_back( ur );
151
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
152
+    rectangle_verts.push_back( ul );
153
+    rectangle_uvs.push_back( glm::vec2(0, 0) );
154
+
155
+    rectangle_verts.push_back( ul + glm::vec2( 0, border ) );
156
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
157
+    rectangle_verts.push_back( ur + glm::vec2( 0, border ) );
158
+    rectangle_uvs.push_back( glm::vec2(1, 1) );
159
+    rectangle_verts.push_back( ur );
160
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
161
+
162
+    // Bot GLRectangle
163
+    rectangle_verts.push_back( bl );
164
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
165
+    rectangle_verts.push_back( br + glm::vec2( 0, -border ) );
166
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
167
+    rectangle_verts.push_back( bl + glm::vec2( 0, -border ) );
168
+    rectangle_uvs.push_back( glm::vec2(0, 0) );
169
+
170
+    rectangle_verts.push_back( bl );
171
+    rectangle_uvs.push_back( glm::vec2(0, 1) );
172
+    rectangle_verts.push_back( br );
173
+    rectangle_uvs.push_back( glm::vec2(1, 1) );
174
+    rectangle_verts.push_back( br + glm::vec2( 0, -border ) );
175
+    rectangle_uvs.push_back( glm::vec2(1, 0) );
176
+
177
+    center_verts.push_back( ul );
178
+    center_uvs.push_back( glm::vec2(0, 1) );
179
+    center_verts.push_back( br );
180
+    center_uvs.push_back( glm::vec2(1, 0) );
181
+    center_verts.push_back( bl );
182
+    center_uvs.push_back( glm::vec2(0, 0) );
183
+
184
+    center_verts.push_back( ul );
185
+    center_uvs.push_back( glm::vec2(0, 1) );
186
+    center_verts.push_back( ur );
187
+    center_uvs.push_back( glm::vec2(1, 1) );
188
+    center_verts.push_back( br );
189
+    center_uvs.push_back( glm::vec2(1, 0) );
190
+
191
+    glGenBuffers( 6, (GLuint*)&buffer );
192
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.corner_verts );
193
+    glBufferData( GL_ARRAY_BUFFER, corner_verts.size() * sizeof( glm::vec2 ), &corner_verts[0], GL_STATIC_DRAW );
194
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.corner_uvs );
195
+    glBufferData( GL_ARRAY_BUFFER, corner_uvs.size() * sizeof( glm::vec2 ), &corner_uvs[0], GL_STATIC_DRAW );
196
+
197
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_verts );
198
+    glBufferData( GL_ARRAY_BUFFER, rectangle_verts.size() * sizeof( glm::vec2 ), &rectangle_verts[0], GL_STATIC_DRAW );
199
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_uvs );
200
+    glBufferData( GL_ARRAY_BUFFER, rectangle_uvs.size() * sizeof( glm::vec2 ), &rectangle_uvs[0], GL_STATIC_DRAW );
201
+
202
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.center_verts );
203
+    glBufferData( GL_ARRAY_BUFFER, center_verts.size() * sizeof( glm::vec2 ), &center_verts[0], GL_STATIC_DRAW );
204
+    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_uvs );
205
+    glBufferData( GL_ARRAY_BUFFER, center_uvs.size() * sizeof( glm::vec2 ), &center_uvs[0], GL_STATIC_DRAW );
206
+    corner_vertCount = corner_verts.size();
207
+    rectangle_vertCount = rectangle_verts.size();
208
+    center_vertCount = center_verts.size();
209
+}
210
+
211
+GLRectangle::~GLRectangle() {
212
+    delete shader;
213
+    glDeleteBuffers( 6, (GLuint*)&buffer );
214
+}
215
+
216
+void GLRectangle::draw( glm::mat4& matrix ) {
217
+    glEnable( GL_BLEND );
218
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
219
+    shader->bind();
220
+    shader->setParameter( "projection", matrix );
221
+    if ( highlight ) {
222
+        // Draw the center of the highlight
223
+        shader->setParameter( "color", color );
224
+        shader->setAttribute( "position", buffer.center_verts, 2 );
225
+        glDrawArrays(GL_TRIANGLES, 0, center_vertCount );
226
+        // Set the color to have no alpha, then draw the borders.
227
+        glm::vec4 fullAlpha = glm::vec4(color.r,color.g,color.b,1);
228
+        shader->setParameter( "color", fullAlpha );
229
+        shader->setAttribute( "position", buffer.corner_verts, 2 );
230
+        glDrawArrays(GL_TRIANGLES, 0, corner_vertCount );
231
+        shader->setAttribute( "position", buffer.rectangle_verts, 2 );
232
+        glDrawArrays(GL_TRIANGLES, 0, rectangle_vertCount );
233
+    } else {
234
+        shader->setParameter( "color", color );
235
+        shader->setAttribute( "position", buffer.corner_verts, 2 );
236
+        glDrawArrays(GL_TRIANGLES, 0, corner_vertCount );
237
+        shader->setAttribute( "position", buffer.rectangle_verts, 2 );
238
+        glDrawArrays(GL_TRIANGLES, 0, rectangle_vertCount );
239
+    }
240
+    shader->unbind();
241
+    glDisable( GL_BLEND );
242
+}
243
+
244
+glm::vec4 GLRectangle::getRect() {
245
+    return glm::vec4( bl.x, bl.y, ur.x-ul.x, ul.y-bl.y );
246
+}

+ 66
- 0
src/glrectangle.hpp View File

1
+/* rectangle.hpp: generates a vertex mesh and draws it.
2
+ *
3
+ * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4
+ *
5
+ * This file is part of Slop.
6
+ *
7
+ * Slop is free software: you can redistribute it and/or modify
8
+ * it under the terms of the GNU General Public License as published by
9
+ * the Free Software Foundation, either version 3 of the License, or
10
+ * (at your option) any later version.
11
+ *
12
+ * Slop is distributed in the hope that it will be useful,
13
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
+ * GNU General Public License for more details.
16
+ *
17
+ * You should have received a copy of the GNU General Public License
18
+ * along with Slop.  If not, see <http://www.gnu.org/licenses/>.
19
+ */
20
+
21
+#ifndef N_GLRECTANGLE_H_
22
+#define N_GLRECTANGLE_H_
23
+
24
+#include "gl_core_3_3.h"
25
+#include <iostream>
26
+#include <glm/glm.hpp>
27
+#include <GL/gl.h>
28
+#include <vector>
29
+
30
+#include "shader.hpp"
31
+#include "rectangle.hpp"
32
+
33
+struct RectangleBuffer {
34
+    unsigned int corner_verts;
35
+    unsigned int corner_uvs;
36
+    unsigned int rectangle_verts;
37
+    unsigned int rectangle_uvs;
38
+    unsigned int center_verts;
39
+    unsigned int center_uvs;
40
+};
41
+
42
+class GLRectangle : public Rectangle {
43
+private:
44
+    glm::vec2 ul, oul;
45
+    glm::vec2 bl, obl;
46
+    glm::vec2 ur, our;
47
+    glm::vec2 br, obr;
48
+    bool highlight;
49
+    void generateBuffers();
50
+    RectangleBuffer buffer;
51
+    unsigned int corner_vertCount;
52
+    unsigned int rectangle_vertCount;
53
+    unsigned int center_vertCount;
54
+    float border;
55
+    float padding;
56
+    Shader* shader;
57
+    glm::vec4 color;
58
+public:
59
+    glm::vec4 getRect();
60
+    GLRectangle(glm::vec2 p1, glm::vec2 p2, float border = 1, float padding = 0, glm::vec4 color = glm::vec4(1,1,1,1), bool highlight = false );
61
+    ~GLRectangle();
62
+    void setPoints( glm::vec2 p1, glm::vec2 p2 );
63
+    void draw(glm::mat4& matrix);
64
+};
65
+
66
+#endif // N_RECTANGLE_H_

+ 0
- 116
src/glslop.cpp View File

1
-#include "slop.hpp"
2
-
3
-#include "slopstates.hpp"
4
-#include "mouse.hpp"
5
-#include "resource.hpp"
6
-#include "keyboard.hpp"
7
-
8
-#include "window.hpp"
9
-#include "shader.hpp"
10
-#include "framebuffer.hpp"
11
-#include "rectangle.hpp"
12
-
13
-
14
-X11* x11;
15
-Mouse* mouse;
16
-Keyboard* keyboard;
17
-Resource* resource;
18
-
19
-// Defaults!
20
-SlopOptions::SlopOptions() {
21
-    borderSize = 1;
22
-    nodecorations = false;
23
-    tolerance = 2;
24
-    padding = 0;
25
-    shader = "textured";
26
-    highlight = false;
27
-    xdisplay = ":0";
28
-    r = 0.5;
29
-    g = 0.5;
30
-    b = 0.5;
31
-    a = 1;
32
-}
33
-
34
-SlopSelection::SlopSelection( float x, float y, float w, float h, Window id ) {
35
-    this->x = x;
36
-    this->y = y;
37
-    this->w = w;
38
-    this->h = h;
39
-    this->id = id;
40
-}
41
-
42
-SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
43
-    bool deleteOptions = false;
44
-    if ( !options ) {
45
-        deleteOptions = true;
46
-        options = new SlopOptions();
47
-    }
48
-    resource = new Resource();
49
-    // Set up x11 temporarily
50
-    x11 = new X11(options->xdisplay);
51
-    keyboard = new Keyboard( x11 );
52
-
53
-    // Set up window with GL context
54
-    SlopWindow* window = new SlopWindow();
55
-
56
-    mouse = new Mouse( x11, options->nodecorations, window->window );
57
-
58
-    if ( options->shader != "textured" ) {
59
-        window->framebuffer->setShader( options->shader );
60
-    }
61
-
62
-    // Init our little state machine, memory is a tad of a misnomer
63
-    SlopMemory memory( options );
64
-
65
-    // This is where we'll run through all of our stuffs
66
-    while( memory.running ) {
67
-        mouse->update();
68
-        keyboard->update();
69
-        // We move our statemachine forward.
70
-        memory.update( 1 );
71
-
72
-        // Then we draw our junk to a framebuffer.
73
-        window->framebuffer->bind();
74
-        glClearColor (0.0, 0.0, 0.0, 0.0);
75
-        glClear (GL_COLOR_BUFFER_BIT);
76
-        memory.draw( window->camera );
77
-        window->framebuffer->unbind();
78
-
79
-        // Then we draw the framebuffer to the screen
80
-        window->framebuffer->draw();
81
-        window->display();
82
-        GLenum err = glGetError();
83
-        if ( err != GL_NO_ERROR ) {
84
-            throw err;
85
-        }
86
-        if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
87
-            memory.running = false;
88
-            if ( cancelled ) {
89
-                *cancelled = true;
90
-            }
91
-        } else {
92
-            *cancelled = false;
93
-        }
94
-    }
95
-
96
-    // Now we should have a selection! We parse everything we know about it here.
97
-    glm::vec4 output = memory.rectangle->getRect();
98
-
99
-    // Lets now clear both front and back buffers before closing.
100
-    // hopefully it'll be completely transparent while closing!
101
-    glClearColor (0.0, 0.0, 0.0, 0.0);
102
-    glClear (GL_COLOR_BUFFER_BIT);
103
-    window->display();
104
-    glClear (GL_COLOR_BUFFER_BIT);
105
-    window->display();
106
-    // Then we clean up.
107
-    delete window;
108
-    delete mouse;
109
-    delete x11;
110
-    delete resource;
111
-    if ( deleteOptions ) {
112
-        delete options;
113
-    }
114
-    // Finally return the data.
115
-    return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
116
-}

+ 0
- 4
src/main.cpp View File

32
     options.getColor("color", 'c', color);
32
     options.getColor("color", 'c', color);
33
     options.getBool("nokeyboard", 'k', foo->nokeyboard);
33
     options.getBool("nokeyboard", 'k', foo->nokeyboard);
34
     options.getString( "xdisplay", 'x', foo->xdisplay );
34
     options.getString( "xdisplay", 'x', foo->xdisplay );
35
-#ifndef SLOP_LEGACY_MODE
36
     options.getString( "shader", 's', foo->shader );
35
     options.getString( "shader", 's', foo->shader );
37
-#endif
38
     foo->r = color.r;
36
     foo->r = color.r;
39
     foo->g = color.g;
37
     foo->g = color.g;
40
     foo->b = color.b;
38
     foo->b = color.b;
114
 	std::cout << "                                  highlights it. This is only useful when\n";
112
 	std::cout << "                                  highlights it. This is only useful when\n";
115
 	std::cout << "                                  --color is set to a transparent color.\n";
113
 	std::cout << "                                  --color is set to a transparent color.\n";
116
 	std::cout << "                                  (default=off)\n";
114
 	std::cout << "                                  (default=off)\n";
117
-#ifndef SLOP_LEGACY_MODE
118
 	std::cout << "      --shader=STRING           Sets the shader to load and use from\n";
115
 	std::cout << "      --shader=STRING           Sets the shader to load and use from\n";
119
 	std::cout << "                                  ~/.config/slop/\n";
116
 	std::cout << "                                  ~/.config/slop/\n";
120
-#endif
121
 	std::cout << "                                  (default=`simple')\n";
117
 	std::cout << "                                  (default=`simple')\n";
122
 	std::cout << "  -f, --format=STRING           Set the output format string. Format specifiers\n";
118
 	std::cout << "  -f, --format=STRING           Set the output format string. Format specifiers\n";
123
 	std::cout << "                                  are %x, %y, %w, %h, %i, %g, and %c.\n";
119
 	std::cout << "                                  are %x, %y, %w, %h, %i, %g, and %c.\n";

+ 5
- 238
src/rectangle.cpp View File

1
 #include "rectangle.hpp"
1
 #include "rectangle.hpp"
2
 
2
 
3
-Rectangle::Rectangle( glm::vec2 p1, glm::vec2 p2, float border, float padding, glm::vec4 color, bool highlight ) {
4
-    this->color = color;
5
-    this->border = border;
6
-    this->padding = padding;
7
-    this->highlight = highlight;
8
-    // Find each corner of the rectangle
9
-    ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
10
-    bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
11
-    ur = glm::vec2( glm::max( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
12
-    br = glm::vec2( glm::max( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
13
-    // Offset the inner corners by the padding.
14
-    ul = ul + glm::vec2(-padding,padding);
15
-    bl = bl + glm::vec2(-padding,-padding);
16
-    ur = ur + glm::vec2(padding,padding);
17
-    br = br + glm::vec2(padding,-padding);
18
-    // Create the outer corners by offsetting the inner by the bordersize
19
-    oul = ul + glm::vec2(-border,border);
20
-    obl = bl + glm::vec2(-border,-border);
21
-    our = ur + glm::vec2(border,border);
22
-    obr = br + glm::vec2(border,-border);
23
-    generateBuffers();
24
-    std::string vert = "#version 130\nin vec2 position;\nuniform mat4 projection;\nvoid main() {\ngl_Position = projection*vec4(position,0,1);\n }";
25
-    std::string frag = "#version 130\nuniform vec4 color;\nout vec4 outColor;\nvoid main() {\noutColor = color;\n}";
26
-
27
-    shader = new Shader( vert, frag, false );
28
-}
29
-
30
-void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
31
-    // Find each corner of the rectangle
32
-    ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
33
-    bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
34
-    ur = glm::vec2( glm::max( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
35
-    br = glm::vec2( glm::max( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
36
-    // Offset the inner corners by the padding.
37
-    ul = ul + glm::vec2(-padding,padding);
38
-    bl = bl + glm::vec2(-padding,-padding);
39
-    ur = ur + glm::vec2(padding,padding);
40
-    br = br + glm::vec2(padding,-padding);
41
-    // Create the outer corners by offsetting the inner by the bordersize
42
-    oul = ul + glm::vec2(-border,border);
43
-    obl = bl + glm::vec2(-border,-border);
44
-    our = ur + glm::vec2(border,border);
45
-    obr = br + glm::vec2(border,-border);
46
-    generateBuffers();
47
-}
48
-
49
-void Rectangle::generateBuffers() {
50
-    std::vector<glm::vec2> rectangle_verts;
51
-    std::vector<glm::vec2> rectangle_uvs;
52
-    std::vector<glm::vec2> corner_verts;
53
-    std::vector<glm::vec2> corner_uvs;
54
-    std::vector<glm::vec2> center_verts;
55
-    std::vector<glm::vec2> center_uvs;
56
-
57
-    // Top left corner
58
-    corner_verts.push_back( oul );
59
-    corner_uvs.push_back( glm::vec2(0, 1) );
60
-    corner_verts.push_back( ul );
61
-    corner_uvs.push_back( glm::vec2(1, 0) );
62
-    corner_verts.push_back( ul + glm::vec2( -border, 0 ) );
63
-    corner_uvs.push_back( glm::vec2(0, 0) );
64
-
65
-    corner_verts.push_back( oul );
66
-    corner_uvs.push_back( glm::vec2(0, 1) );
67
-    corner_verts.push_back( ul + glm::vec2( 0, border ) );
68
-    corner_uvs.push_back( glm::vec2(1, 1) );
69
-    corner_verts.push_back( ul );
70
-    corner_uvs.push_back( glm::vec2(1, 0) );
71
-
72
-    // Top right corner
73
-    corner_verts.push_back( ur + glm::vec2( 0, border ) );
74
-    corner_uvs.push_back( glm::vec2(0, 1) );
75
-    corner_verts.push_back( ur + glm::vec2( border, 0 ) );
76
-    corner_uvs.push_back( glm::vec2(1, 0) );
77
-    corner_verts.push_back( ur );
78
-    corner_uvs.push_back( glm::vec2(0, 0) );
79
-
80
-    corner_verts.push_back( ur + glm::vec2( 0, border ) );
81
-    corner_uvs.push_back( glm::vec2(0, 1) );
82
-    corner_verts.push_back( our );
83
-    corner_uvs.push_back( glm::vec2(1, 1) );
84
-    corner_verts.push_back( ur + glm::vec2( border, 0 ) );
85
-    corner_uvs.push_back( glm::vec2(1, 0) );
86
-
87
-    // Bottom left corner
88
-    corner_verts.push_back( bl + glm::vec2( -border, 0 ) );
89
-    corner_uvs.push_back( glm::vec2(0, 1) );
90
-    corner_verts.push_back( obl + glm::vec2( border, 0 ) );
91
-    corner_uvs.push_back( glm::vec2(1, 0) );
92
-    corner_verts.push_back( obl );
93
-    corner_uvs.push_back( glm::vec2(0, 0) );
94
-
95
-    corner_verts.push_back( bl + glm::vec2( -border, 0 ) );
96
-    corner_uvs.push_back( glm::vec2(0, 1) );
97
-    corner_verts.push_back( bl );
98
-    corner_uvs.push_back( glm::vec2(1, 1) );
99
-    corner_verts.push_back( bl + glm::vec2( 0, -border ) );
100
-    corner_uvs.push_back( glm::vec2(1, 0) );
101
-
102
-    // Bottom right corner
103
-    corner_verts.push_back( br );
104
-    corner_uvs.push_back( glm::vec2(0, 1) );
105
-    corner_verts.push_back( obr );
106
-    corner_uvs.push_back( glm::vec2(1, 0) );
107
-    corner_verts.push_back( br + glm::vec2( 0, -border ) );
108
-    corner_uvs.push_back( glm::vec2(0, 0) );
109
-
110
-    corner_verts.push_back( br );
111
-    corner_uvs.push_back( glm::vec2(0, 1) );
112
-    corner_verts.push_back( br + glm::vec2( border, 0 ) );
113
-    corner_uvs.push_back( glm::vec2(1, 1) );
114
-    corner_verts.push_back( obr );
115
-    corner_uvs.push_back( glm::vec2(1, 0) );
116
-
117
-    // Left Rectangle
118
-    rectangle_verts.push_back( ul + glm::vec2( -border, 0 ) );
119
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
120
-    rectangle_verts.push_back( bl );
121
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
122
-    rectangle_verts.push_back( bl + glm::vec2( -border, 0 ) );
123
-    rectangle_uvs.push_back( glm::vec2(0, 0) );
124
-
125
-    rectangle_verts.push_back( ul + glm::vec2( -border, 0 ) );
126
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
127
-    rectangle_verts.push_back( ul );
128
-    rectangle_uvs.push_back( glm::vec2(1, 1) );
129
-    rectangle_verts.push_back( bl );
130
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
131
-
132
-    // Right Rectangle
133
-    rectangle_verts.push_back( ur );
134
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
135
-    rectangle_verts.push_back( br + glm::vec2( border, 0 ) );
136
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
137
-    rectangle_verts.push_back( br );
138
-    rectangle_uvs.push_back( glm::vec2(0, 0) );
139
-
140
-    rectangle_verts.push_back( ur );
141
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
142
-    rectangle_verts.push_back( ur + glm::vec2( border, 0 ) );
143
-    rectangle_uvs.push_back( glm::vec2(1, 1) );
144
-    rectangle_verts.push_back( br + glm::vec2( border, 0 ) );
145
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
146
-
147
-    // Top Rectangle
148
-    rectangle_verts.push_back( ul + glm::vec2( 0, border ) );
149
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
150
-    rectangle_verts.push_back( ur );
151
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
152
-    rectangle_verts.push_back( ul );
153
-    rectangle_uvs.push_back( glm::vec2(0, 0) );
154
-
155
-    rectangle_verts.push_back( ul + glm::vec2( 0, border ) );
156
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
157
-    rectangle_verts.push_back( ur + glm::vec2( 0, border ) );
158
-    rectangle_uvs.push_back( glm::vec2(1, 1) );
159
-    rectangle_verts.push_back( ur );
160
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
161
-
162
-    // Bot Rectangle
163
-    rectangle_verts.push_back( bl );
164
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
165
-    rectangle_verts.push_back( br + glm::vec2( 0, -border ) );
166
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
167
-    rectangle_verts.push_back( bl + glm::vec2( 0, -border ) );
168
-    rectangle_uvs.push_back( glm::vec2(0, 0) );
169
-
170
-    rectangle_verts.push_back( bl );
171
-    rectangle_uvs.push_back( glm::vec2(0, 1) );
172
-    rectangle_verts.push_back( br );
173
-    rectangle_uvs.push_back( glm::vec2(1, 1) );
174
-    rectangle_verts.push_back( br + glm::vec2( 0, -border ) );
175
-    rectangle_uvs.push_back( glm::vec2(1, 0) );
176
-
177
-    center_verts.push_back( ul );
178
-    center_uvs.push_back( glm::vec2(0, 1) );
179
-    center_verts.push_back( br );
180
-    center_uvs.push_back( glm::vec2(1, 0) );
181
-    center_verts.push_back( bl );
182
-    center_uvs.push_back( glm::vec2(0, 0) );
183
-
184
-    center_verts.push_back( ul );
185
-    center_uvs.push_back( glm::vec2(0, 1) );
186
-    center_verts.push_back( ur );
187
-    center_uvs.push_back( glm::vec2(1, 1) );
188
-    center_verts.push_back( br );
189
-    center_uvs.push_back( glm::vec2(1, 0) );
190
-
191
-    glGenBuffers( 6, (GLuint*)&buffer );
192
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.corner_verts );
193
-    glBufferData( GL_ARRAY_BUFFER, corner_verts.size() * sizeof( glm::vec2 ), &corner_verts[0], GL_STATIC_DRAW );
194
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.corner_uvs );
195
-    glBufferData( GL_ARRAY_BUFFER, corner_uvs.size() * sizeof( glm::vec2 ), &corner_uvs[0], GL_STATIC_DRAW );
196
-
197
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_verts );
198
-    glBufferData( GL_ARRAY_BUFFER, rectangle_verts.size() * sizeof( glm::vec2 ), &rectangle_verts[0], GL_STATIC_DRAW );
199
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_uvs );
200
-    glBufferData( GL_ARRAY_BUFFER, rectangle_uvs.size() * sizeof( glm::vec2 ), &rectangle_uvs[0], GL_STATIC_DRAW );
201
-
202
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.center_verts );
203
-    glBufferData( GL_ARRAY_BUFFER, center_verts.size() * sizeof( glm::vec2 ), &center_verts[0], GL_STATIC_DRAW );
204
-    glBindBuffer( GL_ARRAY_BUFFER, buffer.rectangle_uvs );
205
-    glBufferData( GL_ARRAY_BUFFER, center_uvs.size() * sizeof( glm::vec2 ), &center_uvs[0], GL_STATIC_DRAW );
206
-    corner_vertCount = corner_verts.size();
207
-    rectangle_vertCount = rectangle_verts.size();
208
-    center_vertCount = center_verts.size();
3
+Rectangle::Rectangle() {
209
 }
4
 }
210
-
211
 Rectangle::~Rectangle() {
5
 Rectangle::~Rectangle() {
212
-    delete shader;
213
-    glDeleteBuffers( 6, (GLuint*)&buffer );
214
 }
6
 }
215
-
216
-void Rectangle::draw( glm::mat4& matrix ) {
217
-    glEnable( GL_BLEND );
218
-    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
219
-    shader->bind();
220
-    shader->setParameter( "projection", matrix );
221
-    if ( highlight ) {
222
-        // Draw the center of the highlight
223
-        shader->setParameter( "color", color );
224
-        shader->setAttribute( "position", buffer.center_verts, 2 );
225
-        glDrawArrays(GL_TRIANGLES, 0, center_vertCount );
226
-        // Set the color to have no alpha, then draw the borders.
227
-        glm::vec4 fullAlpha = glm::vec4(color.r,color.g,color.b,1);
228
-        shader->setParameter( "color", fullAlpha );
229
-        shader->setAttribute( "position", buffer.corner_verts, 2 );
230
-        glDrawArrays(GL_TRIANGLES, 0, corner_vertCount );
231
-        shader->setAttribute( "position", buffer.rectangle_verts, 2 );
232
-        glDrawArrays(GL_TRIANGLES, 0, rectangle_vertCount );
233
-    } else {
234
-        shader->setParameter( "color", color );
235
-        shader->setAttribute( "position", buffer.corner_verts, 2 );
236
-        glDrawArrays(GL_TRIANGLES, 0, corner_vertCount );
237
-        shader->setAttribute( "position", buffer.rectangle_verts, 2 );
238
-        glDrawArrays(GL_TRIANGLES, 0, rectangle_vertCount );
239
-    }
240
-    shader->unbind();
241
-    glDisable( GL_BLEND );
7
+void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
8
+}
9
+void Rectangle::draw(glm::mat4& matrix) {
242
 }
10
 }
243
-
244
 glm::vec4 Rectangle::getRect() {
11
 glm::vec4 Rectangle::getRect() {
245
-    return glm::vec4( bl.x, bl.y, ur.x-ul.x, ul.y-bl.y );
12
+    return glm::vec4();
246
 }
13
 }

+ 6
- 36
src/rectangle.hpp View File

1
-/* rectangle.hpp: generates a vertex mesh and draws it.
1
+/* rectangle.hpp: Rectangle generic
2
  *
2
  *
3
  * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
3
  * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4
  *
4
  *
21
 #ifndef N_RECTANGLE_H_
21
 #ifndef N_RECTANGLE_H_
22
 #define N_RECTANGLE_H_
22
 #define N_RECTANGLE_H_
23
 
23
 
24
-#include "gl_core_3_3.h"
25
-#include <iostream>
26
 #include <glm/glm.hpp>
24
 #include <glm/glm.hpp>
27
-#include <GL/gl.h>
28
-#include <vector>
29
-
30
-#include "shader.hpp"
31
-
32
-struct RectangleBuffer {
33
-    unsigned int corner_verts;
34
-    unsigned int corner_uvs;
35
-    unsigned int rectangle_verts;
36
-    unsigned int rectangle_uvs;
37
-    unsigned int center_verts;
38
-    unsigned int center_uvs;
39
-};
40
 
25
 
41
 class Rectangle {
26
 class Rectangle {
42
-private:
43
-    glm::vec2 ul, oul;
44
-    glm::vec2 bl, obl;
45
-    glm::vec2 ur, our;
46
-    glm::vec2 br, obr;
47
-    bool highlight;
48
-    void generateBuffers();
49
-    RectangleBuffer buffer;
50
-    unsigned int corner_vertCount;
51
-    unsigned int rectangle_vertCount;
52
-    unsigned int center_vertCount;
53
-    float border;
54
-    float padding;
55
-    Shader* shader;
56
-    glm::vec4 color;
57
 public:
27
 public:
58
-    glm::vec4 getRect();
59
-    Rectangle(glm::vec2 p1, glm::vec2 p2, float border = 1, float padding = 0, glm::vec4 color = glm::vec4(1,1,1,1), bool highlight = false );
60
-    ~Rectangle();
61
-    void setPoints( glm::vec2 p1, glm::vec2 p2 );
62
-    void draw(glm::mat4& matrix);
28
+    Rectangle();
29
+    virtual glm::vec4 getRect();
30
+    virtual ~Rectangle();
31
+    virtual void setPoints( glm::vec2 p1, glm::vec2 p2 );
32
+    virtual void draw(glm::mat4& matrix);
63
 };
33
 };
64
 
34
 
65
 #endif // N_RECTANGLE_H_
35
 #endif // N_RECTANGLE_H_

+ 169
- 0
src/slop.cpp View File

1
+#include "slop.hpp"
2
+
3
+X11* x11;
4
+Mouse* mouse;
5
+Keyboard* keyboard;
6
+Resource* resource;
7
+
8
+// Defaults!
9
+SlopOptions::SlopOptions() {
10
+    borderSize = 1;
11
+    nodecorations = false;
12
+    tolerance = 2;
13
+    padding = 0;
14
+    shader = "textured";
15
+    highlight = false;
16
+    xdisplay = ":0";
17
+    r = 0.5;
18
+    g = 0.5;
19
+    b = 0.5;
20
+    a = 1;
21
+}
22
+
23
+SlopSelection::SlopSelection( float x, float y, float w, float h, int id ) {
24
+    this->x = x;
25
+    this->y = y;
26
+    this->w = w;
27
+    this->h = h;
28
+    this->id = id;
29
+}
30
+
31
+SlopSelection GLSlopSelect( SlopOptions* options, bool* cancelled, SlopWindow* window );
32
+SlopSelection XShapeSlopSelect( SlopOptions* options, bool* cancelled);
33
+
34
+SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
35
+    SlopSelection returnval(0,0,0,0,0);
36
+    bool deleteOptions = false;
37
+    if ( !options ) {
38
+        deleteOptions = true;
39
+        options = new SlopOptions();
40
+    }
41
+    resource = new Resource();
42
+    // Set up x11 temporarily
43
+    x11 = new X11(options->xdisplay);
44
+    keyboard = new Keyboard( x11 );
45
+    // First we try to make an OpenGL enabled window
46
+    bool success = false;
47
+    SlopWindow* window;
48
+    try {
49
+        window = new SlopWindow();
50
+        success = true;
51
+    } catch (...) {
52
+        success = false;
53
+    }
54
+    if ( !success ) {
55
+        // If we fail, we launch the XShape version of slop.
56
+        std::cerr << "Failed to launch OpenGL context, --shader parameter will be ignored.\n";
57
+        returnval = XShapeSlopSelect( options, cancelled );
58
+    } else {
59
+        returnval = GLSlopSelect( options, cancelled, window );
60
+    }
61
+    delete x11;
62
+    delete resource;
63
+    if ( deleteOptions ) {
64
+        delete options;
65
+    }
66
+    return returnval;
67
+}
68
+
69
+SlopSelection XShapeSlopSelect( SlopOptions* options, bool* cancelled ) {
70
+    // Init our little state machine, memory is a tad of a misnomer
71
+    SlopMemory memory( options, new XShapeRectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
72
+    mouse = new Mouse( x11, options->nodecorations, ((XShapeRectangle*)memory.rectangle)->window );
73
+
74
+    // We have no GL context, so the matrix is useless...
75
+    glm::mat4 fake;
76
+    // This is where we'll run through all of our stuffs
77
+    while( memory.running ) {
78
+        mouse->update();
79
+        keyboard->update();
80
+        // We move our statemachine forward.
81
+        memory.update( 1 );
82
+
83
+        // We don't actually draw anything, but the state machine uses
84
+        // this to know when to spawn the window.
85
+        memory.draw( fake );
86
+
87
+        // X11 explodes if we update as fast as possible, here's a tiny sleep.
88
+        XFlush(x11->display);
89
+        std::this_thread::sleep_for(std::chrono::milliseconds(10));
90
+
91
+        // Then we draw the framebuffer to the screen
92
+        if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
93
+            memory.running = false;
94
+            if ( cancelled ) {
95
+                *cancelled = true;
96
+            }
97
+        } else {
98
+            *cancelled = false;
99
+        }
100
+    }
101
+
102
+    // Now we should have a selection! We parse everything we know about it here.
103
+    glm::vec4 output = memory.rectangle->getRect();
104
+
105
+    // Lets now clear both front and back buffers before closing.
106
+    // hopefully it'll be completely transparent while closing!
107
+    // Then we clean up.
108
+    delete mouse;
109
+    // Finally return the data.
110
+    return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
111
+}
112
+
113
+SlopSelection GLSlopSelect( SlopOptions* options, bool* cancelled, SlopWindow* window ) {
114
+    mouse = new Mouse( x11, options->nodecorations, window->window );
115
+
116
+    if ( options->shader != "textured" ) {
117
+        window->framebuffer->setShader( options->shader );
118
+    }
119
+
120
+    // Init our little state machine, memory is a tad of a misnomer
121
+    SlopMemory memory( options, new GLRectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight) );
122
+
123
+    // This is where we'll run through all of our stuffs
124
+    while( memory.running ) {
125
+        mouse->update();
126
+        keyboard->update();
127
+        // We move our statemachine forward.
128
+        memory.update( 1 );
129
+
130
+        // Then we draw our junk to a framebuffer.
131
+        window->framebuffer->bind();
132
+        glClearColor (0.0, 0.0, 0.0, 0.0);
133
+        glClear (GL_COLOR_BUFFER_BIT);
134
+        memory.draw( window->camera );
135
+        window->framebuffer->unbind();
136
+
137
+        // Then we draw the framebuffer to the screen
138
+        window->framebuffer->draw();
139
+        window->display();
140
+        GLenum err = glGetError();
141
+        if ( err != GL_NO_ERROR ) {
142
+            throw err;
143
+        }
144
+        if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
145
+            memory.running = false;
146
+            if ( cancelled ) {
147
+                *cancelled = true;
148
+            }
149
+        } else {
150
+            *cancelled = false;
151
+        }
152
+    }
153
+
154
+    // Now we should have a selection! We parse everything we know about it here.
155
+    glm::vec4 output = memory.rectangle->getRect();
156
+
157
+    // Lets now clear both front and back buffers before closing.
158
+    // hopefully it'll be completely transparent while closing!
159
+    glClearColor (0.0, 0.0, 0.0, 0.0);
160
+    glClear (GL_COLOR_BUFFER_BIT);
161
+    window->display();
162
+    glClear (GL_COLOR_BUFFER_BIT);
163
+    window->display();
164
+    // Then we clean up.
165
+    delete window;
166
+    delete mouse;
167
+    // Finally return the data.
168
+    return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
169
+}

+ 13
- 0
src/slop.hpp View File

22
 #define N_SLOP_H_
22
 #define N_SLOP_H_
23
 
23
 
24
 #include <string>
24
 #include <string>
25
+#include <chrono>
26
+#include <thread>
27
+
28
+#include "slopstates.hpp"
29
+#include "mouse.hpp"
30
+#include "resource.hpp"
31
+#include "keyboard.hpp"
32
+
33
+#include "window.hpp"
34
+#include "shader.hpp"
35
+#include "framebuffer.hpp"
36
+#include "glrectangle.hpp"
37
+#include "xshaperectangle.hpp"
25
 
38
 
26
 class SlopOptions {
39
 class SlopOptions {
27
 public:
40
 public:

+ 2
- 2
src/slopstates.cpp View File

1
 #include "slopstates.hpp"
1
 #include "slopstates.hpp"
2
 
2
 
3
-SlopMemory::SlopMemory( SlopOptions* options ) {
3
+SlopMemory::SlopMemory( SlopOptions* options, Rectangle* rect ) {
4
     running = true;
4
     running = true;
5
     state = (SlopState*)new SlopStart();
5
     state = (SlopState*)new SlopStart();
6
     nextState = NULL;
6
     nextState = NULL;
7
     tolerance = options->tolerance;
7
     tolerance = options->tolerance;
8
     nodecorations = options->nodecorations;
8
     nodecorations = options->nodecorations;
9
-    rectangle = new Rectangle(glm::vec2(0,0), glm::vec2(0,0), options->borderSize, options->padding, glm::vec4( options->r, options->g, options->b, options->a ), options->highlight);
9
+    rectangle = rect;
10
     selectedWindow = x11->root;
10
     selectedWindow = x11->root;
11
     state->onEnter( *this );
11
     state->onEnter( *this );
12
 }
12
 }

+ 2
- 2
src/slopstates.hpp View File

32
 #endif
32
 #endif
33
 
33
 
34
 class SlopMemory;
34
 class SlopMemory;
35
+class SlopOptions;
35
 
36
 
36
 class SlopState {
37
 class SlopState {
37
 public:
38
 public:
72
     SlopState* state;
73
     SlopState* state;
73
     SlopState* nextState;
74
     SlopState* nextState;
74
 public:
75
 public:
75
-    SlopMemory( SlopOptions* options );
76
+    SlopMemory( SlopOptions* options, Rectangle* rect );
76
     ~SlopMemory();
77
     ~SlopMemory();
77
     Window selectedWindow;
78
     Window selectedWindow;
78
     bool running;
79
     bool running;
84
     void draw( glm::mat4& matrix );
85
     void draw( glm::mat4& matrix );
85
 };
86
 };
86
 
87
 
87
-
88
 #endif // N_SLOPSTATES_H_
88
 #endif // N_SLOPSTATES_H_

+ 8
- 8
src/xshaperectangle.cpp View File

1
 #include "xshaperectangle.hpp"
1
 #include "xshaperectangle.hpp"
2
 
2
 
3
-Rectangle::Rectangle( glm::vec2 p1, glm::vec2 p2, float border, float padding, glm::vec4 color, bool highlight ) {
3
+XShapeRectangle::XShapeRectangle( glm::vec2 p1, glm::vec2 p2, float border, float padding, glm::vec4 color, bool highlight ) {
4
     this->color = convertColor( color );
4
     this->color = convertColor( color );
5
     this->border = border;
5
     this->border = border;
6
     this->padding = padding;
6
     this->padding = padding;
51
     createdWindow = false;
51
     createdWindow = false;
52
 }
52
 }
53
 
53
 
54
-void Rectangle::createWindow() {
54
+void XShapeRectangle::createWindow() {
55
     if ( createdWindow ) {
55
     if ( createdWindow ) {
56
         return;
56
         return;
57
     }
57
     }
59
     createdWindow = true;
59
     createdWindow = true;
60
 }
60
 }
61
 
61
 
62
-void Rectangle::generateHoles() {
62
+void XShapeRectangle::generateHoles() {
63
     if ( !highlight ) {
63
     if ( !highlight ) {
64
         XRectangle rects[4];
64
         XRectangle rects[4];
65
         // Left
65
         // Left
93
     XShapeCombineRectangles( x11->display, window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
93
     XShapeCombineRectangles( x11->display, window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
94
 }
94
 }
95
 
95
 
96
-void Rectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
96
+void XShapeRectangle::setPoints( glm::vec2 p1, glm::vec2 p2 ) {
97
     // Find each corner of the rectangle
97
     // Find each corner of the rectangle
98
     ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
98
     ul = glm::vec2( glm::min( p1.x, p2.x ), glm::max( p1.y, p2.y ) ) ;
99
     bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
99
     bl = glm::vec2( glm::min( p1.x, p2.x ), glm::min( p1.y, p2.y ) ) ;
112
     generateHoles();
112
     generateHoles();
113
 }
113
 }
114
 
114
 
115
-Rectangle::~Rectangle() {
115
+XShapeRectangle::~XShapeRectangle() {
116
 }
116
 }
117
 
117
 
118
-void Rectangle::draw( glm::mat4& matrix ) {
118
+void XShapeRectangle::draw( glm::mat4& matrix ) {
119
     // We don't want to be visible until we're asked to draw.
119
     // We don't want to be visible until we're asked to draw.
120
     createWindow();
120
     createWindow();
121
 }
121
 }
122
 
122
 
123
-glm::vec4 Rectangle::getRect() {
123
+glm::vec4 XShapeRectangle::getRect() {
124
     return glm::vec4( bl.x, bl.y, ur.x-ul.x, ul.y-bl.y );
124
     return glm::vec4( bl.x, bl.y, ur.x-ul.x, ul.y-bl.y );
125
 }
125
 }
126
 
126
 
127
-XColor Rectangle::convertColor( glm::vec4 color ) {
127
+XColor XShapeRectangle::convertColor( glm::vec4 color ) {
128
     // Convert float colors to shorts.
128
     // Convert float colors to shorts.
129
     short red   = short( floor( color.r * 65535.f ) );
129
     short red   = short( floor( color.r * 65535.f ) );
130
     short green = short( floor( color.g * 65535.f ) );
130
     short green = short( floor( color.g * 65535.f ) );

+ 4
- 3
src/xshaperectangle.hpp View File

30
 #include <X11/extensions/shape.h>
30
 #include <X11/extensions/shape.h>
31
 
31
 
32
 #include "x.hpp"
32
 #include "x.hpp"
33
+#include "rectangle.hpp"
33
 
34
 
34
-class Rectangle {
35
+class XShapeRectangle : public Rectangle {
35
 private:
36
 private:
36
     glm::vec2 ul, oul;
37
     glm::vec2 ul, oul;
37
     glm::vec2 bl, obl;
38
     glm::vec2 bl, obl;
49
 public:
50
 public:
50
     Window window;
51
     Window window;
51
     glm::vec4 getRect();
52
     glm::vec4 getRect();
52
-    Rectangle(glm::vec2 p1, glm::vec2 p2, float border = 1, float padding = 0, glm::vec4 color = glm::vec4(1,1,1,1), bool highlight = false );
53
-    ~Rectangle();
53
+    XShapeRectangle(glm::vec2 p1, glm::vec2 p2, float border = 1, float padding = 0, glm::vec4 color = glm::vec4(1,1,1,1), bool highlight = false );
54
+    ~XShapeRectangle();
54
     void setPoints( glm::vec2 p1, glm::vec2 p2 );
55
     void setPoints( glm::vec2 p1, glm::vec2 p2 );
55
     void draw(glm::mat4& matrix);
56
     void draw(glm::mat4& matrix);
56
 };
57
 };

+ 0
- 94
src/xslop.cpp View File

1
-#include "slop.hpp"
2
-
3
-#include "slopstates.hpp"
4
-#include "mouse.hpp"
5
-#include "resource.hpp"
6
-#include "keyboard.hpp"
7
-#include <chrono>
8
-#include <thread>
9
-#include "xshaperectangle.hpp"
10
-
11
-X11* x11;
12
-Mouse* mouse;
13
-Keyboard* keyboard;
14
-Resource* resource;
15
-
16
-// Defaults!
17
-SlopOptions::SlopOptions() {
18
-    borderSize = 1;
19
-    nodecorations = false;
20
-    tolerance = 2;
21
-    padding = 0;
22
-    highlight = false;
23
-    xdisplay = ":0";
24
-    r = 0.5;
25
-    g = 0.5;
26
-    b = 0.5;
27
-    a = 1;
28
-}
29
-
30
-SlopSelection::SlopSelection( float x, float y, float w, float h, int id ) {
31
-    this->x = x;
32
-    this->y = y;
33
-    this->w = w;
34
-    this->h = h;
35
-    this->id = id;
36
-}
37
-
38
-SlopSelection SlopSelect( SlopOptions* options, bool* cancelled ) {
39
-    bool deleteOptions = false;
40
-    if ( !options ) {
41
-        deleteOptions = true;
42
-        options = new SlopOptions();
43
-    }
44
-    resource = new Resource();
45
-    // Set up x11 temporarily
46
-    x11 = new X11(options->xdisplay);
47
-    keyboard = new Keyboard( x11 );
48
-
49
-    // Init our little state machine, memory is a tad of a misnomer
50
-    SlopMemory memory( options );
51
-    mouse = new Mouse( x11, options->nodecorations, memory.rectangle->window );
52
-
53
-    glm::mat4 fake;
54
-    // This is where we'll run through all of our stuffs
55
-    while( memory.running ) {
56
-        mouse->update();
57
-        keyboard->update();
58
-        // We move our statemachine forward.
59
-        memory.update( 1 );
60
-
61
-        // We don't actually draw anything, but the state machine uses
62
-        // this to know when to spawn the window.
63
-        memory.draw( fake );
64
-
65
-        // X11 explodes if we update as fast as possible, here's a tiny sleep.
66
-        XFlush(x11->display);
67
-        std::this_thread::sleep_for(std::chrono::milliseconds(10));
68
-
69
-        // Then we draw the framebuffer to the screen
70
-        if ( keyboard->anyKeyDown() || mouse->getButton( 3 ) ) {
71
-            memory.running = false;
72
-            if ( cancelled ) {
73
-                *cancelled = true;
74
-            }
75
-        } else {
76
-            *cancelled = false;
77
-        }
78
-    }
79
-
80
-    // Now we should have a selection! We parse everything we know about it here.
81
-    glm::vec4 output = memory.rectangle->getRect();
82
-
83
-    // Lets now clear both front and back buffers before closing.
84
-    // hopefully it'll be completely transparent while closing!
85
-    // Then we clean up.
86
-    delete mouse;
87
-    delete x11;
88
-    delete resource;
89
-    if ( deleteOptions ) {
90
-        delete options;
91
-    }
92
-    // Finally return the data.
93
-    return SlopSelection( output.x, output.y, output.z, output.w, memory.selectedWindow );
94
-}