monitor: remove enum_items

Make the monitor and device emit the info and items when the callbacks
are set. This makes it easier to implement the monitor because there
is much less state to keep around and invalidate etc.
Rework the monitors
Add bluez5 device
This commit is contained in:
Wim Taymans 2018-11-26 12:18:53 +01:00
parent 10d277ce03
commit 57e94f7bc4
15 changed files with 510 additions and 332 deletions

View file

@ -126,8 +126,7 @@ static int emit_device(struct impl *this, snd_ctl_card_info_t *info, snd_pcm_inf
else
factory = &spa_alsa_source_factory;
if (this->callbacks->add)
this->callbacks->add(this->callbacks_data, 0,
this->callbacks->add(this->callbacks_data, 0,
factory,
SPA_TYPE_INTERFACE_Node,
&SPA_DICT_INIT(items, 13));
@ -191,6 +190,10 @@ static int emit_devices(struct impl *this)
return err;
}
static const struct spa_dict_item info_items[] = {
{ "media.class", "Audio/Device" },
};
static int impl_set_callbacks(struct spa_device *device,
const struct spa_device_callbacks *callbacks,
void *data)
@ -204,8 +207,13 @@ static int impl_set_callbacks(struct spa_device *device,
this->callbacks = callbacks;
this->callbacks_data = data;
if (callbacks)
emit_devices(this);
if (callbacks) {
if (callbacks->info)
callbacks->info(data, &SPA_DICT_INIT_ARRAY(info_items));
if (this->callbacks->add)
emit_devices(this);
}
return 0;
}
@ -227,18 +235,8 @@ static int impl_set_param(struct spa_device *device,
return -ENOTSUP;
}
static const struct spa_dict_item info_items[] = {
{ "media.class", "Audio/Device" },
};
static const struct spa_dict info = {
info_items,
SPA_N_ELEMENTS(info_items)
};
static const struct spa_device impl_device = {
SPA_VERSION_DEVICE,
&info,
impl_set_callbacks,
impl_enum_params,
impl_set_param,

View file

@ -44,10 +44,6 @@
extern const struct spa_handle_factory spa_alsa_device_factory;
struct item {
struct udev_device *udevice;
};
struct impl {
struct spa_handle handle;
struct spa_monitor monitor;
@ -60,11 +56,7 @@ struct impl {
struct udev *udev;
struct udev_monitor *umonitor;
struct udev_enumerate *enumerate;
uint32_t index;
struct udev_list_entry *devices;
struct item uitem;
uint32_t cards[MAX_CARDS];
int n_cards;
@ -72,14 +64,20 @@ struct impl {
};
static int impl_udev_open(struct impl *this)
{
if (this->udev == NULL) {
this->udev = udev_new();
if (this->udev == NULL)
return -ENOMEM;
}
return 0;
}
static int impl_udev_close(struct impl *this)
{
if (this->udev != NULL)
return 0;
this->udev = udev_new();
if (this->udev == NULL)
return -ENOMEM;
udev_unref(this->udev);
this->udev = NULL;
return 0;
}
@ -112,17 +110,11 @@ static const char *path_get_card_id(const char *path)
return e + 5;
}
static int fill_item(struct impl *this, struct item *item, struct udev_device *dev,
static int fill_item(struct impl *this, struct udev_device *dev,
struct spa_pod **result, struct spa_pod_builder *builder)
{
const char *str, *name;
if (item->udevice)
udev_device_unref(item->udevice);
item->udevice = dev;
if (dev == NULL)
return 0;
name = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL_ENC");
@ -263,16 +255,27 @@ static int need_notify(struct impl *this, struct udev_device *dev, uint32_t id,
return 1;
}
static int emit_device(struct impl *this, uint32_t id, struct udev_device *dev)
{
uint8_t buffer[4096];
struct spa_pod_builder b = { NULL, };
struct spa_event *event;
struct spa_pod *item;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
fill_item(this, dev, &item, &b);
this->callbacks->event(this->callbacks_data, event);
return 0;
}
static void impl_on_fd_events(struct spa_source *source)
{
struct impl *this = source->data;
struct udev_device *dev;
const char *action;
uint32_t id;
uint8_t buffer[4096];
struct spa_pod_builder b = { NULL };
struct spa_event *event;
struct spa_pod *item;
dev = udev_monitor_receive_device(this->umonitor);
if (dev == NULL)
@ -292,13 +295,75 @@ static void impl_on_fd_events(struct spa_source *source)
} else
return;
if (!need_notify(this, dev, id, false))
return;
if (need_notify(this, dev, id, false))
emit_device(this, id, dev);
spa_pod_builder_init(&b, buffer, sizeof(buffer));
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
if (fill_item(this, &this->uitem, dev, &item, &b) == 1)
this->callbacks->event(this->callbacks_data, event);
udev_device_unref(dev);
}
static int start_monitor(struct impl *this)
{
if (this->umonitor != NULL)
return 0;
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
if (this->umonitor == NULL)
return -ENOMEM;
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor,
"sound", NULL);
udev_monitor_enable_receiving(this->umonitor);
this->source.func = impl_on_fd_events;
this->source.data = this;
this->source.fd = udev_monitor_get_fd(this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_loop_add_source(this->main_loop, &this->source);
return 0;
}
static int stop_monitor(struct impl *this)
{
if (this->umonitor == NULL)
return 0;
spa_loop_remove_source(this->main_loop, &this->source);
udev_monitor_unref(this->umonitor);
this->umonitor = NULL;
return 0;
}
static int enum_devices(struct impl *this)
{
struct udev_enumerate *enumerate;
struct udev_list_entry *devices;
enumerate = udev_enumerate_new(this->udev);
if (enumerate == NULL)
return -ENOMEM;
udev_enumerate_add_match_subsystem(enumerate, "sound");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
while (devices) {
struct udev_device *dev;
dev = udev_device_new_from_syspath(this->udev, udev_list_entry_get_name(devices));
if (need_notify(this, dev, SPA_MONITOR_EVENT_Added, true))
emit_device(this, SPA_MONITOR_EVENT_Added, dev);
udev_device_unref(dev);
devices = udev_list_entry_get_next(devices);
}
udev_enumerate_unref(enumerate);
return 0;
}
static int
@ -320,90 +385,22 @@ impl_monitor_set_callbacks(struct spa_monitor *monitor,
if ((res = impl_udev_open(this)) < 0)
return res;
if (this->umonitor)
udev_monitor_unref(this->umonitor);
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
if (this->umonitor == NULL)
return -ENODEV;
if ((res = enum_devices(this)) < 0)
return res;
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor, "sound", NULL);
udev_monitor_enable_receiving(this->umonitor);
this->source.func = impl_on_fd_events;
this->source.data = this;
this->source.fd = udev_monitor_get_fd(this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_loop_add_source(this->main_loop, &this->source);
if ((res = start_monitor(this)) < 0)
return res;
} else {
spa_loop_remove_source(this->main_loop, &this->source);
stop_monitor(this);
impl_udev_close(this);
}
return 0;
}
static int impl_monitor_enum_items(struct spa_monitor *monitor,
uint32_t *index,
struct spa_pod **item,
struct spa_pod_builder *builder)
{
int res;
struct impl *this;
struct udev_device *dev;
spa_return_val_if_fail(monitor != NULL, -EINVAL);
spa_return_val_if_fail(item != NULL, -EINVAL);
this = SPA_CONTAINER_OF(monitor, struct impl, monitor);
if ((res = impl_udev_open(this)) < 0)
return res;
if (*index == 0 || this->index > *index) {
if (this->enumerate)
udev_enumerate_unref(this->enumerate);
this->enumerate = udev_enumerate_new(this->udev);
udev_enumerate_add_match_subsystem(this->enumerate, "sound");
udev_enumerate_scan_devices(this->enumerate);
this->devices = udev_enumerate_get_list_entry(this->enumerate);
this->index = 0;
}
while (*index > this->index && this->devices) {
next:
this->devices = udev_list_entry_get_next(this->devices);
this->index++;
}
if (this->devices == NULL) {
fill_item(this, &this->uitem, NULL, item, builder);
return 0;
}
dev = udev_device_new_from_syspath(this->udev,
udev_list_entry_get_name(this->devices));
if (dev == NULL)
goto next;
if (!need_notify(this, dev, SPA_MONITOR_EVENT_Added, true))
goto next;
if (fill_item(this, &this->uitem, dev, item, builder) != 1)
goto next;
this->devices = udev_list_entry_get_next(this->devices);
this->index++;
(*index)++;
return 1;
}
static const struct spa_monitor impl_monitor = {
SPA_VERSION_MONITOR,
NULL,
impl_monitor_set_callbacks,
impl_monitor_enum_items,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
@ -426,16 +423,7 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
static int impl_clear(struct spa_handle *handle)
{
struct impl *this = (struct impl *) handle;
if (this->uitem.udevice)
udev_device_unref(this->uitem.udevice);
if (this->enumerate)
udev_enumerate_unref(this->enumerate);
if (this->umonitor)
udev_monitor_unref(this->umonitor);
if (this->udev)
udev_unref(this->udev);
impl_monitor_set_callbacks(&this->monitor, NULL, NULL);
return 0;
}

View file

@ -0,0 +1,238 @@
/* Spa ALSA Device
*
* Copyright © 2018 Wim Taymans
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
#include <stddef.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <poll.h>
#include <errno.h>
#include <spa/support/log.h>
#include <spa/utils/type.h>
#include <spa/support/loop.h>
#include <spa/support/plugin.h>
#include <spa/monitor/device.h>
#define NAME "bluez5-device"
#define MAX_DEVICES 64
extern const struct spa_handle_factory spa_a2dp_sink_factory;
static const char default_device[] = "";
struct props {
char device[64];
};
static void reset_props(struct props *props)
{
strncpy(props->device, default_device, 64);
}
struct impl {
struct spa_handle handle;
struct spa_device device;
struct spa_log *log;
struct spa_loop *main_loop;
const struct spa_device_callbacks *callbacks;
void *callbacks_data;
struct props props;
struct spa_bt_transport *transport;
};
static int emit_devices(struct impl *this)
{
struct spa_dict_item items[1];
char transport[16];
snprintf(transport, 16, "%p", this->transport);
items[0] = SPA_DICT_ITEM_INIT("bluez5.transport", transport);
this->callbacks->add(this->callbacks_data, 0,
&spa_a2dp_sink_factory,
SPA_TYPE_INTERFACE_Node,
&SPA_DICT_INIT(items, 1));
return 0;
}
static const struct spa_dict_item info_items[] = {
{ "media.class", "Audio/Device" },
};
static int impl_set_callbacks(struct spa_device *device,
const struct spa_device_callbacks *callbacks,
void *data)
{
struct impl *this;
spa_return_val_if_fail(device != NULL, -EINVAL);
this = SPA_CONTAINER_OF(device, struct impl, device);
this->callbacks = callbacks;
this->callbacks_data = data;
if (callbacks) {
if (callbacks->info)
callbacks->info(data, &SPA_DICT_INIT_ARRAY(info_items));
if (this->callbacks->add)
emit_devices(this);
}
return 0;
}
static int impl_enum_params(struct spa_device *device,
uint32_t id, uint32_t *index,
const struct spa_pod *filter,
struct spa_pod **param,
struct spa_pod_builder *builder)
{
return -ENOTSUP;
}
static int impl_set_param(struct spa_device *device,
uint32_t id, uint32_t flags,
const struct spa_pod *param)
{
return -ENOTSUP;
}
static const struct spa_device impl_device = {
SPA_VERSION_DEVICE,
impl_set_callbacks,
impl_enum_params,
impl_set_param,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
{
struct impl *this;
spa_return_val_if_fail(handle != NULL, -EINVAL);
spa_return_val_if_fail(interface != NULL, -EINVAL);
this = (struct impl *) handle;
if (type == SPA_TYPE_INTERFACE_Device)
*interface = &this->device;
else
return -ENOENT;
return 0;
}
static int impl_clear(struct spa_handle *handle)
{
return 0;
}
static size_t
impl_get_size(const struct spa_handle_factory *factory,
const struct spa_dict *params)
{
return sizeof(struct impl);
}
static int
impl_init(const struct spa_handle_factory *factory,
struct spa_handle *handle,
const struct spa_dict *info,
const struct spa_support *support,
uint32_t n_support)
{
struct impl *this;
uint32_t i;
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(handle != NULL, -EINVAL);
handle->get_interface = impl_get_interface;
handle->clear = impl_clear;
this = (struct impl *) handle;
for (i = 0; i < n_support; i++) {
if (support[i].type == SPA_TYPE_INTERFACE_Log)
this->log = support[i].data;
else if (support[i].type == SPA_TYPE_INTERFACE_MainLoop)
this->main_loop = support[i].data;
}
if (this->main_loop == NULL) {
spa_log_error(this->log, "a main-loop is needed");
return -EINVAL;
}
for (i = 0; info && i < info->n_items; i++) {
if (strcmp(info->items[i].key, "bluez5.transport") == 0)
sscanf(info->items[i].value, "%p", &this->transport);
}
if (this->transport == NULL) {
spa_log_error(this->log, "a transport is needed");
return -EINVAL;
}
this->device = impl_device;
reset_props(&this->props);
return 0;
}
static const struct spa_interface_info impl_interfaces[] = {
{SPA_TYPE_INTERFACE_Device,},
};
static int
impl_enum_interface_info(const struct spa_handle_factory *factory,
const struct spa_interface_info **info,
uint32_t *index)
{
spa_return_val_if_fail(factory != NULL, -EINVAL);
spa_return_val_if_fail(info != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
if (*index >= SPA_N_ELEMENTS(impl_interfaces))
return 0;
*info = &impl_interfaces[(*index)++];
return 1;
}
const struct spa_handle_factory spa_bluez5_device_factory = {
SPA_VERSION_HANDLE_FACTORY,
NAME,
NULL,
impl_get_size,
impl_init,
impl_enum_interface_info,
};

View file

@ -70,7 +70,7 @@ struct spa_bt_monitor {
struct spa_list transport_list;
};
struct spa_handle_factory spa_a2dp_sink_factory;
struct spa_handle_factory spa_bluez5_device_factory;
static inline void add_dict(struct spa_pod_builder *builder, const char *key, const char *val)
{
@ -90,7 +90,9 @@ static void fill_item(struct spa_bt_monitor *this, struct spa_bt_transport *tran
SPA_MONITOR_ITEM_state, &SPA_POD_Id(SPA_MONITOR_ITEM_STATE_Available),
SPA_MONITOR_ITEM_name, &SPA_POD_Stringv(transport->path),
SPA_MONITOR_ITEM_class, &SPA_POD_Stringc("Adapter/Bluetooth"),
SPA_MONITOR_ITEM_factory, &SPA_POD_Pointer(SPA_TYPE_INTERFACE_HandleFactory, &spa_a2dp_sink_factory),
SPA_MONITOR_ITEM_factory, &SPA_POD_Pointer(SPA_TYPE_INTERFACE_HandleFactory,
&spa_bluez5_device_factory),
SPA_MONITOR_ITEM_type, &SPA_POD_Id(SPA_TYPE_INTERFACE_Device),
0);
spa_pod_builder_prop(builder, SPA_MONITOR_ITEM_info, 0);
@ -1266,21 +1268,9 @@ impl_monitor_set_callbacks(struct spa_monitor *monitor,
return 0;
}
static int
impl_monitor_enum_items(struct spa_monitor *monitor, uint32_t *index,
struct spa_pod **item, struct spa_pod_builder *builder)
{
spa_return_val_if_fail(monitor != NULL, -EINVAL);
spa_return_val_if_fail(item != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
return 0;
}
static const struct spa_monitor impl_monitor = {
SPA_VERSION_MONITOR,
NULL,
impl_monitor_set_callbacks,
impl_monitor_enum_items,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)

View file

@ -1,6 +1,7 @@
bluez5_sources = ['plugin.c',
'a2dp-sink.c',
'bluez5-device.c',
'bluez5-monitor.c']
bluez5lib = shared_library('spa-bluez5',

View file

@ -69,6 +69,10 @@ struct impl {
struct spa_v4l2_device dev;
};
static const struct spa_dict_item info_items[] = {
{ "media.class", "Video/Device" },
};
static int impl_set_callbacks(struct spa_device *device,
const struct spa_device_callbacks *callbacks,
void *data)
@ -84,14 +88,17 @@ static int impl_set_callbacks(struct spa_device *device,
this->callbacks_data = data;
if (callbacks) {
if (spa_v4l2_is_capture(&this->dev)) {
items[0] = SPA_DICT_ITEM_INIT("device.path", this->props.device);
if (callbacks->info)
callbacks->info(data, &SPA_DICT_INIT_ARRAY(info_items));
if (callbacks->add)
if (callbacks->add) {
if (spa_v4l2_is_capture(&this->dev)) {
items[0] = SPA_DICT_ITEM_INIT("device.path", this->props.device);
callbacks->add(data, 0,
&spa_v4l2_source_factory,
SPA_TYPE_INTERFACE_Node,
&SPA_DICT_INIT(items, 1));
}
}
@ -116,18 +123,8 @@ static int impl_set_param(struct spa_device *device,
return -ENOTSUP;
}
static const struct spa_dict_item info_items[] = {
{ "media.class", "Video/Device" },
};
static const struct spa_dict info = {
info_items,
SPA_N_ELEMENTS(info_items)
};
static const struct spa_device impl_device = {
SPA_VERSION_DEVICE,
&info,
impl_set_callbacks,
impl_enum_params,
impl_set_param,

View file

@ -41,10 +41,6 @@
extern const struct spa_handle_factory spa_v4l2_device_factory;
struct item {
struct udev_device *udevice;
};
struct impl {
struct spa_handle handle;
struct spa_monitor monitor;
@ -57,24 +53,24 @@ struct impl {
struct udev *udev;
struct udev_monitor *umonitor;
struct udev_enumerate *enumerate;
uint32_t index;
struct udev_list_entry *devices;
struct item uitem;
struct spa_source source;
};
static int impl_udev_open(struct impl *this)
{
if (this->udev == NULL) {
this->udev = udev_new();
if (this->udev == NULL)
return -ENOMEM;
}
return 0;
}
static int impl_udev_close(struct impl *this)
{
if (this->udev != NULL)
return 0;
this->udev = udev_new();
if (this->udev == NULL)
return -ENOMEM;
udev_unref(this->udev);
this->udev = NULL;
return 0;
}
@ -84,17 +80,11 @@ static inline void add_dict(struct spa_pod_builder *builder, const char *key, co
spa_pod_builder_string(builder, val);
}
static void fill_item(struct impl *this, struct item *item, struct udev_device *dev,
static void fill_item(struct impl *this, struct udev_device *dev,
struct spa_pod **result, struct spa_pod_builder *builder)
{
const char *str, *name;
if (item->udevice)
udev_device_unref(item->udevice);
item->udevice = dev;
if (dev == NULL)
return;
name = udev_device_get_property_value(dev, "ID_V4L_PRODUCT");
if (!(name && *name)) {
name = udev_device_get_property_value(dev, "ID_MODEL_FROM_DATABASE");
@ -174,16 +164,27 @@ static void fill_item(struct impl *this, struct item *item, struct udev_device *
*result = spa_pod_builder_pop(builder);
}
static int emit_device(struct impl *this, uint32_t id, struct udev_device *dev)
{
uint8_t buffer[4096];
struct spa_pod_builder b = { NULL, };
struct spa_event *event;
struct spa_pod *item;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
fill_item(this, dev, &item, &b);
this->callbacks->event(this->callbacks_data, event);
return 0;
}
static void impl_on_fd_events(struct spa_source *source)
{
struct impl *this = source->data;
struct udev_device *dev;
struct spa_event *event;
const char *action;
uint32_t id;
struct spa_pod_builder b = { NULL, };
uint8_t buffer[4096];
struct spa_pod *item;
dev = udev_monitor_receive_device(this->umonitor);
if (dev == NULL)
@ -201,13 +202,76 @@ static void impl_on_fd_events(struct spa_source *source)
} else
return;
spa_pod_builder_init(&b, buffer, sizeof(buffer));
event = spa_pod_builder_object(&b, SPA_TYPE_EVENT_Monitor, id, 0);
fill_item(this, &this->uitem, dev, &item, &b);
emit_device(this, id, dev);
this->callbacks->event(this->callbacks_data, event);
udev_device_unref(dev);
}
static int start_monitor(struct impl *this)
{
if (this->umonitor != NULL)
return 0;
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
if (this->umonitor == NULL)
return -ENOMEM;
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor,
"video4linux", NULL);
udev_monitor_enable_receiving(this->umonitor);
this->source.func = impl_on_fd_events;
this->source.data = this;
this->source.fd = udev_monitor_get_fd(this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_loop_add_source(this->main_loop, &this->source);
return 0;
}
static int stop_monitor(struct impl *this)
{
if (this->umonitor == NULL)
return 0;
spa_loop_remove_source(this->main_loop, &this->source);
udev_monitor_unref(this->umonitor);
this->umonitor = NULL;
return 0;
}
static int enum_devices(struct impl *this)
{
struct udev_enumerate *enumerate;
struct udev_list_entry *devices;
enumerate = udev_enumerate_new(this->udev);
if (enumerate == NULL)
return -ENOMEM;
udev_enumerate_add_match_subsystem(enumerate, "video4linux");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
while (devices) {
struct udev_device *dev;
dev = udev_device_new_from_syspath(this->udev, udev_list_entry_get_name(devices));
emit_device(this, SPA_MONITOR_EVENT_Added, dev);
udev_device_unref(dev);
devices = udev_list_entry_get_next(devices);
}
udev_enumerate_unref(enumerate);
return 0;
}
static int
impl_monitor_set_callbacks(struct spa_monitor *monitor,
const struct spa_monitor_callbacks *callbacks,
@ -226,84 +290,21 @@ impl_monitor_set_callbacks(struct spa_monitor *monitor,
if ((res = impl_udev_open(this)) < 0)
return res;
if (this->umonitor)
udev_monitor_unref(this->umonitor);
this->umonitor = udev_monitor_new_from_netlink(this->udev, "udev");
if (this->umonitor == NULL)
return -ENOMEM;
if ((res = enum_devices(this)) < 0)
return res;
udev_monitor_filter_add_match_subsystem_devtype(this->umonitor,
"video4linux", NULL);
udev_monitor_enable_receiving(this->umonitor);
this->source.func = impl_on_fd_events;
this->source.data = this;
this->source.fd = udev_monitor_get_fd(this->umonitor);;
this->source.mask = SPA_IO_IN | SPA_IO_ERR;
spa_loop_add_source(this->main_loop, &this->source);
if ((res = start_monitor(this)) < 0)
return res;
} else {
spa_loop_remove_source(this->main_loop, &this->source);
stop_monitor(this);
impl_udev_close(this);
}
return 0;
}
static int
impl_monitor_enum_items(struct spa_monitor *monitor, uint32_t *index,
struct spa_pod **item, struct spa_pod_builder *builder)
{
int res;
struct impl *this;
struct udev_device *dev;
spa_return_val_if_fail(monitor != NULL, -EINVAL);
spa_return_val_if_fail(item != NULL, -EINVAL);
spa_return_val_if_fail(index != NULL, -EINVAL);
this = SPA_CONTAINER_OF(monitor, struct impl, monitor);
if ((res = impl_udev_open(this)) < 0)
return res;
if (*index == 0) {
if (this->enumerate)
udev_enumerate_unref(this->enumerate);
this->enumerate = udev_enumerate_new(this->udev);
udev_enumerate_add_match_subsystem(this->enumerate, "video4linux");
udev_enumerate_scan_devices(this->enumerate);
this->devices = udev_enumerate_get_list_entry(this->enumerate);
this->index = 0;
}
while (*index > this->index && this->devices) {
this->devices = udev_list_entry_get_next(this->devices);
this->index++;
}
if (this->devices == NULL) {
fill_item(this, &this->uitem, NULL, item, builder);
return 0;
}
dev = udev_device_new_from_syspath(this->udev, udev_list_entry_get_name(this->devices));
fill_item(this, &this->uitem, dev, item, builder);
if (dev == NULL)
return 0;
this->devices = udev_list_entry_get_next(this->devices);
this->index++;
(*index)++;
return 1;
}
static const struct spa_monitor impl_monitor = {
SPA_VERSION_MONITOR,
NULL,
impl_monitor_set_callbacks,
impl_monitor_enum_items,
};
static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **interface)
@ -326,16 +327,7 @@ static int impl_get_interface(struct spa_handle *handle, uint32_t type, void **i
static int impl_clear(struct spa_handle *handle)
{
struct impl *this = (struct impl *) handle;
if (this->uitem.udevice)
udev_device_unref(this->uitem.udevice);
if (this->enumerate)
udev_enumerate_unref(this->enumerate);
if (this->umonitor)
udev_monitor_unref(this->umonitor);
if (this->udev)
udev_unref(this->udev);
impl_monitor_set_callbacks(&this->monitor, NULL, NULL);
return 0;
}