1 /* PipeWire
2  *
3  * Copyright © 2019 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <string.h>
26 #include <stdio.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <time.h>
30 
31 #include "config.h"
32 
33 #include <spa/monitor/device.h>
34 #include <spa/monitor/event.h>
35 #include <spa/node/node.h>
36 #include <spa/utils/hook.h>
37 #include <spa/utils/result.h>
38 #include <spa/utils/names.h>
39 #include <spa/utils/keys.h>
40 #include <spa/utils/string.h>
41 #include <spa/pod/builder.h>
42 #include <spa/pod/parser.h>
43 #include <spa/param/props.h>
44 #include <spa/debug/dict.h>
45 #include <spa/debug/pod.h>
46 
47 #include "pipewire/impl.h"
48 #include "media-session.h"
49 
50 /** \page page_media_session_module_bluez_monitor Media Session Module: BlueZ Monitor
51  */
52 
53 #define NAME		"bluez5-monitor"
54 #define SESSION_CONF	"bluez-monitor.conf"
55 #define FEATURES_CONF	"bluez-hardware.conf"
56 
57 PW_LOG_TOPIC_STATIC(mod_topic, "ms.mod." NAME);
58 #define PW_LOG_TOPIC_DEFAULT mod_topic
59 
60 struct device;
61 
62 struct node {
63 	struct impl *impl;
64 	enum pw_direction direction;
65 	struct device *device;
66 	struct spa_list link;
67 	uint32_t id;
68 
69 	struct pw_properties *props;
70 
71 	struct pw_impl_node *adapter;
72 
73 	struct sm_node *snode;
74 };
75 
76 struct device {
77 	struct impl *impl;
78 	struct spa_list link;
79 	uint32_t id;
80 	uint32_t device_id;
81 
82 	int priority;
83 	int profile;
84 
85 	struct pw_properties *props;
86 
87 	struct spa_handle *handle;
88 	struct spa_device *device;
89 	struct spa_hook device_listener;
90 
91 	struct sm_device *sdevice;
92 	struct spa_hook listener;
93 
94 	unsigned int appeared:1;
95 	struct spa_list node_list;
96 };
97 
98 struct impl {
99 	struct sm_media_session *session;
100 	struct spa_hook session_listener;
101 
102 	bool have_info;
103 	bool seat_active;
104 
105 	struct pw_properties *conf;
106 	struct pw_properties *props;
107 
108 	struct spa_handle *handle;
109 
110 	struct spa_device *monitor;
111 	struct spa_hook listener;
112 
113 	struct spa_list device_list;
114 };
115 
bluez5_find_node(struct device * device,uint32_t id)116 static struct node *bluez5_find_node(struct device *device, uint32_t id)
117 {
118 	struct node *node;
119 
120 	spa_list_for_each(node, &device->node_list, link) {
121 		if (node->id == id)
122 			return node;
123 	}
124 	return NULL;
125 }
126 
update_icon_name(struct pw_properties * p,bool is_sink)127 static void update_icon_name(struct pw_properties *p, bool is_sink)
128 {
129 	const char *s, *d = NULL, *bus;
130 
131 	if ((s = pw_properties_get(p, PW_KEY_DEVICE_FORM_FACTOR))) {
132 		if (spa_streq(s, "microphone"))
133 			d = "audio-input-microphone";
134 		else if (spa_streq(s, "webcam"))
135 			d = "camera-web";
136 		else if (spa_streq(s, "computer"))
137 			d = "computer";
138 		else if (spa_streq(s, "handset"))
139 			d = "phone";
140 		else if (spa_streq(s, "portable"))
141 			d = "multimedia-player";
142 		else if (spa_streq(s, "tv"))
143 			d = "video-display";
144 		else if (spa_streq(s, "headset"))
145 			d = "audio-headset";
146 		else if (spa_streq(s, "headphone"))
147 			d = "audio-headphones";
148 		else if (spa_streq(s, "speaker"))
149 			d = "audio-speakers";
150 		else if (spa_streq(s, "hands-free"))
151 			d = "audio-handsfree";
152 	}
153 	if (!d)
154 		if ((s = pw_properties_get(p, PW_KEY_DEVICE_CLASS)))
155 			if (spa_streq(s, "modem"))
156 				d = "modem";
157 
158 	if (!d) {
159 		if (is_sink)
160 			d = "audio-card";
161 		else
162 			d = "audio-input-microphone";
163 	}
164 
165 	if ((s = pw_properties_get(p, "device.profile.name")) != NULL) {
166 		if (strstr(s, "analog"))
167 			s = "-analog";
168 		else if (strstr(s, "iec958"))
169 			s = "-iec958";
170 		else if (strstr(s, "hdmi"))
171 			s = "-hdmi";
172 		else
173 			s = NULL;
174 	}
175 
176 	bus = pw_properties_get(p, PW_KEY_DEVICE_BUS);
177 
178 	pw_properties_setf(p, PW_KEY_DEVICE_ICON_NAME,
179 			"%s%s%s%s", d, s ? s : "", bus ? "-" : "", bus ? bus : "");
180 }
181 
bluez5_update_node(struct device * device,struct node * node,const struct spa_device_object_info * info)182 static void bluez5_update_node(struct device *device, struct node *node,
183 		const struct spa_device_object_info *info)
184 {
185 	pw_log_debug("update node %u", node->id);
186 
187 	if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
188 		spa_debug_dict(0, info->props);
189 }
190 
bluez5_create_node(struct device * device,uint32_t id,const struct spa_device_object_info * info)191 static struct node *bluez5_create_node(struct device *device, uint32_t id,
192 		const struct spa_device_object_info *info)
193 {
194 	struct node *node;
195 	struct impl *impl = device->impl;
196 	struct pw_context *context = impl->session->context;
197 	struct pw_impl_factory *factory;
198 	int res;
199 	const char *prefix, *str, *profile, *rules;
200 	int priority;
201 	char tmp[1024];
202 	bool is_sink;
203 
204 	pw_log_debug("new node %u", id);
205 
206 	if (!spa_streq(info->type, SPA_TYPE_INTERFACE_Node)) {
207 		errno = EINVAL;
208 		return NULL;
209 	}
210 	node = calloc(1, sizeof(*node));
211 	if (node == NULL) {
212 		res = -errno;
213 		goto exit;
214 	}
215 
216 	node->props = pw_properties_new_dict(info->props);
217 
218 	if (pw_properties_get(node->props, PW_KEY_DEVICE_FORM_FACTOR) == NULL)
219 		pw_properties_set(node->props, PW_KEY_DEVICE_FORM_FACTOR,
220 				pw_properties_get(device->props, PW_KEY_DEVICE_FORM_FACTOR));
221 	if (pw_properties_get(node->props, PW_KEY_DEVICE_BUS) == NULL)
222 		pw_properties_set(node->props, PW_KEY_DEVICE_BUS,
223 				pw_properties_get(device->props, PW_KEY_DEVICE_BUS));
224 
225 	str = pw_properties_get(device->props, SPA_KEY_DEVICE_DESCRIPTION);
226 	if (str == NULL)
227 		str = pw_properties_get(device->props, SPA_KEY_DEVICE_NAME);
228 	if (str == NULL)
229 		str = pw_properties_get(device->props, SPA_KEY_DEVICE_NICK);
230 	if (str == NULL)
231 		str = pw_properties_get(device->props, SPA_KEY_DEVICE_ALIAS);
232 	if (str == NULL)
233 		str = "bluetooth-device";
234 
235 	pw_properties_setf(node->props, PW_KEY_DEVICE_ID, "%d", device->device_id);
236 
237 	pw_properties_set(node->props, PW_KEY_NODE_DESCRIPTION,
238 		sm_media_session_sanitize_description(tmp, sizeof(tmp),
239 			' ', "%s", str));
240 
241 	profile = pw_properties_get(node->props, SPA_KEY_API_BLUEZ5_PROFILE);
242 	if (profile == NULL)
243 		profile = "unknown";
244 	str = pw_properties_get(node->props, SPA_KEY_API_BLUEZ5_ADDRESS);
245 	if (str == NULL)
246 		str = pw_properties_get(device->props, SPA_KEY_DEVICE_NAME);
247 
248 	is_sink = strstr(info->factory_name, "sink") != NULL;
249 	if (is_sink)
250 		prefix = "bluez_output";
251 	else if (strstr(info->factory_name, "source") != NULL)
252 		prefix = "bluez_input";
253 	else
254 		prefix = info->factory_name;
255 
256 	pw_properties_set(node->props, PW_KEY_NODE_NAME,
257 		sm_media_session_sanitize_name(tmp, sizeof(tmp),
258 			'_', "%s.%s.%s", prefix, str, profile));
259 
260 	pw_properties_set(node->props, PW_KEY_FACTORY_NAME, info->factory_name);
261 
262 	if (pw_properties_get(node->props, PW_KEY_PRIORITY_DRIVER) == NULL) {
263 		priority = device->priority + 10;
264 
265 		if (strstr(info->factory_name, "source") != NULL)
266 			priority += 1000;
267 
268 		pw_properties_setf(node->props, PW_KEY_PRIORITY_DRIVER, "%d", priority);
269 		pw_properties_setf(node->props, PW_KEY_PRIORITY_SESSION, "%d", priority);
270 	}
271 	if (pw_properties_get(node->props, PW_KEY_DEVICE_ICON_NAME) == NULL)
272 		update_icon_name(node->props, is_sink);
273 
274 	node->impl = impl;
275 	node->device = device;
276 	node->id = id;
277 
278 	if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
279 		sm_media_session_match_rules(rules, strlen(rules), node->props);
280 
281 	factory = pw_context_find_factory(context, "adapter");
282 	if (factory == NULL) {
283 		pw_log_error("no adapter factory found");
284 		res = -EIO;
285 		goto clean_node;
286 	}
287 	node->adapter = pw_impl_factory_create_object(factory,
288 			NULL,
289 			PW_TYPE_INTERFACE_Node,
290 			PW_VERSION_NODE,
291 			pw_properties_copy(node->props),
292 			0);
293 	if (node->adapter == NULL) {
294 		res = -errno;
295 		goto clean_node;
296 	}
297 	node->snode = sm_media_session_export_node(impl->session,
298 			&node->props->dict,
299 			node->adapter);
300 	if (node->snode == NULL) {
301 		res = -errno;
302 		goto clean_node;
303 	}
304 
305 	spa_list_append(&device->node_list, &node->link);
306 
307 	bluez5_update_node(device, node, info);
308 
309 	return node;
310 
311 clean_node:
312 	pw_properties_free(node->props);
313 	free(node);
314 exit:
315 	errno = -res;
316 	return NULL;
317 }
318 
bluez5_remove_node(struct device * device,struct node * node)319 static void bluez5_remove_node(struct device *device, struct node *node)
320 {
321 	pw_log_debug("remove node %u", node->id);
322 	spa_list_remove(&node->link);
323 	sm_object_destroy(&node->snode->obj);
324 	pw_impl_node_destroy(node->adapter);
325 	pw_properties_free(node->props);
326 	free(node);
327 }
328 
bluez5_device_object_info(void * data,uint32_t id,const struct spa_device_object_info * info)329 static void bluez5_device_object_info(void *data, uint32_t id,
330                 const struct spa_device_object_info *info)
331 {
332 	struct device *device = data;
333 	struct node *node;
334 
335 	node = bluez5_find_node(device, id);
336 
337 	if (info == NULL) {
338 		if (node == NULL) {
339 			pw_log_warn("device %p: unknown node %u", device, id);
340 			return;
341 		}
342 		bluez5_remove_node(device, node);
343 	} else if (node == NULL) {
344 		bluez5_create_node(device, id, info);
345 	} else {
346 		bluez5_update_node(device, node, info);
347 	}
348 
349 }
350 
bluez_device_event(void * data,const struct spa_event * event)351 static void bluez_device_event(void *data, const struct spa_event *event)
352 {
353 	struct device *device = data;
354 	struct node *node;
355 	uint32_t id, type;
356 	struct spa_pod *props = NULL;
357 
358 	if (spa_pod_parse_object(&event->pod,
359 			SPA_TYPE_EVENT_Device, &type,
360 			SPA_EVENT_DEVICE_Object, SPA_POD_Int(&id),
361 			SPA_EVENT_DEVICE_Props, SPA_POD_OPT_Pod(&props)) < 0)
362 		return;
363 
364 	if ((node = bluez5_find_node(device, id)) == NULL) {
365 		pw_log_warn("device %p: unknown node %d", device, id);
366 		return;
367 	}
368 
369 	switch (type) {
370 	case SPA_DEVICE_EVENT_ObjectConfig:
371 		if (props != NULL) {
372 			struct spa_node *adapter;
373 			adapter = pw_impl_node_get_implementation(node->adapter);
374 			spa_node_set_param(adapter, SPA_PARAM_Props, 0, props);
375 		}
376 		break;
377 	default:
378 		break;
379 	}
380 }
381 
382 static const struct spa_device_events bluez5_device_events = {
383 	SPA_VERSION_DEVICE_EVENTS,
384 	.object_info = bluez5_device_object_info,
385 	.event = bluez_device_event,
386 };
387 
bluez5_find_device(struct impl * impl,uint32_t id)388 static struct device *bluez5_find_device(struct impl *impl, uint32_t id)
389 {
390 	struct device *device;
391 
392 	spa_list_for_each(device, &impl->device_list, link) {
393 		if (device->id == id)
394 			return device;
395 	}
396 	return NULL;
397 }
398 
update_device_props(struct device * device)399 static int update_device_props(struct device *device)
400 {
401 	struct pw_properties *p = device->props;
402 	const char *s;
403 	char temp[32], tmp[1024];
404 
405 	s = pw_properties_get(p, SPA_KEY_DEVICE_NAME);
406 	if (s == NULL)
407 		s = pw_properties_get(p, SPA_KEY_API_BLUEZ5_ADDRESS);
408 	if (s == NULL)
409 		s = pw_properties_get(p, SPA_KEY_DEVICE_DESCRIPTION);
410 	if (s == NULL) {
411 		snprintf(temp, sizeof(temp), "%d", device->id);
412 		s = temp;
413 	}
414 	if (spa_strstartswith(s, "bluez_card."))
415 		s += strlen("bluez_card.");
416 
417 	pw_properties_set(p, PW_KEY_DEVICE_NAME,
418 			sm_media_session_sanitize_name(tmp, sizeof(tmp),
419 					'_', "bluez_card.%s", s));
420 
421 	if (pw_properties_get(p, SPA_KEY_DEVICE_ICON_NAME) == NULL)
422 		update_icon_name(p, true);
423 
424 	return 0;
425 }
426 
device_destroy(void * data)427 static void device_destroy(void *data)
428 {
429 	struct device *device = data;
430 	struct node *node;
431 
432 	pw_log_debug("device %p destroy", device);
433 
434 	spa_hook_remove(&device->listener);
435 
436 	if (device->appeared) {
437 		device->appeared = false;
438 		spa_hook_remove(&device->device_listener);
439 	}
440 
441 	spa_list_consume(node, &device->node_list, link)
442 		bluez5_remove_node(device, node);
443 }
444 
device_update(void * data)445 static void device_update(void *data)
446 {
447 	struct device *device = data;
448 
449 	pw_log_debug("device %p appeared %d %d", device, device->appeared, device->profile);
450 
451 	if (device->appeared)
452 		return;
453 
454 	device->device_id = device->sdevice->obj.id;
455 	device->appeared = true;
456 
457 	spa_device_add_listener(device->device,
458 		&device->device_listener,
459 		&bluez5_device_events, device);
460 
461 	sm_object_sync_update(&device->sdevice->obj);
462 }
463 
464 static const struct sm_object_events device_events = {
465 	SM_VERSION_OBJECT_EVENTS,
466         .destroy = device_destroy,
467         .update = device_update,
468 };
469 
bluez5_create_device(struct impl * impl,uint32_t id,const struct spa_device_object_info * info)470 static struct device *bluez5_create_device(struct impl *impl, uint32_t id,
471 		const struct spa_device_object_info *info)
472 {
473 	struct pw_context *context = impl->session->context;
474 	struct device *device;
475 	struct spa_handle *handle;
476 	int res;
477 	void *iface;
478 	const char *rules, *str;
479 
480 	pw_log_debug("new device %u", id);
481 
482 	if (!spa_streq(info->type, SPA_TYPE_INTERFACE_Device)) {
483 		errno = EINVAL;
484 		return NULL;
485 	}
486 
487 	device = calloc(1, sizeof(*device));
488 	if (device == NULL) {
489 		res = -errno;
490 		goto exit;
491 	}
492 
493 	device->impl = impl;
494 	device->id = id;
495 	device->priority = 1000;
496 	device->props = pw_properties_new_dict(info->props);
497 	update_device_props(device);
498 
499 	spa_list_init(&device->node_list);
500 
501 	if ((rules = pw_properties_get(impl->conf, "rules")) != NULL)
502 		sm_media_session_match_rules(rules, strlen(rules), device->props);
503 
504 	/* Propagate the msbc-support global property if it exists and is not
505 	 * overloaded by a device specific one */
506 	if ((str = pw_properties_get(impl->props, "bluez5.msbc-support")) != NULL &&
507 	    pw_properties_get(device->props, "bluez5.msbc-support") == NULL)
508 		pw_properties_set(device->props, "bluez5.msbc-support", str);
509 
510 	handle = pw_context_load_spa_handle(context,
511 		info->factory_name,
512 		&device->props->dict);
513 	if (handle == NULL) {
514 		res = -errno;
515 		pw_log_error("can't make factory instance: %m");
516 		goto clean_device;
517 	}
518 
519 	if ((res = spa_handle_get_interface(handle, info->type, &iface)) < 0) {
520 		pw_log_error("can't get %s interface: %s", info->type, spa_strerror(res));
521 		goto unload_handle;
522 	}
523 
524 	device->handle = handle;
525 	device->device = iface;
526 
527 	spa_list_append(&impl->device_list, &device->link);
528 
529 	return device;
530 
531 unload_handle:
532 	pw_unload_spa_handle(handle);
533 clean_device:
534 	pw_properties_free(device->props);
535 	free(device);
536 exit:
537 	errno = -res;
538 	return NULL;
539 }
540 
bluez5_device_free(struct device * device)541 static void bluez5_device_free(struct device *device)
542 {
543 	if (device->sdevice) {
544 		sm_object_destroy(&device->sdevice->obj);
545 		device->sdevice = NULL;
546 	}
547 	spa_list_remove(&device->link);
548 	pw_unload_spa_handle(device->handle);
549 	pw_properties_free(device->props);
550 	free(device);
551 }
552 
bluez5_remove_device(struct impl * impl,struct device * device)553 static void bluez5_remove_device(struct impl *impl, struct device *device)
554 {
555 
556 	pw_log_debug("remove device %u", device->id);
557 	bluez5_device_free(device);
558 }
559 
bluez5_update_device(struct impl * impl,struct device * device,const struct spa_device_object_info * info)560 static void bluez5_update_device(struct impl *impl, struct device *device,
561 		const struct spa_device_object_info *info)
562 {
563 	bool connected;
564 	const char *str;
565 	if (pw_log_level_enabled(SPA_LOG_LEVEL_DEBUG))
566 		spa_debug_dict(0, info->props);
567 
568 	pw_log_debug("update device %u", device->id);
569 
570 	pw_properties_update(device->props, info->props);
571 	update_device_props(device);
572 
573 	str = spa_dict_lookup(info->props, SPA_KEY_API_BLUEZ5_CONNECTION);
574 	connected = str != NULL && spa_streq(str, "connected");
575 
576 	/* Export device after bluez profiles get connected */
577 	if (device->sdevice == NULL && connected) {
578 		device->sdevice = sm_media_session_export_device(impl->session,
579 					&device->props->dict, device->device);
580 		if (device->sdevice == NULL) {
581 			bluez5_device_free(device);
582 			return;
583 		}
584 
585 		sm_object_add_listener(&device->sdevice->obj,
586 				&device->listener,
587 				&device_events, device);
588 	} else if (device->sdevice != NULL && !connected) {
589 		sm_object_destroy(&device->sdevice->obj);
590 		device->sdevice = NULL;
591 	}
592 }
593 
bluez5_enum_object_info(void * data,uint32_t id,const struct spa_device_object_info * info)594 static void bluez5_enum_object_info(void *data, uint32_t id,
595                 const struct spa_device_object_info *info)
596 {
597 	struct impl *impl = data;
598 	struct device *device;
599 
600 	device = bluez5_find_device(impl, id);
601 
602 	if (info == NULL) {
603 		if (device == NULL)
604 			return;
605 		bluez5_remove_device(impl, device);
606 	} else if (device == NULL) {
607 		if (bluez5_create_device(impl, id, info) == NULL)
608 			return;
609 	} else {
610 		bluez5_update_device(impl, device, info);
611 	}
612 }
613 
614 static const struct spa_device_events bluez5_enum_callbacks =
615 {
616 	SPA_VERSION_DEVICE_EVENTS,
617 	.object_info = bluez5_enum_object_info,
618 };
619 
unload_bluez_handle(struct impl * impl)620 static void unload_bluez_handle(struct impl *impl)
621 {
622 	struct device *device;
623 
624 	if (impl->handle == NULL)
625 		return;
626 
627 	spa_list_consume(device, &impl->device_list, link)
628 		bluez5_device_free(device);
629 
630 	spa_hook_remove(&impl->listener);
631 
632 	pw_unload_spa_handle(impl->handle);
633 	impl->handle = NULL;
634 }
635 
load_bluez_handle(struct impl * impl)636 static int load_bluez_handle(struct impl *impl)
637 {
638 	struct pw_context *context = impl->session->context;
639 	void *iface;
640 	int res;
641 
642 	if (impl->handle != NULL || !impl->seat_active || !impl->have_info)
643 		return 0;
644 
645 	impl->handle = pw_context_load_spa_handle(context, SPA_NAME_API_BLUEZ5_ENUM_DBUS, &impl->props->dict);
646 	if (impl->handle == NULL) {
647 		res = -errno;
648 		pw_log_info("can't load %s: %m", SPA_NAME_API_BLUEZ5_ENUM_DBUS);
649 		goto fail;
650 	}
651 	if ((res = spa_handle_get_interface(impl->handle, SPA_TYPE_INTERFACE_Device, &iface)) < 0) {
652 		pw_log_error("can't get Device interface: %s", spa_strerror(res));
653 		goto fail;
654 	}
655 	impl->monitor = iface;
656 
657 	spa_device_add_listener(impl->monitor, &impl->listener,
658 			&bluez5_enum_callbacks, impl);
659 
660 	return 0;
661 
662 fail:
663 	if (impl->handle)
664 		pw_unload_spa_handle(impl->handle);
665 	impl->handle = NULL;
666 	return res;
667 }
668 
session_info(void * data,const struct pw_core_info * info)669 static void session_info(void *data, const struct pw_core_info *info)
670 {
671 	struct impl *impl = data;
672 
673 	if (info && (info->change_mask & PW_CORE_CHANGE_MASK_PROPS)) {
674 		const char *str;
675 
676 		if ((str = spa_dict_lookup(info->props, "default.clock.rate")) != NULL &&
677 		    pw_properties_get(impl->props, "bluez5.default.rate") == NULL) {
678 			pw_properties_set(impl->props, "bluez5.default.rate", str);
679 		}
680 
681 		impl->have_info = true;
682 		load_bluez_handle(impl);
683 	}
684 }
685 
session_destroy(void * data)686 static void session_destroy(void *data)
687 {
688 	struct impl *impl = data;
689 
690 	spa_hook_remove(&impl->session_listener);
691 
692 	unload_bluez_handle(impl);
693 
694 	pw_properties_free(impl->props);
695 	pw_properties_free(impl->conf);
696 	free(impl);
697 }
698 
seat_active(void * data,bool active)699 static void seat_active(void *data, bool active)
700 {
701 	struct impl *impl = data;
702 
703 	impl->seat_active = active;
704 
705 	if (impl->seat_active) {
706 		pw_log_info("seat active, starting bluetooth");
707 		load_bluez_handle(impl);
708 	} else {
709 		pw_log_info("seat not active, stopping bluetooth");
710 		unload_bluez_handle(impl);
711 	}
712 }
713 
714 static const struct sm_media_session_events session_events = {
715 	SM_VERSION_MEDIA_SESSION_EVENTS,
716 	.info = session_info,
717 	.destroy = session_destroy,
718 	.seat_active = seat_active,
719 };
720 
sm_bluez5_monitor_start(struct sm_media_session * session)721 int sm_bluez5_monitor_start(struct sm_media_session *session)
722 {
723 	int res;
724 	struct impl *impl;
725 	const char *str;
726 
727 	PW_LOG_TOPIC_INIT(mod_topic);
728 
729 	impl = calloc(1, sizeof(struct impl));
730 	if (impl == NULL) {
731 		res = -errno;
732 		goto out;
733 	}
734 	impl->session = session;
735 	impl->seat_active = true;
736 
737 	spa_list_init(&impl->device_list);
738 
739 	if ((impl->conf = pw_properties_new(NULL, NULL)) == NULL) {
740 		res = -errno;
741 		goto out_free;
742 	}
743 	if ((res = sm_media_session_load_conf(impl->session,
744 					SESSION_CONF, impl->conf)) < 0)
745 		pw_log_info("can't load "SESSION_CONF" config: %s", spa_strerror(res));
746 
747 	if ((impl->props = pw_properties_new(NULL, NULL)) == NULL) {
748 		res = -errno;
749 		goto out_free;
750 	}
751 	if ((str = pw_properties_get(impl->conf, "properties")) != NULL)
752 		pw_properties_update_string(impl->props, str, strlen(str));
753 
754 	pw_properties_set(impl->props, "api.bluez5.connection-info", "true");
755 
756 	sm_media_session_add_listener(session, &impl->session_listener,
757 			&session_events, impl);
758 	return 0;
759 
760 out_free:
761 	pw_properties_free(impl->conf);
762 	pw_properties_free(impl->props);
763 	free(impl);
764 out:
765 	return res;
766 }
767