1 /* PipeWire
2  *
3  * Copyright © 2018 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 #ifndef PIPEWIRE_RESOURCE_H
26 #define PIPEWIRE_RESOURCE_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <spa/utils/hook.h>
33 
34 /** \page page_resource Resource
35  *
36  * \section sec_page_resource Overview
37  *
38  * Resources represent objects owned by a \ref pw_impl_client. They are
39  * the result of binding to a global resource or by calling API that
40  * creates client owned objects.
41  *
42  * The client usually has a proxy object associated with the resource
43  * that it can use to communicate with the resource. See \ref page_proxy.
44  *
45  * Resources are destroyed when the client or the bound object is
46  * destroyed.
47  *
48  */
49 
50 /** \class pw_resource
51  *
52  * \brief Client owned objects
53  *
54  * Resources are objects owned by a client and are destroyed when the
55  * client disappears.
56  *
57  * See also \ref page_resource
58  */
59 struct pw_resource;
60 
61 #include <pipewire/impl-client.h>
62 
63 /** Resource events */
64 struct pw_resource_events {
65 #define PW_VERSION_RESOURCE_EVENTS	0
66 	uint32_t version;
67 
68 	/** The resource is destroyed */
69 	void (*destroy) (void *data);
70 
71 	/** a reply to a ping event completed */
72         void (*pong) (void *data, int seq);
73 
74 	/** an error occurred on the resource */
75         void (*error) (void *data, int seq, int res, const char *message);
76 };
77 
78 /** Make a new resource for client */
79 struct pw_resource *
80 pw_resource_new(struct pw_impl_client *client,	/**< the client owning the resource */
81 		uint32_t id,			/**< the remote per client id */
82 		uint32_t permissions,		/**< permissions on this resource */
83 		const char *type,		/**< interface of the resource */
84 		uint32_t version,		/**< requested interface version */
85 		size_t user_data_size		/**< extra user data size */);
86 
87 /** Destroy a resource */
88 void pw_resource_destroy(struct pw_resource *resource);
89 
90 /** Remove a resource, like pw_resource_destroy but without sending a
91  * remove_id message to the client */
92 void pw_resource_remove(struct pw_resource *resource);
93 
94 /** Get the client owning this resource */
95 struct pw_impl_client *pw_resource_get_client(struct pw_resource *resource);
96 
97 /** Get the unique id of this resource */
98 uint32_t pw_resource_get_id(struct pw_resource *resource);
99 
100 /** Get the permissions of this resource */
101 uint32_t pw_resource_get_permissions(struct pw_resource *resource);
102 
103 /** Get the type and optionally the version of this resource */
104 const char *pw_resource_get_type(struct pw_resource *resource, uint32_t *version);
105 
106 /** Get the protocol used for this resource */
107 struct pw_protocol *pw_resource_get_protocol(struct pw_resource *resource);
108 
109 /** Get the user data for the resource, the size was given in \ref pw_resource_new */
110 void *pw_resource_get_user_data(struct pw_resource *resource);
111 
112 /** Add an event listener */
113 void pw_resource_add_listener(struct pw_resource *resource,
114 			      struct spa_hook *listener,
115 			      const struct pw_resource_events *events,
116 			      void *data);
117 
118 /** Set the resource implementation. */
119 void pw_resource_add_object_listener(struct pw_resource *resource,
120 				struct spa_hook *listener,
121 				const void *funcs,
122 				void *data);
123 
124 /** Generate an ping event for a resource. This will generate a pong event
125  * with the same \a sequence number in the return value. */
126 int pw_resource_ping(struct pw_resource *resource, int seq);
127 
128 /** Notify global id this resource is bound to */
129 int pw_resource_set_bound_id(struct pw_resource *resource, uint32_t global_id);
130 
131 /** Get the global id this resource is bound to or SPA_ID_INVALID when not bound */
132 uint32_t pw_resource_get_bound_id(struct pw_resource *resource);
133 
134 /** Generate an error for a resource */
135 void pw_resource_error(struct pw_resource *resource, int res, const char *error);
136 void pw_resource_errorf(struct pw_resource *resource, int res, const char *error, ...) SPA_PRINTF_FUNC(3, 4);
137 void pw_resource_errorf_id(struct pw_resource *resource, uint32_t id, int res, const char *error, ...) SPA_PRINTF_FUNC(4, 5);
138 
139 /** Get the list of object listeners from a resource */
140 struct spa_hook_list *pw_resource_get_object_listeners(struct pw_resource *resource);
141 
142 /** Get the marshal functions for the resource */
143 const struct pw_protocol_marshal *pw_resource_get_marshal(struct pw_resource *resource);
144 
145 /** install a marshal function on a resource */
146 int pw_resource_install_marshal(struct pw_resource *resource, bool implementor);
147 
148 #define pw_resource_notify(r,type,event,version,...)			\
149 	spa_hook_list_call(pw_resource_get_object_listeners(r),		\
150 			type, event, version, ## __VA_ARGS__)
151 
152 #define pw_resource_call(r,type,method,version,...)			\
153 	spa_interface_call((struct spa_interface*)r,			\
154 			type, method, version, ##__VA_ARGS__)
155 
156 #define pw_resource_call_res(r,type,method,version,...)			\
157 ({									\
158 	int _res = -ENOTSUP;						\
159 	spa_interface_call_res((struct spa_interface*)r,		\
160 			type, _res, method, version, ##__VA_ARGS__);	\
161 	_res;								\
162 })
163 
164 #ifdef __cplusplus
165 }
166 #endif
167 
168 #endif /* PIPEWIRE_RESOURCE_H */
169