options.cpp 9.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. #include "options.hpp"
  2. slop::Options* options = new slop::Options();
  3. slop::Options::Options() {
  4. m_borderSize = 10;
  5. m_padding = 0;
  6. m_xdisplay = ":0";
  7. m_tolerance = 4;
  8. m_red = 0;
  9. m_green = 0;
  10. m_blue = 0;
  11. m_gracetime = 0.3;
  12. m_keyboard = true;
  13. m_decorations = true;
  14. m_offsetx = 0;
  15. m_offsety = 0;
  16. m_offsetw = 0;
  17. m_offseth = 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\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( " -o=GEOMETRY, --offset=GEOMETRY Offsets window selections, but only if --nodecorations is active and if the\n" );
  36. printf( " window's decorations were successfully detected. Has a very specific use of\n" );
  37. printf( " removing shadows from Gnome's window selections right now. GEOMETRY is in\n" );
  38. printf( " format WxH+X+Y\n" );
  39. printf( "\n" );
  40. printf( "Examples\n" );
  41. printf( " $ # gray, thick border for maximum visiblity.\n" );
  42. printf( " $ slop -b=20 -c=0.5,0.5,0.5\n" );
  43. printf( "\n" );
  44. printf( " $ # Remove window decorations, but include the 28px titlebar. Useful to remove the arbitrarily sized shadows in Gnome where they are included in window geometry for whatever reason.\n" );
  45. printf( " $ slop -nd -o=0x28+0-28\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, "-o=", "--offset=" ) ) {
  64. int err = parseGeometry( arg, &m_offsetx, &m_offsety, &m_offsetw, &m_offseth );
  65. if ( err ) {
  66. return 1;
  67. }
  68. } else if ( matches( arg, "-p=", "--padding=" ) ) {
  69. int err = parseInt( arg, &m_padding );
  70. if ( err ) {
  71. return 1;
  72. }
  73. } else if ( matches( arg, "-c=", "--color=" ) ) {
  74. int err = parseColor( arg, &m_red, &m_green, &m_blue );
  75. if ( err ) {
  76. return 1;
  77. }
  78. } else if ( matches( arg, "-t=", "--tolerance=" ) ) {
  79. int err = parseInt( arg, &m_tolerance );
  80. if ( err ) {
  81. return 1;
  82. }
  83. if ( m_tolerance < 0 ) {
  84. m_tolerance = 0;
  85. }
  86. } else if ( matches( arg, "-g=", "--gracetime=" ) ) {
  87. int err = parseFloat( arg, &m_gracetime );
  88. if ( err ) {
  89. return 1;
  90. }
  91. if ( m_gracetime < 0 ) {
  92. m_gracetime = 0;
  93. }
  94. } else if ( matches( arg, "-x=", "--xdisplay=" ) ) {
  95. int err = parseString( arg, &m_xdisplay );
  96. if ( err ) {
  97. return 1;
  98. }
  99. } else if ( matches( arg, "-nkb", "--nokeyboard" ) ) {
  100. m_keyboard = false;
  101. } else if ( matches( arg, "-nd", "--nodecorations" ) ) {
  102. m_decorations = false;
  103. } else if ( matches( arg, "-h", "--help" ) ) {
  104. printHelp();
  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. }