options.cpp 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include "options.hpp"
  2. slop::Options* options = new slop::Options();
  3. slop::Options::Options() {
  4. m_borderSize = 10;
  5. m_padding = 0;
  6. m_xdisplay = ":0";
  7. m_tolerance = 4;
  8. m_red = 0;
  9. m_green = 0;
  10. m_blue = 0;
  11. m_gracetime = 0.3;
  12. m_keyboard = true;
  13. }
  14. void slop::Options::printHelp() {
  15. printf( "Usage: slop [options]\n" );
  16. printf( "Print user selected region to stdout. Pressing keys or right-clicking cancels selection.\n" );
  17. printf( "\n" );
  18. printf( "options\n" );
  19. printf( " -h, --help show this message.\n" );
  20. printf( " -nkb, --nokeyboard disables the ability to cancel selections with the keyboard.\n" );
  21. printf( " -b=INT, --bordersize=INT set selection rectangle border size.\n" );
  22. printf( " -p=INT, --padding=INT set padding size for selection.\n" );
  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. Setting to zero will disable window selections.\n" );
  24. 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( " -g=FLOAT, --gracetime=FLOAT set the amount of time before slop will check for keyboard cancellations in seconds.\n" );
  27. printf( "examples\n" );
  28. printf( " slop -b=10 -x=:0 -p=-30 -t=4 -c=0.5,0.5,0.5 -g=.3\n" );
  29. }
  30. int slop::Options::parseOptions( int argc, char** argv ) {
  31. // Simple command parsing. Just uses sscanf to read each argument.
  32. // It looks complicated because you have to have spaces for delimiters for sscanf.
  33. for ( int i=0; i<argc; i++ ) {
  34. std::string arg = argv[i];
  35. if ( matches( arg, "-b=", "--bordersize=" ) ) {
  36. int err = parseInt( arg, &m_borderSize );
  37. if ( err ) {
  38. return 1;
  39. }
  40. if ( m_borderSize < 0 ) {
  41. m_borderSize = 0;
  42. }
  43. } else if ( matches( arg, "-p=", "--padding=" ) ) {
  44. int err = parseInt( arg, &m_padding );
  45. if ( err ) {
  46. return 1;
  47. }
  48. } else if ( matches( arg, "-c=", "--color=" ) ) {
  49. int err = parseColor( arg, &m_red, &m_green, &m_blue );
  50. if ( err ) {
  51. return 1;
  52. }
  53. } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
  54. int err = parseInt( arg, &m_tolerance );
  55. if ( err ) {
  56. return 1;
  57. }
  58. if ( m_tolerance < 0 ) {
  59. m_tolerance = 0;
  60. }
  61. } else if ( matches( arg, "-g=", "--gracetime=" ) ) {
  62. int err = parseFloat( arg, &m_gracetime );
  63. if ( err ) {
  64. return 1;
  65. }
  66. if ( m_gracetime < 0 ) {
  67. m_gracetime = 0;
  68. }
  69. } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
  70. int err = parseString( arg, &m_xdisplay );
  71. if ( err ) {
  72. return 1;
  73. }
  74. } else if ( matches( arg, "-nkb", "--nokeyboard" ) ) {
  75. m_keyboard = false;
  76. } else if ( matches( arg, "-h", "--help" ) ) {
  77. printHelp();
  78. return 2;
  79. } else {
  80. if ( i == 0 ) {
  81. continue;
  82. }
  83. fprintf( stderr, "Error: Unknown argument %s\n", argv[i] );
  84. fprintf( stderr, "Try -h or --help for help.\n" );
  85. return 1;
  86. }
  87. }
  88. return 0;
  89. }
  90. int slop::Options::parseInt( std::string arg, int* returnInt ) {
  91. std::string copy = arg;
  92. int find = copy.find( "=" );
  93. if ( find != copy.npos ) {
  94. copy.at( find ) = ' ';
  95. }
  96. // Just in case we error out, grab the actual argument name into x.
  97. char* x = new char[ arg.size() ];
  98. int num = sscanf( copy.c_str(), "%s %i", x, returnInt );
  99. if ( num != 2 ) {
  100. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  101. fprintf( stderr, "Usage: %s=INT\n", x );
  102. fprintf( stderr, "Example: %s=10 or %s=-12\n", x, x );
  103. fprintf( stderr, "Try -h or --help for help.\n" );
  104. delete[] x;
  105. return 1;
  106. }
  107. delete[] x;
  108. return 0;
  109. }
  110. int slop::Options::parseFloat( std::string arg, float* returnFloat ) {
  111. std::string copy = arg;
  112. int find = copy.find( "=" );
  113. if ( find != copy.npos ) {
  114. copy.at( find ) = ' ';
  115. }
  116. // Just in case we error out, grab the actual argument name into x.
  117. char* x = new char[ arg.size() ];
  118. int num = sscanf( copy.c_str(), "%s %f", x, returnFloat );
  119. if ( num != 2 ) {
  120. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  121. fprintf( stderr, "Usage: %s=FLOAT\n", x );
  122. fprintf( stderr, "Example: %s=3.14 or %s=-99\n", x, x );
  123. fprintf( stderr, "Try -h or --help for help.\n" );
  124. delete[] x;
  125. return 1;
  126. }
  127. delete[] x;
  128. return 0;
  129. }
  130. bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
  131. if ( arg.substr( 0, shorthand.size() ) == shorthand ) {
  132. if ( arg == shorthand || shorthand[shorthand.length()-1] == '=' ) {
  133. return true;
  134. }
  135. }
  136. if ( longhand.size() && arg.substr( 0, longhand.size() ) == longhand ) {
  137. if ( arg == longhand || longhand[longhand.length()-1] == '=' ) {
  138. return true;
  139. }
  140. }
  141. return false;
  142. }
  143. int slop::Options::parseString( std::string arg, std::string* returnString ) {
  144. std::string copy = arg;
  145. int find = copy.find( "=" );
  146. if ( find != copy.npos ) {
  147. copy.at( find ) = ' ';
  148. }
  149. // Just in case we error out, grab the actual argument name into x.
  150. char* x = new char[ arg.size() ];
  151. char* y = new char[ arg.size() ];
  152. int num = sscanf( copy.c_str(), "%s %s", x, y );
  153. if ( num != 2 ) {
  154. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  155. fprintf( stderr, "Usage: %s=STRING\n", x );
  156. fprintf( stderr, "Example: %s=:0 or %s=hostname:0.1\n", x, x );
  157. fprintf( stderr, "Try -h or --help for help.\n" );
  158. delete[] x;
  159. delete[] y;
  160. return 1;
  161. }
  162. *returnString = y;
  163. delete[] x;
  164. delete[] y;
  165. return 0;
  166. }
  167. int slop::Options::parseColor( std::string arg, float* r, float* g, float* b ) {
  168. std::string copy = arg;
  169. int find = copy.find( "=" );
  170. while( find != copy.npos ) {
  171. copy.at( find ) = ' ';
  172. find = copy.find( "," );
  173. }
  174. // Just in case we error out, grab the actual argument name into x.
  175. char* x = new char[ arg.size() ];
  176. int num = sscanf( copy.c_str(), "%s %f %f %f", x, r, g, b );
  177. if ( num != 4 ) {
  178. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  179. fprintf( stderr, "Usage: %s=COLOR\n", x );
  180. fprintf( stderr, "Example: %s=0,0,0 or %s=0.7,0.2,1\n", x, x );
  181. fprintf( stderr, "Try -h or --help for help.\n" );
  182. delete[] x;
  183. return 1;
  184. }
  185. delete[] x;
  186. return 0;
  187. }