@@ -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,
};
@@ -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))
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.odaki@daynix.com> --- hw/core/qdev-properties.c | 17 ++++++++++++++++- scripts/qapi/visit.py | 24 ++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-)