Merge branch 'master' of git://openpandora.org/pandora-libraries
[pandora-libraries.git] / apps / pndevmapperd.c
index 9c209f9..2214aae 100644 (file)
@@ -114,6 +114,7 @@ typedef struct {
 #define MAXEVENTS 255
 evmap_t g_evmap [ MAXEVENTS ];
 unsigned int g_evmap_max = 0;
+unsigned int g_queued_keyups = 0;
 
 // battery
 unsigned char b_threshold = 5;    // %battery
@@ -122,6 +123,8 @@ unsigned int b_blinkfreq = 2;     // blink every 2sec
 unsigned int b_blinkdur = 1000;   // blink duration (uSec), 0sec + uSec is assumed
 unsigned char b_active = 0;       // 0=inactive, 1=active and waiting to blink, 2=blink is on, waiting to turn off
 unsigned char b_shutdown = 1;     // %age battery to force a shutdown!
+unsigned int  b_shutdelay = 30;   // delay for shutdown script
+unsigned char b_warned = 0;       // Shutdown attempted
 char *b_shutdown_script = NULL;
 
 /* get to it
@@ -404,6 +407,10 @@ int main ( int argc, char *argv[] ) {
     b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
     pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
   }
+  if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
+    b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
+    pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
+  }
   if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
     b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
     pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
@@ -469,19 +476,19 @@ int main ( int argc, char *argv[] ) {
 
     pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );
 
-    if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
+    if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
       fds [ 0 ] = fd;
     } else if ( strcmp ( name, "gpio-keys" ) == 0) {
       fds [ 1 ] = fd;
     } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
       fds [ 0 ] = fd;
-    } else if ( strcmp ( name, "triton2-pwrbutton" ) == 0) {
+    } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
       fds [ 2 ] = fd;
-    } else if ( strcmp ( name, "ADS784x Touchscreen" ) == 0) {
+    } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
       fds [ 3 ] = fd;
-    } else if ( strcmp ( name, "vsense66" ) == 0) {
+    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
       fds [ 4 ] = fd;
-    } else if ( strcmp ( name, "vsense67" ) == 0) {
+    } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
       fds [ 5 ] = fd;
     } else {
       pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
@@ -518,6 +525,7 @@ int main ( int argc, char *argv[] ) {
     int fd = -1, rd, ret;
     fd_set fdset;
 
+    // set up fd list
     FD_ZERO ( &fdset );
 
     imaxfd = 0;
@@ -532,12 +540,44 @@ int main ( int argc, char *argv[] ) {
       }
     }
 
-    ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL /* no timeout */ );
+    // figure out if we can block forever, or not
+    unsigned char do_block = 1;
+    struct timeval tv;
+    tv.tv_usec = 0;
+    tv.tv_sec = 1;
+
+    for ( i = i; i < g_evmap_max; i++ ) {
+      if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
+       do_block = 0;
+       break;
+      }
+    }
+
+    // wait for fd's or timeout
+    ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );
 
     if ( ret == -1 ) {
       pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
       continue; // retry!
 
+    } else if ( ret == 0 ) { // select returned with timeout (no fd)
+
+      // timeout occurred; should only happen when 1 or more keys are being held down and
+      // they're "maxhold" keys, so we have to see if their timer has passed
+      unsigned int now = time ( NULL );
+
+      for ( i = i; i < g_evmap_max; i++ ) {
+
+       if ( g_evmap [ i ].keydown_time &&
+            g_evmap [ i ].maxhold &&
+            now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
+       {
+         keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
+         dispatch_key ( k -> keycode, 0 /* key up */ );
+       }
+
+      } // for
+
     } else { // an fd was fiddled with
 
       for ( i = 0; i < max_fd; i++ ) {
@@ -814,28 +854,34 @@ void sigalrm_handler ( int n ) {
   }
 
   // first -- are we critical yet? if so, shut down!
-  if ( batlevel <= b_shutdown && b_shutdown_script ) {
+  if ( batlevel <= b_shutdown && b_shutdown_script) {
     int mamps = 0;
 
     if ( pnd_device_get_charge_current ( &mamps ) && mamps > 100 ) {
-      // critical battery, but charging, so relax.
+        // critical battery, but charging, so relax.
+        b_warned = 0;
     } else {
-      int x;
-
-      pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
-               b_shutdown_script );
-
-      if ( ( x = fork() ) < 0 ) {
-       pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
-       exit ( -3 );
-      }
-
-      if ( x == 0 ) {
-       execl ( b_shutdown_script, b_shutdown_script, (char*)NULL );
-       pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
-       exit ( -4 );
+      if (b_warned == 0) {
+          // Avoid warning again till re-powered
+          b_warned = 1;
+          int x;
+          pnd_log ( pndn_error, "Battery Current: %d\n", mamps );
+          pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
+               b_shutdown_script );
+
+          if ( ( x = fork() ) < 0 ) {
+               pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
+           exit ( -3 );
+          }
+
+         if ( x == 0 ) {
+           char value [ 100 ];
+           sprintf ( value, "%d", b_shutdelay );
+          execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
+          pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
+          exit ( -4 );
+         }
       }
-
     } // charging
 
   }
@@ -847,6 +893,8 @@ void sigalrm_handler ( int n ) {
     // is user charging up? if so, stop blinking.
     // perhaps we shoudl check if charger is connected, and not blink at all in that case..
     if ( batlevel > b_threshold + 1 /* allow for error in read */ ) {
+      //Re-arm warning
+      b_warned = 0;
       pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
       b_active = 0;
       set_next_alarm ( b_frequency, 0 );