Skip to contentSkip to conversation

vl: Simplify vga interface configuration code

PATCH In review Eduardo Habkost 2 participants 7 messages Apr 14, 2016

lore
Share & tools
lore
#0 lore raw

v2 cover vl: Simplify vga interface configuration code

Instead of duplicating class lookup and strstart() code for each VGA interface type, use a table to implement select_vgahw().

Changes v1 -> v2:
* Keep assert(vga_interface_type == VGA_NONE) line

* Fix spacing on select_vgahw() definition

* Include "vl: Use exit(1) when requested VGA interface is unavailable" in the series, also

Eduardo Habkost (3):
vl: Use exit(1) when requested VGA interface is unavailable
vl: Table-based select_vgahw()
vl: Replace *_vga_available() functions with class_names field

 include/sysemu/sysemu.h |   1 +
 vl.c                    | 157 ++++++++++++++++++++++--------------------------
 2 files changed, 73 insertions(+), 85 deletions(-)
Signature · 1 line
2.1.0
#1 lore raw

PATCH v2 1/3 vl: Use exit(1) when requested VGA interface is unavailable

Instead of using exit(0), use exit(1) when an unavailable VGA interface is used in the command-line to indicate it's an error.

---
 vl.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
vl.c +7 -7
index 9df534f..3c565bb 100644
@@ -2025,28 +2025,28 @@ static void select_vgahw (const char *p)
             vga_interface_type = VGA_STD;
         } else {
             error_report("standard VGA not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "cirrus", &opts)) {
         if (cirrus_vga_available()) {
             vga_interface_type = VGA_CIRRUS;
         } else {
             error_report("Cirrus VGA not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "vmware", &opts)) {
         if (vmware_vga_available()) {
             vga_interface_type = VGA_VMWARE;
         } else {
             error_report("VMWare SVGA not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "virtio", &opts)) {
         if (virtio_vga_available()) {
             vga_interface_type = VGA_VIRTIO;
         } else {
             error_report("Virtio VGA not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "xenfb", &opts)) {
         vga_interface_type = VGA_XENFB;
@@ -2055,21 +2055,21 @@ static void select_vgahw (const char *p)
             vga_interface_type = VGA_QXL;
         } else {
             error_report("QXL VGA not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "tcx", &opts)) {
         if (tcx_vga_available()) {
             vga_interface_type = VGA_TCX;
         } else {
             error_report("TCX framebuffer not available");
-            exit(0);
+            exit(1);
         }
     } else if (strstart(p, "cg3", &opts)) {
         if (cg3_vga_available()) {
             vga_interface_type = VGA_CG3;
         } else {
             error_report("CG3 framebuffer not available");
-            exit(0);
+            exit(1);
         }
     } else if (!strstart(p, "none", &opts)) {
     invalid_vga:
Signature · 1 line
2.1.0
#3 lore raw

PATCH v2 2/3 vl: Table-based select_vgahw()

In reply to Eduardo Habkost

Instead of implementing separate check functions for each vga interface type, add a table enumerating the possible VGA interfaces.

---
Changes v1 -> v2:
* Keep assert(vga_interface_type == VGA_NONE) line

* Fix spacing on select_vgahw() definition

---
 include/sysemu/sysemu.h |   1 +
 vl.c                    | 115 ++++++++++++++++++++++++++----------------------
 2 files changed, 64 insertions(+), 52 deletions(-)
include/sysemu/sysemu.h +1 -0
index 38fb3ca..2033f8c 100644
@@ -146,6 +146,7 @@ extern int autostart;
 typedef enum {
     VGA_NONE, VGA_STD, VGA_CIRRUS, VGA_VMWARE, VGA_XENFB, VGA_QXL,
     VGA_TCX, VGA_CG3, VGA_DEVICE, VGA_VIRTIO,
+    VGA_TYPE_MAX,
 } VGAInterfaceType;
 
 extern int vga_interface_type;
vl.c +63 -52

126 lines not on this page · raw message

Signature · 1 line
2.1.0
#5 lore raw

PATCH v2 3/3 vl: Replace *_vga_available() functions with class_names field

In reply to Eduardo Habkost

Instead of requiring a separate function for each VGA interface, just enumerate the corresponding class names on struct VGAInterfaceInfo.

---
 vl.c | 70 +++++++++++++++++++++++---------------------------------------------
 1 file changed, 23 insertions(+), 47 deletions(-)
vl.c +23 -47
index b99f22f..36593c9 100644
@@ -1979,46 +1979,12 @@ static const QEMUOption qemu_options[] = {
     { NULL },
 };
 
-static bool vga_available(void)
-{
-    return object_class_by_name("VGA") || object_class_by_name("isa-vga");
-}
-
-static bool cirrus_vga_available(void)
-{
-    return object_class_by_name("cirrus-vga")
-           || object_class_by_name("isa-cirrus-vga");
-}
-
-static bool vmware_vga_available(void)
-{
-    return object_class_by_name("vmware-svga");
-}
-
-static bool qxl_vga_available(void)
-{
-    return object_class_by_name("qxl-vga");
-}
-
-static bool tcx_vga_available(void)
-{
-    return object_class_by_name("SUNW,tcx");
-}
-
-static bool cg3_vga_available(void)
-{
-    return object_class_by_name("cgthree");
-}
-
-static bool virtio_vga_available(void)
-{
-    return object_class_by_name("virtio-vga");
-}
-
 typedef struct VGAInterfaceInfo {
     const char *opt_name;    /* option name */
     const char *name;        /* human-readable name */
-    bool (*available)(void);
+    /* Class names indicating that support is available.
+     * If no class is specified, the interface is always available */
+    const char *class_names[2];
 } VGAInterfaceInfo;
 
 static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
@@ -2028,43 +1994,53 @@ static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
     [VGA_STD] = {
         .opt_name = "std",
         .name = "standard VGA",
-        .available = vga_available,
+        .class_names = { "VGA", "isa-vga" },
     },
     [VGA_CIRRUS] = {
         .opt_name = "cirrus",
         .name = "Cirrus VGA",
-        .available = cirrus_vga_available,
+        .class_names = { "cirrus-vga", "isa-cirrus-vga" },
     },
     [VGA_VMWARE] = {
         .opt_name = "vmware",
         .name = "VMWare SVGA",
-        .available = vmware_vga_available,
+        .class_names = { "vmware-svga" },
     },
     [VGA_VIRTIO] = {
         .opt_name = "virtio",
         .name = "Virtio VGA",
-        .available = virtio_vga_available,
+        .class_names = { "virtio-vga" },
     },
     [VGA_QXL] = {
         .opt_name = "qxl",
         .name = "QXL VGA",
-        .available = qxl_vga_available,
+        .class_names = { "qxl-vga" },
     },
     [VGA_TCX] = {
         .opt_name = "tcx",
         .name = "TCX framebuffer",
-        .available = tcx_vga_available,
+        .class_names = { "SUNW,tcx" },
     },
     [VGA_CG3] = {
         .opt_name = "cg3",
         .name = "CG3 framebuffer",
-        .available = cg3_vga_available,
+        .class_names = { "cgthree" },
     },
     [VGA_XENFB] = {
         .opt_name = "xenfb",
     },
 };
 
+static bool vga_interface_available(VGAInterfaceType t)
+{
+    VGAInterfaceInfo *ti = &vga_interfaces[t];
+
+    assert(t < VGA_TYPE_MAX);
+    return !ti->class_names[0] ||
+           object_class_by_name(ti->class_names[0]) ||
+           object_class_by_name(ti->class_names[1]);
+}
+
 static void select_vgahw(const char *p)
 {
     const char *opts;
@@ -2074,7 +2050,7 @@ static void select_vgahw(const char *p)
     for (t = 0; t < VGA_TYPE_MAX; t++) {
         VGAInterfaceInfo *ti = &vga_interfaces[t];
         if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
-            if (ti->available && !ti->available()) {
+            if (!vga_interface_available(t)) {
                 error_report("%s not available", ti->name);
                 exit(1);
             }
@@ -4492,9 +4468,9 @@ int main(int argc, char **argv, char **envp)
     if (default_vga) {
         if (machine_class->default_display) {
             vga_model = machine_class->default_display;
-        } else if (cirrus_vga_available()) {
+        } else if (vga_interface_available(VGA_CIRRUS)) {
             vga_model = "cirrus";
-        } else if (vga_available()) {
+        } else if (vga_interface_available(VGA_STD)) {
             vga_model = "std";
         }
     }
Signature · 1 line
2.1.0
Reviewed-by on v2 1/3
#2 lore raw

Re: [Qemu-devel] [PATCH for-2.7 v2 1/3] vl: Use exit(1) when requested VGA interface is unavailable

In reply to Eduardo Habkost
Quoted 62 lines · Eduardo Habkost

Instead of using exit(0), use exit(1) when an unavailable VGA interface is used in the command-line to indicate it's an error.

---
  vl.c | 14 +++++++-------
  1 file changed, 7 insertions(+), 7 deletions(-)
vl.c +6 -6
index 9df534f..3c565bb 100644
@@ -2025,28 +2025,28 @@ static void select_vgahw (const char *p)
              vga_interface_type = VGA_STD;
          } else {
              error_report("standard VGA not available");
-            exit(0);
+            exit(1);
          }
      } else if (strstart(p, "cirrus", &opts)) {
          if (cirrus_vga_available()) {
              vga_interface_type = VGA_CIRRUS;
          } else {
              error_report("Cirrus VGA not available");
-            exit(0);
+            exit(1);
          }
      } else if (strstart(p, "vmware", &opts)) {
          if (vmware_vga_available()) {
              vga_interface_type = VGA_VMWARE;
          } else {
              error_report("VMWare SVGA not available");
-            exit(0);
+            exit(1);
          }
      } else if (strstart(p, "virtio", &opts)) {
          if (virtio_vga_available()) {
              vga_interface_type = VGA_VIRTIO;
          } else {
              error_report("Virtio VGA not available");
-            exit(0);
+            exit(1);
          }
      } else if (strstart(p, "xenfb", &opts)) {
          vga_interface_type = VGA_XENFB;
@@ -2055,21 +2055,21 @@ static void select_vgahw (const char *p)
              vga_interface_type = VGA_QXL;
          } else {
              error_report("QXL VGA not available");
-            exit(0);
+            exit(1);
          }
      } else if (strstart(p, "tcx", &opts)) {
          if (tcx_vga_available()) {
              vga_interface_type = VGA_TCX;
          } else {
              error_report("TCX framebuffer not available");
-            exit(0);
+            exit(1);
          }

vl.c static void select_vgahw (const char *p) · line 2075

Quoted 10 lines
vl.c +1 -1
      } else if (strstart(p, "cg3", &opts)) {
          if (cg3_vga_available()) {
              vga_interface_type = VGA_CG3;
          } else {
              error_report("CG3 framebuffer not available");
-            exit(0);
+            exit(1);
          }
      } else if (!strstart(p, "none", &opts)) {
      invalid_vga:

Thanks,
Marcel

Reviewed-by on v2 2/3
#4 lore raw

Re: [Qemu-devel] [PATCH for-2.7 v2 2/3] vl: Table-based select_vgahw()

In reply to Eduardo Habkost
Quoted 151 lines · Eduardo Habkost

Instead of implementing separate check functions for each vga interface type, add a table enumerating the possible VGA interfaces.

---
Changes v1 -> v2:
* Keep assert(vga_interface_type == VGA_NONE) line

* Fix spacing on select_vgahw() definition

---
  include/sysemu/sysemu.h |   1 +
  vl.c                    | 115 ++++++++++++++++++++++++++----------------------
  2 files changed, 64 insertions(+), 52 deletions(-)
include/sysemu/sysemu.h +1 -0
index 38fb3ca..2033f8c 100644
@@ -146,6 +146,7 @@ extern int autostart;
  typedef enum {
      VGA_NONE, VGA_STD, VGA_CIRRUS, VGA_VMWARE, VGA_XENFB, VGA_QXL,
      VGA_TCX, VGA_CG3, VGA_DEVICE, VGA_VIRTIO,
+    VGA_TYPE_MAX,
  } VGAInterfaceType;
 
  extern int vga_interface_type;
vl.c +58 -51
index 3c565bb..b99f22f 100644
@@ -2015,63 +2015,74 @@ static bool virtio_vga_available(void)
      return object_class_by_name("virtio-vga");
  }
 
-static void select_vgahw (const char *p)
+typedef struct VGAInterfaceInfo {
+    const char *opt_name;    /* option name */
+    const char *name;        /* human-readable name */
+    bool (*available)(void);
+} VGAInterfaceInfo;
+
+static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
+    [VGA_NONE] = {
+        .opt_name = "none",
+    },
+    [VGA_STD] = {
+        .opt_name = "std",
+        .name = "standard VGA",
+        .available = vga_available,
+    },
+    [VGA_CIRRUS] = {
+        .opt_name = "cirrus",
+        .name = "Cirrus VGA",
+        .available = cirrus_vga_available,
+    },
+    [VGA_VMWARE] = {
+        .opt_name = "vmware",
+        .name = "VMWare SVGA",
+        .available = vmware_vga_available,
+    },
+    [VGA_VIRTIO] = {
+        .opt_name = "virtio",
+        .name = "Virtio VGA",
+        .available = virtio_vga_available,
+    },
+    [VGA_QXL] = {
+        .opt_name = "qxl",
+        .name = "QXL VGA",
+        .available = qxl_vga_available,
+    },
+    [VGA_TCX] = {
+        .opt_name = "tcx",
+        .name = "TCX framebuffer",
+        .available = tcx_vga_available,
+    },
+    [VGA_CG3] = {
+        .opt_name = "cg3",
+        .name = "CG3 framebuffer",
+        .available = cg3_vga_available,
+    },
+    [VGA_XENFB] = {
+        .opt_name = "xenfb",
+    },
+};
+
+static void select_vgahw(const char *p)
  {
      const char *opts;
+    int t;
 
      assert(vga_interface_type == VGA_NONE);
-    if (strstart(p, "std", &opts)) {
-        if (vga_available()) {
-            vga_interface_type = VGA_STD;
-        } else {
-            error_report("standard VGA not available");
-            exit(1);
-        }
-    } else if (strstart(p, "cirrus", &opts)) {
-        if (cirrus_vga_available()) {
-            vga_interface_type = VGA_CIRRUS;
-        } else {
-            error_report("Cirrus VGA not available");
-            exit(1);
-        }
-    } else if (strstart(p, "vmware", &opts)) {
-        if (vmware_vga_available()) {
-            vga_interface_type = VGA_VMWARE;
-        } else {
-            error_report("VMWare SVGA not available");
-            exit(1);
-        }
-    } else if (strstart(p, "virtio", &opts)) {
-        if (virtio_vga_available()) {
-            vga_interface_type = VGA_VIRTIO;
-        } else {
-            error_report("Virtio VGA not available");
-            exit(1);
-        }
-    } else if (strstart(p, "xenfb", &opts)) {
-        vga_interface_type = VGA_XENFB;
-    } else if (strstart(p, "qxl", &opts)) {
-        if (qxl_vga_available()) {
-            vga_interface_type = VGA_QXL;
-        } else {
-            error_report("QXL VGA not available");
-            exit(1);
-        }
-    } else if (strstart(p, "tcx", &opts)) {
-        if (tcx_vga_available()) {
-            vga_interface_type = VGA_TCX;
-        } else {
-            error_report("TCX framebuffer not available");
-            exit(1);
-        }
-    } else if (strstart(p, "cg3", &opts)) {
-        if (cg3_vga_available()) {
-            vga_interface_type = VGA_CG3;
-        } else {
-            error_report("CG3 framebuffer not available");
-            exit(1);
+    for (t = 0; t < VGA_TYPE_MAX; t++) {
+        VGAInterfaceInfo *ti = &vga_interfaces[t];
+        if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
+            if (ti->available && !ti->available()) {
+                error_report("%s not available", ti->name);
+                exit(1);

vl.c static bool virtio_vga_available(void) · line 2088

Quoted 10 lines
vl.c +5 -1
+            }
+            vga_interface_type = t;
+            break;
          }
-    } else if (!strstart(p, "none", &opts)) {
+    }
+    if (t == VGA_TYPE_MAX) {
      invalid_vga:
          error_report("unknown vga type: %s", p);
          exit(1);

Thanks,
Marcel

Reviewed-by on v2 3/3
#6 lore raw

Re: [Qemu-devel] [PATCH for-2.7 v2 3/3] vl: Replace *_vga_available() functions with class_names field

In reply to Eduardo Habkost
Quoted 137 lines · Eduardo Habkost

Instead of requiring a separate function for each VGA interface, just enumerate the corresponding class names on struct VGAInterfaceInfo.

---
  vl.c | 70 +++++++++++++++++++++++---------------------------------------------
  1 file changed, 23 insertions(+), 47 deletions(-)
vl.c +21 -45
index b99f22f..36593c9 100644
@@ -1979,46 +1979,12 @@ static const QEMUOption qemu_options[] = {
      { NULL },
  };
 
-static bool vga_available(void)
-{
-    return object_class_by_name("VGA") || object_class_by_name("isa-vga");
-}
-
-static bool cirrus_vga_available(void)
-{
-    return object_class_by_name("cirrus-vga")
-           || object_class_by_name("isa-cirrus-vga");
-}
-
-static bool vmware_vga_available(void)
-{
-    return object_class_by_name("vmware-svga");
-}
-
-static bool qxl_vga_available(void)
-{
-    return object_class_by_name("qxl-vga");
-}
-
-static bool tcx_vga_available(void)
-{
-    return object_class_by_name("SUNW,tcx");
-}
-
-static bool cg3_vga_available(void)
-{
-    return object_class_by_name("cgthree");
-}
-
-static bool virtio_vga_available(void)
-{
-    return object_class_by_name("virtio-vga");
-}
-
  typedef struct VGAInterfaceInfo {
      const char *opt_name;    /* option name */
      const char *name;        /* human-readable name */
-    bool (*available)(void);
+    /* Class names indicating that support is available.
+     * If no class is specified, the interface is always available */
+    const char *class_names[2];
  } VGAInterfaceInfo;
 
  static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
@@ -2028,43 +1994,53 @@ static VGAInterfaceInfo vga_interfaces[VGA_TYPE_MAX] = {
      [VGA_STD] = {
          .opt_name = "std",
          .name = "standard VGA",
-        .available = vga_available,
+        .class_names = { "VGA", "isa-vga" },
      },
      [VGA_CIRRUS] = {
          .opt_name = "cirrus",
          .name = "Cirrus VGA",
-        .available = cirrus_vga_available,
+        .class_names = { "cirrus-vga", "isa-cirrus-vga" },
      },
      [VGA_VMWARE] = {
          .opt_name = "vmware",
          .name = "VMWare SVGA",
-        .available = vmware_vga_available,
+        .class_names = { "vmware-svga" },
      },
      [VGA_VIRTIO] = {
          .opt_name = "virtio",
          .name = "Virtio VGA",
-        .available = virtio_vga_available,
+        .class_names = { "virtio-vga" },
      },
      [VGA_QXL] = {
          .opt_name = "qxl",
          .name = "QXL VGA",
-        .available = qxl_vga_available,
+        .class_names = { "qxl-vga" },
      },
      [VGA_TCX] = {
          .opt_name = "tcx",
          .name = "TCX framebuffer",
-        .available = tcx_vga_available,
+        .class_names = { "SUNW,tcx" },
      },
      [VGA_CG3] = {
          .opt_name = "cg3",
          .name = "CG3 framebuffer",
-        .available = cg3_vga_available,
+        .class_names = { "cgthree" },
      },
      [VGA_XENFB] = {
          .opt_name = "xenfb",
      },
  };
 
+static bool vga_interface_available(VGAInterfaceType t)
+{
+    VGAInterfaceInfo *ti = &vga_interfaces[t];
+
+    assert(t < VGA_TYPE_MAX);
+    return !ti->class_names[0] ||
+           object_class_by_name(ti->class_names[0]) ||
+           object_class_by_name(ti->class_names[1]);
+}
+
  static void select_vgahw(const char *p)
  {
      const char *opts;
@@ -2074,7 +2050,7 @@ static void select_vgahw(const char *p)
      for (t = 0; t < VGA_TYPE_MAX; t++) {
          VGAInterfaceInfo *ti = &vga_interfaces[t];
          if (ti->opt_name && strstart(p, ti->opt_name, &opts)) {
-            if (ti->available && !ti->available()) {
+            if (!vga_interface_available(t)) {
                  error_report("%s not available", ti->name);
                  exit(1);
              }
@@ -4492,9 +4468,9 @@ int main(int argc, char **argv, char **envp)
      if (default_vga) {

vl.c int main(int argc, char **argv, char **envp) · line 4476

Quoted 10 lines
vl.c +2 -2
          if (machine_class->default_display) {
              vga_model = machine_class->default_display;
-        } else if (cirrus_vga_available()) {
+        } else if (vga_interface_available(VGA_CIRRUS)) {
              vga_model = "cirrus";
-        } else if (vga_available()) {
+        } else if (vga_interface_available(VGA_STD)) {
              vga_model = "std";
          }
      }

Thanks,
Marcel