ソースを参照

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

naelstrof 11 年 前
コミット
54345c6c32
共有3 個のファイルを変更した40 個の追加3 個の削除を含む
  1. 6
    2
      main.cpp
  2. 32
    1
      options.cpp
  3. 2
    0
      options.hpp

+ 6
- 2
main.cpp ファイルの表示

44
         // "ticking" the xengine makes it process all queued events.
44
         // "ticking" the xengine makes it process all queued events.
45
         xengine->tick();
45
         xengine->tick();
46
         // If the user presses any key on the keyboard, exit the application.
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
         double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
48
         double timei = double( time.tv_sec*1000000000L + time.tv_nsec )/1000000000.f;
49
         double starti = double( start.tv_sec*1000000000L + start.tv_nsec )/1000000000.f;
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
             if ( xengine->m_keypressed ) {
51
             if ( xengine->m_keypressed ) {
52
                 printf( "X=0\n" );
52
                 printf( "X=0\n" );
53
                 printf( "Y=0\n" );
53
                 printf( "Y=0\n" );
193
     // Clean up global classes.
193
     // Clean up global classes.
194
     delete xengine;
194
     delete xengine;
195
     delete options;
195
     delete options;
196
+    // If we canceled the selection, return error.
197
+    if ( state == -1 ) {
198
+        return 1;
199
+    }
196
     return 0;
200
     return 0;
197
 }
201
 }

+ 32
- 1
options.cpp ファイルの表示

10
     m_red = 0;
10
     m_red = 0;
11
     m_green = 0;
11
     m_green = 0;
12
     m_blue = 0;
12
     m_blue = 0;
13
+    m_gracetime = 0.1;
13
 }
14
 }
14
 
15
 
15
 void slop::Options::printHelp() {
16
 void slop::Options::printHelp() {
23
     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
     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
     printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
25
     printf( "    -x=STRING, --xdisplay=STRING   set x display (STRING must be hostname:number.screen_number format)\n" );
25
     printf( "    -c=COLOR, --color=COLOR        set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT\n" );
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
     printf( "examples\n" );
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
 int slop::Options::parseOptions( int argc, char** argv ) {
32
 int slop::Options::parseOptions( int argc, char** argv ) {
58
             if ( m_tolerance < 0 ) {
60
             if ( m_tolerance < 0 ) {
59
                 m_tolerance = 0;
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
         } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
71
         } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
62
             int err = parseString( arg, &m_xdisplay );
72
             int err = parseString( arg, &m_xdisplay );
63
             if ( err ) {
73
             if ( err ) {
99
     return 0;
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
 bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
133
 bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
103
     if ( arg.substr( 0, shorthand.size() ) == shorthand ||
134
     if ( arg.substr( 0, shorthand.size() ) == shorthand ||
104
          arg.substr( 0, longhand.size() ) == longhand ) {
135
          arg.substr( 0, longhand.size() ) == longhand ) {

+ 2
- 0
options.hpp ファイルの表示

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