From 5da2308ed4311347aaacf20925042d2b58ae536f Mon Sep 17 00:00:00 2001 From: badaix Date: Fri, 11 Dec 2015 08:35:57 +0100 Subject: [PATCH] switched to popl --- client/Makefile | 2 +- client/snapClient.cpp | 134 ++++++++++++++++++------------------------ externals/popl | 2 +- 3 files changed, 58 insertions(+), 80 deletions(-) diff --git a/client/Makefile b/client/Makefile index b548df13..b9460824 100644 --- a/client/Makefile +++ b/client/Makefile @@ -3,7 +3,7 @@ TARGET = snapclient SHELL = /bin/bash CXX = /usr/bin/g++ -CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DASIO_STANDALONE -DVERSION=\"$(VERSION)\" -I.. -I../externals/asio/asio/include +CFLAGS = -std=c++0x -Wall -Wno-unused-function -O3 -pthread -DASIO_STANDALONE -DVERSION=\"$(VERSION)\" -I.. -I../externals/asio/asio/include -I../externals/popl/include LDFLAGS = -lrt -lasound -logg -lvorbis -lvorbisenc -lFLAC -lavahi-client -lavahi-common OBJ = snapClient.o stream.o clientConnection.o timeProvider.o player/player.o player/alsaPlayer.o decoder/oggDecoder.o decoder/pcmDecoder.o decoder/flacDecoder.o controller.o browseAvahi.o ../message/pcmChunk.o ../common/log.o ../message/sampleFormat.o diff --git a/client/snapClient.cpp b/client/snapClient.cpp index 1a6b693d..fd30d887 100644 --- a/client/snapClient.cpp +++ b/client/snapClient.cpp @@ -18,8 +18,8 @@ #include #include -#include +#include "popl.hpp" #include "controller.h" #include "player/alsaPlayer.h" #include "browseAvahi.h" @@ -29,6 +29,7 @@ using namespace std; +using namespace popl; bool g_terminated = false; @@ -65,87 +66,64 @@ int main (int argc, char **argv) string host(""); size_t port(1704); size_t latency(0); - bool runAsDaemon(false); int processPriority(-3); - while (1) + Switch helpSwitch("", "help", "produce help message"); + Switch versionSwitch("v", "version", "show version number"); + Switch listSwitch("l", "list", "list pcm devices"); + Value hostValue("h", "host", "server hostname or ip address", "", &host); + Value portValue("p", "port", "server port", 1704, &port); + Value soundcardValue("s", "soundcard", "index or name of the soundcard", "default", &soundcard); + Implicit daemonOption("d", "daemon", "daemonize, optional process priority [-20..19]", -3, &processPriority); + Value latencyValue("", "latency", "latency of the soundcard", 0, &latency); + + OptionParser op("Allowed options"); + op.add(helpSwitch) + .add(versionSwitch) + .add(listSwitch) + .add(hostValue) + .add(portValue) + .add(soundcardValue) + .add(daemonOption) + .add(latencyValue); + + try { - int option_index = 0; - static struct option long_options[] = + op.parse(argc, argv); + } + catch (const std::invalid_argument& e) + { + logS(kLogErr) << "Exception: " << e.what() << std::endl; + cout << "\n" << op << "\n"; + exit(EXIT_FAILURE); + } + + if (versionSwitch.isSet()) + { + cout << "snapclient v" << VERSION << "\n" + << "Copyright (C) 2014, 2015 BadAix (snapcast@badaix.de).\n" + << "License GPLv3+: GNU GPL version 3 or later .\n" + << "This is free software: you are free to change and redistribute it.\n" + << "There is NO WARRANTY, to the extent permitted by law.\n\n" + << "Written by Johannes M. Pohl.\n\n"; + exit(EXIT_SUCCESS); + } + + if (listSwitch.isSet()) + { + vector pcmDevices = AlsaPlayer::pcm_list(); + for (auto dev: pcmDevices) { - {"help", no_argument, 0, '?'}, - {"version", no_argument, 0, 'v'}, - {"list", no_argument, 0, 'l'}, - {"host", required_argument, 0, 'h'}, - {"port", required_argument, 0, 'p'}, - {"soundcard", required_argument, 0, 's'}, - {"daemon", optional_argument, 0, 'd'}, - {"latency", required_argument, 0, 0 }, - {0, 0, 0, 0 } - }; - - char c = getopt_long(argc, argv, "?vlh:p:s:d::", long_options, &option_index); - if (c == -1) - break; - - switch (c) - { - case 0: - if (strcmp(long_options[option_index].name, "latency") == 0) - latency = atoi(optarg); - break; - - case 'v': - cout << "snapclient v" << VERSION << "\n" - << "Copyright (C) 2014, 2015 BadAix (snapcast@badaix.de).\n" - << "License GPLv3+: GNU GPL version 3 or later .\n" - << "This is free software: you are free to change and redistribute it.\n" - << "There is NO WARRANTY, to the extent permitted by law.\n\n" - << "Written by Johannes M. Pohl.\n\n"; - exit(EXIT_SUCCESS); - - case 'l': - { - vector pcmDevices = AlsaPlayer::pcm_list(); - for (auto dev: pcmDevices) - { - cout << dev.idx << ": " << dev.name << "\n" - << dev.description << "\n\n"; - } - exit(EXIT_SUCCESS); - } - - case 'h': - host = optarg; - break; - - case 'p': - port = atoi(optarg); - break; - - case 's': - soundcard = optarg; - break; - - case 'd': - runAsDaemon = true; - if (optarg) - processPriority = atoi(optarg); - break; - - default: //? - cout << "Allowed options:\n\n" - << " --help \t\t produce help message\n" - << " -v, --version \t\t show version number\n" - << " -l, --list \t\t list pcm devices\n" - << " -h, --host arg \t\t server hostname or ip address\n" - << " -p, --port arg (=" << port << ")\t server port\n" - << " -s, --soundcard arg(=" << soundcard << ")\t index or name of the soundcard\n" - << " -d, --daemon [arg(=" << processPriority << ")]\t daemonize, optional process priority [-20..19]\n" - << " --latency arg(=" << latency << ")\t\t latency of the soundcard [ms]\n" - << "\n"; - exit(EXIT_FAILURE); + cout << dev.idx << ": " << dev.name << "\n" + << dev.description << "\n\n"; } + exit(EXIT_SUCCESS); + } + + if (helpSwitch.isSet()) + { + cout << op << "\n"; + exit(EXIT_SUCCESS); } @@ -155,7 +133,7 @@ int main (int argc, char **argv) signal(SIGTERM, signal_handler); signal(SIGINT, signal_handler); - if (runAsDaemon) + if (daemonOption.isSet()) { daemonize("/var/run/snapclient.pid"); if (processPriority < -20) diff --git a/externals/popl b/externals/popl index ed0a3323..ba037c01 160000 --- a/externals/popl +++ b/externals/popl @@ -1 +1 @@ -Subproject commit ed0a3323d84673a61ea4758cf29972c47b882e8e +Subproject commit ba037c01fcf1d9b6d05bc726e338f2f2e71c53ac