Browse Source

Improved rendering speed and accuracy.

Dalton Nell 10 years ago
parent
commit
e66dba979e
5 changed files with 81 additions and 57 deletions
  1. 6
    5
      main.cpp
  2. 2
    2
      options.cpp
  3. 71
    37
      rectangle.cpp
  4. 1
    1
      rectangle.hpp
  5. 1
    12
      x.cpp

+ 6
- 5
main.cpp View File

203
             case 2: {
203
             case 2: {
204
                 // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
204
                 // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
205
                 if ( !selection ) {
205
                 if ( !selection ) {
206
-                    selection = new slop::Rectangle( cx,
207
-                                                     cy,
208
-                                                     // We add one because pixels start at 0
209
-                                                     xengine->m_mousex + 1,
210
-                                                     xengine->m_mousey + 1,
206
+                    int sx, sy, ex, ey;
207
+                    constrain( cx, cy, xengine->m_mousex, xengine->m_mousey, padding, minimumsize, maximumsize, &sx, &sy, &ex, &ey );
208
+                    selection = new slop::Rectangle( sx,
209
+                                                     sy,
210
+                                                     ex,
211
+                                                     ey,
211
                                                      borderSize,
212
                                                      borderSize,
212
                                                      highlight,
213
                                                      highlight,
213
                                                      r, g, b, a );
214
                                                      r, g, b, a );

+ 2
- 2
options.cpp View File

3
 slop::Options* options = new slop::Options();
3
 slop::Options* options = new slop::Options();
4
 
4
 
5
 slop::Options::Options() {
5
 slop::Options::Options() {
6
-    m_version = "v2.1.0";
6
+    m_version = "v2.1.1";
7
     m_highlight = false;
7
     m_highlight = false;
8
     m_borderSize = 10;
8
     m_borderSize = 10;
9
     m_padding = 0;
9
     m_padding = 0;
55
     printf( "    $ slop -t=0\n" );
55
     printf( "    $ slop -t=0\n" );
56
     printf( "\n" );
56
     printf( "\n" );
57
     printf( "    $ # Classic Windows XP selection.\n" );
57
     printf( "    $ # Classic Windows XP selection.\n" );
58
-    printf( "    $ slop -hi -c=0.2,0.4,0.5,0.4\n" );
58
+    printf( "    $ slop -hi -c=0.3,0.4,0.6,0.4\n" );
59
 }
59
 }
60
 
60
 
61
 int slop::Options::parseOptions( int argc, char** argv ) {
61
 int slop::Options::parseOptions( int argc, char** argv ) {

+ 71
- 37
rectangle.cpp View File

8
     if ( m_window == None ) {
8
     if ( m_window == None ) {
9
         return;
9
         return;
10
     }
10
     }
11
+    // Try to erase the window before destroying it.
12
+    XRectangle rect;
13
+    rect.x = 0;
14
+    rect.y = 0;
15
+    rect.width = 0;
16
+    rect.height = 0;
17
+    XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
18
+    // Sleep for 0.1 seconds in hope that the rectangle was erased.
19
+    usleep( 10000 );
11
     // Free up our color.
20
     // Free up our color.
12
     XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
21
     XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
13
     XDestroyWindow( xengine->m_display, m_window );
22
     XDestroyWindow( xengine->m_display, m_window );
36
         m_border = 0;
45
         m_border = 0;
37
     }
46
     }
38
 
47
 
39
-    // This sets up m_color
40
-    int err = convertColor( r, g, b );
41
-    if ( err ) {
42
-        fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
43
-    }
48
+    m_color = convertColor( r, g, b );
44
     XSetWindowAttributes attributes;
49
     XSetWindowAttributes attributes;
45
     // Set up the window so it's our color 
50
     // Set up the window so it's our color 
46
-    attributes.background_pixmap = None;
47
     attributes.background_pixel = m_color.pixel;
51
     attributes.background_pixel = m_color.pixel;
48
-    attributes.border_pixel = m_color.pixel;
49
-    // Not actually sure what this does, but it keeps the window from bugging out :u.
52
+    // Disable window decorations.
50
     attributes.override_redirect = True;
53
     attributes.override_redirect = True;
51
-    // We must use our color map, because that's where our color is allocated.
52
-    attributes.colormap = xengine->m_colormap;
53
     // Make sure we know when we've been successfully destroyed later!
54
     // Make sure we know when we've been successfully destroyed later!
54
     attributes.event_mask = StructureNotifyMask;
55
     attributes.event_mask = StructureNotifyMask;
55
-    unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
56
+    unsigned long valueMask = CWBackPixel | CWOverrideRedirect | CWEventMask;
56
 
57
 
57
     // Create the window
58
     // Create the window
58
-    m_window = XCreateWindow( xengine->m_display, xengine->m_root, m_x-m_border, m_y-m_border, m_width+m_border*2, m_height+m_border*2,
59
+    m_window = XCreateWindow( xengine->m_display, xengine->m_root, 0, 0, WidthOfScreen( xengine->m_screen ), HeightOfScreen( xengine->m_screen ),
59
                               0, CopyFromParent, InputOutput,
60
                               0, CopyFromParent, InputOutput,
60
                               CopyFromParent, valueMask, &attributes );
61
                               CopyFromParent, valueMask, &attributes );
61
 
62
 
63
+
62
     if ( a < 1 ) {
64
     if ( a < 1 ) {
65
+        // Change the window opacity
63
         unsigned int cardinal_alpha = (unsigned int) (a * (unsigned int)-1) ;
66
         unsigned int cardinal_alpha = (unsigned int) (a * (unsigned int)-1) ;
64
         XChangeProperty( xengine->m_display, m_window, XInternAtom( xengine->m_display, "_NET_WM_WINDOW_OPACITY", 0),
67
         XChangeProperty( xengine->m_display, m_window, XInternAtom( xengine->m_display, "_NET_WM_WINDOW_OPACITY", 0),
65
                          XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&cardinal_alpha, 1 );
68
                          XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&cardinal_alpha, 1 );
73
 
76
 
74
     // Now punch a hole into it so it looks like a selection rectangle, but only if we're not highlighting.
77
     // Now punch a hole into it so it looks like a selection rectangle, but only if we're not highlighting.
75
     if ( !m_highlight ) {
78
     if ( !m_highlight ) {
79
+        XRectangle rects[4];
80
+        // Left
81
+        rects[0].x = m_x-m_border;
82
+        rects[0].y = m_y-m_border;
83
+        rects[0].width = m_border;
84
+        rects[0].height = m_height+m_border*2;
85
+        // Top
86
+        rects[1].x = m_x;
87
+        rects[1].y = m_y-m_border;
88
+        rects[1].width = m_width+m_border;
89
+        rects[1].height = m_border;
90
+        // Right
91
+        rects[2].x = m_x+m_width;
92
+        rects[2].y = m_y-m_border;
93
+        rects[2].width = m_border;
94
+        rects[2].height = m_height+m_border*2;
95
+        // Bottom
96
+        rects[3].x = m_x;
97
+        rects[3].y = m_y+m_height;
98
+        rects[3].width = m_width+m_border;
99
+        rects[3].height = m_border;
100
+        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, rects, 4, ShapeSet, 0);
101
+    } else {
76
         XRectangle rect;
102
         XRectangle rect;
77
-        rect.x = rect.y = m_border;
103
+        rect.x = m_x;
104
+        rect.y = m_y;
78
         rect.width = m_width;
105
         rect.width = m_width;
79
         rect.height = m_height;
106
         rect.height = m_height;
80
-
81
-        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
107
+        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
82
     }
108
     }
83
     // Make it so all input falls through
109
     // Make it so all input falls through
84
     XRectangle rect;
110
     XRectangle rect;
92
     int y = std::min( sy, ey );
118
     int y = std::min( sy, ey );
93
     int w = std::max( sx, ex ) - x;
119
     int w = std::max( sx, ex ) - x;
94
     int h = std::max( sy, ey ) - y;
120
     int h = std::max( sy, ey ) - y;
95
-    if ( m_x == x && m_y == y && m_width == w && m_height == h ) {
96
-        return;
97
-    }
98
 
121
 
122
+    // Only resize or move if we have to, because they're oddly expensive.
99
     m_x = x;
123
     m_x = x;
100
     m_y = y;
124
     m_y = y;
101
     m_width = w;
125
     m_width = w;
102
     m_height = h;
126
     m_height = h;
103
-
104
-    // Change the window size
105
-    XResizeWindow( xengine->m_display, m_window, m_width+m_border*2, m_height+m_border*2 );
106
     if ( m_border > 0 ) {
127
     if ( m_border > 0 ) {
107
-        // Fill up our old hole
128
+        XRectangle rects[4];
129
+        // Left
130
+        rects[0].x = m_x-m_border;
131
+        rects[0].y = m_y-m_border;
132
+        rects[0].width = m_border;
133
+        rects[0].height = m_height+m_border*2;
134
+        // Top
135
+        rects[1].x = m_x;
136
+        rects[1].y = m_y-m_border;
137
+        rects[1].width = m_width+m_border;
138
+        rects[1].height = m_border;
139
+        // Right
140
+        rects[2].x = m_x+m_width;
141
+        rects[2].y = m_y-m_border;
142
+        rects[2].width = m_border;
143
+        rects[2].height = m_height+m_border*2;
144
+        // Bottom
145
+        rects[3].x = m_x;
146
+        rects[3].y = m_y+m_height;
147
+        rects[3].width = m_width+m_border;
148
+        rects[3].height = m_border;
149
+        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, rects, 4, ShapeSet, 0);
150
+    } else {
108
         XRectangle rect;
151
         XRectangle rect;
109
-        rect.x = rect.y = 0;
110
-        rect.width = m_width+m_border*2;
111
-        rect.height = m_height+m_border*2;
112
-        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
113
-        // Then punch out another.
114
-        rect.x = rect.y = m_border;
152
+        rect.x = m_x;
153
+        rect.y = m_y;
115
         rect.width = m_width;
154
         rect.width = m_width;
116
         rect.height = m_height;
155
         rect.height = m_height;
117
-        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSubtract, 0);
118
-        // Then make it so all input falls through.
119
-        rect.x = rect.y = rect.width = rect.height = 0;
120
-        XShapeCombineRectangles( xengine->m_display, m_window, ShapeInput, 0, 0, &rect, 1, ShapeSet, 0);
156
+        XShapeCombineRectangles( xengine->m_display, m_window, ShapeBounding, 0, 0, &rect, 1, ShapeSet, 0);
121
     }
157
     }
122
-    XMoveWindow( xengine->m_display, m_window, m_x-m_border, m_y-m_border );
123
 }
158
 }
124
 
159
 
125
-int slop::Rectangle::convertColor( float r, float g, float b ) {
160
+XColor slop::Rectangle::convertColor( float r, float g, float b ) {
126
     // Convert float colors to shorts.
161
     // Convert float colors to shorts.
127
     short red   = short( floor( r * 65535.f ) );
162
     short red   = short( floor( r * 65535.f ) );
128
     short green = short( floor( g * 65535.f ) );
163
     short green = short( floor( g * 65535.f ) );
133
     color.blue = blue;
168
     color.blue = blue;
134
     int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
169
     int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
135
     if ( err == BadColor ) {
170
     if ( err == BadColor ) {
136
-        return err;
171
+        fprintf( stderr, "Couldn't allocate color of value %f,%f,%f!\n", r, g, b );
137
     }
172
     }
138
-    m_color = color;
139
-    return 0;
173
+    return color;
140
 }
174
 }

+ 1
- 1
rectangle.hpp View File

35
     int     m_border;
35
     int     m_border;
36
     bool    m_highlight;
36
     bool    m_highlight;
37
 private:
37
 private:
38
-    int     convertColor( float r, float g, float b );
38
+    XColor  convertColor( float r, float g, float b );
39
     void    constrain( int w, int h );
39
     void    constrain( int w, int h );
40
 };
40
 };
41
 
41
 

+ 1
- 12
x.cpp View File

140
     }
140
     }
141
     int xfontcursor = getCursor( type );
141
     int xfontcursor = getCursor( type );
142
     int err = XGrabPointer( m_display, m_root, True,
142
     int err = XGrabPointer( m_display, m_root, True,
143
-                            PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask,
143
+                            PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
144
                             GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
144
                             GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
145
     if ( err != GrabSuccess ) {
145
     if ( err != GrabSuccess ) {
146
         fprintf( stderr, "Error: Failed to grab X cursor.\n" );
146
         fprintf( stderr, "Error: Failed to grab X cursor.\n" );
178
     if ( !m_good ) {
178
     if ( !m_good ) {
179
         return;
179
         return;
180
     }
180
     }
181
-    XFlush( m_display );
182
     XEvent event;
181
     XEvent event;
183
     while ( XPending( m_display ) ) {
182
     while ( XPending( m_display ) ) {
184
         XNextEvent( m_display, &event );
183
         XNextEvent( m_display, &event );
206
                 }
205
                 }
207
                 break;
206
                 break;
208
             }
207
             }
209
-            case LeaveNotify: {
210
-                break;
211
-            }
212
             case ButtonRelease: {
208
             case ButtonRelease: {
213
                 if ( m_mouse.size() > event.xbutton.button ) {
209
                 if ( m_mouse.size() > event.xbutton.button ) {
214
                     m_mouse.at( event.xbutton.button ) = false;
210
                     m_mouse.at( event.xbutton.button ) = false;
218
                 }
214
                 }
219
                 break;
215
                 break;
220
             }
216
             }
221
-            // Due to X11 really hating applications grabbing the keyboard, we use XQueryKeymap to check for downed keys elsewhere.
222
-            case KeyPress: {
223
-                break;
224
-            }
225
-            case KeyRelease: {
226
-                break;
227
-            }
228
             default: break;
217
             default: break;
229
         }
218
         }
230
     }
219
     }