Coverage Report

Created: 2025-06-13 06:55

/src/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
 * SECTION:gpowerprofilemonitor
36
 * @title: GPowerProfileMonitor
37
 * @short_description: Power profile monitor
38
 * @include: gio/gio.h
39
 *
40
 * #GPowerProfileMonitor makes it possible for applications as well as OS components
41
 * to monitor system power profiles and act upon them. It currently only exports
42
 * whether the system is in “Power Saver” mode (known as “Low Power” mode on
43
 * some systems).
44
 *
45
 * When in “Low Power” mode, it is recommended that applications:
46
 * - disable automatic downloads;
47
 * - reduce the rate of refresh from online sources such as calendar or
48
 *   email synchronisation;
49
 * - reduce the use of expensive visual effects.
50
 *
51
 * It is also likely that OS components providing services to applications will
52
 * lower their own background activity, for the sake of the system.
53
 *
54
 * There are a variety of tools that exist for power consumption analysis, but those
55
 * usually depend on the OS and hardware used. On Linux, one could use `upower` to
56
 * monitor the battery discharge rate, `powertop` to check on the background activity
57
 * or activity at all), `sysprof` to inspect CPU usage, and `intel_gpu_time` to
58
 * profile GPU usage.
59
 *
60
 * Don't forget to disconnect the #GPowerProfileMonitor::notify::power-saver-enabled
61
 * signal, and unref the #GPowerProfileMonitor itself when exiting.
62
 *
63
 * Since: 2.70
64
 */
65
66
/**
67
 * GPowerProfileMonitor:
68
 *
69
 * #GPowerProfileMonitor monitors system power profile and notifies on
70
 * changes.
71
 *
72
 * Since: 2.70
73
 */
74
75
/**
76
 * GPowerProfileMonitorInterface:
77
 * @g_iface: The parent interface.
78
 *
79
 * The virtual function table for #GPowerProfileMonitor.
80
 *
81
 * Since: 2.70
82
 */
83
84
G_DEFINE_INTERFACE_WITH_CODE (GPowerProfileMonitor, g_power_profile_monitor, G_TYPE_OBJECT,
85
                              g_type_interface_add_prerequisite (g_define_type_id, G_TYPE_INITABLE))
86
87
88
/**
89
 * g_power_profile_monitor_dup_default:
90
 *
91
 * Gets a reference to the default #GPowerProfileMonitor for the system.
92
 *
93
 * Returns: (not nullable) (transfer full): a new reference to the default #GPowerProfileMonitor
94
 *
95
 * Since: 2.70
96
 */
97
GPowerProfileMonitor *
98
g_power_profile_monitor_dup_default (void)
99
0
{
100
0
  return g_object_ref (_g_io_module_get_default (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME,
101
0
                                                 "GIO_USE_POWER_PROFILE_MONITOR",
102
0
                                                 NULL));
103
0
}
104
105
/**
106
 * g_power_profile_monitor_get_power_saver_enabled:
107
 * @monitor: a #GPowerProfileMonitor
108
 *
109
 * Gets whether the system is in “Power Saver” mode.
110
 *
111
 * You are expected to listen to the
112
 * #GPowerProfileMonitor::notify::power-saver-enabled signal to know when the profile has
113
 * changed.
114
 *
115
 * Returns: Whether the system is in “Power Saver” mode.
116
 *
117
 * Since: 2.70
118
 */
119
gboolean
120
g_power_profile_monitor_get_power_saver_enabled (GPowerProfileMonitor *monitor)
121
0
{
122
0
  gboolean enabled;
123
0
  g_object_get (monitor, "power-saver-enabled", &enabled, NULL);
124
0
  return enabled;
125
0
}
126
127
static void
128
g_power_profile_monitor_default_init (GPowerProfileMonitorInterface *iface)
129
0
{
130
  /**
131
   * GPowerProfileMonitor:power-saver-enabled:
132
   *
133
   * Whether “Power Saver” mode is enabled on the system.
134
   *
135
   * Since: 2.70
136
   */
137
0
  g_object_interface_install_property (iface,
138
0
                                       g_param_spec_boolean ("power-saver-enabled",
139
0
                                                             "power-saver-enabled",
140
0
                                                             "Power Saver Enabled",
141
0
                                                             FALSE,
142
0
                                                             G_PARAM_READABLE | G_PARAM_STATIC_STRINGS | G_PARAM_EXPLICIT_NOTIFY));
143
0
}