LCOV - code coverage report
Current view: top level - src/plugins/battery - valent-battery.c (source / functions) Coverage Total Hit
Test: Code Coverage Lines: 96.8 % 125 121
Test Date: 2024-04-23 06:02:46 Functions: 100.0 % 15 15
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 64.3 % 84 54

             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-battery"
       5                 :             : 
       6                 :             : #include "config.h"
       7                 :             : 
       8                 :             : #include <math.h>
       9                 :             : #include <stdint.h>
      10                 :             : 
      11                 :             : #include <gio/gio.h>
      12                 :             : 
      13                 :             : #include "valent-battery.h"
      14                 :             : 
      15                 :             : 
      16                 :             : struct _ValentBattery
      17                 :             : {
      18                 :             :   GObject       parent_instance;
      19                 :             : 
      20                 :             :   GDBusProxy   *proxy;
      21                 :             :   GCancellable *cancellable;
      22                 :             : 
      23                 :             :   unsigned int  current_charge;
      24                 :             :   unsigned int  is_charging : 1;
      25                 :             :   unsigned int  is_present : 1;
      26                 :             :   unsigned int  threshold_event;
      27                 :             : };
      28                 :             : 
      29   [ +  +  +  - ]:         156 : G_DEFINE_FINAL_TYPE (ValentBattery, valent_battery, G_TYPE_OBJECT)
      30                 :             : 
      31                 :             : enum {
      32                 :             :   CHANGED,
      33                 :             :   N_SIGNALS
      34                 :             : };
      35                 :             : 
      36                 :             : static guint signals[N_SIGNALS] = { 0, };
      37                 :             : 
      38                 :             : static ValentBattery *default_battery = NULL;
      39                 :             : 
      40                 :             : 
      41                 :             : /*
      42                 :             :  * These are a convenient representation of the values returned by UPower D-Bus
      43                 :             :  * service, that would otherwise be opaque integers.
      44                 :             :  *
      45                 :             :  * See: https://upower.freedesktop.org/docs/Device.html
      46                 :             :  */
      47                 :             : enum {
      48                 :             :   UPOWER_KIND_UNKNOWN,
      49                 :             :   UPOWER_KIND_LINE_POWER,
      50                 :             :   UPOWER_KIND_BATTERY,
      51                 :             :   UPOWER_KIND_UPS,
      52                 :             :   UPOWER_KIND_MONITOR,
      53                 :             :   UPOWER_KIND_MOUSE,
      54                 :             :   UPOWER_KIND_KEYBOARD,
      55                 :             :   UPOWER_KIND_PDA,
      56                 :             :   UPOWER_KIND_PHONE,
      57                 :             : };
      58                 :             : 
      59                 :             : enum {
      60                 :             :   UPOWER_LEVEL_UNKNOWN,
      61                 :             :   UPOWER_LEVEL_NONE,
      62                 :             :   UPOWER_LEVEL_DISCHARGING,
      63                 :             :   UPOWER_LEVEL_LOW,
      64                 :             :   UPOWER_LEVEL_CRITICAL,
      65                 :             :   UPOWER_LEVEL_ACTION,
      66                 :             :   UPOWER_LEVEL_NORMAL,
      67                 :             :   UPOWER_LEVEL_HIGH,
      68                 :             :   UPOWER_LEVEL_FULL
      69                 :             : };
      70                 :             : 
      71                 :             : enum {
      72                 :             :   UPOWER_STATE_UNKNOWN,
      73                 :             :   UPOWER_STATE_CHARGING,
      74                 :             :   UPOWER_STATE_DISCHARGING,
      75                 :             :   UPOWER_STATE_EMPTY,
      76                 :             :   UPOWER_STATE_FULLY_CHARGED,
      77                 :             :   UPOWER_STATE_PENDING_CHARGE,
      78                 :             :   UPOWER_STATE_PENDING_DISCHARGE
      79                 :             : };
      80                 :             : 
      81                 :             : 
      82                 :             : /*
      83                 :             :  * These are convenience functions for translating UPower states and levels into
      84                 :             :  * values expected by KDE Connect.
      85                 :             :  */
      86                 :             : static inline gboolean
      87                 :          20 : translate_state (uint32_t state)
      88                 :             : {
      89                 :          20 :   switch (state)
      90                 :             :     {
      91                 :             :     case UPOWER_STATE_CHARGING:
      92                 :             :     case UPOWER_STATE_FULLY_CHARGED:
      93                 :             :     case UPOWER_STATE_PENDING_CHARGE:
      94                 :             :       return TRUE;
      95                 :             : 
      96                 :             :     case UPOWER_STATE_DISCHARGING:
      97                 :             :     case UPOWER_STATE_EMPTY:
      98                 :             :     case UPOWER_STATE_PENDING_DISCHARGE:
      99                 :             :       return FALSE;
     100                 :             : 
     101                 :             :     default:
     102                 :             :       return FALSE;
     103                 :             :     }
     104                 :             : }
     105                 :             : 
     106                 :             : static inline unsigned int
     107                 :          19 : translate_warning_level (uint32_t warning_level)
     108                 :             : {
     109                 :          19 :   switch (warning_level)
     110                 :             :     {
     111                 :             :     case UPOWER_LEVEL_NONE:
     112                 :             :       return 0;
     113                 :             : 
     114                 :           4 :     case UPOWER_LEVEL_LOW:
     115                 :             :     case UPOWER_LEVEL_CRITICAL:
     116                 :             :     case UPOWER_LEVEL_ACTION:
     117                 :           4 :       return 1;
     118                 :             : 
     119                 :             :     default:
     120                 :             :       return 0;
     121                 :             :     }
     122                 :             : }
     123                 :             : 
     124                 :             : /*
     125                 :             :  * GDBusProxy
     126                 :             :  */
     127                 :             : static void
     128                 :           4 : valent_battery_load_properties (ValentBattery *self)
     129                 :             : {
     130                 :           8 :   g_autoptr (GVariant) value = NULL;
     131                 :             : 
     132         [ +  - ]:           4 :   if ((value = g_dbus_proxy_get_cached_property (self->proxy, "IsPresent")) != NULL)
     133                 :             :     {
     134                 :           4 :       gboolean is_present = g_variant_get_boolean (value);
     135                 :             : 
     136                 :           4 :       self->is_present = is_present;
     137                 :           4 :       g_clear_pointer (&value, g_variant_unref);
     138                 :             :     }
     139                 :             : 
     140         [ +  - ]:           4 :   g_assert (VALENT_IS_BATTERY (self));
     141                 :             : 
     142         [ +  - ]:           4 :   if ((value = g_dbus_proxy_get_cached_property (self->proxy, "Percentage")) != NULL)
     143                 :             :     {
     144                 :           4 :       double percentage = g_variant_get_double (value);
     145                 :             : 
     146                 :           4 :       self->current_charge = floor (percentage);
     147                 :           4 :       g_clear_pointer (&value, g_variant_unref);
     148                 :             :     }
     149                 :             : 
     150         [ +  - ]:           4 :   if ((value = g_dbus_proxy_get_cached_property (self->proxy, "State")) != NULL)
     151                 :             :     {
     152                 :           4 :       uint32_t state = g_variant_get_uint32 (value);
     153                 :             : 
     154         [ +  - ]:           4 :       self->is_charging = translate_state (state);
     155                 :           4 :       g_clear_pointer (&value, g_variant_unref);
     156                 :             :     }
     157                 :             : 
     158         [ +  - ]:           4 :   if ((value = g_dbus_proxy_get_cached_property (self->proxy, "WarningLevel")) != NULL)
     159                 :             :     {
     160                 :           4 :       uint32_t warning_level = g_variant_get_uint32 (value);
     161                 :             : 
     162         [ -  + ]:           4 :       self->threshold_event = translate_warning_level (warning_level);
     163                 :           4 :       g_clear_pointer (&value, g_variant_unref);
     164                 :             :     }
     165                 :           4 : }
     166                 :             : 
     167                 :             : static void
     168                 :          62 : on_properties_changed (GDBusProxy    *proxy,
     169                 :             :                        GVariant      *changed_properties,
     170                 :             :                        GStrv          invalidated_properties,
     171                 :             :                        ValentBattery *self)
     172                 :             : {
     173                 :          62 :   gboolean changed = FALSE;
     174                 :          62 :   gboolean is_present;
     175                 :          62 :   double percentage;
     176                 :          62 :   uint32_t state;
     177                 :          62 :   uint32_t warning_level;
     178                 :             : 
     179         [ +  - ]:          62 :   g_assert (VALENT_IS_BATTERY (self));
     180                 :             : 
     181                 :             :   /* If the battery was inserted or removed, the properties need to be either
     182                 :             :    * entirely reloaded or reset, respectively. */
     183         [ +  + ]:          62 :   if (g_variant_lookup (changed_properties, "IsPresent", "b", &is_present))
     184                 :             :     {
     185                 :             :       /* An existing battery was physically inserted */
     186   [ +  +  +  - ]:          15 :       if (!self->is_present && is_present)
     187                 :             :         {
     188                 :           1 :           valent_battery_load_properties (self);
     189                 :           1 :           changed = TRUE;
     190                 :             :         }
     191                 :             : 
     192                 :             :       /* An existing battery was physically removed */
     193   [ +  -  +  + ]:          14 :       else if (self->is_present && !is_present)
     194                 :             :         {
     195                 :           1 :           self->current_charge = 0;
     196                 :           1 :           self->is_charging = FALSE;
     197                 :           1 :           self->is_present = FALSE;
     198                 :           1 :           self->threshold_event = 0;
     199                 :           1 :           changed = TRUE;
     200                 :             :         }
     201                 :             : 
     202                 :           2 :       if (changed)
     203                 :             :         {
     204                 :           2 :           g_signal_emit (G_OBJECT (self), signals [CHANGED], 0);
     205                 :           2 :           return;
     206                 :             :         }
     207                 :             :     }
     208                 :             : 
     209         [ +  + ]:          60 :   if (g_variant_lookup (changed_properties, "Percentage", "d", &percentage))
     210                 :             :     {
     211                 :          16 :       unsigned int current_charge = floor (percentage);
     212                 :             : 
     213         [ +  + ]:          16 :       if (self->current_charge != current_charge)
     214                 :             :         {
     215                 :           3 :           self->current_charge = current_charge;
     216                 :           3 :           changed = TRUE;
     217                 :             :         }
     218                 :             :     }
     219                 :             : 
     220         [ +  + ]:          60 :   if (g_variant_lookup (changed_properties, "State", "u", &state))
     221                 :             :     {
     222         [ +  + ]:          16 :       gboolean is_charging = translate_state (state);
     223                 :             : 
     224         [ +  + ]:          16 :       if (self->is_charging != is_charging)
     225                 :             :         {
     226                 :           7 :           self->is_charging = is_charging;
     227                 :           7 :           changed = TRUE;
     228                 :             :         }
     229                 :             :     }
     230                 :             : 
     231         [ +  + ]:          60 :   if (g_variant_lookup (changed_properties, "WarningLevel", "u", &warning_level))
     232                 :             :     {
     233         [ +  + ]:          15 :       unsigned int threshold_event = translate_warning_level (warning_level);
     234                 :             : 
     235         [ +  + ]:          15 :       if (self->threshold_event != threshold_event)
     236                 :             :         {
     237                 :           6 :           self->threshold_event = threshold_event;
     238                 :           6 :           changed = TRUE;
     239                 :             :         }
     240                 :             :     }
     241                 :             : 
     242         [ +  + ]:          60 :   if (changed)
     243                 :          16 :     g_signal_emit (G_OBJECT (self), signals [CHANGED], 0);
     244                 :             : }
     245                 :             : 
     246                 :             : static void
     247                 :           3 : g_dbus_proxy_new_for_bus_cb (GObject       *object,
     248                 :             :                              GAsyncResult  *result,
     249                 :             :                              ValentBattery *self)
     250                 :             : {
     251                 :           3 :   g_autoptr (GError) error = NULL;
     252   [ -  +  -  - ]:           3 :   g_autoptr (GVariant) type = NULL;
     253         [ -  - ]:           3 :   g_autoptr (GVariant) value = NULL;
     254                 :             : 
     255         [ -  + ]:           3 :   if ((self->proxy = g_dbus_proxy_new_for_bus_finish (result, &error)) == NULL)
     256                 :             :     {
     257                 :           0 :       g_warning ("%s: %s", G_OBJECT_TYPE_NAME (self), error->message);
     258                 :           0 :       return;
     259                 :             :     }
     260                 :             : 
     261   [ +  -  -  + ]:           6 :   if ((type = g_dbus_proxy_get_cached_property (self->proxy, "Type")) == NULL ||
     262                 :           3 :       g_variant_get_uint32 (type) != UPOWER_KIND_BATTERY)
     263                 :             :     {
     264                 :           0 :       g_debug ("%s: not a battery", G_OBJECT_TYPE_NAME (self));
     265         [ #  # ]:           0 :       return;
     266                 :             :     }
     267                 :             : 
     268         [ +  - ]:           3 :   if ((value = g_dbus_proxy_get_cached_property (self->proxy, "IsPresent")) != NULL)
     269                 :             :     {
     270                 :           3 :       gboolean is_present = g_variant_get_boolean (value);
     271                 :             : 
     272         [ +  - ]:           3 :       if (is_present)
     273                 :           3 :         valent_battery_load_properties (self);
     274                 :             :     }
     275                 :             : 
     276                 :           3 :   g_signal_connect_object (self->proxy,
     277                 :             :                            "g-properties-changed",
     278                 :             :                            G_CALLBACK (on_properties_changed),
     279                 :             :                            self, 0);
     280         [ +  - ]:           3 :   g_signal_emit (G_OBJECT (self), signals [CHANGED], 0);
     281                 :             : }
     282                 :             : 
     283                 :             : /*
     284                 :             :  * GObject
     285                 :             :  */
     286                 :             : static void
     287                 :           1 : valent_battery_dispose (GObject *object)
     288                 :             : {
     289                 :           1 :   ValentBattery *self = VALENT_BATTERY (object);
     290                 :             : 
     291                 :           1 :   g_cancellable_cancel (self->cancellable);
     292                 :             : 
     293                 :           1 :   G_OBJECT_CLASS (valent_battery_parent_class)->dispose (object);
     294                 :           1 : }
     295                 :             : 
     296                 :             : static void
     297                 :           1 : valent_battery_finalize (GObject *object)
     298                 :             : {
     299                 :           1 :   ValentBattery *self = VALENT_BATTERY (object);
     300                 :             : 
     301         [ +  - ]:           1 :   g_clear_object (&self->cancellable);
     302         [ +  - ]:           1 :   g_clear_object (&self->proxy);
     303                 :             : 
     304                 :           1 :   G_OBJECT_CLASS (valent_battery_parent_class)->finalize (object);
     305                 :           1 : }
     306                 :             : 
     307                 :             : static void
     308                 :           3 : valent_battery_class_init (ValentBatteryClass *klass)
     309                 :             : {
     310                 :           3 :   GObjectClass *object_class = G_OBJECT_CLASS (klass);
     311                 :             : 
     312                 :           3 :   object_class->dispose = valent_battery_dispose;
     313                 :           3 :   object_class->finalize = valent_battery_finalize;
     314                 :             : 
     315                 :             :   /**
     316                 :             :    * ValentBattery::changed:
     317                 :             :    * @self: a `ValentBattery`
     318                 :             :    *
     319                 :             :    * `ValentBattery`::changed is emitted whenever a relevant property changes.
     320                 :             :    */
     321                 :           6 :   signals [CHANGED] =
     322                 :           3 :     g_signal_new ("changed",
     323                 :             :                   G_TYPE_FROM_CLASS (klass),
     324                 :             :                   G_SIGNAL_RUN_LAST,
     325                 :             :                   0,
     326                 :             :                   NULL, NULL, NULL,
     327                 :             :                   G_TYPE_NONE, 0);
     328                 :           3 : }
     329                 :             : 
     330                 :             : static void
     331                 :           3 : valent_battery_init (ValentBattery *self)
     332                 :             : {
     333                 :           3 :   self->cancellable = g_cancellable_new ();
     334                 :           3 :   self->current_charge = 0;
     335                 :           3 :   self->is_charging = FALSE;
     336                 :           3 :   self->is_present = FALSE;
     337                 :           3 :   self->threshold_event = 0;
     338                 :             : 
     339                 :           3 :   g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM,
     340                 :             :                             G_DBUS_PROXY_FLAGS_NONE,
     341                 :             :                             NULL,
     342                 :             :                             "org.freedesktop.UPower",
     343                 :             :                             "/org/freedesktop/UPower/devices/DisplayDevice",
     344                 :             :                             "org.freedesktop.UPower.Device",
     345                 :             :                             self->cancellable,
     346                 :             :                             (GAsyncReadyCallback)g_dbus_proxy_new_for_bus_cb,
     347                 :             :                             self);
     348                 :           3 : }
     349                 :             : 
     350                 :             : /**
     351                 :             :  * valent_battery_get_default:
     352                 :             :  *
     353                 :             :  * Get the default `ValentBattery`.
     354                 :             :  *
     355                 :             :  * Returns: (transfer none): The default `ValentBattery`
     356                 :             :  */
     357                 :             : ValentBattery *
     358                 :           6 : valent_battery_get_default (void)
     359                 :             : {
     360         [ +  + ]:           6 :   if (default_battery == NULL)
     361                 :             :     {
     362                 :           3 :       default_battery = g_object_new (VALENT_TYPE_BATTERY, NULL);
     363                 :             : 
     364                 :           3 :       g_object_add_weak_pointer (G_OBJECT (default_battery),
     365                 :             :                                  (gpointer) &default_battery);
     366                 :             :     }
     367                 :             : 
     368                 :           6 :   return default_battery;
     369                 :             : }
     370                 :             : 
     371                 :             : /**
     372                 :             :  * valent_battery_current_charge:
     373                 :             :  * @battery: a `ValentBattery`
     374                 :             :  *
     375                 :             :  * Get the charge level of @battery.
     376                 :             :  *
     377                 :             :  * The value returned by this method is a simplification of a UPower device
     378                 :             :  * battery percentage, useful for KDE Connect clients.
     379                 :             :  *
     380                 :             :  * Returns: a charge percentage, or `0` if unavailable
     381                 :             :  */
     382                 :             : int
     383                 :          22 : valent_battery_current_charge (ValentBattery *battery)
     384                 :             : {
     385         [ +  - ]:          22 :   g_return_val_if_fail (VALENT_IS_BATTERY (battery), 0);
     386                 :             : 
     387         [ +  + ]:          22 :   if (!battery->is_present)
     388                 :             :     return 0;
     389                 :             : 
     390                 :          20 :   return battery->current_charge;
     391                 :             : }
     392                 :             : 
     393                 :             : /**
     394                 :             :  * valent_battery_is_charging:
     395                 :             :  * @battery: a `ValentBattery`
     396                 :             :  *
     397                 :             :  * Get whether the battery is charging.
     398                 :             :  *
     399                 :             :  * The value returned by this method is a simplification of a UPower device
     400                 :             :  * state to a value useful for KDE Connect clients.
     401                 :             :  *
     402                 :             :  * Returns: %TRUE if the battery is charging
     403                 :             :  */
     404                 :             : gboolean
     405                 :          18 : valent_battery_is_charging (ValentBattery *battery)
     406                 :             : {
     407         [ +  - ]:          18 :   g_return_val_if_fail (VALENT_IS_BATTERY (battery), FALSE);
     408                 :             : 
     409                 :          18 :   return battery->is_charging;
     410                 :             : }
     411                 :             : 
     412                 :             : /**
     413                 :             :  * valent_battery_is_present:
     414                 :             :  * @battery: a `ValentBattery`
     415                 :             :  *
     416                 :             :  * Get whether the battery is present.
     417                 :             :  *
     418                 :             :  * Returns: %TRUE if the battery is present, %FALSE otherwise
     419                 :             :  */
     420                 :             : gboolean
     421                 :          14 : valent_battery_is_present (ValentBattery *battery)
     422                 :             : {
     423         [ +  - ]:          14 :   g_return_val_if_fail (VALENT_IS_BATTERY (battery), FALSE);
     424                 :             : 
     425                 :          14 :   return battery->is_present;
     426                 :             : }
     427                 :             : 
     428                 :             : /**
     429                 :             :  * valent_battery_is_charging:
     430                 :             :  * @battery: a `ValentBattery`
     431                 :             :  *
     432                 :             :  * Get whether the battery is charging.
     433                 :             :  *
     434                 :             :  * The value returned by this method is a simplification of a UPower device
     435                 :             :  * level to a value useful for KDE Connect clients.
     436                 :             :  *
     437                 :             :  * Returns: `1` if the level is below the threshold, `0` otherwise
     438                 :             :  */
     439                 :             : unsigned int
     440                 :          17 : valent_battery_threshold_event (ValentBattery *battery)
     441                 :             : {
     442         [ +  - ]:          17 :   g_return_val_if_fail (VALENT_IS_BATTERY (battery), FALSE);
     443                 :             : 
     444                 :          17 :   return battery->threshold_event;
     445                 :             : }
     446                 :             : 
        

Generated by: LCOV version 2.0-1