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