Branch data Line data Source code
1 : : // SPDX-License-Identifier: GPL-3.0-or-later
2 : : // SPDX-FileCopyrightText: Andy Holmes <andrew.g.r.holmes@gmail.com>
3 : :
4 : : #define G_LOG_DOMAIN "valent-session"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <gio/gio.h>
9 : : #include <libpeas.h>
10 : : #include <libvalent-core.h>
11 : :
12 : : #include "valent-session-adapter.h"
13 : :
14 : : #include "valent-session.h"
15 : :
16 : : /**
17 : : * ValentSession:
18 : : *
19 : : * A class for monitoring the session state.
20 : : *
21 : : * `ValentSession` is an abstraction of session managers, intended for use by
22 : : * [class@Valent.DevicePlugin] implementations.
23 : : *
24 : : * Plugins can implement [class@Valent.SessionAdapter] to provide an interface
25 : : * to monitor and control the session state.
26 : : *
27 : : * Since: 1.0
28 : : */
29 : : struct _ValentSession
30 : : {
31 : : ValentComponent parent_instance;
32 : :
33 : : ValentSessionAdapter *default_adapter;
34 : : };
35 : :
36 [ + + + - ]: 173 : G_DEFINE_FINAL_TYPE (ValentSession, valent_session, VALENT_TYPE_COMPONENT)
37 : :
38 : : typedef enum {
39 : : PROP_ACTIVE = 1,
40 : : PROP_LOCKED,
41 : : } ValentSessionProperty;
42 : :
43 : : static GParamSpec *properties[PROP_LOCKED + 1] = { NULL, };
44 : :
45 : : static void
46 : 7 : on_active_changed (ValentSessionAdapter *adapter,
47 : : GParamSpec *pspec,
48 : : ValentSession *self)
49 : : {
50 : 7 : VALENT_ENTRY;
51 : :
52 [ + - ]: 1 : if (self->default_adapter == adapter)
53 : 1 : g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_ACTIVE]);
54 : :
55 : 7 : VALENT_EXIT;
56 : : }
57 : :
58 : : static void
59 : 11 : on_locked_changed (ValentSessionAdapter *adapter,
60 : : GParamSpec *pspec,
61 : : ValentSession *self)
62 : : {
63 : 11 : VALENT_ENTRY;
64 : :
65 [ + - ]: 5 : if (self->default_adapter == adapter)
66 : 5 : g_object_notify_by_pspec (G_OBJECT (self), properties [PROP_LOCKED]);
67 : :
68 : 11 : VALENT_EXIT;
69 : : }
70 : :
71 : : /*
72 : : * ValentComponent
73 : : */
74 : : static void
75 : 9 : valent_session_bind_preferred (ValentComponent *component,
76 : : ValentExtension *extension)
77 : : {
78 : 9 : ValentSession *self = VALENT_SESSION (component);
79 : 9 : ValentSessionAdapter *adapter = VALENT_SESSION_ADAPTER (extension);
80 : :
81 : 9 : VALENT_ENTRY;
82 : :
83 [ + - ]: 9 : g_assert (VALENT_IS_SESSION (self));
84 [ + + - + ]: 9 : g_assert (adapter == NULL || VALENT_IS_SESSION_ADAPTER (adapter));
85 : :
86 [ + + ]: 9 : if (self->default_adapter != NULL)
87 : : {
88 : 3 : g_signal_handlers_disconnect_by_func (self->default_adapter,
89 : : self,
90 : : on_active_changed);
91 : 3 : g_signal_handlers_disconnect_by_func (self->default_adapter,
92 : : self,
93 : : on_locked_changed);
94 : 3 : self->default_adapter = NULL;
95 : : }
96 : :
97 [ + + ]: 9 : if (adapter != NULL)
98 : : {
99 : 6 : self->default_adapter = adapter;
100 : 6 : g_signal_connect_object (self->default_adapter,
101 : : "notify::active",
102 : : G_CALLBACK (on_active_changed),
103 : : self,
104 : : G_CONNECT_DEFAULT);
105 : 6 : on_active_changed (self->default_adapter, NULL, self);
106 : 6 : g_signal_connect_object (self->default_adapter,
107 : : "notify::locked",
108 : : G_CALLBACK (on_locked_changed),
109 : : self,
110 : : G_CONNECT_DEFAULT);
111 : 6 : on_locked_changed (self->default_adapter, NULL, self);
112 : : }
113 : :
114 : 9 : VALENT_EXIT;
115 : : }
116 : :
117 : : /*
118 : : * GObject
119 : : */
120 : : static void
121 : 2 : valent_session_get_property (GObject *object,
122 : : guint prop_id,
123 : : GValue *value,
124 : : GParamSpec *pspec)
125 : : {
126 : 2 : ValentSession *self = VALENT_SESSION (object);
127 : :
128 [ + + - ]: 2 : switch ((ValentSessionProperty)prop_id)
129 : : {
130 : 1 : case PROP_ACTIVE:
131 : 1 : g_value_set_boolean (value, valent_session_get_active (self));
132 : 1 : break;
133 : :
134 : 1 : case PROP_LOCKED:
135 : 1 : g_value_set_boolean (value, valent_session_get_locked (self));
136 : 1 : break;
137 : :
138 : 0 : default:
139 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
140 : : }
141 : 2 : }
142 : :
143 : : static void
144 : 1 : valent_session_set_property (GObject *object,
145 : : guint prop_id,
146 : : const GValue *value,
147 : : GParamSpec *pspec)
148 : : {
149 : 1 : ValentSession *self = VALENT_SESSION (object);
150 : :
151 [ + - ]: 1 : switch ((ValentSessionProperty)prop_id)
152 : : {
153 : 1 : case PROP_LOCKED:
154 : 1 : valent_session_set_locked (self, g_value_get_boolean (value));
155 : 1 : break;
156 : :
157 : 0 : case PROP_ACTIVE:
158 : : default:
159 : 0 : G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
160 : : }
161 : 1 : }
162 : :
163 : : static void
164 : 6 : valent_session_class_init (ValentSessionClass *klass)
165 : : {
166 : 6 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
167 : 6 : ValentComponentClass *component_class = VALENT_COMPONENT_CLASS (klass);
168 : :
169 : 6 : object_class->get_property = valent_session_get_property;
170 : 6 : object_class->set_property = valent_session_set_property;
171 : :
172 : 6 : component_class->bind_preferred = valent_session_bind_preferred;
173 : :
174 : : /**
175 : : * ValentSession:active: (getter get_active)
176 : : *
177 : : * Whether the session is active.
178 : : *
179 : : * Since: 1.0
180 : : */
181 : 12 : properties [PROP_ACTIVE] =
182 : 6 : g_param_spec_boolean ("active", NULL, NULL,
183 : : FALSE,
184 : : (G_PARAM_READABLE |
185 : : G_PARAM_EXPLICIT_NOTIFY |
186 : : G_PARAM_STATIC_STRINGS));
187 : :
188 : : /**
189 : : * ValentSession:locked: (getter get_locked) (setter set_locked)
190 : : *
191 : : * Whether the session is locked.
192 : : *
193 : : * Since: 1.0
194 : : */
195 : 12 : properties [PROP_LOCKED] =
196 : 6 : g_param_spec_boolean ("locked", NULL, NULL,
197 : : FALSE,
198 : : (G_PARAM_READWRITE |
199 : : G_PARAM_EXPLICIT_NOTIFY |
200 : : G_PARAM_STATIC_STRINGS));
201 : :
202 : 6 : g_object_class_install_properties (object_class, G_N_ELEMENTS (properties), properties);
203 : 6 : }
204 : :
205 : : static void
206 : 6 : valent_session_init (ValentSession *self)
207 : : {
208 : 6 : }
209 : :
210 : : /**
211 : : * valent_session_get_default:
212 : : *
213 : : * Get the default [class@Valent.Session].
214 : : *
215 : : * Returns: (transfer none) (nullable): a `ValentSession`
216 : : *
217 : : * Since: 1.0
218 : : */
219 : : ValentSession *
220 : 13 : valent_session_get_default (void)
221 : : {
222 : 13 : static ValentSession *default_instance = NULL;
223 : :
224 [ + + ]: 13 : if (default_instance == NULL)
225 : : {
226 : 6 : default_instance = g_object_new (VALENT_TYPE_SESSION,
227 : : "plugin-domain", "session",
228 : : "plugin-type", VALENT_TYPE_SESSION_ADAPTER,
229 : : NULL);
230 : 6 : g_object_add_weak_pointer (G_OBJECT (default_instance),
231 : : (gpointer)&default_instance);
232 : : }
233 : :
234 : 13 : return default_instance;
235 : : }
236 : :
237 : : /**
238 : : * valent_session_get_active: (get-property active)
239 : : * @session: a `ValentSession`
240 : : *
241 : : * Get the active state of the primary [class@Valent.SessionAdapter].
242 : : *
243 : : * Returns: %TRUE if the session is active, or %FALSE if not
244 : : *
245 : : * Since: 1.0
246 : : */
247 : : gboolean
248 : 4 : valent_session_get_active (ValentSession *session)
249 : : {
250 : 4 : gboolean ret = FALSE;
251 : :
252 : 4 : VALENT_ENTRY;
253 : :
254 [ + - ]: 4 : g_return_val_if_fail (VALENT_IS_SESSION (session), FALSE);
255 : :
256 [ - + ]: 4 : if G_LIKELY (session->default_adapter != NULL)
257 : 4 : ret = valent_session_adapter_get_active (session->default_adapter);
258 : :
259 : 4 : VALENT_RETURN (ret);
260 : : }
261 : :
262 : : /**
263 : : * valent_session_get_locked: (get-property locked)
264 : : * @session: a `ValentSession`
265 : : *
266 : : * Get the locked state of the primary [class@Valent.SessionAdapter].
267 : : *
268 : : * Returns: %TRUE if the session is locked, or %FALSE if unlocked
269 : : *
270 : : * Since: 1.0
271 : : */
272 : : gboolean
273 : 8 : valent_session_get_locked (ValentSession *session)
274 : : {
275 : 8 : gboolean ret = FALSE;
276 : :
277 : 8 : VALENT_ENTRY;
278 : :
279 [ + - ]: 8 : g_return_val_if_fail (VALENT_IS_SESSION (session), FALSE);
280 : :
281 [ - + ]: 8 : if G_LIKELY (session->default_adapter != NULL)
282 : 8 : ret = valent_session_adapter_get_locked (session->default_adapter);
283 : :
284 : 8 : VALENT_RETURN (ret);
285 : : }
286 : :
287 : : /**
288 : : * valent_session_set_locked: (set-property locked)
289 : : * @session: a `ValentSession`
290 : : * @state: %TRUE to lock, or %FALSE to unlock
291 : : *
292 : : * Set the locked state of the primary [class@Valent.SessionAdapter].
293 : : *
294 : : * Since: 1.0
295 : : */
296 : : void
297 : 4 : valent_session_set_locked (ValentSession *session,
298 : : gboolean state)
299 : : {
300 : 4 : VALENT_ENTRY;
301 : :
302 [ + - ]: 4 : g_return_if_fail (VALENT_IS_SESSION (session));
303 : :
304 [ + - ]: 4 : if G_LIKELY (session->default_adapter != NULL)
305 : 4 : valent_session_adapter_set_locked (session->default_adapter, state);
306 : :
307 : 4 : VALENT_EXIT;
308 : : }
309 : :
|