[gnome-break-timer] Replace Gnome.IdleMonitor with our original implementation



commit 8625f78251cf51eb7e3d930a1d49f1c7dd4c757d
Author: Dylan McCall <dylan dylanmccall ca>
Date:   Tue Nov 24 20:47:36 2020 -0800

    Replace Gnome.IdleMonitor with our original implementation
    
    We need to run some particular code when Mutter appears and disappears,
    and we are not able to do this with Gnome.IdleMonitor's abstraction.

 build-aux/flatpak/org.gnome.BreakTimer.Devel.json  | 11 ----
 meson.build                                        |  2 -
 src/common/IMutterIdleMonitor.vala                 | 34 +++++++++++
 src/common/meson.build                             |  1 +
 .../activity/MutterActivityMonitorBackend.vala     | 71 ++++++++++++++++++----
 src/daemon/meson.build                             |  1 -
 6 files changed, 94 insertions(+), 26 deletions(-)
---
diff --git a/build-aux/flatpak/org.gnome.BreakTimer.Devel.json 
b/build-aux/flatpak/org.gnome.BreakTimer.Devel.json
index 4b291c9..78b99ea 100644
--- a/build-aux/flatpak/org.gnome.BreakTimer.Devel.json
+++ b/build-aux/flatpak/org.gnome.BreakTimer.Devel.json
@@ -73,17 +73,6 @@
                 }
             ]
         },
-        {
-            "name" : "libgnome-desktop",
-            "buildsystem" : "meson",
-            "sources" : [
-                {
-                    "type" : "git",
-                    "url" : "https://gitlab.gnome.org/GNOME/gnome-desktop.git";,
-                    "tag" : "3.38.1"
-                }
-            ]
-        },
         {
             "name" : "gnome-break-timer",
             "buildsystem" : "meson",
diff --git a/meson.build b/meson.build
index c306fac..d88d766 100644
--- a/meson.build
+++ b/meson.build
@@ -13,7 +13,6 @@ cairo_dep = dependency('cairo')
 gio_dep = dependency('gio-2.0')
 gio_unix_dep = dependency('gio-unix-2.0')
 glib_dep = dependency('glib-2.0')
-gnome_desktop_dep = dependency('gnome-desktop-3.0')
 gobject_dep = dependency('gobject-2.0')
 gsound_dep = dependency('gsound')
 gtk_dep = dependency('gtk+-3.0')
@@ -87,7 +86,6 @@ build_conf_quoted.set_quoted('DAEMON_APPLICATION_ID', daemon_application_id)
 build_conf_quoted.set_quoted('DAEMON_OBJECT_PATH', daemon_object_path)
 
 add_project_arguments(['-DGETTEXT_PACKAGE="' + gettext_package + '"'], language: 'c')
-add_project_arguments(['-DGNOME_DESKTOP_USE_UNSTABLE_API=1'], language: 'c')
 add_project_arguments(['--vapidir', src_dir], language: 'vala')
 
 run_script_conf = configuration_data()
diff --git a/src/common/IMutterIdleMonitor.vala b/src/common/IMutterIdleMonitor.vala
new file mode 100644
index 0000000..ac767e0
--- /dev/null
+++ b/src/common/IMutterIdleMonitor.vala
@@ -0,0 +1,34 @@
+/* IMutterIdleMonitor.vala
+ *
+ * Copyright 2020 Dylan McCall <dylan dylanmccall ca>
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-or-later
+ */
+
+namespace BreakTimer.Common {
+
+[DBus (name = "org.gnome.Mutter.IdleMonitor")]
+public interface IMutterIdleMonitor : GLib.Object {
+    public signal void watch_fired (uint32 id);
+
+    public abstract uint32 add_idle_watch (uint64 interval_ms) throws GLib.DBusError, GLib.IOError;
+    public abstract uint32 add_user_active_watch () throws GLib.DBusError, GLib.IOError;
+    public abstract uint64 get_idletime () throws GLib.DBusError, GLib.IOError;
+    public abstract void remove_watch (uint32 id) throws GLib.DBusError, GLib.IOError;
+    public abstract void reset_idletime () throws GLib.DBusError, GLib.IOError;
+}
+
+}
diff --git a/src/common/meson.build b/src/common/meson.build
index 015c838..ad251bd 100644
--- a/src/common/meson.build
+++ b/src/common/meson.build
@@ -3,6 +3,7 @@ common_lib_sources = files(
     'IBreakTimer.vala',
     'IFreedesktopApplication.vala',
     'IGnomeScreenSaver.vala',
+    'IMutterIdleMonitor.vala',
     'IPortalBackground.vala',
     'IPortalRequest.vala',
     'ISessionStatus.vala',
diff --git a/src/daemon/activity/MutterActivityMonitorBackend.vala 
b/src/daemon/activity/MutterActivityMonitorBackend.vala
index 279e9b0..24453bd 100644
--- a/src/daemon/activity/MutterActivityMonitorBackend.vala
+++ b/src/daemon/activity/MutterActivityMonitorBackend.vala
@@ -24,7 +24,9 @@ using BreakTimer.Daemon.Util;
 namespace BreakTimer.Daemon.Activity {
 
 public class MutterActivityMonitorBackend : ActivityMonitorBackend, GLib.Initable {
-    private Gnome.IdleMonitor? gnome_idle_monitor;
+    private GLib.DBusConnection dbus_connection;
+
+    private IMutterIdleMonitor? mutter_idle_monitor;
     private uint32 idle_watch_id;
     private uint32 user_active_watch_id;
 
@@ -41,27 +43,66 @@ public class MutterActivityMonitorBackend : ActivityMonitorBackend, GLib.Initabl
     }
 
     ~MutterActivityMonitorBackend () {
-        if (this.gnome_idle_monitor != null && this.idle_watch_id > 0) {
-            this.gnome_idle_monitor.remove_watch (this.idle_watch_id);
+        if (this.mutter_idle_monitor != null && this.idle_watch_id > 0) {
+            this.mutter_idle_monitor.remove_watch (this.idle_watch_id);
         }
     }
 
-    public override bool init (GLib.Cancellable? cancellable) throws GLib.Error {
-        this.gnome_idle_monitor = new Gnome.IdleMonitor ();
-        this.gnome_idle_monitor.init (cancellable);
-        this.idle_watch_id = this.gnome_idle_monitor.add_idle_watch (
-            IDLE_WATCH_INTERVAL_MS, this.idle_watch_cb
+    public override bool init (GLib.Cancellable? cancellable) throws GLib.Error {        
this.dbus_connection = GLib.Bus.get_sync (GLib.BusType.SESSION, cancellable);
+        GLib.Bus.watch_name_on_connection (
+            this.dbus_connection,
+            "org.gnome.Mutter.IdleMonitor",
+            GLib.BusNameWatcherFlags.NONE,
+            this.mutter_idle_monitor_appeared,
+            this.mutter_idle_monitor_disappeared
         );
         return true;
     }
 
+    private void mutter_idle_monitor_appeared () {
+        try {
+            this.mutter_idle_monitor = GLib.Bus.get_proxy_sync (
+                GLib.BusType.SESSION,
+                "org.gnome.Mutter.IdleMonitor",
+                "/org/gnome/Mutter/IdleMonitor/Core"
+            );
+            this.mutter_idle_monitor.watch_fired.connect (this.mutter_idle_monitor_watch_fired_cb);
+            this.idle_watch_id = this.mutter_idle_monitor.add_idle_watch (IDLE_WATCH_INTERVAL_MS);
+            this.update_idle_time ();
+        } catch (GLib.IOError error) {
+            this.mutter_idle_monitor = null;
+            GLib.warning ("Error connecting to mutter idle monitor service: %s", error.message);
+        } catch (GLib.DBusError error) {
+            this.mutter_idle_monitor = null;
+            GLib.warning ("Error adding mutter idle watch: %s", error.message);
+        }
+    }
+
+    private void mutter_idle_monitor_disappeared () {
+        GLib.warning ("Mutter idle monitor disappeared");
+        this.mutter_idle_monitor = null;
+        this.idle_watch_id = 0;
+    }
+
+    private void mutter_idle_monitor_watch_fired_cb (uint32 id) {
+        if (id == this.idle_watch_id) {
+            this.idle_watch_cb ();
+        } else if (id == this.user_active_watch_id) {
+            this.user_active_watch_cb ();
+        }
+    }
+
     private void idle_watch_cb () {
         this.update_idle_time ();
         this.user_is_active = false;
         this.stop_active_idle_poll ();
-        this.user_active_watch_id = this.gnome_idle_monitor.add_user_active_watch (
-            this.user_active_watch_cb
-        );
+        try {
+            this.user_active_watch_id = this.mutter_idle_monitor.add_user_active_watch ();
+        } catch (GLib.IOError error) {
+            GLib.warning ("Error connecting to mutter idle monitor service: %s", error.message);
+        } catch (GLib.DBusError error) {
+            GLib.warning ("Error adding mutter user active watch: %s", error.message);
+        }
     }
 
     private void user_active_watch_cb () {
@@ -97,7 +138,13 @@ public class MutterActivityMonitorBackend : ActivityMonitorBackend, GLib.Initabl
     }
 
     private void update_idle_time () {
-        this.last_idle_time_ms = this.gnome_idle_monitor.get_idletime ();
+        try {
+            this.last_idle_time_ms = this.mutter_idle_monitor.get_idletime ();
+        } catch (GLib.IOError error) {
+            GLib.warning ("Error connecting to mutter idle monitor service: %s", error.message);
+        } catch (GLib.DBusError error) {
+            GLib.warning ("Error getting mutter idletime: %s", error.message);
+        }
         this.last_idle_time_update_time_ms = TimeUnit.get_monotonic_time_ms ();
         this.user_is_active = (this.last_idle_time_ms < IDLE_WATCH_INTERVAL_MS);
     }
diff --git a/src/daemon/meson.build b/src/daemon/meson.build
index 075b255..348496d 100644
--- a/src/daemon/meson.build
+++ b/src/daemon/meson.build
@@ -33,7 +33,6 @@ daemon_lib_dependencies = [
     config_lib_dep,
     gio_dep,
     gio_unix_dep,
-    gnome_desktop_dep,
     gsound_dep,
     gtk_dep,
     json_glib_dep,


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