1 /*
2  * Copyright (C) 2000-2019 the xine project
3  *
4  * This file is part of xine, a free video player.
5  *
6  * xine is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * xine is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
19  *
20  * config file management
21  */
22 
23 #ifndef HAVE_CONFIGFILE_H
24 #define HAVE_CONFIGFILE_H
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #include <pthread.h>
31 
32 #include <xine.h>
33 
34 #define CONFIG_FILE_VERSION 2
35 
36 /**
37  * config entries above this experience
38  * level must never be changed from MRL
39  */
40 #define XINE_CONFIG_SECURITY 30
41 
42 
43 typedef struct cfg_entry_s cfg_entry_t;
44 typedef struct config_values_s config_values_t;
45 
46 struct cfg_entry_s {
47   cfg_entry_t     *next;
48   config_values_t *config;
49 
50   char            *key;
51   int              type;
52 
53   /** user experience level */
54   int              exp_level;
55 
56   /** type unknown */
57   char            *unknown_value;
58 
59   /** type string */
60   char            *str_value;
61   char            *str_default;
62 
63   /** common to range, enum, num, bool: */
64   int              num_value;
65   int              num_default;
66 
67   /** type range specific: */
68   int              range_min;
69   int              range_max;  /* also used for enum */
70 
71   /** type enum specific: */
72   char           **enum_values;
73 
74   /** help info for the user */
75   char            *description;
76   char            *help;
77 
78   /** callback function and data for live changeable values */
79   xine_config_cb_t callback;
80   void            *callback_data;
81 };
82 
83 struct config_values_s {
84 
85   /*
86    * register config values
87    *
88    * these functions return the current value of the
89    * registered item, i.e. the default value if it was
90    * not found in the config file or the current value
91    * from the config file otherwise
92    *
93    * NOTE on callbacks:
94    * - callback shall be safe to run from _any_ thread.
95    *   There will be no 2 calls at the same time, though.
96    * - callback shall be safe to call at any time between
97    *   entering register_foo (), and leaving unregister_foo ().
98    * - There can be multiple callbacks for the same key.
99    *   They will run in no fixed order.
100    * - if cb_data is a real pointer, make sure it points to
101    *   valid thread shared memory (malloc'ed or static).
102    *   Plain stack variables will not work, and may cause
103    *   strange malfunction.
104    */
105 
106   char* (*register_string) (config_values_t *self,
107 			    const char *key,
108 			    const char *def_value,
109 			    const char *description,
110 			    const char *help,
111 			    int exp_level,
112 			    xine_config_cb_t changed_cb,
113 			    void *cb_data);
114 
115   char* (*register_filename) (config_values_t *self,
116 			      const char *key,
117 			      const char *def_value,
118 			      int req_type,
119 			      const char *description,
120 			      const char *help,
121 			      int exp_level,
122 			      xine_config_cb_t changed_cb,
123 			      void *cb_data);
124 
125   int (*register_range) (config_values_t *self,
126 			 const char *key,
127 			 int def_value,
128 			 int min, int max,
129 			 const char *description,
130 			 const char *help,
131 			 int exp_level,
132 			 xine_config_cb_t changed_cb,
133 			 void *cb_data);
134 
135   int (*register_enum) (config_values_t *self,
136 			const char *key,
137 			int def_value,
138 			char **values,
139 			const char *description,
140 			const char *help,
141 			int exp_level,
142 			xine_config_cb_t changed_cb,
143 			void *cb_data);
144 
145   int (*register_num) (config_values_t *self,
146 		       const char *key,
147 		       int def_value,
148 		       const char *description,
149 		       const char *help,
150 		       int exp_level,
151 		       xine_config_cb_t changed_cb,
152 		       void *cb_data);
153 
154   int (*register_bool) (config_values_t *self,
155 			const char *key,
156 			int def_value,
157 			const char *description,
158 			const char *help,
159 			int exp_level,
160 			xine_config_cb_t changed_cb,
161 			void *cb_data);
162 
163   /** not yet implemented */
164   void (*register_entry) (config_values_t *self, cfg_entry_t* entry);
165 
166   /** convenience function to update range, enum, num and bool values */
167   void (*update_num) (config_values_t *self, const char *key, int value);
168 
169   /** convenience function to update string values */
170   void (*update_string) (config_values_t *self, const char *key, const char *value);
171 
172   /** small utility function for enum handling */
173   int (*parse_enum) (const char *str, const char **values);
174 
175   /**
176    * @brief lookup config entries
177    *
178    * remember to call the changed_cb if it exists
179    * and you changed the value of this item
180    */
181 
182   cfg_entry_t* (*lookup_entry) (config_values_t *self, const char *key);
183 
184   /**
185    * unregister _all_ entry callback functions for this key.
186    * if there may be multiple callbacks on different cb_data,
187    * consider using unregister_callbacks (self, NULL, NULL, my_data, sizeof (*my_data))
188    * before freeing each instance instead. this also eliminates the need
189    * to unregister every key separately.
190    */
191   void (*unregister_callback) (config_values_t *self, const char *key);
192 
193   /**
194    * dispose of all config entries in memory
195    */
196   void (*dispose) (config_values_t *self);
197 
198   /**
199    * callback called when a new config entry is registered
200    */
201   void (*set_new_entry_callback) (config_values_t *self, xine_config_cb_t new_entry_cb, void *cb_data);
202 
203   /**
204    * unregister the callback
205    */
206   void (*unset_new_entry_callback) (config_values_t *self);
207 
208   /**
209    * serialize a config entry.
210    * return a base64 null terminated string.
211    */
212   char* (*get_serialized_entry) (config_values_t *self, const char *key);
213 
214   /**
215    * deserialize a config entry.
216    * value is a base 64 encoded string
217    * return the key of the serialized entry
218    */
219   char* (*register_serialized_entry) (config_values_t *self, const char *value);
220 
221   /**
222    * config values are stored here:
223    */
224   cfg_entry_t         *first, *last, *cur;
225 
226   /**
227    * new entry callback
228    */
229   xine_config_cb_t    new_entry_cb;
230   void                *new_entry_cbdata;
231 
232   /**
233    * mutex for modification to the config
234    */
235   pthread_mutex_t      config_lock;
236 
237   /**
238    * current config file's version number
239    */
240   int current_version;
241 
242   /**
243    * unregister multiple entry callback functions.
244    * all 3 values need to match unless they are NULL.
245    * if cb_data_size is not zero, data pointers within the range
246    * (cb_data <= ptr < cb_data + cb_data_size) will match.
247    * returns the count of unregistered functions.
248    */
249   int (*unregister_callbacks) (config_values_t *self,
250     const char *key, xine_config_cb_t changed_cb, void *cb_data, size_t cb_data_size);
251 
252   /**
253    * Set this manually to enable logging.
254    */
255   xine_t *xine;
256 
257   /**
258    * MT-safe convenience function to lookup string values.
259    * Returns copy of current value or NULL.
260    * Returned string must be freed with config->free_string().
261    */
262   char * (*lookup_string)(config_values_t *, const char *key);
263   void   (*free_string)(config_values_t *, char **);
264 
265   /** convenience function to lookup numeric values */
266   int    (*lookup_num)(config_values_t *, const char *key, int def_value);
267 };
268 
269 /**
270  * @brief allocate and init a new xine config object
271  * @internal
272  */
273 config_values_t *_x_config_init (void);
274 
275 /**
276  * @brief interpret stream_setup part of mrls for config value changes
277  * @internal
278  */
279 
280 int _x_config_change_opt(config_values_t *config, const char *opt);
281 
282 /** deprecated in favour of config_values_t->unregister_callbacks (). */
283 void _x_config_unregister_cb_class_d (config_values_t *config, void *callback_data) XINE_PROTECTED;
284 void _x_config_unregister_cb_class_p (config_values_t *config, xine_config_cb_t callback) XINE_PROTECTED;
285 
286 #ifdef __cplusplus
287 }
288 #endif
289 
290 #endif
291 
292