--- examples/virtxml.c | 3 + libvirt-designer/libvirt-designer-domain.c | 131 +++++++++++++++++++++++++++++ libvirt-designer/libvirt-designer-domain.h | 2 + libvirt-designer/libvirt-designer.sym | 1 + 4 files changed, 137 insertions(+) diff --git a/examples/virtxml.c b/examples/virtxml.c index 6fb0551..d127406 100644 --- a/examples/virtxml.c +++ b/examples/virtxml.c @@ -642,6 +642,9 @@ main(int argc, char *argv[]) gvir_designer_domain_setup_machine(domain, &error); CHECK_ERROR; + g_object_unref(gvir_designer_domain_add_sound(domain, &error)); + CHECK_ERROR; + if (arch_str) { gvir_designer_domain_setup_container_full(domain, arch_str, &error); CHECK_ERROR; diff --git a/libvirt-designer/libvirt-designer-domain.c b/libvirt-designer/libvirt-designer-domain.c index ed47d60..c5105cd 100644 --- a/libvirt-designer/libvirt-designer-domain.c +++ b/libvirt-designer/libvirt-designer-domain.c @@ -389,6 +389,7 @@ static void gvir_designer_domain_add_console(GVirDesignerDomain *design) g_object_unref(G_OBJECT(console)); } + static void gvir_designer_domain_add_input(GVirDesignerDomain *design) { GVirConfigDomainInput *input; @@ -933,6 +934,136 @@ cleanup: } +static OsinfoDevice * +gvir_designer_domain_get_preferred_soundcard(GVirDesignerDomain *design, + GError **error) +{ + OsinfoDevice *device = NULL; + OsinfoDeviceLink *dev_link; + + dev_link = gvir_designer_domain_get_preferred_device(design, + "audio", + error); + if (dev_link == NULL) + goto cleanup; + + device = osinfo_devicelink_get_target(dev_link); + +cleanup: + if (dev_link != NULL) + g_object_unref(dev_link); + + return device; +} + +static OsinfoDeviceList * +gvir_designer_domain_get_fallback_devices(GVirDesignerDomain *design, + const char *class, + GError **error) +{ + OsinfoDeviceList *devices = NULL; + OsinfoFilter *filter; + + filter = osinfo_filter_new(); + osinfo_filter_add_constraint(filter, OSINFO_DEVICE_PROP_CLASS, class); + devices = gvir_designer_domain_get_supported_devices(design, filter); + g_object_unref(G_OBJECT(filter)); + + if ((devices == NULL) || + (osinfo_list_get_length(OSINFO_LIST(devices)) == 0)) { + goto cleanup; + } + + return devices; + +cleanup: + if (devices != NULL) + g_object_unref(devices); + + return NULL; +} + + +static OsinfoDevice * +gvir_designer_domain_get_fallback_soundcard(GVirDesignerDomain *domain, + GError **error) +{ + OsinfoEntity *dev = NULL; + OsinfoDeviceList *devices = NULL; + + devices = gvir_designer_domain_get_fallback_devices(domain, "audio", error); + if (devices == NULL) + goto cleanup; + + dev = osinfo_list_get_nth(OSINFO_LIST(devices), 0); + g_object_ref(G_OBJECT(dev)); + +cleanup: + if (devices != NULL) + g_object_unref(G_OBJECT(devices)); + + return OSINFO_DEVICE(dev); +} + + +static GVirConfigDomainSoundModel +gvir_designer_sound_model_from_soundcard(OsinfoDevice *soundcard) +{ + const char *name; + + name = osinfo_device_get_name(soundcard); + if (g_strcmp0(name, "ac97") == 0) { + return GVIR_CONFIG_DOMAIN_SOUND_MODEL_AC97; + } else if (g_strcmp0(name, "ich6") == 0) { + return GVIR_CONFIG_DOMAIN_SOUND_MODEL_ICH6; + } else if (g_strcmp0(name, "es1370") == 0) { + return GVIR_CONFIG_DOMAIN_SOUND_MODEL_ES1370; + } else if (g_strcmp0(name, "sb16") == 0) { + return GVIR_CONFIG_DOMAIN_SOUND_MODEL_SB16; + } else { + g_warning("Unknown soundcard %s, falling back to PC speaker", name); + return GVIR_CONFIG_DOMAIN_SOUND_MODEL_PCSPK; + } +} + + +/** + * gvir_designer_domain_add_sound: + * @design: (transfer none): the domain designer instance + * @error: return location for a #GError, or NULL + * + * Add a new soundcard to the domain. + * + * Returns: (transfer full): the pointer to the new soundcard. + * If something fails NULL is returned and @error is set. + */ +GVirConfigDomainSound * +gvir_designer_domain_add_sound(GVirDesignerDomain *design, GError **error) +{ + GVirConfigDomainSound *sound; + OsinfoDevice *soundcard; + GVirConfigDomainSoundModel model; + + g_return_val_if_fail(GVIR_DESIGNER_IS_DOMAIN(design), NULL); + + soundcard = gvir_designer_domain_get_preferred_soundcard(design, NULL); + if (soundcard == NULL) + soundcard = gvir_designer_domain_get_fallback_soundcard(design, NULL); + + if (soundcard == NULL) + return NULL; + + sound = gvir_config_domain_sound_new(); + model = gvir_designer_sound_model_from_soundcard(soundcard); + gvir_config_domain_sound_set_model(sound, model); + + gvir_config_domain_add_device(design->priv->config, + GVIR_CONFIG_DOMAIN_DEVICE(sound)); + + return sound; +} + + static gchar * gvir_designer_domain_next_disk_target(GVirDesignerDomain *design, GVirConfigDomainDiskBus bus) diff --git a/libvirt-designer/libvirt-designer-domain.h b/libvirt-designer/libvirt-designer-domain.h index c7b0e5c..c0d06e8 100644 --- a/libvirt-designer/libvirt-designer-domain.h +++ b/libvirt-designer/libvirt-designer-domain.h @@ -125,6 +125,8 @@ GVirConfigDomainInterface *gvir_designer_domain_add_interface_network(GVirDesign const char *network, GError **error); +GVirConfigDomainSound *gvir_designer_domain_add_sound(GVirDesignerDomain *design, GError **error); + gboolean gvir_designer_domain_setup_resources(GVirDesignerDomain *design, GVirDesignerDomainResources req, GError **error); diff --git a/libvirt-designer/libvirt-designer.sym b/libvirt-designer/libvirt-designer.sym index 1dab447..5edc3d1 100644 --- a/libvirt-designer/libvirt-designer.sym +++ b/libvirt-designer/libvirt-designer.sym @@ -20,6 +20,7 @@ LIBVIRT_DESIGNER_0.0.1 { gvir_designer_domain_add_floppy_file; gvir_designer_domain_add_floppy_device; gvir_designer_domain_add_interface_network; + gvir_designer_domain_add_sound; gvir_designer_domain_setup_resources; gvir_designer_domain_resources_get_type; -- 1.8.1.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list