title: How can I detect when the user has pressed the power off button?
body: <p>I have an arcade cocktail cabinet (no keyboard, just a joystick and buttons) running Ubuntu 12.4.1, when the power button is pressed a popup appears and the system shuts down fine, but when my full-screen game launcher menu application is running then pressing the button has no effect. I would like to trap the event when the button is pressed so my app can trigger the system shutdown. My menu app is written in c++ and is using SDL. Any ideas on how I can trap the power off button press event?</p>
  
  <p>Thanks to those that responded, here is the actual code I used to get it to work:</p>
  
  <p><strong>Class members:</strong></p>
  
  <pre><code>int m_acpidsock;
  sockaddr_un m_acpidsockaddr;
  </code></pre>
  
  <p><strong>Setup code:</strong></p>
  
  <pre><code>/* Connect to acpid socket */
  m_acpidsock = socket(AF_UNIX, SOCK_STREAM, 0);
  if(m_acpidsock&gt;=0)
  {
      m_acpidsockaddr.sun_family = AF_UNIX;
      strcpy(m_acpidsockaddr.sun_path,"/var/run/acpid.socket");
      if(connect(m_acpidsock, (struct sockaddr *)&amp;m_acpidsockaddr, 108)&lt;0)
      {
          /* can't connect */
          close(m_acpidsock);
          m_acpidsock=-1;
      }
  }
  </code></pre>
  
  <p><strong>Update code:</strong></p>
  
  <pre><code>/* check for any power events */
  if(m_acpidsock)
  {
      char buf[1024];
      int s=recv(m_acpidsock, buf, sizeof(buf), MSG_DONTWAIT);
  
      if(s&gt;0)
      {
          buf[s]=0;
          printf("ACPID:%s\n\n",buf);
          if(!strncmp(buf,"button/power",12))
          {
              setShutdown();
              system("shutdown -P now");
          }
      }
  }
  </code></pre>
  
  <p><strong>Close socket code:</strong></p>
  
  <pre><code>if(m_acpidsock&gt;=0)
  {
      close(m_acpidsock);
      m_acpidsock=-1;
  }
  </code></pre>
  
  <p>Lastly, I needed to allow non-root users to shutdown and that worked using this line:</p>
  
  <pre><code>sudo chmod u+s /sbin/shutdown
  </code></pre>