Coverage Report

Created: 2026-11-30 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/mpv/options/m_property.c
Line
Count
Source
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
/// \file
19
/// \ingroup Properties
20
21
#include <stdlib.h>
22
#include <stdio.h>
23
#include <string.h>
24
#include <inttypes.h>
25
#include <assert.h>
26
27
#include <libavutil/common.h>
28
29
#include "mpv/client.h"
30
31
#include "mpv_talloc.h"
32
#include "m_option.h"
33
#include "m_property.h"
34
#include "common/msg.h"
35
#include "common/common.h"
36
#include "player/client.h"
37
38
static int compare_prop_name(const void *pa, const void *pb)
39
124M
{
40
124M
    const struct m_property *a = pa, *b = pb;
41
124M
    return strcmp(a->name, b->name);
42
124M
}
43
44
void m_property_list_sort(struct m_property_list *list)
45
16.4k
{
46
16.4k
    qsort(list->props, list->num, sizeof(list->props[0]), compare_prop_name);
47
16.4k
}
48
49
static int search_prop_name(const void *pkey, const void *pelem)
50
73.0k
{
51
73.0k
    const struct m_property *prop = pelem;
52
73.0k
    return bstrcmp0(*(const bstr *)pkey, prop->name);
53
73.0k
}
54
55
static struct m_property *find_prop(const struct m_property_list *list,
56
                                    bstr name)
57
7.88k
{
58
7.88k
    return bsearch(&name, list->props, list->num, sizeof(list->props[0]),
59
7.88k
                   search_prop_name);
60
7.88k
}
61
62
struct m_property *m_property_list_find(const struct m_property_list *list,
63
                                        const char *name)
64
0
{
65
0
    return find_prop(list, bstr0(name));
66
0
}
67
68
// Find the property for name, which may have the form "base/key". *key is set
69
// to the part after the slash, or to NULL if there is none.
70
static struct m_property *resolve_name(const struct m_property_list *list,
71
                                       const char *name, const char **key)
72
7.88k
{
73
7.88k
    *key = NULL;
74
7.88k
    const char *sep = strchr(name, '/');
75
7.88k
    if (sep && sep[1]) {
76
2.17k
        *key = sep + 1;
77
2.17k
        return find_prop(list, bstr_splice(bstr0(name), 0, sep - name));
78
2.17k
    }
79
5.71k
    return find_prop(list, bstr0(name));
80
7.88k
}
81
82
static int do_action(struct m_property *prop, const char *key, int action,
83
                     void *arg, void *ctx)
84
17.8k
{
85
17.8k
    if (key) {
86
3.52k
        struct m_property_action_arg ka = {
87
3.52k
            .key = key,
88
3.52k
            .action = action,
89
3.52k
            .arg = arg,
90
3.52k
        };
91
3.52k
        return prop->call(ctx, prop, M_PROPERTY_KEY_ACTION, &ka);
92
3.52k
    }
93
14.3k
    return prop->call(ctx, prop, action, arg);
94
17.8k
}
95
96
static int m_property_multiply(struct mp_log *log, struct m_property *prop,
97
                               const char *key, double f, void *ctx,
98
                               struct m_option *opt)
99
0
{
100
0
    union m_option_value val = m_option_value_default;
101
0
    int r;
102
103
0
    mp_require(log);
104
0
    if (!opt->type->multiply)
105
0
        return M_PROPERTY_NOT_IMPLEMENTED;
106
107
0
    r = do_action(prop, key, M_PROPERTY_GET, &val, ctx);
108
0
    if (r != M_PROPERTY_OK)
109
0
        return r;
110
0
    opt->type->multiply(opt, &val, f);
111
0
    r = do_action(prop, key, M_PROPERTY_SET, &val, ctx);
112
0
    m_option_free(opt, &val);
113
0
    return r;
114
0
}
115
116
static int m_property_switch(struct mp_log *log, struct m_property *prop,
117
                             const char *key, void *arg, void *ctx,
118
                             struct m_option *opt)
119
0
{
120
0
    union m_option_value val = m_option_value_default;
121
0
    int r;
122
123
0
    mp_require(log);
124
0
    struct m_property_switch_arg *sarg = arg;
125
0
    if ((r = do_action(prop, key, M_PROPERTY_SWITCH, arg, ctx)) !=
126
0
        M_PROPERTY_NOT_IMPLEMENTED)
127
0
        return r;
128
    // Fallback to m_option
129
0
    if (!opt->type->add)
130
0
        return M_PROPERTY_NOT_IMPLEMENTED;
131
0
    if ((r = do_action(prop, key, M_PROPERTY_GET, &val, ctx)) <= 0)
132
0
        return r;
133
0
    opt->type->add(opt, &val, sarg->inc, sarg->wrap);
134
0
    r = do_action(prop, key, M_PROPERTY_SET, &val, ctx);
135
0
    m_option_free(opt, &val);
136
0
    return r;
137
0
}
138
139
// (as a hack, log can be NULL on read-only paths)
140
int m_property_do(struct mp_log *log, const struct m_property_list *prop_list,
141
                  const char *name, int action, void *arg, void *ctx)
142
7.88k
{
143
7.88k
    union m_option_value val = m_option_value_default;
144
7.88k
    int r;
145
146
7.88k
    const char *key;
147
7.88k
    struct m_property *prop = resolve_name(prop_list, name, &key);
148
7.88k
    if (!prop)
149
693
        return M_PROPERTY_UNKNOWN;
150
151
7.19k
    struct m_option opt = {0};
152
7.19k
    bool get_opt_required[M_PROPERTY_MAX_ACTION] = {
153
7.19k
        [M_PROPERTY_FIXED_LEN_PRINT] = true, [M_PROPERTY_PRINT] = true,
154
7.19k
        [M_PROPERTY_PRINT_UNCUT] = true, [M_PROPERTY_GET_STRING] = true,
155
7.19k
        [M_PROPERTY_MULTIPLY] = true, [M_PROPERTY_SWITCH] = true,
156
7.19k
        [M_PROPERTY_GET_NODE] = true, [M_PROPERTY_SET_NODE] = true,
157
7.19k
    };
158
7.19k
    if (get_opt_required[action]) {
159
6.50k
        r = do_action(prop, key, M_PROPERTY_GET_TYPE, &opt, ctx);
160
6.50k
        if (r <= 0)
161
2.30k
            return r;
162
4.20k
        mp_assert(opt.type);
163
        // Make sure dynamic range from properties with M_PROPERTY_GET_CONSTRICTED_TYPE is applied
164
4.20k
        struct m_option copt = {0};
165
4.20k
        r = do_action(prop, key, M_PROPERTY_GET_CONSTRICTED_TYPE, &copt, ctx);
166
4.20k
        if (r == M_PROPERTY_OK) {
167
383
            opt.min = copt.min;
168
383
            opt.max = copt.max;
169
383
        }
170
4.20k
    }
171
172
4.89k
    switch (action) {
173
0
    case M_PROPERTY_PRINT_UNCUT:
174
808
    case M_PROPERTY_FIXED_LEN_PRINT:
175
3.66k
    case M_PROPERTY_PRINT: {
176
3.66k
        if ((r = do_action(prop, key, action, arg, ctx)) >= 0)
177
1.64k
            return r;
178
2.02k
        if (action == M_PROPERTY_PRINT_UNCUT &&
179
0
            (r = do_action(prop, key, M_PROPERTY_PRINT, arg, ctx)) >= 0)
180
0
            return r;
181
        // Fallback to m_option
182
2.02k
        if ((r = do_action(prop, key, M_PROPERTY_GET, &val, ctx)) <= 0)
183
131
            return r;
184
1.89k
        char *str = m_option_pretty_print(&opt, &val, action == M_PROPERTY_FIXED_LEN_PRINT);
185
1.89k
        m_option_free(&opt, &val);
186
1.89k
        *(char **)arg = str;
187
1.89k
        return str != NULL;
188
2.02k
    }
189
539
    case M_PROPERTY_GET_STRING: {
190
539
        if ((r = do_action(prop, key, M_PROPERTY_GET, &val, ctx)) <= 0)
191
55
            return r;
192
484
        char *str = m_option_print(&opt, &val);
193
484
        m_option_free(&opt, &val);
194
484
        *(char **)arg = str;
195
484
        return str != NULL;
196
539
    }
197
0
    case M_PROPERTY_SET_STRING: {
198
0
        struct mpv_node node = { .format = MPV_FORMAT_STRING, .u.string = arg };
199
0
        return m_property_do(log, prop_list, name, M_PROPERTY_SET_NODE, &node, ctx);
200
539
    }
201
0
    case M_PROPERTY_MULTIPLY: {
202
0
        return m_property_multiply(log, prop, key, *(double *)arg, ctx, &opt);
203
539
    }
204
0
    case M_PROPERTY_SWITCH: {
205
0
        return m_property_switch(log, prop, key, arg, ctx, &opt);
206
539
    }
207
271
    case M_PROPERTY_GET_CONSTRICTED_TYPE: {
208
271
        r = do_action(prop, key, action, arg, ctx);
209
271
        if (r >= 0 || r == M_PROPERTY_UNAVAILABLE)
210
0
            return r;
211
271
        if ((r = do_action(prop, key, M_PROPERTY_GET_TYPE, arg, ctx)) >= 0)
212
271
            return r;
213
0
        return M_PROPERTY_NOT_IMPLEMENTED;
214
271
    }
215
0
    case M_PROPERTY_SET: {
216
0
        return do_action(prop, key, M_PROPERTY_SET, arg, ctx);
217
271
    }
218
0
    case M_PROPERTY_GET_NODE: {
219
0
        if ((r = do_action(prop, key, M_PROPERTY_GET_NODE, arg, ctx)) !=
220
0
            M_PROPERTY_NOT_IMPLEMENTED)
221
0
            return r;
222
0
        if ((r = do_action(prop, key, M_PROPERTY_GET, &val, ctx)) <= 0)
223
0
            return r;
224
0
        struct mpv_node *node = arg;
225
0
        int err = m_option_get_node(&opt, NULL, node, &val);
226
0
        if (err == M_OPT_UNKNOWN) {
227
0
            r = M_PROPERTY_NOT_IMPLEMENTED;
228
0
        } else if (err < 0) {
229
0
            r = M_PROPERTY_INVALID_FORMAT;
230
0
        } else {
231
0
            r = M_PROPERTY_OK;
232
0
        }
233
0
        m_option_free(&opt, &val);
234
0
        return r;
235
0
    }
236
0
    case M_PROPERTY_SET_NODE: {
237
0
        if (!log)
238
0
            return M_PROPERTY_ERROR;
239
0
        if ((r = do_action(prop, key, M_PROPERTY_SET_NODE, arg, ctx)) !=
240
0
            M_PROPERTY_NOT_IMPLEMENTED)
241
0
            return r;
242
0
        int err = m_option_set_node_or_string(log, &opt, bstr0(name), &val, arg);
243
0
        if (err == M_OPT_UNKNOWN) {
244
0
            r = M_PROPERTY_NOT_IMPLEMENTED;
245
0
        } else if (err < 0) {
246
0
            r = M_PROPERTY_INVALID_FORMAT;
247
0
        } else {
248
0
            r = do_action(prop, key, M_PROPERTY_SET, &val, ctx);
249
0
        }
250
0
        m_option_free(&opt, &val);
251
0
        return r;
252
0
    }
253
420
    default:
254
420
        return do_action(prop, key, action, arg, ctx);
255
4.89k
    }
256
4.89k
}
257
258
bool m_property_split_path(const char *path, bstr *prefix, const char **rem)
259
391
{
260
391
    const char *next = strchr(path, '/');
261
391
    if (next) {
262
74
        *prefix = bstr_splice(bstr0(path), 0, next - path);
263
74
        *rem = next + 1;
264
74
        return true;
265
317
    } else {
266
317
        *prefix = bstr0(path);
267
317
        *rem = "";
268
317
        return false;
269
317
    }
270
391
}
271
272
// If *action is M_PROPERTY_KEY_ACTION, but the associated path is "", then
273
// make this into a top-level action.
274
static void m_property_unkey(int *action, void **arg)
275
143k
{
276
143k
    if (*action == M_PROPERTY_KEY_ACTION) {
277
3.74k
        struct m_property_action_arg *ka = *arg;
278
3.74k
        if (!ka->key[0]) {
279
342
            *action = ka->action;
280
342
            *arg = ka->arg;
281
342
        }
282
3.74k
    }
283
143k
}
284
285
static int m_property_do_bstr(const struct m_property_list *prop_list, bstr name,
286
                              int action, void *arg, void *ctx)
287
6.97k
{
288
6.97k
    char *name0 = bstrdup0(NULL, name);
289
6.97k
    int ret = m_property_do(NULL, prop_list, name0, action, arg, ctx);
290
6.97k
    talloc_free(name0);
291
6.97k
    return ret;
292
6.97k
}
293
294
static void append_str(char **s, int *len, bstr append)
295
6.32k
{
296
6.32k
    MP_TARRAY_GROW(NULL, *s, *len + append.len);
297
6.32k
    if (append.len)
298
5.52k
        memcpy(*s + *len, append.start, append.len);
299
6.32k
    *len = *len + append.len;
300
6.32k
}
301
302
static int expand_property(const struct m_property_list *prop_list, char **ret,
303
                           int *ret_len, bstr prop, bool silent_error, void *ctx,
304
                           int print_action)
305
6.97k
{
306
6.97k
    bool cond_yes = bstr_eatstart0(&prop, "?");
307
6.97k
    bool cond_no = !cond_yes && bstr_eatstart0(&prop, "!");
308
6.97k
    bool test = cond_yes || cond_no;
309
6.97k
    bool raw = bstr_eatstart0(&prop, "=");
310
6.97k
    bool fixed_len = !raw && bstr_eatstart0(&prop, ">");
311
6.97k
    bstr comp_with = {0};
312
6.97k
    bool comp = test && bstr_split_tok(prop, "==", &prop, &comp_with);
313
6.97k
    if (test && !comp)
314
483
        raw = true;
315
6.97k
    int method = raw ? M_PROPERTY_GET_STRING : print_action;
316
6.97k
    method = fixed_len ? M_PROPERTY_FIXED_LEN_PRINT : method;
317
318
6.97k
    char *s = NULL;
319
6.97k
    int r = m_property_do_bstr(prop_list, prop, method, &s, ctx);
320
6.97k
    bool skip;
321
6.97k
    if (comp) {
322
194
        skip = ((s && bstr_equals0(comp_with, s)) != cond_yes);
323
6.78k
    } else if (test) {
324
483
        skip = (!!s != cond_yes);
325
6.29k
    } else {
326
6.29k
        skip = !!s;
327
6.29k
        char *append = s;
328
6.29k
        if (!s && !silent_error && !raw)
329
2.38k
            append = (r == M_PROPERTY_UNAVAILABLE) ? "(unavailable)" : "(error)";
330
6.29k
        append_str(ret, ret_len, bstr0(append));
331
6.29k
    }
332
6.97k
    talloc_free(s);
333
6.97k
    return skip;
334
6.97k
}
335
336
char *m_properties_expand_string(const struct m_property_list *prop_list,
337
                                 const char *str0, void *ctx, int print_action)
338
3.28k
{
339
3.28k
    char *ret = NULL;
340
3.28k
    int ret_len = 0;
341
3.28k
    bool skip = false;
342
3.28k
    int level = 0, skip_level = 0;
343
3.28k
    bstr str = bstr0(str0);
344
3.28k
    int last_brace = bstrrchr(str, '}');
345
3.28k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
346
3.28k
    int n = 0;
347
3.28k
#endif
348
349
61.6k
    while (str.len) {
350
58.4k
        if (level > 0 && bstr_eatstart0(&str, "}")) {
351
6.60k
            if (skip && level <= skip_level)
352
3.59k
                skip = false;
353
6.60k
            level--;
354
51.8k
        } else if (bstr_startswith0(str, "${") && str.start - (unsigned char *)str0 < last_brace) {
355
7.03k
            str = bstr_cut(str, 2);
356
7.03k
            level++;
357
358
            // Assume ":" and "}" can't be part of the property name
359
            // => if ":" comes before "}", it must be for the fallback
360
7.03k
            int term_pos = bstrcspn(str, ":}");
361
7.03k
            bstr name = bstr_splice(str, 0, term_pos < 0 ? str.len : term_pos);
362
7.03k
            str = bstr_cut(str, term_pos);
363
7.03k
            bool have_fallback = bstr_eatstart0(&str, ":");
364
365
7.03k
#ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
366
7.03k
            if (n++ > 10)
367
13
                break;
368
7.01k
#endif
369
370
7.01k
            if (!skip) {
371
6.97k
                skip = expand_property(prop_list, &ret, &ret_len, name,
372
6.97k
                                       have_fallback, ctx, print_action);
373
6.97k
                if (skip)
374
3.60k
                    skip_level = level;
375
6.97k
            }
376
44.7k
        } else if (level == 0 && bstr_eatstart0(&str, "$>")) {
377
29
            append_str(&ret, &ret_len, str);
378
29
            break;
379
44.7k
        } else {
380
44.7k
            char c;
381
382
            // Other combinations, e.g. "$x", are added verbatim
383
44.7k
            if (bstr_eatstart0(&str, "$$")) {
384
247
                c = '$';
385
44.4k
            } else if (bstr_eatstart0(&str, "$}")) {
386
252
                c = '}';
387
44.2k
            } else {
388
44.2k
                c = str.start[0];
389
44.2k
                str = bstr_cut(str, 1);
390
44.2k
            }
391
392
44.7k
            if (!skip)
393
42.8k
                MP_TARRAY_APPEND(NULL, ret, ret_len, c);
394
44.7k
        }
395
58.4k
    }
396
397
3.28k
    MP_TARRAY_APPEND(NULL, ret, ret_len, '\0');
398
3.28k
    return ret;
399
3.28k
}
400
401
void m_properties_print_help_list(struct mp_log *log,
402
                                  const struct m_property_list *list)
403
4
{
404
4
    mp_info(log, "Name\n\n");
405
3.71k
    for (int i = 0; i < list->num; i++)
406
3.70k
        mp_info(log, " %s\n", list->props[i].name);
407
4
    mp_info(log, "\nTotal: %d properties\n", list->num);
408
4
}
409
410
int m_property_bool_ro(int action, void* arg, bool var)
411
315
{
412
315
    switch (action) {
413
74
    case M_PROPERTY_GET:
414
74
        *(bool *)arg = !!var;
415
74
        return M_PROPERTY_OK;
416
74
    case M_PROPERTY_GET_TYPE:
417
74
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_BOOL};
418
74
        return M_PROPERTY_OK;
419
315
    }
420
167
    return M_PROPERTY_NOT_IMPLEMENTED;
421
315
}
422
423
int m_property_int_ro(int action, void *arg, int var)
424
213
{
425
213
    switch (action) {
426
53
    case M_PROPERTY_GET:
427
53
        *(int *)arg = var;
428
53
        return M_PROPERTY_OK;
429
53
    case M_PROPERTY_GET_TYPE:
430
53
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_INT};
431
53
        return M_PROPERTY_OK;
432
213
    }
433
107
    return M_PROPERTY_NOT_IMPLEMENTED;
434
213
}
435
436
int m_property_int64_ro(int action, void* arg, int64_t var)
437
147
{
438
147
    switch (action) {
439
36
    case M_PROPERTY_GET:
440
36
        *(int64_t *)arg = var;
441
36
        return M_PROPERTY_OK;
442
36
    case M_PROPERTY_GET_TYPE:
443
36
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_INT64};
444
36
        return M_PROPERTY_OK;
445
147
    }
446
75
    return M_PROPERTY_NOT_IMPLEMENTED;
447
147
}
448
449
int m_property_float_ro(int action, void *arg, float var)
450
0
{
451
0
    switch (action) {
452
0
    case M_PROPERTY_GET:
453
0
        *(float *)arg = var;
454
0
        return M_PROPERTY_OK;
455
0
    case M_PROPERTY_GET_TYPE:
456
0
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_FLOAT};
457
0
        return M_PROPERTY_OK;
458
0
    }
459
0
    return M_PROPERTY_NOT_IMPLEMENTED;
460
0
}
461
462
int m_property_double_ro(int action, void *arg, double var)
463
0
{
464
0
    switch (action) {
465
0
    case M_PROPERTY_GET:
466
0
        *(double *)arg = var;
467
0
        return M_PROPERTY_OK;
468
0
    case M_PROPERTY_GET_TYPE:
469
0
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_DOUBLE};
470
0
        return M_PROPERTY_OK;
471
0
    }
472
0
    return M_PROPERTY_NOT_IMPLEMENTED;
473
0
}
474
475
int m_property_strdup_ro(int action, void* arg, const char *var)
476
542
{
477
542
    if (!var)
478
75
        return M_PROPERTY_UNAVAILABLE;
479
467
    switch (action) {
480
119
    case M_PROPERTY_GET:
481
119
        *(char **)arg = talloc_strdup(NULL, var);
482
119
        return M_PROPERTY_OK;
483
119
    case M_PROPERTY_GET_TYPE:
484
119
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_STRING};
485
119
        return M_PROPERTY_OK;
486
467
    }
487
229
    return M_PROPERTY_NOT_IMPLEMENTED;
488
467
}
489
490
int m_property_read_sub_validate(void *ctx, struct m_property *prop,
491
                                 int action, void *arg)
492
2.94k
{
493
2.94k
    m_property_unkey(&action, &arg);
494
2.94k
    switch (action) {
495
581
    case M_PROPERTY_GET_TYPE:
496
581
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE};
497
581
        return M_PROPERTY_OK;
498
326
    case M_PROPERTY_GET:
499
597
    case M_PROPERTY_PRINT:
500
1.54k
    case M_PROPERTY_KEY_ACTION:
501
1.54k
        return M_PROPERTY_VALID;
502
825
    default:
503
825
        return M_PROPERTY_NOT_IMPLEMENTED;
504
2.94k
    };
505
0
}
506
507
// This allows you to make a list of values (like from a struct) available
508
// as a number of sub-properties. The property list is set up with the current
509
// property values on the stack before calling this function.
510
// This does not support write access.
511
int m_property_read_sub(const struct m_sub_property *props, int action, void *arg)
512
136k
{
513
136k
    m_property_unkey(&action, &arg);
514
136k
    switch (action) {
515
285
    case M_PROPERTY_GET_TYPE:
516
285
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE};
517
285
        return M_PROPERTY_OK;
518
152
    case M_PROPERTY_GET:
519
63.8k
    case M_PROPERTY_GET_NODE: {
520
63.8k
        struct mpv_node node;
521
63.8k
        node.format = MPV_FORMAT_NODE_MAP;
522
63.8k
        node.u.list = talloc_zero(NULL, mpv_node_list);
523
63.8k
        mpv_node_list *list = node.u.list;
524
283k
        for (int n = 0; props && props[n].name; n++) {
525
219k
            const struct m_sub_property *prop = &props[n];
526
219k
            if (prop->unavailable)
527
36.5k
                continue;
528
182k
            MP_TARRAY_GROW(list, list->values, list->num);
529
182k
            MP_TARRAY_GROW(list, list->keys, list->num);
530
182k
            mpv_node *val = &list->values[list->num];
531
182k
            if (m_option_get_node(&prop->type, list, val, (void*)&prop->value) < 0)
532
0
            {
533
0
                char *s = m_option_print(&prop->type, &prop->value);
534
0
                val->format = MPV_FORMAT_STRING;
535
0
                val->u.string = talloc_steal(list, s);
536
0
            }
537
182k
            list->keys[list->num] = (char *)prop->name;
538
182k
            list->num++;
539
182k
        }
540
63.8k
        *(struct mpv_node *)arg = node;
541
63.8k
        return M_PROPERTY_OK;
542
152
    }
543
71.6k
    case M_PROPERTY_PRINT: {
544
        // Output "something" - what it really should return is not yet decided.
545
        // It should probably be something that is easy to consume by slave
546
        // mode clients. (M_PROPERTY_PRINT on the other hand can return this
547
        // as human readable version just fine).
548
71.6k
        char *res = NULL;
549
286k
        for (int n = 0; props && props[n].name; n++) {
550
215k
            const struct m_sub_property *prop = &props[n];
551
215k
            if (prop->unavailable)
552
241
                continue;
553
214k
            char *s = m_option_print(&prop->type, &prop->value);
554
214k
            ta_xasprintf_append(&res, "%s=%s\n", prop->name, s);
555
214k
            talloc_free(s);
556
214k
        }
557
71.6k
        *(char **)arg = res;
558
71.6k
        return M_PROPERTY_OK;
559
152
    }
560
751
    case M_PROPERTY_KEY_ACTION: {
561
751
        struct m_property_action_arg *ka = arg;
562
751
        const struct m_sub_property *prop = NULL;
563
2.51k
        for (int n = 0; props && props[n].name; n++) {
564
2.38k
            if (strcmp(props[n].name, ka->key) == 0) {
565
621
                prop = &props[n];
566
621
                break;
567
621
            }
568
2.38k
        }
569
751
        if (!prop)
570
130
            return M_PROPERTY_UNKNOWN;
571
621
        if (prop->unavailable)
572
153
            return M_PROPERTY_UNAVAILABLE;
573
468
        switch (ka->action) {
574
73
        case M_PROPERTY_GET: {
575
73
            memset(ka->arg, 0, prop->type.type->size);
576
73
            m_option_copy(&prop->type, ka->arg, &prop->value);
577
73
            return M_PROPERTY_OK;
578
0
        }
579
195
        case M_PROPERTY_GET_TYPE:
580
195
            *(struct m_option *)ka->arg = prop->type;
581
195
            return M_PROPERTY_OK;
582
468
        }
583
468
    }
584
136k
    }
585
516
    return M_PROPERTY_NOT_IMPLEMENTED;
586
136k
}
587
588
589
// Make a list of items available as indexed sub-properties. E.g. you can access
590
// item 0 as "property/0", item 1 as "property/1", etc., where each of these
591
// properties is redirected to the get_item(0, ...), get_item(1, ...), callback.
592
// Additionally, the number of entries is made available as "property/count".
593
// action, arg: property access.
594
// count: number of items.
595
// get_item: callback to access a single item.
596
// ctx: userdata passed to get_item.
597
int m_property_read_list(int action, void *arg, int count,
598
                         m_get_item_cb get_item, void *ctx)
599
3.33k
{
600
3.33k
    m_property_unkey(&action, &arg);
601
3.33k
    switch (action) {
602
442
    case M_PROPERTY_GET_TYPE:
603
442
        *(struct m_option *)arg = (struct m_option){.type = CONF_TYPE_NODE};
604
442
        return M_PROPERTY_OK;
605
389
    case M_PROPERTY_GET:
606
389
    case M_PROPERTY_GET_NODE: {
607
389
        struct mpv_node node;
608
389
        node.format = MPV_FORMAT_NODE_ARRAY;
609
389
        node.u.list = talloc_zero(NULL, mpv_node_list);
610
389
        node.u.list->num = count;
611
389
        node.u.list->values = talloc_array(node.u.list, mpv_node, count);
612
64.1k
        for (int n = 0; n < count; n++) {
613
63.7k
            struct mpv_node *sub = &node.u.list->values[n];
614
63.7k
            sub->format = MPV_FORMAT_NONE;
615
63.7k
            int r;
616
63.7k
            r = get_item(n, M_PROPERTY_GET_NODE, sub, ctx);
617
63.7k
            if (r >= 0) {
618
63.7k
                talloc_steal(node.u.list, node_get_alloc(sub));
619
63.7k
            } else if (r == M_PROPERTY_NOT_IMPLEMENTED) {
620
0
                struct m_option opt = {0};
621
0
                r = get_item(n, M_PROPERTY_GET_TYPE, &opt, ctx);
622
0
                if (r != M_PROPERTY_OK)
623
0
                    goto err;
624
0
                union m_option_value val = m_option_value_default;
625
0
                r = get_item(n, M_PROPERTY_GET, &val, ctx);
626
0
                if (r != M_PROPERTY_OK)
627
0
                    goto err;
628
0
                m_option_get_node(&opt, node.u.list, sub, &val);
629
0
                m_option_free(&opt, &val);
630
0
            err: ;
631
0
            }
632
63.7k
        }
633
389
        *(struct mpv_node *)arg = node;
634
389
        return M_PROPERTY_OK;
635
389
    }
636
214
    case M_PROPERTY_PRINT: {
637
        // See m_property_read_sub() remarks.
638
214
        char *res = NULL;
639
71.6k
        for (int n = 0; n < count; n++) {
640
71.3k
            char *s = NULL;
641
71.3k
            int r = get_item(n, M_PROPERTY_PRINT, &s, ctx);
642
71.3k
            if (r != M_PROPERTY_OK) {
643
0
                talloc_free(res);
644
0
                return r;
645
0
            }
646
71.3k
            ta_xasprintf_append(&res, "%d: %s\n", n, s);
647
71.3k
            talloc_free(s);
648
71.3k
        }
649
214
        *(char **)arg = res;
650
214
        return M_PROPERTY_OK;
651
214
    }
652
1.70k
    case M_PROPERTY_KEY_ACTION: {
653
1.70k
        struct m_property_action_arg *ka = arg;
654
1.70k
        if (strcmp(ka->key, "count") == 0) {
655
288
            switch (ka->action) {
656
123
            case M_PROPERTY_GET_TYPE: {
657
123
                struct m_option opt = {.type = CONF_TYPE_INT};
658
123
                *(struct m_option *)ka->arg = opt;
659
123
                return M_PROPERTY_OK;
660
0
            }
661
42
            case M_PROPERTY_GET:
662
42
                *(int *)ka->arg = MPMAX(0, count);
663
42
                return M_PROPERTY_OK;
664
288
            }
665
123
            return M_PROPERTY_NOT_IMPLEMENTED;
666
288
        }
667
        // This is expected of the form "123" or "123/rest"
668
1.41k
        char *end;
669
1.41k
        long int item = strtol(ka->key, &end, 10);
670
        // not a number, trailing characters, etc.
671
1.41k
        if (end == ka->key || (end[0] == '/' && !end[1]))
672
178
            return M_PROPERTY_UNKNOWN;
673
1.24k
        if (item < 0 || item >= count)
674
218
            return M_PROPERTY_UNKNOWN;
675
1.02k
        if (*end) {
676
            // Sub-path
677
539
            struct m_property_action_arg n_ka = *ka;
678
539
            n_ka.key = end + 1;
679
539
            return get_item(item, M_PROPERTY_KEY_ACTION, &n_ka, ctx);
680
539
        } else {
681
            // Direct query
682
483
            return get_item(item, ka->action, ka->arg, ctx);
683
483
        }
684
1.02k
    }
685
3.33k
    }
686
584
    return M_PROPERTY_NOT_IMPLEMENTED;
687
3.33k
}