Coverage Report

Created: 2025-07-23 08:13

/src/pango/subprojects/glib/gio/gpowerprofilemonitor.c
Line
Count
Source (jump to first uncovered line)
1
/* GIO - GLib Input, Output and Streaming Library
2
 *
3
 * Copyright 2019 Red Hat, Inc
4
 * Copyright 2021 Igalia S.L.
5
 *
6
 * SPDX-License-Identifier: LGPL-2.1-or-later
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General
19
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
20
 */
21
22
#include "config.h"
23
#include "glib.h"
24
#include "glibintl.h"
25
26
#include "gpowerprofilemonitor.h"
27
#include "ginetaddress.h"
28
#include "ginetsocketaddress.h"
29
#include "ginitable.h"
30
#include "gioenumtypes.h"
31
#include "giomodule-priv.h"
32
#include "gtask.h"
33
34
/**
35
 * GPowerProfileMonitor:
36
 *
37
 * `GPowerProfileMonitor` makes it possible for applications as well as OS
38
 * components to monitor system power profiles and act upon them. It currently
39
 * only exports whether the system is in “Power Saver” mode (known as
40
 * “Low Power” mode on some systems).
41
 *
42
 * When in “Low Power” mode, it is recommended that applications:
43
 *
44
 * - disable automatic downloads;
45
 * - reduce the rate of refresh from online sources such as calendar or
46
 *   email synchronisation;
47
 * - reduce the use of expensive visual effects.
48
 *
49
 * It is also likely that OS components providing services to applications will
50
 * lower their own background activity, for the sake of the system.
51
 *
52
 * There are a variety of tools that exist for power consumption analysis, but those
53
 * usually depend on the OS and hardware used. On Linux, one could use `upower` to
54
 * monitor the battery discharge rate, `powertop` to check on the background activity
55
 * or activity at all), `sysprof` to inspect CPU usage, and `intel_gpu_time` to
56
 * profile GPU usage.
57
 *
58
 * Don’t forget to disconnect the [signal@GObject.Object::notify] signal for
59
 * [property@Gio.PowerProfileMonitor:power-saver-enabled], and unref the
60
 * `GPowerProfileMonitor` itself when exiting.
61
 *
62
 * Since: 2.70
63
 */
64
65
/**
66
 * GPowerProfileMonitorInterface:
67
 * @g_iface: The parent interface.
68
 *
69
 * The virtual function table for #GPowerProfileMonitor.
70
 *
71
 * Since: 2.70
72
 */
73
74
G_DEFINE_INTERFACE_WITH_CODE (GPowerProfileMonitor, g_power_profile_monitor, G_TYPE_OBJECT,
75
                              g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE))
76
77
78
/**
79
 * g_power_profile_monitor_dup_default:
80
 *
81
 * Gets a reference to the default #GPowerProfileMonitor for the system.
82
 *
83
 * Returns: (not nullable) (transfer full): a new reference to the default #GPowerProfileMonitor
84
 *
85
 * Since: 2.70
86
 */
87
GPowerProfileMonitor *
88
g_power_profile_monitor_dup_default (void)
89
0
{
90
0
  return g_object_ref (_g_io_module_get_default (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME,
91
0
                                                 "GIO_USE_POWER_PROFILE_MONITOR",
92
0
                                                 NULL));
93
0
}
94
95
/**
96
 * g_power_profile_monitor_get_power_saver_enabled:
97
 * @monitor: a #GPowerProfileMonitor
98
 *
99
 * Gets whether the system is in “Power Saver” mode.
100
 *
101
 * You are expected to listen to the
102
 * #GPowerProfileMonitor::notify::power-saver-enabled signal to know when the profile has
103
 * changed.
104
 *
105
 * Returns: Whether the system is in “Power Saver” mode.
106
 *
107
 * Since: 2.70
108
 */
109
gboolean
110
g_power_profile_monitor_get_power_saver_enabled (GPowerProfileMonitor *monitor)
111
0
{
112
0
  gboolean enabled;
113
0
  g_object_get (monitor, "power-saver-enabled", &enabled, NULL);
114
0
  return enabled;
115
0
}
116
117
static void
118
g_power_profile_monitor_default_init (GPowerProfileMonitorInterface *iface)
119
0
{
120
  /**
121
   * GPowerProfileMonitor:power-saver-enabled:
122
   *
123
   * Whether “Power Saver” mode is enabled on the system.
124
   *
125
   * Since: 2.70
126
   */
127
0
  g_object_interface_install_property (iface,
128
0
                                       g_param_spec_boolean ("power-saver-enabled", NULL, NULL,
129
0
                                                             FALSE,
130
0
                                                             G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY));
131
0
}