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_DEVICE_H
26 #define PIPEWIRE_DEVICE_H
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 #include <spa/utils/defs.h>
33 #include <spa/utils/hook.h>
34 
35 #include <pipewire/proxy.h>
36 
37 #define PW_TYPE_INTERFACE_Device	PW_TYPE_INFO_INTERFACE_BASE "Device"
38 
39 #define PW_VERSION_DEVICE		3
40 struct pw_device;
41 
42 /** The device information. Extra information can be added in later versions \memberof pw_introspect */
43 struct pw_device_info {
44 	uint32_t id;			/**< id of the global */
45 #define PW_DEVICE_CHANGE_MASK_PROPS	(1 << 0)
46 #define PW_DEVICE_CHANGE_MASK_PARAMS	(1 << 1)
47 #define PW_DEVICE_CHANGE_MASK_ALL	((1 << 2)-1)
48 	uint64_t change_mask;		/**< bitfield of changed fields since last call */
49 	struct spa_dict *props;		/**< extra properties */
50 	struct spa_param_info *params;	/**< parameters */
51 	uint32_t n_params;		/**< number of items in \a params */
52 };
53 
54 /** Update and existing \ref pw_device_info with \a update \memberof pw_introspect */
55 struct pw_device_info *
56 pw_device_info_update(struct pw_device_info *info,
57 		      const struct pw_device_info *update);
58 
59 /** Free a \ref pw_device_info \memberof pw_introspect */
60 void pw_device_info_free(struct pw_device_info *info);
61 
62 #define PW_DEVICE_EVENT_INFO	0
63 #define PW_DEVICE_EVENT_PARAM	1
64 #define PW_DEVICE_EVENT_NUM	2
65 
66 /** Device events */
67 struct pw_device_events {
68 #define PW_VERSION_DEVICE_EVENTS	0
69 	uint32_t version;
70 	/**
71 	 * Notify device info
72 	 *
73 	 * \param info info about the device
74 	 */
75 	void (*info) (void *object, const struct pw_device_info *info);
76 	/**
77 	 * Notify a device param
78 	 *
79 	 * Event emitted as a result of the enum_params method.
80 	 *
81 	 * \param seq the sequence number of the request
82 	 * \param id the param id
83 	 * \param index the param index
84 	 * \param next the param index of the next param
85 	 * \param param the parameter
86 	 */
87 	void (*param) (void *object, int seq,
88 		      uint32_t id, uint32_t index, uint32_t next,
89 		      const struct spa_pod *param);
90 };
91 
92 
93 #define PW_DEVICE_METHOD_ADD_LISTENER		0
94 #define PW_DEVICE_METHOD_SUBSCRIBE_PARAMS	1
95 #define PW_DEVICE_METHOD_ENUM_PARAMS		2
96 #define PW_DEVICE_METHOD_SET_PARAM		3
97 #define PW_DEVICE_METHOD_NUM			4
98 
99 /** Device methods */
100 struct pw_device_methods {
101 #define PW_VERSION_DEVICE_METHODS	0
102 	uint32_t version;
103 
104 	int (*add_listener) (void *object,
105 			struct spa_hook *listener,
106 			const struct pw_device_events *events,
107 			void *data);
108 	/**
109 	 * Subscribe to parameter changes
110 	 *
111 	 * Automatically emit param events for the given ids when
112 	 * they are changed.
113 	 *
114 	 * \param ids an array of param ids
115 	 * \param n_ids the number of ids in \a ids
116 	 */
117 	int (*subscribe_params) (void *object, uint32_t *ids, uint32_t n_ids);
118 
119 	/**
120 	 * Enumerate device parameters
121 	 *
122 	 * Start enumeration of device parameters. For each param, a
123 	 * param event will be emitted.
124 	 *
125 	 * \param seq a sequence number to place in the reply
126 	 * \param id the parameter id to enum or PW_ID_ANY for all
127 	 * \param start the start index or 0 for the first param
128 	 * \param num the maximum number of params to retrieve
129 	 * \param filter a param filter or NULL
130 	 */
131 	int (*enum_params) (void *object, int seq, uint32_t id, uint32_t start, uint32_t num,
132 			    const struct spa_pod *filter);
133 	/**
134 	 * Set a parameter on the device
135 	 *
136 	 * \param id the parameter id to set
137 	 * \param flags extra parameter flags
138 	 * \param param the parameter to set
139 	 */
140 	int (*set_param) (void *object, uint32_t id, uint32_t flags,
141 			  const struct spa_pod *param);
142 };
143 
144 #define pw_device_method(o,method,version,...)				\
145 ({									\
146 	int _res = -ENOTSUP;						\
147 	spa_interface_call_res((struct spa_interface*)o,		\
148 			struct pw_device_methods, _res,			\
149 			method, version, ##__VA_ARGS__);		\
150 	_res;								\
151 })
152 
153 #define pw_device_add_listener(c,...)		pw_device_method(c,add_listener,0,__VA_ARGS__)
154 #define pw_device_subscribe_params(c,...)	pw_device_method(c,subscribe_params,0,__VA_ARGS__)
155 #define pw_device_enum_params(c,...)		pw_device_method(c,enum_params,0,__VA_ARGS__)
156 #define pw_device_set_param(c,...)		pw_device_method(c,set_param,0,__VA_ARGS__)
157 
158 #ifdef __cplusplus
159 }  /* extern "C" */
160 #endif
161 
162 #endif /* PIPEWIRE_DEVICE_H */
163