2 /* pndevmapperd exists to watch for a few interesting events and to launch scripts when they occur
3 * ie: when the lid closes, should invoke power-mode toggle sh-script
6 // woot for writing code while sick.
8 // this code begs a rewrite, but should work fine; its just arranged goofily
9 // -> I mean, why the racial divide between keys and other events?
10 // -> now that I've put together pnd_io_evdev, should leverage that; be much cleaner.
12 #include <stdio.h> /* for printf, NULL */
13 #include <stdlib.h> /* for free */
14 #include <string.h> /* for strdup */
15 #include <unistd.h> // for exit()
16 #include <sys/types.h> // for umask
17 #include <sys/stat.h> // for umask
18 #include <fcntl.h> // for open(2)
19 #include <errno.h> // for errno
20 #include <time.h> // for time(2)
21 #include <ctype.h> // for isdigit
22 #include <signal.h> // for sigaction
23 #include <sys/wait.h> // for wait
24 #include <sys/time.h> // setitimer
26 #include <linux/input.h> // for keys
27 //#include "../../kernel-rip/input.h" // for keys
30 #include "pnd_container.h"
32 #include "pnd_discovery.h"
33 #include "pnd_locate.h"
34 #include "pnd_pndfiles.h"
36 #include "pnd_logger.h"
37 #include "pnd_utility.h"
38 #include "pnd_notify.h"
39 #include "pnd_device.h"
43 unsigned char g_daemon_mode = 0;
44 unsigned int g_minimum_separation = 1;
48 pndn_rem, // will set default log level to here, so 'debug' is omitted
52 } pndnotify_loglevels_e;
54 // key/event definition
61 keycode_t keycodes[] = {
64 { KEY_MENU, "pandora" },
65 { KEY_POWER, "power" },
66 { KEY_DELETE, "del" },
67 { KEY_COMMA, "comma" },
78 { KEY_BRIGHTNESSDOWN, "lcdbrightdown" },
79 { KEY_BRIGHTNESSUP, "lcdbrightup" },
89 generic_event_t generics[] = {
90 { EV_SW, 0, "lid-toggle" }, // expecting value 1 (lid close) or 0 (lid open)
94 // event-to-sh mapping
98 unsigned char key_p; // 1 if its a key, otherwise an event
100 /* template information
102 void *reqs; // scancode/etc for the event in question
104 char *script; // script to invoke
105 unsigned int maxhold; // maximum hold-time before forcing script invocation
109 time_t last_trigger_time;
114 #define MAXEVENTS 255
115 evmap_t g_evmap [ MAXEVENTS ];
116 unsigned int g_evmap_max = 0;
117 unsigned int g_queued_keyups = 0;
120 unsigned char b_threshold = 5; // %battery
121 unsigned int b_frequency = 300; // frequency to check
122 unsigned int b_blinkfreq = 2; // blink every 2sec
123 unsigned int b_blinkdur = 1000; // blink duration (uSec), 0sec + uSec is assumed
124 unsigned char b_active = 0; // 0=inactive, 1=active and waiting to blink, 2=blink is on, waiting to turn off
125 unsigned char b_shutdown = 1; // %age battery to force a shutdown!
126 unsigned int b_shutdelay = 30; // delay for shutdown script
127 unsigned char b_warned = 0; // Shutdown attempted
128 char *b_shutdown_script = NULL;
132 void dispatch_key ( int keycode, int val );
133 void dispatch_event ( int code, int val );
134 void sigchld_handler ( int n );
135 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs );
136 void sigalrm_handler ( int n );
138 static void usage ( char *argv[] ) {
139 printf ( "%s [-d]\n", argv [ 0 ] );
140 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
141 printf ( "-l#\tLog-it; -l is 0-and-up (or all), and -l2 means 2-and-up (not all); l[0-3] for now. Log goes to /tmp/pndevmapperd.log\n" );
145 int main ( int argc, char *argv[] ) {
147 int logall = -1; // -1 means normal logging rules; >=0 means log all!
149 for ( i = 1; i < argc; i++ ) {
151 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
152 //printf ( "Going daemon mode. Silent running.\n" );
154 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
156 if ( isdigit ( argv [ i ][ 2 ] ) ) {
157 unsigned char x = atoi ( argv [ i ] + 2 );
175 pnd_log_set_pretext ( "pndevmapperd" );
176 pnd_log_set_flush ( 1 );
178 if ( logall == -1 ) {
179 // standard logging; non-daemon versus daemon
181 if ( g_daemon_mode ) {
184 pnd_log_set_filter ( pndn_rem );
191 f = fopen ( "/tmp/pndevmapperd.log", "w" );
194 pnd_log_set_filter ( logall );
195 pnd_log_to_stream ( f );
196 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
199 if ( logall == pndn_debug ) {
200 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
201 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
206 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
208 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
210 // basic daemon set up
211 if ( g_daemon_mode ) {
213 // set a CWD somewhere else
216 // detach from terminal
217 if ( ( i = fork() ) < 0 ) {
218 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
222 exit ( 0 ); // exit parent
227 umask ( 022 ); // emitted files can be rwxr-xr-x
231 /* hmm, seems to not like working right after boot.. do we depend on another daemon or
232 * on giving kernel time to init something, or ... wtf?
233 * -- lets give the system some time to wake up
237 // this one works for pndnotifyd, which actually needs INOTIFYH..
240 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
241 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
243 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
244 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
248 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
250 // pndnotifyd also waits for user to log in .. pretty excessive, especially since
251 // what if user wants to close the lid while at the log in screen? for now play the
252 // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
255 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
256 // log-out and back in again, with SDs popping in and out between..
257 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
258 char tmp_username [ 128 ];
260 if ( pnd_check_login ( tmp_username, 127 ) ) {
263 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
266 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
270 /* inhale config or die trying
274 // attempt to fetch a sensible default searchpath for configs
275 configpath = pnd_conf_query_searchpath();
277 // attempt to fetch the apps config. since it finds us the runscript
278 pnd_conf_handle evmaph;
280 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
283 // couldn't locate conf, just bail
284 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
288 /* iterate across conf, stocking the event map
290 void *n = pnd_box_get_head ( evmaph );
293 char *k = pnd_box_get_key ( n );
294 //printf ( "key %s\n", k );
296 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
299 // keys should really push push generic-events onto the table, since they;'re just a special case of them
300 // to make things easier to read
302 // figure out which keycode we're talking about
303 keycode_t *p = keycodes;
304 while ( p -> keycode != -1 ) {
305 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
311 if ( p -> keycode != -1 ) {
312 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
313 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
315 // note the script to activate in response
316 if ( strchr ( n, ' ' ) ) {
317 char *foo = strdup ( n );
318 char *t = strchr ( foo, ' ' );
320 g_evmap [ g_evmap_max ].script = foo;
321 g_evmap [ g_evmap_max ].maxhold = atoi ( t + 1 );
323 g_evmap [ g_evmap_max ].script = n;
324 g_evmap [ g_evmap_max ].maxhold = 0;
327 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s with maxhold %d\n",
328 p -> keyname, p -> keycode, (char*) n, g_evmap [ g_evmap_max ].maxhold );
332 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
335 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
338 // yes, key events could really be defined in this generic sense, and really we could just let people
339 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
340 // add a 'generic' section to conf file and just let folks redefine random events that way
341 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
342 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
343 // should just exist as scripts rather than daemons, but whose counting?
345 // figure out which keycode we're talking about
346 generic_event_t *p = generics;
347 while ( p -> code != -1 ) {
348 if ( strcasecmp ( p -> name, k ) == 0 ) {
354 if ( p -> code != -1 ) {
355 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
356 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
357 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
358 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
361 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
364 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
365 // not consumed here, skip silently
367 } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
368 // not consumed here, skip silently
372 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
375 n = pnd_box_get_next ( n );
378 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
379 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
380 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
383 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
384 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
385 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
389 if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
390 b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
391 pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
393 if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
394 b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
395 pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
397 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
398 b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
399 pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
401 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
402 b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
403 pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
406 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
407 b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
408 pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
410 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
411 b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
412 pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
414 if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
415 b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
416 pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
419 /* do we have anything to do?
421 if ( ! g_evmap_max ) {
422 // uuuh, nothing to do?
423 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
427 /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
432 struct sigaction siggy;
433 siggy.sa_handler = sigchld_handler;
434 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
435 siggy.sa_flags = SA_RESTART; /* don't need anything */
436 sigaction ( SIGCHLD, &siggy, NULL );
438 /* set up the battery level warning timers
440 siggy.sa_handler = sigalrm_handler;
441 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
442 siggy.sa_flags = SA_RESTART; /* don't need anything */
443 sigaction ( SIGALRM, &siggy, NULL );
445 if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
446 pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
448 pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
451 /* actually try to do something useful
454 // stolen in part from notaz :)
456 // try to locate the appropriate devices
458 int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
461 for ( id = 0; ; id++ ) {
463 char name[256] = { 0, };
466 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
467 fd = open ( fname, O_RDONLY );
473 if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
477 pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );
479 if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
481 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
483 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
485 } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
487 } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
489 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
491 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
494 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
501 if (imaxfd < fd) imaxfd = fd;
505 if ( fds [ 0 ] == -1 ) {
506 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
509 if ( fds [ 1 ] == -1 ) {
510 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
513 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
514 pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
518 /* loop forever, watching for events
522 struct input_event ev[64];
524 unsigned int max_fd = 3; /* imaxfd */
525 int fd = -1, rd, ret;
532 for (i = 0; i < max_fd /*imaxfd*/; i++) {
533 if ( fds [ i ] != -1 ) {
534 FD_SET( fds [ i ], &fdset );
536 if ( fds [ i ] > imaxfd ) {
543 // figure out if we can block forever, or not
544 unsigned char do_block = 1;
549 for ( i = i; i < g_evmap_max; i++ ) {
550 if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
556 // wait for fd's or timeout
557 ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );
560 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
563 } else if ( ret == 0 ) { // select returned with timeout (no fd)
565 // timeout occurred; should only happen when 1 or more keys are being held down and
566 // they're "maxhold" keys, so we have to see if their timer has passed
567 unsigned int now = time ( NULL );
569 for ( i = i; i < g_evmap_max; i++ ) {
571 if ( g_evmap [ i ].keydown_time &&
572 g_evmap [ i ].maxhold &&
573 now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
575 keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
576 dispatch_key ( k -> keycode, 0 /* key up */ );
581 } else { // an fd was fiddled with
583 for ( i = 0; i < max_fd; i++ ) {
584 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
589 /* buttons or keypad */
590 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
591 if ( rd < (int) sizeof(struct input_event) ) {
592 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
596 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
598 if ( ev[i].type == EV_SYN ) {
600 } else if ( ev[i].type == EV_KEY ) {
602 // do we even know about this key at all?
603 keycode_t *p = keycodes;
604 while ( p -> keycode != -1 ) {
605 if ( p -> keycode == ev [ i ].code ) {
611 // if we do, hand it off to dispatcher to look up if we actually do something with it
612 if ( p -> keycode != -1 ) {
614 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
616 dispatch_key ( p -> keycode, ev [ i ].value );
619 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
623 } else if ( ev[i].type == EV_SW ) {
625 // do we even know about this event at all?
626 generic_event_t *p = generics;
627 while ( p -> code != -1 ) {
628 if ( p -> code == ev [ i ].code ) {
634 // if we do, hand it off to dispatcher to look up if we actually do something with it
635 if ( p -> code != -1 ) {
637 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
639 dispatch_event ( p -> code, ev [ i ].value );
642 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
647 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
653 } // an fd was touched
657 for (i = 0; i < 2; i++) {
658 if ( i != 2 && fds [ i ] != -1 ) {
666 // this should really register the keystate and time, and then another func to monitor
667 // time-passage and check the registered list and act on the event..
668 void dispatch_key ( int keycode, int val ) {
672 // 1 - down (pressed)
673 // 2 - down again (hold)
676 for ( i = 0; i < g_evmap_max; i++ ) {
678 if ( ( g_evmap [ i ].key_p ) &&
679 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
680 ( g_evmap [ i ].script ) )
682 unsigned char invoke_it = 0;
684 // is this a keydown or a keyup?
687 g_evmap [ i ].keydown_time = time ( NULL );
689 } else if ( val == 2 && g_evmap [ i ].keydown_time ) {
690 // key is being held; we should check if max-hold is set
692 if ( g_evmap [ i ].maxhold &&
693 time ( NULL ) - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
698 } else if ( val == 0 && g_evmap [ i ].keydown_time ) {
699 // keyup (while key is down)
701 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
704 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
711 char holdtime [ 128 ];
712 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
713 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
716 g_evmap [ i ].keydown_time = 0; // clear the keydown-ness
717 g_evmap [ i ].last_trigger_time = time ( NULL );
722 if ( ( x = fork() ) < 0 ) {
723 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
728 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
729 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
733 } // invoke the script!
736 } // found matching event for keycode
743 void dispatch_event ( int code, int val ) {
746 // LID val decodes as:
750 pnd_log ( pndn_rem, "Dispatching Event..\n" );
752 for ( i = 0; i < g_evmap_max; i++ ) {
754 if ( ( g_evmap [ i ].key_p == 0 ) &&
755 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
756 ( g_evmap [ i ].script ) )
759 // just hand the code to the script (ie: 0 or 1 to script)
760 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
764 sprintf ( value, "%d", val );
766 g_evmap [ i ].last_trigger_time = time ( NULL );
768 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
770 if ( ( x = fork() ) < 0 ) {
771 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
776 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
777 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
782 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
786 } // found matching event for keycode
793 void sigchld_handler ( int n ) {
795 pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );
800 pnd_log ( pndn_rem, " SIGCHLD done ]---\n" );
805 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs ) {
807 // assume that SIGALRM is already being caught, we just set the itimer here
809 struct itimerval itv;
811 // if no timer at all, set the 'current' one so it does something; otherwise
813 getitimer ( ITIMER_REAL, &itv );
815 if ( itv.it_value.tv_sec == 0 && itv.it_value.tv_sec == 0 ) {
816 itv.it_value.tv_sec = secs;
817 itv.it_value.tv_usec = usecs;
820 // set the next timer
821 //bzero ( &itv, sizeof(struct itimerval) );
823 itv.it_interval.tv_sec = secs;
824 itv.it_interval.tv_usec = usecs;
826 // if next-timer is less than current, set current too
827 if ( itv.it_value.tv_sec > itv.it_interval.tv_sec ) {
828 itv.it_value.tv_sec = secs;
829 itv.it_value.tv_usec = usecs;
832 if ( setitimer ( ITIMER_REAL, &itv, NULL /* old value returned here */ ) < 0 ) {
840 void sigalrm_handler ( int n ) {
842 pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );
844 int batlevel = pnd_device_get_battery_gauge_perc();
846 if ( batlevel < 0 ) {
848 // couldn't read the battery level, so just assume low and make blinks?
849 batlevel = 4; // low, but not cause a shutdown
851 // couldn't read the battery level, so just assume ok!
856 // first -- are we critical yet? if so, shut down!
857 if ( batlevel <= b_shutdown && b_shutdown_script) {
860 if ( pnd_device_get_charge_current ( &mamps ) && mamps > 100 ) {
861 // critical battery, but charging, so relax.
865 // Avoid warning again till re-powered
868 pnd_log ( pndn_error, "Battery Current: %d\n", mamps );
869 pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
872 if ( ( x = fork() ) < 0 ) {
873 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
879 sprintf ( value, "%d", b_shutdelay );
880 execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
881 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
889 // is battery warning already active?
893 // is user charging up? if so, stop blinking.
894 // perhaps we shoudl check if charger is connected, and not blink at all in that case..
895 if ( batlevel > b_threshold + 1 /* allow for error in read */ ) {
898 pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
900 set_next_alarm ( b_frequency, 0 );
901 pnd_device_set_led_charger_brightness ( 250 );
905 if ( b_active == 1 ) {
907 pnd_log ( pndn_debug, "Blink on\n" );
908 pnd_device_set_led_charger_brightness ( 200 );
909 // set timer to short duration
911 set_next_alarm ( 0, b_blinkdur );
912 } else if ( b_active == 2 ) {
914 pnd_log ( pndn_debug, "Blink off\n" );
915 pnd_device_set_led_charger_brightness ( 10 );
916 // back to longer duration
918 set_next_alarm ( b_blinkfreq, 0 );
925 if ( batlevel <= b_threshold ) {
926 // battery seems low, go to active mode
927 pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
929 set_next_alarm ( b_blinkfreq, 0 );