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