Blame SOURCES/0001-network-Request-periodic-Wi-Fi-scans.patch

b8fc81
From bbce097905a43a3d25d4ee9994837b27e653a111 Mon Sep 17 00:00:00 2001
b8fc81
From: Bastien Nocera <hadess@hadess.net>
b8fc81
Date: Tue, 20 Feb 2018 17:09:35 +0100
b8fc81
Subject: [PATCH 1/2] network: Request periodic Wi-Fi scans
b8fc81
b8fc81
As NetworkManager from version 1.10 doesn't handle background scanning
b8fc81
itself, to minimise battery drain, we need to periodically request it to
b8fc81
scan for Wi-Fi Access Points.
b8fc81
b8fc81
We now do this request every 15 seconds, as NetworkManager-applet and
b8fc81
gnome-shell do, and disable that periodic scan if Wi-Fi is disabled.
b8fc81
b8fc81
https://bugzilla.gnome.org/show_bug.cgi?id=793647
b8fc81
---
b8fc81
 panels/network/net-device-wifi.c | 52 ++++++++++++++++++++++++++++++++++++++--
b8fc81
 1 file changed, 50 insertions(+), 2 deletions(-)
b8fc81
b8fc81
diff --git a/panels/network/net-device-wifi.c b/panels/network/net-device-wifi.c
b8fc81
index 290f7cd3f..8fc9f8aa1 100644
b8fc81
--- a/panels/network/net-device-wifi.c
b8fc81
+++ b/panels/network/net-device-wifi.c
b8fc81
@@ -37,6 +37,8 @@
b8fc81
 #include "connection-editor/net-connection-editor.h"
b8fc81
 #include "net-device-wifi.h"
b8fc81
 
b8fc81
+#define PERIODIC_WIFI_SCAN_TIMEOUT 15
b8fc81
+
b8fc81
 #define NET_DEVICE_WIFI_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), NET_TYPE_DEVICE_WIFI, NetDeviceWifiPrivate))
b8fc81
 
b8fc81
 typedef enum {
b8fc81
@@ -61,6 +63,8 @@ struct _NetDeviceWifiPrivate
b8fc81
         gchar                   *selected_ssid_title;
b8fc81
         gchar                   *selected_connection_id;
b8fc81
         gchar                   *selected_ap_id;
b8fc81
+        guint                    scan_id;
b8fc81
+        GCancellable            *cancellable;
b8fc81
 };
b8fc81
 
b8fc81
 G_DEFINE_TYPE (NetDeviceWifi, net_device_wifi, NET_TYPE_DEVICE)
b8fc81
@@ -263,6 +267,16 @@ net_device_wifi_access_point_changed (NMDeviceWifi *nm_device_wifi,
b8fc81
         populate_ap_list (device_wifi);
b8fc81
 }
b8fc81
 
b8fc81
+static void
b8fc81
+disable_scan_timeout (NetDeviceWifi *device_wifi)
b8fc81
+{
b8fc81
+        g_debug ("Disabling periodic Wi-Fi scan");
b8fc81
+        if (device_wifi->priv->scan_id > 0) {
b8fc81
+                g_source_remove (device_wifi->priv->scan_id);
b8fc81
+                device_wifi->priv->scan_id = 0;
b8fc81
+        }
b8fc81
+}
b8fc81
+
b8fc81
 static void
b8fc81
 wireless_enabled_toggled (NMClient       *client,
b8fc81
                           GParamSpec     *pspec,
b8fc81
@@ -282,6 +296,7 @@ wireless_enabled_toggled (NMClient       *client,
b8fc81
 
b8fc81
         device_wifi->priv->updating_device = TRUE;
b8fc81
         gtk_switch_set_active (sw, enabled);
b8fc81
+        disable_scan_timeout (device_wifi);
b8fc81
         device_wifi->priv->updating_device = FALSE;
b8fc81
 }
b8fc81
 
b8fc81
@@ -515,6 +530,21 @@ out:
b8fc81
         g_free (last_used);
b8fc81
 }
b8fc81
 
b8fc81
+static gboolean
b8fc81
+request_scan (gpointer user_data)
b8fc81
+{
b8fc81
+        NetDeviceWifi *device_wifi = user_data;
b8fc81
+        NMDevice *nm_device;
b8fc81
+
b8fc81
+        g_debug ("Periodic Wi-Fi scan requested");
b8fc81
+
b8fc81
+        nm_device = net_device_get_nm_device (NET_DEVICE (device_wifi));
b8fc81
+        nm_device_wifi_request_scan_async (NM_DEVICE_WIFI (nm_device),
b8fc81
+                                           device_wifi->priv->cancellable, NULL, NULL);
b8fc81
+
b8fc81
+        return G_SOURCE_CONTINUE;
b8fc81
+}
b8fc81
+
b8fc81
 static void
b8fc81
 nm_device_wifi_refresh_ui (NetDeviceWifi *device_wifi)
b8fc81
 {
b8fc81
@@ -534,22 +564,30 @@ nm_device_wifi_refresh_ui (NetDeviceWifi *device_wifi)
b8fc81
         if (device_is_hotspot (device_wifi)) {
b8fc81
                 nm_device_wifi_refresh_hotspot (device_wifi);
b8fc81
                 show_hotspot_ui (device_wifi);
b8fc81
+                disable_scan_timeout (device_wifi);
b8fc81
                 return;
b8fc81
         }
b8fc81
 
b8fc81
-        nm_device = net_device_get_nm_device (NET_DEVICE (device_wifi));
b8fc81
+        client = net_object_get_client (NET_OBJECT (device_wifi));
b8fc81
+
b8fc81
+        if (device_wifi->priv->scan_id == 0 &&
b8fc81
+            nm_client_wireless_get_enabled (client)) {
b8fc81
+                device_wifi->priv->scan_id = g_timeout_add_seconds (PERIODIC_WIFI_SCAN_TIMEOUT,
b8fc81
+                                                                    request_scan, device_wifi);
b8fc81
+                request_scan (device_wifi);
b8fc81
+        }
b8fc81
 
b8fc81
         dialog = device_wifi->priv->details_dialog;
b8fc81
 
b8fc81
         ap = g_object_get_data (G_OBJECT (dialog), "ap");
b8fc81
         connection = g_object_get_data (G_OBJECT (dialog), "connection");
b8fc81
 
b8fc81
+        nm_device = net_device_get_nm_device (NET_DEVICE (device_wifi));
b8fc81
         active_ap = nm_device_wifi_get_active_access_point (NM_DEVICE_WIFI (nm_device));
b8fc81
 
b8fc81
         state = nm_device_get_state (nm_device);
b8fc81
 
b8fc81
         /* keep this in sync with the signal handler setup in cc_network_panel_init */
b8fc81
-        client = net_object_get_client (NET_OBJECT (device_wifi));
b8fc81
         wireless_enabled_toggled (client, NULL, device_wifi);
b8fc81
 
b8fc81
         if (ap != active_ap)
b8fc81
@@ -647,6 +685,8 @@ device_off_toggled (GtkSwitch *sw,
b8fc81
         client = net_object_get_client (NET_OBJECT (device_wifi));
b8fc81
         active = gtk_switch_get_active (sw);
b8fc81
         nm_client_wireless_set_enabled (client, active);
b8fc81
+        if (!active)
b8fc81
+                disable_scan_timeout (device_wifi);
b8fc81
 }
b8fc81
 
b8fc81
 static void
b8fc81
@@ -1525,6 +1565,12 @@ net_device_wifi_finalize (GObject *object)
b8fc81
         NetDeviceWifi *device_wifi = NET_DEVICE_WIFI (object);
b8fc81
         NetDeviceWifiPrivate *priv = device_wifi->priv;
b8fc81
 
b8fc81
+        if (priv->cancellable) {
b8fc81
+                g_cancellable_cancel (priv->cancellable);
b8fc81
+                g_clear_object (&priv->cancellable);
b8fc81
+        }
b8fc81
+        disable_scan_timeout (device_wifi);
b8fc81
+
b8fc81
         g_clear_pointer (&priv->details_dialog, gtk_widget_destroy);
b8fc81
         g_object_unref (priv->builder);
b8fc81
         g_free (priv->selected_ssid_title);
b8fc81
@@ -2185,6 +2231,8 @@ net_device_wifi_init (NetDeviceWifi *device_wifi)
b8fc81
                 return;
b8fc81
         }
b8fc81
 
b8fc81
+        device_wifi->priv->cancellable = g_cancellable_new ();
b8fc81
+
b8fc81
         widget = GTK_WIDGET (gtk_builder_get_object (device_wifi->priv->builder,
b8fc81
                                                      "details_dialog"));
b8fc81
         device_wifi->priv->details_dialog = widget;
b8fc81
-- 
b8fc81
2.14.3
b8fc81