options.cpp 9.3KB

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