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
11 #include <stdio.h> /* for printf, NULL */
12 #include <stdlib.h> /* for free */
13 #include <string.h> /* for strdup */
14 #include <unistd.h> // for exit()
15 #include <sys/types.h> // for umask
16 #include <sys/stat.h> // for umask
17 #include <fcntl.h> // for open(2)
18 #include <errno.h> // for errno
19 #include <time.h> // for time(2)
20 #include <ctype.h> // for isdigit
21 #include <signal.h> // for sigaction
22 #include <sys/wait.h> // for wait
24 #include <linux/input.h> // for keys
25 //#include "../../kernel-rip/input.h" // for keys
28 #include "pnd_container.h"
30 #include "pnd_discovery.h"
31 #include "pnd_locate.h"
32 #include "pnd_pndfiles.h"
34 #include "pnd_logger.h"
35 #include "pnd_utility.h"
36 #include "pnd_notify.h"
40 unsigned char g_daemon_mode = 0;
41 unsigned int g_minimum_separation = 1;
45 pndn_rem, // will set default log level to here, so 'debug' is omitted
49 } pndnotify_loglevels_e;
51 // key/event definition
58 keycode_t keycodes[] = {
60 { KEY_MENU, "pandora" },
61 { KEY_POWER, "power" },
71 generic_event_t generics[] = {
72 { EV_SW, 0, "lid-toggle" }, // expecting value 1 (lid close) or 0 (lid open)
76 // event-to-sh mapping
80 unsigned char key_p; // 1 if its a key, otherwise an event
82 /* template information
84 void *reqs; // scancode/etc for the event in question
86 char *script; // script to invoke
87 //unsigned int hold_min; // minimum hold-time to trigger
91 time_t last_trigger_time;
97 evmap_t g_evmap [ MAXEVENTS ];
98 unsigned int g_evmap_max = 0;
102 void dispatch_key ( int keycode, int val );
103 void dispatch_event ( int code, int val );
104 void sigchld_handler ( int n );
106 static void usage ( char *argv[] ) {
107 printf ( "%s [-d]\n", argv [ 0 ] );
108 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
109 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" );
113 int main ( int argc, char *argv[] ) {
115 int logall = -1; // -1 means normal logging rules; >=0 means log all!
117 for ( i = 1; i < argc; i++ ) {
119 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
120 //printf ( "Going daemon mode. Silent running.\n" );
122 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
124 if ( isdigit ( argv [ i ][ 2 ] ) ) {
125 unsigned char x = atoi ( argv [ i ] + 2 );
143 pnd_log_set_pretext ( "pndevmapperd" );
144 pnd_log_set_flush ( 1 );
146 if ( logall == -1 ) {
147 // standard logging; non-daemon versus daemon
149 if ( g_daemon_mode ) {
152 pnd_log_set_filter ( pndn_rem );
159 f = fopen ( "/tmp/pndevmapperd.log", "w" );
162 pnd_log_set_filter ( logall );
163 pnd_log_to_stream ( f );
164 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
167 if ( logall == pndn_debug ) {
168 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
169 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
174 pnd_log ( pndn_rem, "%s built %s %s", argv [ 0 ], __DATE__, __TIME__ );
176 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
178 // basic daemon set up
179 if ( g_daemon_mode ) {
181 // set a CWD somewhere else
184 // detach from terminal
185 if ( ( i = fork() ) < 0 ) {
186 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
190 exit ( 0 ); // exit parent
195 umask ( 022 ); // emitted files can be rwxr-xr-x
199 /* hmm, seems to not like working right after boot.. do we depend on another daemon or
200 * on giving kernel time to init something, or ... wtf?
201 * -- lets give the system some time to wake up
205 // this one works for pndnotifyd, which actually needs INOTIFYH..
208 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
209 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
211 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
212 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
216 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
218 // pndnotifyd also waits for user to log in .. pretty excessive, especially since
219 // what if user wants to close the lid while at the log in screen? for now play the
220 // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
223 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
224 // log-out and back in again, with SDs popping in and out between..
225 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
226 char tmp_username [ 128 ];
228 if ( pnd_check_login ( tmp_username, 127 ) ) {
231 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
234 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
238 /* inhale config or die trying
242 // attempt to fetch a sensible default searchpath for configs
243 configpath = pnd_conf_query_searchpath();
245 // attempt to fetch the apps config. since it finds us the runscript
246 pnd_conf_handle evmaph;
248 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
251 // couldn't locate conf, just bail
252 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
256 /* iterate across conf, stocking the event map
258 void *n = pnd_box_get_head ( evmaph );
261 char *k = pnd_box_get_key ( n );
262 //printf ( "key %s\n", k );
264 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
267 // keys should really push push generic-events onto the table, since they;'re just a special case of them
268 // to make things easier to read
270 // figure out which keycode we're talking about
271 keycode_t *p = keycodes;
272 while ( p -> keycode != -1 ) {
273 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
279 if ( p -> keycode != -1 ) {
280 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
281 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
282 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
283 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s\n", p -> keyname, p -> keycode, (char*) n );
286 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
289 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
292 // yes, key events could really be defined in this generic sense, and really we could just let people
293 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
294 // add a 'generic' section to conf file and just let folks redefine random events that way
295 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
296 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
297 // should just exist as scripts rather than daemons, but whose counting?
299 // figure out which keycode we're talking about
300 generic_event_t *p = generics;
301 while ( p -> code != -1 ) {
302 if ( strcasecmp ( p -> name, k ) == 0 ) {
308 if ( p -> code != -1 ) {
309 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
310 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
311 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
312 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
315 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
318 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
319 // not consumed here, skip silently
323 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
326 n = pnd_box_get_next ( n );
329 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
330 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
331 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
334 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
335 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
336 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
339 /* do we have anything to do?
341 if ( ! g_evmap_max ) {
342 // uuuh, nothing to do?
343 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
347 /* set up sigchld -- don't want zombies all over; well, we do, but not process zombies
352 struct sigaction siggy;
353 siggy.sa_handler = sigchld_handler;
354 siggy.sa_mask = ss; /* implicitly blocks the origin signal */
355 siggy.sa_flags = 0; /* don't need anything */
357 sigaction ( SIGCHLD, &siggy, NULL );
359 /* actually try to do something useful
362 // stolen in part from notaz :)
364 // try to locate the appropriate devices
366 int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
369 for ( id = 0; ; id++ ) {
371 char name[256] = { 0, };
374 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
375 fd = open ( fname, O_RDONLY );
381 ioctl (fd, EVIOCGNAME(sizeof(name)), name );
383 if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
385 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
387 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
390 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
395 if (imaxfd < fd) imaxfd = fd;
398 if ( fds [ 0 ] == -1 ) {
399 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
402 if ( fds [ 1 ] == -1 ) {
403 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
406 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
407 pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
411 /* loop forever, watching for events
415 struct input_event ev[64];
417 int fd = -1, rd, ret;
422 for (i = 0; i < 2; i++) {
423 if ( fds [ i ] != -1 ) {
424 FD_SET( fds [ i ], &fdset );
428 ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
431 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
435 for ( i = 0; i < 2; i++ ) {
436 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
441 /* buttons or keypad */
442 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
443 if ( rd < (int) sizeof(struct input_event) ) {
444 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
448 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
450 if ( ev[i].type == EV_SYN ) {
452 } else if ( ev[i].type == EV_KEY ) {
454 // do we even know about this key at all?
455 keycode_t *p = keycodes;
456 while ( p -> keycode != -1 ) {
457 if ( p -> keycode == ev [ i ].code ) {
463 // if we do, hand it off to dispatcher to look up if we actually do something with it
464 if ( p -> keycode != -1 ) {
465 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
466 dispatch_key ( p -> keycode, ev [ i ].value );
468 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
471 } else if ( ev[i].type == EV_SW ) {
473 // do we even know about this event at all?
474 generic_event_t *p = generics;
475 while ( p -> code != -1 ) {
476 if ( p -> code == ev [ i ].code ) {
482 // if we do, hand it off to dispatcher to look up if we actually do something with it
483 if ( p -> code != -1 ) {
484 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
485 dispatch_event ( p -> code, ev [ i ].value );
487 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
491 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
499 for (i = 0; i < 2; i++) {
500 if ( i != 2 && fds [ i ] != -1 ) {
508 // this should really register the keystate and time, and then another func to monitor
509 // time-passage and check the registered list and act on the event..
510 void dispatch_key ( int keycode, int val ) {
514 // 1 - down (pressed)
515 // 2 - down again (hold)
518 for ( i = 0; i < g_evmap_max; i++ ) {
520 if ( ( g_evmap [ i ].key_p ) &&
521 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
522 ( g_evmap [ i ].script ) )
525 // is this a keydown or a keyup?
528 g_evmap [ i ].keydown_time = time ( NULL );
530 } else if ( val == 0 ) {
533 char holdtime [ 128 ];
534 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
536 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
539 g_evmap [ i ].last_trigger_time = time ( NULL );
541 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
543 if ( ( x = fork() ) < 0 ) {
544 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
549 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
550 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
555 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
561 } // found matching event for keycode
568 void dispatch_event ( int code, int val ) {
571 // LID val decodes as:
575 for ( i = 0; i < g_evmap_max; i++ ) {
577 if ( ( g_evmap [ i ].key_p == 0 ) &&
578 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
579 ( g_evmap [ i ].script ) )
582 // just hand the code to the script (ie: 0 or 1 to script)
583 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
587 sprintf ( value, "%d", val );
589 g_evmap [ i ].last_trigger_time = time ( NULL );
591 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
593 if ( ( x = fork() ) < 0 ) {
594 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
599 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
600 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
605 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
609 } // found matching event for keycode
616 void sigchld_handler ( int n ) {
618 pnd_log ( pndn_rem, "---[ SIGCHLD received ]---\n" );