Skip to content

Commit

Permalink
simple mode switching
Browse files Browse the repository at this point in the history
  • Loading branch information
Sian Cao committed Apr 24, 2014
1 parent 5f4ffbc commit d85bbcb
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 2 deletions.
10 changes: 8 additions & 2 deletions driver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using namespace std;
#include "scene.h"
#include "atlas.h"
#include "driver.h"
#include "options.h"

static DisplayContext dc;

Expand Down Expand Up @@ -315,12 +316,17 @@ static void cleanup()
close(dc.fd);
}

int main(int argc, const char* argv[])
int main(int argc, char* argv[])
{
OptionManager* optManager = OptionManager::get(argc, argv);

setup_drm();
setup_egl();

dc.action_mode = new TextMode;
if (optManager->get<string>("mode") == "text")
dc.action_mode = new TextMode;
else
dc.action_mode = new SceneMode;
if (!dc.action_mode->init(dc.mode.hdisplay, dc.mode.vdisplay)) {
return -1;
}
Expand Down
52 changes: 52 additions & 0 deletions options.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#include <unistd.h>
#include <getopt.h>
#include <iostream>

using namespace std;
#include "options.h"

OptionManager* OptionManager::_instance = nullptr;

OptionManager::OptionManager()
{
}

void OptionManager::usage()
{
cerr << "usage: " << _progName << " [-m [text|scene]] [-h]" << endl;
exit(EXIT_FAILURE);
}

void OptionManager::parse(int argc, char *argv[])
{
_progName = {argv[0]};
if (_progName.find("./") == 0) {
_progName.erase(_progName.begin(), _progName.begin()+1);
}

struct option opts[] = {
{"mode", 1, NULL, 0},
{NULL, 0, NULL, 0}
};

int c, index;
while ((c = getopt_long(argc, argv, "m:h", opts, &index)) != -1) {
switch(c) {
case 'm': _mode = {optarg}; break;
case 'h': usage(); break;
default: break;
}
}

}

OptionManager* OptionManager::get(int argc, char *argv[])
{
if (!_instance) {
_instance = new OptionManager;
_instance->parse(argc, argv);

}

return _instance;
}
32 changes: 32 additions & 0 deletions options.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#ifndef _OPTIONS_H
#define _OPTIONS_H

#include <type_traits>

class OptionManager {
public:
static OptionManager* get(int argc, char *argv[]);
template <typename T>
T get(string opt);

private:
static OptionManager* _instance;
string _progName;
string _mode;

OptionManager();
void parse(int argc, char *argv[]);
void usage();
};


template <typename T>
T OptionManager::get(string opt)
{
if (opt == "mode")
return _mode;

return "";
}

#endif

0 comments on commit d85bbcb

Please sign in to comment.