options.cpp 9.9KB

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