/* main.cpp: parses options, runs slop, prints results.
*
* Copyright (C) 2014: Dalton Nell, Slop Contributors (https://github.com/naelstrof/slop/graphs/contributors).
*
* This file is part of Slop.
*
* Slop is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Slop is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Slop. If not, see .
*/
#include
#include
#include
#include
#include
#include "slop.hpp"
#include "cxxopts.hpp"
using namespace slop;
template
static void split(const std::string &s, char delim, Out result) {
std::stringstream ss;
ss.str(s);
std::string item;
while (std::getline(ss, item, delim)) {
*(result++) = item;
}
}
static std::vector split(const std::string &s, char delim) {
std::vector elems;
split(s, delim, std::back_inserter(elems));
return elems;
}
glm::vec4 parseColor( std::string value ) {
std::string valuecopy = value;
glm::vec4 found;
std::string::size_type sz;
try {
found[0] = std::stof(value,&sz);
value = value.substr(sz+1);
found[1] = std::stof(value,&sz);
value = value.substr(sz+1);
found[2] = std::stof(value,&sz);
if ( value.size() != sz ) {
value = value.substr(sz+1);
found[3] = std::stof(value,&sz);
if ( value.size() != sz ) {
throw "dur";
}
} else {
found[3] = 1;
}
} catch ( ... ) {
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.");
}
return found;
}
SlopOptions* getOptions( cxxopts::Options& options ) {
slop::SlopOptions* foo = new slop::SlopOptions();
if ( options.count( "bordersize" ) > 0 ) {
foo->borderSize = options["bordersize"].as();
}
if ( options.count( "padding" ) > 0 ) {
foo->padding = options["padding"].as();
}
if ( options.count( "tolerance" ) > 0 ) {
foo->tolerance = options["tolerance"].as();
}
glm::vec4 color = glm::vec4( foo->r, foo->g, foo->b, foo->a );
if ( options.count( "color" ) > 0 ) {
color = parseColor( options["color"].as() );
}
foo->r = color.r;
foo->g = color.g;
foo->b = color.b;
foo->a = color.a;
if ( options.count( "nokeyboard" ) > 0 ) {
foo->nokeyboard = options["nokeyboard"].as();
}
if ( options.count( "xdisplay" ) > 0 ) {
foo->xdisplay = options["xdisplay"].as();
}
std::string shaders = "textured";
if ( options.count( "shader" ) > 0 ) {
shaders = options["shader"].as();
}
foo->shaders = split( shaders, ',' );
if ( options.count( "noopengl" ) > 0 ) {
foo->noopengl = options["noopengl"].as();
}
if ( options.count( "highlight" ) > 0 ) {
foo->highlight = options["highlight"].as();
}
if ( options.count( "nodecorations" ) > 0 ) {
foo->nodecorations = options["nodecorations"].as();
if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) {
throw new std::invalid_argument( "--nodecorations must be between 0 and 2. Or be used as a flag." );
}
}
return foo;
}
std::string formatOutput( std::string input, SlopSelection selection, bool cancelled ) {
std::stringstream output;
for( unsigned int i=0;i())
("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())
("b,bordersize", "Sets the selection rectangle's thickness.", cxxopts::value())
("p,padding", "Sets the padding size for the selection, this can be negative.", cxxopts::value())
("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())
("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())
("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())
("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()->implicit_value("1"))
("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.")
("q,quiet", "Disable any unnecessary cerr output. Any warnings or info simply won't print.")
("k,nokeyboard", "Disables the ability to cancel selections with the keyboard.")
("o,noopengl", "Disables graphics hardware acceleration.")
("positional", "Positional parameters", cxxopts::value>())
;
options.parse_positional("positional");
options.parse(argc, argv);
// Options just validates all of our input from argv
bool quiet = false;
if ( options.count( "quiet" ) > 0 ) {
quiet = options["quiet"].as();
}
auto& positional = options["positional"].as>();
if ( positional.size() > 0 ) {
throw new std::invalid_argument("Unexpected positional argument: " + positional[0]);
}
bool help = false;
if ( options.count( "help" ) > 0 ) {
help = options["help"].as();
}
if ( help ) {
printHelp();
return 0;
}
bool version = false;
if ( options.count( "version" ) > 0 ) {
version = options["version"].as();
}
if ( version ) {
std::cout << SLOP_VERSION << "\n";
return 0;
}
// We then parse the options into something slop can understand.
SlopOptions* parsedOptions = getOptions( options );
// We want to validate our format option if we got one, we do that by just doing a dry run
// on a fake selection.
SlopSelection selection(0,0,0,0,0);
std::string format;
bool gotFormat = options.count( "format" ) > 0;
if ( gotFormat ) {
format = options["format"].as();
formatOutput( format, selection, false );
}
// Finally we do the real selection.
bool cancelled = false;
selection = SlopSelect(parsedOptions, &cancelled, quiet);
// Here we're done with the parsed option data.
delete parsedOptions;
// We know if we cancelled or not
if ( cancelled ) {
if ( !quiet ) {
std::cerr << "Selection was cancelled by keystroke or right-click.\n";
}
return 1;
}
// If we recieved a format option, we output the specified output.
if ( gotFormat ) {
std::cout << formatOutput( format, selection, cancelled );
return 0;
}
std::cout << formatOutput( "%g\n", selection, cancelled );
return 0;
}
int main( int argc, char** argv ) {
try {
return app( argc, argv );
} catch( std::exception* e ) {
std::cerr << "Slop encountered an error:\n" << e->what() << "\n";
return 1;
} // let the operating system handle any other kind of exception.
return 1;
}