You are not logged in.

#1 2014-08-12 07:14:23

mwillems
Member
Registered: 2014-08-09
Posts: 89

(SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

I'm trying to figure out a way to configure differential behavior for laptop lid close triggers on a system using Gnome 3.12.2 as my desktop environment. Specifically, I'd like the laptop to suspend when closed on battery power, but do nothing (or blank the screen) when closed while plugged in. 

Gnome-tweak-tool used to have options for this, but currently the heading "When Laptop Lid is Closed" shows no configurable options beneath it.  As I understand it from reading the forums (e.g. http://bbs.archlinux.org/viewtopic.php?pid=1396282) and Gnome bug reports (http://bugzilla.gnome.org/show_bug.cgi?id=687277), the relevant options have been removed from gnome-tweak-tool because the gnome development team would prefer that systemd handle lid close.  But systemd's lid close handling seems to be either on or off (i.e. it can't be configured to give a different result on battery than it does on A/C).  Attempts to manually set the previously relevant gnome power plugin keys using gsettings throw an error "No such key 'lid-close-battery-action,'" and the relevant keys are no longer visible in dconf. 

Three questions:
1) Is anyone aware of a way to create differential lid close behavior using gnome, assuming that systemd's handling is first disabled? 
2) The official login.d documentation doesn't seem to suggest there's anyway to achieve the configuration I want, but is anyone aware of any systemd options I'm missing/misunderstanding? 
3) If the answers to 1) and 2) are negative, is anyone aware of a good alternative solution?  It looks like XFCE's power manager may still have differential options, but I'd prefer not to switch desktop environments or try to script a solution if there's a readily available solution that I'm missing.

Last edited by mwillems (2014-08-13 23:49:39)

Offline

#2 2014-08-12 19:50:04

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: (SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

This looks promising: https://bbs.archlinux.org/viewtopic.php … 7#p1398807
Have you checked the wikis?

You would have to add tests to check if run on battery to the relevant options. From the handler you could call systemd services, that would do exactly what you want. This is how I would try to accomplish it, this is untested and pure speculation however.

There are some directives for power and lid in the systemd.directives manual, they seems to correspond to options in logind.conf. However, if those could not be used to trigger a service, you should be able trigger one using udev directives, I have not tried that myself yet.

Offline

#3 2014-08-12 23:21:06

mwillems
Member
Registered: 2014-08-09
Posts: 89

Re: (SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

Thanks for that, that thread does look promising.  I did see the acpid entry on the wiki, which also looked promising, but wasn't sure how all the pieces fit together.  There's an acpi command that will test for A/C state, so I can probably use that as a condition for a nested if in the acpid handler script.  Thanks for your help, I'll try and get this working, and if I get it sorted, I'll mark the thread solved (with a code example).

Also, for the record, I tested this morning and XFCE's power manager does work correctly for this if anyone reading this wants an "out of the box solution," provided that you tell systemd to ignore lid close events.

Offline

#4 2014-08-13 23:48:05

mwillems
Member
Registered: 2014-08-09
Posts: 89

Re: (SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

Ok I got it sorted based on your suggestion and I'll mark this solved.  Acpid was the answer, after disabling systemd's lid handling.  I just had to install acpi and acpid, start/enable the acpid.service, and then edit the relevant portion of /etc/acpi/handler.sh as shown:

    button/lid)
        case "$3" in
            close)
                logger 'LID closed'
                if [ "$(acpi -a | grep off)" ]; then
                        systemctl suspend
                fi
                ;;

Thanks again for the help, I learned a few things doing this, not least of which is that grep returns a non-zero value on STDERR when it doesn't find anything :-)

Last edited by mwillems (2014-08-13 23:54:03)

Offline

#5 2014-08-14 12:09:50

emeres
Member
Registered: 2013-10-08
Posts: 1,570

Re: (SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

I do not recall what a typical 'acpi -a' output is, but in bash the above could be also replaced with :

acpi -a | grep off && systemctl suspend;

Since you probably not interested in porting this, you might also want to use double square brackets. For more information read this. This is not really relevant for this case, but might prove useful in the future.

mwillems wrote:

Thanks again for the help, I learned a few things doing this, not least of which is that grep returns a non-zero value on STDERR when it doesn't find anything :-)

These might interest you as well:
http://www.tldp.org/LDP/abs/html/exit-status.html
http://www.tldp.org/LDP/abs/html/exitcodes.html

There is no reason to thank me, you have done all the work, but you are welcome.

Edit: Additional information from the wiki:

wiki note wrote:

systemd cannot handle AC and Battery ACPI events, so if you use Laptop Mode Tools or other similar tools acpid is still required.

Last edited by emeres (2014-08-14 20:45:32)

Offline

#6 2014-08-14 23:57:58

mwillems
Member
Registered: 2014-08-09
Posts: 89

Re: (SOLVED) Configuring Different Lid Close Behavior on A/C or Battery

I figured there was a more elegant way to do it.  I didn't realize that && was a short-circuit operator in bash until I saw your code snippet and puzzled over it.  I'm used to it being a logical operator from other contexts.  The code you provided works great, and has the advantage of being shorter and easier to read.

Thanks also for the links.  I was generally familiar with the idea of error exit statuses, but I didn't realize there was a conventional numeration, which is handy.  I was mostly surprised that a well-formed grep that didn't happen to find a match would even return an error status, if you see what I mean (after all, it confirmed that there were no matches).  After reading the links you provided, I realized that I was thinking of error exit statuses too narrowly.

Running Arch is definitely improving my Linux fluency :-)

Last edited by mwillems (2014-08-14 23:59:46)

Offline

Board footer

Powered by FluxBB