You are not logged in.

#1 2014-05-01 05:12:31

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

[sovled] Handling HandleLidSwitch interactively depending on AC state

Hi there,

I would like to set HandleLidSwitch depending on the AC state of my laptop.

If I run the laptop on battery, HandleLidSwitch should be set to suspend. If I am on charging, HandleLidSwitch should be set to ignore.

The HandleLidSwitch key is defined in /etc/systemd/login.conf. Thus I wrote a small script to detect changes of the AC state when the system is running, and change the HandleLidSwitch setting:

sed -i 's/HandleLidSwitch=ignore/HandleLidSwitch=ignore/' /etc/systemd/logind.conf

OR

sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf

However, the system does not consider my change in a running session because logind.conf is only considered once on user login but not during an already running session, correct?

Is there any way to overcome this shortcoming?

Thanks!

Last edited by orschiro (2014-06-12 12:54:41)

Offline

#2 2014-05-01 07:10:59

tomk
Forum Fellow
From: Ireland
Registered: 2004-07-21
Posts: 9,839

Re: [sovled] Handling HandleLidSwitch interactively depending on AC state

Where exactly did you put this script? How is it triggered?

Offline

#3 2014-05-01 07:46:31

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [sovled] Handling HandleLidSwitch interactively depending on AC state

@tomk

An udev rule detects the AC state change and runs the the script:

udev rule

SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="0", RUN+="/usr/bin/powersaving battery"
SUBSYSTEM=="power_supply", ENV{POWER_SUPPLY_ONLINE}=="1", RUN+="/usr/bin/powersaving AC"

script

#!/bin/bash

# Run powersaving script
case $1 in
  battery)
    echo "Running powersaving on battery"

    # Enable suspend on lid closing
    sed -i 's/HandleLidSwitch=ignore/HandleLidSwitch=suspend/' /etc/systemd/logind.conf
  ;;
  AC)
    echo "Running powersaving on AC"
    
    # Disable suspend on lid closing
    sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf
  ;;
esac

Offline

#4 2014-06-11 11:27:25

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [sovled] Handling HandleLidSwitch interactively depending on AC state

I did more investitgation and found a command that might to the trick: systemd-inhibit

It provides a flag called --what=handle-lid-switch

However, I am struggling to understand how I could use this with a command that:

* changes the lid closing action to suspend
* is triggered only when the laptop is on battery. Otherwise the lid closing action should be none (default)
* is triggered only on lid closing

Basically I need something like:

systemd-inhibit --what=handle-lid-switch SOME_COMMAND_HERE

Offline

#5 2014-06-12 12:55:45

orschiro
Member
Registered: 2009-06-04
Posts: 2,136
Website

Re: [sovled] Handling HandleLidSwitch interactively depending on AC state

The solution turned out to be very simple. I just have to restart the systemd-logind service after the sed command:

# Disable suspend on lid closing
sed -i 's/HandleLidSwitch=suspend/HandleLidSwitch=ignore/' /etc/systemd/logind.conf
systemctl restart systemd-logind

Offline

Board footer

Powered by FluxBB