main.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 <string>
  23. #include <vector>
  24. #include <glm/glm.hpp>
  25. #include "slop.hpp"
  26. #include "cxxopts.hpp"
  27. using namespace slop;
  28. template<typename Out>
  29. static void split(const std::string &s, char delim, Out result) {
  30. std::stringstream ss;
  31. ss.str(s);
  32. std::string item;
  33. while (std::getline(ss, item, delim)) {
  34. *(result++) = item;
  35. }
  36. }
  37. static std::vector<std::string> split(const std::string &s, char delim) {
  38. std::vector<std::string> elems;
  39. split(s, delim, std::back_inserter(elems));
  40. return elems;
  41. }
  42. glm::vec4 parseColor( std::string value ) {
  43. std::string valuecopy = value;
  44. glm::vec4 found;
  45. std::string::size_type sz;
  46. try {
  47. found[0] = std::stof(value,&sz);
  48. value = value.substr(sz+1);
  49. found[1] = std::stof(value,&sz);
  50. value = value.substr(sz+1);
  51. found[2] = std::stof(value,&sz);
  52. if ( value.size() != sz ) {
  53. value = value.substr(sz+1);
  54. found[3] = std::stof(value,&sz);
  55. if ( value.size() != sz ) {
  56. throw "dur";
  57. }
  58. } else {
  59. found[3] = 1;
  60. }
  61. } catch ( ... ) {
  62. throw new std::invalid_argument("Unable to parse value `" + valuecopy + "` as a color. Should be in the format r,g,b or r,g,b,a. Like 1,1,1,1.");
  63. }
  64. return found;
  65. }
  66. SlopOptions* getOptions( cxxopts::Options& options ) {
  67. slop::SlopOptions* foo = new slop::SlopOptions();
  68. if ( options.count( "bordersize" ) > 0 ) {
  69. foo->borderSize = options["bordersize"].as<float>();
  70. }
  71. if ( options.count( "padding" ) > 0 ) {
  72. foo->padding = options["padding"].as<float>();
  73. }
  74. if ( options.count( "tolerance" ) > 0 ) {
  75. foo->tolerance = options["tolerance"].as<float>();
  76. }
  77. glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
  78. if ( options.count( "color" ) > 0 ) {
  79. color = parseColor( options["color"].as<std::string>() );
  80. }
  81. foo->r = color.r;
  82. foo->g = color.g;
  83. foo->b = color.b;
  84. foo->a = color.a;
  85. if ( options.count( "nokeyboard" ) > 0 ) {
  86. foo->nokeyboard = options["nokeyboard"].as<bool>();
  87. }
  88. if ( options.count( "xdisplay" ) > 0 ) {
  89. foo->xdisplay = options["xdisplay"].as<std::string>();
  90. }
  91. std::string shaders = "textured";
  92. if ( options.count( "shader" ) > 0 ) {
  93. shaders = options["shader"].as<std::string>();
  94. }
  95. foo->shaders = split( shaders, ',' );
  96. if ( options.count( "noopengl" ) > 0 ) {
  97. foo->noopengl = options["noopengl"].as<bool>();
  98. }
  99. if ( options.count( "highlight" ) > 0 ) {
  100. foo->highlight = options["highlight"].as<bool>();
  101. }
  102. if ( options.count( "nodecorations" ) > 0 ) {
  103. foo->nodecorations = options["nodecorations"].as<int>();
  104. if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
  105. throw new std::invalid_argument( "--nodecorations must be between 0 and 2. Or be used as a flag." );
  106. }
  107. }
  108. return foo;
  109. }
  110. std::string formatOutput( std::string input, SlopSelection selection, bool cancelled ) {
  111. std::stringstream output;
  112. for( unsigned int i=0;i<input.length();i++) {
  113. if ( input[i] == '%' ) {
  114. if ( input.length() <= i+1 ) {
  115. throw new std::invalid_argument( "Expected character after `%`, got END." );
  116. }
  117. switch( input[i+1] ) {
  118. case 'x':
  119. case 'X': output << round(selection.x); break;
  120. case 'y':
  121. case 'Y': output << round(selection.y); break;
  122. case 'w':
  123. case 'W': output << round(selection.w); break;
  124. case 'c':
  125. case 'C': output << cancelled; break;
  126. case 'h':
  127. case 'H': output << round(selection.h); break;
  128. case 'g':
  129. case 'G': output << round(selection.w) << "x" << round(selection.h)
  130. << "+" << round(selection.x) << "+" << round(selection.y); break;
  131. case 'i':
  132. case 'I': output << selection.id; break;
  133. case '%': output << "%"; break;
  134. default: throw new std::invalid_argument( std::string()+"Expected x, y, w, h, g, i, c, or % after % in format. Got `" + input[i+1] + "`." );
  135. }
  136. i++;
  137. continue;
  138. }
  139. output << input[i];
  140. }
  141. return output.str();
  142. }
  143. void printHelp() {
  144. std::cout << "slop v5.3.21\n";
  145. std::cout << "\n";
  146. std::cout << "Copyright (C) 2017 Dalton Nell, Slop Contributors\n";
  147. std::cout << "(https://github.com/naelstrof/slop/graphs/contributors)\n";
  148. std::cout << "Usage: slop [options]\n";
  149. std::cout << "\n";
  150. std::cout << "slop (Select Operation) is an application that queries for a selection from the\n";
  151. std::cout << "user and prints the region to stdout.\n";
  152. std::cout << "\n";
  153. std::cout << "-h, --help Print help and exit\n";
  154. std::cout << "-v, --version Print version and exit\n";
  155. std::cout << "Options\n";
  156. std::cout << " -x, --xdisplay=hostname:number.screen_number\n";
  157. std::cout << " Sets the x display.\n";
  158. std::cout << " -k, --nokeyboard Disables the ability to cancel selections with\n";
  159. std::cout << " the keyboard. (default=off)\n";
  160. std::cout << " -b, --bordersize=FLOAT Set the selection rectangle's thickness.\n";
  161. std::cout << " (default=`1')\n";
  162. std::cout << " -p, --padding=FLOAT Set the padding size of the selection. Can be\n";
  163. std::cout << " negative. (default=`0')\n";
  164. std::cout << " -t, --tolerance=FLOAT How far in pixels the mouse can move after\n";
  165. std::cout << " clicking and still be detected as a normal\n";
  166. std::cout << " click instead of a click and drag. Setting\n";
  167. std::cout << " this to 0 will disable window selections.\n";
  168. std::cout << " Alternatively setting it to 999999 would.\n";
  169. std::cout << " only allow for window selections.\n";
  170. std::cout << " (default=`2')\n";
  171. std::cout << " -c, --color=FLOAT,FLOAT,FLOAT,FLOAT\n";
  172. std::cout << " Set the selection rectangle's color. Supports\n";
  173. std::cout << " RGB or RGBA values.\n";
  174. std::cout << " (default=`0.5,0.5,0.5,1')\n";
  175. std::cout << " -n, --nodecorations=INT Attempt to select child windows in order to\n";
  176. std::cout << " avoid window decorations. Setting this to\n";
  177. std::cout << " 1 will enable a light attempt to\n";
  178. std::cout << " remove decorations. Setting this to 2 will\n";
  179. std::cout << " enable aggressive decoration removal.\n";
  180. std::cout << " Supplying slop with just `-n` is\n";
  181. std::cout << " equivalent to supplying `-n1`.\n";
  182. std::cout << " (default=`0')\n";
  183. std::cout << " -q, --quiet Disable any unnecessary cerr output. Any\n";
  184. std::cout << " warnings simply won't print.\n";
  185. std::cout << " -l, --highlight Instead of outlining selections, slop\n";
  186. std::cout << " highlights it. This is only useful when\n";
  187. std::cout << " --color is set to a transparent color.\n";
  188. std::cout << " (default=off)\n";
  189. std::cout << " -r, --shader=STRING Sets the shader to load and use from\n";
  190. std::cout << " ~/.config/slop/\n";
  191. std::cout << " -f, --format=STRING Set the output format string. Format specifiers\n";
  192. std::cout << " are %x, %y, %w, %h, %i, %g, and %c.\n";
  193. std::cout << " (default=`%g\n')\n";
  194. std::cout << " -o, --noopengl Disable graphics acceleration.\n";
  195. std::cout << "Examples\n";
  196. std::cout << " $ # Gray, thick, transparent border for maximum visiblity.\n";
  197. std::cout << " $ slop -b 20 -c 0.5,0.5,0.5,0.8\n";
  198. std::cout << "\n";
  199. std::cout << " $ # Remove window decorations.\n";
  200. std::cout << " $ slop --nodecorations\n";
  201. std::cout << "\n";
  202. std::cout << " $ # Disable window selections. Useful for selecting individual pixels.\n";
  203. std::cout << " $ slop -t 0\n";
  204. std::cout << "\n";
  205. std::cout << " $ # Classic Windows XP selection.\n";
  206. std::cout << " $ slop -l -c 0.3,0.4,0.6,0.4\n";
  207. std::cout << "\n";
  208. std::cout << " $ # Read slop output for use in scripts.\n";
  209. std::cout << " $ read -r X Y W H G ID < <(slop -f '%x %y %w %h %g %i')\n";
  210. std::cout << "\n";
  211. std::cout << "Tips\n";
  212. std::cout << " * If you don't like a selection: you can cancel it by right-clicking\n";
  213. std::cout << "regardless of which options are enabled or disabled for slop.\n";
  214. std::cout << " * If slop doesn't seem to select a window accurately, the problem could be\n";
  215. std::cout << "because of decorations getting in the way. Try enabling the --nodecorations\n";
  216. std::cout << "flag.\n";
  217. }
  218. int app( int argc, char** argv ) {
  219. cxxopts::Options options("maim", "Screenshot application.");
  220. options.add_options()
  221. ("h,help", "Print help and exit.")
  222. ("v,version", "Print version and exit.")
  223. ("x,xdisplay", "Sets the xdisplay to use", cxxopts::value<std::string>())
  224. ("f,format", "Sets the output format for slop. Format specifiers are %x, %y, %w, %h, %i, %c, and %g. If actual percentage signs are desired in output, use a double percentage sign like so `%%`.", cxxopts::value<std::string>())
  225. ("b,bordersize", "Sets the selection rectangle's thickness.", cxxopts::value<float>())
  226. ("p,padding", "Sets the padding size for the selection, this can be negative.", cxxopts::value<float>())
  227. ("t,tolerance", "How far in pixels the mouse can move after clicking, and still be detected as a normal click instead of a click-and-drag. Setting this to 0 will disable window selections. Alternatively setting it to 9999999 would force a window selection.", cxxopts::value<float>())
  228. ("c,color", "Sets the selection rectangle's color. Supports RGB or RGBA input. Depending on the system's window manager/OpenGL support, the opacity may be ignored.", cxxopts::value<std::string>())
  229. ("r,shader", "This sets the vertex shader, and fragment shader combo to use when drawing the final framebuffer to the screen. This obviously only works when OpenGL is enabled. The shaders are loaded from ~/.config/maim. See https://github.com/naelstrof/slop for more information on how to create your own shaders.", cxxopts::value<std::string>())
  230. ("n,nodecorations", "Sets the level of aggressiveness when trying to remove window decroations. `0' is off, `1' will try lightly to remove decorations, and `2' will recursively descend into the root tree until it gets the deepest available visible child under the mouse. Defaults to `0'.", cxxopts::value<int>()->implicit_value("1"))
  231. ("l,highlight", "Instead of outlining a selection, maim will highlight it instead. This is particularly useful if the color is set to an opacity lower than 1.")
  232. ("q,quiet", "Disable any unnecessary cerr output. Any warnings or info simply won't print.")
  233. ("k,nokeyboard", "Disables the ability to cancel selections with the keyboard.")
  234. ("o,noopengl", "Disables graphics hardware acceleration.")
  235. ("positional", "Positional parameters", cxxopts::value<std::vector<std::string>>())
  236. ;
  237. options.parse_positional("positional");
  238. options.parse(argc, argv);
  239. // Options just validates all of our input from argv
  240. bool quiet = false;
  241. if ( options.count( "quiet" ) > 0 ) {
  242. quiet = options["quiet"].as<bool>();
  243. }
  244. auto& positional = options["positional"].as<std::vector<std::string>>();
  245. if ( positional.size() > 0 ) {
  246. throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
  247. }
  248. bool help = false;
  249. if ( options.count( "help" ) > 0 ) {
  250. help = options["help"].as<bool>();
  251. }
  252. if ( help ) {
  253. printHelp();
  254. return 0;
  255. }
  256. bool version = false;
  257. if ( options.count( "version" ) > 0 ) {
  258. version = options["version"].as<bool>();
  259. }
  260. if ( version ) {
  261. std::cout << SLOP_VERSION << "\n";
  262. return 0;
  263. }
  264. // We then parse the options into something slop can understand.
  265. SlopOptions* parsedOptions = getOptions( options );
  266. // We want to validate our format option if we got one, we do that by just doing a dry run
  267. // on a fake selection.
  268. SlopSelection selection(0,0,0,0,0);
  269. std::string format;
  270. bool gotFormat = options.count( "format" ) > 0;
  271. if ( gotFormat ) {
  272. format = options["format"].as<std::string>();
  273. formatOutput( format, selection, false );
  274. }
  275. // Finally we do the real selection.
  276. bool cancelled = false;
  277. selection = SlopSelect(parsedOptions, &cancelled, quiet);
  278. // Here we're done with the parsed option data.
  279. delete parsedOptions;
  280. // We know if we cancelled or not
  281. if ( cancelled ) {
  282. if ( !quiet ) {
  283. std::cerr << "Selection was cancelled by keystroke or right-click.\n";
  284. }
  285. return 1;
  286. }
  287. // If we recieved a format option, we output the specified output.
  288. if ( gotFormat ) {
  289. std::cout << formatOutput( format, selection, cancelled );
  290. return 0;
  291. }
  292. std::cout << formatOutput( "%g\n", selection, cancelled );
  293. return 0;
  294. }
  295. int main( int argc, char** argv ) {
  296. try {
  297. return app( argc, argv );
  298. } catch( std::exception* e ) {
  299. std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
  300. return 1;
  301. } // let the operating system handle any other kind of exception.
  302. return 1;
  303. }