[simple-scan] Inhibit screensaver while scanning



commit ac2f26557e26a2180276724c60864cfa125ac876
Author: Stéphane Fillion <stphanef3724 gmail com>
Date:   Tue May 30 16:47:09 2017 -0400

    Inhibit screensaver while scanning

 src/meson.build      |    1 +
 src/screensaver.vala |   25 ++++++++++++++++++++++
 src/simple-scan.vala |   56 +++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 81 insertions(+), 1 deletions(-)
---
diff --git a/src/meson.build b/src/meson.build
index f0083f9..9e40e42 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -28,6 +28,7 @@ simple_scan = executable ('simple-scan',
                             'preferences-dialog.vala',
                             'simple-scan.vala',
                             'scanner.vala',
+                            'screensaver.vala',
                             'autosave-manager.vala' ] + resources,
                           dependencies: dependencies,
                           vala_args: vala_args,
diff --git a/src/screensaver.vala b/src/screensaver.vala
new file mode 100644
index 0000000..ef2dfb8
--- /dev/null
+++ b/src/screensaver.vala
@@ -0,0 +1,25 @@
+/*
+ * Copyright (C) 2017 Stéphane Fillion
+ * Authors: Stéphane Fillion <stphanef3724 gmail com>
+ *
+ * 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. See http://www.gnu.org/copyleft/gpl.html the full text of the
+ * license.
+ */
+
+[DBus (name = "org.freedesktop.ScreenSaver")]
+public interface FreedesktopScreensaver : Object
+{
+    public static FreedesktopScreensaver get_proxy () throws IOError
+    {
+        return Bus.get_proxy_sync (BusType.SESSION, "org.freedesktop.ScreenSaver", 
"/org/freedesktop/ScreenSaver");
+    }
+
+    [DBus (name = "Inhibit")]
+    public abstract uint32 inhibit (string application_name, string reason_for_inhibit) throws IOError;
+
+    [DBus (name = "UnInhibit")]
+    public abstract void uninhibit (uint32 cookie) throws IOError;
+}
diff --git a/src/simple-scan.vala b/src/simple-scan.vala
index a99efd3..7e8c641 100644
--- a/src/simple-scan.vala
+++ b/src/simple-scan.vala
@@ -38,6 +38,10 @@ public class SimpleScan : Gtk.Application
 
     public SimpleScan (ScanDevice? device = null)
     {
+        /* The inhibit () method use this */
+        Object (application_id: "org.gnome.simple-scan");
+        register_session = true;
+
         default_device = device;
     }
 
@@ -390,9 +394,59 @@ public class SimpleScan : Gtk.Application
         }
     }
 
+    private uint inhibit_cookie;
+    private FreedesktopScreensaver? fdss;
+
     private void scanner_scanning_changed_cb (Scanner scanner)
     {
-        app.scanning = scanner.is_scanning ();
+        var is_scanning = scanner.is_scanning ();
+
+        if (is_scanning)
+        {
+            /* Attempt to inhibit the screensaver when scanning */
+            var reason = _("Scan in progress");
+
+            /* This should work on Gnome, Budgie, Cinnamon, Mate, Unity, ...
+             * but will not work on KDE, LXDE, XFCE, ... */
+            inhibit_cookie = inhibit (app, Gtk.ApplicationInhibitFlags.IDLE, reason);
+
+            if (!is_inhibited (Gtk.ApplicationInhibitFlags.IDLE))
+            {
+                /* If the previous method didn't work, try the one
+                 * provided by Freedesktop. It should work with KDE,
+                 * LXDE, XFCE, and maybe others as well. */
+                try
+                {
+                    if ((fdss = FreedesktopScreensaver.get_proxy ()) != null)
+                    {
+                        inhibit_cookie = fdss.inhibit ("Simple-Scan", reason);
+                    }
+                }
+                catch (IOError error) {}
+            }
+        }
+        else
+        {
+            /* When finished scanning, uninhibit if inhibit was working */
+            if (inhibit_cookie != 0)
+            {
+                if (fdss == null)
+                        uninhibit (inhibit_cookie);
+                else
+                {
+                    try
+                    {
+                        fdss.uninhibit (inhibit_cookie);
+                    }
+                    catch (IOError error) {}
+                    fdss = null;
+                }
+
+                inhibit_cookie = 0;
+            }
+        }
+
+        app.scanning = is_scanning;
     }
 
     private void scan_cb (AppWindow ui, string? device, ScanOptions options)


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