naelstrof hace 10 años
padre
commit
56b8727248
Se han modificado 3 ficheros con 45 adiciones y 29 borrados
  1. 37
    25
      main.cpp
  2. 1
    1
      options.cpp
  3. 7
    3
      x.cpp

+ 37
- 25
main.cpp Ver fichero

@@ -10,7 +10,7 @@ int main( int argc, char** argv ) {
10 10
     }
11 11
     int state = 0;
12 12
     bool running = true;
13
-    slop::Rectangle* selection;
13
+    slop::Rectangle* selection = NULL;
14 14
     slop::Rectangle* windowselection = NULL;
15 15
     Window window = None;
16 16
     std::string xdisplay = options->m_xdisplay;
@@ -20,7 +20,8 @@ int main( int argc, char** argv ) {
20 20
     float r = options->m_red;
21 21
     float g = options->m_green;
22 22
     float b = options->m_blue;
23
-    timespec start,time;
23
+    int cx = 0;
24
+    int cy = 0;
24 25
 
25 26
     // First we set up the x interface and grab the mouse,
26 27
     // if we fail for either we exit immediately.
@@ -99,10 +100,9 @@ int main( int argc, char** argv ) {
99 100
                 break;
100 101
             }
101 102
             case 1: {
102
-                // Simply create a new rectangle at the mouse position and move on
103
-                // to the next state.
104
-                selection = new slop::Rectangle( xengine->m_mousex, xengine->m_mousey, 0, 0, borderSize, padding, r, g, b );
105
-                xengine->addRect( selection );
103
+                // Set the mouse position of where we clicked, used so that click tolerance doesn't affect the rectangle's position.
104
+                cx = xengine->m_mousex;
105
+                cy = xengine->m_mousey;
106 106
                 state++;
107 107
                 break;
108 108
             }
@@ -113,10 +113,19 @@ int main( int argc, char** argv ) {
113 113
                     state++;
114 114
                     break;
115 115
                 }
116
+                // Check to make sure the user actually wants to drag for a selection before creating a rectangle.
117
+                int w = xengine->m_mousex - cx;
118
+                int h = xengine->m_mousey - cy;
119
+                if ( ( std::abs( w ) > tolerance || std::abs( h ) > tolerance ) && !selection ) {
120
+                    selection = new slop::Rectangle( cx, cy, 0, 0, borderSize, padding, r, g, b );
121
+                    xengine->addRect( selection );
122
+                } else if ( std::abs( w ) <= tolerance && std::abs( h ) <= tolerance ) {
123
+                    continue;
124
+                }
116 125
                 // Set the selection rectangle's dimensions to mouse movement.
117 126
                 // We use the function setDim since rectangles can't have negative widths,
118 127
                 // and because the rectangles have borders and padding to worry about.
119
-                selection->setDim( xengine->m_mousex - selection->m_x, xengine->m_mousey - selection->m_y );
128
+                selection->setDim( w, h );
120 129
                 // We also detect which way the user is pulling and set the mouse icon accordingly.
121 130
                 bool x = selection->m_flippedx;
122 131
                 bool y = selection->m_flippedy;
@@ -133,26 +142,29 @@ int main( int argc, char** argv ) {
133 142
                 break;
134 143
             }
135 144
             case 3: {
136
-                // We pull the dimensions and positions from the selection rectangle.
137
-                // The selection rectangle automatically converts the positions and
138
-                // dimensions to absolute coordinates when we set them earilier.
139
-                int x = selection->m_x+selection->m_xoffset;
140
-                int y = selection->m_y+selection->m_yoffset;
141
-                int w = selection->m_width;
142
-                int h = selection->m_height;
143
-                // Delete the rectangle.
144
-                xengine->removeRect( selection );
145
+                int x, y, w, h;
145 146
                 // Exit the utility after this state runs once.
146 147
                 running = false;
147
-                // If the user simply clicked (and thus made the width and height smaller than
148
-                // our tolerance) or if we're not hovering over a window, just print the selection
149
-                // rectangle's stuff.
150
-                if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
151
-                    printf( "X=%i\n", x );
152
-                    printf( "Y=%i\n", y );
153
-                    printf( "W=%i\n", w + 1 );
154
-                    printf( "H=%i\n", h + 1 );
155
-                    break;
148
+                if ( selection ) {
149
+                    // We pull the dimensions and positions from the selection rectangle.
150
+                    // The selection rectangle automatically converts the positions and
151
+                    // dimensions to absolute coordinates when we set them earilier.
152
+                    x = selection->m_x+selection->m_xoffset;
153
+                    y = selection->m_y+selection->m_yoffset;
154
+                    w = selection->m_width;
155
+                    h = selection->m_height;
156
+                    // Delete the rectangle.
157
+                    xengine->removeRect( selection );
158
+                    // If the user simply clicked (and thus made the width and height smaller than
159
+                    // our tolerance) or if we're not hovering over a window, just print the selection
160
+                    // rectangle's stuff.
161
+                    if ( w > tolerance || h > tolerance || xengine->m_hoverXWindow == None ) {
162
+                        printf( "X=%i\n", x );
163
+                        printf( "Y=%i\n", y );
164
+                        printf( "W=%i\n", w + 1 );
165
+                        printf( "H=%i\n", h + 1 );
166
+                        break;
167
+                    }
156 168
                 }
157 169
                 // Otherwise lets grab the window's dimensions and use those (with padding).
158 170
                 slop::WindowRectangle t = xengine->m_hoverWindow;

+ 1
- 1
options.cpp Ver fichero

@@ -32,7 +32,7 @@ int slop::Options::parseOptions( int argc, char** argv ) {
32 32
     // It looks complicated because you have to have spaces for delimiters for sscanf.
33 33
     for ( int i=0; i<argc; i++ ) {
34 34
         std::string arg = argv[i];
35
-        if ( matches( arg, "--b=", "--bordersize=" ) ) {
35
+        if ( matches( arg, "-b=", "--bordersize=" ) ) {
36 36
             int err = parseInt( arg, &m_borderSize );
37 37
             if ( err ) {
38 38
                 return 1;

+ 7
- 3
x.cpp Ver fichero

@@ -61,7 +61,8 @@ int slop::XEngine::init( std::string display ) {
61 61
     m_screen    = ScreenOfDisplay( m_display, DefaultScreen( m_display ) );
62 62
     m_visual    = DefaultVisual  ( m_display, XScreenNumberOfScreen( m_screen ) );
63 63
     m_colormap  = DefaultColormap( m_display, XScreenNumberOfScreen( m_screen ) );
64
-    m_root      = RootWindow     ( m_display, XScreenNumberOfScreen( m_screen ) );
64
+    //m_root      = RootWindow     ( m_display, XScreenNumberOfScreen( m_screen ) );
65
+    m_root      = DefaultRootWindow( m_display );
65 66
 
66 67
     m_good = true;
67 68
     return 0;
@@ -71,14 +72,17 @@ int slop::XEngine::grabKeyboard() {
71 72
     if ( !m_good ) {
72 73
         return 1;
73 74
     }
74
-    int err = XGrabKeyboard( m_display, m_root, False,
75
+    XGrabKey( m_display, AnyKey, AnyModifier, m_root, False, GrabModeAsync, GrabModeAsync );
76
+    // For whatever we fail to grab the keyboard 100% of the time if slop is launched in the background.
77
+    /*int err = XGrabKeyboard( m_display, m_root, False,
75 78
                              GrabModeAsync, GrabModeAsync, CurrentTime );
76 79
     if ( err != GrabSuccess ) {
77 80
         fprintf( stderr, "Error: Failed to grab X keyboard.\n" );
78 81
         fprintf( stderr, "This can be caused by launching slop incorrectly.\n" );
79 82
         fprintf( stderr, "gnome-session launches it fine from keyboard binds.\n" );
80 83
         return 1;
81
-    }
84
+    }*/
85
+    return 0;
82 86
 }
83 87
 
84 88
 int slop::XEngine::releaseKeyboard() {