Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[QUESTION] acpid handlers not working #25248

Closed
cmacrae opened this issue Apr 26, 2017 · 2 comments
Closed

[QUESTION] acpid handlers not working #25248

cmacrae opened this issue Apr 26, 2017 · 2 comments

Comments

@cmacrae
Copy link
Contributor

cmacrae commented Apr 26, 2017

Issue description

I am attempting to declare some actions to take upon acpid events.
For instance, I would like to launch a rofi menu when pressing the power button, or run slock & systemctl suspend -i when closing my laptop's lid.

However, the declarations I've made in my configuration don't seem to operate as I'd expect.
I can see the files written by the properties I'm defining (an action file and an event file), and they seem correct, yet when these events happen, it doesn't seem the actions are triggered.

Steps to reproduce

Relevant portions of configuration.nix (two examples).

  1. Using services.acpid.handlers.lidEventCommands/powerEventCommands:
  # Slock & suspend on lid close
  # Power menu a la rofi on power button
  services.acpid = {
    lidEventCommands = "sudo -u cmacrae slock & systemctl suspend -i";
    powerEventCommands = ''
OPTIONS="Reboot\nKexec\nPower-off\nSuspend\nHibernate"
LAUNCHER="rofi -width 30 -dmenu -i -p rofi-power:"

option=`echo -e $OPTIONS | $LAUNCHER | awk '{print $1}' | tr -d '\r\n'`
case $option in
  Reboot)
    systemctl reboot
    ;;
  Kexec)
    systemctl kexec
    ;;
  Power-off)
    systemctl poweroff
    ;;
  Suspend)
    systemctl suspend
    ;;
  Hibernate)
    systemctl hibernate
    ;;
  Exit)
    eval $1
    ;;
  *)
    ;;
esac
'';
  };
  1. Using services.acpid.handlers.powerEvent
  services.acpid.handlers = {
    powerEvent = {
      event = "button/power.*";
      action =
        ''
	  OPTIONS="Reboot\nKexec\nPower-off\nSuspend\nHibernate"
	  LAUNCHER="rofi -width 30 -dmenu -i -p rofi-power:"
	  
	  option=`echo -e $OPTIONS | $LAUNCHER | awk '{print $1}' | tr -d '\r\n'`
	  case $option in
	    Reboot)
              systemctl reboot
              ;;
	    Kexec)
	      systemctl kexec
	      ;;
      	    Power-off)
              systemctl poweroff
              ;;
      	    Suspend)
              systemctl suspend
              ;;
      	    Hibernate)
              systemctl hibernate
              ;;
            Exit)
              eval $1
	      ;;
      	    *)
            ;;
    	  esac
        '';
    };
    lidEvent = {
      event = "button/lid.*";
      action =
        ''
	  sudo -u cmacrae slock & systemctl suspend -i
	'';
    };
  };

Debugging from shell (follow along)

macbook $ systemctl status acpid
● acpid.service - ACPI Daemon
   Loaded: loaded (/nix/store/j9ri99skqfb8xr42psqrnk7j34pmspsp-unit-acpid.service/acpid.service; enabled; vendor preset: enabled)
   Active: active (running) since Tue 2017-04-25 09:16:31 BST; 6h ago
 Main PID: 755 (acpid)
    Tasks: 1 (limit: 4915)
   Memory: 1.8M
      CPU: 2.264s
   CGroup: /system.slice/acpid.service
           └─755 acpid --confdir /nix/store/cdca2d9vg3d3gkgfsk5amp9gpblcsw6z-acpi-events

Apr 25 09:16:31 macbook acpid[755]: client connected from 820[0:0]
Apr 25 09:16:31 macbook acpid[755]: 1 client rule loaded
Apr 25 09:16:31 macbook acpid[755]: client connected from 820[0:0]
Apr 25 09:16:31 macbook acpid[755]: 1 client rule loaded
Apr 25 14:15:22 macbook acpid[755]: client 820[0:0] has disconnected
Apr 25 14:15:22 macbook acpid[755]: client 820[0:0] has disconnected
Apr 25 14:15:22 macbook acpid[755]: client connected from 15964[0:0]
Apr 25 14:15:22 macbook acpid[755]: 1 client rule loaded
Apr 25 14:15:22 macbook acpid[755]: client connected from 15964[0:0]
Apr 25 14:15:22 macbook acpid[755]: 1 client rule loaded

macbook $ ls /nix/store/cdca2d9vg3d3gkgfsk5amp9gpblcsw6z-acpi-events
acEvent  lidEvent  powerEvent

macbook $ cat /nix/store/cdca2d9vg3d3gkgfsk5amp9gpblcsw6z-acpi-events/powerEvent
event=button/power.*
action=/nix/store/7mg1v8n52zl14cjlnz3j1gby0vc268sk-powerEvent.sh

macbook $ cat /nix/store/7mg1v8n52zl14cjlnz3j1gby0vc268sk-powerEvent.sh
#! /nix/store/fi3mbd2ml4pbgzyasrlnp0wyy6qi48fh-bash-4.4-p5/bin/sh
OPTIONS="Reboot\nKexec\nPower-off\nSuspend\nHibernate"
LAUNCHER="rofi -width 30 -dmenu -i -p rofi-power:"

option=`echo -e $OPTIONS | $LAUNCHER | awk '{print $1}' | tr -d '\r\n'`
case $option in
  Reboot)
    systemctl reboot
    ;;
  Kexec)
    systemctl kexec
    ;;
  Power-off)
    systemctl poweroff
    ;;
  Suspend)
    systemctl suspend
    ;;
  Hibernate)
    systemctl hibernate
    ;;
  Exit)
    eval $1
    ;;
  *)
    ;;
esac

Results

When pressing the power button on my system, nothing happens.
If I manually execute the above script, it works as expected.
Watching journalctl -f, I can see the power button presses happening.

Other notes

I have the power button set to ignore in logind, so it doesn't intercept any presses (and shut my system down).

Technical details

  • System: NixOS - 17.03.1033.99dfb6dce3 (Gorilla)
  • Nix version: nix-env (Nix) 1.11.8
  • Nixpkgs version: 17.03.1033.99dfb6dce3
@cmacrae
Copy link
Contributor Author

cmacrae commented Apr 27, 2017

Hmm, after a thought last night, I woke up this morning and replaced the power button even command with a simple echo to file:

  services.acpid = {
    powerEventCommands = ''
    echo "Power button pressed" > /tmp/pwrbtn
    '';
  };

And, alas; the file is there after pressing the power button... so obviously this is in fact an error on my part with the scripts! I'll close this off.

@cmacrae cmacrae closed this as completed Apr 27, 2017
@cmacrae
Copy link
Contributor Author

cmacrae commented Apr 27, 2017

EDIT: Updated to include double invocation workaround.
And here's what I've finally ended up with (working!).
The power button script seems to be invoked twice (perhaps once for key press, and once for key release?), so I've included a little logic to work around it. The same can be said for lid close and open.

Figure I'd share here in case anyone finds it handy:

  # acpid
  services.acpid = {

    # Slock & suspend on lid close
    lidEventCommands = ''
    export PATH=/run/wrappers/bin:/run/current-system/sw/bin:$PATH
    export DISPLAY=":0.0"
    export XAUTHORITY="/home/cmacrae/.Xauthority"
    export LID_STATE=$(awk '{print$NF}' /proc/acpi/button/lid/LID0/state)
    if [[ $LID_STATE == "closed" ]]; then
      sudo -u cmacrae slock
    else
      rmmod brcmfmac
      modprobe brcmfmac
      systemctl restart wpa_supplicant
    fi
    '';

    # Power menu a la rofi on power button
    powerEventCommands = ''
    export PATH=/run/current-system/sw/bin:$PATH
    export DISPLAY=":0.0"
    export XAUTHORITY="/home/cmacrae/.Xauthority"
    OPTIONS="Reboot\nKexec\nPower-off\nSuspend\nHibernate\nExit"
    LAUNCHER="rofi -lines 6 -width 20 -dmenu -i -p rofi-power:"
    
    menu (){
      option=`echo -e $OPTIONS | $LAUNCHER | awk '{print $1}' | tr -d '\r\n'`
      case $option in
        Reboot)
          systemctl reboot
          ;;
        Kexec)
          systemctl kexec
          ;;
        Power-off)
          systemctl poweroff
          ;;
        Suspend)
          sudo -u cmacrae slock & \
  	    systemctl suspend
          ;;
        Hibernate)
          systemctl hibernate
          ;;
        Exit)
          eval $1
          ;;
        *)
          ;;
      esac
    }

    # Handle double invocation
    if [ ! -f /tmp/rofipwr.hasrun ]; then
      menu
      touch /tmp/rofipwr.hasrun
    else
      rm -f /tmp/rofipwr.hasrun
    fi
    '';
  };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant