main.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /* main.cpp: parses options, runs slop, prints results.
  2. *
  3. * Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
  4. *
  5. * This file is part of Slop.
  6. *
  7. * Slop is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * Slop is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with Slop. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. #include <iostream>
  21. #include <sstream>
  22. #include "slop.hpp"
  23. #include "options.hpp"
  24. using namespace slop;
  25. template<typename Out>
  26. static void split(const std::string &s, char delim, Out result) {
  27. std::stringstream ss;
  28. ss.str(s);
  29. std::string item;
  30. while (std::getline(ss, item, delim)) {
  31. *(result++) = item;
  32. }
  33. }
  34. static std::vector<std::string> split(const std::string &s, char delim) {
  35. std::vector<std::string> elems;
  36. split(s, delim, std::back_inserter(elems));
  37. return elems;
  38. }
  39. SlopOptions* getOptions( Options& options ) {
  40. SlopOptions* foo = new SlopOptions();
  41. options.getFloat("bordersize", 'b', foo->borderSize);
  42. options.getFloat("padding", 'p', foo->padding);
  43. options.getFloat("tolerance", 't', foo->tolerance);
  44. glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
  45. options.getColor("color", 'c', color);
  46. options.getBool("nokeyboard", 'k', foo->nokeyboard);
  47. options.getBool("noopengl", 'o', foo->noopengl);
  48. options.getString( "xdisplay", 'x', foo->xdisplay );
  49. std::string shaders = "textured";
  50. options.getString( "shader", 'r', shaders );
  51. foo->shaders = split( shaders, ',' );
  52. foo->r = color.r;
  53. foo->g = color.g;
  54. foo->b = color.b;
  55. foo->a = color.a;
  56. options.getBool("highlight", 'l', foo->highlight);
  57. try {
  58. bool test = false;
  59. options.getBool("nodecorations", 'n', test);
  60. if ( test ) {
  61. foo->nodecorations = 1;
  62. }
  63. } catch( ... ) {
  64. options.getInt("nodecorations", 'n', foo->nodecorations);
  65. }
  66. if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
  67. throw new std::invalid_argument( "--nodecorations must be between 0 and 2. Or be used as a flag." );
  68. }
  69. return foo;
  70. }
  71. std::string formatOutput( std::string input, SlopSelection selection, bool cancelled ) {
  72. std::stringstream output;
  73. for( unsigned int i=0;i<input.length();i++) {
  74. if ( input[i] == '%' ) {
  75. if ( input.length() <= i+1 ) {
  76. throw new std::invalid_argument( "Expected character after `%`, got END." );
  77. }
  78. switch( input[i+1] ) {
  79. case 'x':
  80. case 'X': output << round(selection.x); break;
  81. case 'y':
  82. case 'Y': output << round(selection.y); break;
  83. case 'w':
  84. case 'W': output << round(selection.w); break;
  85. case 'c':
  86. case 'C': output << cancelled; break;
  87. case 'h':
  88. case 'H': output << round(selection.h); break;
  89. case 'g':
  90. case 'G': output << round(selection.w) << "x" << round(selection.h)
  91. << "+" << round(selection.x) << "+" << round(selection.y); break;
  92. case 'i':
  93. case 'I': output << selection.id; break;
  94. case '%': output << "%"; break;
  95. default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, c, or % after % in format. Got `" + input[i+1] + "`." );
  96. }
  97. i++;
  98. continue;
  99. }
  100. output << input[i];
  101. }
  102. return output.str();
  103. }
  104. void printHelp() {
  105. std::cout << "slop v5.3.21\n";
  106. std::cout << "\n";
  107. std::cout << "Copyright (C) 2017 Dalton Nell, Slop Contributors\n";
  108. std::cout << "(https://github.com/naelstrof/slop/graphs/contributors)\n";
  109. std::cout << "Usage: slop [options]\n";
  110. std::cout << "\n";
  111. std::cout << "slop (Select Operation) is an application that queries for a selection from the\n";
  112. std::cout << "user and prints the region to stdout.\n";
  113. std::cout << "\n";
  114. std::cout << "-h, --help Print help and exit\n";
  115. std::cout << "-v, --version Print version and exit\n";
  116. std::cout << "Options\n";
  117. std::cout << " -x, --xdisplay=hostname:number.screen_number\n";
  118. std::cout << " Sets the x display.\n";
  119. std::cout << " -k, --nokeyboard Disables the ability to cancel selections with\n";
  120. std::cout << " the keyboard. (default=off)\n";
  121. std::cout << " -b, --bordersize=FLOAT Set the selection rectangle's thickness.\n";
  122. std::cout << " (default=`1')\n";
  123. std::cout << " -p, --padding=FLOAT Set the padding size of the selection. Can be\n";
  124. std::cout << " negative. (default=`0')\n";
  125. std::cout << " -t, --tolerance=FLOAT How far in pixels the mouse can move after\n";
  126. std::cout << " clicking and still be detected as a normal\n";
  127. std::cout << " click instead of a click and drag. Setting\n";
  128. std::cout << " this to 0 will disable window selections.\n";
  129. std::cout << " Alternatively setting it to 999999 would.\n";
  130. std::cout << " only allow for window selections.\n";
  131. std::cout << " (default=`2')\n";
  132. std::cout << " -c, --color=FLOAT,FLOAT,FLOAT,FLOAT\n";
  133. std::cout << " Set the selection rectangle's color. Supports\n";
  134. std::cout << " RGB or RGBA values.\n";
  135. std::cout << " (default=`0.5,0.5,0.5,1')\n";
  136. std::cout << " -n, --nodecorations=INT Attempt to select child windows in order to\n";
  137. std::cout << " avoid window decorations. Setting this to\n";
  138. std::cout << " 1 will enable a light attempt to\n";
  139. std::cout << " remove decorations. Setting this to 2 will\n";
  140. std::cout << " enable aggressive decoration removal.\n";
  141. std::cout << " Supplying slop with just `-n` is\n";
  142. std::cout << " equivalent to supplying `-n1`.\n";
  143. std::cout << " (default=`0')\n";
  144. std::cout << " -q, --quiet Disable any unnecessary cerr output. Any\n";
  145. std::cout << " warnings simply won't print.\n";
  146. std::cout << " -l, --highlight Instead of outlining selections, slop\n";
  147. std::cout << " highlights it. This is only useful when\n";
  148. std::cout << " --color is set to a transparent color.\n";
  149. std::cout << " (default=off)\n";
  150. std::cout << " -r, --shader=STRING Sets the shader to load and use from\n";
  151. std::cout << " ~/.config/slop/\n";
  152. std::cout << " -f, --format=STRING Set the output format string. Format specifiers\n";
  153. std::cout << " are %x, %y, %w, %h, %i, %g, and %c.\n";
  154. std::cout << " (default=`%g\n')\n";
  155. std::cout << " -o, --noopengl Disable graphics acceleration.\n";
  156. std::cout << "Examples\n";
  157. std::cout << " $ # Gray, thick, transparent border for maximum visiblity.\n";
  158. std::cout << " $ slop -b 20 -c 0.5,0.5,0.5,0.8\n";
  159. std::cout << "\n";
  160. std::cout << " $ # Remove window decorations.\n";
  161. std::cout << " $ slop --nodecorations\n";
  162. std::cout << "\n";
  163. std::cout << " $ # Disable window selections. Useful for selecting individual pixels.\n";
  164. std::cout << " $ slop -t 0\n";
  165. std::cout << "\n";
  166. std::cout << " $ # Classic Windows XP selection.\n";
  167. std::cout << " $ slop -l -c 0.3,0.4,0.6,0.4\n";
  168. std::cout << "\n";
  169. std::cout << " $ # Read slop output for use in scripts.\n";
  170. std::cout << " $ read -r X Y W H G ID < <(slop -f '%x %y %w %h %g %i')\n";
  171. std::cout << "\n";
  172. std::cout << "Tips\n";
  173. std::cout << " * If you don't like a selection: you can cancel it by right-clicking\n";
  174. std::cout << "regardless of which options are enabled or disabled for slop.\n";
  175. std::cout << " * If slop doesn't seem to select a window accurately, the problem could be\n";
  176. std::cout << "because of decorations getting in the way. Try enabling the --nodecorations\n";
  177. std::cout << "flag.\n";
  178. }
  179. int app( int argc, char** argv ) {
  180. // Options just validates all of our input from argv
  181. Options options( argc, argv );
  182. bool quiet = false;
  183. options.getBool( "quiet", 'q', quiet );
  184. bool help = false;
  185. if ( options.getBool( "help", 'h', help ) ) {
  186. printHelp();
  187. return 0;
  188. }
  189. if ( options.getBool( "version", 'v', help ) ) {
  190. std::cout << SLOP_VERSION << "\n";
  191. return 0;
  192. }
  193. // We then parse the options into something slop can understand.
  194. SlopOptions* parsedOptions = getOptions( options );
  195. // We want to validate our format option if we got one, we do that by just doing a dry run
  196. // on a fake selection.
  197. SlopSelection selection(0,0,0,0,0);
  198. std::string format;
  199. bool gotFormat = options.getString("format", 'f', format);
  200. if ( gotFormat ) {
  201. formatOutput( format, selection, false );
  202. }
  203. // Finally we do the real selection.
  204. bool cancelled = false;
  205. selection = SlopSelect(parsedOptions, &cancelled, quiet);
  206. // Here we're done with the parsed option data.
  207. delete parsedOptions;
  208. // We know if we cancelled or not
  209. if ( cancelled ) {
  210. if ( !quiet ) {
  211. std::cerr << "Selection was cancelled by keystroke or right-click.\n";
  212. }
  213. return 1;
  214. }
  215. // If we recieved a format option, we output the specified output.
  216. if ( gotFormat ) {
  217. std::cout << formatOutput( format, selection, cancelled );
  218. return 0;
  219. }
  220. std::cout << formatOutput( "%g\n", selection, cancelled );
  221. return 0;
  222. }
  223. int main( int argc, char** argv ) {
  224. try {
  225. return app( argc, argv );
  226. } catch( std::exception* e ) {
  227. std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
  228. return 1;
  229. } // let the operating system handle any other kind of exception.
  230. return 1;
  231. }