Blob Blame History Raw
From 6356afb4cf9724847c99a4c49dc0622161ab20b6 Mon Sep 17 00:00:00 2001
From: Rui Matos <tiagomatos@gmail.com>
Date: Sat, 1 Feb 2014 15:47:36 +0100
Subject: [PATCH] Add a tweak to ignore systemd's default lid switch action

---
 Makefile.am                       | 10 +++++++++-
 configure.ac                      |  1 +
 gnome-tweak-tool                  |  3 ++-
 gnome-tweak-tool-lid-inhibitor.in | 39 +++++++++++++++++++++++++++++++++++++
 gtweak/defs.py.in                 |  1 +
 gtweak/tweaks/tweak_shell.py      | 41 +++++++++++++++++++++++++++++++++++++--
 gtweak/utils.py                   | 14 +++++++++++--
 7 files changed, 103 insertions(+), 6 deletions(-)
 create mode 100644 gnome-tweak-tool-lid-inhibitor.in

diff --git a/Makefile.am b/Makefile.am
index d7446bc..181bff2 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -1,6 +1,13 @@
 bin_SCRIPTS = \
 	gnome-tweak-tool
 
+libexec_SCRIPTS = \
+	gnome-tweak-tool-lid-inhibitor
+
+gnome-tweak-tool-lid-inhibitor: gnome-tweak-tool-lid-inhibitor.in Makefile
+	$(AM_V_GEN)sed -e s!\@PYTHON\@!@PYTHON@! < $< > $@
+	chmod +x $@
+
 #nobase means gtweak dir is preserved in site-packages
 appdir = $(pythondir)
 nobase_dist_app_PYTHON = \
@@ -8,7 +15,8 @@ nobase_dist_app_PYTHON = \
 	$(wildcard $(srcdir)/gtweak/tweaks/*.py)
 
 EXTRA_DIST = \
-	$(bin_SCRIPTS)
+	$(bin_SCRIPTS) \
+	gnome-tweak-tool-lid-inhibitor.in
 
 CLEANFILES = \
 	$(wildcard $(srcdir)/gtweak/*.pyc) \
diff --git a/configure.ac b/configure.ac
index 09f070f..8baa112 100644
--- a/configure.ac
+++ b/configure.ac
@@ -29,6 +29,7 @@ AS_AC_EXPAND(PKGDATADIR, "${datadir}/${PACKAGE}")
 AS_AC_EXPAND(GSETTINGSSCHEMADIR, "${gsettingsschemadir}")
 AS_AC_EXPAND(TWEAKDIR, "${pythondir}/gtweak/tweaks")
 AS_AC_EXPAND(LOCALEDIR, "${localedir}")
+AS_AC_EXPAND(LIBEXECDIR, "${libexecdir}")
 
 AC_CONFIG_FILES([ po/Makefile.in
 Makefile
diff --git a/gnome-tweak-tool b/gnome-tweak-tool
index 6104959..6cd5f52 100755
--- a/gnome-tweak-tool
+++ b/gnome-tweak-tool
@@ -37,7 +37,7 @@ if __name__ == '__main__':
     options, args = parser.parse_args()
 
     try:
-        from gtweak.defs import GSETTINGS_SCHEMA_DIR, TWEAK_DIR, DATA_DIR, PKG_DATA_DIR, LOCALE_DIR
+        from gtweak.defs import GSETTINGS_SCHEMA_DIR, TWEAK_DIR, DATA_DIR, PKG_DATA_DIR, LOCALE_DIR, LIBEXEC_DIR
         _defs_present = True
     except ImportError:
         GSETTINGS_SCHEMA_DIR = TWEAK_DIR = DATA_DIR = PKG_DATA_DIR = LOCALE_DIR = ""
@@ -58,6 +58,7 @@ if __name__ == '__main__':
     gtweak.DATA_DIR = DATA_DIR
     gtweak.PKG_DATA_DIR = PKG_DATA_DIR
     gtweak.LOCALE_DIR = LOCALE_DIR
+    gtweak.LIBEXEC_DIR = LIBEXEC_DIR
     gtweak.ENABLE_TEST = options.test
     gtweak.APP_NAME = "gnome-tweak-tool"
     gtweak.VERBOSE = options.verbose
diff --git a/gnome-tweak-tool-lid-inhibitor.in b/gnome-tweak-tool-lid-inhibitor.in
new file mode 100644
index 0000000..7a48f6b
--- /dev/null
+++ b/gnome-tweak-tool-lid-inhibitor.in
@@ -0,0 +1,39 @@
+#!@PYTHON@
+
+import gi
+gi.require_version("GLib", "2.0")
+
+from gi.repository import Gio, GLib
+
+def on_activate(app):
+    if app._inhibitor:
+        return
+
+    app.hold()
+
+    bus = Gio.bus_get_sync(Gio.BusType.SYSTEM, None)
+    var, fdlist = bus.call_with_unix_fd_list_sync('org.freedesktop.login1',
+                                                  '/org/freedesktop/login1',
+                                                  'org.freedesktop.login1.Manager',
+                                                  'Inhibit',
+                                                  GLib.Variant('(ssss)',
+                                                               ('handle-lid-switch',
+                                                                'gnome-tweak-tool-lid-inhibitor',
+                                                                'user preference',
+                                                                'block')),
+                                                  None, 0, -1, None, None)
+    app._inhibitor = Gio.UnixInputStream(fd=fdlist.steal_fds()[var[0]])
+
+def on_quit_action(action, param, app):
+    app.quit()
+
+if __name__ == '__main__':
+    app = Gio.Application(application_id='org.gnome.tweak-tool.lid-inhibitor', flags=0)
+    app.connect('activate', on_activate)
+    app._inhibitor = None
+
+    action = Gio.SimpleAction(name='quit')
+    app.add_action(action)
+    action.connect('activate', on_quit_action, app)
+
+    app.run([])
diff --git a/gtweak/defs.py.in b/gtweak/defs.py.in
index 12180db..6c8fdf8 100644
--- a/gtweak/defs.py.in
+++ b/gtweak/defs.py.in
@@ -4,3 +4,4 @@ GSETTINGS_SCHEMA_DIR = "@GSETTINGSSCHEMADIR@"
 TWEAK_DIR = "@TWEAKDIR@"
 VERSION = "@VERSION@"
 LOCALE_DIR = "@LOCALEDIR@"
+LIBEXEC_DIR = "@LIBEXECDIR@"
diff --git a/gtweak/tweaks/tweak_shell.py b/gtweak/tweaks/tweak_shell.py
index d6ffd35..28faa44 100644
--- a/gtweak/tweaks/tweak_shell.py
+++ b/gtweak/tweaks/tweak_shell.py
@@ -24,7 +24,8 @@ import pprint
 
 from gi.repository import Gtk, GLib, GObject, Gio
 
-from gtweak.utils import walk_directories, extract_zip_file, make_combo_list_with_default
+import gtweak
+from gtweak.utils import walk_directories, extract_zip_file, make_combo_list_with_default, AutostartManager
 from gtweak.gsettings import GSettingsSetting, GSettingsMissingError, GSettingsFakeSetting
 from gtweak.gshellwrapper import GnomeShellFactory
 from gtweak.tweakmodel import Tweak, TweakGroup, TWEAK_GROUP_THEME, TWEAK_GROUP_SHELL, TWEAK_SORT_LAST
@@ -242,6 +243,40 @@ class StaticWorkspaceTweak(Tweak):
         self.widget = build_label_beside_widget(self.name, hb)
         self.widget_for_size_group = hb
 
+class IgnoreLidSwitchTweak(Tweak):
+    def __init__(self, **options):
+        Tweak.__init__(self, _("Don't suspend on lid close"), _("Prevents the default systemd action on laptop lid close"), **options)
+
+        self._inhibitor_name = "gnome-tweak-tool-lid-inhibitor"
+        self._inhibitor_path = "%s/%s" % (gtweak.LIBEXEC_DIR, self._inhibitor_name)
+
+        self._dfile = AutostartManager("ignore-lid-switch-tweak.desktop",
+                                       exec_cmd = self._inhibitor_path,
+                                       create = True)
+        sw = Gtk.Switch()
+        sw.set_active(self._sync_inhibitor())
+        sw.connect('notify::active', self._notify_active)
+
+        self.widget = build_label_beside_widget(self.name, sw)
+
+    def _notify_active(self, sw, _param):
+        self._dfile.update_start_at_login(sw.get_active())
+        self._sync_inhibitor()
+
+    def _sync_inhibitor(self):
+        if (self._dfile.is_start_at_login_enabled()):
+            GLib.spawn_command_line_async(self._inhibitor_path)
+            return True
+        else:
+            bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)
+            bus.call('org.gnome.tweak-tool.lid-inhibitor',
+                     '/org/gnome/tweak_tool/lid_inhibitor',
+                     'org.gtk.Actions',
+                     'Activate',
+                     GLib.Variant('(sava{sv})', ('quit', [], {})),
+                     None, 0, -1, None, None, None)
+            return False
+
 sg = build_horizontal_sizegroup()
 
 TWEAKS = (
@@ -261,5 +296,7 @@ TWEAK_GROUPS = (
             GSettingsComboEnumTweak("org.gnome.settings-daemon.plugins.power", "button-power", size_group=sg),
             GSettingsComboEnumTweak("org.gnome.settings-daemon.plugins.xrandr", "default-monitors-setup", size_group=sg),
             GSettingsSwitchTweak("org.gnome.mutter", "workspaces-only-on-primary", schema_filename="org.gnome.shell.gschema.xml", loaded=_shell_loaded),
-            StaticWorkspaceTweak(size_group=sg, loaded=_shell_loaded)),
+            StaticWorkspaceTweak(size_group=sg, loaded=_shell_loaded),
+            IgnoreLidSwitchTweak(size_group=sg)
+        ),
 )
diff --git a/gtweak/utils.py b/gtweak/utils.py
index d3e5411..63f109b 100644
--- a/gtweak/utils.py
+++ b/gtweak/utils.py
@@ -112,11 +112,12 @@ def execute_subprocess(cmd_then_args, block=True):
         return stdout, stderr, p.returncode
 
 class AutostartManager:
-    def __init__(self, desktop_filename, autostart_desktop_filename="", exec_cmd="", extra_exec_args=""):
+    def __init__(self, desktop_filename, autostart_desktop_filename="", exec_cmd="", extra_exec_args="", create=False):
         self.desktop_filename = desktop_filename
         self._autostart_desktop_filename = autostart_desktop_filename or desktop_filename
         self._exec_cmd = exec_cmd
         self._extra_exec_args = " %s\n" % extra_exec_args
+        self._create = create
 
         user_autostart_dir = os.path.join(GLib.get_user_config_dir(), "autostart")
         if not os.path.isdir(user_autostart_dir):
@@ -135,6 +136,12 @@ class AutostartManager:
         logging.debug("Found desktop file: %s" % self._desktop_file)
         logging.debug("User autostart desktop file: %s" % self._user_autostart_file)
 
+    def _create_user_autostart_file(self):
+        f = open(self._user_autostart_file, "w")
+        f.write("[Desktop Entry]\nType=Application\nName=%s\nExec=%s\n" %
+                (self.desktop_filename[0:-len('.desktop')], self._exec_cmd + self._extra_exec_args))
+        f.close()
+
     def _get_system_autostart_files(self):
         return [
             os.path.join(d, "autostart", self._autostart_desktop_filename)
@@ -207,7 +214,10 @@ class AutostartManager:
 
         if update:
             if (not self._desktop_file) or (not os.path.exists(self._desktop_file)):
-                logging.critical("Could not find desktop file: %s" % self._desktop_file)
+                if self._create:
+                    self._create_user_autostart_file()
+                else:
+                    logging.critical("Could not find desktop file: %s" % self._desktop_file)
                 return
 
             logging.info("Adding autostart %s" % self._user_autostart_file)
-- 
1.8.3.1