From: Stephen Seo Date: Mon, 18 Jun 2018 11:25:38 +0000 (+0900) Subject: Add option parsing, minor fixes X-Git-Tag: 1.0~1 X-Git-Url: https://git.seodisparate.com/js/base.js?a=commitdiff_plain;h=d93f6e6a78a82c387f59bfe340167d94ec4c1e54;p=MeterForPulseAudio Add option parsing, minor fixes Update submodule AnotherDangParser --- diff --git a/AnotherDangParser b/AnotherDangParser index c3dad62..59d9d27 160000 --- a/AnotherDangParser +++ b/AnotherDangParser @@ -1 +1 @@ -Subproject commit c3dad6273a6b80cb35174e3bd217ee92fd762c07 +Subproject commit 59d9d27e47efc2e4d4f061ad3b80cbf13060bdfc diff --git a/src/Main.cpp b/src/Main.cpp index 0f3b480..27b4d47 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -1,8 +1,106 @@ +#include +#include +#include + +#include + +#include #include "MfPA/Meter.hpp" int main(int argc, char** argv) { - MfPA::Meter meter; + std::string sinkOrSourceName; + bool isSink = true; + unsigned int framerateLimit = 60; + sf::Color color(sf::Color::Green); + + ADP::AnotherDangParser parser; + parser.addLongOptionFlag( + "sink", + [&sinkOrSourceName, &isSink] (std::string opt) { + sinkOrSourceName = opt; + isSink = true; + }, + "Sets the sink to monitor (use default sink-monitor by default)"); + parser.addLongOptionFlag( + "source", + [&sinkOrSourceName, &isSink] (std::string opt) { + sinkOrSourceName = opt; + isSink = false; + }, + "Sets the source to monitor"); + parser.addOptionFlag( + "f", + [&framerateLimit] (std::string opt) { + try { + framerateLimit = std::stoul(opt); + } catch (const std::invalid_argument& e) { + std::cerr << "ERROR: Got invalid argument for \"-f\"" << std::endl; + std::exit(1); + } + }, + "Sets the framerate (default 60)"); + parser.addLongFlag( + "red", + [&color] () { + color = sf::Color::Red; + }, + "Sets the bar color to red"); + parser.addLongFlag( + "green", + [&color] () { + color = sf::Color::Green; + }, + "Sets the bar color to green (default)"); + parser.addLongFlag( + "blue", + [&color] () { + color = sf::Color::Blue; + }, + "Sets the bar color to blue"); + parser.addLongFlag( + "magenta", + [&color] () { + color = sf::Color::Magenta; + }, + "Sets the bar color to magenta"); + parser.addLongFlag( + "yellow", + [&color] () { + color = sf::Color::Yellow; + }, + "Sets the bar color to yellow"); + parser.addLongFlag( + "cyan", + [&color] () { + color = sf::Color::Cyan; + }, + "Sets the bar color to cyan"); + parser.addLongOptionFlag( + "color", + [&color] (std::string opt) { + unsigned int c = std::stoul(opt, nullptr, 16); + color.r = (c >> 16) & 0xFF; + color.g = (c >> 8) & 0xFF; + color.b = c & 0xFF; + }, + "Sets the bar color to a specified color (hex input like 0xFFFFFF, " + "red is most significant byte out of 3)"); + parser.addFlag( + "h", + [&parser] () { + parser.printHelp(); + std::exit(0); + }, + "Prints help text"); + parser.aliasFlag("-h", "--help"); + + if(!parser.parse(argc, argv)) + { + return 1; + } + + MfPA::Meter meter(sinkOrSourceName.c_str(), isSink, framerateLimit, color); meter.startMainLoop(); return 0; diff --git a/src/MfPA/Meter.cpp b/src/MfPA/Meter.cpp index ba1eb4d..8010ab8 100644 --- a/src/MfPA/Meter.cpp +++ b/src/MfPA/Meter.cpp @@ -7,10 +7,16 @@ #include -MfPA::Meter::Meter(const char* sinkOrSourceName, bool isSink) : +MfPA::Meter::Meter( + const char* sinkOrSourceName, + bool isSink, + unsigned int framerateLimit, + sf::Color barColor +) : currentState(WAITING), isMonitoringSink(isSink), sinkOrSourceName(sinkOrSourceName), +framerateLimit(framerateLimit), gotSinkInfo(false), gotSourceInfo(false), mainLoop(nullptr), @@ -18,10 +24,11 @@ context(nullptr), stream(nullptr), runFlag(true), channels(1), -window(sf::VideoMode(100,400), "Meter for PulseAudio") +window(sf::VideoMode(100,400), "Meter for PulseAudio"), +barColor(barColor) { window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, 1.0f, 1.0f))); - bar.setFillColor(sf::Color::Green); + bar.setFillColor(barColor); setenv("PULSE_PROP_application.name", "Meter for PulseAudio", 1); setenv("PULSE_PROP_application.icon_name", "multimedia-volume-control", 1); @@ -344,7 +351,7 @@ void MfPA::Meter::startMainLoop() [this] () { draw(); }, - 60, + framerateLimit, 1.0f / 120.0f); } @@ -440,8 +447,8 @@ void MfPA::Meter::draw() for(unsigned int i = 0; i < channels; ++i) { // prev levels - bar.setFillColor(sf::Color( - 0, 255, 0, 255 * std::get<1>(prevLevels[i]))); + barColor.a = 255 * std::get<1>(prevLevels[i]); + bar.setFillColor(barColor); bar.setSize(sf::Vector2f( 1.0f / (float)channels, std::get<0>(prevLevels[i]))); @@ -450,7 +457,8 @@ void MfPA::Meter::draw() 1.0f - std::get<0>(prevLevels[i]))); window.draw(bar); // levels - bar.setFillColor(sf::Color::Green); + barColor.a = 255; + bar.setFillColor(barColor); bar.setSize(sf::Vector2f( 1.0f / (float)channels, levels[i])); diff --git a/src/MfPA/Meter.hpp b/src/MfPA/Meter.hpp index f3eb4aa..4a07a7f 100644 --- a/src/MfPA/Meter.hpp +++ b/src/MfPA/Meter.hpp @@ -1,7 +1,7 @@ #ifndef METER_FOR_PULSEAUDIO_HPP #define METER_FOR_PULSEAUDIO_HPP -#define METER_DECAY_RATE 5.0f +#define METER_DECAY_RATE 2.0f #define METER_PREV_DECAY_RATE 1.0f #include @@ -17,7 +17,12 @@ namespace MfPA class Meter { public: - Meter(const char* sinkOrSourceName = "", bool isSink = true); + Meter( + const char* sinkOrSourceName = "", + bool isSink = true, + unsigned int framerateLimit = 0, + sf::Color barColor = sf::Color::Green + ); ~Meter(); // callbacks required by pulseaudio @@ -63,6 +68,7 @@ private: CurrentState currentState; bool isMonitoringSink; const char* sinkOrSourceName; + unsigned int framerateLimit; bool gotSinkInfo; bool gotSourceInfo; @@ -82,6 +88,7 @@ private: sf::RenderWindow window; sf::RectangleShape bar; + sf::Color barColor; #ifndef NDEBUG float levelsPrintTimer;