summaryrefslogtreecommitdiff
path: root/gio/gpowerprofilemonitordbus.c
blob: ac6f8a88c7a82a1199bb0afe3d96432ca0a2f78e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/* GIO - GLib Input, Output and Streaming Library
 *
 * Copyright 2019 Red Hat, Inc.
 * Copyrgith 2021 Igalia S.L.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
 */

#include "config.h"

#include "gpowerprofilemonitor.h"
#include "gpowerprofilemonitordbus.h"
#include "gioerror.h"
#include "ginitable.h"
#include "giomodule-priv.h"
#include "glibintl.h"
#include "glib/gstdio.h"
#include "gcancellable.h"
#include "gdbusproxy.h"
#include "gdbusnamewatching.h"

#define G_POWER_PROFILE_MONITOR_DBUS_GET_INITABLE_IFACE(o) (G_TYPE_INSTANCE_GET_INTERFACE ((o), G_TYPE_INITABLE, GInitable))

static void g_power_profile_monitor_dbus_iface_init (GPowerProfileMonitorInterface *iface);
static void g_power_profile_monitor_dbus_initable_iface_init (GInitableIface *iface);

struct _GPowerProfileMonitorDBus
{
  GObject parent_instance;

  guint watch_id;
  GCancellable *cancellable;
  GDBusProxy *proxy;
  gulong signal_id;

  gboolean power_saver_enabled;
};

typedef enum
{
  PROP_POWER_SAVER_ENABLED = 1,
} GPowerProfileMonitorDBusProperty;

#define POWERPROFILES_DBUS_NAME "net.hadess.PowerProfiles"
#define POWERPROFILES_DBUS_IFACE "net.hadess.PowerProfiles"
#define POWERPROFILES_DBUS_PATH "/net/hadess/PowerProfiles"

G_DEFINE_TYPE_WITH_CODE (GPowerProfileMonitorDBus, g_power_profile_monitor_dbus, G_TYPE_OBJECT,
                         G_IMPLEMENT_INTERFACE (G_TYPE_INITABLE,
                                                g_power_profile_monitor_dbus_initable_iface_init)
                         G_IMPLEMENT_INTERFACE (G_TYPE_POWER_PROFILE_MONITOR,
                                                g_power_profile_monitor_dbus_iface_init)
                         _g_io_modules_ensure_extension_points_registered ();
                         g_io_extension_point_implement (G_POWER_PROFILE_MONITOR_EXTENSION_POINT_NAME,
                                                         g_define_type_id,
                                                         "dbus",
                                                         30))

static void
g_power_profile_monitor_dbus_init (GPowerProfileMonitorDBus *dbus)
{
  dbus->power_saver_enabled = FALSE;
}

static void
ppd_properties_changed_cb (GDBusProxy *proxy,
                           GVariant   *changed_properties,
                           GStrv      *invalidated_properties,
                           gpointer    user_data)
{
  GPowerProfileMonitorDBus *dbus = user_data;
  const char *active_profile;
  gboolean enabled;

  if (!g_variant_lookup (changed_properties, "ActiveProfile", "&s", &active_profile))
    return;

  enabled = g_strcmp0 (active_profile, "power-saver") == 0;
  if (enabled == dbus->power_saver_enabled)
    return;

  dbus->power_saver_enabled = enabled;
  g_object_notify (G_OBJECT (dbus), "power-saver-enabled");
}

static void
ppd_proxy_cb (GObject      *source_object,
              GAsyncResult *res,
              gpointer      user_data)
{
  GPowerProfileMonitorDBus *dbus = user_data;
  GVariant *active_profile_variant;
  GDBusProxy *proxy;
  GError *error = NULL;
  const char *active_profile;
  gboolean power_saver_enabled;

  proxy = g_dbus_proxy_new_finish (res, &error);
  if (!proxy)
    {
      g_debug ("GPowerProfileMonitorDBus: Failed to create PowerProfiles D-Bus proxy: %s",
               error->message);
      g_error_free (error);
      return;
    }

  active_profile_variant = g_dbus_proxy_get_cached_property (proxy, "ActiveProfile");
  if (active_profile_variant != NULL &&
      g_variant_is_of_type (active_profile_variant, G_VARIANT_TYPE_STRING))
    {
      active_profile = g_variant_get_string (active_profile_variant, NULL);
      power_saver_enabled = g_strcmp0 (active_profile, "power-saver") == 0;
      if (power_saver_enabled != dbus->power_saver_enabled)
        {
          dbus->power_saver_enabled = power_saver_enabled;
          g_object_notify (G_OBJECT (dbus), "power-saver-enabled");
        }
    }
  g_clear_pointer (&active_profile_variant, g_variant_unref);

  dbus->signal_id = g_signal_connect (G_OBJECT (proxy), "g-properties-changed",
                                      G_CALLBACK (ppd_properties_changed_cb), dbus);
  dbus->proxy = g_steal_pointer (&proxy);
}

static void
ppd_appeared_cb (GDBusConnection *connection,
                 const gchar     *name,
                 const gchar     *name_owner,
                 gpointer         user_data)
{
  GPowerProfileMonitorDBus *dbus = user_data;

  g_dbus_proxy_new (connection,
                    G_DBUS_PROXY_FLAGS_NONE,
                    NULL,
                    POWERPROFILES_DBUS_NAME,
                    POWERPROFILES_DBUS_PATH,
                    POWERPROFILES_DBUS_IFACE,
                    dbus->cancellable,
                    ppd_proxy_cb,
                    dbus);
}

static void
ppd_vanished_cb (GDBusConnection *connection,
                 const gchar     *name,
                 gpointer         user_data)
{
  GPowerProfileMonitorDBus *dbus = user_data;

  g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
  g_clear_object (&dbus->proxy);

  dbus->power_saver_enabled = FALSE;
  g_object_notify (G_OBJECT (dbus), "power-saver-enabled");
}

static void
g_power_profile_monitor_dbus_get_property (GObject    *object,
                                           guint       prop_id,
                                           GValue     *value,
                                           GParamSpec *pspec)
{
  GPowerProfileMonitorDBus *dbus = G_POWER_PROFILE_MONITOR_DBUS (object);

  switch ((GPowerProfileMonitorDBusProperty) prop_id)
    {
    case PROP_POWER_SAVER_ENABLED:
      g_value_set_boolean (value, dbus->power_saver_enabled);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
    }
}

static gboolean
g_power_profile_monitor_dbus_initable_init (GInitable     *initable,
                                            GCancellable  *cancellable,
                                            GError       **error)
{
  GPowerProfileMonitorDBus *dbus = G_POWER_PROFILE_MONITOR_DBUS (initable);

  dbus->cancellable = g_cancellable_new ();
  dbus->watch_id = g_bus_watch_name (G_BUS_TYPE_SYSTEM,
                                     POWERPROFILES_DBUS_NAME,
                                     G_BUS_NAME_WATCHER_FLAGS_AUTO_START,
                                     ppd_appeared_cb,
                                     ppd_vanished_cb,
                                     dbus,
                                     NULL);

  return TRUE;
}

static void
g_power_profile_monitor_dbus_finalize (GObject *object)
{
  GPowerProfileMonitorDBus *dbus = G_POWER_PROFILE_MONITOR_DBUS (object);

  g_cancellable_cancel (dbus->cancellable);
  g_clear_object (&dbus->cancellable);
  g_clear_signal_handler (&dbus->signal_id, dbus->proxy);
  g_clear_object (&dbus->proxy);
  g_clear_handle_id (&dbus->watch_id, g_bus_unwatch_name);

  G_OBJECT_CLASS (g_power_profile_monitor_dbus_parent_class)->finalize (object);
}

static void
g_power_profile_monitor_dbus_class_init (GPowerProfileMonitorDBusClass *nl_class)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (nl_class);

  gobject_class->get_property = g_power_profile_monitor_dbus_get_property;
  gobject_class->finalize = g_power_profile_monitor_dbus_finalize;

  g_object_class_override_property (gobject_class, PROP_POWER_SAVER_ENABLED, "power-saver-enabled");
}

static void
g_power_profile_monitor_dbus_iface_init (GPowerProfileMonitorInterface *monitor_iface)
{
}

static void
g_power_profile_monitor_dbus_initable_iface_init (GInitableIface *iface)
{
  iface->init = g_power_profile_monitor_dbus_initable_init;
}