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 | char *action_description; | ||
42 | gboolean optin; | ||
43 | gboolean active; | ||
44 | PpdProfile profile; | ||
45 | } PpdActionPrivate; | ||
46 | |||
47 | enum { | ||
48 | PROP_0, | ||
49 | PROP_ACTION_NAME, | ||
50 | PROP_ACTION_DESCRIPTION, | ||
51 | PROP_ACTION_OPTIN, | ||
52 | }; | ||
53 | |||
54 | #define PPD_ACTION_GET_PRIVATE(o) (ppd_action_get_instance_private (o)) | ||
55 |
3/5✓ Branch 0 taken 142 times.
✓ Branch 1 taken 9649 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 142 times.
✗ Branch 4 not taken.
|
31396 | G_DEFINE_TYPE_WITH_PRIVATE (PpdAction, ppd_action, G_TYPE_OBJECT) |
56 | |||
57 | static void | ||
58 | 2844 | ppd_action_set_property (GObject *object, | |
59 | guint property_id, | ||
60 | const GValue *value, | ||
61 | GParamSpec *pspec) | ||
62 | { | ||
63 | 2844 | PpdAction *action = PPD_ACTION (object); | |
64 | 2844 | PpdActionPrivate *priv = PPD_ACTION_GET_PRIVATE (action); | |
65 | |||
66 |
3/4✓ Branch 0 taken 948 times.
✓ Branch 1 taken 948 times.
✓ Branch 2 taken 948 times.
✗ Branch 3 not taken.
|
2844 | switch (property_id) { |
67 | 948 | case PROP_ACTION_NAME: | |
68 |
1/2✓ Branch 0 taken 948 times.
✗ Branch 1 not taken.
|
948 | g_return_if_fail (priv->action_name == NULL); |
69 | 948 | priv->action_name = g_value_dup_string (value); | |
70 | 948 | break; | |
71 | 948 | case PROP_ACTION_DESCRIPTION: | |
72 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 948 times.
|
948 | g_return_if_fail (priv->action_description == NULL); |
73 | 948 | g_set_str (&priv->action_description, g_value_get_string (value)); | |
74 | 948 | break; | |
75 | 948 | case PROP_ACTION_OPTIN: | |
76 | 948 | priv->optin = g_value_get_boolean (value); | |
77 | 948 | break; | |
78 | ✗ | default: | |
79 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
80 | } | ||
81 | } | ||
82 | |||
83 | static void | ||
84 | ✗ | ppd_action_get_property (GObject *object, | |
85 | guint property_id, | ||
86 | GValue *value, | ||
87 | GParamSpec *pspec) | ||
88 | { | ||
89 | ✗ | PpdAction *action = PPD_ACTION (object); | |
90 | ✗ | PpdActionPrivate *priv = PPD_ACTION_GET_PRIVATE (action); | |
91 | |||
92 | ✗ | switch (property_id) { | |
93 | ✗ | case PROP_ACTION_NAME: | |
94 | ✗ | g_value_set_string (value, priv->action_name); | |
95 | ✗ | break; | |
96 | ✗ | case PROP_ACTION_DESCRIPTION: | |
97 | ✗ | g_value_set_string (value, priv->action_description); | |
98 | ✗ | break; | |
99 | ✗ | case PROP_ACTION_OPTIN: | |
100 | ✗ | g_value_set_boolean (value, priv->optin); | |
101 | ✗ | break; | |
102 | ✗ | default: | |
103 | ✗ | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
104 | } | ||
105 | ✗ | } | |
106 | |||
107 | static void | ||
108 | 474 | ppd_action_finalize (GObject *object) | |
109 | { | ||
110 | 474 | PpdActionPrivate *priv; | |
111 | |||
112 | 474 | priv = PPD_ACTION_GET_PRIVATE (PPD_ACTION (object)); | |
113 |
1/2✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
|
474 | g_clear_pointer (&priv->action_name, g_free); |
114 |
1/2✓ Branch 0 taken 474 times.
✗ Branch 1 not taken.
|
474 | g_clear_pointer (&priv->action_description, g_free); |
115 | |||
116 | 474 | G_OBJECT_CLASS (ppd_action_parent_class)->finalize (object); | |
117 | 474 | } | |
118 | |||
119 | static void | ||
120 | 142 | ppd_action_class_init (PpdActionClass *klass) | |
121 | { | ||
122 | 142 | GObjectClass *object_class; | |
123 | |||
124 | 142 | object_class = G_OBJECT_CLASS (klass); | |
125 | 142 | object_class->finalize = ppd_action_finalize; | |
126 | 142 | object_class->get_property = ppd_action_get_property; | |
127 | 142 | object_class->set_property = ppd_action_set_property; | |
128 | |||
129 | /** | ||
130 | * PpdAction::action-name: | ||
131 | * | ||
132 | * A unique action name, only used for debugging. | ||
133 | */ | ||
134 | 142 | g_object_class_install_property (object_class, PROP_ACTION_NAME, | |
135 | g_param_spec_string ("action-name", | ||
136 | "Action name", | ||
137 | "Action name", | ||
138 | NULL, | ||
139 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); | ||
140 | /** | ||
141 | * PpdAction::action-description: | ||
142 | * | ||
143 | * A unique action description, only used for debugging. | ||
144 | */ | ||
145 | 142 | g_object_class_install_property (object_class, PROP_ACTION_DESCRIPTION, | |
146 | g_param_spec_string ("action-description", | ||
147 | "Action description", | ||
148 | "Action description", | ||
149 | NULL, | ||
150 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); | ||
151 | |||
152 | /** | ||
153 | * PpdAction::optin: | ||
154 | * | ||
155 | * Whether the action is opt-in or not. | ||
156 | */ | ||
157 | 142 | g_object_class_install_property (object_class, PROP_ACTION_OPTIN, | |
158 | g_param_spec_boolean ("action-optin", | ||
159 | "Opt-in", | ||
160 | "Whether the action is opt-in or not", | ||
161 | FALSE, | ||
162 | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY)); | ||
163 | 142 | } | |
164 | |||
165 | static void | ||
166 | 474 | ppd_action_init (PpdAction *self) | |
167 | { | ||
168 | 474 | } | |
169 | |||
170 | PpdProbeResult | ||
171 | 158 | ppd_action_probe (PpdAction *action) | |
172 | { | ||
173 |
1/2✓ Branch 1 taken 158 times.
✗ Branch 2 not taken.
|
158 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
174 | |||
175 |
2/2✓ Branch 0 taken 4 times.
✓ Branch 1 taken 154 times.
|
158 | if (!PPD_ACTION_GET_CLASS (action)->probe) |
176 | return PPD_PROBE_RESULT_SUCCESS; | ||
177 | |||
178 | 4 | return PPD_ACTION_GET_CLASS (action)->probe (action); | |
179 | } | ||
180 | |||
181 | gboolean | ||
182 | 306 | ppd_action_activate_profile (PpdAction *action, | |
183 | PpdProfile profile, | ||
184 | GError **error) | ||
185 | { | ||
186 |
1/2✓ Branch 1 taken 306 times.
✗ Branch 2 not taken.
|
306 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
187 | |||
188 |
1/2✓ Branch 0 taken 306 times.
✗ Branch 1 not taken.
|
306 | if (!PPD_ACTION_GET_CLASS (action)->activate_profile) |
189 | return TRUE; | ||
190 | |||
191 | 306 | return PPD_ACTION_GET_CLASS (action)->activate_profile (action, profile, error); | |
192 | } | ||
193 | |||
194 | 50 | gboolean ppd_action_power_changed (PpdAction *action, | |
195 | PpdPowerChangedReason reason, | ||
196 | GError **error) | ||
197 | { | ||
198 |
1/2✓ Branch 1 taken 50 times.
✗ Branch 2 not taken.
|
50 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
199 | |||
200 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 48 times.
|
50 | if (!PPD_ACTION_GET_CLASS (action)->power_changed) |
201 | return TRUE; | ||
202 | |||
203 | 2 | return PPD_ACTION_GET_CLASS (action)->power_changed (action, reason, error); | |
204 | } | ||
205 | |||
206 | 23 | gboolean ppd_action_battery_changed (PpdAction *action, | |
207 | gdouble val, | ||
208 | GError **error) | ||
209 | { | ||
210 |
1/2✓ Branch 1 taken 23 times.
✗ Branch 2 not taken.
|
23 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
211 | |||
212 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 18 times.
|
23 | if (!PPD_ACTION_GET_CLASS (action)->battery_changed) |
213 | return TRUE; | ||
214 | |||
215 | 5 | return PPD_ACTION_GET_CLASS (action)->battery_changed (action, val, error); | |
216 | } | ||
217 | |||
218 | const char * | ||
219 | 2828 | ppd_action_get_action_name (PpdAction *action) | |
220 | { | ||
221 | 2828 | PpdActionPrivate *priv; | |
222 | |||
223 |
1/2✓ Branch 1 taken 2828 times.
✗ Branch 2 not taken.
|
2828 | g_return_val_if_fail (PPD_IS_ACTION (action), NULL); |
224 | |||
225 | 2828 | priv = PPD_ACTION_GET_PRIVATE (action); | |
226 | 2828 | return priv->action_name; | |
227 | } | ||
228 | |||
229 | const char * | ||
230 | 810 | ppd_action_get_action_description (PpdAction *action) | |
231 | { | ||
232 | 810 | PpdActionPrivate *priv; | |
233 | |||
234 |
1/2✓ Branch 1 taken 810 times.
✗ Branch 2 not taken.
|
810 | g_return_val_if_fail (PPD_IS_ACTION (action), NULL); |
235 | |||
236 | 810 | priv = PPD_ACTION_GET_PRIVATE (action); | |
237 | 810 | return priv->action_description; | |
238 | } | ||
239 | |||
240 | void | ||
241 | 474 | ppd_action_set_active (PpdAction *action, | |
242 | gboolean active) | ||
243 | { | ||
244 | 474 | PpdActionPrivate *priv; | |
245 | |||
246 |
1/2✓ Branch 1 taken 474 times.
✗ Branch 2 not taken.
|
474 | g_return_if_fail (PPD_IS_ACTION (action)); |
247 | |||
248 | 474 | priv = PPD_ACTION_GET_PRIVATE (action); | |
249 | 474 | priv->active = active; | |
250 | } | ||
251 | |||
252 | gboolean | ||
253 | 3666 | ppd_action_get_active (PpdAction *action) | |
254 | { | ||
255 | 3666 | PpdActionPrivate *priv; | |
256 | |||
257 |
1/2✓ Branch 1 taken 3666 times.
✗ Branch 2 not taken.
|
3666 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
258 | |||
259 | 3666 | priv = PPD_ACTION_GET_PRIVATE (action); | |
260 | 3666 | return priv->active; | |
261 | } | ||
262 | |||
263 | gboolean | ||
264 | 434 | ppd_action_get_optin (PpdAction *action) | |
265 | { | ||
266 | 434 | PpdActionPrivate *priv; | |
267 | |||
268 |
1/2✓ Branch 1 taken 434 times.
✗ Branch 2 not taken.
|
434 | g_return_val_if_fail (PPD_IS_ACTION (action), FALSE); |
269 | |||
270 | 434 | priv = PPD_ACTION_GET_PRIVATE (action); | |
271 | 434 | return priv->optin; | |
272 | } | ||
273 |