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

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