[gnome-bluetooth] lib: Add API to check whether input devices are connected



commit a43e1689e3e32253e2203d28df8ffcd917d1ce00
Author: Bastien Nocera <hadess hadess net>
Date:   Thu Jan 20 12:20:33 2022 +0100

    lib: Add API to check whether input devices are connected
    
    This will figure out whether there are any connected input devices, so
    that gnome-shell (or gnome-control-center) can show a confirmation
    dialogue before really turning things off.
    
    Closes: #101

 lib/bluetooth-client.c  | 29 +++++++++++++++++++++++++++++
 lib/bluetooth-client.h  |  2 ++
 lib/gnome-bluetooth.map |  1 +
 tests/integration-test  | 48 ++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 80 insertions(+)
---
diff --git a/lib/bluetooth-client.c b/lib/bluetooth-client.c
index cf7ae67c..652d6777 100644
--- a/lib/bluetooth-client.c
+++ b/lib/bluetooth-client.c
@@ -1518,3 +1518,32 @@ bluetooth_client_connect_service_finish (BluetoothClient *client,
 
        return g_task_propagate_boolean (task, error);
 }
+
+gboolean
+bluetooth_client_has_connected_input_devices (BluetoothClient *client)
+{
+       guint i, n_items;
+       guint n_connected = 0;
+
+       n_items = g_list_model_get_n_items (G_LIST_MODEL (client->list_store));
+       for (i = 0; i < n_items; i++) {
+               g_autoptr(BluetoothDevice) device = NULL;
+               g_auto(GStrv) uuids = NULL;
+               gboolean connected = FALSE;
+
+               device = g_list_model_get_item (G_LIST_MODEL (client->list_store), i);
+               g_object_get (device,
+                             "connected", &connected,
+                             "uuids", &uuids, NULL);
+               if (!connected)
+                       continue;
+               if (!uuids)
+                       continue;
+               if (g_strv_contains ((const gchar * const *) uuids, "Human Interface Device") ||
+                   g_strv_contains ((const gchar * const *) uuids, "HumanInterfaceDeviceService"))
+                       n_connected++;
+       }
+       g_debug ("Found %i input devices connected", n_connected);
+
+       return n_connected > 0;
+}
diff --git a/lib/bluetooth-client.h b/lib/bluetooth-client.h
index 26effcaf..c79c460d 100644
--- a/lib/bluetooth-client.h
+++ b/lib/bluetooth-client.h
@@ -44,3 +44,5 @@ void bluetooth_client_connect_service (BluetoothClient     *client,
 gboolean bluetooth_client_connect_service_finish (BluetoothClient *client,
                                                  GAsyncResult    *res,
                                                  GError         **error);
+
+gboolean bluetooth_client_has_connected_input_devices (BluetoothClient *client);
diff --git a/lib/gnome-bluetooth.map b/lib/gnome-bluetooth.map
index 02e00cb2..d9a88dda 100644
--- a/lib/gnome-bluetooth.map
+++ b/lib/gnome-bluetooth.map
@@ -7,6 +7,7 @@ global:
   bluetooth_client_get_type;
   bluetooth_client_new;
   bluetooth_client_get_devices;
+  bluetooth_client_has_connected_input_devices;
   bluetooth_client_connect_service;
   bluetooth_client_connect_service_finish;
   bluetooth_client_set_trusted;
diff --git a/tests/integration-test b/tests/integration-test
index 2ad777cc..a79066ef 100755
--- a/tests/integration-test
+++ b/tests/integration-test
@@ -374,6 +374,50 @@ class OopTests(dbusmock.DBusTestCase):
         agent.unregister()
         self.wait_for_mainloop()
 
+    def test_connected_input_devices(self):
+        bus = dbus.SystemBus()
+        dbusmock_bluez = dbus.Interface(bus.get_object('org.bluez', '/'), 'org.bluez.Mock')
+
+        path = dbusmock_bluez.AddDevice('hci0', '11:22:33:44:55:66', 'My LE Mouse')
+        dev1 = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Mock')
+        dev1.UpdateProperties('org.bluez.Device1',
+                {'UUIDs': dbus.Array(['00001812-0000-1000-8000-00805f9b34fb'], variant_level=1)})
+
+        path = dbusmock_bluez.AddDevice('hci0', '11:22:33:44:55:67', 'My Classic Mouse')
+        dev2 = dbus.Interface(bus.get_object('org.bluez', path), 'org.freedesktop.DBus.Mock')
+        dev2.UpdateProperties('org.bluez.Device1',
+                {'UUIDs': dbus.Array(['00001124-0000-1000-8000-00805f9b34fb'], variant_level=1)})
+
+        self.wait_for_mainloop()
+        list_store = self.client.get_devices()
+        self.assertEqual(list_store.get_n_items(), 2)
+        self.assertEqual(self.client.has_connected_input_devices(), False)
+
+        dev1.UpdateProperties('org.bluez.Device1',
+                {'Connected': dbus.Boolean(True, variant_level=1)})
+
+        self.wait_for_mainloop()
+        self.assertEqual(self.client.has_connected_input_devices(), True)
+
+        dev1.UpdateProperties('org.bluez.Device1',
+                {'Connected': dbus.Boolean(False, variant_level=1)})
+
+        self.wait_for_mainloop()
+        self.assertEqual(self.client.has_connected_input_devices(), False)
+
+        dev2.UpdateProperties('org.bluez.Device1',
+                {'Connected': dbus.Boolean(True, variant_level=1)})
+
+        self.wait_for_mainloop()
+        self.assertEqual(self.client.has_connected_input_devices(), True)
+
+        dev2.UpdateProperties('org.bluez.Device1',
+                {'Connected': dbus.Boolean(False, variant_level=1)})
+
+        self.wait_for_mainloop()
+        self.assertEqual(self.client.has_connected_input_devices(), False)
+
+
 class Tests(dbusmock.DBusTestCase):
 
     @classmethod
@@ -471,5 +515,9 @@ class Tests(dbusmock.DBusTestCase):
     def test_agent(self):
         self.run_test_process()
 
+    def test_connected_input_devices(self):
+        self.dbusmock_bluez.AddAdapter('hci0', 'my-computer')
+        self.run_test_process()
+
 if __name__ == '__main__':
     unittest.main()


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]