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;
129 unsigned char bc_enable = 1; // enable charger control
130 unsigned char bc_stopcap = 99; // battery capacity threshold as stop condition 1
131 unsigned int bc_stopcur = 80000; // charge current threshold as stop condition 2, in uA
132 unsigned char bc_startcap = 95; // battery capacity threshold to resume charging
133 char *bc_charge_devices = NULL; // charger /sys/class/power_supply/ devices, changes between kernel versions
137 void dispatch_key ( int keycode, int val );
138 void dispatch_event ( int code, int val );
139 void sigchld_handler ( int n );
140 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs );
141 void sigalrm_handler ( int n );
143 static void usage ( char *argv[] ) {
144 printf ( "%s [-d]\n", argv [ 0 ] );
145 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
146 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" );
150 int main ( int argc, char *argv[] ) {
152 int logall = -1; // -1 means normal logging rules; >=0 means log all!
154 for ( i = 1; i < argc; i++ ) {
156 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
157 //printf ( "Going daemon mode. Silent running.\n" );
159 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
161 if ( isdigit ( argv [ i ][ 2 ] ) ) {
162 unsigned char x = atoi ( argv [ i ] + 2 );
180 pnd_log_set_pretext ( "pndevmapperd" );
181 pnd_log_set_flush ( 1 );
183 if ( logall == -1 ) {
184 // standard logging; non-daemon versus daemon
186 if ( g_daemon_mode ) {
189 pnd_log_set_filter ( pndn_rem );
196 f = fopen ( "/tmp/pndevmapperd.log", "w" );
199 pnd_log_set_filter ( logall );
200 pnd_log_to_stream ( f );
201 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
204 if ( logall == pndn_debug ) {
205 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
206 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
211 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
213 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
215 // basic daemon set up
216 if ( g_daemon_mode ) {
218 // set a CWD somewhere else
221 // detach from terminal
222 if ( ( i = fork() ) < 0 ) {
223 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
227 exit ( 0 ); // exit parent
232 umask ( 022 ); // emitted files can be rwxr-xr-x
236 /* hmm, seems to not like working right after boot.. do we depend on another daemon or
237 * on giving kernel time to init something, or ... wtf?
238 * -- lets give the system some time to wake up
242 // this one works for pndnotifyd, which actually needs INOTIFYH..
245 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
246 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
248 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
249 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
253 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
255 // pndnotifyd also waits for user to log in .. pretty excessive, especially since
256 // what if user wants to close the lid while at the log in screen? for now play the
257 // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
260 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
261 // log-out and back in again, with SDs popping in and out between..
262 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
263 char tmp_username [ 128 ];
265 if ( pnd_check_login ( tmp_username, 127 ) ) {
268 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
271 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
275 /* inhale config or die trying
279 // attempt to fetch a sensible default searchpath for configs
280 configpath = pnd_conf_query_searchpath();
282 // attempt to fetch the apps config. since it finds us the runscript
283 pnd_conf_handle evmaph;
285 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
288 // couldn't locate conf, just bail
289 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
293 /* iterate across conf, stocking the event map
295 void *n = pnd_box_get_head ( evmaph );
298 char *k = pnd_box_get_key ( n );
299 //printf ( "key %s\n", k );
301 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
304 // keys should really push push generic-events onto the table, since they;'re just a special case of them
305 // to make things easier to read
307 // figure out which keycode we're talking about
308 keycode_t *p = keycodes;
309 while ( p -> keycode != -1 ) {
310 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
316 if ( p -> keycode != -1 ) {
317 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
318 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
320 // note the script to activate in response
321 if ( strchr ( n, ' ' ) ) {
322 char *foo = strdup ( n );
323 char *t = strchr ( foo, ' ' );
325 g_evmap [ g_evmap_max ].script = foo;
326 g_evmap [ g_evmap_max ].maxhold = atoi ( t + 1 );
328 g_evmap [ g_evmap_max ].script = n;
329 g_evmap [ g_evmap_max ].maxhold = 0;
332 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s with maxhold %d\n",
333 p -> keyname, p -> keycode, (char*) n, g_evmap [ g_evmap_max ].maxhold );
337 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
340 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
343 // yes, key events could really be defined in this generic sense, and really we could just let people
344 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
345 // add a 'generic' section to conf file and just let folks redefine random events that way
346 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
347 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
348 // should just exist as scripts rather than daemons, but whose counting?
350 // figure out which keycode we're talking about
351 generic_event_t *p = generics;
352 while ( p -> code != -1 ) {
353 if ( strcasecmp ( p -> name, k ) == 0 ) {
359 if ( p -> code != -1 ) {
360 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
361 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
362 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
363 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
366 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
369 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
370 // not consumed here, skip silently
372 } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
373 // not consumed here, skip silently
375 } else if ( strncmp ( k, "battery_charge.", 15 ) == 0 ) {
376 // not consumed here, skip silently
380 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
383 n = pnd_box_get_next ( n );
386 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
387 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
388 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
391 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
392 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
393 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
397 if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
398 b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
399 pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
401 if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
402 b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
403 pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
405 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
406 b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
407 pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
409 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
410 b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
411 pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
414 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
415 b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
416 pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
418 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
419 b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
420 pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
422 if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
423 b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
424 pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
426 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.enable" ) != PND_CONF_BADNUM ) {
427 bc_enable = pnd_conf_get_as_int ( evmaph, "battery_charge.enable" );
428 pnd_log ( pndn_rem, "Battery charge enable set to %u", bc_enable );
430 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" ) != PND_CONF_BADNUM ) {
431 bc_stopcap = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" );
432 pnd_log ( pndn_rem, "Battery charge stop capacity set to %u", bc_stopcap );
434 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" ) != PND_CONF_BADNUM ) {
435 bc_stopcur = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" );
436 pnd_log ( pndn_rem, "Battery charge stop current set to %u", bc_stopcur );
438 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" ) != PND_CONF_BADNUM ) {
439 bc_startcap = pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" );
440 pnd_log ( pndn_rem, "Battery charge start capacity set to %u", bc_startcap );
442 if ( pnd_conf_get_as_char ( evmaph, "battery_charge.devices" ) != NULL ) {
443 bc_charge_devices = strdup ( pnd_conf_get_as_char ( evmaph, "battery_charge.devices" ) );
444 pnd_log ( pndn_rem, "Battery charge devices set to %s", bc_charge_devices );
447 /* do we have anything to do?
449 if ( ! g_evmap_max ) {
450 // uuuh, nothing to do?
451 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
455 /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
460 struct sigaction siggy;
461 siggy.sa_handler = sigchld_handler;
462 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
463 siggy.sa_flags = SA_RESTART; /* don't need anything */
464 sigaction ( SIGCHLD, &siggy, NULL );
466 /* set up the battery level warning timers
468 siggy.sa_handler = sigalrm_handler;
469 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
470 siggy.sa_flags = SA_RESTART; /* don't need anything */
471 sigaction ( SIGALRM, &siggy, NULL );
473 if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
474 pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
476 pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
479 /* actually try to do something useful
482 // stolen in part from notaz :)
484 // try to locate the appropriate devices
486 int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
489 for ( id = 0; ; id++ ) {
491 char name[256] = { 0, };
494 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
495 fd = open ( fname, O_RDONLY );
501 if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
505 pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );
507 if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
509 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
511 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
513 } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
515 } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
517 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
519 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
522 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
529 if (imaxfd < fd) imaxfd = fd;
533 if ( fds [ 0 ] == -1 ) {
534 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
537 if ( fds [ 1 ] == -1 ) {
538 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
541 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
542 pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
546 /* loop forever, watching for events
550 struct input_event ev[64];
552 unsigned int max_fd = 3; /* imaxfd */
553 int fd = -1, rd, ret;
560 for (i = 0; i < max_fd /*imaxfd*/; i++) {
561 if ( fds [ i ] != -1 ) {
562 FD_SET( fds [ i ], &fdset );
564 if ( fds [ i ] > imaxfd ) {
571 // figure out if we can block forever, or not
572 unsigned char do_block = 1;
577 for ( i = i; i < g_evmap_max; i++ ) {
578 if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
584 // wait for fd's or timeout
585 ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );
588 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
591 } else if ( ret == 0 ) { // select returned with timeout (no fd)
593 // timeout occurred; should only happen when 1 or more keys are being held down and
594 // they're "maxhold" keys, so we have to see if their timer has passed
595 unsigned int now = time ( NULL );
597 for ( i = i; i < g_evmap_max; i++ ) {
599 if ( g_evmap [ i ].keydown_time &&
600 g_evmap [ i ].maxhold &&
601 now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
603 keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
604 dispatch_key ( k -> keycode, 0 /* key up */ );
609 } else { // an fd was fiddled with
611 for ( i = 0; i < max_fd; i++ ) {
612 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
617 /* buttons or keypad */
618 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
619 if ( rd < (int) sizeof(struct input_event) ) {
620 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
624 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
626 if ( ev[i].type == EV_SYN ) {
628 } else if ( ev[i].type == EV_KEY ) {
630 // do we even know about this key at all?
631 keycode_t *p = keycodes;
632 while ( p -> keycode != -1 ) {
633 if ( p -> keycode == ev [ i ].code ) {
639 // if we do, hand it off to dispatcher to look up if we actually do something with it
640 if ( p -> keycode != -1 ) {
642 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
644 dispatch_key ( p -> keycode, ev [ i ].value );
647 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
651 } else if ( ev[i].type == EV_SW ) {
653 // do we even know about this event at all?
654 generic_event_t *p = generics;
655 while ( p -> code != -1 ) {
656 if ( p -> code == ev [ i ].code ) {
662 // if we do, hand it off to dispatcher to look up if we actually do something with it
663 if ( p -> code != -1 ) {
665 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
667 dispatch_event ( p -> code, ev [ i ].value );
670 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
675 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
681 } // an fd was touched
685 for (i = 0; i < 2; i++) {
686 if ( i != 2 && fds [ i ] != -1 ) {
694 // this should really register the keystate and time, and then another func to monitor
695 // time-passage and check the registered list and act on the event..
696 void dispatch_key ( int keycode, int val ) {
700 // 1 - down (pressed)
701 // 2 - down again (hold)
704 for ( i = 0; i < g_evmap_max; i++ ) {
706 if ( ( g_evmap [ i ].key_p ) &&
707 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
708 ( g_evmap [ i ].script ) )
710 unsigned char invoke_it = 0;
712 // is this a keydown or a keyup?
715 g_evmap [ i ].keydown_time = time ( NULL );
717 } else if ( val == 2 && g_evmap [ i ].keydown_time ) {
718 // key is being held; we should check if max-hold is set
720 if ( g_evmap [ i ].maxhold &&
721 time ( NULL ) - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
726 } else if ( val == 0 && g_evmap [ i ].keydown_time ) {
727 // keyup (while key is down)
729 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
732 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
739 char holdtime [ 128 ];
740 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
741 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
744 g_evmap [ i ].keydown_time = 0; // clear the keydown-ness
745 g_evmap [ i ].last_trigger_time = time ( NULL );
750 if ( ( x = fork() ) < 0 ) {
751 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
756 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
757 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
761 } // invoke the script!
764 } // found matching event for keycode
771 void dispatch_event ( int code, int val ) {
774 // LID val decodes as:
778 pnd_log ( pndn_rem, "Dispatching Event..\n" );
780 for ( i = 0; i < g_evmap_max; i++ ) {
782 if ( ( g_evmap [ i ].key_p == 0 ) &&
783 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
784 ( g_evmap [ i ].script ) )
787 // just hand the code to the script (ie: 0 or 1 to script)
788 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
792 sprintf ( value, "%d", val );
794 g_evmap [ i ].last_trigger_time = time ( NULL );
796 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
798 if ( ( x = fork() ) < 0 ) {
799 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
804 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
805 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
810 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
814 } // found matching event for keycode
821 void sigchld_handler ( int n ) {
823 pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );
828 pnd_log ( pndn_rem, " SIGCHLD done ]---\n" );
833 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs ) {
835 // assume that SIGALRM is already being caught, we just set the itimer here
837 struct itimerval itv;
839 // if no timer at all, set the 'current' one so it does something; otherwise
841 getitimer ( ITIMER_REAL, &itv );
843 if ( itv.it_value.tv_sec == 0 && itv.it_value.tv_sec == 0 ) {
844 itv.it_value.tv_sec = secs;
845 itv.it_value.tv_usec = usecs;
848 // set the next timer
849 //bzero ( &itv, sizeof(struct itimerval) );
851 itv.it_interval.tv_sec = secs;
852 itv.it_interval.tv_usec = usecs;
854 // if next-timer is less than current, set current too
855 if ( itv.it_value.tv_sec > itv.it_interval.tv_sec ) {
856 itv.it_value.tv_sec = secs;
857 itv.it_value.tv_usec = usecs;
860 if ( setitimer ( ITIMER_REAL, &itv, NULL /* old value returned here */ ) < 0 ) {
868 void sigalrm_handler ( int n ) {
870 pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );
872 static time_t last_charge_check, last_charge_worka;
873 int batlevel = pnd_device_get_battery_gauge_perc();
877 pnd_device_get_charge_current ( &uamps );
879 if ( batlevel < 0 ) {
881 // couldn't read the battery level, so just assume low and make blinks?
882 batlevel = 4; // low, but not cause a shutdown
884 // couldn't read the battery level, so just assume ok!
889 // first -- are we critical yet? if so, shut down!
890 if ( batlevel <= b_shutdown && b_shutdown_script) {
893 // critical battery, but charging, so relax.
897 // Avoid warning again till re-powered
900 pnd_log ( pndn_error, "Battery Current: %d\n", uamps );
901 pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
904 if ( ( x = fork() ) < 0 ) {
905 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
911 sprintf ( value, "%d", b_shutdelay );
912 execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
913 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
923 if ( bc_enable && bc_charge_devices != NULL && (unsigned int)(now - last_charge_check) > 60 ) {
925 int charge_enabled = pnd_device_get_charger_enable ( bc_charge_devices );
926 if ( charge_enabled < 0 )
927 pnd_log ( pndn_error, "ERROR: Couldn't read charger enable control\n" );
930 if ( charge_enabled && batlevel >= bc_stopcap && 0 < uamps && uamps < bc_stopcur ) {
931 pnd_log ( pndn_debug, "Charge stop conditions reached, disabling charging\n" );
932 pnd_device_set_charger_enable ( bc_charge_devices, 0 );
934 else if ( !charge_enabled && batlevel <= bc_startcap ) {
935 pnd_log ( pndn_debug, "Charge start conditions reached, enabling charging\n" );
936 pnd_device_set_charger_enable ( bc_charge_devices, 1 );
939 // for some unknown reason it just stops charging randomly (happens once per week or so),
940 // and does not restart, resulting in a flat battery if machine is unattended.
941 // What seems to help here is writing to chip registers, we can do it here indirectly
942 // by writing to enable. Doing it occasionally should do no harm even with missing charger.
943 if ( batlevel <= bc_startcap && (unsigned int)(now - last_charge_worka) > 20*60 ) {
944 pnd_log ( pndn_debug, "Charge workaround trigger\n" );
945 pnd_device_set_charger_enable ( bc_charge_devices, 1 );
946 last_charge_worka = now;
949 last_charge_check = now;
952 // is battery warning already active?
956 // is user charging up? if so, stop blinking.
957 // perhaps we shoudl check if charger is connected, and not blink at all in that case..
961 pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
963 set_next_alarm ( b_frequency, 0 );
964 pnd_device_set_led_charger_brightness ( 250 );
968 if ( b_active == 1 ) {
970 pnd_log ( pndn_debug, "Blink on\n" );
971 pnd_device_set_led_charger_brightness ( 200 );
972 // set timer to short duration
974 set_next_alarm ( 0, b_blinkdur );
975 } else if ( b_active == 2 ) {
977 pnd_log ( pndn_debug, "Blink off\n" );
978 pnd_device_set_led_charger_brightness ( 10 );
979 // back to longer duration
981 set_next_alarm ( b_blinkfreq, 0 );
988 if ( batlevel <= b_threshold && uamps < 0 ) {
989 // battery seems low, go to active mode
990 pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
992 set_next_alarm ( b_blinkfreq, 0 );