| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2019 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "client.h" |
| 10 |
#include "log.h" |
| 11 |
#include "private/pipewire-object-mixin.h" |
| 12 |
|
| 13 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-client") |
| 14 |
|
| 15 |
/*! \defgroup wpclient WpClient */ |
| 16 |
/*! |
| 17 |
* \struct WpClient |
| 18 |
* |
| 19 |
* The WpClient class allows accessing the properties and methods of a PipeWire |
| 20 |
* client object (`struct pw_client`). A WpClient is constructed internally |
| 21 |
* when a new client connects to PipeWire and it is made available through the |
| 22 |
* WpObjectManager API. |
| 23 |
*/ |
| 24 |
|
| 25 |
struct _WpClient |
| 26 |
{ |
| 27 |
WpGlobalProxy parent; |
| 28 |
}; |
| 29 |
|
| 30 |
static void wp_client_pw_object_mixin_priv_interface_init ( |
| 31 |
WpPwObjectMixinPrivInterface * iface); |
| 32 |
|
| 33 |
G_DEFINE_TYPE_WITH_CODE (WpClient, wp_client, WP_TYPE_GLOBAL_PROXY, |
| 34 |
G_IMPLEMENT_INTERFACE (WP_TYPE_PIPEWIRE_OBJECT, |
| 35 |
wp_pw_object_mixin_object_interface_init) |
| 36 |
G_IMPLEMENT_INTERFACE (WP_TYPE_PW_OBJECT_MIXIN_PRIV, |
| 37 |
wp_client_pw_object_mixin_priv_interface_init)) |
| 38 |
|
| 39 |
static void |
| 40 |
wp_client_init (WpClient * self) |
| 41 |
{ |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
wp_client_activate_execute_step (WpObject * object, |
| 46 |
WpFeatureActivationTransition * transition, guint step, |
| 47 |
WpObjectFeatures missing) |
| 48 |
{ |
| 49 |
switch (step) { |
| 50 |
case WP_PW_OBJECT_MIXIN_STEP_BIND: |
| 51 |
case WP_TRANSITION_STEP_ERROR: |
| 52 |
/* base class can handle BIND and ERROR */ |
| 53 |
WP_OBJECT_CLASS (wp_client_parent_class)-> |
| 54 |
activate_execute_step (object, transition, step, missing); |
| 55 |
break; |
| 56 |
case WP_PW_OBJECT_MIXIN_STEP_WAIT_INFO: |
| 57 |
/* just wait, info will be emitted anyway after binding */ |
| 58 |
break; |
| 59 |
default: |
| 60 |
g_assert_not_reached (); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
static const struct pw_client_events client_events = { |
| 65 |
PW_VERSION_CLIENT_EVENTS, |
| 66 |
.info = (HandleEventInfoFunc(client)) wp_pw_object_mixin_handle_event_info, |
| 67 |
}; |
| 68 |
|
| 69 |
static void |
| 70 |
wp_client_pw_proxy_created (WpProxy * proxy, struct pw_proxy * pw_proxy) |
| 71 |
{ |
| 72 |
wp_pw_object_mixin_handle_pw_proxy_created (proxy, pw_proxy, |
| 73 |
client, &client_events); |
| 74 |
} |
| 75 |
|
| 76 |
static void |
| 77 |
wp_client_pw_proxy_destroyed (WpProxy * proxy) |
| 78 |
{ |
| 79 |
wp_pw_object_mixin_handle_pw_proxy_destroyed (proxy); |
| 80 |
|
| 81 |
WP_PROXY_CLASS (wp_client_parent_class)->pw_proxy_destroyed (proxy); |
| 82 |
} |
| 83 |
|
| 84 |
static void |
| 85 |
wp_client_class_init (WpClientClass * klass) |
| 86 |
{ |
| 87 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 88 |
WpObjectClass *wpobject_class = (WpObjectClass *) klass; |
| 89 |
WpProxyClass *proxy_class = (WpProxyClass *) klass; |
| 90 |
|
| 91 |
object_class->get_property = wp_pw_object_mixin_get_property; |
| 92 |
|
| 93 |
wpobject_class->get_supported_features = |
| 94 |
wp_pw_object_mixin_get_supported_features; |
| 95 |
wpobject_class->activate_get_next_step = |
| 96 |
wp_pw_object_mixin_activate_get_next_step; |
| 97 |
wpobject_class->activate_execute_step = wp_client_activate_execute_step; |
| 98 |
|
| 99 |
proxy_class->pw_iface_type = PW_TYPE_INTERFACE_Client; |
| 100 |
proxy_class->pw_iface_version = PW_VERSION_CLIENT; |
| 101 |
proxy_class->pw_proxy_created = wp_client_pw_proxy_created; |
| 102 |
proxy_class->pw_proxy_destroyed = wp_client_pw_proxy_destroyed; |
| 103 |
|
| 104 |
wp_pw_object_mixin_class_override_properties (object_class); |
| 105 |
} |
| 106 |
|
| 107 |
static void |
| 108 |
wp_client_pw_object_mixin_priv_interface_init ( |
| 109 |
WpPwObjectMixinPrivInterface * iface) |
| 110 |
{ |
| 111 |
wp_pw_object_mixin_priv_interface_info_init_no_params (iface, client, CLIENT); |
| 112 |
} |
| 113 |
|
| 114 |
/*! |
| 115 |
* \brief Send an error to the client |
| 116 |
* |
| 117 |
* \ingroup wpclient |
| 118 |
* \param self the client |
| 119 |
* \param id the global id to report the error on |
| 120 |
* \param res an errno style error code |
| 121 |
* \param message the error message string |
| 122 |
*/ |
| 123 |
void |
| 124 |
wp_client_send_error (WpClient * self, guint32 id, int res, |
| 125 |
const gchar * message) |
| 126 |
{ |
| 127 |
struct pw_client *pwp; |
| 128 |
|
| 129 |
g_return_if_fail (WP_IS_CLIENT (self)); |
| 130 |
|
| 131 |
pwp = (struct pw_client *) wp_proxy_get_pw_proxy (WP_PROXY (self)); |
| 132 |
g_return_if_fail (pwp != NULL); |
| 133 |
|
| 134 |
pw_client_error (pwp, id, res, message); |
| 135 |
} |
| 136 |
|
| 137 |
/*! |
| 138 |
* \brief Update client's permissions on a list of objects. |
| 139 |
* |
| 140 |
* An object id of `-1` can be used to set the default object permissions |
| 141 |
* for this client |
| 142 |
* |
| 143 |
* \ingroup wpclient |
| 144 |
* \param self the client |
| 145 |
* \param n_perm the number of permissions specified in the variable arguments |
| 146 |
* \param ... \a n_perm pairs of guint32 numbers; the first number is the |
| 147 |
* object id and the second is the permissions that this client should have |
| 148 |
* on this object |
| 149 |
*/ |
| 150 |
void |
| 151 |
wp_client_update_permissions (WpClient * self, guint n_perm, ...) |
| 152 |
{ |
| 153 |
va_list args; |
| 154 |
struct pw_permission *perm = |
| 155 |
g_alloca (n_perm * sizeof (struct pw_permission)); |
| 156 |
|
| 157 |
va_start (args, n_perm); |
| 158 |
for (guint i = 0; i < n_perm; i++) { |
| 159 |
perm[i].id = va_arg (args, guint32); |
| 160 |
perm[i].permissions = va_arg (args, guint32); |
| 161 |
} |
| 162 |
va_end (args); |
| 163 |
|
| 164 |
wp_client_update_permissions_array (self, n_perm, perm); |
| 165 |
} |
| 166 |
|
| 167 |
/*! |
| 168 |
* \brief Update client's permissions on a list of objects. |
| 169 |
* |
| 170 |
* An object id of `-1` can be used to set the default object permissions |
| 171 |
* for this client |
| 172 |
* |
| 173 |
* \ingroup wpclient |
| 174 |
* \param self the client |
| 175 |
* \param n_perm the number of permissions specified in the \a permissions array |
| 176 |
* \param permissions (array length=n_perm) (element-type pw_permission): an array |
| 177 |
* of permissions per object id |
| 178 |
*/ |
| 179 |
void |
| 180 |
wp_client_update_permissions_array (WpClient * self, |
| 181 |
guint n_perm, const struct pw_permission *permissions) |
| 182 |
{ |
| 183 |
struct pw_client *pwp; |
| 184 |
int client_update_permissions_result; |
| 185 |
|
| 186 |
g_return_if_fail (WP_IS_CLIENT (self)); |
| 187 |
|
| 188 |
pwp = (struct pw_client *) wp_proxy_get_pw_proxy (WP_PROXY (self)); |
| 189 |
g_return_if_fail (pwp != NULL); |
| 190 |
|
| 191 |
client_update_permissions_result = pw_client_update_permissions ( |
| 192 |
pwp, n_perm, permissions); |
| 193 |
g_warn_if_fail (client_update_permissions_result >= 0); |
| 194 |
} |
| 195 |
|
| 196 |
/*! |
| 197 |
* \brief Updates the properties of \a self |
| 198 |
* |
| 199 |
* This requires W and X permissions on the client. |
| 200 |
* |
| 201 |
* \ingroup wpclient |
| 202 |
* \param self the client |
| 203 |
* \param updates (transfer full): updates to apply to the properties of |
| 204 |
* \a self; this does not need to include properties that have not changed |
| 205 |
*/ |
| 206 |
void |
| 207 |
wp_client_update_properties (WpClient * self, WpProperties * updates) |
| 208 |
{ |
| 209 |
g_autoptr (WpProperties) upd = updates; |
| 210 |
struct pw_client *pwp; |
| 211 |
int client_update_properties_result; |
| 212 |
|
| 213 |
g_return_if_fail (WP_IS_CLIENT (self)); |
| 214 |
g_return_if_fail (updates != NULL); |
| 215 |
|
| 216 |
pwp = (struct pw_client *) wp_proxy_get_pw_proxy (WP_PROXY (self)); |
| 217 |
g_return_if_fail (pwp != NULL); |
| 218 |
|
| 219 |
client_update_properties_result = pw_client_update_properties ( |
| 220 |
pwp, wp_properties_peek_dict (upd)); |
| 221 |
|
| 222 |
g_warn_if_fail (client_update_properties_result >= 0); |
| 223 |
} |