mirror of
https://gitlab.freedesktop.org/NetworkManager/NetworkManager.git
synced 2025-12-25 10:40:08 +01:00
core: factor out the upower suspend/resume code (bgo #677694)
Factor the code that listens for upower sleeping and resuming signals out into a class code NMSleepMonitor.
This commit is contained in:
parent
27a14a7d11
commit
64fd8eea77
4 changed files with 214 additions and 26 deletions
|
|
@ -191,6 +191,8 @@ NetworkManager_SOURCES = \
|
|||
nm-session-monitor.h \
|
||||
nm-session-utils.c \
|
||||
nm-session-utils.h \
|
||||
nm-sleep-monitor.h \
|
||||
nm-sleep-monitor.c \
|
||||
nm-connection-provider.h \
|
||||
nm-connection-provider.c \
|
||||
nm-dispatcher.c \
|
||||
|
|
|
|||
|
|
@ -69,6 +69,7 @@
|
|||
#include "nm-device-factory.h"
|
||||
#include "wifi-utils.h"
|
||||
#include "nm-enum-types.h"
|
||||
#include "nm-sleep-monitor.h"
|
||||
|
||||
#if WITH_CONCHECK
|
||||
#include "nm-connectivity.h"
|
||||
|
|
@ -78,8 +79,6 @@
|
|||
#define NM_AUTOIP_DBUS_SERVICE "org.freedesktop.nm_avahi_autoipd"
|
||||
#define NM_AUTOIP_DBUS_IFACE "org.freedesktop.nm_avahi_autoipd"
|
||||
|
||||
#define UPOWER_DBUS_SERVICE "org.freedesktop.UPower"
|
||||
|
||||
static gboolean impl_manager_get_devices (NMManager *manager,
|
||||
GPtrArray **devices,
|
||||
GError **err);
|
||||
|
|
@ -228,7 +227,7 @@ typedef struct {
|
|||
guint modem_removed_id;
|
||||
|
||||
DBusGProxy *aipd_proxy;
|
||||
DBusGProxy *upower_proxy;
|
||||
NMSleepMonitor *sleep_monitor;
|
||||
|
||||
GSList *auth_chains;
|
||||
|
||||
|
|
@ -3288,16 +3287,16 @@ impl_manager_sleep (NMManager *self,
|
|||
}
|
||||
|
||||
static void
|
||||
upower_sleeping_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
sleeping_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
{
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received UPower sleeping signal");
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received sleeping signal");
|
||||
_internal_sleep (NM_MANAGER (user_data), TRUE);
|
||||
}
|
||||
|
||||
static void
|
||||
upower_resuming_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
resuming_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
{
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received UPower resuming signal");
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received resuming signal");
|
||||
_internal_sleep (NM_MANAGER (user_data), FALSE);
|
||||
}
|
||||
|
||||
|
|
@ -4049,8 +4048,8 @@ dispose (GObject *object)
|
|||
if (priv->aipd_proxy)
|
||||
g_object_unref (priv->aipd_proxy);
|
||||
|
||||
if (priv->upower_proxy)
|
||||
g_object_unref (priv->upower_proxy);
|
||||
if (priv->sleep_monitor)
|
||||
g_object_unref (priv->sleep_monitor);
|
||||
|
||||
if (priv->fw_monitor) {
|
||||
if (priv->fw_monitor_id)
|
||||
|
|
@ -4372,23 +4371,12 @@ nm_manager_init (NMManager *manager)
|
|||
} else
|
||||
nm_log_warn (LOGD_AUTOIP4, "could not initialize avahi-autoipd D-Bus proxy");
|
||||
|
||||
/* upower sleep/wake handling */
|
||||
priv->upower_proxy = dbus_g_proxy_new_for_name (g_connection,
|
||||
UPOWER_DBUS_SERVICE,
|
||||
"/org/freedesktop/UPower",
|
||||
"org.freedesktop.UPower");
|
||||
if (priv->upower_proxy) {
|
||||
dbus_g_proxy_add_signal (priv->upower_proxy, "Sleeping", G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (priv->upower_proxy, "Sleeping",
|
||||
G_CALLBACK (upower_sleeping_cb),
|
||||
manager, NULL);
|
||||
|
||||
dbus_g_proxy_add_signal (priv->upower_proxy, "Resuming", G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (priv->upower_proxy, "Resuming",
|
||||
G_CALLBACK (upower_resuming_cb),
|
||||
manager, NULL);
|
||||
} else
|
||||
nm_log_warn (LOGD_SUSPEND, "could not initialize UPower D-Bus proxy");
|
||||
/* sleep/wake handling */
|
||||
priv->sleep_monitor = nm_sleep_monitor_get ();
|
||||
g_signal_connect (priv->sleep_monitor, "Sleeping",
|
||||
G_CALLBACK (sleeping_cb), manager);
|
||||
g_signal_connect (priv->sleep_monitor, "Resuming",
|
||||
G_CALLBACK (resuming_cb), manager);
|
||||
|
||||
/* Listen for authorization changes */
|
||||
nm_auth_changed_func_register (authority_changed_cb, manager);
|
||||
|
|
|
|||
152
src/nm-sleep-monitor.c
Normal file
152
src/nm-sleep-monitor.c
Normal file
|
|
@ -0,0 +1,152 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* (C) Copyright 2012 Red Hat, Inc.
|
||||
* Author: Matthias Clasen <mclasen@redhat.com>
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dbus/dbus-glib-lowlevel.h>
|
||||
#include <dbus/dbus-glib.h>
|
||||
#include <gio/gio.h>
|
||||
#include "nm-logging.h"
|
||||
#include "nm-dbus-manager.h"
|
||||
|
||||
#include "nm-sleep-monitor.h"
|
||||
|
||||
#define UPOWER_DBUS_SERVICE "org.freedesktop.UPower"
|
||||
|
||||
struct _NMSleepMonitor {
|
||||
GObject parent_instance;
|
||||
|
||||
DBusGProxy *upower_proxy;
|
||||
};
|
||||
|
||||
struct _NMSleepMonitorClass {
|
||||
GObjectClass parent_class;
|
||||
|
||||
void (*sleeping) (NMSleepMonitor *monitor);
|
||||
void (*resuming) (NMSleepMonitor *monitor);
|
||||
};
|
||||
|
||||
|
||||
enum {
|
||||
SLEEPING,
|
||||
RESUMING,
|
||||
LAST_SIGNAL,
|
||||
};
|
||||
static guint signals[LAST_SIGNAL] = {0};
|
||||
|
||||
G_DEFINE_TYPE (NMSleepMonitor, nm_sleep_monitor, G_TYPE_OBJECT);
|
||||
|
||||
/********************************************************************/
|
||||
|
||||
static void
|
||||
upower_sleeping_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
{
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received UPower sleeping signal");
|
||||
g_signal_emit (user_data, signals[SLEEPING], 0);
|
||||
}
|
||||
|
||||
static void
|
||||
upower_resuming_cb (DBusGProxy *proxy, gpointer user_data)
|
||||
{
|
||||
nm_log_dbg (LOGD_SUSPEND, "Received UPower resuming signal");
|
||||
g_signal_emit (user_data, signals[RESUMING], 0);
|
||||
}
|
||||
|
||||
static void
|
||||
nm_sleep_monitor_init (NMSleepMonitor *self)
|
||||
{
|
||||
NMDBusManager *dbus_mgr;
|
||||
DBusGConnection *bus;
|
||||
|
||||
dbus_mgr = nm_dbus_manager_get ();
|
||||
bus = nm_dbus_manager_get_connection (dbus_mgr);
|
||||
self->upower_proxy = dbus_g_proxy_new_for_name (bus,
|
||||
UPOWER_DBUS_SERVICE,
|
||||
"/org/freedesktop/UPower",
|
||||
"org.freedesktop.UPower");
|
||||
if (self->upower_proxy) {
|
||||
dbus_g_proxy_add_signal (self->upower_proxy, "Sleeping", G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (self->upower_proxy, "Sleeping",
|
||||
G_CALLBACK (upower_sleeping_cb),
|
||||
self, NULL);
|
||||
|
||||
dbus_g_proxy_add_signal (self->upower_proxy, "Resuming", G_TYPE_INVALID);
|
||||
dbus_g_proxy_connect_signal (self->upower_proxy, "Resuming",
|
||||
G_CALLBACK (upower_resuming_cb),
|
||||
self, NULL);
|
||||
} else
|
||||
nm_log_warn (LOGD_SUSPEND, "could not initialize UPower D-Bus proxy");
|
||||
g_object_unref (bus);
|
||||
g_object_unref (dbus_mgr);
|
||||
}
|
||||
|
||||
static void
|
||||
finalize (GObject *object)
|
||||
{
|
||||
NMSleepMonitor *self = NM_SLEEP_MONITOR (object);
|
||||
|
||||
if (self->upower_proxy)
|
||||
g_object_unref (self->upower_proxy);
|
||||
|
||||
if (G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize != NULL)
|
||||
G_OBJECT_CLASS (nm_sleep_monitor_parent_class)->finalize (object);
|
||||
}
|
||||
|
||||
static void
|
||||
nm_sleep_monitor_class_init (NMSleepMonitorClass *klass)
|
||||
{
|
||||
GObjectClass *gobject_class;
|
||||
|
||||
gobject_class = G_OBJECT_CLASS (klass);
|
||||
|
||||
gobject_class->finalize = finalize;
|
||||
|
||||
signals[SLEEPING] = g_signal_new (NM_SLEEP_MONITOR_SLEEPING,
|
||||
NM_TYPE_SLEEP_MONITOR,
|
||||
G_SIGNAL_RUN_LAST,
|
||||
G_STRUCT_OFFSET (NMSleepMonitorClass, sleeping),
|
||||
NULL, /* accumulator */
|
||||
NULL, /* accumulator data */
|
||||
g_cclosure_marshal_VOID__VOID,
|
||||
G_TYPE_NONE, 0);
|
||||
signals[RESUMING] = g_signal_new (NM_SLEEP_MONITOR_RESUMING,
|
||||
NM_TYPE_SLEEP_MONITOR,
|
||||
G_SIGNAL_RUN_LAST,
|
||||
G_STRUCT_OFFSET (NMSleepMonitorClass, resuming),
|
||||
NULL, /* accumulator */
|
||||
NULL, /* accumulator data */
|
||||
g_cclosure_marshal_VOID__VOID,
|
||||
G_TYPE_NONE, 0);
|
||||
}
|
||||
|
||||
NMSleepMonitor *
|
||||
nm_sleep_monitor_get (void)
|
||||
{
|
||||
static NMSleepMonitor *singleton = NULL;
|
||||
|
||||
if (singleton)
|
||||
return g_object_ref (singleton);
|
||||
|
||||
singleton = NM_SLEEP_MONITOR (g_object_new (NM_TYPE_SLEEP_MONITOR, NULL));
|
||||
return singleton;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------------------------------------- */
|
||||
46
src/nm-sleep-monitor.h
Normal file
46
src/nm-sleep-monitor.h
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*- */
|
||||
/* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
||||
*
|
||||
* (C) Copyright 2012 Red Hat, Inc.
|
||||
* Author: Matthias Clasen <mclasen@redhat.com>
|
||||
*/
|
||||
|
||||
#ifndef NM_SLEEP_MONITOR_H
|
||||
#define NM_SLEEP_MONITOR_H
|
||||
|
||||
#include <glib-object.h>
|
||||
|
||||
G_BEGIN_DECLS
|
||||
|
||||
#define NM_TYPE_SLEEP_MONITOR (nm_sleep_monitor_get_type ())
|
||||
#define NM_SLEEP_MONITOR(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), NM_TYPE_SLEEP_MONITOR, NMSleepMonitor))
|
||||
#define NM_SLEEP_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), NM_TYPE_SLEEP_MONITOR, NMSleepMonitorClass))
|
||||
#define NM_SLEEP_MONITOR_GET_CLASS(o) (G_TYPE_INSTANCE_GET_CLASS ((o), NM_TYPE_SLEEP_MONITOR, NMSleepMonitorClass))
|
||||
#define NM_IS_SLEEP_MONITOR(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), NM_TYPE_SLEEP_MONITOR))
|
||||
#define NM_IS_SLEEP_MONITOR_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), NM_TYPE_SLEEP_MONITOR))
|
||||
|
||||
#define NM_SLEEP_MONITOR_SLEEPING "sleeping"
|
||||
#define NM_SLEEP_MONITOR_RESUMING "resuming"
|
||||
|
||||
typedef struct _NMSleepMonitor NMSleepMonitor;
|
||||
typedef struct _NMSleepMonitorClass NMSleepMonitorClass;
|
||||
|
||||
GType nm_sleep_monitor_get_type (void) G_GNUC_CONST;
|
||||
NMSleepMonitor *nm_sleep_monitor_get (void);
|
||||
|
||||
G_END_DECLS
|
||||
|
||||
#endif /* NM_SLEEP_MONITOR_H */
|
||||
|
||||
Loading…
Add table
Reference in a new issue