Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 1 | /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved. |
| 2 | * |
| 3 | * This is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License as published by |
| 5 | * the Free Software Foundation; either version 2 of the License, or |
| 6 | * (at your option) any later version. |
| 7 | * |
| 8 | * This software is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * along with this software; if not, write to the Free Software |
| 15 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 16 | * USA. |
| 17 | */ |
| 18 | |
| 19 | // -=- Configuration.h |
| 20 | // |
| 21 | // This header defines a set of classes used to represent configuration |
| 22 | // parameters of different types. Instances of the different parameter |
| 23 | // types are associated with instances of the Configuration class, and |
| 24 | // are each given a unique name. The Configuration class provides a |
| 25 | // generic API through which parameters may be located by name and their |
| 26 | // value set, thus removing the need to write platform-specific code. |
| 27 | // Simply defining a new parameter and associating it with a Configuration |
| 28 | // will allow it to be configured by the user. |
| 29 | // |
| 30 | // If no Configuration is specified when creating a Parameter, then the |
| 31 | // global Configuration will be assumed. |
| 32 | // |
| 33 | // Configurations can be "chained" into groups. Each group has a root |
| 34 | // Configuration, a pointer to which should be passed to the constructors |
| 35 | // of the other group members. set() and get() operations called on the |
| 36 | // root will iterate through all of the group's members. |
| 37 | // |
| 38 | // NB: On platforms that support Threading, locking is performed to protect |
| 39 | // complex parameter types from concurrent access (e.g. strings). |
| 40 | // NB: NO LOCKING is performed when linking Configurations to groups |
| 41 | // or when adding Parameters to Configurations. |
| 42 | |
| 43 | #ifndef __RFB_CONFIGURATION_H__ |
| 44 | #define __RFB_CONFIGURATION_H__ |
| 45 | |
| 46 | #include <rfb/util.h> |
| 47 | |
| 48 | namespace rfb { |
| 49 | class VoidParameter; |
| 50 | struct ParameterIterator; |
| 51 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 52 | enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer }; |
| 53 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 54 | // -=- Configuration |
| 55 | // Class used to access parameters. |
| 56 | |
| 57 | class Configuration { |
| 58 | public: |
| 59 | // - Create a new Configuration object |
Adam Tkac | 1be0894 | 2013-03-14 15:35:42 +0000 | [diff] [blame] | 60 | Configuration(const char* name_) : name(strDup(name_)), head(0), _next(0) {} |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 61 | |
| 62 | // - Return the buffer containing the Configuration's name |
| 63 | const char* getName() const { return name.buf; } |
| 64 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 65 | // - Set named parameter to value |
| 66 | bool set(const char* param, const char* value, bool immutable=false); |
| 67 | |
| 68 | // - Set parameter to value (separated by "=") |
| 69 | bool set(const char* config, bool immutable=false); |
| 70 | |
| 71 | // - Set named parameter to value, with name truncated at len |
| 72 | bool set(const char* name, int len, |
| 73 | const char* val, bool immutable); |
| 74 | |
| 75 | // - Get named parameter |
| 76 | VoidParameter* get(const char* param); |
| 77 | |
| 78 | // - List the parameters of this Configuration group |
| 79 | void list(int width=79, int nameWidth=10); |
| 80 | |
| 81 | // - readFromFile |
| 82 | // Read configuration parameters from the specified file. |
| 83 | void readFromFile(const char* filename); |
| 84 | |
| 85 | // - writeConfigToFile |
| 86 | // Write a new configuration parameters file, then mv it |
| 87 | // over the old file. |
| 88 | void writeToFile(const char* filename); |
| 89 | |
| 90 | |
| 91 | // - Get the Global Configuration object |
| 92 | // NB: This call does NOT lock the Configuration system. |
| 93 | // ALWAYS ensure that if you have ANY global Parameters, |
| 94 | // then they are defined as global objects, to ensure that |
| 95 | // global() is called when only the main thread is running. |
| 96 | static Configuration* global(); |
| 97 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 98 | // Enable server/viewer specific parameters |
| 99 | static void enableServerParams() { global()->appendConfiguration(server()); } |
| 100 | static void enableViewerParams() { global()->appendConfiguration(viewer()); } |
| 101 | |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 102 | // - Container for process-wide Global parameters |
| 103 | static bool setParam(const char* param, const char* value, bool immutable=false) { |
| 104 | return global()->set(param, value, immutable); |
| 105 | } |
| 106 | static bool setParam(const char* config, bool immutable=false) { |
| 107 | return global()->set(config, immutable); |
| 108 | } |
| 109 | static bool setParam(const char* name, int len, |
| 110 | const char* val, bool immutable) { |
| 111 | return global()->set(name, len, val, immutable); |
| 112 | } |
| 113 | static VoidParameter* getParam(const char* param) { return global()->get(param); } |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 114 | static void listParams(int width=79, int nameWidth=10) { |
| 115 | global()->list(width, nameWidth); |
| 116 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 117 | |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 118 | private: |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 119 | friend class VoidParameter; |
| 120 | friend struct ParameterIterator; |
| 121 | |
| 122 | // Name for this Configuration |
| 123 | CharArray name; |
| 124 | |
| 125 | // - Pointer to first Parameter in this group |
| 126 | VoidParameter* head; |
| 127 | |
| 128 | // Pointer to next Configuration in this group |
| 129 | Configuration* _next; |
| 130 | |
| 131 | // The process-wide, Global Configuration object |
| 132 | static Configuration* global_; |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 133 | |
| 134 | // The server only Configuration object |
| 135 | static Configuration* server_; |
| 136 | |
| 137 | // The viewer only Configuration object |
| 138 | static Configuration* viewer_; |
| 139 | |
| 140 | // Get server/viewer specific configuration object |
| 141 | static Configuration* server(); |
| 142 | static Configuration* viewer(); |
| 143 | |
| 144 | // Append configuration object to this instance. |
| 145 | // NOTE: conf instance can be only one configuration object |
| 146 | void appendConfiguration(Configuration *conf) { |
| 147 | conf->_next = _next; _next = conf; |
| 148 | } |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // -=- VoidParameter |
| 152 | // Configuration parameter base-class. |
| 153 | |
| 154 | class VoidParameter { |
| 155 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 156 | VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 157 | virtual ~VoidParameter(); |
| 158 | const char* getName() const; |
| 159 | const char* getDescription() const; |
| 160 | |
| 161 | virtual bool setParam(const char* value) = 0; |
| 162 | virtual bool setParam(); |
| 163 | virtual char* getDefaultStr() const = 0; |
| 164 | virtual char* getValueStr() const = 0; |
| 165 | virtual bool isBool() const; |
| 166 | |
| 167 | virtual void setImmutable(); |
| 168 | virtual void setHasBeenSet(); |
| 169 | bool hasBeenSet(); |
| 170 | |
| 171 | protected: |
| 172 | friend class Configuration; |
| 173 | friend struct ParameterIterator; |
| 174 | |
| 175 | VoidParameter* _next; |
| 176 | bool immutable; |
| 177 | bool _hasBeenSet; |
| 178 | const char* name; |
| 179 | const char* description; |
| 180 | }; |
| 181 | |
| 182 | class AliasParameter : public VoidParameter { |
| 183 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 184 | AliasParameter(const char* name_, const char* desc_,VoidParameter* param_, |
| 185 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 186 | virtual bool setParam(const char* value); |
| 187 | virtual bool setParam(); |
| 188 | virtual char* getDefaultStr() const; |
| 189 | virtual char* getValueStr() const; |
| 190 | virtual bool isBool() const; |
| 191 | virtual void setImmutable(); |
| 192 | private: |
| 193 | VoidParameter* param; |
| 194 | }; |
| 195 | |
| 196 | class BoolParameter : public VoidParameter { |
| 197 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 198 | BoolParameter(const char* name_, const char* desc_, bool v, |
| 199 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 200 | virtual bool setParam(const char* value); |
| 201 | virtual bool setParam(); |
| 202 | virtual void setParam(bool b); |
| 203 | virtual char* getDefaultStr() const; |
| 204 | virtual char* getValueStr() const; |
| 205 | virtual bool isBool() const; |
| 206 | operator bool() const; |
| 207 | protected: |
| 208 | bool value; |
| 209 | bool def_value; |
| 210 | }; |
| 211 | |
| 212 | class IntParameter : public VoidParameter { |
| 213 | public: |
| 214 | IntParameter(const char* name_, const char* desc_, int v, |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 215 | int minValue=INT_MIN, int maxValue=INT_MAX, |
| 216 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 217 | virtual bool setParam(const char* value); |
| 218 | virtual bool setParam(int v); |
| 219 | virtual char* getDefaultStr() const; |
| 220 | virtual char* getValueStr() const; |
| 221 | operator int() const; |
| 222 | protected: |
| 223 | int value; |
| 224 | int def_value; |
| 225 | int minValue, maxValue; |
| 226 | }; |
| 227 | |
| 228 | class StringParameter : public VoidParameter { |
| 229 | public: |
| 230 | // StringParameter contains a null-terminated string, which CANNOT |
| 231 | // be Null, and so neither can the default value! |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 232 | StringParameter(const char* name_, const char* desc_, const char* v, |
| 233 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 234 | virtual ~StringParameter(); |
| 235 | virtual bool setParam(const char* value); |
| 236 | virtual char* getDefaultStr() const; |
| 237 | virtual char* getValueStr() const; |
Adam Tkac | 3c7f8e1 | 2010-11-11 14:29:35 +0000 | [diff] [blame] | 238 | void setDefaultStr(const char* v); |
Pierre Ossman | 660f108 | 2011-03-02 12:44:12 +0000 | [diff] [blame] | 239 | operator const char*() const; |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 240 | |
| 241 | // getData() returns a copy of the data - it must be delete[]d by the |
| 242 | // caller. |
| 243 | char* getData() const { return getValueStr(); } |
| 244 | protected: |
| 245 | char* value; |
| 246 | const char* def_value; |
| 247 | }; |
| 248 | |
| 249 | class BinaryParameter : public VoidParameter { |
| 250 | public: |
Adam Tkac | c58b3d1 | 2010-04-23 13:55:10 +0000 | [diff] [blame] | 251 | BinaryParameter(const char* name_, const char* desc_, const void* v, int l, |
| 252 | ConfigurationObject co=ConfGlobal); |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 253 | virtual ~BinaryParameter(); |
| 254 | virtual bool setParam(const char* value); |
| 255 | virtual void setParam(const void* v, int l); |
| 256 | virtual char* getDefaultStr() const; |
| 257 | virtual char* getValueStr() const; |
| 258 | |
| 259 | // getData() will return length zero if there is no data |
| 260 | // NB: data may be set to zero, OR set to a zero-length buffer |
| 261 | void getData(void** data, int* length) const; |
| 262 | |
| 263 | protected: |
| 264 | char* value; |
| 265 | int length; |
| 266 | char* def_value; |
| 267 | int def_length; |
| 268 | }; |
| 269 | |
| 270 | // -=- ParameterIterator |
Adam Tkac | 40060b4 | 2013-03-14 17:11:22 +0000 | [diff] [blame^] | 271 | // Iterates over all enabled parameters (global + server/viewer). |
| 272 | // Current Parameter is accessed via param, the current Configuration |
| 273 | // via config. The next() method moves on to the next Parameter. |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 274 | |
| 275 | struct ParameterIterator { |
Adam Tkac | 40060b4 | 2013-03-14 17:11:22 +0000 | [diff] [blame^] | 276 | ParameterIterator() : config(Configuration::global()), param(config->head) {} |
Constantin Kaplinsky | a2adc8d | 2006-05-25 05:01:55 +0000 | [diff] [blame] | 277 | void next() { |
| 278 | param = param->_next; |
| 279 | while (!param) { |
| 280 | config = config->_next; |
| 281 | if (!config) break; |
| 282 | param = config->head; |
| 283 | } |
| 284 | } |
| 285 | Configuration* config; |
| 286 | VoidParameter* param; |
| 287 | }; |
| 288 | |
| 289 | }; |
| 290 | |
| 291 | #endif // __RFB_CONFIGURATION_H__ |