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" },
80 { KEY_COFFEE, "hold" }, /* coffee? lol */
102 generic_event_t generics[] = {
103 { EV_SW, 0, "lid-toggle" }, // expecting value 1 (lid close) or 0 (lid open)
107 // FAKESCRIPT_ entries are to better handle a virtual script name; if we have to parse
108 // "TOGGLE_HOLD" for every key down, it seems a little inefficient; at conf-time, if we
109 // see this string for example, why not change it to a magic number .. and if we see that
110 // down the road, we can act with just an integer compare, instead of a string compare..
111 #define FAKESCRIPT_TOGGLE_HOLD 0001
113 // event-to-sh mapping
117 unsigned char key_p; // 1 if its a key, otherwise an event
119 /* template information
121 void *reqs; // scancode/etc for the event in question
123 char *script; // script to invoke
124 unsigned int maxhold; // maximum hold-time before forcing script invocation
128 time_t last_trigger_time;
133 #define MAXEVENTS 255
134 evmap_t g_evmap [ MAXEVENTS ];
135 unsigned int g_evmap_max = 0;
136 unsigned int g_queued_keyups = 0;
139 unsigned char b_threshold = 5; // %battery
140 unsigned int b_frequency = 300; // frequency to check
141 unsigned int b_blinkfreq = 2; // blink every 2sec
142 unsigned int b_blinkdur = 1000; // blink duration (uSec), 0sec + uSec is assumed
143 unsigned char b_active = 0; // 0=inactive, 1=active and waiting to blink, 2=blink is on, waiting to turn off
144 unsigned char b_shutdown = 1; // %age battery to force a shutdown!
145 unsigned int b_shutdelay = 30; // delay for shutdown script
146 unsigned char b_warned = 0; // Shutdown attempted
147 char *b_shutdown_script = NULL;
148 unsigned char bc_enable = 1; // enable charger control
149 unsigned char bc_stopcap = 99; // battery capacity threshold as stop condition 1
150 unsigned int bc_stopcur = 80000; // charge current threshold as stop condition 2, in uA
151 unsigned char bc_startcap = 95; // battery capacity threshold to resume charging
152 char *bc_charge_devices = NULL; // charger /sys/class/power_supply/ devices, changes between kernel versions
154 // fd's; pulled from main() so I can be lazy
155 int fds [ 8 ] = { -1, -1, -1, -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
160 void dispatch_key ( int keycode, int val );
161 void dispatch_event ( int code, int val );
162 void sigchld_handler ( int n );
163 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs );
164 void sigalrm_handler ( int n );
165 void fakescript_hold_on ( void );
166 void fakescript_hold_off ( void );
168 static void usage ( char *argv[] ) {
169 printf ( "%s [-d]\n", argv [ 0 ] );
170 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
171 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" );
175 int main ( int argc, char *argv[] ) {
177 int logall = -1; // -1 means normal logging rules; >=0 means log all!
179 for ( i = 1; i < argc; i++ ) {
181 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
182 //printf ( "Going daemon mode. Silent running.\n" );
184 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
186 if ( isdigit ( argv [ i ][ 2 ] ) ) {
187 unsigned char x = atoi ( argv [ i ] + 2 );
205 pnd_log_set_pretext ( "pndevmapperd" );
206 pnd_log_set_flush ( 1 );
208 if ( logall == -1 ) {
209 // standard logging; non-daemon versus daemon
211 if ( g_daemon_mode ) {
214 pnd_log_set_filter ( pndn_rem );
221 f = fopen ( "/tmp/pndevmapperd.log", "w" );
224 pnd_log_set_filter ( logall );
225 pnd_log_to_stream ( f );
226 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
229 if ( logall == pndn_debug ) {
230 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
231 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
236 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
238 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
240 // basic daemon set up
241 if ( g_daemon_mode ) {
243 // set a CWD somewhere else
246 // detach from terminal
247 if ( ( i = fork() ) < 0 ) {
248 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
252 exit ( 0 ); // exit parent
257 umask ( 022 ); // emitted files can be rwxr-xr-x
261 /* hmm, seems to not like working right after boot.. do we depend on another daemon or
262 * on giving kernel time to init something, or ... wtf?
263 * -- lets give the system some time to wake up
267 // this one works for pndnotifyd, which actually needs INOTIFYH..
270 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
271 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
273 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
274 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
278 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
280 // pndnotifyd also waits for user to log in .. pretty excessive, especially since
281 // what if user wants to close the lid while at the log in screen? for now play the
282 // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
285 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
286 // log-out and back in again, with SDs popping in and out between..
287 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
288 char tmp_username [ 128 ];
290 if ( pnd_check_login ( tmp_username, 127 ) ) {
293 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
296 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
300 /* inhale config or die trying
304 // attempt to fetch a sensible default searchpath for configs
305 configpath = pnd_conf_query_searchpath();
307 // attempt to fetch the apps config. since it finds us the runscript
308 pnd_conf_handle evmaph;
310 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
313 // couldn't locate conf, just bail
314 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
318 /* iterate across conf, stocking the event map
320 void *n = pnd_box_get_head ( evmaph );
323 char *k = pnd_box_get_key ( n );
324 //printf ( "key %s\n", k );
326 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
329 // keys should really push push generic-events onto the table, since they;'re just a special case of them
330 // to make things easier to read
332 // figure out which keycode we're talking about
333 keycode_t *p = keycodes;
334 while ( p -> keycode != -1 ) {
335 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
341 if ( p -> keycode != -1 ) {
342 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
343 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
345 // note the script to activate in response
346 if ( strchr ( n, ' ' ) ) {
347 char *foo = strdup ( n );
348 char *t = strchr ( foo, ' ' );
350 g_evmap [ g_evmap_max ].script = foo;
351 g_evmap [ g_evmap_max ].maxhold = atoi ( t + 1 );
353 g_evmap [ g_evmap_max ].script = n;
354 g_evmap [ g_evmap_max ].maxhold = 0;
356 if ( strcmp ( n, "TOGGLE_HOLD" ) == 0 ) {
357 g_evmap [ g_evmap_max ].script = (char*)FAKESCRIPT_TOGGLE_HOLD;
362 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s with maxhold %d\n",
363 p -> keyname, p -> keycode, (char*) n, g_evmap [ g_evmap_max ].maxhold );
367 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
370 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
373 // yes, key events could really be defined in this generic sense, and really we could just let people
374 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
375 // add a 'generic' section to conf file and just let folks redefine random events that way
376 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
377 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
378 // should just exist as scripts rather than daemons, but whose counting?
380 // figure out which keycode we're talking about
381 generic_event_t *p = generics;
382 while ( p -> code != -1 ) {
383 if ( strcasecmp ( p -> name, k ) == 0 ) {
389 if ( p -> code != -1 ) {
390 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
391 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
392 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
393 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
396 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
399 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
400 // not consumed here, skip silently
402 } else if ( strncmp ( k, "battery.", 8 ) == 0 ) {
403 // not consumed here, skip silently
405 } else if ( strncmp ( k, "battery_charge.", 15 ) == 0 ) {
406 // not consumed here, skip silently
410 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
413 n = pnd_box_get_next ( n );
416 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
417 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
418 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
421 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
422 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
423 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
427 if ( pnd_conf_get_as_int ( evmaph, "battery.threshold" ) != PND_CONF_BADNUM ) {
428 b_threshold = pnd_conf_get_as_int ( evmaph, "battery.threshold" );
429 pnd_log ( pndn_rem, "Battery threshold set to %u", b_threshold );
431 if ( pnd_conf_get_as_int ( evmaph, "battery.check_interval" ) != PND_CONF_BADNUM ) {
432 b_frequency = pnd_conf_get_as_int ( evmaph, "battery.check_interval" );
433 pnd_log ( pndn_rem, "Battery check interval set to %u", b_frequency );
435 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_interval" ) != PND_CONF_BADNUM ) {
436 b_blinkfreq = pnd_conf_get_as_int ( evmaph, "battery.blink_interval" );
437 pnd_log ( pndn_rem, "Battery blink interval set to %u", b_blinkfreq );
439 if ( pnd_conf_get_as_int ( evmaph, "battery.blink_duration" ) != PND_CONF_BADNUM ) {
440 b_blinkdur = pnd_conf_get_as_int ( evmaph, "battery.blink_duration" );
441 pnd_log ( pndn_rem, "Battery blink duration set to %u", b_blinkdur );
444 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" ) != PND_CONF_BADNUM ) {
445 b_shutdown = pnd_conf_get_as_int ( evmaph, "battery.shutdown_threshold" );
446 pnd_log ( pndn_rem, "Battery shutdown threshold set to %u", b_shutdown );
448 if ( pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" ) != PND_CONF_BADNUM ) {
449 b_shutdelay = pnd_conf_get_as_int ( evmaph, "battery.shutdown_delay" );
450 pnd_log ( pndn_rem, "Battery shutdown delay set to %u", b_shutdelay );
452 if ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) != NULL ) {
453 b_shutdown_script = strdup ( pnd_conf_get_as_char ( evmaph, "battery.shutdown_script" ) );
454 pnd_log ( pndn_rem, "Battery shutdown script set to %s", b_shutdown_script );
456 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.enable" ) != PND_CONF_BADNUM ) {
457 bc_enable = pnd_conf_get_as_int ( evmaph, "battery_charge.enable" );
458 pnd_log ( pndn_rem, "Battery charge enable set to %u", bc_enable );
460 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" ) != PND_CONF_BADNUM ) {
461 bc_stopcap = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_capacity" );
462 pnd_log ( pndn_rem, "Battery charge stop capacity set to %u", bc_stopcap );
464 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" ) != PND_CONF_BADNUM ) {
465 bc_stopcur = pnd_conf_get_as_int ( evmaph, "battery_charge.stop_current" );
466 pnd_log ( pndn_rem, "Battery charge stop current set to %u", bc_stopcur );
468 if ( pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" ) != PND_CONF_BADNUM ) {
469 bc_startcap = pnd_conf_get_as_int ( evmaph, "battery_charge.start_capacity" );
470 pnd_log ( pndn_rem, "Battery charge start capacity set to %u", bc_startcap );
472 if ( pnd_conf_get_as_char ( evmaph, "battery_charge.devices" ) != NULL ) {
473 bc_charge_devices = strdup ( pnd_conf_get_as_char ( evmaph, "battery_charge.devices" ) );
474 pnd_log ( pndn_rem, "Battery charge devices set to %s", bc_charge_devices );
477 /* do we have anything to do?
479 if ( ! g_evmap_max ) {
480 // uuuh, nothing to do?
481 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
485 /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
490 struct sigaction siggy;
491 siggy.sa_handler = sigchld_handler;
492 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
493 siggy.sa_flags = SA_RESTART; /* don't need anything */
494 sigaction ( SIGCHLD, &siggy, NULL );
496 /* set up the battery level warning timers
498 siggy.sa_handler = sigalrm_handler;
499 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
500 siggy.sa_flags = SA_RESTART; /* don't need anything */
501 sigaction ( SIGALRM, &siggy, NULL );
503 if ( set_next_alarm ( b_frequency, 0 ) ) { // check every 'frequency' seconds
504 pnd_log ( pndn_rem, "Checking for low battery every %u seconds\n", b_frequency );
506 pnd_log ( pndn_error, "ERROR: Couldn't set up timer for every %u seconds\n", b_frequency );
509 /* actually try to do something useful
512 // stolen in part from notaz :)
514 // try to locate the appropriate devices
517 for ( id = 0; ; id++ ) {
519 char name[256] = { 0, };
522 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
523 fd = open ( fname, O_RDONLY );
529 if ( ioctl (fd, EVIOCGNAME(sizeof(name)), name ) < 0 ) {
533 pnd_log ( pndn_rem, "%s maps to %s\n", fname, name );
535 if ( strcmp ( name, PND_EVDEV_KEYPAD/*"omap_twl4030keypad"*/ ) == 0 ) {
537 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
539 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
541 } else if ( strcmp ( name, PND_EVDEV_POWER/*"triton2-pwrbutton"*/ ) == 0) {
543 } else if ( strcmp ( name, PND_EVDEV_TS/*"ADS784x Touchscreen"*/ ) == 0) {
545 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense66"*/ ) == 0) {
547 } else if ( strcmp ( name, PND_EVDEV_NUB1/*"vsense67"*/ ) == 0) {
550 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
557 if (imaxfd < fd) imaxfd = fd;
561 if ( fds [ 0 ] == -1 ) {
562 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
565 if ( fds [ 1 ] == -1 ) {
566 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
569 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
570 pnd_log ( pndn_error, "ERROR! Couldn't find either device!\n" );
574 /* loop forever, watching for events
578 struct input_event ev[64];
580 unsigned int max_fd = 3; /* imaxfd */
581 int fd = -1, rd, ret;
588 for (i = 0; i < max_fd /*imaxfd*/; i++) {
589 if ( fds [ i ] != -1 ) {
590 FD_SET( fds [ i ], &fdset );
592 if ( fds [ i ] > imaxfd ) {
599 // figure out if we can block forever, or not
600 unsigned char do_block = 1;
605 for ( i = i; i < g_evmap_max; i++ ) {
606 if ( g_evmap [ i ].keydown_time && g_evmap [ i ].maxhold ) {
612 // wait for fd's or timeout
613 ret = select ( imaxfd + 1, &fdset, NULL, NULL, do_block ? NULL /* no timeout */ : &tv );
616 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
619 } else if ( ret == 0 ) { // select returned with timeout (no fd)
621 // timeout occurred; should only happen when 1 or more keys are being held down and
622 // they're "maxhold" keys, so we have to see if their timer has passed
623 unsigned int now = time ( NULL );
625 for ( i = i; i < g_evmap_max; i++ ) {
627 if ( g_evmap [ i ].keydown_time &&
628 g_evmap [ i ].maxhold &&
629 now - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
631 keycode_t *k = (keycode_t*) g_evmap [ i ].reqs;
632 dispatch_key ( k -> keycode, 0 /* key up */ );
637 } else { // an fd was fiddled with
639 for ( i = 0; i < max_fd; i++ ) {
640 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
645 /* buttons or keypad */
646 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
647 if ( rd < (int) sizeof(struct input_event) ) {
648 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
652 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
654 if ( ev[i].type == EV_SYN ) {
656 } else if ( ev[i].type == EV_KEY ) {
658 // do we even know about this key at all?
659 keycode_t *p = keycodes;
660 while ( p -> keycode != -1 ) {
661 if ( p -> keycode == ev [ i ].code ) {
667 // if we do, hand it off to dispatcher to look up if we actually do something with it
668 if ( p -> keycode != -1 ) {
670 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
672 dispatch_key ( p -> keycode, ev [ i ].value );
675 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
679 } else if ( ev[i].type == EV_SW ) {
681 // do we even know about this event at all?
682 generic_event_t *p = generics;
683 while ( p -> code != -1 ) {
684 if ( p -> code == ev [ i ].code ) {
690 // if we do, hand it off to dispatcher to look up if we actually do something with it
691 if ( p -> code != -1 ) {
693 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
695 dispatch_event ( p -> code, ev [ i ].value );
698 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
703 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
709 } // an fd was touched
713 for (i = 0; i < 2; i++) {
714 if ( i != 2 && fds [ i ] != -1 ) {
722 // this should really register the keystate and time, and then another func to monitor
723 // time-passage and check the registered list and act on the event..
724 void dispatch_key ( int keycode, int val ) {
728 // 1 - down (pressed)
729 // 2 - down again (hold)
732 for ( i = 0; i < g_evmap_max; i++ ) {
734 if ( ( g_evmap [ i ].key_p ) &&
735 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
736 ( g_evmap [ i ].script ) )
738 unsigned char invoke_it = 0;
740 // is this a keydown or a keyup?
744 if ( g_evmap [ i ].maxhold == 0 ) {
746 // is this a special internally handled key, or normal key?
747 if ( g_evmap [ i ].script == (char*)FAKESCRIPT_TOGGLE_HOLD ) {
748 // handle this specially
749 fakescript_hold_on();
751 // normal key, with script to run
752 g_evmap [ i ].keydown_time = 0;
757 g_evmap [ i ].keydown_time = time ( NULL );
760 } else if ( val == 2 && g_evmap [ i ].keydown_time ) {
761 // key is being held; we should check if max-hold is set
763 if ( g_evmap [ i ].maxhold &&
764 time ( NULL ) - g_evmap [ i ].keydown_time >= g_evmap [ i ].maxhold )
769 } else if ( val == 0 ) {
771 if ( g_evmap [ i ].script == (char*)FAKESCRIPT_TOGGLE_HOLD ) {
772 // handle this specially
773 fakescript_hold_off();
775 } else if ( g_evmap [ i ].keydown_time ) {
776 // keyup (while key is down)
778 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
781 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
790 char holdtime [ 128 ];
791 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
792 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
795 g_evmap [ i ].keydown_time = 0; // clear the keydown-ness
796 g_evmap [ i ].last_trigger_time = time ( NULL );
801 if ( ( x = fork() ) < 0 ) {
802 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
807 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
808 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
812 } // invoke the script!
815 } // found matching event for keycode
822 void dispatch_event ( int code, int val ) {
825 // LID val decodes as:
829 pnd_log ( pndn_rem, "Dispatching Event..\n" );
831 for ( i = 0; i < g_evmap_max; i++ ) {
833 if ( ( g_evmap [ i ].key_p == 0 ) &&
834 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
835 ( g_evmap [ i ].script ) )
838 // just hand the code to the script (ie: 0 or 1 to script)
839 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
843 sprintf ( value, "%d", val );
845 g_evmap [ i ].last_trigger_time = time ( NULL );
847 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
849 if ( ( x = fork() ) < 0 ) {
850 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
855 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
856 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
861 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
865 } // found matching event for keycode
872 void sigchld_handler ( int n ) {
874 pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );
879 pnd_log ( pndn_rem, " SIGCHLD done ]---\n" );
884 unsigned char set_next_alarm ( unsigned int secs, unsigned int usecs ) {
886 // assume that SIGALRM is already being caught, we just set the itimer here
888 struct itimerval itv;
890 // if no timer at all, set the 'current' one so it does something; otherwise
892 getitimer ( ITIMER_REAL, &itv );
894 if ( itv.it_value.tv_sec == 0 && itv.it_value.tv_sec == 0 ) {
895 itv.it_value.tv_sec = secs;
896 itv.it_value.tv_usec = usecs;
899 // set the next timer
900 //bzero ( &itv, sizeof(struct itimerval) );
902 itv.it_interval.tv_sec = secs;
903 itv.it_interval.tv_usec = usecs;
905 // if next-timer is less than current, set current too
906 if ( itv.it_value.tv_sec > itv.it_interval.tv_sec ) {
907 itv.it_value.tv_sec = secs;
908 itv.it_value.tv_usec = usecs;
911 if ( setitimer ( ITIMER_REAL, &itv, NULL /* old value returned here */ ) < 0 ) {
919 void sigalrm_handler ( int n ) {
921 pnd_log ( pndn_debug, "---[ SIGALRM ]---\n" );
923 static time_t last_charge_check, last_charge_worka;
924 int batlevel = pnd_device_get_battery_gauge_perc();
928 pnd_device_get_charge_current ( &uamps );
930 if ( batlevel < 0 ) {
932 // couldn't read the battery level, so just assume low and make blinks?
933 batlevel = 4; // low, but not cause a shutdown
935 // couldn't read the battery level, so just assume ok!
940 // first -- are we critical yet? if so, shut down!
941 if ( batlevel <= b_shutdown && b_shutdown_script) {
944 // critical battery, but charging, so relax.
948 // Avoid warning again till re-powered
951 pnd_log ( pndn_error, "Battery Current: %d\n", uamps );
952 pnd_log ( pndn_error, "CRITICAL BATTERY LEVEL -- shutdown the system down! Invoke: %s\n",
955 if ( ( x = fork() ) < 0 ) {
956 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
962 sprintf ( value, "%d", b_shutdelay );
963 execl ( b_shutdown_script, b_shutdown_script, value, (char*)NULL );
964 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", b_shutdown_script );
974 if ( bc_enable && bc_charge_devices != NULL && (unsigned int)(now - last_charge_check) > 60 ) {
976 int charge_enabled = pnd_device_get_charger_enable ( bc_charge_devices );
977 if ( charge_enabled < 0 )
978 pnd_log ( pndn_error, "ERROR: Couldn't read charger enable control\n" );
981 if ( charge_enabled && batlevel >= bc_stopcap && 0 < uamps && uamps < bc_stopcur ) {
982 pnd_log ( pndn_debug, "Charge stop conditions reached, disabling charging\n" );
983 pnd_device_set_charger_enable ( bc_charge_devices, 0 );
985 else if ( !charge_enabled && batlevel <= bc_startcap ) {
986 pnd_log ( pndn_debug, "Charge start conditions reached, enabling charging\n" );
987 pnd_device_set_charger_enable ( bc_charge_devices, 1 );
990 // for some unknown reason it just stops charging randomly (happens once per week or so),
991 // and does not restart, resulting in a flat battery if machine is unattended.
992 // What seems to help here is writing to chip registers, we can do it here indirectly
993 // by writing to enable. Doing it occasionally should do no harm even with missing charger.
994 if ( batlevel <= bc_startcap && (unsigned int)(now - last_charge_worka) > 20*60 ) {
995 pnd_log ( pndn_debug, "Charge workaround trigger\n" );
996 pnd_device_set_charger_enable ( bc_charge_devices, 1 );
997 last_charge_worka = now;
1000 last_charge_check = now;
1003 // is battery warning already active?
1007 // is user charging up? if so, stop blinking.
1008 // perhaps we shoudl check if charger is connected, and not blink at all in that case..
1012 pnd_log ( pndn_debug, "Battery is high again, flipping to non-blinker mode\n" );
1014 set_next_alarm ( b_frequency, 0 );
1015 pnd_device_set_led_charger_brightness ( 250 );
1019 if ( b_active == 1 ) {
1021 pnd_log ( pndn_debug, "Blink on\n" );
1022 pnd_device_set_led_charger_brightness ( 200 );
1023 // set timer to short duration
1025 set_next_alarm ( 0, b_blinkdur );
1026 } else if ( b_active == 2 ) {
1028 pnd_log ( pndn_debug, "Blink off\n" );
1029 pnd_device_set_led_charger_brightness ( 10 );
1030 // back to longer duration
1032 set_next_alarm ( b_blinkfreq, 0 );
1039 if ( batlevel <= b_threshold && uamps < 0 ) {
1040 // battery seems low, go to active mode
1041 pnd_log ( pndn_debug, "Battery is low, flipping to blinker mode\n" );
1043 set_next_alarm ( b_blinkfreq, 0 );
1049 void fakescript_hold_on ( void ) {
1050 pnd_log ( pndn_rem, "HOLD is being enabled.\n" );
1053 for ( i = 0; i < imaxfd; i++ ) {
1054 ioctl ( fds [ i ], EVIOCGRAB, 1 /* enable */ );
1060 void fakescript_hold_off ( void ) {
1061 pnd_log ( pndn_rem, "HOLD is being disabled.\n" );
1064 for ( i = 0; i < imaxfd; i++ ) {
1065 ioctl ( fds [ i ], EVIOCGRAB, 0 /* disable */ );