Przeglądaj źródła

reduced time till slop responds to keyboard, and added options for it

naelstrof 10 lat temu
rodzic
commit
54345c6c32
3 zmienionych plików z 40 dodań i 3 usunięć
  1. 6
    2
      main.cpp
  2. 32
    1
      options.cpp
  3. 2
    0
      options.hpp

+ 6
- 2
main.cpp Wyświetl plik

@@ -44,10 +44,10 @@ int main( int argc, char** argv ) {
44 44
         // "ticking" the xengine makes it process all queued events.
45 45
         xengine->tick();
46 46
         // If the user presses any key on the keyboard, exit the application.
47
-        // Make sure at least a half second has passed before allowing canceling
47
+        // Make sure at least options->m_gracetime has passed before allowing canceling
48 48
         double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
49 49
         double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
50
-        if ( timei - starti > 0.5 ) {
50
+        if ( timei - starti > options->m_gracetime ) {
51 51
             if ( xengine->m_keypressed ) {
52 52
                 printf( "X=0\n" );
53 53
                 printf( "Y=0\n" );
@@ -193,5 +193,9 @@ int main( int argc, char** argv ) {
193 193
     // Clean up global classes.
194 194
     delete xengine;
195 195
     delete options;
196
+    // If we canceled the selection, return error.
197
+    if ( state == -1 ) {
198
+        return 1;
199
+    }
196 200
     return 0;
197 201
 }

+ 32
- 1
options.cpp Wyświetl plik

@@ -10,6 +10,7 @@ slop::Options::Options() {
10 10
     m_red = 0;
11 11
     m_green = 0;
12 12
     m_blue = 0;
13
+    m_gracetime = 0.1;
13 14
 }
14 15
 
15 16
 void slop::Options::printHelp() {
@@ -23,8 +24,9 @@ void slop::Options::printHelp() {
23 24
     printf( "    -t=INT, --tolerance=INT        if you have a shaky mouse, increasing this value will make slop detect single clicks better. Rather than interpreting your shaky clicks as region selections.\n" );
24 25
     printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
25 26
     printf( "    -c=COLOR, --color=COLOR        set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT\n" );
27
+    printf( "    -g=FLOAT, --gracetime=FLOAT    set the amount of time before slop will check for keyboard cancellations in seconds.\n" );
26 28
     printf( "examples\n" );
27
-    printf( "    slop -b=10 -x=:0 -p=-30 -t=4 -c=0.5,0.5,0.5\n" );
29
+    printf( "    slop -b=10 -x=:0 -p=-30 -t=4 -c=0.5,0.5,0.5 -g=.2\n" );
28 30
 }
29 31
 
30 32
 int slop::Options::parseOptions( int argc, char** argv ) {
@@ -58,6 +60,14 @@ int slop::Options::parseOptions( int argc, char** argv ) {
58 60
             if ( m_tolerance < 0 ) {
59 61
                 m_tolerance = 0;
60 62
             }
63
+        } else if ( matches( arg, "-g=", "--gracetime=" ) ) {
64
+            int err = parseFloat( arg, &m_gracetime );
65
+            if ( err ) {
66
+                return 1;
67
+            }
68
+            if ( m_gracetime < 0 ) {
69
+                m_gracetime = 0;
70
+            }
61 71
         } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
62 72
             int err = parseString( arg, &m_xdisplay );
63 73
             if ( err ) {
@@ -99,6 +109,27 @@ int slop::Options::parseInt( std::string arg, int* returnInt ) {
99 109
     return 0;
100 110
 }
101 111
 
112
+int slop::Options::parseFloat( std::string arg, float* returnFloat ) {
113
+    std::string copy = arg;
114
+    int find = copy.find( "=" );
115
+    if ( find != copy.npos ) {
116
+        copy.at( find ) = ' ';
117
+    }
118
+    // Just in case we error out, grab the actual argument name into x.
119
+    char* x = new char[ arg.size() ];
120
+    int num = sscanf( copy.c_str(), "%s %f", x, returnFloat );
121
+    if ( num != 2 ) {
122
+        fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
123
+        fprintf( stderr, "Usage: %s=FLOAT\n", x );
124
+        fprintf( stderr, "Example: %s=3.14 or %s=-99\n", x, x );
125
+        fprintf( stderr, "Try -h or --help for help.\n" );
126
+        delete[] x;
127
+        return 1;
128
+    }
129
+    delete[] x;
130
+    return 0;
131
+}
132
+
102 133
 bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
103 134
     if ( arg.substr( 0, shorthand.size() ) == shorthand ||
104 135
          arg.substr( 0, longhand.size() ) == longhand ) {

+ 2
- 0
options.hpp Wyświetl plik

@@ -18,8 +18,10 @@ public:
18 18
     float       m_green;
19 19
     float       m_blue;
20 20
     std::string m_xdisplay;
21
+    float       m_gracetime;
21 22
 private:
22 23
     int         parseInt( std::string arg, int* returnInt );
24
+    int         parseFloat( std::string arg, float* returnFloat );
23 25
     int         parseString( std::string arg, std::string* returnString );
24 26
     int         parseColor( std::string arg, float* r, float* g, float* b );
25 27
     bool        matches( std::string arg, std::string shorthand, std::string longhand );