/* PipeWire */
/* SPDX-FileCopyrightText: Copyright © 2020 Collabora Ltd. */
/*                         @author George Kiagiadakis <george.kiagiadakis@collabora.com> */
/* SPDX-License-Identifier: MIT */

#include "pipewire/pipewire.h"
#include "pipewire/extensions/session-manager.h"

struct object_data {
	struct spa_hook object_listener;
	struct spa_hook object_methods;
	struct spa_hook proxy_listener;
};

static void proxy_object_destroy(void *_data)
{
	struct object_data *data = _data;
	spa_hook_remove(&data->object_listener);
}

static const struct pw_proxy_events proxy_events = {
	PW_VERSION_PROXY_EVENTS,
	.destroy = proxy_object_destroy,
};

struct pw_proxy *pw_core_endpoint_export(struct pw_core *core,
		const char *type, const struct spa_dict *props, void *object,
		size_t user_data_size)
{
	struct pw_endpoint *endpoint = object;
	struct spa_interface *remote_iface, *local_iface;
	struct pw_proxy *proxy;
	struct object_data *data;

	proxy = pw_core_create_object(core,
				    "endpoint",
				    PW_TYPE_INTERFACE_Endpoint,
				    PW_VERSION_ENDPOINT,
				    props,
				    user_data_size + sizeof(struct object_data));
        if (proxy == NULL)
		return NULL;

	data = pw_proxy_get_user_data(proxy);
	data = SPA_PTROFF(data, user_data_size, struct object_data);

	remote_iface = (struct spa_interface*)proxy;
	local_iface = (struct spa_interface*)endpoint;

	pw_proxy_install_marshal(proxy, true);

	pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data);

	pw_proxy_add_object_listener(proxy, &data->object_methods,
			local_iface->cb.funcs, local_iface->cb.data);
	pw_endpoint_add_listener(endpoint, &data->object_listener,
			remote_iface->cb.funcs, remote_iface->cb.data);

	return proxy;
}

struct pw_proxy *pw_core_endpoint_stream_export(struct pw_core *core,
		const char *type, const struct spa_dict *props, void *object,
		size_t user_data_size)
{
	struct pw_endpoint_stream *endpoint_stream = object;
	struct spa_interface *remote_iface, *local_iface;
	struct pw_proxy *proxy;
	struct object_data *data;

	proxy = pw_core_create_object(core,
				    "endpoint-stream",
				    PW_TYPE_INTERFACE_EndpointStream,
				    PW_VERSION_ENDPOINT_STREAM,
				    props,
				    user_data_size + sizeof(struct object_data));
        if (proxy == NULL)
		return NULL;

	data = pw_proxy_get_user_data(proxy);
	data = SPA_PTROFF(data, user_data_size, struct object_data);

	remote_iface = (struct spa_interface*)proxy;
	local_iface = (struct spa_interface*)endpoint_stream;

	pw_proxy_install_marshal(proxy, true);

	pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data);

	pw_proxy_add_object_listener(proxy, &data->object_methods,
			local_iface->cb.funcs, local_iface->cb.data);
	pw_endpoint_stream_add_listener(endpoint_stream, &data->object_listener,
			remote_iface->cb.funcs, remote_iface->cb.data);

	return proxy;
}

struct pw_proxy *pw_core_endpoint_link_export(struct pw_core *core,
		const char *type, const struct spa_dict *props, void *object,
		size_t user_data_size)
{
	struct pw_endpoint_link *endpoint_link = object;
	struct spa_interface *remote_iface, *local_iface;
	struct pw_proxy *proxy;
	struct object_data *data;

	proxy = pw_core_create_object(core,
				    "endpoint-link",
				    PW_TYPE_INTERFACE_EndpointLink,
				    PW_VERSION_ENDPOINT_LINK,
				    props,
				    user_data_size + sizeof(struct object_data));
        if (proxy == NULL)
		return NULL;

	data = pw_proxy_get_user_data(proxy);
	data = SPA_PTROFF(data, user_data_size, struct object_data);

	remote_iface = (struct spa_interface*)proxy;
	local_iface = (struct spa_interface*)endpoint_link;

	pw_proxy_install_marshal(proxy, true);

	pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data);

	pw_proxy_add_object_listener(proxy, &data->object_methods,
			local_iface->cb.funcs, local_iface->cb.data);
	pw_endpoint_link_add_listener(endpoint_link, &data->object_listener,
			remote_iface->cb.funcs, remote_iface->cb.data);

	return proxy;
}

struct pw_proxy *pw_core_session_export(struct pw_core *core,
		const char *type, const struct spa_dict *props, void *object,
		size_t user_data_size)
{
	struct pw_session *session = object;
	struct spa_interface *remote_iface, *local_iface;
	struct pw_proxy *proxy;
	struct object_data *data;

	proxy = pw_core_create_object(core,
				    "session",
				    PW_TYPE_INTERFACE_Session,
				    PW_VERSION_SESSION,
				    props,
				    user_data_size + sizeof(struct object_data));
        if (proxy == NULL)
		return NULL;

	data = pw_proxy_get_user_data(proxy);
	data = SPA_PTROFF(data, user_data_size, struct object_data);

	remote_iface = (struct spa_interface*)proxy;
	local_iface = (struct spa_interface*)session;

	pw_proxy_install_marshal(proxy, true);

	pw_proxy_add_listener(proxy, &data->proxy_listener, &proxy_events, data);

	pw_proxy_add_object_listener(proxy, &data->object_methods,
			local_iface->cb.funcs, local_iface->cb.data);
	pw_session_add_listener(session, &data->object_listener,
			remote_iface->cb.funcs, remote_iface->cb.data);

	return proxy;
}
