bool has representations of "on" and "off" different from OnOffAuto/OnOffSplit: - The command line syntax accepts on/yes/true/y and off/no/false/n for bool but only on and off for OnOffAuto. - JSON uses true/false for bool but "on" and "off" for OnOffAuto/OnOffSplit.
This inconsistency causes some problems: - Users need to take the underlying type into consideration to determine what literal to specify, increasing cognitive loads for human users and complexity for programs invoking QEMU. - Converting an existing bool property to OnOffAuto/OnOffSplit will break compatibility. Fix these problems by accepting bool literals for OnOffAuto/OnOffSplit. This change is specific to OnOffAuto/OnOffSplit; types added in the future may be defined as an alternate of bool and enum to avoid the mentioned problems in the first place. Signed-off-by: Akihiko Odaki <akihiko.od...@daynix.com> --- hw/core/qdev-properties.c | 17 ++++++++++++++++- scripts/qapi/visit.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/hw/core/qdev-properties.c b/hw/core/qdev-properties.c index 434a76f5036edd2091a9c79525b8e102582637be..073902431213c5be47197cb0d993d60cc2562501 100644 --- a/hw/core/qdev-properties.c +++ b/hw/core/qdev-properties.c @@ -2,6 +2,7 @@ #include "hw/qdev-properties.h" #include "qapi/error.h" #include "qapi/qapi-types-misc.h" +#include "qapi/qapi-visit-common.h" #include "qapi/qmp/qlist.h" #include "qemu/ctype.h" #include "qemu/error-report.h" @@ -493,12 +494,26 @@ const PropertyInfo qdev_prop_string = { /* --- on/off/auto --- */ +static void set_on_off_auto(Object *obj, Visitor *v, const char *name, + void *opaque, Error **errp) +{ + Property *prop = opaque; + int *ptr = object_field_prop_ptr(obj, prop); + OnOffAuto value; + + if (!visit_type_OnOffAuto(v, name, &value, errp)) { + return; + } + + *ptr = value; +} + const PropertyInfo qdev_prop_on_off_auto = { .name = "OnOffAuto", .description = "on/off/auto", .enum_table = &OnOffAuto_lookup, .get = qdev_propinfo_get_enum, - .set = qdev_propinfo_set_enum, + .set = set_on_off_auto, .set_default_value = qdev_propinfo_set_default_value_enum, }; diff --git a/scripts/qapi/visit.py b/scripts/qapi/visit.py index 12f92e429f6bafc091f74af88c1b837d08c7f733..221373b165aa95bceb4eb50a557edf0e5b4c01f7 100644 --- a/scripts/qapi/visit.py +++ b/scripts/qapi/visit.py @@ -209,6 +209,29 @@ def gen_visit_list(name: str, element_type: QAPISchemaType) -> str: def gen_visit_enum(name: str) -> str: + if name in ('OnOffAuto', 'OnOffSplit'): + return mcgen(''' + +bool visit_type_%(c_name)s(Visitor *v, const char *name, + %(c_name)s *obj, Error **errp) +{ + bool b; + int i; + + if (v->type == VISITOR_INPUT && visit_type_bool(v, name, &b, NULL)) { + *obj = b ? %(on)s : %(off)s; + return true; + } + + b = visit_type_enum(v, name, &i, &%(c_name)s_lookup, errp); + *obj = i; + + return b; +} +''', + c_name=c_name(name), + on=c_enum_const(name, 'on'), off=c_enum_const(name, 'off')) + return mcgen(''' bool visit_type_%(c_name)s(Visitor *v, const char *name, @@ -359,6 +382,7 @@ def _begin_user_module(self, name: str) -> None: self._genc.preamble_add(mcgen(''' #include "qemu/osdep.h" #include "qapi/error.h" +#include "qapi/visitor-impl.h" #include "%(visit)s.h" ''', visit=visit)) -- 2.48.1