[libvirt-designer 2/9] Add sound device upon GVirDesignerDomain creation

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



---
 libvirt-designer/libvirt-designer-domain.c | 120 +++++++++++++++++++++++++++++
 1 file changed, 120 insertions(+)

diff --git a/libvirt-designer/libvirt-designer-domain.c b/libvirt-designer/libvirt-designer-domain.c
index c0b0e19..7b1cb33 100644
--- a/libvirt-designer/libvirt-designer-domain.c
+++ b/libvirt-designer/libvirt-designer-domain.c
@@ -58,6 +58,8 @@ typedef enum {
     /* add new type here */
 } GVirDesignerDomainNICType;
 
+static void gvir_designer_domain_add_sound(GVirDesignerDomain *design);
+
 static GQuark
 gvir_designer_domain_error_quark(void)
 {
@@ -389,6 +391,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;
@@ -765,6 +768,7 @@ gvir_designer_domain_setup_guest(GVirDesignerDomain *design,
     gvir_designer_domain_set_lifecycle(design);
     gvir_designer_domain_add_console(design);
     gvir_designer_domain_add_input(design);
+    gvir_designer_domain_add_sound(design);
 
     ret = TRUE;
 cleanup:
@@ -933,6 +937,122 @@ 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;
+    }
+}
+
+
+static void gvir_designer_domain_add_sound(GVirDesignerDomain *design)
+{
+    GVirConfigDomainSound *sound;
+    OsinfoDevice *soundcard;
+    GVirConfigDomainSoundModel model;
+
+    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;
+
+    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));
+    g_object_unref(G_OBJECT(sound));
+}
+
+
 static gchar *
 gvir_designer_domain_next_disk_target(GVirDesignerDomain *design,
                                       GVirConfigDomainDiskBus bus)
-- 
1.8.1.4

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]