[gnome-boxes] Add devices based on what is supported by the OS



commit 9dcd91be071a45753bf5fbac27e152a1381e479a
Author: Zeeshan Ali (Khattak) <zeeshanak gnome org>
Date:   Thu Dec 22 23:53:33 2011 +0200

    Add devices based on what is supported by the OS
    
    Add video, sound and tablet devices to new VM based on what is supported
    by the OS in question. The information about device support comes from
    libosinfo.
    
    Note that we don't add these devices if OS itself or its support for the device
    in question is unknown and rely on libvirt to fill in the safe defaults for us.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=666739

 configure.ac             |    2 +-
 src/util.vala            |   18 ++++++++++++++++
 src/vm-configurator.vala |   51 ++++++++++++++++++++++++++++++++-------------
 3 files changed, 55 insertions(+), 16 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index c992b9d..8ec8200 100644
--- a/configure.ac
+++ b/configure.ac
@@ -54,7 +54,7 @@ LIBVIRT_GCONFIG_MIN_VERSION=0.0.4
 LIBXML2_MIN_VERSION=2.7.8
 SPICE_GTK_MIN_VERSION=0.7.98
 GUDEV_MIN_VERSION=147
-OSINFO_MIN_VERSION=0.0.3
+OSINFO_MIN_VERSION=0.0.4
 
 PKG_CHECK_MODULES(BOXES, [
   clutter-gtk-1.0 >= $CLUTTER_GTK_MIN_VERSION
diff --git a/src/util.vala b/src/util.vala
index e7e4c43..2495745 100644
--- a/src/util.vala
+++ b/src/util.vala
@@ -253,6 +253,24 @@ namespace Boxes {
         actor.set_position (-1, -1);
     }
 
+    public Osinfo.Device? get_os_device_by_prop (Osinfo.Os? os, string prop_name, string prop_value) {
+        if (os == null)
+            return null;
+
+        var filter = new Osinfo.Filter ();
+        filter.add_constraint (prop_name, prop_value);
+        var devices = os.get_all_devices (null).new_filtered (filter);
+        return (devices.get_length () > 0) ? devices.get_nth (0) as Osinfo.Device : null;
+    }
+
+    public int get_enum_value (string value_nick, Type enum_type) {
+        var enum_class = (EnumClass) enum_type.class_ref ();
+        var val = enum_class.get_value_by_nick (value_nick);
+        return_val_if_fail (val != null, -1);
+
+        return val.value;
+    }
+
     public class Pair<T1,T2> {
         public T1 first;
         public T2 second;
diff --git a/src/vm-configurator.vala b/src/vm-configurator.vala
index 7392062..5aa10d3 100644
--- a/src/vm-configurator.vala
+++ b/src/vm-configurator.vala
@@ -29,11 +29,6 @@ private class Boxes.VMConfigurator {
         set_unattended_disk_config (domain, install_media);
         set_source_media_config (domain, install_media);
 
-        var input = new DomainInput ();
-        input.set_bus (DomainInputBus.USB);
-        input.set_device_type (DomainInputDeviceType.TABLET);
-        domain.add_device (input);
-
         var graphics = new DomainGraphicsSpice ();
         graphics.set_autoport (true);
         if (install_media is UnattendedInstaller) {
@@ -45,6 +40,8 @@ private class Boxes.VMConfigurator {
         domain.add_device (graphics);
 
         set_video_config (domain, install_media);
+        set_sound_config (domain, install_media);
+        set_tablet_config (domain, install_media);
 
         domain.set_lifecycle (DomainLifecycleEvent.ON_POWEROFF, DomainLifecycleAction.DESTROY);
         domain.set_lifecycle (DomainLifecycleEvent.ON_REBOOT, DomainLifecycleAction.DESTROY);
@@ -57,10 +54,6 @@ private class Boxes.VMConfigurator {
         var iface = new DomainInterfaceUser ();
         domain.add_device (iface);
 
-        var sound = new DomainSound ();
-        sound.set_model (DomainSoundModel.AC97);
-        domain.add_device (sound);
-
         return domain;
     }
 
@@ -168,17 +161,45 @@ private class Boxes.VMConfigurator {
     }
 
     private void set_video_config (Domain domain, InstallerMedia install_media) {
-        var video = new DomainVideo ();
+        var device = get_os_device_by_prop (install_media.os, DEVICE_PROP_CLASS, "video");
+        if (device == null)
+            return;
 
-        // FIXME: Should be QXL for every OS. Work-around for a Qemu bug
-        if (install_media is Win7Installer)
-            video.set_model (DomainVideoModel.VGA);
-        else
-            video.set_model (DomainVideoModel.QXL);
+        var video = new DomainVideo ();
+        var model = get_enum_value (device.get_name (), typeof (DomainVideoModel));
+        return_if_fail (model != -1);
+        video.set_model ((DomainVideoModel) model);
 
         domain.add_device (video);
     }
 
+    private void set_sound_config (Domain domain, InstallerMedia install_media) {
+        var device = get_os_device_by_prop (install_media.os, DEVICE_PROP_CLASS, "audio");
+        if (device == null)
+            return;
+
+        var sound = new DomainSound ();
+        var model = get_enum_value (device.get_name (), typeof (DomainSoundModel));
+        return_if_fail (model != -1);
+        sound.set_model ((DomainSoundModel) model);
+
+        domain.add_device (sound);
+    }
+
+    private void set_tablet_config (Domain domain, InstallerMedia install_media) {
+        var device = get_os_device_by_prop (install_media.os, DEVICE_PROP_NAME, "tablet");
+        if (device == null)
+            return;
+
+        var input = new DomainInput ();
+        var bus = get_enum_value (device.get_bus_type (), typeof (DomainInputBus));
+        return_if_fail (bus != -1);
+        input.set_bus ((DomainInputBus) bus);
+        input.set_device_type (DomainInputDeviceType.TABLET);
+
+        domain.add_device (input);
+    }
+
     private StoragePermissions get_default_permissions () {
         var permissions = new StoragePermissions ();
 



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