/* 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; 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 std::runtime_error("dur"); } } else { found[3] = 1; } } catch ( ... ) { 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."); } return found; } SlopOptions* getOptions( cxxopts::Options& options ) { slop::SlopOptions* foo = new slop::SlopOptions(); if ( options.count( "bordersize" ) > 0 ) { foo->border = 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 ) { std::string xdisplay = options["xdisplay"].as(); char* cxdisplay = new char[xdisplay.length()+1]; memcpy( cxdisplay, xdisplay.c_str(), xdisplay.length() ); cxdisplay[xdisplay.length()]='\0'; foo->xdisplay = cxdisplay; } if ( options.count( "shader" ) > 0 ) { std::string shaders = options["shader"].as(); char* cshaders = new char[shaders.length()+1]; memcpy( cshaders, shaders.c_str(), shaders.length() ); cshaders[shaders.length()]='\0'; foo->shaders = cshaders; } if ( options.count( "noopengl" ) > 0 ) { foo->noopengl = options["noopengl"].as(); } if ( options.count( "highlight" ) > 0 ) { foo->highlight = options["highlight"].as(); } if ( options.count( "quiet" ) > 0 ) { foo->quiet = options["quiet"].as(); } if ( options.count( "nodecorations" ) > 0 ) { foo->nodecorations = options["nodecorations"].as(); if ( foo->nodecorations < 0 || foo->nodecorations > 2 ) { throw 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 ) { 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 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()->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 auto& positional = options["positional"].as>(); if ( positional.size() > 0 ) { throw 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,true); std::string format; bool gotFormat = options.count( "format" ) > 0; if ( gotFormat ) { format = options["format"].as(); formatOutput( format, selection ); } // Finally we do the real selection. selection = SlopSelect(parsedOptions); // Here we're done with the parsed option data. delete parsedOptions; // We know if we cancelled or not if ( selection.cancelled ) { if ( !parsedOptions->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 ); return 0; } std::cout << formatOutput( "%g\n", selection ); 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; }