mirror of
https://github.com/badaix/snapcast.git
synced 2025-07-14 23:27:40 +02:00
replaced boost program options with getopt
This commit is contained in:
parent
26af00a8c3
commit
884a65f289
6 changed files with 208 additions and 120 deletions
|
@ -18,18 +18,17 @@
|
|||
|
||||
#include <iostream>
|
||||
#include <sys/resource.h>
|
||||
#include <boost/program_options.hpp>
|
||||
#include <getopt.h>
|
||||
|
||||
#include "common/daemon.h"
|
||||
#include "common/log.h"
|
||||
#include "common/signalHandler.h"
|
||||
#include "controller.h"
|
||||
#include "player/alsaPlayer.h"
|
||||
#include "browseAvahi.h"
|
||||
#include "common/daemon.h"
|
||||
#include "common/log.h"
|
||||
#include "common/signalHandler.h"
|
||||
|
||||
|
||||
using namespace std;
|
||||
namespace po = boost::program_options;
|
||||
|
||||
bool g_terminated = false;
|
||||
|
||||
|
@ -58,76 +57,113 @@ PcmDevice getPcmDevice(const std::string& soundcard)
|
|||
}
|
||||
|
||||
|
||||
int main (int argc, char *argv[])
|
||||
int main (int argc, char **argv)
|
||||
{
|
||||
try
|
||||
{
|
||||
string soundcard;
|
||||
string host;
|
||||
// int bufferMs;
|
||||
size_t port;
|
||||
size_t latency;
|
||||
int runAsDaemon;
|
||||
bool listPcmDevices;
|
||||
string soundcard("default");
|
||||
string host("");
|
||||
size_t port(1704);
|
||||
size_t latency(0);
|
||||
bool runAsDaemon(false);
|
||||
int processPriority(-3);
|
||||
|
||||
po::options_description desc("Allowed options");
|
||||
desc.add_options()
|
||||
("help", "produce help message")
|
||||
("version,v", "show version number")
|
||||
("list,l", po::bool_switch(&listPcmDevices)->default_value(false), "list pcm devices")
|
||||
("host,h", po::value<string>(&host), "server hostname or ip address")
|
||||
("port,p", po::value<size_t>(&port)->default_value(1704), "server port")
|
||||
("soundcard,s", po::value<string>(&soundcard)->default_value("default"), "index or name of the soundcard")
|
||||
("daemon,d", po::value<int>(&runAsDaemon)->implicit_value(-3), "daemonize, optional process priority [-20..19]")
|
||||
("latency", po::value<size_t>(&latency)->default_value(0), "latency of the soundcard")
|
||||
;
|
||||
while (1)
|
||||
{
|
||||
int option_index = 0;
|
||||
static struct option long_options[] =
|
||||
{
|
||||
{"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 <http://gnu.org/licenses/gpl.html>.\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<PcmDevice> 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);
|
||||
}
|
||||
}
|
||||
|
||||
po::variables_map vm;
|
||||
po::store(po::parse_command_line(argc, argv, desc), vm);
|
||||
po::notify(vm);
|
||||
|
||||
std::clog.rdbuf(new Log("snapclient", LOG_DAEMON));
|
||||
|
||||
if (vm.count("help"))
|
||||
{
|
||||
cout << desc << "\n";
|
||||
return 1;
|
||||
}
|
||||
signal(SIGHUP, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
if (vm.count("version"))
|
||||
{
|
||||
cout << "snapclient v" << VERSION << "\n"
|
||||
<< "Copyright (C) 2014 BadAix (snapcast@badaix.de).\n"
|
||||
<< "License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.\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";
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (listPcmDevices)
|
||||
{
|
||||
vector<PcmDevice> pcmDevices = AlsaPlayer::pcm_list();
|
||||
for (auto dev: pcmDevices)
|
||||
{
|
||||
cout << dev.idx << ": " << dev.name << "\n"
|
||||
<< dev.description << "\n\n";
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
signal(SIGHUP, signal_handler);
|
||||
signal(SIGTERM, signal_handler);
|
||||
signal(SIGINT, signal_handler);
|
||||
|
||||
if (vm.count("daemon"))
|
||||
if (runAsDaemon)
|
||||
{
|
||||
daemonize("/var/run/snapclient.pid");
|
||||
if (runAsDaemon < -20)
|
||||
runAsDaemon = -20;
|
||||
else if (runAsDaemon > 19)
|
||||
runAsDaemon = 19;
|
||||
setpriority(PRIO_PROCESS, 0, runAsDaemon);
|
||||
if (processPriority < -20)
|
||||
processPriority = -20;
|
||||
else if (processPriority > 19)
|
||||
processPriority = 19;
|
||||
if (processPriority != 0)
|
||||
setpriority(PRIO_PROCESS, 0, processPriority);
|
||||
logS(kLogNotice) << "daemon started" << std::endl;
|
||||
}
|
||||
|
||||
|
@ -135,10 +171,10 @@ int main (int argc, char *argv[])
|
|||
if (pcmDevice.idx == -1)
|
||||
{
|
||||
cout << "soundcard \"" << soundcard << "\" not found\n";
|
||||
return 1;
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
|
||||
if (!vm.count("host"))
|
||||
if (!host.empty())
|
||||
{
|
||||
BrowseAvahi browseAvahi;
|
||||
AvahiResult avahiResult;
|
||||
|
@ -165,6 +201,7 @@ int main (int argc, char *argv[])
|
|||
std::unique_ptr<Controller> controller(new Controller());
|
||||
if (!g_terminated)
|
||||
{
|
||||
logO << "Latency: " << latency << "\n";
|
||||
controller->start(pcmDevice, host, port, latency);
|
||||
while(!g_terminated)
|
||||
usleep(100*1000);
|
||||
|
@ -178,7 +215,7 @@ int main (int argc, char *argv[])
|
|||
|
||||
logS(kLogNotice) << "daemon terminated." << endl;
|
||||
daemonShutdown();
|
||||
return 0;
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue