How to determine when the user pressed the power button?

I have an arcade cocktail cabinet (without a keyboard, only a joystick and buttons) that works under Ubuntu 12.4.1, when the power button is pressed, a pop-up window appears and the system stops working, but when my full-screen game launch menu the application works, and pressing the button does not affect. I would like to capture an event when a button is pressed, so my application can cause a system shutdown. My menu application is written in C ++ and uses SDL. Any ideas on how I can capture the power off button click event?

Thanks to those who answered, here is the actual code that I used to make it work:

Class members:

int m_acpidsock; sockaddr_un m_acpidsockaddr; 

Installation Code:

 /* Connect to acpid socket */ m_acpidsock = socket(AF_UNIX, SOCK_STREAM, 0); if(m_acpidsock>=0) { m_acpidsockaddr.sun_family = AF_UNIX; strcpy(m_acpidsockaddr.sun_path,"/var/run/acpid.socket"); if(connect(m_acpidsock, (struct sockaddr *)&m_acpidsockaddr, 108)<0) { /* can't connect */ close(m_acpidsock); m_acpidsock=-1; } } 

Update code:

 /* check for any power events */ if(m_acpidsock) { char buf[1024]; int s=recv(m_acpidsock, buf, sizeof(buf), MSG_DONTWAIT); if(s>0) { buf[s]=0; printf("ACPID:%s\n\n",buf); if(!strncmp(buf,"button/power",12)) { setShutdown(); system("shutdown -P now"); } } } 

Close socket code:

 if(m_acpidsock>=0) { close(m_acpidsock); m_acpidsock=-1; } 

Finally, I needed to allow non-root users to disconnect and work with this line:

 sudo chmod u+s /sbin/shutdown 
+7
source share
3 answers

You can simply start the stream to read from /proc/events/acpi and decode the messages there.

But what about using acpid for this? You should listen to /var/run/acpid.socket , and when the message you care about comes in, do what you did.

See: http://www.linuxmanpages.com/man8/acpid.8.php

Hope this is helpful.

+5
source

Look at acpid , I think you can change one of the scripts in /etc/acpi/ specifically /etc/acpi/powerbtn.sh to add custom commands. You can also try reading /proc/acpi/event yourself.

+2
source

Things like pressing the power button trigger ACPI events that acpid disables the script in response to the configuration in / etc / acpi / events. In this case, you want / etc / acpi / powerbtn, which looks something like this:

 event=button[ /]power action=/etc/acpi/powerbtn.sh 

You can either configure /etc/acpi/powerbtn.sh, or point it to another script of your choice.

+2
source

All Articles