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-mousepad-plugin"
5 : :
6 : : #include "config.h"
7 : :
8 : : #include <glib/gi18n.h>
9 : : #include <gio/gio.h>
10 : : #include <json-glib/json-glib.h>
11 : : #include <valent.h>
12 : :
13 : : #include "valent-mousepad-device.h"
14 : : #include "valent-mousepad-keydef.h"
15 : : #include "valent-mousepad-plugin.h"
16 : :
17 : :
18 : : struct _ValentMousepadPlugin
19 : : {
20 : : ValentDevicePlugin parent_instance;
21 : :
22 : : ValentInputAdapter *local_adapter;
23 : : unsigned int local_state : 1;
24 : : unsigned int local_watch : 1;
25 : :
26 : : ValentMousepadDevice *remote_adapter;
27 : : unsigned int remote_state : 1;
28 : : };
29 : :
30 : : static void valent_mousepad_plugin_send_echo (ValentMousepadPlugin *self,
31 : : JsonNode *packet);
32 : : static void valent_mousepad_plugin_send_keyboardstate (ValentMousepadPlugin *self);
33 : :
34 [ + + + - ]: 169 : G_DEFINE_FINAL_TYPE (ValentMousepadPlugin, valent_mousepad_plugin, VALENT_TYPE_DEVICE_PLUGIN)
35 : :
36 : : static void
37 : 5 : on_primary_adapter_changed (ValentComponent *component,
38 : : GParamSpec *pspec,
39 : : ValentMousepadPlugin *self)
40 : : {
41 [ - + ]: 5 : g_assert (VALENT_IS_INPUT (component));
42 [ + - ]: 5 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
43 : :
44 [ - + ]: 5 : g_clear_object (&self->local_adapter);
45 : 5 : g_object_get (component, "primary-adapter", &self->local_adapter, NULL);
46 : :
47 [ + - ]: 5 : if (self->local_state != (self->local_adapter != NULL))
48 : : {
49 : 5 : self->local_state = (self->local_adapter != NULL);
50 : 5 : valent_mousepad_plugin_send_keyboardstate (self);
51 : : }
52 : 5 : }
53 : :
54 : : static void
55 : 20 : valent_mousepad_plugin_watch_local_state (ValentMousepadPlugin *self,
56 : : gboolean watch)
57 : : {
58 : 20 : ValentInput *input = valent_input_get_default ();
59 : :
60 [ - + ]: 20 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
61 : :
62 [ + + ]: 20 : if (self->local_watch == watch)
63 : : return;
64 : :
65 [ + + ]: 10 : if (watch)
66 : : {
67 : 5 : g_signal_connect_object (input,
68 : : "notify::primary-adapter",
69 : : G_CALLBACK (on_primary_adapter_changed),
70 : : self,
71 : : G_CONNECT_DEFAULT);
72 : 5 : on_primary_adapter_changed (VALENT_COMPONENT (input), NULL, self);
73 : 5 : self->local_watch = TRUE;
74 : : }
75 : : else
76 : : {
77 : 5 : g_signal_handlers_disconnect_by_data (input, self);
78 : 5 : self->local_watch = FALSE;
79 : : }
80 : : }
81 : :
82 : : static void
83 : 22 : valent_mousepad_plugin_update_remote_state (ValentMousepadPlugin *self,
84 : : gboolean state)
85 : : {
86 : 22 : ValentInput *input = valent_input_get_default ();
87 : 22 : GAction *action;
88 : :
89 [ - + ]: 22 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
90 : :
91 : 22 : action = g_action_map_lookup_action (G_ACTION_MAP (self), "event");
92 [ + + ]: 22 : if (action != NULL)
93 : 17 : g_simple_action_set_enabled (G_SIMPLE_ACTION (action), state);
94 : :
95 [ + + ]: 22 : if (self->remote_state == state)
96 : : return;
97 : :
98 : 4 : self->remote_state = state;
99 [ + + + - ]: 4 : if (self->remote_state && self->remote_adapter == NULL)
100 : 2 : {
101 : 2 : ValentDevice *device = NULL;
102 : :
103 : 2 : device = valent_object_get_parent (VALENT_OBJECT (self));
104 : 2 : self->remote_adapter = valent_mousepad_device_new (device);
105 : 2 : valent_component_export_adapter (VALENT_COMPONENT (input),
106 : : VALENT_EXTENSION (self->remote_adapter));
107 : : }
108 [ + - + - ]: 2 : else if (!self->remote_state && self->remote_adapter != NULL)
109 : : {
110 : 2 : valent_component_unexport_adapter (VALENT_COMPONENT (input),
111 : : VALENT_EXTENSION (self->remote_adapter));
112 : 2 : valent_object_destroy (VALENT_OBJECT (self->remote_adapter));
113 [ + - ]: 2 : g_clear_object (&self->remote_adapter);
114 : : }
115 : : }
116 : :
117 : : static KeyModifierType
118 : 5 : event_to_mask (JsonObject *body)
119 : : {
120 : 5 : KeyModifierType mask = 0;
121 : :
122 [ + + ]: 5 : if (json_object_get_boolean_member_with_default (body, "alt", FALSE))
123 : 1 : mask |= KEYMOD_ALT_MASK;
124 : :
125 [ + + ]: 5 : if (json_object_get_boolean_member_with_default (body, "ctrl", FALSE))
126 : 1 : mask |= KEYMOD_CONTROL_MASK;
127 : :
128 [ + + ]: 5 : if (json_object_get_boolean_member_with_default (body, "shift", FALSE))
129 : 1 : mask |= KEYMOD_SHIFT_MASK;
130 : :
131 [ + + ]: 5 : if (json_object_get_boolean_member_with_default (body, "super", FALSE))
132 : 1 : mask |= KEYMOD_SUPER_MASK;
133 : :
134 : 5 : return mask;
135 : : }
136 : :
137 : : static inline void
138 : 2 : keyboard_mask (ValentInputAdapter *input,
139 : : unsigned int mask,
140 : : gboolean lock)
141 : : {
142 [ + - ]: 2 : if (mask & KEYMOD_ALT_MASK)
143 : 2 : valent_input_adapter_keyboard_keysym (input, KEYSYM_Alt_L, lock);
144 : :
145 [ + - ]: 2 : if (mask & KEYMOD_CONTROL_MASK)
146 : 2 : valent_input_adapter_keyboard_keysym (input, KEYSYM_Control_L, lock);
147 : :
148 [ + - ]: 2 : if (mask & KEYMOD_SHIFT_MASK)
149 : 2 : valent_input_adapter_keyboard_keysym (input, KEYSYM_Shift_L, lock);
150 : :
151 [ + - ]: 2 : if (mask & KEYMOD_SUPER_MASK)
152 : 2 : valent_input_adapter_keyboard_keysym (input, KEYSYM_Super_L, lock);
153 : 2 : }
154 : :
155 : : /*
156 : : * Packet Handlers
157 : : */
158 : : static void
159 : 13 : valent_mousepad_plugin_handle_mousepad_request (ValentMousepadPlugin *self,
160 : : JsonNode *packet)
161 : : {
162 : 13 : JsonObject *body;
163 : 13 : const char *key;
164 : 13 : int64_t keycode;
165 : :
166 [ - + ]: 13 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
167 [ + - ]: 13 : g_assert (VALENT_IS_PACKET (packet));
168 : :
169 [ - + ]: 13 : if (self->local_adapter == NULL)
170 : : {
171 : 0 : g_warning ("%s(): No input adapter available", G_STRFUNC);
172 : 0 : return;
173 : : }
174 : :
175 : 13 : body = valent_packet_get_body (packet);
176 : :
177 : : /* Pointer movement */
178 [ + + - + ]: 13 : if (json_object_has_member (body, "dx") || json_object_has_member (body, "dy"))
179 : : {
180 : 2 : double dx, dy;
181 : :
182 : 2 : dx = json_object_get_double_member_with_default (body, "dx", 0.0);
183 : 2 : dy = json_object_get_double_member_with_default (body, "dy", 0.0);
184 : :
185 [ + + ]: 2 : if (valent_packet_check_field (packet, "scroll"))
186 : 1 : valent_input_adapter_pointer_axis (self->local_adapter, dx, dy);
187 : : else
188 : 1 : valent_input_adapter_pointer_motion (self->local_adapter, dx, dy);
189 : : }
190 : :
191 : : /* Keyboard Event */
192 [ + + ]: 11 : else if (valent_packet_get_string (packet, "key", &key))
193 : : {
194 : 4 : KeyModifierType mask;
195 : 4 : const char *next;
196 : 4 : gunichar codepoint;
197 : :
198 : : /* Lock modifiers */
199 [ + + ]: 4 : if ((mask = event_to_mask (body)) != 0)
200 : 1 : keyboard_mask (self->local_adapter, mask, TRUE);
201 : :
202 : : /* Input each keysym */
203 : 4 : next = key;
204 : :
205 [ + + ]: 54 : while ((codepoint = g_utf8_get_char (next)) != 0)
206 : : {
207 : 50 : uint32_t keysym;
208 : :
209 : 50 : keysym = valent_input_unicode_to_keysym (codepoint);
210 : 50 : valent_input_adapter_keyboard_keysym (self->local_adapter, keysym, TRUE);
211 : 50 : valent_input_adapter_keyboard_keysym (self->local_adapter, keysym, FALSE);
212 : :
213 : 50 : next = g_utf8_next_char (next);
214 : : }
215 : :
216 : : /* Unlock modifiers */
217 [ + + ]: 4 : if (mask != 0)
218 : 1 : keyboard_mask (self->local_adapter, mask, FALSE);
219 : :
220 : : /* Send ack, if requested */
221 [ - + ]: 4 : if (valent_packet_check_field (packet, "sendAck"))
222 : 0 : valent_mousepad_plugin_send_echo (self, packet);
223 : : }
224 [ + + ]: 7 : else if (valent_packet_get_int (packet, "specialKey", &keycode))
225 : : {
226 : 1 : KeyModifierType mask;
227 : 1 : uint32_t keysym;
228 : :
229 [ - + ]: 1 : if ((keysym = valent_mousepad_keycode_to_keysym (keycode)) == 0)
230 : : {
231 : 0 : g_debug ("%s(): expected \"specialKey\" field holding a keycode",
232 : : G_STRFUNC);
233 : 0 : return;
234 : : }
235 : :
236 : : /* Lock modifiers */
237 [ - + ]: 1 : if ((mask = event_to_mask (body)) != 0)
238 : 0 : keyboard_mask (self->local_adapter, mask, TRUE);
239 : :
240 : : /* Input each keysym */
241 : 1 : valent_input_adapter_keyboard_keysym (self->local_adapter, keysym, TRUE);
242 : 1 : valent_input_adapter_keyboard_keysym (self->local_adapter, keysym, FALSE);
243 : :
244 : : /* Unlock modifiers */
245 [ - + ]: 1 : if (mask != 0)
246 : 0 : keyboard_mask (self->local_adapter, mask, FALSE);
247 : :
248 : : /* Send ack, if requested */
249 [ - + ]: 1 : if (valent_packet_check_field (packet, "sendAck"))
250 : 0 : valent_mousepad_plugin_send_echo (self, packet);
251 : : }
252 : :
253 [ + + ]: 6 : else if (valent_packet_check_field (packet, "singleclick"))
254 : : {
255 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, TRUE);
256 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, FALSE);
257 : : }
258 : :
259 [ + + ]: 5 : else if (valent_packet_check_field (packet, "doubleclick"))
260 : : {
261 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, TRUE);
262 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, FALSE);
263 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, TRUE);
264 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, FALSE);
265 : : }
266 : :
267 [ + + ]: 4 : else if (valent_packet_check_field (packet, "middleclick"))
268 : : {
269 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_MIDDLE, TRUE);
270 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_MIDDLE, FALSE);
271 : : }
272 : :
273 [ + + ]: 3 : else if (valent_packet_check_field (packet, "rightclick"))
274 : : {
275 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_SECONDARY, TRUE);
276 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_SECONDARY, FALSE);
277 : : }
278 : :
279 [ + + ]: 2 : else if (valent_packet_check_field (packet, "singlehold"))
280 : : {
281 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, TRUE);
282 : : }
283 : :
284 : : /* Not used by kdeconnect-android, hold is released with a regular click */
285 [ + - ]: 1 : else if (valent_packet_check_field (packet, "singlerelease"))
286 : : {
287 : 1 : valent_input_adapter_pointer_button (self->local_adapter, VALENT_POINTER_PRIMARY, FALSE);
288 : : }
289 : :
290 : : else
291 : : {
292 : 0 : g_debug ("%s: unknown input", G_STRFUNC);
293 : : }
294 : : }
295 : :
296 : : static void
297 : 1 : valent_mousepad_plugin_handle_mousepad_echo (ValentMousepadPlugin *self,
298 : : JsonNode *packet)
299 : : {
300 [ - + ]: 1 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
301 [ + - ]: 1 : g_assert (VALENT_IS_PACKET (packet));
302 : :
303 : 1 : VALENT_NOTE ("Not implemented");
304 : 1 : }
305 : :
306 : : static void
307 : 2 : valent_mousepad_plugin_handle_mousepad_keyboardstate (ValentMousepadPlugin *self,
308 : : JsonNode *packet)
309 : : {
310 : 2 : gboolean state;
311 : :
312 [ - + ]: 2 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
313 [ + - ]: 2 : g_assert (VALENT_IS_PACKET (packet));
314 : :
315 : : /* Update the remote keyboard state */
316 [ - + ]: 2 : if (!valent_packet_get_boolean (packet, "state", &state))
317 : : {
318 : 0 : g_debug ("%s(): expected \"state\" field holding a boolean",
319 : : G_STRFUNC);
320 : 0 : return;
321 : : }
322 : :
323 [ + - ]: 2 : if (self->remote_state != state)
324 : : {
325 : 2 : valent_mousepad_plugin_update_remote_state (self, state);
326 : : }
327 : : }
328 : :
329 : : /*
330 : : * Packet Providers
331 : : */
332 : : static void
333 : 34 : valent_mousepad_plugin_mousepad_request_keyboard (ValentMousepadPlugin *self,
334 : : uint32_t keysym,
335 : : KeyModifierType mask)
336 : : {
337 : 34 : g_autoptr (JsonBuilder) builder = NULL;
338 [ - + - + ]: 34 : g_autoptr (JsonNode) packet = NULL;
339 : 34 : uint32_t special_key = 0;
340 : :
341 [ - + ]: 34 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
342 : :
343 [ + + ]: 34 : if G_UNLIKELY (keysym == 0 && mask == 0)
344 : : return;
345 : :
346 : 30 : valent_packet_init (&builder, "kdeconnect.mousepad.request");
347 : :
348 [ + + ]: 30 : if ((special_key = valent_mousepad_keysym_to_keycode (keysym)) != 0)
349 : : {
350 : 28 : json_builder_set_member_name (builder, "specialKey");
351 : 28 : json_builder_add_int_value (builder, special_key);
352 : : }
353 : : else
354 : : {
355 : 2 : g_autoptr (GError) error = NULL;
356 [ - - - + ]: 2 : g_autofree char *key = NULL;
357 : 2 : gunichar wc;
358 : :
359 : 2 : wc = valent_input_keysym_to_unicode (keysym);
360 : 2 : key = g_ucs4_to_utf8 (&wc, 1, NULL, NULL, &error);
361 : :
362 [ - + ]: 2 : if (key == NULL)
363 : : {
364 : 0 : g_warning ("Converting %u to string: %s", keysym, error->message);
365 : 0 : return;
366 : : }
367 : :
368 : 2 : json_builder_set_member_name (builder, "key");
369 : 2 : json_builder_add_string_value (builder, key);
370 : : }
371 : :
372 [ + + ]: 30 : if (mask & KEYMOD_ALT_MASK)
373 : : {
374 : 1 : json_builder_set_member_name (builder, "alt");
375 : 1 : json_builder_add_boolean_value (builder, TRUE);
376 : : }
377 : :
378 [ + + ]: 30 : if (mask & KEYMOD_CONTROL_MASK)
379 : : {
380 : 1 : json_builder_set_member_name (builder, "ctrl");
381 : 1 : json_builder_add_boolean_value (builder, TRUE);
382 : : }
383 : :
384 [ + + ]: 30 : if (mask & KEYMOD_SHIFT_MASK)
385 : : {
386 : 1 : json_builder_set_member_name (builder, "shift");
387 : 1 : json_builder_add_boolean_value (builder, TRUE);
388 : : }
389 : :
390 [ + + ]: 30 : if (mask & KEYMOD_SUPER_MASK)
391 : : {
392 : 1 : json_builder_set_member_name (builder, "super");
393 : 1 : json_builder_add_boolean_value (builder, TRUE);
394 : : }
395 : :
396 : 30 : packet = valent_packet_end (&builder);
397 : :
398 [ + - ]: 30 : valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
399 : : }
400 : :
401 : : static void
402 : 2 : valent_mousepad_plugin_mousepad_request_pointer (ValentMousepadPlugin *self,
403 : : double dx,
404 : : double dy,
405 : : gboolean axis)
406 : : {
407 : 4 : g_autoptr (JsonBuilder) builder = NULL;
408 [ - + ]: 2 : g_autoptr (JsonNode) packet = NULL;
409 : :
410 [ - + ]: 2 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
411 : :
412 : 2 : valent_packet_init (&builder, "kdeconnect.mousepad.request");
413 : :
414 : 2 : json_builder_set_member_name (builder, "dx");
415 : 2 : json_builder_add_double_value (builder, dx);
416 : 2 : json_builder_set_member_name (builder, "dy");
417 : 2 : json_builder_add_double_value (builder, dy);
418 : :
419 [ + + ]: 2 : if (axis)
420 : : {
421 : 1 : json_builder_set_member_name (builder, "scroll");
422 : 1 : json_builder_add_boolean_value (builder, TRUE);
423 : : }
424 : :
425 : 2 : packet = valent_packet_end (&builder);
426 : :
427 [ + - ]: 2 : valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
428 : 2 : }
429 : :
430 : : static void
431 : 0 : valent_mousepad_plugin_send_echo (ValentMousepadPlugin *self,
432 : : JsonNode *packet)
433 : : {
434 : 0 : g_autoptr (JsonBuilder) builder = NULL;
435 [ # # ]: 0 : g_autoptr (JsonNode) response = NULL;
436 : 0 : JsonObjectIter iter;
437 : 0 : const char *name;
438 : 0 : JsonNode *node;
439 : :
440 [ # # ]: 0 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
441 : :
442 : 0 : valent_packet_init (&builder, "kdeconnect.mousepad.echo");
443 : :
444 : 0 : json_object_iter_init (&iter, valent_packet_get_body (packet));
445 : :
446 [ # # ]: 0 : while (json_object_iter_next (&iter, &name, &node))
447 : : {
448 [ # # ]: 0 : if (g_strcmp0 (name, "sendAck") == 0)
449 : 0 : continue;
450 : :
451 : 0 : json_builder_set_member_name (builder, name);
452 : 0 : json_builder_add_value (builder, json_node_ref (node));
453 : : }
454 : :
455 : 0 : json_builder_set_member_name (builder, "isAck");
456 : 0 : json_builder_add_boolean_value (builder, TRUE);
457 : :
458 : 0 : response = valent_packet_end (&builder);
459 : :
460 [ # # ]: 0 : valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), response);
461 : 0 : }
462 : :
463 : : static void
464 : 5 : valent_mousepad_plugin_send_keyboardstate (ValentMousepadPlugin *self)
465 : : {
466 : 10 : g_autoptr (JsonBuilder) builder = NULL;
467 [ - + ]: 5 : g_autoptr (JsonNode) packet = NULL;
468 : :
469 [ - + ]: 5 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
470 : :
471 : 5 : valent_packet_init (&builder, "kdeconnect.mousepad.keyboardstate");
472 : 5 : json_builder_set_member_name (builder, "state");
473 : 5 : json_builder_add_boolean_value (builder, self->local_state);
474 : 5 : packet = valent_packet_end (&builder);
475 : :
476 [ + - ]: 5 : valent_device_plugin_queue_packet (VALENT_DEVICE_PLUGIN (self), packet);
477 : 5 : }
478 : :
479 : : /*
480 : : * GActions
481 : : */
482 : : static void
483 : 36 : mousepad_event_action (GSimpleAction *action,
484 : : GVariant *parameter,
485 : : gpointer user_data)
486 : : {
487 : 36 : ValentMousepadPlugin *self = VALENT_MOUSEPAD_PLUGIN (user_data);
488 : 36 : GVariantDict dict;
489 : 36 : double dx, dy;
490 : 36 : uint32_t keysym;
491 : :
492 [ - + ]: 36 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
493 [ + - ]: 36 : g_return_if_fail (self->remote_state);
494 : :
495 : 36 : g_variant_dict_init (&dict, parameter);
496 : :
497 [ + + + - ]: 38 : if (g_variant_dict_lookup (&dict, "dx", "d", &dx) &&
498 : 2 : g_variant_dict_lookup (&dict, "dy", "d", &dy))
499 : 2 : {
500 : 2 : gboolean scroll = FALSE;
501 : :
502 : 2 : g_variant_dict_lookup (&dict, "scroll", "b", &scroll);
503 : 2 : valent_mousepad_plugin_mousepad_request_pointer (self, dx, dy, scroll);
504 : : }
505 [ + - ]: 34 : else if (g_variant_dict_lookup (&dict, "keysym", "u", &keysym))
506 : : {
507 : 34 : KeyModifierType mask = 0;
508 : :
509 : 34 : g_variant_dict_lookup (&dict, "mask", "u", &mask);
510 : 34 : valent_mousepad_plugin_mousepad_request_keyboard (self, keysym, mask);
511 : : }
512 : : else
513 : 0 : g_warning ("%s(): unknown event type", G_STRFUNC);
514 : :
515 : 36 : g_variant_dict_clear (&dict);
516 : : }
517 : :
518 : : static const GActionEntry actions[] = {
519 : : {"event", mousepad_event_action, "a{sv}", NULL, NULL}
520 : : };
521 : :
522 : : /*
523 : : * ValentDevicePlugin
524 : : */
525 : : static void
526 : 10 : valent_mousepad_plugin_update_state (ValentDevicePlugin *plugin,
527 : : ValentDeviceState state)
528 : : {
529 : 10 : ValentMousepadPlugin *self = VALENT_MOUSEPAD_PLUGIN (plugin);
530 : 10 : gboolean available;
531 : :
532 [ - + ]: 10 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
533 : :
534 [ + + ]: 10 : available = (state & VALENT_DEVICE_STATE_CONNECTED) != 0 &&
535 [ - + ]: 5 : (state & VALENT_DEVICE_STATE_PAIRED) != 0;
536 : :
537 : 10 : valent_mousepad_plugin_watch_local_state (self, available);
538 [ + + + - ]: 20 : valent_mousepad_plugin_update_remote_state (self, available && self->remote_state);
539 : 10 : }
540 : :
541 : : static void
542 : 16 : valent_mousepad_plugin_handle_packet (ValentDevicePlugin *plugin,
543 : : const char *type,
544 : : JsonNode *packet)
545 : : {
546 : 16 : ValentMousepadPlugin *self = VALENT_MOUSEPAD_PLUGIN (plugin);
547 : :
548 [ - + ]: 16 : g_assert (VALENT_IS_MOUSEPAD_PLUGIN (self));
549 [ + - ]: 16 : g_assert (type != NULL);
550 [ + - ]: 16 : g_assert (VALENT_IS_PACKET (packet));
551 : :
552 : : /* A request to simulate input */
553 [ + + ]: 16 : if (g_str_equal (type, "kdeconnect.mousepad.request"))
554 : 13 : valent_mousepad_plugin_handle_mousepad_request (self, packet);
555 : :
556 : : /* A confirmation of input we requested */
557 [ + + ]: 3 : else if (g_str_equal (type, "kdeconnect.mousepad.echo"))
558 : 1 : valent_mousepad_plugin_handle_mousepad_echo (self, packet);
559 : :
560 : : /* The remote keyboard is ready/not ready for input */
561 [ + - ]: 2 : else if (g_str_equal (type, "kdeconnect.mousepad.keyboardstate"))
562 : 2 : valent_mousepad_plugin_handle_mousepad_keyboardstate (self, packet);
563 : :
564 : : else
565 : 0 : g_assert_not_reached ();
566 : 16 : }
567 : :
568 : : /*
569 : : * ValentObject
570 : : */
571 : : static void
572 : 10 : valent_mousepad_plugin_destroy (ValentObject *object)
573 : : {
574 : 10 : ValentMousepadPlugin *self = VALENT_MOUSEPAD_PLUGIN (object);
575 : :
576 : 10 : valent_mousepad_plugin_watch_local_state (self, FALSE);
577 : 10 : valent_mousepad_plugin_update_remote_state (self, FALSE);
578 : :
579 : 10 : VALENT_OBJECT_CLASS (valent_mousepad_plugin_parent_class)->destroy (object);
580 : 10 : }
581 : :
582 : : /*
583 : : * GObject
584 : : */
585 : : static void
586 : 5 : valent_mousepad_plugin_constructed (GObject *object)
587 : : {
588 : 5 : ValentDevicePlugin *plugin = VALENT_DEVICE_PLUGIN (object);
589 : :
590 : 5 : G_OBJECT_CLASS (valent_mousepad_plugin_parent_class)->constructed (object);
591 : :
592 : 5 : g_action_map_add_action_entries (G_ACTION_MAP (plugin),
593 : : actions,
594 : : G_N_ELEMENTS (actions),
595 : : plugin);
596 : 5 : }
597 : :
598 : : static void
599 : 1 : valent_mousepad_plugin_class_init (ValentMousepadPluginClass *klass)
600 : : {
601 : 1 : GObjectClass *object_class = G_OBJECT_CLASS (klass);
602 : 1 : ValentObjectClass *vobject_class = VALENT_OBJECT_CLASS (klass);
603 : 1 : ValentDevicePluginClass *plugin_class = VALENT_DEVICE_PLUGIN_CLASS (klass);
604 : :
605 : 1 : object_class->constructed = valent_mousepad_plugin_constructed;
606 : :
607 : 1 : vobject_class->destroy = valent_mousepad_plugin_destroy;
608 : :
609 : 1 : plugin_class->handle_packet = valent_mousepad_plugin_handle_packet;
610 : 1 : plugin_class->update_state = valent_mousepad_plugin_update_state;
611 : : }
612 : :
613 : : static void
614 : 5 : valent_mousepad_plugin_init (ValentMousepadPlugin *self)
615 : : {
616 : 5 : }
617 : :
|