1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /* This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License along
13 * with this program; if not, write to the Free Software Foundation, Inc.,
14 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 * (C) Copyright 2012 Red Hat, Inc.
17 * Author: Matthias Clasen <mclasen@redhat.com>
20 #include "nm-default.h"
26 #include "nm-core-internal.h"
28 #include "nm-sleep-monitor.h"
30 #define UPOWER_DBUS_SERVICE "org.freedesktop.UPower"
32 struct _NMSleepMonitor {
33 GObject parent_instance;
35 GDBusProxy *upower_proxy;
38 struct _NMSleepMonitorClass {
39 GObjectClass parent_class;
41 void (*sleeping) (NMSleepMonitor *monitor);
42 void (*resuming) (NMSleepMonitor *monitor);
51 static guint signals[LAST_SIGNAL] = {0};
53 G_DEFINE_TYPE (NMSleepMonitor, nm_sleep_monitor, G_TYPE_OBJECT);
55 /********************************************************************/
58 upower_sleeping_cb (GDBusProxy *proxy, gpointer user_data)
60 nm_log_dbg (LOGD_SUSPEND, "Received UPower sleeping signal");
61 g_signal_emit (user_data, signals[SLEEPING], 0);
65 upower_resuming_cb (GDBusProxy *proxy, gpointer user_data)
67 nm_log_dbg (LOGD_SUSPEND, "Received UPower resuming signal");
68 g_signal_emit (user_data, signals[RESUMING], 0);
72 nm_sleep_monitor_init (NMSleepMonitor *self)
76 self->upower_proxy = g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
77 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
78 G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES,
81 "/org/freedesktop/UPower",
82 "org.freedesktop.UPower",
84 if (self->upower_proxy) {
85 _nm_dbus_signal_connect (self->upower_proxy, "Sleeping", NULL,
86 G_CALLBACK (upower_sleeping_cb), self);
87 _nm_dbus_signal_connect (self->upower_proxy, "Resuming", NULL,
88 G_CALLBACK (upower_resuming_cb), self);
90 nm_log_warn (LOGD_SUSPEND, "could not initialize UPower D-Bus proxy: %s", error->message);
96 finalize (GObject *object)
98 NMSleepMonitor *self = NM_SLEEP_MONITOR (object);
100 g_clear_object (&self->upower_proxy);
102 G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize (object);
106 nm_sleep_monitor_class_init (NMSleepMonitorClass *klass)
108 GObjectClass *gobject_class;
110 gobject_class = G_OBJECT_CLASS (klass);
112 gobject_class->finalize = finalize;
114 signals[SLEEPING] = g_signal_new (NM_SLEEP_MONITOR_SLEEPING,
115 NM_TYPE_SLEEP_MONITOR,
117 G_STRUCT_OFFSET (NMSleepMonitorClass, sleeping),
118 NULL, /* accumulator */
119 NULL, /* accumulator data */
120 g_cclosure_marshal_VOID__VOID,
122 signals[RESUMING] = g_signal_new (NM_SLEEP_MONITOR_RESUMING,
123 NM_TYPE_SLEEP_MONITOR,
125 G_STRUCT_OFFSET (NMSleepMonitorClass, resuming),
126 NULL, /* accumulator */
127 NULL, /* accumulator data */
128 g_cclosure_marshal_VOID__VOID,
132 NM_DEFINE_SINGLETON_GETTER (NMSleepMonitor, nm_sleep_monitor_get, NM_TYPE_SLEEP_MONITOR);
134 /* ---------------------------------------------------------------------------------------------------- */