monitor: use dynamic types

Make the monitor item a POD object and use dynamic types.
This commit is contained in:
Wim Taymans 2017-03-23 08:54:04 +01:00
parent c44a7c9735
commit 7270986c3a
14 changed files with 360 additions and 248 deletions

View file

@ -39,14 +39,13 @@ extern const SpaHandleFactory spa_alsa_source_factory;
typedef struct _SpaALSAMonitor SpaALSAMonitor;
typedef struct {
SpaMonitorItem item;
SpaMonitorItem *item;
struct udev_device *udevice;
SpaDict info;
SpaDictItem info_items[32];
} ALSAItem;
typedef struct {
uint32_t monitor;
uint32_t handle_factory;
SpaMonitorTypes monitor_types;
} URI;
struct _SpaALSAMonitor {
@ -66,6 +65,7 @@ struct _SpaALSAMonitor {
struct udev_enumerate *enumerate;
uint32_t index;
struct udev_list_entry *devices;
uint8_t item_buffer[4096];
ALSAItem uitem;
@ -107,10 +107,12 @@ static int
fill_item (SpaALSAMonitor *this, ALSAItem *item, struct udev_device *udevice)
{
int err;
uint32_t i;
const char *str;
const char *str, *name;
snd_pcm_t *hndl;
char device[64];
SpaPODBuilder b = { NULL, };
const SpaHandleFactory *factory = NULL;
SpaPODFrame f[3];
if (item->udevice)
udev_device_unref (item->udevice);
@ -148,60 +150,72 @@ fill_item (SpaALSAMonitor *this, ALSAItem *item, struct udev_device *udevice)
spa_log_error (this->log, "CAPTURE open failed: %s", snd_strerror(err));
return -1;
} else {
item->item.factory = &spa_alsa_source_factory;
factory = &spa_alsa_source_factory;
snd_pcm_close (hndl);
}
} else {
item->item.factory = &spa_alsa_sink_factory;
factory = &spa_alsa_sink_factory;
snd_pcm_close (hndl);
}
item->item.id = udev_device_get_syspath (item->udevice);
item->item.flags = 0;
item->item.state = SPA_MONITOR_ITEM_STATE_AVAILABLE;
item->item.klass = "Audio/Device";
item->item.info = &item->info;
name = udev_device_get_property_value (item->udevice, "ID_MODEL_FROM_DATABASE");
if (!(name && *name)) {
name = udev_device_get_property_value (item->udevice, "ID_MODEL_ENC");
if (!(name && *name)) {
name = udev_device_get_property_value (item->udevice, "ID_MODEL");
}
}
if (!(str && *str))
name = "Unknown";
item->info.items = item->info_items;
i = 0;
item->info_items[i].key = "alsa.card";
item->info_items[i++].value = str;
item->info_items[i].key = "udev-probed";
item->info_items[i++].value = "1";
item->info_items[i].key = "device.api";
item->info_items[i++].value = "alsa";
spa_pod_builder_init (&b, this->item_buffer, sizeof (this->item_buffer));
spa_pod_builder_push_object (&b, &f[0], 0, this->uri.monitor_types.MonitorItem);
spa_pod_builder_add (&b,
SPA_POD_PROP (&f[1], this->uri.monitor_types.id, 0, SPA_POD_TYPE_STRING, 1, udev_device_get_syspath (item->udevice)),
SPA_POD_PROP (&f[1], this->uri.monitor_types.flags, 0, SPA_POD_TYPE_INT, 1, 0),
SPA_POD_PROP (&f[1], this->uri.monitor_types.state, 0, SPA_POD_TYPE_INT, 1, SPA_MONITOR_ITEM_STATE_AVAILABLE),
SPA_POD_PROP (&f[1], this->uri.monitor_types.name, 0, SPA_POD_TYPE_STRING, 1, name),
SPA_POD_PROP (&f[1], this->uri.monitor_types.klass, 0, SPA_POD_TYPE_STRING, 1, "Audio/Device"),
SPA_POD_PROP (&f[1], this->uri.monitor_types.factory, 0, SPA_POD_TYPE_POINTER, 1, this->uri.handle_factory,
factory),
0);
spa_pod_builder_add (&b,
SPA_POD_TYPE_PROP, &f[1], this->uri.monitor_types.info, 0,
SPA_POD_TYPE_STRUCT, 1, &f[2], 0);
spa_pod_builder_add (&b,
SPA_POD_TYPE_STRING, "alsa.card", SPA_POD_TYPE_STRING, str,
SPA_POD_TYPE_STRING, "udev-probed", SPA_POD_TYPE_STRING, "1",
SPA_POD_TYPE_STRING, "device.api", SPA_POD_TYPE_STRING, "alsa",
0);
if ((str = udev_device_get_property_value (udevice, "SOUND_CLASS")) && *str) {
item->info_items[i].key = "device.class";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.class", SPA_POD_TYPE_STRING, str, 0);
}
str = udev_device_get_property_value (item->udevice, "ID_PATH");
if (!(str && *str))
str = udev_device_get_syspath (item->udevice);
if (str && *str) {
item->info_items[i].key = "device.bus_path";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.bus_path", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_syspath (item->udevice)) && *str) {
item->info_items[i].key = "sysfs.path";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "sysfs.path", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "ID_ID")) && *str) {
item->info_items[i].key = "udev.id";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "udev.id", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "ID_BUS")) && *str) {
item->info_items[i].key = "device.bus";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.bus", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "SUBSYSTEM")) && *str) {
item->info_items[i].key = "device.subsystem";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.subsystem", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "ID_VENDOR_ID")) && *str) {
item->info_items[i].key = "device.vendor.id";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.vendor.id", SPA_POD_TYPE_STRING, str, 0);
}
str = udev_device_get_property_value (item->udevice, "ID_VENDOR_FROM_DATABASE");
if (!(str && *str)) {
@ -211,36 +225,28 @@ fill_item (SpaALSAMonitor *this, ALSAItem *item, struct udev_device *udevice)
}
}
if (str && *str) {
item->info_items[i].key = "device.vendor.name";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.vendor.name", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "ID_MODEL_ID")) && *str) {
item->info_items[i].key = "device.product.id";
item->info_items[i++].value = str;
}
str = udev_device_get_property_value (item->udevice, "ID_MODEL_FROM_DATABASE");
if (!(str && *str)) {
str = udev_device_get_property_value (item->udevice, "ID_MODEL_ENC");
if (!(str && *str)) {
str = udev_device_get_property_value (item->udevice, "ID_MODEL");
}
}
if (str && *str) {
item->info_items[i].key = "device.product.name";
item->info_items[i++].value = str;
item->item.name = str;
} else {
item->item.name = "Unknown";
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.product.id", SPA_POD_TYPE_STRING, str, 0);
}
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.product.name", SPA_POD_TYPE_STRING, name, 0);
if ((str = udev_device_get_property_value (item->udevice, "ID_SERIAL")) && *str) {
item->info_items[i].key = "device.serial";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.serial", SPA_POD_TYPE_STRING, str, 0);
}
if ((str = udev_device_get_property_value (item->udevice, "SOUND_FORM_FACTOR")) && *str) {
item->info_items[i].key = "device.form_factor";
item->info_items[i++].value = str;
spa_pod_builder_add (&b, SPA_POD_TYPE_STRING, "device.form_factor", SPA_POD_TYPE_STRING, str, 0);
}
item->info.n_items = i;
spa_pod_builder_add (&b,
-SPA_POD_TYPE_STRUCT, &f[2],
-SPA_POD_TYPE_PROP, &f[1],
0);
spa_pod_builder_pop (&b, &f[0]);
item->item = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaMonitorItem);
return 0;
}
@ -250,8 +256,12 @@ alsa_on_fd_events (SpaSource *source)
{
SpaALSAMonitor *this = source->data;
struct udev_device *dev;
SpaEvent *event;
const char *str;
SpaMonitorItem *item;
uint32_t type;
SpaPODBuilder b = { NULL, };
SpaPODFrame f[1];
uint8_t buffer[4096];
dev = udev_monitor_receive_device (this->umonitor);
if (fill_item (this, &this->uitem, dev) < 0)
@ -260,17 +270,21 @@ alsa_on_fd_events (SpaSource *source)
if ((str = udev_device_get_action (dev)) == NULL)
str = "change";
item = &this->uitem.item;
if (strcmp (str, "add") == 0) {
item->event.type = SPA_MONITOR_EVENT_TYPE_ADDED;
type = this->uri.monitor_types.Added;
} else if (strcmp (str, "change") == 0) {
item->event.type = SPA_MONITOR_EVENT_TYPE_CHANGED;
type = this->uri.monitor_types.Changed;
} else if (strcmp (str, "remove") == 0) {
item->event.type = SPA_MONITOR_EVENT_TYPE_REMOVED;
}
item->event.size = sizeof (this->uitem);
this->event_cb (&this->monitor, &item->event, this->user_data);
type = this->uri.monitor_types.Removed;
} else
return;
spa_pod_builder_init (&b, buffer, sizeof (buffer));
spa_pod_builder_object (&b, &f[0], 0, type,
SPA_POD_TYPE_POD, this->uitem.item);
event = SPA_POD_BUILDER_DEREF (&b, f[0].ref, SpaMonitorEvent);
this->event_cb (&this->monitor, event, this->user_data);
}
static SpaResult
@ -364,7 +378,7 @@ again:
this->index++;
*item = &this->uitem.item;
*item = this->uitem.item;
return SPA_RESULT_OK;
}
@ -388,7 +402,7 @@ spa_alsa_monitor_get_interface (SpaHandle *handle,
this = (SpaALSAMonitor *) handle;
if (interface_id == this->uri.monitor)
if (interface_id == this->uri.monitor_types.Monitor)
*interface = &this->monitor;
else
return SPA_RESULT_UNKNOWN_INTERFACE;
@ -436,7 +450,8 @@ alsa_monitor_init (const SpaHandleFactory *factory,
spa_log_error (this->log, "a main-loop is needed");
return SPA_RESULT_ERROR;
}
this->uri.monitor = spa_id_map_get_id (this->map, SPA_MONITOR_URI);
this->uri.handle_factory = spa_id_map_get_id (this->map, SPA_HANDLE_FACTORY_URI);
spa_monitor_types_map (this->map, &this->uri.monitor_types);
this->monitor = alsamonitor;