blob: d319915af38d1ebe2bf40e3daa444e7864415213 [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
Pierre Ossman338e73a2016-07-07 15:35:13 +020048namespace os { class Mutex; }
49
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000050namespace rfb {
51 class VoidParameter;
52 struct ParameterIterator;
53
Adam Tkacc58b3d12010-04-23 13:55:10 +000054 enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
55
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000056 // -=- Configuration
57 // Class used to access parameters.
58
59 class Configuration {
60 public:
61 // - Create a new Configuration object
Adam Tkac1be08942013-03-14 15:35:42 +000062 Configuration(const char* name_) : name(strDup(name_)), head(0), _next(0) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000063
64 // - Return the buffer containing the Configuration's name
65 const char* getName() const { return name.buf; }
66
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +000067 // - Set named parameter to value
68 bool set(const char* param, const char* value, bool immutable=false);
69
70 // - Set parameter to value (separated by "=")
71 bool set(const char* config, bool immutable=false);
72
73 // - Set named parameter to value, with name truncated at len
74 bool set(const char* name, int len,
75 const char* val, bool immutable);
76
77 // - Get named parameter
78 VoidParameter* get(const char* param);
79
80 // - List the parameters of this Configuration group
81 void list(int width=79, int nameWidth=10);
82
83 // - readFromFile
84 // Read configuration parameters from the specified file.
85 void readFromFile(const char* filename);
86
87 // - writeConfigToFile
88 // Write a new configuration parameters file, then mv it
89 // over the old file.
90 void writeToFile(const char* filename);
91
92
93 // - Get the Global Configuration object
94 // NB: This call does NOT lock the Configuration system.
95 // ALWAYS ensure that if you have ANY global Parameters,
96 // then they are defined as global objects, to ensure that
97 // global() is called when only the main thread is running.
98 static Configuration* global();
99
Adam Tkacc58b3d12010-04-23 13:55:10 +0000100 // Enable server/viewer specific parameters
101 static void enableServerParams() { global()->appendConfiguration(server()); }
102 static void enableViewerParams() { global()->appendConfiguration(viewer()); }
103
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000104 // - Container for process-wide Global parameters
105 static bool setParam(const char* param, const char* value, bool immutable=false) {
106 return global()->set(param, value, immutable);
107 }
108 static bool setParam(const char* config, bool immutable=false) {
109 return global()->set(config, immutable);
110 }
111 static bool setParam(const char* name, int len,
112 const char* val, bool immutable) {
113 return global()->set(name, len, val, immutable);
114 }
115 static VoidParameter* getParam(const char* param) { return global()->get(param); }
Adam Tkacc58b3d12010-04-23 13:55:10 +0000116 static void listParams(int width=79, int nameWidth=10) {
117 global()->list(width, nameWidth);
118 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000119
Adam Tkacc58b3d12010-04-23 13:55:10 +0000120 private:
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000121 friend class VoidParameter;
122 friend struct ParameterIterator;
123
124 // Name for this Configuration
125 CharArray name;
126
127 // - Pointer to first Parameter in this group
128 VoidParameter* head;
129
130 // Pointer to next Configuration in this group
131 Configuration* _next;
132
133 // The process-wide, Global Configuration object
134 static Configuration* global_;
Adam Tkacc58b3d12010-04-23 13:55:10 +0000135
136 // The server only Configuration object
137 static Configuration* server_;
138
139 // The viewer only Configuration object
140 static Configuration* viewer_;
141
142 // Get server/viewer specific configuration object
143 static Configuration* server();
144 static Configuration* viewer();
145
146 // Append configuration object to this instance.
147 // NOTE: conf instance can be only one configuration object
148 void appendConfiguration(Configuration *conf) {
149 conf->_next = _next; _next = conf;
150 }
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000151 };
152
153 // -=- VoidParameter
154 // Configuration parameter base-class.
155
156 class VoidParameter {
157 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000158 VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000159 virtual ~VoidParameter();
160 const char* getName() const;
161 const char* getDescription() const;
162
163 virtual bool setParam(const char* value) = 0;
164 virtual bool setParam();
165 virtual char* getDefaultStr() const = 0;
166 virtual char* getValueStr() const = 0;
167 virtual bool isBool() const;
168
169 virtual void setImmutable();
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000170
171 protected:
172 friend class Configuration;
173 friend struct ParameterIterator;
174
175 VoidParameter* _next;
176 bool immutable;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000177 const char* name;
178 const char* description;
Pierre Ossman338e73a2016-07-07 15:35:13 +0200179
180 os::Mutex* mutex;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000181 };
182
183 class AliasParameter : public VoidParameter {
184 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000185 AliasParameter(const char* name_, const char* desc_,VoidParameter* param_,
186 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000187 virtual bool setParam(const char* value);
188 virtual bool setParam();
189 virtual char* getDefaultStr() const;
190 virtual char* getValueStr() const;
191 virtual bool isBool() const;
192 virtual void setImmutable();
193 private:
194 VoidParameter* param;
195 };
196
197 class BoolParameter : public VoidParameter {
198 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000199 BoolParameter(const char* name_, const char* desc_, bool v,
200 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000201 virtual bool setParam(const char* value);
202 virtual bool setParam();
203 virtual void setParam(bool b);
204 virtual char* getDefaultStr() const;
205 virtual char* getValueStr() const;
206 virtual bool isBool() const;
207 operator bool() const;
208 protected:
209 bool value;
210 bool def_value;
211 };
212
213 class IntParameter : public VoidParameter {
214 public:
215 IntParameter(const char* name_, const char* desc_, int v,
Adam Tkacc58b3d12010-04-23 13:55:10 +0000216 int minValue=INT_MIN, int maxValue=INT_MAX,
217 ConfigurationObject co=ConfGlobal);
Steve Kondik3f744f72017-07-08 01:45:10 -0700218 using VoidParameter::setParam;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000219 virtual bool setParam(const char* value);
220 virtual bool setParam(int v);
221 virtual char* getDefaultStr() const;
222 virtual char* getValueStr() const;
223 operator int() const;
224 protected:
225 int value;
226 int def_value;
227 int minValue, maxValue;
228 };
229
230 class StringParameter : public VoidParameter {
231 public:
232 // StringParameter contains a null-terminated string, which CANNOT
233 // be Null, and so neither can the default value!
Adam Tkacc58b3d12010-04-23 13:55:10 +0000234 StringParameter(const char* name_, const char* desc_, const char* v,
235 ConfigurationObject co=ConfGlobal);
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000236 virtual ~StringParameter();
237 virtual bool setParam(const char* value);
238 virtual char* getDefaultStr() const;
239 virtual char* getValueStr() const;
Adam Tkac3c7f8e12010-11-11 14:29:35 +0000240 void setDefaultStr(const char* v);
Pierre Ossman660f1082011-03-02 12:44:12 +0000241 operator const char*() const;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000242
243 // getData() returns a copy of the data - it must be delete[]d by the
244 // caller.
245 char* getData() const { return getValueStr(); }
246 protected:
247 char* value;
248 const char* def_value;
249 };
250
251 class BinaryParameter : public VoidParameter {
252 public:
Adam Tkacc58b3d12010-04-23 13:55:10 +0000253 BinaryParameter(const char* name_, const char* desc_, const void* v, int l,
254 ConfigurationObject co=ConfGlobal);
Steve Kondik3f744f72017-07-08 01:45:10 -0700255 using VoidParameter::setParam;
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000256 virtual ~BinaryParameter();
257 virtual bool setParam(const char* value);
258 virtual void setParam(const void* v, int l);
259 virtual char* getDefaultStr() const;
260 virtual char* getValueStr() const;
261
262 // getData() will return length zero if there is no data
263 // NB: data may be set to zero, OR set to a zero-length buffer
264 void getData(void** data, int* length) const;
265
266 protected:
267 char* value;
268 int length;
269 char* def_value;
270 int def_length;
271 };
272
273 // -=- ParameterIterator
Adam Tkac40060b42013-03-14 17:11:22 +0000274 // Iterates over all enabled parameters (global + server/viewer).
275 // Current Parameter is accessed via param, the current Configuration
276 // via config. The next() method moves on to the next Parameter.
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000277
278 struct ParameterIterator {
Adam Tkac40060b42013-03-14 17:11:22 +0000279 ParameterIterator() : config(Configuration::global()), param(config->head) {}
Constantin Kaplinskya2adc8d2006-05-25 05:01:55 +0000280 void next() {
281 param = param->_next;
282 while (!param) {
283 config = config->_next;
284 if (!config) break;
285 param = config->head;
286 }
287 }
288 Configuration* config;
289 VoidParameter* param;
290 };
291
292};
293
294#endif // __RFB_CONFIGURATION_H__