Debian Codegraph @

lib/wp/plugin.c

raw
1 /* WirePlumber
2 *
3 * Copyright © 2020 Collabora Ltd.
4 * @author George Kiagiadakis <[email protected]>
5 *
6 * SPDX-License-Identifier: MIT
7 */
8
9 #include "plugin.h"
10 #include "core.h"
11 #include "log.h"
12
13 WP_DEFINE_LOCAL_LOG_TOPIC ("wp-plugin")
14
15 /*! \defgroup wpplugin WpPlugin */
16 /*!
17 * \struct WpPlugin
18 *
19 * WpPlugin is a base class for objects that provide functionality to the
20 * WirePlumber daemon.
21 *
22 * Typically, a plugin is created within a module and then registered to
23 * make it available for use by the daemon. The daemon is responsible for
24 * calling wp_object_activate() on it after all modules have been loaded,
25 * the core is connected and the initial discovery of global objects is
26 * done.
27 *
28 * Being a WpObject subclass, the plugin inherits WpObject's activation system.
29 * For most implementations, there is only need for activating one
30 * feature, WP_PLUGIN_FEATURE_ENABLED, and this can be done by implementing
31 * only WpPluginClass::enable() and WpPluginClass::disable().
32 * For more advanced plugins that need to have more features, you may
33 * implement directly the functions of WpObjectClass and ignore the ones of
34 * WpPluginClass.
35 *
36 * \gproperties
37 *
38 * \gproperty{name, gchar *, G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY,
39 * The name of this plugin}
40 */
41
42 enum {
43 PROP_0,
44 PROP_NAME,
45 };
46
47 typedef struct _WpPluginPrivate WpPluginPrivate;
48 struct _WpPluginPrivate
49 {
50 GQuark name_quark;
51 };
52
53 G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (WpPlugin, wp_plugin, WP_TYPE_OBJECT)
54
55 static void
56 wp_plugin_init (WpPlugin * self)
57 {
58 }
59
60 static void
61 wp_plugin_set_property (GObject * object, guint property_id,
62 const GValue * value, GParamSpec * pspec)
63 {
64 WpPlugin *self = WP_PLUGIN (object);
65 WpPluginPrivate *priv = wp_plugin_get_instance_private (self);
66
67 switch (property_id) {
68 case PROP_NAME:
69 priv->name_quark = g_quark_from_string (g_value_get_string (value));
70 break;
71 default:
72 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
73 break;
74 }
75 }
76
77 static void
78 wp_plugin_get_property (GObject * object, guint property_id,
79 GValue * value, GParamSpec * pspec)
80 {
81 WpPlugin *self = WP_PLUGIN (object);
82 WpPluginPrivate *priv = wp_plugin_get_instance_private (self);
83
84 switch (property_id) {
85 case PROP_NAME:
86 g_value_set_string (value, g_quark_to_string (priv->name_quark));
87 break;
88 default:
89 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
90 break;
91 }
92 }
93
94 static WpObjectFeatures
95 wp_plugin_get_supported_features (WpObject * self)
96 {
97 return WP_PLUGIN_FEATURE_ENABLED;
98 }
99
100 enum {
101 STEP_ENABLE = WP_TRANSITION_STEP_CUSTOM_START,
102 };
103
104 static guint
105 wp_plugin_activate_get_next_step (WpObject * object,
106 WpFeatureActivationTransition * transition, guint step,
107 WpObjectFeatures missing)
108 {
109 /* we only support ENABLED, so this is the only
110 feature that can be in @em missing */
111 g_return_val_if_fail (missing == WP_PLUGIN_FEATURE_ENABLED,
112 WP_TRANSITION_STEP_ERROR);
113
114 return STEP_ENABLE;
115 }
116
117 static void
118 wp_plugin_activate_execute_step (WpObject * object,
119 WpFeatureActivationTransition * transition, guint step,
120 WpObjectFeatures missing)
121 {
122 switch (step) {
123 case STEP_ENABLE: {
124 WpPlugin *self = WP_PLUGIN (object);
125 wp_info_object (self, "enabling plugin '%s'", wp_plugin_get_name (self));
126 g_return_if_fail (WP_PLUGIN_GET_CLASS (self)->enable);
127 WP_PLUGIN_GET_CLASS (self)->enable (self, WP_TRANSITION (transition));
128 break;
129 }
130 case WP_TRANSITION_STEP_ERROR:
131 break;
132 default:
133 g_assert_not_reached ();
134 }
135 }
136
137 static void
138 wp_plugin_deactivate (WpObject * object, WpObjectFeatures features)
139 {
140 if (features & WP_PLUGIN_FEATURE_ENABLED) {
141 WpPlugin *self = WP_PLUGIN (object);
142 wp_info_object (self, "disabling plugin '%s'", wp_plugin_get_name (self));
143 if (WP_PLUGIN_GET_CLASS (self)->disable)
144 WP_PLUGIN_GET_CLASS (self)->disable (self);
145 wp_object_update_features (WP_OBJECT (self), 0, WP_PLUGIN_FEATURE_ENABLED);
146 }
147 }
148
149 static void
150 wp_plugin_class_init (WpPluginClass * klass)
151 {
152 GObjectClass * object_class = (GObjectClass *) klass;
153 WpObjectClass * wpobject_class = (WpObjectClass *) klass;
154
155 object_class->set_property = wp_plugin_set_property;
156 object_class->get_property = wp_plugin_get_property;
157
158 wpobject_class->get_supported_features = wp_plugin_get_supported_features;
159 wpobject_class->activate_get_next_step = wp_plugin_activate_get_next_step;
160 wpobject_class->activate_execute_step = wp_plugin_activate_execute_step;
161 wpobject_class->deactivate = wp_plugin_deactivate;
162
163 g_object_class_install_property (object_class, PROP_NAME,
164 g_param_spec_string ("name", "name",
165 "The name of this plugin", NULL,
166 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY | G_PARAM_STATIC_STRINGS));
167 }
168
169 static gboolean
170 find_plugin_func (gpointer plugin, gpointer name_quark)
171 {
172 if (!WP_IS_PLUGIN (plugin))
173 return FALSE;
174
175 WpPluginPrivate *priv = wp_plugin_get_instance_private (plugin);
176 return priv->name_quark == GPOINTER_TO_UINT (name_quark);
177 }
178
179 /*!
180 * \brief Looks up a plugin.
181 *
182 * \ingroup wpplugin
183 * \param core the core
184 * \param plugin_name the lookup name
185 * \returns (transfer full) (nullable): the plugin matching the lookup name
186 */
187 WpPlugin *
188 wp_plugin_find (WpCore * core, const gchar * plugin_name)
189 {
190 g_return_val_if_fail (WP_IS_CORE (core), NULL);
191
192 GQuark q = g_quark_try_string (plugin_name);
193 if (q == 0)
194 return NULL;
195 GObject *p = wp_core_find_object (core,
196 (GEqualFunc) find_plugin_func, GUINT_TO_POINTER (q));
197 return p ? WP_PLUGIN (p) : NULL;
198 }
199
200 /*!
201 * \brief Retrieves the name of a plugin.
202 *
203 * \ingroup wpplugin
204 * \param self the plugin
205 * \returns the name of this plugin
206 */
207 const gchar *
208 wp_plugin_get_name (WpPlugin * self)
209 {
210 g_return_val_if_fail (WP_IS_PLUGIN (self), NULL);
211
212 WpPluginPrivate *priv = wp_plugin_get_instance_private (self);
213 return g_quark_to_string (priv->name_quark);
214 }
215
216 /**
217 * \var _WpPluginClass::enable
218 *
219 * \brief Enables the plugin. The plugin is required to start any operations
220 * only when this method is called and not before.
221 *
222 * When enabling the plugin is done, you must call wp_object_update_features()
223 * with WP_PLUGIN_FEATURE_ENABLED marked as activated, or return an error
224 * on \a transition.
225 *
226 * \param self the plugin
227 * \param transition the activation transition
228 */
229
230 /**
231 * \var _WpPluginClass::disable
232 *
233 * \brief Disables the plugin. The plugin is required to stop all operations
234 * and release all resources associated with it.
235 *
236 * \param self the plugin
237 */