| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2021 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "plugin.h" |
| 10 |
#include "reserve-device.h" |
| 11 |
#include "reserve-device-enums.h" |
| 12 |
#include "../dbus-connection-state.h" |
| 13 |
|
| 14 |
WP_LOG_TOPIC (log_topic_rd, "m-reserve-device") |
| 15 |
|
| 16 |
G_DEFINE_TYPE (WpReserveDevicePlugin, wp_reserve_device_plugin, WP_TYPE_PLUGIN) |
| 17 |
|
| 18 |
enum |
| 19 |
{ |
| 20 |
ACTION_CREATE_RESERVATION, |
| 21 |
ACTION_DESTROY_RESERVATION, |
| 22 |
ACTION_GET_RESERVATION, |
| 23 |
ACTION_GET_DBUS, |
| 24 |
LAST_SIGNAL |
| 25 |
}; |
| 26 |
|
| 27 |
static guint signals[LAST_SIGNAL] = { 0 }; |
| 28 |
|
| 29 |
static void |
| 30 |
rd_unref (gpointer data) |
| 31 |
{ |
| 32 |
WpReserveDevice *rd = data; |
| 33 |
g_signal_emit_by_name (rd, "release"); |
| 34 |
g_object_unref (rd); |
| 35 |
} |
| 36 |
|
| 37 |
static void |
| 38 |
clear_reservation (WpReserveDevicePlugin *self) |
| 39 |
{ |
| 40 |
g_hash_table_remove_all (self->reserve_devices); |
| 41 |
g_clear_object (&self->manager); |
| 42 |
} |
| 43 |
|
| 44 |
static void |
| 45 |
on_dbus_state_changed (GObject * dbus, GParamSpec * spec, |
| 46 |
WpReserveDevicePlugin *self) |
| 47 |
{ |
| 48 |
WpDBusConnectionState state = -1; |
| 49 |
g_object_get (dbus, "state", &state, NULL); |
| 50 |
|
| 51 |
switch (state) { |
| 52 |
case WP_DBUS_CONNECTION_STATE_CONNECTED: { |
| 53 |
g_autoptr (GDBusConnection) conn = NULL; |
| 54 |
|
| 55 |
g_object_get (dbus, "connection", &conn, NULL); |
| 56 |
g_return_if_fail (conn); |
| 57 |
|
| 58 |
self->manager = g_dbus_object_manager_server_new ( |
| 59 |
FDO_RESERVE_DEVICE1_PATH); |
| 60 |
g_dbus_object_manager_server_set_connection (self->manager, conn); |
| 61 |
break; |
| 62 |
} |
| 63 |
|
| 64 |
case WP_DBUS_CONNECTION_STATE_CONNECTING: |
| 65 |
case WP_DBUS_CONNECTION_STATE_CLOSED: |
| 66 |
clear_reservation (self); |
| 67 |
break; |
| 68 |
|
| 69 |
default: |
| 70 |
g_assert_not_reached (); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
static void |
| 75 |
wp_reserve_device_plugin_init (WpReserveDevicePlugin * self) |
| 76 |
{ |
| 77 |
self->reserve_devices = g_hash_table_new_full (g_str_hash, g_str_equal, |
| 78 |
NULL, rd_unref); |
| 79 |
} |
| 80 |
|
| 81 |
static void |
| 82 |
wp_reserve_device_plugin_finalize (GObject * object) |
| 83 |
{ |
| 84 |
WpReserveDevicePlugin *self = WP_RESERVE_DEVICE_PLUGIN (object); |
| 85 |
|
| 86 |
g_clear_pointer (&self->reserve_devices, g_hash_table_unref); |
| 87 |
|
| 88 |
G_OBJECT_CLASS (wp_reserve_device_plugin_parent_class)->finalize (object); |
| 89 |
} |
| 90 |
|
| 91 |
static void |
| 92 |
wp_reserve_device_plugin_enable (WpPlugin * plugin, WpTransition * transition) |
| 93 |
{ |
| 94 |
WpReserveDevicePlugin *self = WP_RESERVE_DEVICE_PLUGIN (plugin); |
| 95 |
g_autoptr (WpCore) core = wp_object_get_core (WP_OBJECT (self)); |
| 96 |
|
| 97 |
self->dbus = wp_plugin_find (core, "dbus-connection"); |
| 98 |
if (!self->dbus) { |
| 99 |
wp_transition_return_error (transition, g_error_new (WP_DOMAIN_LIBRARY, |
| 100 |
WP_LIBRARY_ERROR_INVARIANT, |
| 101 |
"dbus-connection module must be loaded before reserve-device")); |
| 102 |
return; |
| 103 |
} |
| 104 |
|
| 105 |
g_signal_connect_object (self->dbus, "notify::state", |
| 106 |
G_CALLBACK (on_dbus_state_changed), self, 0); |
| 107 |
on_dbus_state_changed (G_OBJECT (self->dbus), NULL, self); |
| 108 |
|
| 109 |
wp_object_update_features (WP_OBJECT (self), WP_PLUGIN_FEATURE_ENABLED, 0); |
| 110 |
} |
| 111 |
|
| 112 |
static void |
| 113 |
wp_reserve_device_plugin_disable (WpPlugin * plugin) |
| 114 |
{ |
| 115 |
WpReserveDevicePlugin *self = WP_RESERVE_DEVICE_PLUGIN (plugin); |
| 116 |
|
| 117 |
clear_reservation (self); |
| 118 |
g_clear_object (&self->dbus); |
| 119 |
|
| 120 |
wp_object_update_features (WP_OBJECT (self), 0, WP_PLUGIN_FEATURE_ENABLED); |
| 121 |
} |
| 122 |
|
| 123 |
static gpointer |
| 124 |
wp_reserve_device_plugin_create_reservation (WpReserveDevicePlugin *self, |
| 125 |
const gchar *name, const gchar *app_name, const gchar *app_dev_name, |
| 126 |
gint priority) |
| 127 |
{ |
| 128 |
WpDBusConnectionState state = WP_DBUS_CONNECTION_STATE_CLOSED; |
| 129 |
g_object_get (self->dbus, "state", &state, NULL); |
| 130 |
|
| 131 |
if (state != WP_DBUS_CONNECTION_STATE_CONNECTED) { |
| 132 |
wp_notice_object (self, "not connected to D-Bus"); |
| 133 |
return NULL; |
| 134 |
} |
| 135 |
|
| 136 |
WpReserveDevice *rd = g_object_new (wp_reserve_device_get_type (), |
| 137 |
"plugin", self, |
| 138 |
"name", name, |
| 139 |
"application-name", app_name, |
| 140 |
"application-device-name", app_dev_name, |
| 141 |
"priority", priority, |
| 142 |
NULL); |
| 143 |
|
| 144 |
/* use rd->name to avoid copying @em name again */ |
| 145 |
g_hash_table_replace (self->reserve_devices, rd->name, rd); |
| 146 |
|
| 147 |
return g_object_ref (rd); |
| 148 |
} |
| 149 |
|
| 150 |
static void |
| 151 |
wp_reserve_device_plugin_destroy_reservation (WpReserveDevicePlugin *self, |
| 152 |
const gchar *name) |
| 153 |
{ |
| 154 |
WpDBusConnectionState state = WP_DBUS_CONNECTION_STATE_CLOSED; |
| 155 |
g_object_get (self->dbus, "state", &state, NULL); |
| 156 |
|
| 157 |
if (state != WP_DBUS_CONNECTION_STATE_CONNECTED) { |
| 158 |
wp_notice_object (self, "not connected to D-Bus"); |
| 159 |
return; |
| 160 |
} |
| 161 |
g_hash_table_remove (self->reserve_devices, name); |
| 162 |
} |
| 163 |
|
| 164 |
static gpointer |
| 165 |
wp_reserve_device_plugin_get_reservation (WpReserveDevicePlugin *self, |
| 166 |
const gchar *name) |
| 167 |
{ |
| 168 |
WpDBusConnectionState state = WP_DBUS_CONNECTION_STATE_CLOSED; |
| 169 |
g_object_get (self->dbus, "state", &state, NULL); |
| 170 |
|
| 171 |
if (state != WP_DBUS_CONNECTION_STATE_CONNECTED) { |
| 172 |
wp_notice_object (self, "not connected to D-Bus"); |
| 173 |
return NULL; |
| 174 |
} |
| 175 |
|
| 176 |
WpReserveDevice *rd = g_hash_table_lookup (self->reserve_devices, name); |
| 177 |
return rd ? g_object_ref (rd) : NULL; |
| 178 |
} |
| 179 |
|
| 180 |
static gpointer |
| 181 |
wp_reserve_device_plugin_get_dbus (WpReserveDevicePlugin *self) |
| 182 |
{ |
| 183 |
return self->dbus ? g_object_ref (self->dbus) : NULL; |
| 184 |
} |
| 185 |
|
| 186 |
static void |
| 187 |
wp_reserve_device_plugin_class_init (WpReserveDevicePluginClass * klass) |
| 188 |
{ |
| 189 |
GObjectClass *object_class = (GObjectClass *) klass; |
| 190 |
WpPluginClass *plugin_class = (WpPluginClass *) klass; |
| 191 |
|
| 192 |
object_class->finalize = wp_reserve_device_plugin_finalize; |
| 193 |
|
| 194 |
plugin_class->enable = wp_reserve_device_plugin_enable; |
| 195 |
plugin_class->disable = wp_reserve_device_plugin_disable; |
| 196 |
|
| 197 |
/** |
| 198 |
* WpReserveDevicePlugin::create-reservation: |
| 199 |
* |
| 200 |
* @brief |
| 201 |
* @em name: |
| 202 |
* @em app_name: |
| 203 |
* @em app_dev_name: |
| 204 |
* @em priority: |
| 205 |
* |
| 206 |
* Returns: (transfer full): the reservation object |
| 207 |
*/ |
| 208 |
signals[ACTION_CREATE_RESERVATION] = g_signal_new_class_handler ( |
| 209 |
"create-reservation", G_TYPE_FROM_CLASS (klass), |
| 210 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 211 |
(GCallback) wp_reserve_device_plugin_create_reservation, |
| 212 |
NULL, NULL, NULL, |
| 213 |
G_TYPE_OBJECT, 4, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_INT); |
| 214 |
|
| 215 |
/** |
| 216 |
* WpReserveDevicePlugin::destroy-reservation: |
| 217 |
* |
| 218 |
* @brief |
| 219 |
* @em name: |
| 220 |
* |
| 221 |
*/ |
| 222 |
signals[ACTION_DESTROY_RESERVATION] = g_signal_new_class_handler ( |
| 223 |
"destroy-reservation", G_TYPE_FROM_CLASS (klass), |
| 224 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 225 |
(GCallback) wp_reserve_device_plugin_destroy_reservation, |
| 226 |
NULL, NULL, NULL, |
| 227 |
G_TYPE_NONE, 1, G_TYPE_STRING); |
| 228 |
|
| 229 |
/** |
| 230 |
* WpReserveDevicePlugin::get-reservation: |
| 231 |
* |
| 232 |
* @brief |
| 233 |
* @em name: |
| 234 |
* |
| 235 |
* Returns: (transfer full): the reservation object |
| 236 |
*/ |
| 237 |
signals[ACTION_GET_RESERVATION] = g_signal_new_class_handler ( |
| 238 |
"get-reservation", G_TYPE_FROM_CLASS (klass), |
| 239 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 240 |
(GCallback) wp_reserve_device_plugin_get_reservation, |
| 241 |
NULL, NULL, NULL, |
| 242 |
G_TYPE_OBJECT, 1, G_TYPE_STRING); |
| 243 |
|
| 244 |
/** |
| 245 |
* WpReserveDevicePlugin::get-dbus: |
| 246 |
* |
| 247 |
* Returns: (transfer full): the dbus object |
| 248 |
*/ |
| 249 |
signals[ACTION_GET_DBUS] = g_signal_new_class_handler ( |
| 250 |
"get-dbus", G_TYPE_FROM_CLASS (klass), |
| 251 |
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION, |
| 252 |
(GCallback) wp_reserve_device_plugin_get_dbus, |
| 253 |
NULL, NULL, NULL, |
| 254 |
G_TYPE_OBJECT, 0); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
WP_PLUGIN_EXPORT GObject * |
| 259 |
wireplumber__module_init (WpCore * core, WpSpaJson * args, GError ** error) |
| 260 |
{ |
| 261 |
return G_OBJECT (g_object_new (wp_reserve_device_plugin_get_type (), |
| 262 |
"name", "reserve-device", |
| 263 |
"core", core, |
| 264 |
NULL)); |
| 265 |
} |