options.cpp 7.3KB

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