17

What is the quickest way to quickly disable the standby feature when closing the lid? I very much like this behaviour but especially when playing music, I would like to close my lid without switching my machine into standby.

However, I do not want to permanently disable this feature but just temporarily turn it off until I finished listening to music, for example.

Maybe there is an indicator similar to Caffeine?

enter image description here

orschiro
  • 13,317
  • 17
  • 87
  • 161

2 Answers2

19

The script below will toggle the close-lid action between "nothing" and "suspend":

#!/usr/bin/env python3
import subprocess

key = ["org.gnome.settings-daemon.plugins.power",
       "lid-close-ac-action", "lid-close-battery-action"]

currstate = subprocess.check_output(["gsettings", "get",
    key[0], key[1]]).decode("utf-8").strip()

if currstate == "'suspend'":
    command = "'nothing'"
    subprocess.Popen(["notify-send", "Lid closes with no action"])
else:
    command = "'suspend'"
    subprocess.Popen(["notify-send", "Suspend will be activated when lid closes"])

for k in [key[1], key[2]]:
    subprocess.Popen(["gsettings", "set", key[0], k, command])

...And notify what is the currently set state:

enter image description here

How to use

Simply:

  • Copy the script into an empty file, save it as toggle_lid.py
  • Add it to a shortcut key: choose: System Settings > "Keyboard" > "Shortcuts" > "Custom Shortcuts". Click the "+" and add the command:

    python3 /path/to/toggle_lid.py
    

Explanation

The current state of the close-lid action setting can be retrieved by the command(s)

gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action

(on power), and

gsettings get org.gnome.settings-daemon.plugins.power lid-close-battery-action

(on battery)

The script reads the current state, and sets the opposite ('suspend'/'nothing') with the command:

gsettings get org.gnome.settings-daemon.plugins.power lid-close-ac-action '<action>'

Optionally (additionally)

Optionally/additionally, you can run an indicator as a detector to show what is the current state of the lid- setting. It will show:

enter image description here

...in the panel, if the suspend will be prevented on closing the lid, It will show a grey one if not.

enter image description here

The script

#!/usr/bin/env python3
import subprocess
import os
import time
import signal
import gi
gi.require_version('Gtk', '3.0')
gi.require_version('AppIndicator3', '0.1')
from gi.repository import Gtk, AppIndicator3, GObject
from threading import Thread

key = ["org.gnome.settings-daemon.plugins.power",
       "lid-close-ac-action", "lid-close-battery-action"]

currpath = os.path.dirname(os.path.realpath(__file__))

def runs():
    # The test True/False
    return subprocess.check_output([
        "gsettings", "get", key[0], key[1]
        ]).decode("utf-8").strip() == "'suspend'"

class Indicator():
    def __init__(self):
        self.app = 'show_proc'
        iconpath = currpath+"/nocolor.png"
        self.indicator = AppIndicator3.Indicator.new(
            self.app, iconpath,
            AppIndicator3.IndicatorCategory.OTHER)
        self.indicator.set_status(AppIndicator3.IndicatorStatus.ACTIVE)       
        self.indicator.set_menu(self.create_menu())
        self.update = Thread(target=self.check_runs)
        # daemonize the thread to make the indicator stopable
        self.update.setDaemon(True)
        self.update.start()     

    def check_runs(self):
        # the function (thread), checking for the process to run
        runs1 = None
        while True:
            time.sleep(1)
            runs2 = runs()
            # if there is a change in state, update the icon
            if runs1 != runs2:
                if runs2:
                    # set the icon to show
                    GObject.idle_add(
                        self.indicator.set_icon,
                        currpath+"/nocolor.png",
                        priority=GObject.PRIORITY_DEFAULT
                        )
                else:
                    # set the icon to hide
                    GObject.idle_add(
                        self.indicator.set_icon,
                        currpath+"/green.png",
                        priority=GObject.PRIORITY_DEFAULT
                        )
            runs1 = runs2

    def create_menu(self):
        menu = Gtk.Menu()
        # quit
        item_quit = Gtk.MenuItem('Quit')
        item_quit.connect('activate', self.stop)
        menu.append(item_quit)
        menu.show_all()
        return menu

    def stop(self, source):
        Gtk.main_quit()

Indicator()
GObject.threads_init()
signal.signal(signal.SIGINT, signal.SIG_DFL)
Gtk.main()

How to use

  1. Copy the script above into an empty file, save it as show_state.py
  2. Copy both icons below (right click -> save as), and save them in one and the same directory as show_proc.py, and exactly named as indicated below

    green.png

    enter image description here

    nocolor.png

    enter image description here

  3. Now test- run show_state.py by the command:

    python3 /path/to/show_state.py
    

    and change the current state by pressing the shortcut you set the first section of this answer.

  4. If all works fine, add the following to startup applications:

    /bin/bash -c "sleep 15 && python3 /path/to/show_state.py"
    

Note

The detector- indicator above is an edited version of this answer. Simply by changing the test in the function runs() (and optionally the according panel icons), you can use it to show the state of anything that is True or False.

Jacob Vlijm
  • 83,767
  • Excellent solution with the indicator! One question: Why did you decide for a transparent icon? – orschiro Aug 22 '16 at 08:15
  • @orschiro I consider(ed) the ste where the lid- closing will suspend as the default situation. The green one as an indication you can close the lid without action(s). It coul be any icon however. Suggestions? – Jacob Vlijm Aug 22 '16 at 08:26
  • Maybe like the green one but in fading-grey? It's slightly irritating to see an empty space in the indicator bar due to the transparency of the icon. – orschiro Aug 22 '16 at 08:45
  • @orschiro haha, now I see what you mean :). edited. – Jacob Vlijm Aug 22 '16 at 08:51
6

Another option would be changing from "Suspend" to "Do Nothing" in Ubuntu Settings - Power:

Ubuntu Settings - Power

PS: This doesn't provide an indicator in the notifications area but is simpler than creating a script for new users.

PPS: In this screen snapshot the UPS is for Cable Modem + Sony Android TV, not the laptop which has battery... HAHA.

  • Eh, Ok. Manual way, takes a few clicks here and there. But fairly decent. – Sergiy Kolodyazhnyy Aug 21 '16 at 20:55
  • @Serg I actually like the script posted by Jacob. Will probably play with it some day after I make Ubuntu 16.04 / Kernel 4.7.2 more stable on my system. Indeed my suspend on lid close never even worked out of the 14.04 to 16.04 upgrade box... had to setup a parameter in systemd which assumes when lid is closed you want TV to stay active and simply want to dim laptop screen . In hind sight I should have posted instructions for that too! – WinEunuuchs2Unix Aug 21 '16 at 23:04
  • Yeah, before 16.04 those options didn't really work for me. I personally wanted to post the file editing as answer, but Jacob has beaten me to it :) – Sergiy Kolodyazhnyy Aug 21 '16 at 23:08
  • In Unity 22.04 the Power settings has almost nothing compared to that screenshot! – Déjà vu Sep 26 '22 at 07:06
  • @Déjàvu The answer was written in 2016, so Ubuntu 16.04 is the version. – WinEunuuchs2Unix Sep 26 '22 at 11:13