Skip to content

Commit

Permalink
OptionManager support different opt type and daemonize server
Browse files Browse the repository at this point in the history
  • Loading branch information
Sian Cao committed May 9, 2014
1 parent 963a3f6 commit ed852ed
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 12 deletions.
21 changes: 14 additions & 7 deletions driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ static void setup_drm()

//open default dri device
string card = "/dev/dri/card0";
if (!optManager->get("card").empty()) {
card = optManager->get("card");
if (!optManager->value<string>("card").empty()) {
card = optManager->value<string>("card");
}
std::cerr << "open " << card << endl;
dc.fd = open(card.c_str(), O_RDWR|O_CLOEXEC|O_NONBLOCK);
if (dc.fd <= 0) {
err_quit(strerror(errno));
Expand Down Expand Up @@ -333,8 +334,8 @@ static void on_enter_vt(int sig)
static void setup_vt()
{
string ttyname = "/dev/tty0";
if (!optManager->get("tty").empty())
ttyname = optManager->get("tty");
if (!optManager->value<string>("tty").empty())
ttyname = optManager->value<string>("tty");
dc.vtfd = open(ttyname.c_str(), O_RDWR|O_NOCTTY);
struct vt_mode mode = {0};

Expand Down Expand Up @@ -378,17 +379,23 @@ static void cleanup()

int main(int argc, char* argv[])
{
std::setlocale(LC_ALL, "");
optManager = OptionManager::get(argc, argv);

if (!optManager->value<bool>("nodaemon")) {
daemon(1, 1);
std::cerr << "daemonize " << optManager->progName() << endl;
}

std::setlocale(LC_ALL, "en_US.UTF-8");
setup_drm();
setup_egl();
setup_vt();

if (optManager->get("mode") == "text") {
if (optManager->value<string>("mode") == "text") {
std::cerr << "run in text rendering mode" << endl;
dc.action_mode = new TextMode;
} else {
string theme = optManager->get("theme");
string theme = optManager->value<string>("theme");
auto m = new SceneMode;
if (!theme.empty())
m->setThemeFile(theme);
Expand Down
43 changes: 39 additions & 4 deletions options.cc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ OptionManager::OptionManager()
void OptionManager::usage()
{
cerr << "usage: " << _progName
<< " [-c card] [-m [text|scene] [-t theme]] [-T ttyname] [-h]" << endl;
<< " [-c|--card card] [-m|--mode [text|scene] "
<< "[-t|--theme theme]] [-T|--tty ttyname] [-n|--nodaemon] [-h]"
<< endl;
exit(EXIT_FAILURE);
}

Expand All @@ -30,16 +32,18 @@ void OptionManager::parse(int argc, char *argv[])
{"theme", 1, NULL, 't'},
{"card", 1, NULL, 'c'},
{"tty", 1, NULL, 'T'},
{"nodaemon", 0, NULL, 'n'},
{NULL, 0, NULL, 0},
};

int c, index;
while ((c = getopt_long(argc, argv, "m:t:c:T:h", opts, &index)) != -1) {
while ((c = getopt_long(argc, argv, "m:t:c:T:nh", opts, &index)) != -1) {
switch(c) {
case 'm': _opts["mode"] = {optarg}; break;
case 't': _opts["theme"] = {optarg}; break;
case 'c': _opts["card"] = {optarg}; break;
case 'T': _opts["tty"] = {optarg}; break;
case 'n': _opts["nodaemon"] = "true"; break;
case 'h': usage(); break;
default: break;
}
Expand All @@ -58,8 +62,39 @@ OptionManager* OptionManager::get(int argc, char *argv[])
return _instance;
}

std::string OptionManager::get(std::string opt)
int OptionManager::_value_helper(std::string opt, int)
{
return _opts[opt];
int i = -1;
try {
i = std::stoi(opt);
} catch(...) {
//pass
}
return i;
}

bool OptionManager::_value_helper(std::string opt, bool)
{
bool b = false;
try {
b = std::stoi(opt);
} catch(...) {
b = (opt == "true" || opt == "t");
}
return b;
}

float OptionManager::_value_helper(std::string opt, float)
{
float f = 0.0;
try {
f = std::stof(opt);
} catch(...) {
}
return f;
}

std::string OptionManager::_value_helper(std::string opt, std::string)
{
return opt;
}
15 changes: 14 additions & 1 deletion options.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
class OptionManager {
public:
static OptionManager* get(int argc, char *argv[]);
std::string get(std::string opt);
std::string progName() const { return _progName; }
template<typename T>
T value(std::string opt);

private:
static OptionManager* _instance;
Expand All @@ -18,7 +20,18 @@ class OptionManager {
OptionManager();
void parse(int argc, char *argv[]);
void usage();

int _value_helper(std::string, int);
bool _value_helper(std::string, bool);
float _value_helper(std::string, float);
std::string _value_helper(std::string, std::string);
};

template<typename T>
T OptionManager::value(std::string opt)
{
return _value_helper(_opts[opt], T {});
}


#endif

0 comments on commit ed852ed

Please sign in to comment.