1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv 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 Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include <stdlib.h>
19 #include <stdio.h>
20 #include <errno.h>
21 #include <string.h>
22 #include <strings.h>
23 #include <assert.h>
24 #include <stdbool.h>
25 #include <pthread.h>
26 
27 #include "m_config_core.h"
28 #include "options/m_option.h"
29 #include "common/common.h"
30 #include "common/global.h"
31 #include "common/msg.h"
32 #include "common/msg_control.h"
33 #include "misc/dispatch.h"
34 #include "osdep/atomic.h"
35 
36 // For use with m_config_cache.
37 struct m_config_shadow {
38     pthread_mutex_t lock;
39     // Incremented on every option change.
40     mp_atomic_uint64 ts;
41     // -- immutable after init
42     // List of m_sub_options instances.
43     // Index 0 is the top-level and is always present.
44     // Immutable after init.
45     // Invariant: a parent is always at a lower index than any of its children.
46     struct m_config_group *groups;
47     int num_groups;
48     // -- protected by lock
49     struct m_config_data *data; // protected shadow copy of the option data
50     struct config_cache **listeners;
51     int num_listeners;
52 };
53 
54 // Represents a sub-struct (OPT_SUBSTRUCT()).
55 struct m_config_group {
56     const struct m_sub_options *group;
57     int opt_count;      // cached opt. count; group->opts[opt_count].name==NULL
58     int group_count;    // 1 + number of all sub groups owned by this (so
59                         // m_config_shadow.groups[idx..idx+group_count] is used
60                         // by the entire tree of sub groups included by this
61                         // group)
62     int parent_group;   // index of parent group into m_config_shadow.groups[],
63                         // or -1 for group 0
64     int parent_ptr;     // ptr offset in the parent group's data, or -1 if
65                         // none
66     const char *prefix; // concat_name(_, prefix, opt->name) => full name
67                         // (the parent names are already included in this)
68 };
69 
70 // A copy of option data. Used for the main option struct, the shadow data,
71 // and copies for m_config_cache.
72 struct m_config_data {
73     struct m_config_shadow *shadow; // option definitions etc., main data copy
74     int group_index;                // start index into m_config.groups[]
75     struct m_group_data *gdata;     // user struct allocation (our copy of data)
76     int num_gdata;                  // (group_index+num_gdata = end index)
77 };
78 
79 struct config_cache {
80     struct m_config_cache *public;
81 
82     struct m_config_data *data;     // public data
83     struct m_config_data *src;      // global data (currently ==shadow->data)
84     struct m_config_shadow *shadow; // global metadata
85     int group_start, group_end;     // derived from data->group_index etc.
86     uint64_t ts;                    // timestamp of this data copy
87     bool in_list;                   // part of m_config_shadow->listeners[]
88     int upd_group;                  // for "incremental" change notification
89     int upd_opt;
90 
91 
92     // --- Implicitly synchronized by setting/unsetting wakeup_cb.
93     struct mp_dispatch_queue *wakeup_dispatch_queue;
94     void (*wakeup_dispatch_cb)(void *ctx);
95     void *wakeup_dispatch_cb_ctx;
96 
97     // --- Protected by shadow->lock
98     void (*wakeup_cb)(void *ctx);
99     void *wakeup_cb_ctx;
100 };
101 
102 // Per m_config_data state for each m_config_group.
103 struct m_group_data {
104     char *udata;        // pointer to group user option struct
105     uint64_t ts;        // timestamp of the data copy
106 };
107 
108 static const union m_option_value default_value = {0};
109 
110 static void add_sub_group(struct m_config_shadow *shadow, const char *name_prefix,
111                           int parent_group_index, int parent_ptr,
112                           const struct m_sub_options *subopts);
113 
m_config_gdata(struct m_config_data * data,int group_index)114 static struct m_group_data *m_config_gdata(struct m_config_data *data,
115                                            int group_index)
116 {
117     if (group_index < data->group_index ||
118         group_index >= data->group_index + data->num_gdata)
119         return NULL;
120 
121     return &data->gdata[group_index - data->group_index];
122 }
123 
124 // Like concat_name(), but returns either a, b, or buf. buf/buf_size is used as
125 // target for snprintf(). (buf_size is recommended to be MAX_OPT_NAME_LEN.)
concat_name_buf(char * buf,size_t buf_size,const char * a,const char * b)126 static const char *concat_name_buf(char *buf, size_t buf_size,
127                                    const char *a, const char *b)
128 {
129     assert(a);
130     assert(b);
131     if (!a[0])
132         return b;
133     if (!b[0])
134         return a;
135     snprintf(buf, buf_size, "%s-%s", a, b);
136     return buf;
137 }
138 
139 // Return full option name from prefix (a) and option name (b). Returns either
140 // a, b, or a talloc'ed string under ta_parent.
concat_name(void * ta_parent,const char * a,const char * b)141 static const char *concat_name(void *ta_parent, const char *a, const char *b)
142 {
143     char buf[M_CONFIG_MAX_OPT_NAME_LEN];
144     const char *r = concat_name_buf(buf, sizeof(buf), a, b);
145     return r == buf ? talloc_strdup(ta_parent, r) : r;
146 }
147 
iter_next(struct m_config_shadow * shadow,int group_start,int group_end,int32_t * p_id)148 static bool iter_next(struct m_config_shadow *shadow, int group_start,
149                       int group_end, int32_t *p_id)
150 {
151     int32_t id = *p_id;
152     int group_index = id == -1 ? group_start : id >> 16;
153     int opt_index = id == -1 ? -1 : id & 0xFFFF;
154 
155     assert(group_index >= group_start && group_index <= group_end);
156 
157     while (1) {
158         if (group_index >= group_end)
159             return false;
160 
161         struct m_config_group *g = &shadow->groups[group_index];
162         const struct m_option *opts = g->group->opts;
163 
164         assert(opt_index >= -1 && opt_index < g->opt_count);
165 
166         opt_index += 1;
167 
168         if (!opts || !opts[opt_index].name) {
169             group_index += 1;
170             opt_index = -1;
171             continue;
172         }
173 
174         if (opts[opt_index].type == &m_option_type_subconfig)
175             continue;
176 
177         *p_id = (group_index << 16) | opt_index;
178         return true;
179     }
180 }
181 
m_config_shadow_get_next_opt(struct m_config_shadow * shadow,int32_t * p_id)182 bool m_config_shadow_get_next_opt(struct m_config_shadow *shadow, int32_t *p_id)
183 {
184     return iter_next(shadow, 0, shadow->num_groups, p_id);
185 }
186 
m_config_cache_get_next_opt(struct m_config_cache * cache,int32_t * p_id)187 bool m_config_cache_get_next_opt(struct m_config_cache *cache, int32_t *p_id)
188 {
189     return iter_next(cache->shadow, cache->internal->group_start,
190                      cache->internal->group_end, p_id);
191 }
192 
get_opt_from_id(struct m_config_shadow * shadow,int32_t id,int * out_group_index,int * out_opt_index)193 static void get_opt_from_id(struct m_config_shadow *shadow, int32_t id,
194                             int *out_group_index, int *out_opt_index)
195 {
196     int group_index = id >> 16;
197     int opt_index = id & 0xFFFF;
198 
199     assert(group_index >= 0 && group_index < shadow->num_groups);
200     assert(opt_index >= 0 && opt_index < shadow->groups[group_index].opt_count);
201 
202     *out_group_index = group_index;
203     *out_opt_index = opt_index;
204 }
205 
m_config_shadow_get_opt(struct m_config_shadow * shadow,int32_t id)206 const struct m_option *m_config_shadow_get_opt(struct m_config_shadow *shadow,
207                                                int32_t id)
208 {
209     int group_index, opt_index;
210     get_opt_from_id(shadow, id, &group_index, &opt_index);
211 
212     return &shadow->groups[group_index].group->opts[opt_index];
213 }
214 
m_config_shadow_get_opt_name(struct m_config_shadow * shadow,int32_t id,char * buf,size_t buf_size)215 const char *m_config_shadow_get_opt_name(struct m_config_shadow *shadow,
216                                          int32_t id, char *buf, size_t buf_size)
217 {
218     int group_index, opt_index;
219     get_opt_from_id(shadow, id, &group_index, &opt_index);
220 
221     struct m_config_group *g = &shadow->groups[group_index];
222     return concat_name_buf(buf, buf_size, g->prefix,
223                            g->group->opts[opt_index].name);
224 }
225 
m_config_shadow_get_opt_default(struct m_config_shadow * shadow,int32_t id)226 const void *m_config_shadow_get_opt_default(struct m_config_shadow *shadow,
227                                             int32_t id)
228 {
229     int group_index, opt_index;
230     get_opt_from_id(shadow, id, &group_index, &opt_index);
231 
232     const struct m_sub_options *subopt = shadow->groups[group_index].group;
233     const struct m_option *opt = &subopt->opts[opt_index];
234 
235     if (opt->offset < 0)
236         return NULL;
237 
238     if (opt->defval)
239         return opt->defval;
240 
241     if (subopt->defaults)
242         return (char *)subopt->defaults + opt->offset;
243 
244     return &default_value;
245 }
246 
m_config_cache_get_opt_data(struct m_config_cache * cache,int32_t id)247 void *m_config_cache_get_opt_data(struct m_config_cache *cache, int32_t id)
248 {
249     int group_index, opt_index;
250     get_opt_from_id(cache->shadow, id, &group_index, &opt_index);
251 
252     assert(group_index >= cache->internal->group_start &&
253            group_index < cache->internal->group_end);
254 
255     struct m_group_data *gd = m_config_gdata(cache->internal->data, group_index);
256     const struct m_option *opt =
257         &cache->shadow->groups[group_index].group->opts[opt_index];
258 
259     return gd && opt->offset >= 0 ? gd->udata + opt->offset : NULL;
260 }
261 
get_opt_change_mask(struct m_config_shadow * shadow,int group_index,int group_root,const struct m_option * opt)262 static uint64_t get_opt_change_mask(struct m_config_shadow *shadow, int group_index,
263                                     int group_root, const struct m_option *opt)
264 {
265     uint64_t changed = opt->flags & UPDATE_OPTS_MASK;
266     while (group_index != group_root) {
267         struct m_config_group *g = &shadow->groups[group_index];
268         changed |= g->group->change_flags;
269         group_index = g->parent_group;
270     }
271     return changed;
272 }
273 
m_config_cache_get_option_change_mask(struct m_config_cache * cache,int32_t id)274 uint64_t m_config_cache_get_option_change_mask(struct m_config_cache *cache,
275                                                int32_t id)
276 {
277     struct m_config_shadow *shadow = cache->shadow;
278     int group_index, opt_index;
279     get_opt_from_id(shadow, id, &group_index, &opt_index);
280 
281     assert(group_index >= cache->internal->group_start &&
282            group_index < cache->internal->group_end);
283 
284     return get_opt_change_mask(cache->shadow, group_index,
285                                cache->internal->data->group_index,
286                                &shadow->groups[group_index].group->opts[opt_index]);
287 }
288 
289 // The memcpys are supposed to work around the strict aliasing violation,
290 // that would result if we just dereferenced a void** (where the void** is
291 // actually casted from struct some_type* ). The dummy struct type is in
292 // theory needed, because void* and struct pointers could have different
293 // representations, while pointers to different struct types don't.
substruct_read_ptr(const void * ptr)294 static void *substruct_read_ptr(const void *ptr)
295 {
296     struct mp_dummy_ *res;
297     memcpy(&res, ptr, sizeof(res));
298     return res;
299 }
substruct_write_ptr(void * ptr,void * val)300 static void substruct_write_ptr(void *ptr, void *val)
301 {
302     struct mp_dummy_ *src = val;
303     memcpy(ptr, &src, sizeof(src));
304 }
305 
306 // Initialize a field with a given value. In case this is dynamic data, it has
307 // to be allocated and copied. src can alias dst.
init_opt_inplace(const struct m_option * opt,void * dst,const void * src)308 static void init_opt_inplace(const struct m_option *opt, void *dst,
309                              const void *src)
310 {
311     // The option will use dynamic memory allocation iff it has a free callback.
312     if (opt->type->free) {
313         union m_option_value temp;
314         memcpy(&temp, src, opt->type->size);
315         memset(dst, 0, opt->type->size);
316         m_option_copy(opt, dst, &temp);
317     } else if (src != dst) {
318         memcpy(dst, src, opt->type->size);
319     }
320 }
321 
alloc_group(struct m_config_data * data,int group_index,struct m_config_data * copy)322 static void alloc_group(struct m_config_data *data, int group_index,
323                         struct m_config_data *copy)
324 {
325     assert(group_index == data->group_index + data->num_gdata);
326     assert(group_index < data->shadow->num_groups);
327     struct m_config_group *group = &data->shadow->groups[group_index];
328     const struct m_sub_options *opts = group->group;
329 
330     MP_TARRAY_GROW(data, data->gdata, data->num_gdata);
331     struct m_group_data *gdata = &data->gdata[data->num_gdata++];
332 
333     struct m_group_data *copy_gdata =
334         copy ? m_config_gdata(copy, group_index) : NULL;
335 
336     *gdata = (struct m_group_data){
337         .udata = talloc_zero_size(data, opts->size),
338         .ts = copy_gdata ? copy_gdata->ts : 0,
339     };
340 
341     if (opts->defaults)
342         memcpy(gdata->udata, opts->defaults, opts->size);
343 
344     char *copy_src = copy_gdata ? copy_gdata->udata : NULL;
345 
346     for (int n = 0; opts->opts && opts->opts[n].name; n++) {
347         const struct m_option *opt = &opts->opts[n];
348 
349         if (opt->offset < 0 || opt->type->size == 0)
350             continue;
351 
352         void *dst = gdata->udata + opt->offset;
353         const void *defptr = opt->defval ? opt->defval : dst;
354         if (copy_src)
355             defptr = copy_src + opt->offset;
356 
357         init_opt_inplace(opt, dst, defptr);
358     }
359 
360     // If there's a parent, update its pointer to the new struct.
361     if (group->parent_group >= data->group_index && group->parent_ptr >= 0) {
362         struct m_group_data *parent_gdata =
363             m_config_gdata(data, group->parent_group);
364         assert(parent_gdata);
365 
366         substruct_write_ptr(parent_gdata->udata + group->parent_ptr, gdata->udata);
367     }
368 }
369 
free_option_data(void * p)370 static void free_option_data(void *p)
371 {
372     struct m_config_data *data = p;
373 
374     for (int i = 0; i < data->num_gdata; i++) {
375         struct m_group_data *gdata = &data->gdata[i];
376         struct m_config_group *group =
377             &data->shadow->groups[data->group_index + i];
378         const struct m_option *opts = group->group->opts;
379 
380         for (int n = 0; opts && opts[n].name; n++) {
381             const struct m_option *opt = &opts[n];
382 
383             if (opt->offset >= 0 && opt->type->size > 0)
384                 m_option_free(opt, gdata->udata + opt->offset);
385         }
386     }
387 }
388 
389 // Allocate data using the option description in shadow, starting at group_index
390 // (index into m_config.groups[]).
391 // If copy is not NULL, copy all data from there (for groups which are in both
392 // m_config_data instances), in all other cases init the data with the defaults.
allocate_option_data(void * ta_parent,struct m_config_shadow * shadow,int group_index,struct m_config_data * copy)393 static struct m_config_data *allocate_option_data(void *ta_parent,
394                                                   struct m_config_shadow *shadow,
395                                                   int group_index,
396                                                   struct m_config_data *copy)
397 {
398     assert(group_index >= 0 && group_index < shadow->num_groups);
399     struct m_config_data *data = talloc_zero(ta_parent, struct m_config_data);
400     talloc_set_destructor(data, free_option_data);
401 
402     data->shadow = shadow;
403     data->group_index = group_index;
404 
405     struct m_config_group *root_group = &shadow->groups[group_index];
406     assert(root_group->group_count > 0);
407 
408     for (int n = group_index; n < group_index + root_group->group_count; n++)
409         alloc_group(data, n, copy);
410 
411     return data;
412 }
413 
shadow_destroy(void * p)414 static void shadow_destroy(void *p)
415 {
416     struct m_config_shadow *shadow = p;
417 
418     // must all have been unregistered
419     assert(shadow->num_listeners == 0);
420 
421     talloc_free(shadow->data);
422     pthread_mutex_destroy(&shadow->lock);
423 }
424 
m_config_shadow_new(const struct m_sub_options * root)425 struct m_config_shadow *m_config_shadow_new(const struct m_sub_options *root)
426 {
427     struct m_config_shadow *shadow = talloc_zero(NULL, struct m_config_shadow);
428     talloc_set_destructor(shadow, shadow_destroy);
429     pthread_mutex_init(&shadow->lock, NULL);
430 
431     add_sub_group(shadow, NULL, -1, -1, root);
432 
433     if (!root->size)
434         return shadow;
435 
436     shadow->data = allocate_option_data(shadow, shadow, 0, NULL);
437 
438     return shadow;
439 }
440 
init_obj_settings_list(struct m_config_shadow * shadow,int parent_group_index,const struct m_obj_list * list)441 static void init_obj_settings_list(struct m_config_shadow *shadow,
442                                    int parent_group_index,
443                                    const struct m_obj_list *list)
444 {
445     struct m_obj_desc desc;
446     for (int n = 0; ; n++) {
447         if (!list->get_desc(&desc, n))
448             break;
449         if (desc.global_opts) {
450             add_sub_group(shadow, NULL, parent_group_index, -1,
451                           desc.global_opts);
452         }
453         if (list->use_global_options && desc.options) {
454             struct m_sub_options *conf = talloc_ptrtype(shadow, conf);
455             *conf = (struct m_sub_options){
456                 .prefix = desc.options_prefix,
457                 .opts = desc.options,
458                 .defaults = desc.priv_defaults,
459                 .size = desc.priv_size,
460             };
461             add_sub_group(shadow, NULL, parent_group_index, -1, conf);
462         }
463     }
464 }
465 
add_sub_group(struct m_config_shadow * shadow,const char * name_prefix,int parent_group_index,int parent_ptr,const struct m_sub_options * subopts)466 static void add_sub_group(struct m_config_shadow *shadow, const char *name_prefix,
467                           int parent_group_index, int parent_ptr,
468                           const struct m_sub_options *subopts)
469 {
470     // Can't be used multiple times.
471     for (int n = 0; n < shadow->num_groups; n++)
472         assert(shadow->groups[n].group != subopts);
473 
474     if (!name_prefix)
475         name_prefix = "";
476     if (subopts->prefix && subopts->prefix[0]) {
477         assert(!name_prefix[0]);
478         name_prefix = subopts->prefix;
479     }
480 
481     // You can only use UPDATE_ flags here.
482     assert(!(subopts->change_flags & ~(unsigned)UPDATE_OPTS_MASK));
483 
484     assert(parent_group_index >= -1 && parent_group_index < shadow->num_groups);
485 
486     int group_index = shadow->num_groups++;
487     MP_TARRAY_GROW(shadow, shadow->groups, group_index);
488     shadow->groups[group_index] = (struct m_config_group){
489         .group = subopts,
490         .parent_group = parent_group_index,
491         .parent_ptr = parent_ptr,
492         .prefix = name_prefix,
493     };
494 
495     for (int i = 0; subopts->opts && subopts->opts[i].name; i++) {
496         const struct m_option *opt = &subopts->opts[i];
497 
498         if (opt->type == &m_option_type_subconfig) {
499             const struct m_sub_options *new_subopts = opt->priv;
500 
501             // Providing default structs in-place is not allowed.
502             if (opt->offset >= 0 && subopts->defaults) {
503                 void *ptr = (char *)subopts->defaults + opt->offset;
504                 assert(!substruct_read_ptr(ptr));
505             }
506 
507             const char *prefix = concat_name(shadow, name_prefix, opt->name);
508             add_sub_group(shadow, prefix, group_index, opt->offset, new_subopts);
509 
510         } else if (opt->type == &m_option_type_obj_settings_list) {
511             const struct m_obj_list *objlist = opt->priv;
512             init_obj_settings_list(shadow, group_index, objlist);
513         }
514 
515         shadow->groups[group_index].opt_count = i + 1;
516     }
517 
518     if (subopts->get_sub_options) {
519         for (int i = 0; ; i++) {
520             const struct m_sub_options *sub = NULL;
521             if (!subopts->get_sub_options(i, &sub))
522                 break;
523             if (sub)
524                 add_sub_group(shadow, NULL, group_index, -1, sub);
525         }
526     }
527 
528     shadow->groups[group_index].group_count = shadow->num_groups - group_index;
529 }
530 
cache_destroy(void * p)531 static void cache_destroy(void *p)
532 {
533     struct m_config_cache *cache = p;
534 
535     // (technically speaking, being able to call them both without anything
536     // breaking is a feature provided by these functions)
537     m_config_cache_set_wakeup_cb(cache, NULL, NULL);
538     m_config_cache_set_dispatch_change_cb(cache, NULL, NULL, NULL);
539 }
540 
m_config_cache_from_shadow(void * ta_parent,struct m_config_shadow * shadow,const struct m_sub_options * group)541 struct m_config_cache *m_config_cache_from_shadow(void *ta_parent,
542                                             struct m_config_shadow *shadow,
543                                             const struct m_sub_options *group)
544 {
545     int group_index = -1;
546 
547     for (int n = 0; n < shadow->num_groups; n++) {
548         if (shadow->groups[n].group == group) {
549             group_index = n;
550             break;
551         }
552     }
553 
554     assert(group_index >= 0); // invalid group (or not in option tree)
555 
556     struct cache_alloc {
557         struct m_config_cache a;
558         struct config_cache b;
559     };
560     struct cache_alloc *alloc = talloc_zero(ta_parent, struct cache_alloc);
561     assert((void *)&alloc->a == (void *)alloc);
562     struct m_config_cache *cache = &alloc->a;
563     talloc_set_destructor(cache, cache_destroy);
564     cache->internal = &alloc->b;
565     cache->shadow = shadow;
566 
567     struct config_cache *in = cache->internal;
568     in->shadow = shadow;
569     in->src = shadow->data;
570 
571     pthread_mutex_lock(&shadow->lock);
572     in->data = allocate_option_data(cache, shadow, group_index, in->src);
573     pthread_mutex_unlock(&shadow->lock);
574 
575     cache->opts = in->data->gdata[0].udata;
576 
577     in->group_start = in->data->group_index;
578     in->group_end = in->group_start + in->data->num_gdata;
579     assert(shadow->groups[in->group_start].group_count == in->data->num_gdata);
580 
581     in->upd_group = -1;
582 
583     return cache;
584 }
585 
m_config_cache_alloc(void * ta_parent,struct mpv_global * global,const struct m_sub_options * group)586 struct m_config_cache *m_config_cache_alloc(void *ta_parent,
587                                             struct mpv_global *global,
588                                             const struct m_sub_options *group)
589 {
590     return m_config_cache_from_shadow(ta_parent, global->config, group);
591 }
592 
update_next_option(struct m_config_cache * cache,void ** p_opt)593 static void update_next_option(struct m_config_cache *cache, void **p_opt)
594 {
595     struct config_cache *in = cache->internal;
596     struct m_config_data *dst = in->data;
597     struct m_config_data *src = in->src;
598 
599     assert(src->group_index == 0); // must be the option root currently
600 
601     *p_opt = NULL;
602 
603     while (in->upd_group < dst->group_index + dst->num_gdata) {
604         struct m_group_data *gsrc = m_config_gdata(src, in->upd_group);
605         struct m_group_data *gdst = m_config_gdata(dst, in->upd_group);
606         assert(gsrc && gdst);
607 
608         if (gdst->ts < gsrc->ts) {
609             struct m_config_group *g = &dst->shadow->groups[in->upd_group];
610             const struct m_option *opts = g->group->opts;
611 
612             while (opts && opts[in->upd_opt].name) {
613                 const struct m_option *opt = &opts[in->upd_opt];
614 
615                 if (opt->offset >= 0 && opt->type->size) {
616                     void *dsrc = gsrc->udata + opt->offset;
617                     void *ddst = gdst->udata + opt->offset;
618 
619                     if (!m_option_equal(opt, ddst, dsrc)) {
620                         uint64_t ch = get_opt_change_mask(dst->shadow,
621                                         in->upd_group, dst->group_index, opt);
622 
623                         if (cache->debug) {
624                             char *vdst = m_option_print(opt, ddst);
625                             char *vsrc = m_option_print(opt, dsrc);
626                             mp_warn(cache->debug, "Option '%s' changed from "
627                                     "'%s' to' %s' (flags = 0x%"PRIx64")\n",
628                                     opt->name, vdst, vsrc, ch);
629                             talloc_free(vdst);
630                             talloc_free(vsrc);
631                         }
632 
633                         m_option_copy(opt, ddst, dsrc);
634                         cache->change_flags |= ch;
635 
636                         in->upd_opt++; // skip this next time
637                         *p_opt = ddst;
638                         return;
639                     }
640                 }
641 
642                 in->upd_opt++;
643             }
644 
645             gdst->ts = gsrc->ts;
646         }
647 
648         in->upd_group++;
649         in->upd_opt = 0;
650     }
651 
652     in->upd_group = -1;
653 }
654 
cache_check_update(struct m_config_cache * cache)655 static bool cache_check_update(struct m_config_cache *cache)
656 {
657     struct config_cache *in = cache->internal;
658     struct m_config_shadow *shadow = in->shadow;
659 
660     // Using atomics and checking outside of the lock - it's unknown whether
661     // this makes it faster or slower. Just cargo culting it.
662     uint64_t new_ts = atomic_load(&shadow->ts);
663     if (in->ts >= new_ts)
664         return false;
665 
666     in->ts = new_ts;
667     in->upd_group = in->data->group_index;
668     in->upd_opt = 0;
669     return true;
670 }
671 
m_config_cache_update(struct m_config_cache * cache)672 bool m_config_cache_update(struct m_config_cache *cache)
673 {
674     struct config_cache *in = cache->internal;
675     struct m_config_shadow *shadow = in->shadow;
676 
677     if (!cache_check_update(cache))
678         return false;
679 
680     pthread_mutex_lock(&shadow->lock);
681     bool res = false;
682     while (1) {
683         void *p;
684         update_next_option(cache, &p);
685         if (!p)
686             break;
687         res = true;
688     }
689     pthread_mutex_unlock(&shadow->lock);
690     return res;
691 }
692 
m_config_cache_get_next_changed(struct m_config_cache * cache,void ** opt)693 bool m_config_cache_get_next_changed(struct m_config_cache *cache, void **opt)
694 {
695     struct config_cache *in = cache->internal;
696     struct m_config_shadow *shadow = in->shadow;
697 
698     *opt = NULL;
699     if (!cache_check_update(cache) && in->upd_group < 0)
700         return false;
701 
702     pthread_mutex_lock(&shadow->lock);
703     update_next_option(cache, opt);
704     pthread_mutex_unlock(&shadow->lock);
705     return !!*opt;
706 }
707 
find_opt(struct m_config_shadow * shadow,struct m_config_data * data,void * ptr,int * group_idx,int * opt_idx)708 static void find_opt(struct m_config_shadow *shadow, struct m_config_data *data,
709                      void *ptr, int *group_idx, int *opt_idx)
710 {
711     *group_idx = -1;
712     *opt_idx = -1;
713 
714     for (int n = data->group_index; n < data->group_index + data->num_gdata; n++)
715     {
716         struct m_group_data *gd = m_config_gdata(data, n);
717         struct m_config_group *g = &shadow->groups[n];
718         const struct m_option *opts = g->group->opts;
719 
720         for (int i = 0; opts && opts[i].name; i++) {
721             const struct m_option *opt = &opts[i];
722 
723             if (opt->offset >= 0 && opt->type->size &&
724                 ptr == gd->udata + opt->offset)
725             {
726                 *group_idx = n;
727                 *opt_idx = i;
728                 return;
729             }
730         }
731     }
732 }
733 
m_config_cache_write_opt(struct m_config_cache * cache,void * ptr)734 bool m_config_cache_write_opt(struct m_config_cache *cache, void *ptr)
735 {
736     struct config_cache *in = cache->internal;
737     struct m_config_shadow *shadow = in->shadow;
738 
739     int group_idx = -1;
740     int opt_idx = -1;
741     find_opt(shadow, in->data, ptr, &group_idx, &opt_idx);
742 
743     // ptr was not in cache->opts, or no option declaration matching it.
744     assert(group_idx >= 0);
745 
746     struct m_config_group *g = &shadow->groups[group_idx];
747     const struct m_option *opt = &g->group->opts[opt_idx];
748 
749     pthread_mutex_lock(&shadow->lock);
750 
751     struct m_group_data *gdst = m_config_gdata(in->data, group_idx);
752     struct m_group_data *gsrc = m_config_gdata(in->src, group_idx);
753     assert(gdst && gsrc);
754 
755     bool changed = !m_option_equal(opt, gsrc->udata + opt->offset, ptr);
756     if (changed) {
757         m_option_copy(opt, gsrc->udata + opt->offset, ptr);
758 
759         gsrc->ts = atomic_fetch_add(&shadow->ts, 1) + 1;
760 
761         for (int n = 0; n < shadow->num_listeners; n++) {
762             struct config_cache *listener = shadow->listeners[n];
763             if (listener->wakeup_cb && m_config_gdata(listener->data, group_idx))
764                 listener->wakeup_cb(listener->wakeup_cb_ctx);
765         }
766     }
767 
768     pthread_mutex_unlock(&shadow->lock);
769 
770     return changed;
771 }
772 
m_config_cache_set_wakeup_cb(struct m_config_cache * cache,void (* cb)(void * ctx),void * cb_ctx)773 void m_config_cache_set_wakeup_cb(struct m_config_cache *cache,
774                                   void (*cb)(void *ctx), void *cb_ctx)
775 {
776     struct config_cache *in = cache->internal;
777     struct m_config_shadow *shadow = in->shadow;
778 
779     pthread_mutex_lock(&shadow->lock);
780     if (in->in_list) {
781         for (int n = 0; n < shadow->num_listeners; n++) {
782             if (shadow->listeners[n] == in) {
783                 MP_TARRAY_REMOVE_AT(shadow->listeners, shadow->num_listeners, n);
784                 break;
785             }
786         }
787         for (int n = 0; n < shadow->num_listeners; n++)
788             assert(shadow->listeners[n] != in); // only 1 wakeup_cb per cache
789         // (The deinitialization path relies on this to free all memory.)
790         if (!shadow->num_listeners) {
791             talloc_free(shadow->listeners);
792             shadow->listeners = NULL;
793         }
794     }
795     if (cb) {
796         MP_TARRAY_APPEND(NULL, shadow->listeners, shadow->num_listeners, in);
797         in->in_list = true;
798         in->wakeup_cb = cb;
799         in->wakeup_cb_ctx = cb_ctx;
800     }
801     pthread_mutex_unlock(&shadow->lock);
802 }
803 
dispatch_notify(void * p)804 static void dispatch_notify(void *p)
805 {
806     struct config_cache *in = p;
807 
808     assert(in->wakeup_dispatch_queue);
809     mp_dispatch_enqueue_notify(in->wakeup_dispatch_queue,
810                                in->wakeup_dispatch_cb,
811                                in->wakeup_dispatch_cb_ctx);
812 }
813 
m_config_cache_set_dispatch_change_cb(struct m_config_cache * cache,struct mp_dispatch_queue * dispatch,void (* cb)(void * ctx),void * cb_ctx)814 void m_config_cache_set_dispatch_change_cb(struct m_config_cache *cache,
815                                            struct mp_dispatch_queue *dispatch,
816                                            void (*cb)(void *ctx), void *cb_ctx)
817 {
818     struct config_cache *in = cache->internal;
819 
820     // Removing the old one is tricky. First make sure no new notifications will
821     // come.
822     m_config_cache_set_wakeup_cb(cache, NULL, NULL);
823     // Remove any pending notifications (assume we're on the same thread as
824     // any potential mp_dispatch_queue_process() callers).
825     if (in->wakeup_dispatch_queue) {
826         mp_dispatch_cancel_fn(in->wakeup_dispatch_queue,
827                               in->wakeup_dispatch_cb,
828                               in->wakeup_dispatch_cb_ctx);
829     }
830 
831     in->wakeup_dispatch_queue = NULL;
832     in->wakeup_dispatch_cb = NULL;
833     in->wakeup_dispatch_cb_ctx = NULL;
834 
835     if (cb) {
836         in->wakeup_dispatch_queue = dispatch;
837         in->wakeup_dispatch_cb = cb;
838         in->wakeup_dispatch_cb_ctx = cb_ctx;
839         m_config_cache_set_wakeup_cb(cache, dispatch_notify, in);
840     }
841 }
842 
mp_get_config_group(void * ta_parent,struct mpv_global * global,const struct m_sub_options * group)843 void *mp_get_config_group(void *ta_parent, struct mpv_global *global,
844                           const struct m_sub_options *group)
845 {
846     struct m_config_cache *cache = m_config_cache_alloc(NULL, global, group);
847     // Make talloc_free(cache->opts) free the entire cache.
848     ta_set_parent(cache->opts, ta_parent);
849     ta_set_parent(cache, cache->opts);
850     return cache->opts;
851 }
852 
mp_read_option_raw(struct mpv_global * global,const char * name,const struct m_option_type * type,void * dst)853 void mp_read_option_raw(struct mpv_global *global, const char *name,
854                         const struct m_option_type *type, void *dst)
855 {
856     struct m_config_shadow *shadow = global->config;
857 
858     int32_t optid = -1;
859     while (m_config_shadow_get_next_opt(shadow, &optid)) {
860         char buf[M_CONFIG_MAX_OPT_NAME_LEN];
861         const char *opt_name =
862             m_config_shadow_get_opt_name(shadow, optid, buf, sizeof(buf));
863 
864         if (strcmp(name, opt_name) == 0) {
865             const struct m_option *opt = m_config_shadow_get_opt(shadow, optid);
866 
867             int group_index, opt_index;
868             get_opt_from_id(shadow, optid, &group_index, &opt_index);
869 
870             struct m_group_data *gdata = m_config_gdata(shadow->data, group_index);
871             assert(gdata);
872 
873             assert(opt->offset >= 0);
874             assert(opt->type == type);
875 
876             memset(dst, 0, opt->type->size);
877             m_option_copy(opt, dst, gdata->udata + opt->offset);
878             return;
879         }
880     }
881 
882     assert(0); // not found
883 }
884 
find_group(struct mpv_global * global,const struct m_option * cfg)885 static const struct m_config_group *find_group(struct mpv_global *global,
886                                                const struct m_option *cfg)
887 {
888     struct m_config_shadow *shadow = global->config;
889 
890     for (int n = 0; n < shadow->num_groups; n++) {
891         if (shadow->groups[n].group->opts == cfg)
892             return &shadow->groups[n];
893     }
894 
895     return NULL;
896 }
897 
m_config_group_from_desc(void * ta_parent,struct mp_log * log,struct mpv_global * global,struct m_obj_desc * desc,const char * name)898 void *m_config_group_from_desc(void *ta_parent, struct mp_log *log,
899         struct mpv_global *global, struct m_obj_desc *desc, const char *name)
900 {
901     const struct m_config_group *group = find_group(global, desc->options);
902     if (group) {
903         return mp_get_config_group(ta_parent, global, group->group);
904     } else {
905         void *d = talloc_zero_size(ta_parent, desc->priv_size);
906         if (desc->priv_defaults)
907             memcpy(d, desc->priv_defaults, desc->priv_size);
908         return d;
909     }
910 }
911