options.cpp 6.6KB

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