LCOV - code coverage report
Current view: top level - src/plugins/mousepad - valent-mousepad-plugin.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 86.4 % 264 228
Test Date: 2024-08-25 03:21:54 Functions: 94.7 % 19 18
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 58.3 % 180 105

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

Generated by: LCOV version 2.0-1