Browse Source

Added experimental OpenGL rendering. Use --opengl to activate it.

Dalton Nell 9 years ago
parent
commit
bf50639960

+ 17
- 2
CMakeLists.txt View File

@@ -2,7 +2,7 @@ cmake_minimum_required( VERSION 2.8 )
2 2
 
3 3
 project( "slop" )
4 4
 set( slop_VERSION_MAJOR 4 )
5
-set( slop_VERSION_MINOR 1 )
5
+set( slop_VERSION_MINOR 2 )
6 6
 set( slop_VERSION_PATCH 16 )
7 7
 
8 8
 set( BIN_TARGET     "${PROJECT_NAME}" )
@@ -77,7 +77,9 @@ add_custom_target( README.md "./generateReadme.sh" DEPENDS "slop" )
77 77
 # Sources
78 78
 set( source
79 79
      src/cmdline.c
80
-     src/rectangle.cpp
80
+     src/selectrectangle.cpp
81
+     src/glselectrectangle.cpp
82
+     src/xselectrectangle.cpp
81 83
      src/x.cpp
82 84
      src/main.cpp )
83 85
 
@@ -85,6 +87,9 @@ set( source
85 87
 set( CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmakemodules" )
86 88
 find_package( X11       REQUIRED )
87 89
 find_package( XExt      REQUIRED )
90
+find_package( OpenGL    REQUIRED )
91
+find_package( GLX       REQUIRED )
92
+find_package( XRender   REQUIRED )
88 93
 # This library is needed only for Ubuntu it seems, some platforms don't even
89 94
 # ship with it. I couldn't find a way to do a test compile to check if librt
90 95
 # was needed, so instead I just didn't mark it as REQUIRED.
@@ -97,9 +102,14 @@ set( CMAKE_CXX_FLAGS
97 102
 if ( RT_INCLUDE_DIR )
98 103
     include_directories( ${X11_INCLUDE_DIR}
99 104
                          ${XEXT_INCLUDE_DIR}
105
+                         ${OPENGL_INCLUDE_DIR}
106
+                         ${GLX_INCLUDE_DIR}
107
+                         ${XRENDER_INCLUDE_DIRS}
100 108
                          ${RT_INCLUDE_DIR} )
101 109
 else()
102 110
     include_directories( ${X11_INCLUDE_DIR}
111
+                         ${OPENGL_INCLUDE_DIR}
112
+                         ${GLX_INCLUDE_DIR}
103 113
                          ${XEXT_INCLUDE_DIR} )
104 114
 endif()
105 115
 
@@ -110,10 +120,15 @@ add_executable( "${BIN_TARGET}" ${source} )
110 120
 if ( RT_LIBRARY )
111 121
     target_link_libraries( "${BIN_TARGET}"
112 122
                            ${X11_LIBRARIES}
123
+                           ${OPENGL_LIBRARIES}
124
+                           ${GLX_LIBRARIES}
125
+                           ${XRENDER_LIBRARIES}
113 126
                            "${XEXT_LIBRARY}"
114 127
                            "${RT_LIBRARY}" )
115 128
 else()
116 129
     target_link_libraries( "${BIN_TARGET}"
130
+                           ${OPENGL_LIBRARIES}
131
+                           ${GLX_LIBRARIES}
117 132
                            ${X11_LIBRARIES}
118 133
                            "${XEXT_LIBRARY}" )
119 134
 endif()

+ 4
- 1
README.md View File

@@ -87,7 +87,7 @@ Make sure to check out and install [maim](https://github.com/naelstrof/maim) too
87 87
 help
88 88
 ----
89 89
 ```text
90
-slop v4.1.16
90
+slop v4.2.16
91 91
 
92 92
 Copyright (C) 2014 Dalton Nell, Slop Contributors
93 93
 (https://github.com/naelstrof/slop/graphs/contributors)
@@ -134,6 +134,9 @@ Options
134 134
                                   highlights it. This is only useful when
135 135
                                   --color is set to a transparent color.
136 136
                                   (default=off)
137
+      --opengl                  Enable hardware acceleration. Only works with
138
+                                  modern systems that are also running a
139
+                                  compositor.  (default=off)
137 140
   -f, --format=STRING           Set the output format string. Format specifiers
138 141
                                   are %x, %y, %w, %h, %i, %g, and %c.
139 142
                                   (default=`X=%x\nY=%y\nW=%w\nH=%h\nG=%g\nID=%i\nCancel=%c\n')

+ 20
- 1
src/cmdline.c View File

@@ -48,6 +48,7 @@ const char *gengetopt_args_info_help[] = {
48 48
   "      --min=INT                 Set the minimum output of width or height\n                                  values. This is useful to avoid outputting 0.\n                                  Setting min and max to the same value\n                                  disables drag selections.  (default=`0')",
49 49
   "      --max=INT                 Set the maximum output of width or height\n                                  values. Setting min and max to the same value\n                                  disables drag selections.  (default=`0')",
50 50
   "  -l, --highlight               Instead of outlining selections, slop\n                                  highlights it. This is only useful when\n                                  --color is set to a transparent color.\n                                  (default=off)",
51
+  "      --opengl                  Enable hardware acceleration. Only works with\n                                  modern systems that are also running a\n                                  compositor.  (default=off)",
51 52
   "  -f, --format=STRING           Set the output format string. Format specifiers\n                                  are %x, %y, %w, %h, %i, %g, and %c.\n                                  (default=`X=%x\\nY=%y\\nW=%w\\nH=%h\\nG=%g\\nID=%i\\nCancel=%c\\n')",
52 53
   "\nExamples\n    $ # Gray, thick, transparent border for maximum visiblity.\n    $ slop -b 20 -c 0.5,0.5,0.5,0.8\n\n    $ # Remove window decorations.\n    $ slop --nodecorations\n\n    $ # Disable window selections. Useful for selecting individual pixels.\n    $ slop -t 0\n\n    $ # Classic Windows XP selection.\n    $ slop -l -c 0.3,0.4,0.6,0.4\n\n    $ # Change output format to use safer parsing\n    $ slopoutput=$(slop -f \"%x %y %w %h\")\n    $ X=$(echo $slopoutput | awk '{print $1}')\n    $ Y=$(echo $slopoutput | awk '{print $2}')\n    $ W=$(echo $slopoutput | awk '{print $3}')\n    $ H=$(echo $slopoutput | awk '{print $4}')\n\nTips\n    * You can use the arrow keys to move the starting point of a\ndrag-selection, just in case you missed it by a few pixels.\n    * If you don't like a selection: you can cancel it by right-clicking\nregardless of which options are enabled or disabled for slop.\n    * If slop doesn't seem to select a window accurately, the problem could be\nbecause of decorations getting in the way. Try enabling the --nodecorations\nflag.\n",
53 54
     0
@@ -88,6 +89,7 @@ void clear_given (struct gengetopt_args_info *args_info)
88 89
   args_info->min_given = 0 ;
89 90
   args_info->max_given = 0 ;
90 91
   args_info->highlight_given = 0 ;
92
+  args_info->opengl_given = 0 ;
91 93
   args_info->format_given = 0 ;
92 94
 }
93 95
 
@@ -114,6 +116,7 @@ void clear_args (struct gengetopt_args_info *args_info)
114 116
   args_info->max_arg = 0;
115 117
   args_info->max_orig = NULL;
116 118
   args_info->highlight_flag = 0;
119
+  args_info->opengl_flag = 0;
117 120
   args_info->format_arg = gengetopt_strdup ("X=%x\nY=%y\nW=%w\nH=%h\nG=%g\nID=%i\nCancel=%c\n");
118 121
   args_info->format_orig = NULL;
119 122
   
@@ -137,7 +140,8 @@ void init_args_info(struct gengetopt_args_info *args_info)
137 140
   args_info->min_help = gengetopt_args_info_help[11] ;
138 141
   args_info->max_help = gengetopt_args_info_help[12] ;
139 142
   args_info->highlight_help = gengetopt_args_info_help[13] ;
140
-  args_info->format_help = gengetopt_args_info_help[14] ;
143
+  args_info->opengl_help = gengetopt_args_info_help[14] ;
144
+  args_info->format_help = gengetopt_args_info_help[15] ;
141 145
   
142 146
 }
143 147
 
@@ -290,6 +294,8 @@ cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
290 294
     write_into_file(outfile, "max", args_info->max_orig, 0);
291 295
   if (args_info->highlight_given)
292 296
     write_into_file(outfile, "highlight", 0, 0 );
297
+  if (args_info->opengl_given)
298
+    write_into_file(outfile, "opengl", 0, 0 );
293 299
   if (args_info->format_given)
294 300
     write_into_file(outfile, "format", args_info->format_orig, 0);
295 301
   
@@ -559,6 +565,7 @@ cmdline_parser_internal (
559 565
         { "min",	1, NULL, 0 },
560 566
         { "max",	1, NULL, 0 },
561 567
         { "highlight",	0, NULL, 'l' },
568
+        { "opengl",	0, NULL, 0 },
562 569
         { "format",	1, NULL, 'f' },
563 570
         { 0,  0, 0, 0 }
564 571
       };
@@ -726,6 +733,18 @@ cmdline_parser_internal (
726 733
                 additional_error))
727 734
               goto failure;
728 735
           
736
+          }
737
+          /* Enable hardware acceleration. Only works with modern systems that are also running a compositor..  */
738
+          else if (strcmp (long_options[option_index].name, "opengl") == 0)
739
+          {
740
+          
741
+          
742
+            if (update_arg((void *)&(args_info->opengl_flag), 0, &(args_info->opengl_given),
743
+                &(local_args_info.opengl_given), optarg, 0, 0, ARG_FLAG,
744
+                check_ambiguity, override, 1, 0, "opengl", '-',
745
+                additional_error))
746
+              goto failure;
747
+          
729 748
           }
730 749
           
731 750
           break;

+ 3
- 0
src/cmdline.in View File

@@ -69,6 +69,8 @@ struct gengetopt_args_info
69 69
   const char *max_help; /**< @brief Set the maximum output of width or height values. Setting min and max to the same value disables drag selections. help description.  */
70 70
   int highlight_flag;	/**< @brief Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color. (default=off).  */
71 71
   const char *highlight_help; /**< @brief Instead of outlining selections, slop highlights it. This is only useful when --color is set to a transparent color. help description.  */
72
+  int opengl_flag;	/**< @brief Enable hardware acceleration. Only works with modern systems that are also running a compositor. (default=off).  */
73
+  const char *opengl_help; /**< @brief Enable hardware acceleration. Only works with modern systems that are also running a compositor. help description.  */
72 74
   char * format_arg;	/**< @brief Set the output format string. Format specifiers are %x, %y, %w, %h, %i, %g, and %c. (default='REPLACEME').  */
73 75
   char * format_orig;	/**< @brief Set the output format string. Format specifiers are %x, %y, %w, %h, %i, %g, and %c. original value given at command line.  */
74 76
   const char *format_help; /**< @brief Set the output format string. Format specifiers are %x, %y, %w, %h, %i, %g, and %c. help description.  */
@@ -86,6 +88,7 @@ struct gengetopt_args_info
86 88
   unsigned int min_given ;	/**< @brief Whether min was given.  */
87 89
   unsigned int max_given ;	/**< @brief Whether max was given.  */
88 90
   unsigned int highlight_given ;	/**< @brief Whether highlight was given.  */
91
+  unsigned int opengl_given ;	/**< @brief Whether opengl was given.  */
89 92
   unsigned int format_given ;	/**< @brief Whether format was given.  */
90 93
 
91 94
 } ;

+ 204
- 0
src/glselectrectangle.cpp View File

@@ -0,0 +1,204 @@
1
+/* glrectangle.hpp: Handles creating hardware accelerated rectangles on the screen using X11 and OpenGL.
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
+#include "glselectrectangle.hpp"
21
+
22
+static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
23
+    return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
24
+}
25
+
26
+slop::GLSelectRectangle::~GLSelectRectangle() {
27
+    if ( m_window == None ) {
28
+        return;
29
+    }
30
+    // Try to erase the window before destroying it.
31
+    glClearColor( 0, 0, 0, 0 );
32
+    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
33
+    glXSwapBuffers( xengine->m_display, m_glxWindow );
34
+    // Sleep for 0.1 seconds in hope that the rectangle was erased.
35
+    usleep( 10000 );
36
+    XDestroyWindow( xengine->m_display, m_window );
37
+    XEvent event;
38
+    // Block until the window is actually completely removed.
39
+    XIfEvent( xengine->m_display, &event, &isDestroyNotify, (XPointer)&m_window );
40
+    // Sleep for 0.1 seconds in hope that the screen actually cleared the window.
41
+    usleep( 10000 );
42
+}
43
+
44
+slop::GLSelectRectangle::GLSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
45
+    m_x = std::min( sx, ex );
46
+    m_y = std::min( sy, ey );
47
+    m_width = std::max( sx, ex ) - m_x;
48
+    m_height = std::max( sy, ey ) - m_y;
49
+    m_r = r;
50
+    m_g = g;
51
+    m_b = b;
52
+    m_a = a;
53
+    m_border = border;
54
+    m_window = None;
55
+    m_highlight = highlight;
56
+
57
+    // If we don't have a border, we don't exist, so just die.
58
+    if ( m_border == 0 ) {
59
+        return;
60
+    }
61
+
62
+    if ( m_highlight ) {
63
+        m_border = 0;
64
+    }
65
+
66
+    static int visdata[] = {
67
+        GLX_RENDER_TYPE, GLX_RGBA_BIT,
68
+        GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
69
+        GLX_DOUBLEBUFFER, True,
70
+        GLX_RED_SIZE, 8,
71
+        GLX_GREEN_SIZE, 8,
72
+        GLX_BLUE_SIZE, 8,
73
+        GLX_ALPHA_SIZE, 8,
74
+        GLX_DEPTH_SIZE, 16,
75
+        None
76
+    };
77
+
78
+    int numfbconfigs = 0;
79
+    GLXFBConfig* fbconfigs = glXChooseFBConfig( xengine->m_display,  DefaultScreen( xengine->m_display ), visdata, &numfbconfigs );
80
+    m_fbconfig = 0;
81
+    for ( int i=0; i<numfbconfigs; i++ ) {
82
+        m_visual = (XVisualInfo*)glXGetVisualFromFBConfig( xengine->m_display, fbconfigs[i] );
83
+        if ( !m_visual ) {
84
+            continue;
85
+        }
86
+        m_pictFormat = XRenderFindVisualFormat( xengine->m_display, m_visual->visual );
87
+        if ( !m_pictFormat ) {
88
+            continue;
89
+        }
90
+        m_fbconfig = fbconfigs[i];
91
+        if ( m_pictFormat->direct.alphaMask > 0 ) {
92
+            break;
93
+        }
94
+    }
95
+
96
+    if ( !m_fbconfig ) {
97
+        fprintf( stderr, "Couldn't find a matching FB config for a transparent OpenGL window!\n");
98
+    }
99
+
100
+    m_cmap = XCreateColormap( xengine->m_display, xengine->m_root, m_visual->visual, AllocNone );
101
+
102
+    XSetWindowAttributes attributes;
103
+    attributes.colormap = m_cmap;
104
+    attributes.background_pixmap = None;
105
+    attributes.border_pixmap = None;
106
+    attributes.border_pixel = 0;
107
+    // Disable window decorations.
108
+    attributes.override_redirect = True;
109
+    // Make sure we know when we've been successfully destroyed later!
110
+    attributes.event_mask = StructureNotifyMask;
111
+    unsigned long valueMask = CWOverrideRedirect | CWEventMask | CWBackPixmap | CWColormap | CWBorderPixel;
112
+
113
+
114
+    // Create the window
115
+    m_window = XCreateWindow( xengine->m_display, xengine->m_root, 0, 0, xengine->getWidth(), xengine->getHeight(),
116
+                              0, m_visual->depth, InputOutput,
117
+                              m_visual->visual, valueMask, &attributes );
118
+
119
+    if ( !m_window ) {
120
+        fprintf( stderr, "Couldn't create a GL window!\n");
121
+    }
122
+
123
+    m_glxWindow = m_window;
124
+
125
+    static char title[] = "OpenGL Slop";
126
+    XWMHints* startup_state = XAllocWMHints();
127
+    startup_state->initial_state = NormalState;
128
+    startup_state->flags = StateHint;
129
+    XTextProperty textprop;
130
+    textprop.value = (unsigned char*)title;
131
+    textprop.encoding = XA_STRING;
132
+    textprop.format = 8;
133
+    textprop.nitems = strlen( title );
134
+    XSizeHints sizehints;
135
+    sizehints.x = 0;
136
+    sizehints.y = 0;
137
+    sizehints.width = xengine->getWidth();
138
+    sizehints.height = xengine->getHeight();
139
+    sizehints.flags = USPosition | USSize;
140
+    XClassHint classhints;
141
+    char name[] = "slop";
142
+    classhints.res_name = name;
143
+    classhints.res_class = name;
144
+    XSetClassHint( xengine->m_display, m_window, &classhints );
145
+    XSetWMProperties( xengine->m_display, m_window, &textprop, &textprop, NULL, 0, &sizehints, startup_state, NULL );
146
+    XFree( startup_state );
147
+
148
+    // Make it so all input falls through
149
+    XRectangle rect;
150
+    rect.x = rect.y = rect.width = rect.height = 0;
151
+    XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
152
+
153
+    XMapWindow( xengine->m_display, m_window );
154
+
155
+    int dummy;
156
+    if ( !glXQueryExtension( xengine->m_display, &dummy, &dummy ) ) {
157
+        fprintf( stderr, "OpenGL is not supported!\n" );
158
+    }
159
+    m_renderContext = glXCreateNewContext( xengine->m_display, m_fbconfig, GLX_RGBA_TYPE, 0, True );
160
+    if ( !m_renderContext ) {
161
+        fprintf( stderr, "Failed to create a GL context.\n" );
162
+    }
163
+    if ( !glXMakeContextCurrent( xengine->m_display, m_glxWindow, m_glxWindow, m_renderContext ) ) {
164
+        fprintf( stderr, "Failed to attach GL context to window!\n" );
165
+    }
166
+    glClearColor( 0, 0, 0, 0 );
167
+    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
168
+    glXSwapBuffers( xengine->m_display, m_glxWindow );
169
+}
170
+
171
+void slop::GLSelectRectangle::setGeo( int sx, int sy, int ex, int ey ) {
172
+    int x = std::min( sx, ex );
173
+    int y = std::min( sy, ey );
174
+    int w = std::max( sx, ex ) - x;
175
+    int h = std::max( sy, ey ) - y;
176
+
177
+    m_x = x;
178
+    m_y = y;
179
+    m_width = w;
180
+    m_height = h;
181
+    glDrawBuffer( GL_BACK );
182
+    glViewport( 0, 0, xengine->getWidth(), xengine->getHeight() );
183
+
184
+    glClearColor( 0, 0, 0, 0 );
185
+    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
186
+
187
+    glMatrixMode( GL_PROJECTION );
188
+    glLoadIdentity();
189
+    glOrtho( 0, xengine->getWidth(), xengine->getHeight(), 0, 1, -1 );
190
+
191
+    glMatrixMode( GL_MODELVIEW );
192
+    glLoadIdentity();
193
+
194
+    glEnable( GL_BLEND );
195
+    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
196
+
197
+    glColor4f( m_r, m_g, m_b, m_a );
198
+    glRecti( m_x-m_border, m_y, m_x+m_width+m_border, m_y-m_border );
199
+    glRecti( m_x-m_border, m_y+m_height, m_x+m_width+m_border, m_y+m_height+m_border );
200
+    glRecti( m_x-m_border, m_y, m_x, m_y+m_height );
201
+    glRecti( m_x+m_width, m_y, m_x+m_width+m_border, m_y+m_height );
202
+
203
+    glXSwapBuffers( xengine->m_display, m_glxWindow );
204
+}

+ 64
- 0
src/glselectrectangle.hpp View File

@@ -0,0 +1,64 @@
1
+/* glselectrectangle.hpp: Handles creating hardware accelerated rectangles on the screen using X11 and OpenGL.
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 IS_GL_SELECT_RECTANGLE_H_
22
+#define IS_GL_SELECT_RECTANGLE_H_
23
+
24
+#include "x.hpp"
25
+#include "selectrectangle.hpp"
26
+
27
+#include <unistd.h>
28
+
29
+#include <GL/gl.h>
30
+#include <GL/glx.h>
31
+#include <GL/glxext.h>
32
+#include <X11/Xlib.h>
33
+#include <X11/Xatom.h>
34
+#include <X11/extensions/Xrender.h>
35
+#include <X11/Xutil.h>
36
+
37
+#include <cstdlib>
38
+#include <cmath>
39
+#include <cstdio>
40
+#include <string>
41
+#include <vector>
42
+
43
+namespace slop {
44
+
45
+class GLSelectRectangle: public SelectRectangle {
46
+public:
47
+                        GLSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a );
48
+                        ~GLSelectRectangle();
49
+    void                setGeo( int x, int y, int w, int h );
50
+    float               m_r;
51
+    float               m_g;
52
+    float               m_b;
53
+    float               m_a;
54
+    GLXFBConfig         m_fbconfig;
55
+    XVisualInfo*        m_visual;
56
+    XRenderPictFormat*  m_pictFormat;
57
+    GLXContext          m_renderContext;
58
+    GLXWindow           m_glxWindow;
59
+    Colormap            m_cmap;
60
+};
61
+
62
+}
63
+
64
+#endif // IS_GL_SELECT_RECTANGLE_H_

+ 34
- 17
src/main.cpp View File

@@ -28,7 +28,9 @@
28 28
 #endif
29 29
 
30 30
 #include "x.hpp"
31
-#include "rectangle.hpp"
31
+#include "selectrectangle.hpp"
32
+#include "glselectrectangle.hpp"
33
+#include "xselectrectangle.hpp"
32 34
 #include "cmdline.h"
33 35
 
34 36
 // Work around lack of clock_gettime in OSX
@@ -210,7 +212,8 @@ int app( int argc, char** argv ) {
210 212
     }
211 213
     int state = 0;
212 214
     bool running = true;
213
-    slop::Rectangle* selection = NULL;
215
+    bool opengl = options.opengl_flag;
216
+    slop::SelectRectangle* selection = NULL;
214 217
     Window window = None;
215 218
     Window windowmemory = None;
216 219
     std::string xdisplay;
@@ -272,7 +275,7 @@ int app( int argc, char** argv ) {
272 275
         printSelection( format, true, 0, 0, 0, 0, None );
273 276
         return EXIT_FAILURE;
274 277
     }
275
-    if ( !slop::isRectangleSupported() ) {
278
+    if ( !slop::isSelectRectangleSupported() ) {
276 279
         fprintf( stderr, "Error: Your X server doesn't support the XShape extension. There's nothing slop can do about this!\n" );
277 280
         fprintf( stderr, "  Try updating X and making sure you have XExtensions installed. (/usr/lib/libXext.so, /usr/include/X11/extensions/shape.h)\n" );
278 281
         return EXIT_FAILURE;
@@ -346,13 +349,21 @@ int app( int argc, char** argv ) {
346 349
                     t.applyMinMaxSize( minimumsize, maximumsize );
347 350
                     // Make sure we only apply offsets to windows that we've forcibly removed decorations on.
348 351
                     if ( !selection ) {
349
-                        selection = new slop::Rectangle( t.m_x,
350
-                                                         t.m_y,
351
-                                                         t.m_x + t.m_width,
352
-                                                         t.m_y + t.m_height,
353
-                                                         borderSize,
354
-                                                         highlight,
355
-                                                         r, g, b, a );
352
+                        if ( opengl ) {
353
+                            selection = new slop::GLSelectRectangle( t.m_x, t.m_y,
354
+                                                                     t.m_x + t.m_width,
355
+                                                                     t.m_y + t.m_height,
356
+                                                                     borderSize,
357
+                                                                     highlight,
358
+                                                                     r, g, b, a );
359
+                        } else {
360
+                            selection = new slop::XSelectRectangle( t.m_x, t.m_y,
361
+                                                                    t.m_x + t.m_width,
362
+                                                                    t.m_y + t.m_height,
363
+                                                                    borderSize,
364
+                                                                    highlight,
365
+                                                                    r, g, b, a );
366
+                        }
356 367
                     } else {
357 368
                         selection->setGeo( t.m_x, t.m_y, t.m_x + t.m_width, t.m_y + t.m_height );
358 369
                     }
@@ -391,13 +402,19 @@ int app( int argc, char** argv ) {
391 402
                 if ( !selection ) {
392 403
                     int sx, sy, ex, ey;
393 404
                     constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
394
-                    selection = new slop::Rectangle( sx,
395
-                                                     sy,
396
-                                                     ex,
397
-                                                     ey,
398
-                                                     borderSize,
399
-                                                     highlight,
400
-                                                     r, g, b, a );
405
+                    if ( opengl ) {
406
+                        selection = new slop::GLSelectRectangle( sx, sy,
407
+                                                                 ex, ey,
408
+                                                                 borderSize,
409
+                                                                 highlight,
410
+                                                                 r, g, b, a );
411
+                    } else {
412
+                        selection = new slop::XSelectRectangle( sx, sy,
413
+                                                                ex, ey,
414
+                                                                borderSize,
415
+                                                                highlight,
416
+                                                                r, g, b, a );
417
+                    }
401 418
                 }
402 419
                 windowmemory = window;
403 420
                 // If the user has let go of the mouse button, we'll just

+ 4
- 0
src/options.ggo View File

@@ -60,6 +60,10 @@ option "highlight" l "Instead of outlining selections, slop highlights it. This
60 60
     flag
61 61
     off
62 62
 
63
+option "opengl" - "Enable hardware acceleration. Only works with modern systems that are also running a compositor."
64
+    flag
65
+    off
66
+
63 67
 option "format" f "Set the output format string. Format specifiers are %x, %y, %w, %h, %i, %g, and %c."
64 68
     string
65 69
     default="REPLACEME"

+ 33
- 0
src/selectrectangle.cpp View File

@@ -0,0 +1,33 @@
1
+/* selectrectangle.cpp: Handles creating rectangles on the screen.
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
+#include "selectrectangle.hpp"
21
+
22
+bool slop::isSelectRectangleSupported() {
23
+    int event_base;
24
+    int error_base;
25
+    return XShapeQueryExtension( xengine->m_display, &event_base, &error_base );
26
+}
27
+
28
+slop::SelectRectangle::~SelectRectangle() {
29
+}
30
+
31
+void slop::SelectRectangle::setGeo( int sx, int sy, int ex, int ey ) {
32
+    fprintf( stderr, "Tried to use a class function that's meant to be overridden!\n");
33
+}

+ 58
- 0
src/selectrectangle.hpp View File

@@ -0,0 +1,58 @@
1
+/* selectrectangle.hpp: Handles creating rectangles on the screen.
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 IS_SELECT_RECTANGLE_H_
22
+#define IS_SELECT_RECTANGLE_H_
23
+
24
+#include "x.hpp"
25
+#include "selectrectangle.hpp"
26
+
27
+#include <unistd.h>
28
+
29
+#include <X11/Xlib.h>
30
+#include <X11/extensions/shape.h>
31
+#include <X11/Xatom.h>
32
+
33
+#include <cstdlib>
34
+#include <cmath>
35
+#include <cstdio>
36
+#include <string>
37
+#include <vector>
38
+
39
+namespace slop {
40
+
41
+class SelectRectangle {
42
+public:
43
+    virtual ~SelectRectangle();
44
+    virtual void    setGeo( int x, int y, int w, int h );
45
+    Window          m_window;
46
+    int             m_x;
47
+    int             m_y;
48
+    int             m_width;
49
+    int             m_height;
50
+    int             m_border;
51
+    bool            m_highlight;
52
+};
53
+
54
+bool isSelectRectangleSupported();
55
+
56
+}
57
+
58
+#endif // IS_SELECT_RECTANGLE_H_

src/rectangle.cpp → src/xselectrectangle.cpp View File

@@ -1,4 +1,4 @@
1
-/* rectangle.cpp: Handles creating rectangles on the screen.
1
+/* xselectrectangle.cpp: Handles creating rectangles on the screen in pure X11.
2 2
  *
3 3
  * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4 4
  *
@@ -17,13 +17,13 @@
17 17
  * You should have received a copy of the GNU General Public License
18 18
  * along with Slop.  If not, see <http://www.gnu.org/licenses/>.
19 19
  */
20
-#include "rectangle.hpp"
20
+#include "xselectrectangle.hpp"
21 21
 
22 22
 static Bool isDestroyNotify( Display* dpy, XEvent* ev, XPointer win ) {
23 23
     return ev->type == DestroyNotify && ev->xdestroywindow.window == *((Window*)win);
24 24
 }
25 25
 
26
-slop::Rectangle::~Rectangle() {
26
+slop::XSelectRectangle::~XSelectRectangle() {
27 27
     if ( m_window == None ) {
28 28
         return;
29 29
     }
@@ -42,7 +42,7 @@ slop::Rectangle::~Rectangle() {
42 42
     usleep( 10000 );
43 43
 }
44 44
 
45
-slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
45
+slop::XSelectRectangle::XSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a ) {
46 46
     m_x = std::min( sx, ex );
47 47
     m_y = std::min( sy, ey );
48 48
     m_width = std::max( sx, ex ) - m_x;
@@ -128,7 +128,7 @@ slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool hig
128 128
     XMapWindow( xengine->m_display, m_window );
129 129
 }
130 130
 
131
-void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
131
+void slop::XSelectRectangle::setGeo( int sx, int sy, int ex, int ey ) {
132 132
     int x = std::min( sx, ex );
133 133
     int y = std::min( sy, ey );
134 134
     int w = std::max( sx, ex ) - x;
@@ -172,7 +172,7 @@ void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
172 172
     }
173 173
 }
174 174
 
175
-XColor slop::Rectangle::convertColor( float r, float g, float b ) {
175
+XColor slop::XSelectRectangle::convertColor( float r, float g, float b ) {
176 176
     // Convert float colors to shorts.
177 177
     short red   = short( floor( r * 65535.f ) );
178 178
     short green = short( floor( g * 65535.f ) );
@@ -187,9 +187,3 @@ XColor slop::Rectangle::convertColor( float r, float g, float b ) {
187 187
     }
188 188
     return color;
189 189
 }
190
-
191
-bool slop::isRectangleSupported() {
192
-    int event_base;
193
-    int error_base;
194
-    return XShapeQueryExtension( xengine->m_display, &event_base, &error_base );
195
-}

src/rectangle.hpp → src/xselectrectangle.hpp View File

@@ -1,4 +1,4 @@
1
-/* rectangle.hpp: Handles creating rectangles on the screen.
1
+/* xselectrectangle.hpp: Handles creating rectangles on the screen in pure X11.
2 2
  *
3 3
  * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
4 4
  *
@@ -18,10 +18,11 @@
18 18
  * along with Slop.  If not, see <http://www.gnu.org/licenses/>.
19 19
  */
20 20
 
21
-#ifndef IS_RECTANGLE_H_
22
-#define IS_RECTANGLE_H_
21
+#ifndef IS_X_SELECT_RECTANGLE_H_
22
+#define IS_X_SELECT_RECTANGLE_H_
23 23
 
24 24
 #include "x.hpp"
25
+#include "selectrectangle.hpp"
25 26
 
26 27
 #include <unistd.h>
27 28
 
@@ -37,10 +38,10 @@
37 38
 
38 39
 namespace slop {
39 40
 
40
-class Rectangle {
41
+class XSelectRectangle: public SelectRectangle {
41 42
 public:
42
-            Rectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a );
43
-            ~Rectangle();
43
+            XSelectRectangle( int sx, int sy, int ex, int ey, int border, bool highlight, float r, float g, float b, float a );
44
+            ~XSelectRectangle();
44 45
     void    setPos( int x, int y );
45 46
     void    setDim( int w, int h );
46 47
     void    setGeo( int x, int y, int w, int h );
@@ -57,8 +58,6 @@ private:
57 58
     void    constrain( int w, int h );
58 59
 };
59 60
 
60
-bool isRectangleSupported();
61
-
62 61
 }
63 62
 
64
-#endif // IS_RECTANGLE_H_
63
+#endif // IS_X_SELECT_RECTANGLE_H_