Преглед изворни кода

Improved rendering speed and accuracy.

Dalton Nell пре 9 година
родитељ
комит
e66dba979e
5 измењених фајлова са 81 додато и 57 уклоњено
  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 Прегледај датотеку

@@ -203,11 +203,12 @@ int main( int argc, char** argv ) {
203 203
             case 2: {
204 204
                 // It's possible that our selection doesn't exist still, lets make sure it actually gets created here.
205 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 212
                                                      borderSize,
212 213
                                                      highlight,
213 214
                                                      r, g, b, a );

+ 2
- 2
options.cpp Прегледај датотеку

@@ -3,7 +3,7 @@
3 3
 slop::Options* options = new slop::Options();
4 4
 
5 5
 slop::Options::Options() {
6
-    m_version = "v2.1.0";
6
+    m_version = "v2.1.1";
7 7
     m_highlight = false;
8 8
     m_borderSize = 10;
9 9
     m_padding = 0;
@@ -55,7 +55,7 @@ void slop::Options::printHelp() {
55 55
     printf( "    $ slop -t=0\n" );
56 56
     printf( "\n" );
57 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 61
 int slop::Options::parseOptions( int argc, char** argv ) {

+ 71
- 37
rectangle.cpp Прегледај датотеку

@@ -8,6 +8,15 @@ slop::Rectangle::~Rectangle() {
8 8
     if ( m_window == None ) {
9 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 20
     // Free up our color.
12 21
     XFreeColors( xengine->m_display, xengine->m_colormap, &m_color.pixel, 1, 0 );
13 22
     XDestroyWindow( xengine->m_display, m_window );
@@ -36,30 +45,24 @@ slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool hig
36 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 49
     XSetWindowAttributes attributes;
45 50
     // Set up the window so it's our color 
46
-    attributes.background_pixmap = None;
47 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 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 54
     // Make sure we know when we've been successfully destroyed later!
54 55
     attributes.event_mask = StructureNotifyMask;
55
-    unsigned long valueMask = CWBackPixmap | CWBackPixel | CWOverrideRedirect | CWColormap | CWEventMask;
56
+    unsigned long valueMask = CWBackPixel | CWOverrideRedirect | CWEventMask;
56 57
 
57 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 60
                               0, CopyFromParent, InputOutput,
60 61
                               CopyFromParent, valueMask, &attributes );
61 62
 
63
+
62 64
     if ( a < 1 ) {
65
+        // Change the window opacity
63 66
         unsigned int cardinal_alpha = (unsigned int) (a * (unsigned int)-1) ;
64 67
         XChangeProperty( xengine->m_display, m_window, XInternAtom( xengine->m_display, "_NET_WM_WINDOW_OPACITY", 0),
65 68
                          XA_CARDINAL, 32, PropModeReplace, (unsigned char*)&cardinal_alpha, 1 );
@@ -73,12 +76,35 @@ slop::Rectangle::Rectangle( int sx, int sy, int ex, int ey, int border, bool hig
73 76
 
74 77
     // Now punch a hole into it so it looks like a selection rectangle, but only if we're not highlighting.
75 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 102
         XRectangle rect;
77
-        rect.x = rect.y = m_border;
103
+        rect.x = m_x;
104
+        rect.y = m_y;
78 105
         rect.width = m_width;
79 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 109
     // Make it so all input falls through
84 110
     XRectangle rect;
@@ -92,37 +118,46 @@ void slop::Rectangle::setGeo( int sx, int sy, int ex, int ey ) {
92 118
     int y = std::min( sy, ey );
93 119
     int w = std::max( sx, ex ) - x;
94 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 123
     m_x = x;
100 124
     m_y = y;
101 125
     m_width = w;
102 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 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 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 154
         rect.width = m_width;
116 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 161
     // Convert float colors to shorts.
127 162
     short red   = short( floor( r * 65535.f ) );
128 163
     short green = short( floor( g * 65535.f ) );
@@ -133,8 +168,7 @@ int slop::Rectangle::convertColor( float r, float g, float b ) {
133 168
     color.blue = blue;
134 169
     int err = XAllocColor( xengine->m_display, xengine->m_colormap, &color );
135 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 Прегледај датотеку

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

+ 1
- 12
x.cpp Прегледај датотеку

@@ -140,7 +140,7 @@ int slop::XEngine::grabCursor( slop::CursorType type ) {
140 140
     }
141 141
     int xfontcursor = getCursor( type );
142 142
     int err = XGrabPointer( m_display, m_root, True,
143
-                            PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask | LeaveWindowMask,
143
+                            PointerMotionMask | ButtonPressMask | ButtonReleaseMask | EnterWindowMask,
144 144
                             GrabModeAsync, GrabModeAsync, None, xfontcursor, CurrentTime );
145 145
     if ( err != GrabSuccess ) {
146 146
         fprintf( stderr, "Error: Failed to grab X cursor.\n" );
@@ -178,7 +178,6 @@ void slop::XEngine::tick() {
178 178
     if ( !m_good ) {
179 179
         return;
180 180
     }
181
-    XFlush( m_display );
182 181
     XEvent event;
183 182
     while ( XPending( m_display ) ) {
184 183
         XNextEvent( m_display, &event );
@@ -206,9 +205,6 @@ void slop::XEngine::tick() {
206 205
                 }
207 206
                 break;
208 207
             }
209
-            case LeaveNotify: {
210
-                break;
211
-            }
212 208
             case ButtonRelease: {
213 209
                 if ( m_mouse.size() > event.xbutton.button ) {
214 210
                     m_mouse.at( event.xbutton.button ) = false;
@@ -218,13 +214,6 @@ void slop::XEngine::tick() {
218 214
                 }
219 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 217
             default: break;
229 218
         }
230 219
     }