blob: da93fddf4640bafb20abd1a734329602e23de260 [file] [log] [blame]
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +00001/* 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
48namespace rfb {
49 class VoidParameter;
50 struct ParameterIterator;
51
Adam Tkacc58b3d12010-04-23 13:55:10 +000052 enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
53
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000054 // -=- Configuration
55 // Class used to access parameters.
56
57 class Configuration {
58 public:
59 // - Create a new Configuration object
Adam Tkac1be08942013-03-14 15:35:42 +000060 Configuration(const char* name_) : name(strDup(name_)), head(0), _next(0) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000061
62 // - Return the buffer containing the Configuration's name
63 const char* getName() const { return name.buf; }
64
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000065 // - 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 Tkacc58b3d12010-04-23 13:55:10 +000098 // Enable server/viewer specific parameters
99 static void enableServerParams() { global()->appendConfiguration(server()); }
100 static void enableViewerParams() { global()->appendConfiguration(viewer()); }
101
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000102 // - 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 Tkacc58b3d12010-04-23 13:55:10 +0000114 static void listParams(int width=79, int nameWidth=10) {
115 global()->list(width, nameWidth);
116 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000117
Adam Tkacc58b3d12010-04-23 13:55:10 +0000118 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119 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 Tkacc58b3d12010-04-23 13:55:10 +0000133
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 Kaplinskya2adc8d2006-05-25 05:01:55 +0000149 };
150
151 // -=- VoidParameter
152 // Configuration parameter base-class.
153
154 class VoidParameter {
155 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000156 VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000157 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();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000168
169 protected:
170 friend class Configuration;
171 friend struct ParameterIterator;
172
173 VoidParameter* _next;
174 bool immutable;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000175 const char* name;
176 const char* description;
177 };
178
179 class AliasParameter : public VoidParameter {
180 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000181 AliasParameter(const char* name_, const char* desc_,VoidParameter* param_,
182 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000183 virtual bool setParam(const char* value);
184 virtual bool setParam();
185 virtual char* getDefaultStr() const;
186 virtual char* getValueStr() const;
187 virtual bool isBool() const;
188 virtual void setImmutable();
189 private:
190 VoidParameter* param;
191 };
192
193 class BoolParameter : public VoidParameter {
194 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000195 BoolParameter(const char* name_, const char* desc_, bool v,
196 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000197 virtual bool setParam(const char* value);
198 virtual bool setParam();
199 virtual void setParam(bool b);
200 virtual char* getDefaultStr() const;
201 virtual char* getValueStr() const;
202 virtual bool isBool() const;
203 operator bool() const;
204 protected:
205 bool value;
206 bool def_value;
207 };
208
209 class IntParameter : public VoidParameter {
210 public:
211 IntParameter(const char* name_, const char* desc_, int v,
Adam Tkacc58b3d12010-04-23 13:55:10 +0000212 int minValue=INT_MIN, int maxValue=INT_MAX,
213 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000214 virtual bool setParam(const char* value);
215 virtual bool setParam(int v);
216 virtual char* getDefaultStr() const;
217 virtual char* getValueStr() const;
218 operator int() const;
219 protected:
220 int value;
221 int def_value;
222 int minValue, maxValue;
223 };
224
225 class StringParameter : public VoidParameter {
226 public:
227 // StringParameter contains a null-terminated string, which CANNOT
228 // be Null, and so neither can the default value!
Adam Tkacc58b3d12010-04-23 13:55:10 +0000229 StringParameter(const char* name_, const char* desc_, const char* v,
230 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000231 virtual ~StringParameter();
232 virtual bool setParam(const char* value);
233 virtual char* getDefaultStr() const;
234 virtual char* getValueStr() const;
Adam Tkac3c7f8e12010-11-11 14:29:35 +0000235 void setDefaultStr(const char* v);
Pierre Ossman660f1082011-03-02 12:44:12 +0000236 operator const char*() const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000237
238 // getData() returns a copy of the data - it must be delete[]d by the
239 // caller.
240 char* getData() const { return getValueStr(); }
241 protected:
242 char* value;
243 const char* def_value;
244 };
245
246 class BinaryParameter : public VoidParameter {
247 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000248 BinaryParameter(const char* name_, const char* desc_, const void* v, int l,
249 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000250 virtual ~BinaryParameter();
251 virtual bool setParam(const char* value);
252 virtual void setParam(const void* v, int l);
253 virtual char* getDefaultStr() const;
254 virtual char* getValueStr() const;
255
256 // getData() will return length zero if there is no data
257 // NB: data may be set to zero, OR set to a zero-length buffer
258 void getData(void** data, int* length) const;
259
260 protected:
261 char* value;
262 int length;
263 char* def_value;
264 int def_length;
265 };
266
267 // -=- ParameterIterator
Adam Tkac40060b42013-03-14 17:11:22 +0000268 // Iterates over all enabled parameters (global + server/viewer).
269 // Current Parameter is accessed via param, the current Configuration
270 // via config. The next() method moves on to the next Parameter.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000271
272 struct ParameterIterator {
Adam Tkac40060b42013-03-14 17:11:22 +0000273 ParameterIterator() : config(Configuration::global()), param(config->head) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000274 void next() {
275 param = param->_next;
276 while (!param) {
277 config = config->_next;
278 if (!config) break;
279 param = config->head;
280 }
281 }
282 Configuration* config;
283 VoidParameter* param;
284 };
285
286};
287
288#endif // __RFB_CONFIGURATION_H__