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:
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) { close(m_acpidsock); m_acpidsock=-1; } }
Update code:
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
KpexEA
source share