| 1 |
/* WirePlumber |
| 2 |
* |
| 3 |
* Copyright © 2022 Collabora Ltd. |
| 4 |
* @author George Kiagiadakis <[email protected]> |
| 5 |
* |
| 6 |
* SPDX-License-Identifier: MIT |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "event.h" |
| 10 |
#include "event-dispatcher.h" |
| 11 |
#include "event-hook.h" |
| 12 |
#include "log.h" |
| 13 |
#include "proxy.h" |
| 14 |
|
| 15 |
#include <spa/utils/defs.h> |
| 16 |
#include <spa/utils/list.h> |
| 17 |
|
| 18 |
WP_DEFINE_LOCAL_LOG_TOPIC ("wp-event") |
| 19 |
|
| 20 |
struct _WpEvent |
| 21 |
{ |
| 22 |
grefcount ref; |
| 23 |
GData *datalist; |
| 24 |
GPtrArray *hooks; |
| 25 |
|
| 26 |
/* immutable fields */ |
| 27 |
gint priority; |
| 28 |
WpProperties *properties; |
| 29 |
GObject *source; |
| 30 |
GObject *subject; |
| 31 |
GCancellable *cancellable; |
| 32 |
gchar *name; |
| 33 |
}; |
| 34 |
|
| 35 |
G_DEFINE_BOXED_TYPE (WpEvent, wp_event, wp_event_ref, wp_event_unref) |
| 36 |
|
| 37 |
static gchar * |
| 38 |
form_event_name (WpEvent *e) |
| 39 |
{ |
| 40 |
WpProperties *props = e->properties; |
| 41 |
const gchar *type = wp_properties_get (props, "event.type"); |
| 42 |
const gchar *subject_type = wp_properties_get (props, "event.subject.type"); |
| 43 |
const gchar *metadata_name = wp_properties_get (props, "metadata.name"); |
| 44 |
const gchar *param = wp_properties_get (props, "event.subject.param-id"); |
| 45 |
|
| 46 |
return g_strdup_printf ("<%p>%s%s%s%s%s%s%s", e, (type ? type : ""), |
| 47 |
((type && subject_type) ? "@" : ""), |
| 48 |
(subject_type ? subject_type : ""), |
| 49 |
((subject_type && metadata_name) ? "@" : ""), |
| 50 |
(metadata_name ? metadata_name : ""), |
| 51 |
((param && subject_type) ? "@" : ""), |
| 52 |
(param ? param : "") |
| 53 |
); |
| 54 |
} |
| 55 |
|
| 56 |
/*! |
| 57 |
* \brief Creates a new event |
| 58 |
* \ingroup wpevent |
| 59 |
* \param type the type of the event |
| 60 |
* \param priority the priority of the event |
| 61 |
* \param properties (transfer full)(nullable): properties of the event |
| 62 |
* \param source (transfer none): the source of the event |
| 63 |
* \param subject (transfer none)(nullable): the object that the event is about |
| 64 |
* \return (transfer full): the newly constructed event |
| 65 |
*/ |
| 66 |
WpEvent * |
| 67 |
wp_event_new (const gchar * type, gint priority, WpProperties * properties, |
| 68 |
GObject * source, GObject * subject) |
| 69 |
{ |
| 70 |
WpEvent * self = g_new0 (WpEvent, 1); |
| 71 |
g_ref_count_init (&self->ref); |
| 72 |
g_datalist_init (&self->datalist); |
| 73 |
self->hooks = g_ptr_array_new_with_free_func (g_object_unref); |
| 74 |
|
| 75 |
self->priority = priority; |
| 76 |
self->properties = properties ? |
| 77 |
wp_properties_ensure_unique_owner (properties) : |
| 78 |
wp_properties_new_empty (); |
| 79 |
|
| 80 |
self->source = source ? g_object_ref (source) : NULL; |
| 81 |
self->subject = subject ? g_object_ref (subject) : NULL; |
| 82 |
self->cancellable = g_cancellable_new (); |
| 83 |
|
| 84 |
if (self->subject) { |
| 85 |
/* merge properties from subject */ |
| 86 |
/* PW properties */ |
| 87 |
GParamSpec *pspec = g_object_class_find_property ( |
| 88 |
G_OBJECT_GET_CLASS (self->subject), "properties"); |
| 89 |
if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == WP_TYPE_PROPERTIES) { |
| 90 |
g_autoptr (WpProperties) subj_props = NULL; |
| 91 |
g_object_get (self->subject, "properties", &subj_props, NULL); |
| 92 |
if (subj_props) { |
| 93 |
wp_properties_update (self->properties, subj_props); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/* global properties */ |
| 98 |
pspec = g_object_class_find_property ( G_OBJECT_GET_CLASS (self->subject), |
| 99 |
"global-properties"); |
| 100 |
if (pspec && G_PARAM_SPEC_VALUE_TYPE (pspec) == WP_TYPE_PROPERTIES) { |
| 101 |
g_autoptr (WpProperties) subj_props = NULL; |
| 102 |
g_object_get (self->subject, "global-properties", &subj_props, NULL); |
| 103 |
if (subj_props) { |
| 104 |
wp_properties_update (self->properties, subj_props); |
| 105 |
} |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
wp_properties_set (self->properties, "event.type", type); |
| 110 |
self->name = form_event_name (self); |
| 111 |
|
| 112 |
wp_trace ("event(%s) created", self->name); |
| 113 |
return self; |
| 114 |
} |
| 115 |
|
| 116 |
/*! |
| 117 |
* \brief Gets the name of the event |
| 118 |
* \ingroup wpevent |
| 119 |
* \param self the event |
| 120 |
* \return the event name |
| 121 |
*/ |
| 122 |
const gchar * |
| 123 |
wp_event_get_name(WpEvent *self) |
| 124 |
{ |
| 125 |
g_return_val_if_fail(self != NULL, NULL); |
| 126 |
return self->name; |
| 127 |
} |
| 128 |
|
| 129 |
static void |
| 130 |
wp_event_free (WpEvent * self) |
| 131 |
{ |
| 132 |
g_clear_pointer (&self->hooks, g_ptr_array_unref); |
| 133 |
g_datalist_clear (&self->datalist); |
| 134 |
g_clear_pointer (&self->properties, wp_properties_unref); |
| 135 |
g_clear_object (&self->source); |
| 136 |
g_clear_object (&self->subject); |
| 137 |
g_cancellable_cancel (self->cancellable); |
| 138 |
g_clear_object (&self->cancellable); |
| 139 |
g_free (self->name); |
| 140 |
g_free (self); |
| 141 |
} |
| 142 |
|
| 143 |
WpEvent * |
| 144 |
wp_event_ref (WpEvent * self) |
| 145 |
{ |
| 146 |
g_ref_count_inc (&self->ref); |
| 147 |
return self; |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
wp_event_unref (WpEvent * self) |
| 152 |
{ |
| 153 |
if (g_ref_count_dec (&self->ref)) |
| 154 |
wp_event_free (self); |
| 155 |
} |
| 156 |
|
| 157 |
/*! |
| 158 |
* \brief Gets the priority of the event |
| 159 |
* \ingroup wpevent |
| 160 |
* \param self the event |
| 161 |
* \return the event priority |
| 162 |
*/ |
| 163 |
gint |
| 164 |
wp_event_get_priority (WpEvent * self) |
| 165 |
{ |
| 166 |
g_return_val_if_fail (self != NULL, 0); |
| 167 |
return self->priority; |
| 168 |
} |
| 169 |
|
| 170 |
/*! |
| 171 |
* \brief Gets the properties of the Event |
| 172 |
* \ingroup wpevent |
| 173 |
* \param self the handle |
| 174 |
* \return (transfer full): the properties of the event |
| 175 |
*/ |
| 176 |
WpProperties * |
| 177 |
wp_event_get_properties (WpEvent * self) |
| 178 |
{ |
| 179 |
g_return_val_if_fail(self != NULL, NULL); |
| 180 |
return wp_properties_ref (self->properties); |
| 181 |
} |
| 182 |
|
| 183 |
/*! |
| 184 |
* \brief Gets the Source Object of the Event |
| 185 |
* \ingroup wpevent |
| 186 |
* \param self the handle |
| 187 |
* \return (transfer full): the source of the event |
| 188 |
*/ |
| 189 |
GObject * |
| 190 |
wp_event_get_source (WpEvent * self) |
| 191 |
{ |
| 192 |
g_return_val_if_fail(self != NULL, NULL); |
| 193 |
return self->source ? g_object_ref (self->source) : NULL; |
| 194 |
} |
| 195 |
|
| 196 |
/*! |
| 197 |
* \brief Gets the Subject Object of the Event |
| 198 |
* \ingroup wpevent |
| 199 |
* \param self the handle |
| 200 |
* \return (transfer full): the subject of the event |
| 201 |
*/ |
| 202 |
GObject * |
| 203 |
wp_event_get_subject (WpEvent * self) |
| 204 |
{ |
| 205 |
g_return_val_if_fail(self != NULL, NULL); |
| 206 |
return self->subject ? g_object_ref (self->subject) : NULL; |
| 207 |
} |
| 208 |
|
| 209 |
/*! |
| 210 |
* \brief Returns the internal GCancellable that is used to track whether this |
| 211 |
* event has been stopped by wp_event_stop_processing() |
| 212 |
* \ingroup wpevent |
| 213 |
* \param self the event |
| 214 |
* \return (transfer none): the cancellable |
| 215 |
*/ |
| 216 |
GCancellable * |
| 217 |
wp_event_get_cancellable (WpEvent * self) |
| 218 |
{ |
| 219 |
return self->cancellable; |
| 220 |
} |
| 221 |
|
| 222 |
/*! |
| 223 |
* \brief Stops processing of this event; any further hooks will not be executed |
| 224 |
* from this moment onwards and the event will be discarded from the stack |
| 225 |
* \ingroup wpevent |
| 226 |
* \param self the event |
| 227 |
*/ |
| 228 |
void |
| 229 |
wp_event_stop_processing (WpEvent * self) |
| 230 |
{ |
| 231 |
g_return_if_fail (self != NULL); |
| 232 |
wp_debug ("stopping event(%s)", self->name); |
| 233 |
g_cancellable_cancel (self->cancellable); |
| 234 |
} |
| 235 |
|
| 236 |
static void |
| 237 |
destroy_event_data (gpointer data) |
| 238 |
{ |
| 239 |
g_value_unset ((GValue *) data); |
| 240 |
g_free (data); |
| 241 |
} |
| 242 |
|
| 243 |
/*! |
| 244 |
* \brief Stores \a data on the event, associated with the specified \a key |
| 245 |
* |
| 246 |
* This can be used to exchange arbitrary data between hooks that run for |
| 247 |
* this event. |
| 248 |
* |
| 249 |
* \ingroup wpevent |
| 250 |
* \param self the event |
| 251 |
* \param key the key to associate \a data with |
| 252 |
* \param data (transfer none)(nullable): the data element, or \c NULL to |
| 253 |
* remove any previous data associated with this \a key |
| 254 |
*/ |
| 255 |
void |
| 256 |
wp_event_set_data (WpEvent * self, const gchar * key, const GValue * data) |
| 257 |
{ |
| 258 |
g_return_if_fail (self != NULL); |
| 259 |
g_return_if_fail (key != NULL); |
| 260 |
GValue *data_copy = NULL; |
| 261 |
|
| 262 |
if (data && G_IS_VALUE (data)) { |
| 263 |
data_copy = g_new0 (GValue, 1); |
| 264 |
g_value_init (data_copy, G_VALUE_TYPE (data)); |
| 265 |
g_value_copy (data, data_copy); |
| 266 |
} |
| 267 |
|
| 268 |
g_datalist_set_data_full (&self->datalist, key, data_copy, |
| 269 |
data_copy ? destroy_event_data : NULL); |
| 270 |
} |
| 271 |
|
| 272 |
/*! |
| 273 |
* \brief Gets the data that was previously associated with \a key by |
| 274 |
* wp_event_set_data() |
| 275 |
* \ingroup wpevent |
| 276 |
* \param self the event |
| 277 |
* \param key the key |
| 278 |
* \return (transfer none)(nullable): the data associated with \a key or \c NULL |
| 279 |
*/ |
| 280 |
const GValue * |
| 281 |
wp_event_get_data (WpEvent * self, const gchar * key) |
| 282 |
{ |
| 283 |
g_return_val_if_fail (self != NULL, NULL); |
| 284 |
g_return_val_if_fail (key != NULL, NULL); |
| 285 |
|
| 286 |
return g_datalist_get_data (&self->datalist, key); |
| 287 |
} |
| 288 |
|
| 289 |
/*! |
| 290 |
* \brief Collects all the hooks registered in the \a dispatcher that run for |
| 291 |
* this \a event |
| 292 |
* \ingroup wpevent |
| 293 |
* \param event the event |
| 294 |
* \param dispatcher the event dispatcher |
| 295 |
* \return TRUE if at least one hook has been collected, |
| 296 |
* FALSE if no hooks run for this event or an error occurred |
| 297 |
*/ |
| 298 |
gboolean |
| 299 |
wp_event_collect_hooks (WpEvent * event, WpEventDispatcher * dispatcher) |
| 300 |
{ |
| 301 |
g_autoptr (WpIterator) all_hooks = NULL; |
| 302 |
g_auto (GValue) value = G_VALUE_INIT; |
| 303 |
const gchar *event_type = NULL; |
| 304 |
|
| 305 |
g_return_val_if_fail (event != NULL, FALSE); |
| 306 |
g_return_val_if_fail (WP_IS_EVENT_DISPATCHER (dispatcher), FALSE); |
| 307 |
|
| 308 |
/* Clear all current hooks */ |
| 309 |
g_ptr_array_set_size (event->hooks, 0); |
| 310 |
|
| 311 |
/* Get the event type */ |
| 312 |
event_type = wp_properties_get (event->properties, "event.type"); |
| 313 |
wp_debug_object (dispatcher, "Collecting hooks for event %s with type %s", |
| 314 |
event->name, event_type); |
| 315 |
|
| 316 |
/* Collect hooks that run for this event */ |
| 317 |
all_hooks = wp_event_dispatcher_new_hooks_for_event_type_iterator (dispatcher, |
| 318 |
event_type); |
| 319 |
while (wp_iterator_next (all_hooks, &value)) { |
| 320 |
WpEventHook *hook = g_value_get_object (&value); |
| 321 |
if (wp_event_hook_runs_for_event (hook, event)) { |
| 322 |
g_ptr_array_add (event->hooks, g_object_ref (hook)); |
| 323 |
wp_debug_boxed (WP_TYPE_EVENT, event, "added "WP_OBJECT_FORMAT"(%s)", |
| 324 |
WP_OBJECT_ARGS (hook), wp_event_hook_get_name (hook)); |
| 325 |
} |
| 326 |
g_value_unset (&value); |
| 327 |
} |
| 328 |
|
| 329 |
return event->hooks->len > 0; |
| 330 |
} |
| 331 |
|
| 332 |
/*! |
| 333 |
* \brief Returns an iterator that iterates over all the hooks that were |
| 334 |
* collected by wp_event_collect_hooks() |
| 335 |
* \ingroup wpevent |
| 336 |
* \param event the event |
| 337 |
* \return (transfer full): the new iterator |
| 338 |
*/ |
| 339 |
WpIterator * |
| 340 |
wp_event_new_hooks_iterator (WpEvent * event) |
| 341 |
{ |
| 342 |
GPtrArray *hooks; |
| 343 |
hooks = g_ptr_array_copy (event->hooks, (GCopyFunc) g_object_ref, NULL); |
| 344 |
return wp_iterator_new_ptr_array (hooks, WP_TYPE_EVENT_HOOK); |
| 345 |
|
| 346 |
} |