How can I quickly disable standby lid off in Ubuntu Unity 16.04?

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


Solution 1:

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.

Solution 2:

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.