summaryrefslogtreecommitdiffstats
path: root/options/m_option.h
diff options
context:
space:
mode:
authorwm4 <wm4@nowhere>2020-03-14 02:07:35 +0100
committerwm4 <wm4@nowhere>2020-03-14 02:23:38 +0100
commitc78482045444c488bb7948305d583a55d17cd236 (patch)
tree32f9142ac9cd79f6a59220a623d390ef9e269da6 /options/m_option.h
parent7c4a550c0ed9690e441d2bd0b212d42fffbec70c (diff)
downloadmpv-c78482045444c488bb7948305d583a55d17cd236.tar.bz2
mpv-c78482045444c488bb7948305d583a55d17cd236.tar.xz
options: introduce bool option type, use it for --fullscreen
The option code is very old and was added to MPlayer in the early 2000s, when C99 was still new. MPlayer did not use the "bool" type anywhere,l and the logical option equivalent to bool, the "flag" option type, used int, with the convention that only the values 0 and 1 are allowed. mpv may have hammered many, many additional tentacles to the option code, but some of the basics never changed, and m_option_type_flag still uses int. This seems a bit weird, since mpv uses bool for booleans. So finally introduce an m_option_type_bool. To avoid duplicating too much code, change the flag code to bool, and "reimplement" m_option_type_flag on top of m_option_type_bool. As a "demonstration", change the --fullscreen option to this new type. Ideally, all options would be changed too bool, and m_option_type_flag would be removed. But that is a lot of monotonous thankless work, so I'm not doing it, and making it a painful years long transition. At the same time, I'm introducing a new concept for option declarations. Instead of OPT_BOOL(), which define the full m_option struct contents, there's OPTF_BOOL(), which only takes the option field name itself. The name is provided via a normal struct field initializer. Other fields (such as flags) can be provided via designated initializers. The advantage of this is that we don't need tons of nested vararg macros. We also don't need to deal with 0-sized varargs being a pain (and in fact they are not a thing in standard C99 and probably C11). There is no need to provide a mandatory flags argument either, which is the reason why so many OPT_ macros are used with a "0" argument. (The flag argument seems to confuse other developers; they either don't immediately recognize what it is, and sometimes it's supposed to be the option's default value.) Not having to mess with the flag argument in such option macros is also a reason for the removal of M_OPT_RANGE etc., for the better or worse. The only place that special-cased the _flag option type was in command.c; change it to use something effectively very similar that automatically includes the new _bool option type. Everything else should be transparent to the change. The fullscreen option change should be transparent too, as C99 bool is basically an integer type that is clamped to 0/1 (except in Swift, Swift sucks).
Diffstat (limited to 'options/m_option.h')
-rw-r--r--options/m_option.h8
1 files changed, 8 insertions, 0 deletions
diff --git a/options/m_option.h b/options/m_option.h
index 8d7063c212..97383237a5 100644
--- a/options/m_option.h
+++ b/options/m_option.h
@@ -38,6 +38,7 @@ struct mpv_global;
///////////////////////////// Options types declarations ////////////////////
// Simple types
+extern const m_option_type_t m_option_type_bool;
extern const m_option_type_t m_option_type_flag;
extern const m_option_type_t m_option_type_dummy_flag;
extern const m_option_type_t m_option_type_int;
@@ -226,6 +227,7 @@ struct m_sub_options {
// through this union. It serves for self-documentation and to get minimal
// size/alignment requirements for option values in general.
union m_option_value {
+ bool bool_;
int flag; // not the C type "bool"!
int int_;
int64_t int64;
@@ -352,6 +354,7 @@ struct m_option_type {
// Option description
struct m_option {
// Option name.
+ // Option declarations can use this as positional field.
const char *name;
// Option type.
@@ -587,6 +590,10 @@ extern const char m_option_path_separator;
#define OPT_HELPER_REMOVEPAREN(...) __VA_ARGS__
+#define OPTF_BOOL(field) \
+ .type = &m_option_type_bool, \
+ .offset = MP_CHECKED_OFFSETOF(OPT_BASE_STRUCT, field, bool),
+
/* The OPT_SOMETHING->OPT_SOMETHING_ kind of redirection exists to
* make the code fully standard-conforming: the C standard requires that
* __VA_ARGS__ has at least one argument (though GCC for example would accept
@@ -594,6 +601,7 @@ extern const char m_option_path_separator;
* argument to ensure __VA_ARGS__ is not empty when calling the next macro.
*/
+// Note: new code should use OPTF_BOOL instead
#define OPT_FLAG(...) \
OPT_GENERAL(int, __VA_ARGS__, .type = &m_option_type_flag)