core: add API to get virtual machine type

This commit is contained in:
Julian Bouzas
2022-05-25 12:45:13 -04:00
parent 46e7c5c144
commit cee5ee7c37
2 changed files with 75 additions and 0 deletions

View File

@@ -16,6 +16,7 @@
#include <spa/utils/result.h> #include <spa/utils/result.h>
#include <spa/debug/types.h> #include <spa/debug/types.h>
#include <spa/support/cpu.h>
/* /*
* Integration between the PipeWire main loop and GMainLoop * Integration between the PipeWire main loop and GMainLoop
@@ -506,6 +507,77 @@ wp_core_get_pw_core (WpCore * self)
return self->pw_core; return self->pw_core;
} }
/*!
* \brief Gets the virtual machine type of the core
*
* \ingroup wpcore
* \param self the core
* \returns (transfer full) (nullable): a comma separated string with all the
* virtual machine types that this core matches, or NULL if the core is not
* running in a virtual machine.
*/
gchar *
wp_core_get_vm_type (WpCore *self)
{
const struct spa_support *support;
uint32_t n_support;
struct spa_cpu *spa_cpu;
uint32_t vm_type;
gchar *res;
gboolean first = TRUE;
struct vm_type_info {
uint32_t type;
const gchar *name;
};
static struct vm_type_info vm_types[] = {
{SPA_CPU_VM_OTHER, "other"},
{SPA_CPU_VM_KVM, "kvm"},
{SPA_CPU_VM_QEMU, "qemu"},
{SPA_CPU_VM_BOCHS, "bochs"},
{SPA_CPU_VM_XEN, "zen"},
{SPA_CPU_VM_UML, "uml"},
{SPA_CPU_VM_VMWARE, "vmware"},
{SPA_CPU_VM_ORACLE, "oracle"},
{SPA_CPU_VM_MICROSOFT, "microsoft"},
{SPA_CPU_VM_ZVM, "zvm"},
{SPA_CPU_VM_PARALLELS, "parallels"},
{SPA_CPU_VM_BHYVE, "bhyve"},
{SPA_CPU_VM_QNX, "qnx"},
{SPA_CPU_VM_ACRN, "acrn"},
{SPA_CPU_VM_POWERVM, "powervm"},
{0, NULL},
};
g_return_val_if_fail (WP_IS_CORE (self), NULL);
g_return_val_if_fail (self->pw_context, NULL);
/* Get spa_cpu */
support = pw_context_get_support (self->pw_context, &n_support);
spa_cpu = spa_support_find (support, n_support, SPA_TYPE_INTERFACE_CPU);
g_return_val_if_fail (spa_cpu, NULL);
/* Just return NULL if CPU is not a VM */
vm_type = spa_cpu_get_vm_type (spa_cpu);
if (vm_type == SPA_CPU_VM_NONE)
return NULL;
/* Otherwise return a string with all matching VM types */
res = g_strdup ("");
for (guint i = 0; vm_types[i].name; i++) {
if (vm_type & vm_types[i].type) {
gchar *tmp = g_strdup_printf ("%s%s%s", res, first ? "": ",",
vm_types[i].name);
g_free (res);
res = tmp;
first = FALSE;
}
}
return res;
}
/*! /*!
* \brief Connects this core to the PipeWire server. * \brief Connects this core to the PipeWire server.
* *

View File

@@ -44,6 +44,9 @@ struct pw_context * wp_core_get_pw_context (WpCore * self);
WP_API WP_API
struct pw_core * wp_core_get_pw_core (WpCore * self); struct pw_core * wp_core_get_pw_core (WpCore * self);
WP_API
gchar *wp_core_get_vm_type (WpCore *self);
WP_API WP_API
gboolean wp_core_load_component (WpCore * self, const gchar * component, gboolean wp_core_load_component (WpCore * self, const gchar * component,
const gchar * type, GVariant * args, GError ** error); const gchar * type, GVariant * args, GError ** error);