Configuration class. Change needed viewer/server code appropriately. git-svn-id: svn://svn.code.sf.net/p/tigervnc/code/trunk@4032 3789f03b-4d11-0410-bbf8-ca57d06f2519tags/v1.0.90
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <ctype.h> | #include <ctype.h> | ||||
#include <string.h> | #include <string.h> | ||||
#include <assert.h> | |||||
#ifdef WIN32 | #ifdef WIN32 | ||||
#define strcasecmp _stricmp | #define strcasecmp _stricmp | ||||
#define strncasecmp _strnicmp | #define strncasecmp _strnicmp | ||||
static LogWriter vlog("Config"); | static LogWriter vlog("Config"); | ||||
// -=- The Global Configuration object | |||||
// -=- The Global/server/viewer Configuration objects | |||||
Configuration* Configuration::global_ = 0; | Configuration* Configuration::global_ = 0; | ||||
Configuration* Configuration::server_ = 0; | |||||
Configuration* Configuration::viewer_ = 0; | |||||
Configuration* Configuration::global() { | Configuration* Configuration::global() { | ||||
if (!global_) | if (!global_) | ||||
global_ = new Configuration("Global"); | global_ = new Configuration("Global"); | ||||
return global_; | return global_; | ||||
} | } | ||||
Configuration* Configuration::server() { | |||||
if (!server_) | |||||
server_ = new Configuration("Server"); | |||||
return server_; | |||||
} | |||||
Configuration* Configuration::viewer() { | |||||
if (!viewer_) | |||||
viewer_ = new Configuration("Viewer"); | |||||
return viewer_; | |||||
} | |||||
// -=- Configuration implementation | // -=- Configuration implementation | ||||
// -=- VoidParameter | // -=- VoidParameter | ||||
VoidParameter::VoidParameter(const char* name_, const char* desc_, Configuration* conf) | |||||
: immutable(false), _hasBeenSet(false), name(name_), description(desc_) { | |||||
if (!conf) | |||||
conf = Configuration::global(); | |||||
VoidParameter::VoidParameter(const char* name_, const char* desc_, | |||||
ConfigurationObject co) | |||||
: immutable(false), _hasBeenSet(false), name(name_), description(desc_) | |||||
{ | |||||
Configuration *conf = NULL; | |||||
switch (co) { | |||||
case ConfGlobal: conf = Configuration::global(); | |||||
break; | |||||
case ConfServer: conf = Configuration::server(); | |||||
break; | |||||
case ConfViewer: conf = Configuration::viewer(); | |||||
break; | |||||
} | |||||
_next = conf->head; | _next = conf->head; | ||||
conf->head = this; | conf->head = this; | ||||
} | } | ||||
// -=- AliasParameter | // -=- AliasParameter | ||||
AliasParameter::AliasParameter(const char* name_, const char* desc_, | AliasParameter::AliasParameter(const char* name_, const char* desc_, | ||||
VoidParameter* param_, Configuration* conf) | |||||
: VoidParameter(name_, desc_, conf), param(param_) { | |||||
VoidParameter* param_, ConfigurationObject co) | |||||
: VoidParameter(name_, desc_, co), param(param_) { | |||||
} | } | ||||
bool | bool | ||||
// -=- BoolParameter | // -=- BoolParameter | ||||
BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v, Configuration* conf) | |||||
: VoidParameter(name_, desc_, conf), value(v), def_value(v) { | |||||
BoolParameter::BoolParameter(const char* name_, const char* desc_, bool v, | |||||
ConfigurationObject co) | |||||
: VoidParameter(name_, desc_, co), value(v), def_value(v) { | |||||
} | } | ||||
bool | bool | ||||
// -=- IntParameter | // -=- IntParameter | ||||
IntParameter::IntParameter(const char* name_, const char* desc_, int v, | IntParameter::IntParameter(const char* name_, const char* desc_, int v, | ||||
int minValue_, int maxValue_, Configuration* conf) | |||||
: VoidParameter(name_, desc_, conf), value(v), def_value(v), | |||||
int minValue_, int maxValue_, ConfigurationObject co) | |||||
: VoidParameter(name_, desc_, co), value(v), def_value(v), | |||||
minValue(minValue_), maxValue(maxValue_) | minValue(minValue_), maxValue(maxValue_) | ||||
{ | { | ||||
} | } | ||||
// -=- StringParameter | // -=- StringParameter | ||||
StringParameter::StringParameter(const char* name_, const char* desc_, | StringParameter::StringParameter(const char* name_, const char* desc_, | ||||
const char* v, Configuration* conf) | |||||
: VoidParameter(name_, desc_, conf), value(strDup(v)), def_value(v) | |||||
const char* v, ConfigurationObject co) | |||||
: VoidParameter(name_, desc_, co), value(strDup(v)), def_value(v) | |||||
{ | { | ||||
if (!v) { | if (!v) { | ||||
fprintf(stderr,"Default value <null> for %s not allowed\n",name_); | fprintf(stderr,"Default value <null> for %s not allowed\n",name_); | ||||
// -=- BinaryParameter | // -=- BinaryParameter | ||||
BinaryParameter::BinaryParameter(const char* name_, const char* desc_, const void* v, int l, Configuration* conf) | |||||
: VoidParameter(name_, desc_, conf), value(0), length(0), def_value((char*)v), def_length(l) { | |||||
BinaryParameter::BinaryParameter(const char* name_, const char* desc_, | |||||
const void* v, int l, ConfigurationObject co) | |||||
: VoidParameter(name_, desc_, co), value(0), length(0), def_value((char*)v), def_length(l) { | |||||
if (l) { | if (l) { | ||||
value = new char[l]; | value = new char[l]; | ||||
length = l; | length = l; |
class VoidParameter; | class VoidParameter; | ||||
struct ParameterIterator; | struct ParameterIterator; | ||||
enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer }; | |||||
// -=- Configuration | // -=- Configuration | ||||
// Class used to access parameters. | // Class used to access parameters. | ||||
// global() is called when only the main thread is running. | // global() is called when only the main thread is running. | ||||
static Configuration* global(); | static Configuration* global(); | ||||
// Enable server/viewer specific parameters | |||||
static void enableServerParams() { global()->appendConfiguration(server()); } | |||||
static void enableViewerParams() { global()->appendConfiguration(viewer()); } | |||||
// - Container for process-wide Global parameters | // - Container for process-wide Global parameters | ||||
static bool setParam(const char* param, const char* value, bool immutable=false) { | static bool setParam(const char* param, const char* value, bool immutable=false) { | ||||
return global()->set(param, value, immutable); | return global()->set(param, value, immutable); | ||||
return global()->set(name, len, val, immutable); | return global()->set(name, len, val, immutable); | ||||
} | } | ||||
static VoidParameter* getParam(const char* param) { return global()->get(param); } | static VoidParameter* getParam(const char* param) { return global()->get(param); } | ||||
static void listParams(int width=79, int nameWidth=10) { global()->list(width, nameWidth); } | |||||
static void listParams(int width=79, int nameWidth=10) { | |||||
global()->list(width, nameWidth); | |||||
} | |||||
protected: | |||||
private: | |||||
friend class VoidParameter; | friend class VoidParameter; | ||||
friend struct ParameterIterator; | friend struct ParameterIterator; | ||||
// The process-wide, Global Configuration object | // The process-wide, Global Configuration object | ||||
static Configuration* global_; | static Configuration* global_; | ||||
// The server only Configuration object | |||||
static Configuration* server_; | |||||
// The viewer only Configuration object | |||||
static Configuration* viewer_; | |||||
// Get server/viewer specific configuration object | |||||
static Configuration* server(); | |||||
static Configuration* viewer(); | |||||
// Append configuration object to this instance. | |||||
// NOTE: conf instance can be only one configuration object | |||||
void appendConfiguration(Configuration *conf) { | |||||
conf->_next = _next; _next = conf; | |||||
} | |||||
}; | }; | ||||
// -=- VoidParameter | // -=- VoidParameter | ||||
class VoidParameter { | class VoidParameter { | ||||
public: | public: | ||||
VoidParameter(const char* name_, const char* desc_, Configuration* conf=0); | |||||
VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal); | |||||
virtual ~VoidParameter(); | virtual ~VoidParameter(); | ||||
const char* getName() const; | const char* getName() const; | ||||
const char* getDescription() const; | const char* getDescription() const; | ||||
class AliasParameter : public VoidParameter { | class AliasParameter : public VoidParameter { | ||||
public: | public: | ||||
AliasParameter(const char* name_, const char* desc_,VoidParameter* param_, Configuration* conf=0); | |||||
AliasParameter(const char* name_, const char* desc_,VoidParameter* param_, | |||||
ConfigurationObject co=ConfGlobal); | |||||
virtual bool setParam(const char* value); | virtual bool setParam(const char* value); | ||||
virtual bool setParam(); | virtual bool setParam(); | ||||
virtual char* getDefaultStr() const; | virtual char* getDefaultStr() const; | ||||
class BoolParameter : public VoidParameter { | class BoolParameter : public VoidParameter { | ||||
public: | public: | ||||
BoolParameter(const char* name_, const char* desc_, bool v, Configuration* conf=0); | |||||
BoolParameter(const char* name_, const char* desc_, bool v, | |||||
ConfigurationObject co=ConfGlobal); | |||||
virtual bool setParam(const char* value); | virtual bool setParam(const char* value); | ||||
virtual bool setParam(); | virtual bool setParam(); | ||||
virtual void setParam(bool b); | virtual void setParam(bool b); | ||||
class IntParameter : public VoidParameter { | class IntParameter : public VoidParameter { | ||||
public: | public: | ||||
IntParameter(const char* name_, const char* desc_, int v, | IntParameter(const char* name_, const char* desc_, int v, | ||||
int minValue=INT_MIN, int maxValue=INT_MAX, Configuration* conf=0); | |||||
int minValue=INT_MIN, int maxValue=INT_MAX, | |||||
ConfigurationObject co=ConfGlobal); | |||||
virtual bool setParam(const char* value); | virtual bool setParam(const char* value); | ||||
virtual bool setParam(int v); | virtual bool setParam(int v); | ||||
virtual char* getDefaultStr() const; | virtual char* getDefaultStr() const; | ||||
public: | public: | ||||
// StringParameter contains a null-terminated string, which CANNOT | // StringParameter contains a null-terminated string, which CANNOT | ||||
// be Null, and so neither can the default value! | // be Null, and so neither can the default value! | ||||
StringParameter(const char* name_, const char* desc_, const char* v, Configuration* conf=0); | |||||
StringParameter(const char* name_, const char* desc_, const char* v, | |||||
ConfigurationObject co=ConfGlobal); | |||||
virtual ~StringParameter(); | virtual ~StringParameter(); | ||||
virtual bool setParam(const char* value); | virtual bool setParam(const char* value); | ||||
virtual char* getDefaultStr() const; | virtual char* getDefaultStr() const; | ||||
class BinaryParameter : public VoidParameter { | class BinaryParameter : public VoidParameter { | ||||
public: | public: | ||||
BinaryParameter(const char* name_, const char* desc_, const void* v, int l, Configuration* conf=0); | |||||
BinaryParameter(const char* name_, const char* desc_, const void* v, int l, | |||||
ConfigurationObject co=ConfGlobal); | |||||
virtual ~BinaryParameter(); | virtual ~BinaryParameter(); | ||||
virtual bool setParam(const char* value); | virtual bool setParam(const char* value); | ||||
virtual void setParam(const void* v, int l); | virtual void setParam(const void* v, int l); |
char* vncServerName = 0; | char* vncServerName = 0; | ||||
Display* dpy = 0; | Display* dpy = 0; | ||||
Configuration::enableViewerParams(); | |||||
for (int i = 1; i < argc; i++) { | for (int i = 1; i < argc; i++) { | ||||
if (Configuration::setParam(argv[i])) | if (Configuration::setParam(argv[i])) | ||||
continue; | continue; |
programName = argv[0]; | programName = argv[0]; | ||||
Display* dpy; | Display* dpy; | ||||
Configuration::enableServerParams(); | |||||
for (int i = 1; i < argc; i++) { | for (int i = 1; i < argc; i++) { | ||||
if (Configuration::setParam(argv[i])) | if (Configuration::setParam(argv[i])) | ||||
continue; | continue; |
{ | { | ||||
rfb::initStdIOLoggers(); | rfb::initStdIOLoggers(); | ||||
rfb::LogWriter::setLogParams("*:stderr:30"); | rfb::LogWriter::setLogParams("*:stderr:30"); | ||||
rfb::Configuration::enableServerParams(); | |||||
for (int scr = 0; scr < screenInfo.numScreens; scr++) { | for (int scr = 0; scr < screenInfo.numScreens; scr++) { | ||||
ScrnInfoPtr pScrn = xf86Screens[scr]; | ScrnInfoPtr pScrn = xf86Screens[scr]; |
firstTime = FALSE; | firstTime = FALSE; | ||||
rfb::initStdIOLoggers(); | rfb::initStdIOLoggers(); | ||||
rfb::LogWriter::setLogParams("*:stderr:30"); | rfb::LogWriter::setLogParams("*:stderr:30"); | ||||
rfb::Configuration::enableServerParams(); | |||||
} | } | ||||
if (argv[i][0] == ':') | if (argv[i][0] == ':') |
printf("\nLog destinations:\n"); | printf("\nLog destinations:\n"); | ||||
Logger::listLoggers(); | Logger::listLoggers(); | ||||
printf("\nParameters:\n"); | printf("\nParameters:\n"); | ||||
Configuration::listParams(); | |||||
Configuration::listParams(ConfViewer); | |||||
printf("Press Enter/Return key to continue\n"); | printf("Press Enter/Return key to continue\n"); | ||||
getchar(); | getchar(); | ||||
exit(1); | exit(1); |
printf("\nLog destinations:\n"); | printf("\nLog destinations:\n"); | ||||
Logger::listLoggers(); | Logger::listLoggers(); | ||||
printf("\nAvailable configuration parameters:\n"); | printf("\nAvailable configuration parameters:\n"); | ||||
Configuration::listParams(); | |||||
Configuration::listParams(ConfServer); | |||||
} | } | ||||
static void MsgBoxOrLog(const char* msg, bool isError=false) { | static void MsgBoxOrLog(const char* msg, bool isError=false) { |