You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Configuration.h 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /* Copyright (C) 2002-2005 RealVNC Ltd. All Rights Reserved.
  2. * Copyright 2011-2022 Pierre Ossman for Cendio AB
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This software is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this software; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  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. #ifndef __RFB_CONFIGURATION_H__
  43. #define __RFB_CONFIGURATION_H__
  44. #include <vector>
  45. #include <rfb/util.h>
  46. namespace os { class Mutex; }
  47. namespace rfb {
  48. class VoidParameter;
  49. struct ParameterIterator;
  50. enum ConfigurationObject { ConfGlobal, ConfServer, ConfViewer };
  51. // -=- Configuration
  52. // Class used to access parameters.
  53. class Configuration {
  54. public:
  55. // - Create a new Configuration object
  56. Configuration(const char* name_) : name(strDup(name_)), head(0), _next(0) {}
  57. // - Return the buffer containing the Configuration's name
  58. const char* getName() const { return name.buf; }
  59. // - Set named parameter to value
  60. bool set(const char* param, const char* value, bool immutable=false);
  61. // - Set parameter to value (separated by "=")
  62. bool set(const char* config, bool immutable=false);
  63. // - Set named parameter to value, with name truncated at len
  64. bool set(const char* name, int len,
  65. const char* val, bool immutable);
  66. // - Get named parameter
  67. VoidParameter* get(const char* param);
  68. // - List the parameters of this Configuration group
  69. void list(int width=79, int nameWidth=10);
  70. // - Remove a parameter from this Configuration group
  71. bool remove(const char* param);
  72. // - readFromFile
  73. // Read configuration parameters from the specified file.
  74. void readFromFile(const char* filename);
  75. // - writeConfigToFile
  76. // Write a new configuration parameters file, then mv it
  77. // over the old file.
  78. void writeToFile(const char* filename);
  79. // - Get the Global Configuration object
  80. // NB: This call does NOT lock the Configuration system.
  81. // ALWAYS ensure that if you have ANY global Parameters,
  82. // then they are defined as global objects, to ensure that
  83. // global() is called when only the main thread is running.
  84. static Configuration* global();
  85. // Enable server/viewer specific parameters
  86. static void enableServerParams() { global()->appendConfiguration(server()); }
  87. static void enableViewerParams() { global()->appendConfiguration(viewer()); }
  88. // - Container for process-wide Global parameters
  89. static bool setParam(const char* param, const char* value, bool immutable=false) {
  90. return global()->set(param, value, immutable);
  91. }
  92. static bool setParam(const char* config, bool immutable=false) {
  93. return global()->set(config, immutable);
  94. }
  95. static bool setParam(const char* name, int len,
  96. const char* val, bool immutable) {
  97. return global()->set(name, len, val, immutable);
  98. }
  99. static VoidParameter* getParam(const char* param) { return global()->get(param); }
  100. static void listParams(int width=79, int nameWidth=10) {
  101. global()->list(width, nameWidth);
  102. }
  103. static bool removeParam(const char* param) {
  104. return global()->remove(param);
  105. }
  106. private:
  107. friend class VoidParameter;
  108. friend struct ParameterIterator;
  109. // Name for this Configuration
  110. CharArray name;
  111. // - Pointer to first Parameter in this group
  112. VoidParameter* head;
  113. // Pointer to next Configuration in this group
  114. Configuration* _next;
  115. // The process-wide, Global Configuration object
  116. static Configuration* global_;
  117. // The server only Configuration object
  118. static Configuration* server_;
  119. // The viewer only Configuration object
  120. static Configuration* viewer_;
  121. // Get server/viewer specific configuration object
  122. static Configuration* server();
  123. static Configuration* viewer();
  124. // Append configuration object to this instance.
  125. // NOTE: conf instance can be only one configuration object
  126. void appendConfiguration(Configuration *conf) {
  127. conf->_next = _next; _next = conf;
  128. }
  129. };
  130. // -=- VoidParameter
  131. // Configuration parameter base-class.
  132. class VoidParameter {
  133. public:
  134. VoidParameter(const char* name_, const char* desc_, ConfigurationObject co=ConfGlobal);
  135. virtual ~VoidParameter();
  136. const char* getName() const;
  137. const char* getDescription() const;
  138. virtual bool setParam(const char* value) = 0;
  139. virtual bool setParam();
  140. virtual std::string getDefaultStr() const = 0;
  141. virtual std::string getValueStr() const = 0;
  142. virtual bool isBool() const;
  143. virtual void setImmutable();
  144. protected:
  145. friend class Configuration;
  146. friend struct ParameterIterator;
  147. VoidParameter* _next;
  148. bool immutable;
  149. const char* name;
  150. const char* description;
  151. os::Mutex* mutex;
  152. };
  153. class AliasParameter : public VoidParameter {
  154. public:
  155. AliasParameter(const char* name_, const char* desc_,VoidParameter* param_,
  156. ConfigurationObject co=ConfGlobal);
  157. virtual bool setParam(const char* value);
  158. virtual bool setParam();
  159. virtual std::string getDefaultStr() const;
  160. virtual std::string getValueStr() const;
  161. virtual bool isBool() const;
  162. virtual void setImmutable();
  163. private:
  164. VoidParameter* param;
  165. };
  166. class BoolParameter : public VoidParameter {
  167. public:
  168. BoolParameter(const char* name_, const char* desc_, bool v,
  169. ConfigurationObject co=ConfGlobal);
  170. virtual bool setParam(const char* value);
  171. virtual bool setParam();
  172. virtual void setParam(bool b);
  173. virtual std::string getDefaultStr() const;
  174. virtual std::string getValueStr() const;
  175. virtual bool isBool() const;
  176. operator bool() const;
  177. protected:
  178. bool value;
  179. bool def_value;
  180. };
  181. class IntParameter : public VoidParameter {
  182. public:
  183. IntParameter(const char* name_, const char* desc_, int v,
  184. int minValue=INT_MIN, int maxValue=INT_MAX,
  185. ConfigurationObject co=ConfGlobal);
  186. using VoidParameter::setParam;
  187. virtual bool setParam(const char* value);
  188. virtual bool setParam(int v);
  189. virtual std::string getDefaultStr() const;
  190. virtual std::string getValueStr() const;
  191. operator int() const;
  192. protected:
  193. int value;
  194. int def_value;
  195. int minValue, maxValue;
  196. };
  197. class StringParameter : public VoidParameter {
  198. public:
  199. // StringParameter contains a null-terminated string, which CANNOT
  200. // be Null, and so neither can the default value!
  201. StringParameter(const char* name_, const char* desc_, const char* v,
  202. ConfigurationObject co=ConfGlobal);
  203. virtual ~StringParameter();
  204. virtual bool setParam(const char* value);
  205. virtual std::string getDefaultStr() const;
  206. virtual std::string getValueStr() const;
  207. operator const char*() const;
  208. protected:
  209. char* value;
  210. char* def_value;
  211. };
  212. class BinaryParameter : public VoidParameter {
  213. public:
  214. BinaryParameter(const char* name_, const char* desc_,
  215. const uint8_t* v, size_t l,
  216. ConfigurationObject co=ConfGlobal);
  217. using VoidParameter::setParam;
  218. virtual ~BinaryParameter();
  219. virtual bool setParam(const char* value);
  220. virtual void setParam(const uint8_t* v, size_t l);
  221. virtual std::string getDefaultStr() const;
  222. virtual std::string getValueStr() const;
  223. std::vector<uint8_t> getData() const;
  224. protected:
  225. uint8_t* value;
  226. size_t length;
  227. uint8_t* def_value;
  228. size_t def_length;
  229. };
  230. // -=- ParameterIterator
  231. // Iterates over all enabled parameters (global + server/viewer).
  232. // Current Parameter is accessed via param, the current Configuration
  233. // via config. The next() method moves on to the next Parameter.
  234. struct ParameterIterator {
  235. ParameterIterator() : config(Configuration::global()), param(config->head) {}
  236. void next() {
  237. param = param->_next;
  238. while (!param) {
  239. config = config->_next;
  240. if (!config) break;
  241. param = config->head;
  242. }
  243. }
  244. Configuration* config;
  245. VoidParameter* param;
  246. };
  247. };
  248. #endif // __RFB_CONFIGURATION_H__