Received: from eggs.gnu.org ([2001:4830:134:3::10]:58384) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3mbm-000082-Bm for qemu-devel@nongnu.org; Mon, 29 Jul 2013 08:31:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V3mbg-0006Dd-7w for qemu-devel@nongnu.org; Mon, 29 Jul 2013 08:31:26 -0400 Received: from mx1.redhat.com ([209.132.183.28]:43920) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V3mbf-0006DQ-Uq for qemu-devel@nongnu.org; Mon, 29 Jul 2013 08:31:20 -0400 Message-ID: <1375101058.18120.12.camel@localhost.localdomain> From: Marcel Apfelbaum Date: Mon, 29 Jul 2013 15:30:58 +0300 In-Reply-To: <1375099903-17335-3-git-send-email-marcel.a@redhat.com> References: <1375099903-17335-1-git-send-email-marcel.a@redhat.com> <1375099903-17335-3-git-send-email-marcel.a@redhat.com> Content-Type: text/plain; charset="UTF-8" Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [PATCH v3 2/3] qemu-help: Sort devices by logical functionality List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: pbonzini@redhat.com, aliguori@us.ibm.com, afaerber@suse.de, mst@redhat.com On Mon, 2013-07-29 at 15:11 +0300, Marcel Apfelbaum wrote: > Categorize devices that appear as output to "-device ?" command > by logical functionality. Sort the devices by logical categories > before showing them to user. > > The sort is done by functionality rather than alphabetical. > > Signed-off-by: Marcel Apfelbaum > --- > Changes from v2: > Addressed Michael Tsirkin's review: > - Explicit connection between the categories > and their names > - Refactoring of unsafe code > Addressed Paolo Bonzini's review > - Replaced Management category by USB > > Changes from v1: > Addressed Michael Tsirkin's review: > - Used bitmap operations on categories > - Moved category names into the header file > > include/hw/qdev-core.h | 29 ++++++++++++++++++++++++++++ > qdev-monitor.c | 52 +++++++++++++++++++++++++++++++++++++++++--------- > 2 files changed, 72 insertions(+), 9 deletions(-) > > diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h > index e8b89b1..111ad06 100644 > --- a/include/hw/qdev-core.h > +++ b/include/hw/qdev-core.h > @@ -18,6 +18,34 @@ enum { > #define DEVICE_CLASS(klass) OBJECT_CLASS_CHECK(DeviceClass, (klass), TYPE_DEVICE) > #define DEVICE_GET_CLASS(obj) OBJECT_GET_CLASS(DeviceClass, (obj), TYPE_DEVICE) > > +typedef enum DeviceCategory { > + DEVICE_CATEGORY_ASSEMBLY, > + DEVICE_CATEGORY_USB, > + DEVICE_CATEGORY_STORAGE, > + DEVICE_CATEGORY_NETWORK, > + DEVICE_CATEGORY_INPUT, > + DEVICE_CATEGORY_DISPLAY, > + DEVICE_CATEGORY_SOUND, > + DEVICE_CATEGORY_MISC, > + DEVICE_CATEGORY_MAX > +} DeviceCategory; > + > +static inline const char *qdev_category_get_name(DeviceCategory category) > +{ > + static const char *category_names[DEVICE_CATEGORY_MAX] = { > + [DEVICE_CATEGORY_ASSEMBLY] = "Assembly", > + [DEVICE_CATEGORY_USB] = "USB", > + [DEVICE_CATEGORY_STORAGE] = "Storage", > + [DEVICE_CATEGORY_NETWORK] = "Network", > + [DEVICE_CATEGORY_INPUT] = "Input", > + [DEVICE_CATEGORY_DISPLAY] = "Display", > + [DEVICE_CATEGORY_SOUND] = "Sound", > + [DEVICE_CATEGORY_MISC] = "Misc", > + }; > + > + return category_names[category]; > +}; > + > typedef int (*qdev_initfn)(DeviceState *dev); > typedef int (*qdev_event)(DeviceState *dev); > typedef void (*qdev_resetfn)(DeviceState *dev); > @@ -81,6 +109,7 @@ typedef struct DeviceClass { > ObjectClass parent_class; > /*< public >*/ > > + DECLARE_BITMAP(categories, DEVICE_CATEGORY_MAX); > const char *fw_name; > const char *desc; > Property *props; > diff --git a/qdev-monitor.c b/qdev-monitor.c > index e54dbc2..536e246 100644 > --- a/qdev-monitor.c > +++ b/qdev-monitor.c > @@ -75,24 +75,27 @@ static bool qdev_class_has_alias(DeviceClass *dc) > return (qdev_class_get_alias(dc) != NULL); > } > > -static void qdev_print_devinfo(ObjectClass *klass, void *opaque) > +static void qdev_print_class_devinfo(DeviceClass *dc) > { > - DeviceClass *dc; > - bool *show_no_user = opaque; > - > - dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); > + DeviceCategory category; > > - if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { > + if (!dc || dc->no_user) { > return; > } > > - error_printf("name \"%s\"", object_class_get_name(klass)); > + error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc))); > if (dc->bus_type) { > error_printf(", bus %s", dc->bus_type); > } > if (qdev_class_has_alias(dc)) { > error_printf(", alias \"%s\"", qdev_class_get_alias(dc)); > } > + error_printf(", categories"); > + for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) { > + if (test_bit(category, dc->categories)) { > + error_printf(" \"%s\"", qdev_category_get_name(category)); > + } > + } > if (dc->desc) { > error_printf(", desc \"%s\"", dc->desc); > } > @@ -102,6 +105,20 @@ static void qdev_print_devinfo(ObjectClass *klass, void *opaque) > error_printf("\n"); > } > > +static void qdev_print_devinfo(ObjectClass *klass, void *opaque) > +{ > + DeviceClass *dc; > + bool *show_no_user = opaque; > + > + dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE); > + > + if (!dc || (show_no_user && !*show_no_user && dc->no_user)) { > + return; > + } > + > + qdev_print_class_devinfo(dc); > +} > + > static int set_property(const char *name, const char *value, void *opaque) > { > DeviceState *dev = opaque; > @@ -139,6 +156,20 @@ static const char *find_typename_by_alias(const char *alias) > return NULL; > } > > +static void qdev_print_category_devices(DeviceCategory category) > +{ > + DeviceClass *dc; > + GSList *list, *curr; > + > + list = object_class_get_list(TYPE_DEVICE, false); > + for (curr = list; curr; curr = g_slist_next(curr)) { > + dc = (DeviceClass *)object_class_dynamic_cast(curr->data, TYPE_DEVICE); > + if (test_bit(category, dc->categories)) { > + qdev_print_class_devinfo(dc); > + } > + } Here is a memory leak. It will be fixed in v4 Marcel > +} > + > int qdev_device_help(QemuOpts *opts) > { > const char *driver; > @@ -147,8 +178,11 @@ int qdev_device_help(QemuOpts *opts) > > driver = qemu_opt_get(opts, "driver"); > if (driver && is_help_option(driver)) { > - bool show_no_user = false; > - object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, &show_no_user); > + DeviceCategory category; > + for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) { > + qdev_print_category_devices(category); > + } > + > return 1; > } >