From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:39735) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO4Hb-0006VV-M8 for qemu-devel@nongnu.org; Tue, 26 Jan 2016 09:07:48 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aO4HY-0002vR-RY for qemu-devel@nongnu.org; Tue, 26 Jan 2016 09:07:47 -0500 Received: from mx1.redhat.com ([209.132.183.28]:47018) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO4HY-0002vK-Kw for qemu-devel@nongnu.org; Tue, 26 Jan 2016 09:07:44 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by mx1.redhat.com (Postfix) with ESMTPS id 5DB24C0023B2 for ; Tue, 26 Jan 2016 14:07:44 +0000 (UTC) From: Gerd Hoffmann Date: Tue, 26 Jan 2016 15:07:31 +0100 Message-Id: <1453817255-10287-1-git-send-email-kraxel@redhat.com> Subject: [Qemu-devel] [PATCH v2 1/5] console: add & use qemu_console_lookup_by_device_name List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: Marcelo Tosatti , Markus Armbruster , Gerd Hoffmann We have two places needing this, and a third one will come shortly. So factor things out into a helper function to reduce code duplication. Signed-off-by: Gerd Hoffmann Reviewed-by: Daniel P. Berrange --- include/ui/console.h | 2 ++ ui/console.c | 23 +++++++++++++++++++++++ ui/input.c | 15 ++++----------- ui/vnc.c | 15 ++++----------- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/include/ui/console.h b/include/ui/console.h index adac36d..bbc3b7c 100644 --- a/include/ui/console.h +++ b/include/ui/console.h @@ -377,6 +377,8 @@ void graphic_hw_text_update(QemuConsole *con, console_ch_t *chardata); QemuConsole *qemu_console_lookup_by_index(unsigned int index); QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head); +QemuConsole *qemu_console_lookup_by_device_name(const char *device_id, + uint32_t head, Error **errp); bool qemu_console_is_visible(QemuConsole *con); bool qemu_console_is_graphic(QemuConsole *con); bool qemu_console_is_fixedsize(QemuConsole *con); diff --git a/ui/console.c b/ui/console.c index fe950c6..e883d53 100644 --- a/ui/console.c +++ b/ui/console.c @@ -1779,6 +1779,29 @@ QemuConsole *qemu_console_lookup_by_device(DeviceState *dev, uint32_t head) return NULL; } +QemuConsole *qemu_console_lookup_by_device_name(const char *device_id, + uint32_t head, Error **errp) +{ + DeviceState *dev; + QemuConsole *con; + + dev = qdev_find_recursive(sysbus_get_default(), device_id); + if (dev == NULL) { + error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, + "Device '%s' not found", device_id); + return NULL; + } + + con = qemu_console_lookup_by_device(dev, head); + if (con == NULL) { + error_setg(errp, "Device %s (head %d) is not bound to a QemuConsole", + device_id, head); + return NULL; + } + + return con; +} + bool qemu_console_is_visible(QemuConsole *con) { return (con == active_console) || (con->dcls > 0); diff --git a/ui/input.c b/ui/input.c index 006667b..1409e01 100644 --- a/ui/input.c +++ b/ui/input.c @@ -81,19 +81,12 @@ void qemu_input_handler_bind(QemuInputHandlerState *s, const char *device_id, int head, Error **errp) { - DeviceState *dev; QemuConsole *con; + Error *err = NULL; - dev = qdev_find_recursive(sysbus_get_default(), device_id); - if (dev == NULL) { - error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND, - "Device '%s' not found", device_id); - return; - } - - con = qemu_console_lookup_by_device(dev, head); - if (con == NULL) { - error_setg(errp, "Device %s is not bound to a QemuConsole", device_id); + con = qemu_console_lookup_by_device_name(device_id, head, &err); + if (err) { + error_propagate(errp, err); return; } diff --git a/ui/vnc.c b/ui/vnc.c index 339f8c3..19dcfce 100644 --- a/ui/vnc.c +++ b/ui/vnc.c @@ -3732,19 +3732,12 @@ void vnc_display_open(const char *id, Error **errp) device_id = qemu_opt_get(opts, "display"); if (device_id) { - DeviceState *dev; int head = qemu_opt_get_number(opts, "head", 0); + Error *err = NULL; - dev = qdev_find_recursive(sysbus_get_default(), device_id); - if (dev == NULL) { - error_setg(errp, "Device '%s' not found", device_id); - goto fail; - } - - con = qemu_console_lookup_by_device(dev, head); - if (con == NULL) { - error_setg(errp, "Device %s is not bound to a QemuConsole", - device_id); + con = qemu_console_lookup_by_device_name(device_id, head, &err); + if (err) { + error_propagate(errp, err); goto fail; } } else { -- 1.8.3.1