1
/* ide-battery-monitor.c
3
* Copyright (C) 2015 Christian Hergert <christian@hergert.me>
5
* This program is free software: you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation, either version 3 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21
#include "ide-battery-monitor.h"
23
#define CONSERVE_THRESHOLD 50.0
25
static GDBusProxy *gUPowerProxy;
26
static GDBusProxy *gUPowerDeviceProxy;
27
static gint gUPowerHold;
29
G_LOCK_DEFINE_STATIC (proxy_lock);
32
ide_battery_monitor_get_proxy (void)
34
GDBusProxy *proxy = NULL;
42
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
46
gUPowerProxy = g_dbus_proxy_new_sync (bus,
47
G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
49
"org.freedesktop.UPower",
50
"/org/freedesktop/UPower",
51
"org.freedesktop.UPower",
58
proxy = gUPowerProxy ? g_object_ref (gUPowerProxy) : NULL;
60
G_UNLOCK (proxy_lock);
66
ide_battery_monitor_get_device_proxy (void)
68
GDBusProxy *proxy = NULL;
72
if (!gUPowerDeviceProxy)
76
bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
80
gUPowerDeviceProxy = g_dbus_proxy_new_sync (bus,
81
G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
83
"org.freedesktop.UPower",
84
"/org/freedesktop/UPower/devices/DisplayDevice",
85
"org.freedesktop.UPower.Device",
92
proxy = gUPowerDeviceProxy ? g_object_ref (gUPowerDeviceProxy) : NULL;
94
G_UNLOCK (proxy_lock);
100
ide_battery_monitor_get_on_battery (void)
103
gboolean ret = FALSE;
105
proxy = ide_battery_monitor_get_proxy ();
111
prop = g_dbus_proxy_get_cached_property (proxy, "OnBattery");
113
ret = g_variant_get_boolean (prop);
114
g_object_unref (proxy);
121
ide_battery_monitor_get_energy_percentage (void)
126
proxy = ide_battery_monitor_get_device_proxy ();
132
prop = g_dbus_proxy_get_cached_property (proxy, "Percentage");
134
ret = g_variant_get_double (prop);
135
g_object_unref (proxy);
142
ide_battery_monitor_get_should_conserve (void)
144
gboolean should_conserve = FALSE;
146
if (ide_battery_monitor_get_on_battery ())
150
energy = ide_battery_monitor_get_energy_percentage ();
151
should_conserve = (energy != 0.0) && (energy < CONSERVE_THRESHOLD);
154
return should_conserve;
158
_ide_battery_monitor_shutdown (void)
162
if (--gUPowerHold == 0)
164
g_clear_object (&gUPowerProxy);
165
g_clear_object (&gUPowerDeviceProxy);
168
G_UNLOCK (proxy_lock);
172
_ide_battery_monitor_init (void)
174
g_autoptr(GDBusProxy) proxy = NULL;
175
g_autoptr(GDBusProxy) device_proxy = NULL;
179
G_UNLOCK (proxy_lock);
181
proxy = ide_battery_monitor_get_proxy ();
182
device_proxy = ide_battery_monitor_get_device_proxy ();