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 #ifndef MPLAYER_M_OPTION_H
19 #define MPLAYER_M_OPTION_H
20 
21 #include <float.h>
22 #include <string.h>
23 #include <stddef.h>
24 #include <stdbool.h>
25 
26 #include "misc/bstr.h"
27 #include "audio/chmap.h"
28 
29 // m_option allows to parse, print and copy data of various types.
30 
31 typedef struct m_option_type m_option_type_t;
32 typedef struct m_option m_option_t;
33 struct m_config;
34 struct mp_log;
35 struct mpv_node;
36 struct mpv_global;
37 
38 ///////////////////////////// Options types declarations ////////////////////
39 
40 // Simple types
41 extern const m_option_type_t m_option_type_bool;
42 extern const m_option_type_t m_option_type_flag;
43 extern const m_option_type_t m_option_type_dummy_flag;
44 extern const m_option_type_t m_option_type_int;
45 extern const m_option_type_t m_option_type_int64;
46 extern const m_option_type_t m_option_type_byte_size;
47 extern const m_option_type_t m_option_type_float;
48 extern const m_option_type_t m_option_type_double;
49 extern const m_option_type_t m_option_type_string;
50 extern const m_option_type_t m_option_type_string_list;
51 extern const m_option_type_t m_option_type_string_append_list;
52 extern const m_option_type_t m_option_type_keyvalue_list;
53 extern const m_option_type_t m_option_type_time;
54 extern const m_option_type_t m_option_type_rel_time;
55 extern const m_option_type_t m_option_type_choice;
56 extern const m_option_type_t m_option_type_flags;
57 extern const m_option_type_t m_option_type_msglevels;
58 extern const m_option_type_t m_option_type_print_fn;
59 extern const m_option_type_t m_option_type_imgfmt;
60 extern const m_option_type_t m_option_type_fourcc;
61 extern const m_option_type_t m_option_type_afmt;
62 extern const m_option_type_t m_option_type_color;
63 extern const m_option_type_t m_option_type_geometry;
64 extern const m_option_type_t m_option_type_size_box;
65 extern const m_option_type_t m_option_type_channels;
66 extern const m_option_type_t m_option_type_aspect;
67 extern const m_option_type_t m_option_type_obj_settings_list;
68 extern const m_option_type_t m_option_type_node;
69 
70 // Used internally by m_config.c
71 extern const m_option_type_t m_option_type_alias;
72 extern const m_option_type_t m_option_type_cli_alias;
73 extern const m_option_type_t m_option_type_removed;
74 extern const m_option_type_t m_option_type_subconfig;
75 
76 // Callback used by m_option_type_print_fn options.
77 typedef void (*m_opt_print_fn)(struct mp_log *log);
78 
79 enum m_rel_time_type {
80     REL_TIME_NONE,
81     REL_TIME_ABSOLUTE,
82     REL_TIME_RELATIVE,
83     REL_TIME_PERCENT,
84     REL_TIME_CHAPTER,
85 };
86 
87 struct m_rel_time {
88     double pos;
89     enum m_rel_time_type type;
90 };
91 
92 struct m_color {
93     uint8_t r, g, b, a;
94 };
95 
96 struct m_geometry {
97     int x, y, w, h;
98     bool xy_valid : 1, wh_valid : 1;
99     bool w_per : 1, h_per : 1;
100     bool x_sign : 1, y_sign : 1, x_per : 1, y_per : 1;
101     int ws; // workspace; valid if !=0
102 };
103 
104 void m_geometry_apply(int *xpos, int *ypos, int *widw, int *widh,
105                       int scrw, int scrh, struct m_geometry *gm);
106 
107 struct m_channels {
108     bool set : 1;
109     bool auto_safe : 1;
110     struct mp_chmap *chmaps;
111     int num_chmaps;
112 };
113 
114 struct m_obj_desc {
115     // Name which will be used in the option string
116     const char *name;
117     // Will be printed when "help" is passed
118     const char *description;
119     // Size of the private struct
120     int priv_size;
121     // If not NULL, default values for private struct
122     const void *priv_defaults;
123     // Options which refer to members in the private struct
124     const struct m_option *options;
125     // Prefix for each of the above options (none if NULL).
126     const char *options_prefix;
127     // For free use by the implementer of m_obj_list.get_desc
128     const void *p;
129     // Don't list entry with "help"
130     bool hidden;
131     // Callback to print custom help if "vf=entry=help" is passed
132     void (*print_help)(struct mp_log *log);
133     // Set by m_obj_list_find(). If the requested name is an old alias, this
134     // is set to the old name (while the name field uses the new name).
135     const char *replaced_name;
136     // For convenience: these are added as global command-line options.
137     const struct m_sub_options *global_opts;
138 };
139 
140 // Extra definition needed for \ref m_option_type_obj_settings_list options.
141 struct m_obj_list {
142     bool (*get_desc)(struct m_obj_desc *dst, int index);
143     const char *description;
144     // Can be set to a NULL terminated array of aliases
145     const char *aliases[5][2];
146     // Allow a trailing ",", which adds an entry with name=""
147     bool allow_trailer;
148     // Allow unknown entries, for which a dummy entry is inserted, and whose
149     // options are skipped and ignored.
150     bool allow_unknown_entries;
151     // Callback to test whether an unknown entry should be allowed. (This can
152     // be useful if adding them as explicit entries is too much work.)
153     bool (*check_unknown_entry)(const char *name);
154     // Allow syntax for disabling entries.
155     bool allow_disable_entries;
156     // This helps with confusing error messages if unknown flag options are used.
157     bool disallow_positional_parameters;
158     // Each sub-item is backed by global options (for AOs and VOs).
159     bool use_global_options;
160     // Callback to print additional custom help if "vf=help" is passed
161     void (*print_help_list)(struct mp_log *log);
162     // Callback to print help for _unknown_ entries with "vf=entry=help"
163     void (*print_unknown_entry_help)(struct mp_log *log, const char *name);
164 };
165 
166 // Find entry by name
167 bool m_obj_list_find(struct m_obj_desc *dst, const struct m_obj_list *list,
168                      bstr name);
169 
170 // The data type used by \ref m_option_type_obj_settings_list.
171 typedef struct m_obj_settings {
172     // Type of the object.
173     char *name;
174     // Optional user-defined name.
175     char *label;
176     // User enable flag.
177     bool enabled;
178     // NULL terminated array of parameter/value pairs.
179     char **attribs;
180 } m_obj_settings_t;
181 
182 bool m_obj_settings_equal(struct m_obj_settings *a, struct m_obj_settings *b);
183 
184 struct m_opt_choice_alternatives {
185     char *name;
186     int value;
187 };
188 
189 const char *m_opt_choice_str(const struct m_opt_choice_alternatives *choices,
190                              int value);
191 
192 // Validator function signatures. Required to properly type the param value.
193 typedef int (*m_opt_generic_validate_fn)(struct mp_log *log, const m_option_t *opt,
194                                          struct bstr name, void *value);
195 
196 typedef int (*m_opt_string_validate_fn)(struct mp_log *log, const m_option_t *opt,
197                                         struct bstr name, const char **value);
198 typedef int (*m_opt_int_validate_fn)(struct mp_log *log, const m_option_t *opt,
199                                      struct bstr name, const int *value);
200 
201 
202 // m_option.priv points to this if OPT_SUBSTRUCT is used
203 struct m_sub_options {
204     const char *prefix;
205     const struct m_option *opts;
206     size_t size;
207     const void *defaults;
208     // Change flags passed to mp_option_change_callback() if any option that is
209     // directly or indirectly part of this group is changed.
210     int change_flags;
211     // Return further sub-options, for example for optional components. If set,
212     // this is called with increasing index (starting from 0), as long as true
213     // is returned. If true is returned and *sub is set in any of these calls,
214     // they are added as options.
215     bool (*get_sub_options)(int index, const struct m_sub_options **sub);
216 };
217 
218 #define CONF_TYPE_FLAG          (&m_option_type_flag)
219 #define CONF_TYPE_INT           (&m_option_type_int)
220 #define CONF_TYPE_INT64         (&m_option_type_int64)
221 #define CONF_TYPE_FLOAT         (&m_option_type_float)
222 #define CONF_TYPE_DOUBLE        (&m_option_type_double)
223 #define CONF_TYPE_STRING        (&m_option_type_string)
224 #define CONF_TYPE_STRING_LIST   (&m_option_type_string_list)
225 #define CONF_TYPE_IMGFMT        (&m_option_type_imgfmt)
226 #define CONF_TYPE_FOURCC        (&m_option_type_fourcc)
227 #define CONF_TYPE_AFMT          (&m_option_type_afmt)
228 #define CONF_TYPE_OBJ_SETTINGS_LIST (&m_option_type_obj_settings_list)
229 #define CONF_TYPE_TIME          (&m_option_type_time)
230 #define CONF_TYPE_CHOICE        (&m_option_type_choice)
231 #define CONF_TYPE_NODE          (&m_option_type_node)
232 
233 // Possible option values. Code is allowed to access option data without going
234 // through this union. It serves for self-documentation and to get minimal
235 // size/alignment requirements for option values in general.
236 union m_option_value {
237     bool bool_;
238     int flag; // not the C type "bool"!
239     int int_;
240     int64_t int64;
241     float float_;
242     double double_;
243     char *string;
244     char **string_list;
245     char **keyvalue_list;
246     int imgfmt;
247     unsigned int fourcc;
248     int afmt;
249     m_obj_settings_t *obj_settings_list;
250     double time;
251     struct m_rel_time rel_time;
252     struct m_color color;
253     struct m_geometry geometry;
254     struct m_geometry size_box;
255     struct m_channels channels;
256 };
257 
258 ////////////////////////////////////////////////////////////////////////////
259 
260 struct m_option_action {
261     // The name of the suffix, e.g. "add" for a list. If the option is named
262     // "foo", this will be available as "--foo-add". Note that no suffix (i.e.
263     // "--foo" is implicitly always available.
264     const char *name;
265     // One of M_OPT_TYPE*.
266     unsigned int flags;
267 };
268 
269 // Option type description
270 struct m_option_type {
271     const char *name;
272     // Size needed for the data.
273     unsigned int size;
274     // One of M_OPT_TYPE*.
275     unsigned int flags;
276 
277     // Parse the data from a string.
278     /** It is the only required function, all others can be NULL.
279      *  Generally should not be called directly outside of the options module,
280      *  but instead through \ref m_option_parse which calls additional option
281      *  specific callbacks during the process.
282      *
283      *  \param log for outputting parser error or help messages
284      *  \param opt The option that is parsed.
285      *  \param name The full option name.
286      *  \param param The parameter to parse.
287      *         may not be an argument meant for this option
288      *  \param dst Pointer to the memory where the data should be written.
289      *             If NULL the parameter validity should still be checked.
290      *  \return On error a negative value is returned, on success the number
291      *          of arguments consumed. For details see \ref OptionParserReturn.
292      */
293     int (*parse)(struct mp_log *log, const m_option_t *opt,
294                  struct bstr name, struct bstr param, void *dst);
295 
296     // Print back a value in string form.
297     /** \param opt The option to print.
298      *  \param val Pointer to the memory holding the data to be printed.
299      *  \return An allocated string containing the text value or (void*)-1
300      *          on error.
301      */
302     char *(*print)(const m_option_t *opt, const void *val);
303 
304     // Print the value in a human readable form. Unlike print(), it doesn't
305     // necessarily return the exact value, and is generally not parseable with
306     // parse().
307     char *(*pretty_print)(const m_option_t *opt, const void *val);
308 
309     // Copy data between two locations. Deep copy if the data has pointers.
310     // The implementation must free *dst if memory allocation is involved.
311     /** \param opt The option to copy.
312      *  \param dst Pointer to the destination memory.
313      *  \param src Pointer to the source memory.
314      */
315     void (*copy)(const m_option_t *opt, void *dst, const void *src);
316 
317     // Free the data allocated for a save slot.
318     /** This is only needed for dynamic types like strings.
319      *  \param dst Pointer to the data, usually a pointer that should be freed and
320      *             set to NULL.
321      */
322     void (*free)(void *dst);
323 
324     // Add the value add to the value in val. For types that are not numeric,
325     // add gives merely the direction. The wrap parameter determines whether
326     // the value is clipped, or wraps around to the opposite max/min.
327     void (*add)(const m_option_t *opt, void *val, double add, bool wrap);
328 
329     // Multiply the value with the factor f. The callback must clip the result
330     // to the valid value range of the option.
331     void (*multiply)(const m_option_t *opt, void *val, double f);
332 
333     // Set the option value in dst to the contents of src.
334     // (If the option is dynamic, the old value in *dst has to be freed.)
335     // Return values:
336     //  M_OPT_UNKNOWN:      src is in an unknown format
337     //  M_OPT_INVALID:      src is incorrectly formatted
338     //  >= 0:               success
339     //  other error code:   some other error, essentially M_OPT_INVALID refined
340     int (*set)(const m_option_t *opt, void *dst, struct mpv_node *src);
341 
342     // Copy the option value in src to dst. Use ta_parent for any dynamic
343     // memory allocations. It's explicitly allowed to have mpv_node reference
344     // static strings (and even mpv_node_list.keys), though.
345     int (*get)(const m_option_t *opt, void *ta_parent, struct mpv_node *dst,
346                void *src);
347 
348     // Return whether the values are the same. (There are no "unordered"
349     // results; for example, two floats with the value NaN compare equal. Other
350     // ambiguous floats, such as +0 and -0 compare equal. Some option types may
351     // incorrectly report unequal for values that are equal, such as sets (if
352     // the element order is different, which incorrectly matters), but values
353     // duplicated with m_option_copy() always return as equal. Empty strings
354     // and NULL strings are equal. Ambiguous unicode representations compare
355     // unequal.)
356     // If not set, values are always considered equal (=> not really optional).
357     bool (*equal)(const m_option_t *opt, void *a, void *b);
358 
359     // Optional: list of suffixes, terminated with a {0} entry. An empty list
360     // behaves like the list being NULL.
361     const struct m_option_action *actions;
362 };
363 
364 // Option description
365 struct m_option {
366     // Option name.
367     // Option declarations can use this as positional field.
368     const char *name;
369 
370     // Option type.
371     const m_option_type_t *type;
372 
373     // See \ref OptionFlags.
374     unsigned int flags;
375 
376     int offset;
377 
378     // Most numeric types restrict the range to [min, max] if min<max (this
379     // implies that if min/max are not set, the full range is used). In all
380     // cases, the actual range is clamped to the type's native range.
381     // Float types use [DBL_MIN, DBL_MAX], though by setting min or max to
382     // -/+INFINITY, the range can be extended to INFINITY. (This part is buggy
383     // for "float".)
384     // Preferably use M_RANGE() to set these fields.
385     double min, max;
386 
387     // Type dependent data (for all kinds of extended settings).
388     void *priv;
389 
390     // Initialize variable to given default before parsing options
391     const void *defval;
392 
393     // Print a warning when this option is used (for options with no direct
394     // replacement.)
395     const char *deprecation_message;
396 
397     // Optional function that validates a param value for this option.
398     m_opt_generic_validate_fn validate;
399 
400     // Optional function that displays help. Will replace type-specific help.
401     int (*help)(struct mp_log *log, const m_option_t *opt, struct bstr name);
402 };
403 
404 char *format_file_size(int64_t size);
405 
406 // The option is forbidden in config files.
407 #define M_OPT_NOCFG             (1 << 2)
408 
409 // The option should be set during command line pre-parsing
410 #define M_OPT_PRE_PARSE         (1 << 4)
411 
412 // The option expects a file name (or a list of file names)
413 #define M_OPT_FILE              (1 << 5)
414 
415 // Do not add as property.
416 #define M_OPT_NOPROP            (1 << 6)
417 
418 // Enable special semantics for some options when parsing the string "help".
419 #define M_OPT_HAVE_HELP         (1 << 7)
420 
421 // The following are also part of the M_OPT_* flags, and are used to update
422 // certain groups of options.
423 #define UPDATE_OPT_FIRST        (1 << 8)
424 #define UPDATE_TERM             (1 << 8)  // terminal options
425 #define UPDATE_SUB_FILT         (1 << 9)  // subtitle filter options
426 #define UPDATE_OSD              (1 << 10) // related to OSD rendering
427 #define UPDATE_BUILTIN_SCRIPTS  (1 << 11) // osc/ytdl/stats
428 #define UPDATE_IMGPAR           (1 << 12) // video image params overrides
429 #define UPDATE_INPUT            (1 << 13) // mostly --input-* options
430 #define UPDATE_AUDIO            (1 << 14) // --audio-channels etc.
431 #define UPDATE_PRIORITY         (1 << 15) // --priority (Windows-only)
432 #define UPDATE_SCREENSAVER      (1 << 16) // --stop-screensaver
433 #define UPDATE_VOL              (1 << 17) // softvol related options
434 #define UPDATE_LAVFI_COMPLEX    (1 << 18) // --lavfi-complex
435 #define UPDATE_HWDEC            (1 << 20) // --hwdec
436 #define UPDATE_DVB_PROG         (1 << 21) // some --dvbin-...
437 #define UPDATE_SUB_HARD         (1 << 22) // subtitle opts. that need full reinit
438 #define UPDATE_OPT_LAST         (1 << 22)
439 
440 // All bits between _FIRST and _LAST (inclusive)
441 #define UPDATE_OPTS_MASK \
442     (((UPDATE_OPT_LAST << 1) - 1) & ~(unsigned)(UPDATE_OPT_FIRST - 1))
443 
444 // type_float/type_double: string "default" is parsed as NaN (and reverse)
445 #define M_OPT_DEFAULT_NAN       (1 << 25)
446 
447 // type time: string "no" maps to MP_NOPTS_VALUE (if unset, NOPTS is rejected)
448 #define M_OPT_ALLOW_NO          (1 << 26)
449 
450 // type channels: disallow "auto" (still accept ""), limit list to at most 1 item.
451 #define M_OPT_CHANNELS_LIMITED  (1 << 27)
452 
453 // Like M_OPT_TYPE_OPTIONAL_PARAM.
454 #define M_OPT_OPTIONAL_PARAM    (1 << 30)
455 
456 // These are kept for compatibility with older code.
457 #define CONF_NOCFG              M_OPT_NOCFG
458 #define CONF_PRE_PARSE          M_OPT_PRE_PARSE
459 
460 // These flags are used to describe special parser capabilities or behavior.
461 
462 // The parameter is optional and by default no parameter is preferred. If
463 // ambiguous syntax is used ("--opt value"), the command line parser will
464 // assume that the argument takes no parameter. In config files, these
465 // options can be used without "=" and value.
466 #define M_OPT_TYPE_OPTIONAL_PARAM       (1 << 0)
467 
468 // Behaves fundamentally like a choice or a superset of it (all allowed string
469 // values are from a fixed set, although other types of values like numbers
470 // might be allowed too). E.g. m_option_type_choice and m_option_type_flag.
471 #define M_OPT_TYPE_CHOICE               (1 << 1)
472 
473 // When m_option.min/max are set, they denote a value range.
474 #define M_OPT_TYPE_USES_RANGE           (1 << 2)
475 
476 ///////////////////////////// Parser flags /////////////////////////////////
477 
478 // OptionParserReturn
479 //
480 // On success parsers return a number >= 0.
481 //
482 // To indicate that MPlayer should exit without playing anything,
483 // parsers return M_OPT_EXIT.
484 //
485 // On error one of the following (negative) error codes is returned:
486 
487 // For use by higher level APIs when the option name is invalid.
488 #define M_OPT_UNKNOWN           -1
489 
490 // Returned when a parameter is needed but wasn't provided.
491 #define M_OPT_MISSING_PARAM     -2
492 
493 // Returned when the given parameter couldn't be parsed.
494 #define M_OPT_INVALID           -3
495 
496 // Returned if the value is "out of range". The exact meaning may
497 // vary from type to type.
498 #define M_OPT_OUT_OF_RANGE      -4
499 
500 // The option doesn't take a parameter.
501 #define M_OPT_DISALLOW_PARAM    -5
502 
503 // Returned when MPlayer should exit. Used by various help stuff.
504 #define M_OPT_EXIT              -6
505 
506 char *m_option_strerror(int code);
507 
508 // Base function to parse options. Includes calling help and validation
509 // callbacks. Only when this functionality is for some reason required to not
510 // happen should the parse function pointer be utilized by itself.
511 //
512 // See \ref m_option_type::parse.
513 int m_option_parse(struct mp_log *log, const m_option_t *opt,
514                    struct bstr name, struct bstr param, void *dst);
515 
516 // Helper to print options, see \ref m_option_type::print.
m_option_print(const m_option_t * opt,const void * val_ptr)517 static inline char *m_option_print(const m_option_t *opt, const void *val_ptr)
518 {
519     if (opt->type->print)
520         return opt->type->print(opt, val_ptr);
521     else
522         return NULL;
523 }
524 
m_option_pretty_print(const m_option_t * opt,const void * val_ptr)525 static inline char *m_option_pretty_print(const m_option_t *opt,
526                                           const void *val_ptr)
527 {
528     if (opt->type->pretty_print)
529         return opt->type->pretty_print(opt, val_ptr);
530     else
531         return m_option_print(opt, val_ptr);
532 }
533 
534 // Helper around \ref m_option_type::copy.
m_option_copy(const m_option_t * opt,void * dst,const void * src)535 static inline void m_option_copy(const m_option_t *opt, void *dst,
536                                  const void *src)
537 {
538     if (opt->type->copy)
539         opt->type->copy(opt, dst, src);
540 }
541 
542 // Helper around \ref m_option_type::free.
m_option_free(const m_option_t * opt,void * dst)543 static inline void m_option_free(const m_option_t *opt, void *dst)
544 {
545     if (opt->type->free)
546         opt->type->free(dst);
547 }
548 
549 // see m_option_type.set
m_option_set_node(const m_option_t * opt,void * dst,struct mpv_node * src)550 static inline int m_option_set_node(const m_option_t *opt, void *dst,
551                                     struct mpv_node *src)
552 {
553     if (opt->type->set)
554         return opt->type->set(opt, dst, src);
555     return M_OPT_UNKNOWN;
556 }
557 
558 // Call m_option_parse for strings, m_option_set_node otherwise.
559 int m_option_set_node_or_string(struct mp_log *log, const m_option_t *opt,
560                                 const char *name, void *dst, struct mpv_node *src);
561 
562 // see m_option_type.get
m_option_get_node(const m_option_t * opt,void * ta_parent,struct mpv_node * dst,void * src)563 static inline int m_option_get_node(const m_option_t *opt, void *ta_parent,
564                                     struct mpv_node *dst, void *src)
565 {
566     if (opt->type->get)
567         return opt->type->get(opt, ta_parent, dst, src);
568     return M_OPT_UNKNOWN;
569 }
570 
m_option_equal(const m_option_t * opt,void * a,void * b)571 static inline bool m_option_equal(const m_option_t *opt, void *a, void *b)
572 {
573     // Handle trivial equivalence.
574     // If not implemented, assume this type has no actual values => always equal.
575     if (a == b || !opt->type->equal)
576         return true;
577     return opt->type->equal(opt, a, b);
578 }
579 
580 int m_option_required_params(const m_option_t *opt);
581 
582 extern const char m_option_path_separator;
583 
584 // Cause a compilation warning if typeof(expr) != type.
585 // Should be used with pointer types only.
586 #define MP_EXPECT_TYPE(type, expr) (0 ? (type)0 : (expr))
587 
588 // This behaves like offsetof(type, member), but will cause a compilation
589 // warning if typeof(member) != expected_member_type.
590 // It uses some trickery to make it compile as expression.
591 #define MP_CHECKED_OFFSETOF(type, member, expected_member_type)             \
592     (offsetof(type, member) + (0 && MP_EXPECT_TYPE(expected_member_type*,   \
593                                                    &((type*)0)->member)))
594 
595 #define OPT_TYPED_FIELD(type_, c_type, field) \
596     .type = &type_, \
597     .offset = MP_CHECKED_OFFSETOF(OPT_BASE_STRUCT, field, c_type)
598 
599 #define OPTION_LIST_SEPARATOR ','
600 
601 #define OPTDEF_STR(s)     .defval = (void *)&(char * const){s}
602 #define OPTDEF_INT(i)     .defval = (void *)&(const int){i}
603 #define OPTDEF_INT64(i)   .defval = (void *)&(const int64_t){i}
604 #define OPTDEF_FLOAT(f)   .defval = (void *)&(const float){f}
605 #define OPTDEF_DOUBLE(d)  .defval = (void *)&(const double){d}
606 
607 #define M_RANGE(a, b) .min = (a), .max = (b)
608 
609 #define OPT_BOOL(field) \
610     OPT_TYPED_FIELD(m_option_type_bool, bool, field)
611 
612 #define OPT_FLAG(field) \
613     OPT_TYPED_FIELD(m_option_type_flag, int, field)
614 
615 #define OPT_INT(field) \
616     OPT_TYPED_FIELD(m_option_type_int, int, field)
617 
618 #define OPT_INT64(field) \
619     OPT_TYPED_FIELD(m_option_type_int64, int64_t, field)
620 
621 #define OPT_FLOAT(field) \
622     OPT_TYPED_FIELD(m_option_type_float, float, field)
623 
624 #define OPT_DOUBLE(field) \
625     OPT_TYPED_FIELD(m_option_type_double, double, field)
626 
627 #define OPT_STRING(field) \
628     OPT_TYPED_FIELD(m_option_type_string, char*, field)
629 
630 #define OPT_STRINGLIST(field) \
631     OPT_TYPED_FIELD(m_option_type_string_list, char**, field)
632 
633 #define OPT_KEYVALUELIST(field) \
634     OPT_TYPED_FIELD(m_option_type_keyvalue_list, char**, field)
635 
636 #define OPT_PATHLIST(field) \
637     OPT_TYPED_FIELD(m_option_type_string_list, char**, field), \
638     .priv = (void *)&m_option_path_separator
639 
640 #define OPT_TIME(field) \
641     OPT_TYPED_FIELD(m_option_type_time, double, field)
642 
643 #define OPT_REL_TIME(field) \
644     OPT_TYPED_FIELD(m_option_type_rel_time, struct m_rel_time, field)
645 
646 #define OPT_COLOR(field) \
647     OPT_TYPED_FIELD(m_option_type_color, struct m_color, field)
648 
649 #define OPT_BYTE_SIZE(field) \
650     OPT_TYPED_FIELD(m_option_type_byte_size, int64_t, field)
651 
652 // (Approximation of x<=SIZE_MAX/2 for m_option.max, which is double.)
653 #define M_MAX_MEM_BYTES MPMIN((1ULL << 62), (size_t)-1 / 2)
654 
655 #define OPT_GEOMETRY(field) \
656     OPT_TYPED_FIELD(m_option_type_geometry, struct m_geometry, field)
657 
658 #define OPT_SIZE_BOX(field) \
659     OPT_TYPED_FIELD(m_option_type_size_box, struct m_geometry, field)
660 
661 #define OPT_TRACKCHOICE(field) \
662     OPT_CHOICE(field, {"no", -2}, {"auto", -1}), \
663     M_RANGE(0, 8190)
664 
665 #define OPT_MSGLEVELS(field) \
666     OPT_TYPED_FIELD(m_option_type_msglevels, char **, field)
667 
668 #define OPT_ASPECT(field) \
669     OPT_TYPED_FIELD(m_option_type_aspect, float, field)
670 
671 #define OPT_IMAGEFORMAT(field) \
672     OPT_TYPED_FIELD(m_option_type_imgfmt, int, field)
673 
674 #define OPT_AUDIOFORMAT(field) \
675     OPT_TYPED_FIELD(m_option_type_afmt, int, field)
676 
677 #define OPT_CHANNELS(field) \
678     OPT_TYPED_FIELD(m_option_type_channels, struct m_channels, field)
679 
680 #define OPT_INT_VALIDATE(field, validate_fn) \
681     OPT_TYPED_FIELD(m_option_type_int, int, field), \
682     .validate = (m_opt_generic_validate_fn) \
683         MP_EXPECT_TYPE(m_opt_int_validate_fn, validate_fn)
684 
685 #define OPT_STRING_VALIDATE(field, validate_fn) \
686     OPT_TYPED_FIELD(m_option_type_string, char*, field), \
687     .validate = (m_opt_generic_validate_fn) \
688         MP_EXPECT_TYPE(m_opt_string_validate_fn, validate_fn)
689 
690 #define M_CHOICES(...) \
691     .priv = (void *)&(const struct m_opt_choice_alternatives[]){ __VA_ARGS__, {0}}
692 
693 // Variant which takes a pointer to struct m_opt_choice_alternatives directly
694 #define OPT_CHOICE_C(field, choices) \
695     OPT_TYPED_FIELD(m_option_type_choice, int, field), \
696     .priv = (void *)MP_EXPECT_TYPE(const struct m_opt_choice_alternatives*, choices)
697 
698 // Variant where you pass a struct m_opt_choice_alternatives initializer
699 #define OPT_CHOICE(field, ...) \
700     OPT_TYPED_FIELD(m_option_type_choice, int, field), \
701     M_CHOICES(__VA_ARGS__)
702 
703 #define OPT_FLAGS(field, ...) \
704     OPT_TYPED_FIELD(m_option_type_flags, int, field), \
705     M_CHOICES(__VA_ARGS__)
706 
707 #define OPT_SETTINGSLIST(field, objlist) \
708     OPT_TYPED_FIELD(m_option_type_obj_settings_list, m_obj_settings_t*, field), \
709     .priv = (void*)MP_EXPECT_TYPE(const struct m_obj_list*, objlist)
710 
711 #define OPT_FOURCC(field) \
712     OPT_TYPED_FIELD(m_option_type_fourcc, int, field)
713 
714 #define OPT_CYCLEDIR(field) \
715     OPT_TYPED_FIELD(m_option_type_cycle_dir, double, field)
716 
717 // subconf must have the type struct m_sub_options.
718 // All sub-options are prefixed with "name-" and are added to the current
719 // (containing) option list.
720 // If name is "", add the sub-options directly instead.
721 // "field" refers to the field, that must be a pointer to a field described by
722 // the subconf struct.
723 #define OPT_SUBSTRUCT(field, subconf) \
724     .offset = offsetof(OPT_BASE_STRUCT, field), \
725     .type = &m_option_type_subconfig, .priv = (void*)&subconf
726 
727 // Non-fields
728 
729 #define OPT_ALIAS(newname) \
730     .type = &m_option_type_alias, .priv = newname, .offset = -1
731 
732 // If "--optname" was removed, but "--newname" has the same semantics.
733 // It will be redirected, and a warning will be printed on first use.
734 #define OPT_REPLACED_MSG(newname, msg) \
735     .type = &m_option_type_alias, .priv = newname, \
736     .deprecation_message = (msg), .offset = -1
737 
738 // Same, with a generic deprecation message.
739 #define OPT_REPLACED(newname) OPT_REPLACED_MSG(newname, "")
740 
741 // Alias, resolved on the CLI/config file/profile parser level only.
742 #define OPT_CLI_ALIAS(newname) \
743     .type = &m_option_type_cli_alias, .priv = newname, \
744     .flags = M_OPT_NOPROP, .offset = -1
745 
746 // "--optname" doesn't exist, but inform the user about a replacement with msg.
747 #define OPT_REMOVED(msg) \
748     .type = &m_option_type_removed, .priv = msg, \
749     .deprecation_message = "", .flags = M_OPT_NOPROP, .offset = -1
750 
751 #define OPT_PRINT(fn) \
752     .flags = M_OPT_NOCFG | M_OPT_PRE_PARSE | M_OPT_NOPROP, \
753     .type = &m_option_type_print_fn, \
754     .priv = MP_EXPECT_TYPE(m_opt_print_fn, fn), \
755     .offset = -1
756 
757 #endif /* MPLAYER_M_OPTION_H */
758