GCC Code Coverage Report


Directory: ./
File: src/ppd-action.c
Date: 2024-09-13 00:56:02
Exec Total Coverage
Lines: 46 58 79.3%
Functions: 12 13 92.3%
Branches: 18 31 58.1%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2020 Bastien Nocera <hadess@hadess.net>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 3 as published by
6 * the Free Software Foundation.
7 *
8 */
9
10 #define G_LOG_DOMAIN "Action"
11
12 #include "ppd-action.h"
13 #include "ppd-enums.h"
14
15 /**
16 * SECTION:ppd-action
17 * @Short_description: Profile Actions
18 * @Title: Profile Actions
19 *
20 * Profile actions are actions to run on profile change that do not affect
21 * the overall power usage, or performance level of the system, but instead
22 * of individual components.
23 *
24 * For example, an action might want to save energy when in the `power-saver`
25 * profile, and thus reduce the charging speed of a particular device. Or it
26 * could automatically reduce the speed of animations, or luminosity of an
27 * RGB keyboard.
28 *
29 * The list of actions that are currently running is available through the
30 * D-Bus API.
31 *
32 * Note that `power-profiles-daemon` can only accept #PpdAction<!-- -->s that
33 * will not make devices appear “broken” to users not in the know, so actions
34 * will never disable Wi-Fi or Bluetooth, or make some buttons stop working
35 * until power saving is turned off.
36 */
37
38 typedef struct
39 {
40 char *action_name;
41 PpdProfile profile;
42 } PpdActionPrivate;
43
44 enum {
45 PROP_0,
46 PROP_ACTION_NAME
47 };
48
49 #define PPD_ACTION_GET_PRIVATE(o) (ppd_action_get_instance_private (o))
50
3/5
✓ Branch 0 taken 130 times.
✓ Branch 1 taken 3294 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 130 times.
✗ Branch 4 not taken.
10038 G_DEFINE_TYPE_WITH_PRIVATE (PpdAction, ppd_action, G_TYPE_OBJECT)
51
52 static void
53 804 ppd_action_set_property (GObject *object,
54 guint property_id,
55 const GValue *value,
56 GParamSpec *pspec)
57 {
58 804 PpdAction *action = PPD_ACTION (object);
59 804 PpdActionPrivate *priv = PPD_ACTION_GET_PRIVATE (action);
60
61
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 switch (property_id) {
62 804 case PROP_ACTION_NAME:
63
1/2
✓ Branch 0 taken 804 times.
✗ Branch 1 not taken.
804 g_return_if_fail (priv->action_name == NULL);
64 804 priv->action_name = g_value_dup_string (value);
65 804 break;
66 default:
67 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
68 }
69 }
70
71 static void
72 ppd_action_get_property (GObject *object,
73 guint property_id,
74 GValue *value,
75 GParamSpec *pspec)
76 {
77 PpdAction *action = PPD_ACTION (object);
78 PpdActionPrivate *priv = PPD_ACTION_GET_PRIVATE (action);
79
80 switch (property_id) {
81 case PROP_ACTION_NAME:
82 g_value_set_string (value, priv->action_name);
83 break;
84 default:
85 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
86 }
87 }
88
89 static void
90 402 ppd_action_finalize (GObject *object)
91 {
92 402 PpdActionPrivate *priv;
93
94 402 priv = PPD_ACTION_GET_PRIVATE (PPD_ACTION (object));
95
1/2
✓ Branch 0 taken 402 times.
✗ Branch 1 not taken.
402 g_clear_pointer (&priv->action_name, g_free);
96
97 402 G_OBJECT_CLASS (ppd_action_parent_class)->finalize (object);
98 402 }
99
100 static void
101 130 ppd_action_class_init (PpdActionClass *klass)
102 {
103 130 GObjectClass *object_class;
104
105 130 object_class = G_OBJECT_CLASS (klass);
106 130 object_class->finalize = ppd_action_finalize;
107 130 object_class->get_property = ppd_action_get_property;
108 130 object_class->set_property = ppd_action_set_property;
109
110 /**
111 * PpdAction::action-name:
112 *
113 * A unique action name, only used for debugging.
114 */
115 130 g_object_class_install_property (object_class, PROP_ACTION_NAME,
116 g_param_spec_string ("action-name",
117 "Action name",
118 "Action name",
119 NULL,
120 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
121 130 }
122
123 static void
124 402 ppd_action_init (PpdAction *self)
125 {
126 402 }
127
128 PpdProbeResult
129 400 ppd_action_probe (PpdAction *action)
130 {
131
1/2
✓ Branch 1 taken 400 times.
✗ Branch 2 not taken.
400 g_return_val_if_fail (PPD_IS_ACTION (action), FALSE);
132
133
2/2
✓ Branch 0 taken 266 times.
✓ Branch 1 taken 134 times.
400 if (!PPD_ACTION_GET_CLASS (action)->probe)
134 return PPD_PROBE_RESULT_SUCCESS;
135
136 266 return PPD_ACTION_GET_CLASS (action)->probe (action);
137 }
138
139 gboolean
140 300 ppd_action_activate_profile (PpdAction *action,
141 PpdProfile profile,
142 GError **error)
143 {
144
1/2
✓ Branch 1 taken 300 times.
✗ Branch 2 not taken.
300 g_return_val_if_fail (PPD_IS_ACTION (action), FALSE);
145
146
1/2
✓ Branch 0 taken 300 times.
✗ Branch 1 not taken.
300 if (!PPD_ACTION_GET_CLASS (action)->activate_profile)
147 return TRUE;
148
149 300 return PPD_ACTION_GET_CLASS (action)->activate_profile (action, profile, error);
150 }
151
152 48 gboolean ppd_action_power_changed (PpdAction *action,
153 PpdPowerChangedReason reason,
154 GError **error)
155 {
156
1/2
✓ Branch 1 taken 48 times.
✗ Branch 2 not taken.
48 g_return_val_if_fail (PPD_IS_ACTION (action), FALSE);
157
158
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 44 times.
48 if (!PPD_ACTION_GET_CLASS (action)->power_changed)
159 return TRUE;
160
161 4 return PPD_ACTION_GET_CLASS (action)->power_changed (action, reason, error);
162 }
163
164 30 gboolean ppd_action_battery_changed (PpdAction *action,
165 gdouble val,
166 GError **error)
167 {
168
1/2
✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
30 g_return_val_if_fail (PPD_IS_ACTION (action), FALSE);
169
170
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 20 times.
30 if (!PPD_ACTION_GET_CLASS (action)->battery_changed)
171 return TRUE;
172
173 10 return PPD_ACTION_GET_CLASS (action)->battery_changed (action, val, error);
174 }
175
176 const char *
177 1724 ppd_action_get_action_name (PpdAction *action)
178 {
179 1724 PpdActionPrivate *priv;
180
181
1/2
✓ Branch 1 taken 1724 times.
✗ Branch 2 not taken.
1724 g_return_val_if_fail (PPD_IS_ACTION (action), NULL);
182
183 1724 priv = PPD_ACTION_GET_PRIVATE (action);
184 1724 return priv->action_name;
185 }
186