options.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. #include "options.hpp"
  2. slop::Options* options = new slop::Options();
  3. slop::Options::Options() {
  4. m_version = "v2.0.2";
  5. m_borderSize = 10;
  6. m_padding = 0;
  7. m_xdisplay = ":0";
  8. m_tolerance = 4;
  9. m_red = 0;
  10. m_green = 0;
  11. m_blue = 0;
  12. m_gracetime = 0.4;
  13. m_keyboard = true;
  14. m_decorations = true;
  15. }
  16. void slop::Options::printHelp() {
  17. printf( "Usage: slop [options]\n" );
  18. printf( "Print user selected region to stdout. Pressing keys or right-clicking cancels selection.\n" );
  19. printf( "\n" );
  20. printf( "Options\n" );
  21. printf( " -h, --help Show this message.\n" );
  22. printf( " -nkb, --nokeyboard Disables the ability to cancel selections with the keyboard.\n" );
  23. printf( " -b=INT, --bordersize=INT Set selection rectangle border size.\n" );
  24. printf( " -p=INT, --padding=INT Set padding size for selection.\n" );
  25. printf( " -t=INT, --tolerance=INT How far in pixels the mouse can move after clicking and still be detected\n" );
  26. printf( " as a normal click. Setting to zero will disable window selections.\n" );
  27. printf( " -x=STRING, --xdisplay=STRING Set x display (STRING must be hostname:number.screen_number format)\n" );
  28. printf( " -c=COLOR, --color=COLOR Set selection rectangle color, COLOR is in format FLOAT,FLOAT,FLOAT\n" );
  29. printf( " -g=FLOAT, --gracetime=FLOAT Set the amount of time before slop will check for keyboard cancellations\n" );
  30. printf( " in seconds.\n" );
  31. printf( " -nd, --nodecorations attempts to remove decorations from window selections.\n" );
  32. printf( " -v, --version prints version.\n" );
  33. printf( "\n" );
  34. printf( "Examples\n" );
  35. printf( " $ # Gray, thick border for maximum visiblity.\n" );
  36. printf( " $ slop -b=20 -c=0.5,0.5,0.5\n" );
  37. printf( "\n" );
  38. printf( " $ # Remove window decorations.\n" );
  39. printf( " $ slop -nd\n" );
  40. printf( "\n" );
  41. printf( " $ # Disable window selections. Useful for selecting individual pixels.\n" );
  42. printf( " $ slop -t=0\n" );
  43. }
  44. int slop::Options::parseOptions( int argc, char** argv ) {
  45. // Simple command parsing. Just uses sscanf to read each argument.
  46. // It looks complicated because you have to have spaces for delimiters for sscanf.
  47. for ( int i=0; i<argc; i++ ) {
  48. std::string arg = argv[i];
  49. if ( matches( arg, "-b=", "--bordersize=" ) ) {
  50. int err = parseInt( arg, &m_borderSize );
  51. if ( err ) {
  52. return 1;
  53. }
  54. if ( m_borderSize < 0 ) {
  55. m_borderSize = 0;
  56. }
  57. } else if ( matches( arg, "-p=", "--padding=" ) ) {
  58. int err = parseInt( arg, &m_padding );
  59. if ( err ) {
  60. return 1;
  61. }
  62. } else if ( matches( arg, "-c=", "--color=" ) ) {
  63. int err = parseColor( arg, &m_red, &m_green, &m_blue );
  64. if ( err ) {
  65. return 1;
  66. }
  67. } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
  68. int err = parseInt( arg, &m_tolerance );
  69. if ( err ) {
  70. return 1;
  71. }
  72. if ( m_tolerance < 0 ) {
  73. m_tolerance = 0;
  74. }
  75. } else if ( matches( arg, "-g=", "--gracetime=" ) ) {
  76. int err = parseFloat( arg, &m_gracetime );
  77. if ( err ) {
  78. return 1;
  79. }
  80. if ( m_gracetime < 0 ) {
  81. m_gracetime = 0;
  82. }
  83. } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
  84. int err = parseString( arg, &m_xdisplay );
  85. if ( err ) {
  86. return 1;
  87. }
  88. } else if ( matches( arg, "-nkb", "--nokeyboard" ) ) {
  89. m_keyboard = false;
  90. } else if ( matches( arg, "-nd", "--nodecorations" ) ) {
  91. m_decorations = false;
  92. } else if ( matches( arg, "-h", "--help" ) ) {
  93. printHelp();
  94. return 2;
  95. } else if ( matches( arg, "-v", "--version" ) ) {
  96. printf( "slop %s\n", m_version.c_str() );
  97. return 2;
  98. } else {
  99. if ( i == 0 ) {
  100. continue;
  101. }
  102. fprintf( stderr, "Error: Unknown argument %s\n", argv[i] );
  103. fprintf( stderr, "Try -h or --help for help.\n" );
  104. return 1;
  105. }
  106. }
  107. return 0;
  108. }
  109. int slop::Options::parseInt( std::string arg, int* returnInt ) {
  110. std::string copy = arg;
  111. int find = copy.find( "=" );
  112. if ( find != copy.npos ) {
  113. copy.at( find ) = ' ';
  114. }
  115. // Just in case we error out, grab the actual argument name into x.
  116. char* x = new char[ arg.size() ];
  117. int num = sscanf( copy.c_str(), "%s %i", x, returnInt );
  118. if ( num != 2 ) {
  119. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  120. fprintf( stderr, "Usage: %s=INT\n", x );
  121. fprintf( stderr, "Example: %s=10 or %s=-12\n", x, x );
  122. fprintf( stderr, "Try -h or --help for help.\n" );
  123. delete[] x;
  124. return 1;
  125. }
  126. delete[] x;
  127. return 0;
  128. }
  129. int slop::Options::parseFloat( std::string arg, float* returnFloat ) {
  130. std::string copy = arg;
  131. int find = copy.find( "=" );
  132. if ( find != copy.npos ) {
  133. copy.at( find ) = ' ';
  134. }
  135. // Just in case we error out, grab the actual argument name into x.
  136. char* x = new char[ arg.size() ];
  137. int num = sscanf( copy.c_str(), "%s %f", x, returnFloat );
  138. if ( num != 2 ) {
  139. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  140. fprintf( stderr, "Usage: %s=FLOAT\n", x );
  141. fprintf( stderr, "Example: %s=3.14 or %s=-99\n", x, x );
  142. fprintf( stderr, "Try -h or --help for help.\n" );
  143. delete[] x;
  144. return 1;
  145. }
  146. delete[] x;
  147. return 0;
  148. }
  149. bool slop::Options::matches( std::string arg, std::string shorthand, std::string longhand ) {
  150. if ( arg.substr( 0, shorthand.size() ) == shorthand ) {
  151. if ( arg == shorthand || shorthand[shorthand.length()-1] == '=' ) {
  152. return true;
  153. }
  154. }
  155. if ( longhand.size() && arg.substr( 0, longhand.size() ) == longhand ) {
  156. if ( arg == longhand || longhand[longhand.length()-1] == '=' ) {
  157. return true;
  158. }
  159. }
  160. return false;
  161. }
  162. int slop::Options::parseString( std::string arg, std::string* returnString ) {
  163. std::string copy = arg;
  164. int find = copy.find( "=" );
  165. if ( find != copy.npos ) {
  166. copy.at( find ) = ' ';
  167. }
  168. // Just in case we error out, grab the actual argument name into x.
  169. char* x = new char[ arg.size() ];
  170. char* y = new char[ arg.size() ];
  171. int num = sscanf( copy.c_str(), "%s %s", x, y );
  172. if ( num != 2 ) {
  173. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  174. fprintf( stderr, "Usage: %s=STRING\n", x );
  175. fprintf( stderr, "Example: %s=:0 or %s=hostname:0.1\n", x, x );
  176. fprintf( stderr, "Try -h or --help for help.\n" );
  177. delete[] x;
  178. delete[] y;
  179. return 1;
  180. }
  181. *returnString = y;
  182. delete[] x;
  183. delete[] y;
  184. return 0;
  185. }
  186. int slop::Options::parseColor( std::string arg, float* r, float* g, float* b ) {
  187. std::string copy = arg;
  188. int find = copy.find( "=" );
  189. while( find != copy.npos ) {
  190. copy.at( find ) = ' ';
  191. find = copy.find( "," );
  192. }
  193. // Just in case we error out, grab the actual argument name into x.
  194. char* x = new char[ arg.size() ];
  195. int num = sscanf( copy.c_str(), "%s %f %f %f", x, r, g, b );
  196. if ( num != 4 ) {
  197. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  198. fprintf( stderr, "Usage: %s=COLOR\n", x );
  199. fprintf( stderr, "Example: %s=0,0,0 or %s=0.7,0.2,1\n", x, x );
  200. fprintf( stderr, "Try -h or --help for help.\n" );
  201. delete[] x;
  202. return 1;
  203. }
  204. delete[] x;
  205. return 0;
  206. }
  207. int slop::Options::parseGeometry( std::string arg, int* x, int* y, int* w, int* h ) {
  208. std::string copy = arg;
  209. // Replace the first =, all x's and +'s with spaces.
  210. int find = copy.find( "=" );
  211. while( find != copy.npos ) {
  212. copy.at( find ) = ' ';
  213. find = copy.find( "x" );
  214. }
  215. find = copy.find( "+" );
  216. while( find != copy.npos ) {
  217. copy.at( find ) = ' ';
  218. find = copy.find( "+" );
  219. }
  220. // Just in case we error out, grab the actual argument name into x.
  221. char* foo = new char[ arg.size() ];
  222. int num = sscanf( copy.c_str(), "%s %d %d %d %d", foo, w, h, x, y );
  223. if ( num != 5 ) {
  224. fprintf( stderr, "Error parsing command arguments near %s\n", arg.c_str() );
  225. fprintf( stderr, "Usage: %s=GEOMETRY\n", foo );
  226. fprintf( stderr, "Example: %s=1920x1080+0+0 or %s=256x256+100+-200\n", foo, foo );
  227. fprintf( stderr, "Try -h or --help for help.\n" );
  228. delete[] foo;
  229. return 1;
  230. }
  231. delete[] foo;
  232. return 0;
  233. }