main.cpp 9.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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. SlopOptions* getOptions( Options& options ) {
  26. SlopOptions* foo = new SlopOptions();
  27. options.getFloat("bordersize", 'b', foo->borderSize);
  28. options.getFloat("padding", 'p', foo->padding);
  29. options.getFloat("tolerance", 't', foo->tolerance);
  30. glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
  31. options.getColor("color", 'c', color);
  32. options.getBool("nokeyboard", 'k', foo->nokeyboard);
  33. options.getBool("noopengl", 'o', foo->noopengl);
  34. options.getString( "xdisplay", 'x', foo->xdisplay );
  35. options.getString( "shader", 's', foo->shader );
  36. foo->r = color.r;
  37. foo->g = color.g;
  38. foo->b = color.b;
  39. foo->a = color.a;
  40. options.getBool("highlight", 'l', foo->highlight);
  41. options.getInt("nodecorations", 'n', foo->nodecorations);
  42. return foo;
  43. }
  44. std::string formatOutput( std::string input, SlopSelection selection, bool cancelled ) {
  45. std::stringstream output;
  46. for( unsigned int i=0;i<input.length();i++) {
  47. if ( input[i] == '%' ) {
  48. if ( input.length() <= i+1 ) {
  49. throw new std::invalid_argument( "Expected character after `%`, got END." );
  50. }
  51. switch( input[i+1] ) {
  52. case 'x':
  53. case 'X': output << round(selection.x); break;
  54. case 'y':
  55. case 'Y': output << round(selection.y); break;
  56. case 'w':
  57. case 'W': output << round(selection.w); break;
  58. case 'c':
  59. case 'C': output << cancelled; break;
  60. case 'h':
  61. case 'H': output << round(selection.h); break;
  62. case 'g':
  63. case 'G': output << round(selection.w) << "x" << round(selection.h)
  64. << "+" << round(selection.x) << "+" << round(selection.y); break;
  65. case 'i':
  66. case 'I': output << selection.id; break;
  67. case '%': output << "%"; break;
  68. default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, c, or % after % in format. Got `" + input[i+1] + "`." );
  69. }
  70. i++;
  71. continue;
  72. }
  73. output << input[i];
  74. }
  75. return output.str();
  76. }
  77. void printHelp() {
  78. std::cout << "slop v5.3.21\n";
  79. std::cout << "\n";
  80. std::cout << "Copyright (C) 2017 Dalton Nell, Slop Contributors\n";
  81. std::cout << "(https://github.com/naelstrof/slop/graphs/contributors)\n";
  82. std::cout << "Usage: slop [options]\n";
  83. std::cout << "\n";
  84. std::cout << "slop (Select Operation) is an application that queries for a selection from the\n";
  85. std::cout << "user and prints the region to stdout.\n";
  86. std::cout << "\n";
  87. std::cout << "-h, --help Print help and exit\n";
  88. std::cout << "-v, --version Print version and exit\n";
  89. std::cout << "Options\n";
  90. std::cout << " -x, --xdisplay=hostname:number.screen_number\n";
  91. std::cout << " Sets the x display.\n";
  92. std::cout << " -k, --nokeyboard Disables the ability to cancel selections with\n";
  93. std::cout << " the keyboard. (default=off)\n";
  94. std::cout << " -b, --bordersize=FLOAT Set the selection rectangle's thickness.\n";
  95. std::cout << " (default=`1')\n";
  96. std::cout << " -p, --padding=FLOAT Set the padding size of the selection. Can be\n";
  97. std::cout << " negative. (default=`0')\n";
  98. std::cout << " -t, --tolerance=FLOAT How far in pixels the mouse can move after\n";
  99. std::cout << " clicking and still be detected as a normal\n";
  100. std::cout << " click instead of a click and drag. Setting\n";
  101. std::cout << " this to 0 will disable window selections.\n";
  102. std::cout << " Alternatively setting it to 999999 would.\n";
  103. std::cout << " only allow for window selections.\n";
  104. std::cout << " (default=`2')\n";
  105. std::cout << " -c, --color=FLOAT,FLOAT,FLOAT,FLOAT\n";
  106. std::cout << " Set the selection rectangle's color. Supports\n";
  107. std::cout << " RGB or RGBA values.\n";
  108. std::cout << " (default=`0.5,0.5,0.5,1')\n";
  109. std::cout << " -n, --nodecorations=INT Attempt to select child windows in order to\n";
  110. std::cout << " avoid window decorations. Setting this to\n";
  111. std::cout << " 1 will enable a light attempt to\n";
  112. std::cout << " remove decorations. Setting this to 2 will\n";
  113. std::cout << " enable aggressive decoration removal.\n";
  114. std::cout << " (default=`0')\n";
  115. std::cout << " -q, --quiet Disable any unnecessary cerr output. Any\n";
  116. std::cout << " warnings simply won't print.\n";
  117. std::cout << " -l, --highlight Instead of outlining selections, slop\n";
  118. std::cout << " highlights it. This is only useful when\n";
  119. std::cout << " --color is set to a transparent color.\n";
  120. std::cout << " (default=off)\n";
  121. std::cout << " -r, --shader=STRING Sets the shader to load and use from\n";
  122. std::cout << " ~/.config/slop/\n";
  123. std::cout << " -f, --format=STRING Set the output format string. Format specifiers\n";
  124. std::cout << " are %x, %y, %w, %h, %i, %g, and %c.\n";
  125. std::cout << " (default=`%g\n')\n";
  126. std::cout << " -o, --noopengl Disable graphics acceleration.\n";
  127. std::cout << "Examples\n";
  128. std::cout << " $ # Gray, thick, transparent border for maximum visiblity.\n";
  129. std::cout << " $ slop -b 20 -c 0.5,0.5,0.5,0.8\n";
  130. std::cout << "\n";
  131. std::cout << " $ # Remove window decorations.\n";
  132. std::cout << " $ slop --nodecorations\n";
  133. std::cout << "\n";
  134. std::cout << " $ # Disable window selections. Useful for selecting individual pixels.\n";
  135. std::cout << " $ slop -t 0\n";
  136. std::cout << "\n";
  137. std::cout << " $ # Classic Windows XP selection.\n";
  138. std::cout << " $ slop -l -c 0.3,0.4,0.6,0.4\n";
  139. std::cout << "\n";
  140. std::cout << " $ # Read slop output for use in scripts.\n";
  141. std::cout << " $ read -r X Y W H G ID < <(slop -f '%x %y %w %h %g %i')\n";
  142. std::cout << "\n";
  143. std::cout << "Tips\n";
  144. std::cout << " * If you don't like a selection: you can cancel it by right-clicking\n";
  145. std::cout << "regardless of which options are enabled or disabled for slop.\n";
  146. std::cout << " * If slop doesn't seem to select a window accurately, the problem could be\n";
  147. std::cout << "because of decorations getting in the way. Try enabling the --nodecorations\n";
  148. std::cout << "flag.\n";
  149. }
  150. int app( int argc, char** argv ) {
  151. // Options just validates all of our input from argv
  152. Options options( argc, argv );
  153. bool quiet = false;
  154. options.getBool( "quiet", 'q', quiet );
  155. bool help = false;
  156. if ( options.getBool( "help", 'h', help ) ) {
  157. printHelp();
  158. return 0;
  159. }
  160. if ( options.getBool( "version", 'v', help ) ) {
  161. std::cout << SLOP_VERSION << "\n";
  162. return 0;
  163. }
  164. // We then parse the options into something slop can understand.
  165. SlopOptions* parsedOptions = getOptions( options );
  166. // We want to validate our format option if we got one, we do that by just doing a dry run
  167. // on a fake selection.
  168. SlopSelection selection(0,0,0,0,0);
  169. std::string format;
  170. bool gotFormat = options.getString("format", 'f', format);
  171. if ( gotFormat ) {
  172. formatOutput( format, selection, false );
  173. }
  174. // Finally we do the real selection.
  175. bool cancelled = false;
  176. selection = SlopSelect(parsedOptions, &cancelled, quiet);
  177. // Here we're done with the parsed option data.
  178. delete parsedOptions;
  179. // We know if we cancelled or not
  180. if ( cancelled ) {
  181. if ( !quiet ) {
  182. std::cerr << "Selection was cancelled by keystroke or right-click.\n";
  183. }
  184. return 1;
  185. }
  186. // If we recieved a format option, we output the specified output.
  187. if ( gotFormat ) {
  188. std::cout << formatOutput( format, selection, cancelled );
  189. return 0;
  190. }
  191. std::cout << formatOutput( "%g\n", selection, cancelled );
  192. return 0;
  193. }
  194. int main( int argc, char** argv ) {
  195. try {
  196. return app( argc, argv );
  197. } catch( std::exception* e ) {
  198. std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
  199. return 1;
  200. } // let the operating system handle any other kind of exception.
  201. return 1;
  202. }