main.cpp 15KB

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