~codygarver/+junk/gnome-builder

« back to all changes in this revision

Viewing changes to libide/ide-battery-monitor.c

  • Committer: Cody Garver
  • Date: 2015-11-21 00:50:38 UTC
  • Revision ID: cody@elementary.io-20151121005038-8wygis63zt0ljqlz
Import https://github.com/chergert/gnome-builder 06e3158922a02a27f4abca250d70aa7b2970e06a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ide-battery-monitor.c
 
2
 *
 
3
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 
4
 *
 
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.
 
9
 *
 
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.
 
14
 *
 
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/>.
 
17
 */
 
18
 
 
19
#include <gio/gio.h>
 
20
 
 
21
#include "ide-battery-monitor.h"
 
22
 
 
23
#define CONSERVE_THRESHOLD 50.0
 
24
 
 
25
static GDBusProxy *gUPowerProxy;
 
26
static GDBusProxy *gUPowerDeviceProxy;
 
27
static gint        gUPowerHold;
 
28
 
 
29
G_LOCK_DEFINE_STATIC (proxy_lock);
 
30
 
 
31
static GDBusProxy *
 
32
ide_battery_monitor_get_proxy (void)
 
33
{
 
34
  GDBusProxy *proxy = NULL;
 
35
 
 
36
  G_LOCK (proxy_lock);
 
37
 
 
38
  if (!gUPowerProxy)
 
39
    {
 
40
      GDBusConnection *bus;
 
41
 
 
42
      bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
 
43
 
 
44
      if (bus)
 
45
        {
 
46
          gUPowerProxy = g_dbus_proxy_new_sync (bus,
 
47
                                                G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
48
                                                NULL,
 
49
                                                "org.freedesktop.UPower",
 
50
                                                "/org/freedesktop/UPower",
 
51
                                                "org.freedesktop.UPower",
 
52
                                                NULL,
 
53
                                                NULL);
 
54
          g_object_unref (bus);
 
55
        }
 
56
    }
 
57
 
 
58
  proxy = gUPowerProxy ? g_object_ref (gUPowerProxy) : NULL;
 
59
 
 
60
  G_UNLOCK (proxy_lock);
 
61
 
 
62
  return proxy;
 
63
}
 
64
 
 
65
static GDBusProxy *
 
66
ide_battery_monitor_get_device_proxy (void)
 
67
{
 
68
  GDBusProxy *proxy = NULL;
 
69
 
 
70
  G_LOCK (proxy_lock);
 
71
 
 
72
  if (!gUPowerDeviceProxy)
 
73
    {
 
74
      GDBusConnection *bus;
 
75
 
 
76
      bus = g_bus_get_sync (G_BUS_TYPE_SYSTEM, NULL, NULL);
 
77
 
 
78
      if (bus)
 
79
        {
 
80
          gUPowerDeviceProxy = g_dbus_proxy_new_sync (bus,
 
81
                                                      G_DBUS_PROXY_FLAGS_GET_INVALIDATED_PROPERTIES,
 
82
                                                      NULL,
 
83
                                                      "org.freedesktop.UPower",
 
84
                                                      "/org/freedesktop/UPower/devices/DisplayDevice",
 
85
                                                      "org.freedesktop.UPower.Device",
 
86
                                                      NULL,
 
87
                                                      NULL);
 
88
          g_object_unref (bus);
 
89
        }
 
90
    }
 
91
 
 
92
  proxy = gUPowerDeviceProxy ? g_object_ref (gUPowerDeviceProxy) : NULL;
 
93
 
 
94
  G_UNLOCK (proxy_lock);
 
95
 
 
96
  return proxy;
 
97
}
 
98
 
 
99
gboolean
 
100
ide_battery_monitor_get_on_battery (void)
 
101
{
 
102
  GDBusProxy *proxy;
 
103
  gboolean ret = FALSE;
 
104
 
 
105
  proxy = ide_battery_monitor_get_proxy ();
 
106
 
 
107
  if (proxy)
 
108
    {
 
109
      GVariant *prop;
 
110
 
 
111
      prop = g_dbus_proxy_get_cached_property (proxy, "OnBattery");
 
112
      if (prop)
 
113
        ret = g_variant_get_boolean (prop);
 
114
      g_object_unref (proxy);
 
115
    }
 
116
 
 
117
  return ret;
 
118
}
 
119
 
 
120
gdouble
 
121
ide_battery_monitor_get_energy_percentage (void)
 
122
{
 
123
  GDBusProxy *proxy;
 
124
  gdouble ret = 0.0;
 
125
 
 
126
  proxy = ide_battery_monitor_get_device_proxy ();
 
127
 
 
128
  if (proxy)
 
129
    {
 
130
      GVariant *prop;
 
131
 
 
132
      prop = g_dbus_proxy_get_cached_property (proxy, "Percentage");
 
133
      if (prop)
 
134
        ret = g_variant_get_double (prop);
 
135
      g_object_unref (proxy);
 
136
    }
 
137
 
 
138
  return ret;
 
139
}
 
140
 
 
141
gboolean
 
142
ide_battery_monitor_get_should_conserve (void)
 
143
{
 
144
  gboolean should_conserve = FALSE;
 
145
 
 
146
  if (ide_battery_monitor_get_on_battery ())
 
147
    {
 
148
      gdouble energy;
 
149
 
 
150
      energy = ide_battery_monitor_get_energy_percentage ();
 
151
      should_conserve = (energy != 0.0) && (energy < CONSERVE_THRESHOLD);
 
152
    }
 
153
 
 
154
  return should_conserve;
 
155
}
 
156
 
 
157
void
 
158
_ide_battery_monitor_shutdown (void)
 
159
{
 
160
  G_LOCK (proxy_lock);
 
161
 
 
162
  if (--gUPowerHold == 0)
 
163
    {
 
164
      g_clear_object (&gUPowerProxy);
 
165
      g_clear_object (&gUPowerDeviceProxy);
 
166
    }
 
167
 
 
168
  G_UNLOCK (proxy_lock);
 
169
}
 
170
 
 
171
void
 
172
_ide_battery_monitor_init (void)
 
173
{
 
174
  g_autoptr(GDBusProxy) proxy = NULL;
 
175
  g_autoptr(GDBusProxy) device_proxy = NULL;
 
176
 
 
177
  G_LOCK (proxy_lock);
 
178
  gUPowerHold++;
 
179
  G_UNLOCK (proxy_lock);
 
180
 
 
181
  proxy = ide_battery_monitor_get_proxy ();
 
182
  device_proxy = ide_battery_monitor_get_device_proxy ();
 
183
}