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
22 #include <linux/input.h> // for keys
23 //#include "../../kernel-rip/input.h" // for keys
26 #include "pnd_container.h"
28 #include "pnd_discovery.h"
29 #include "pnd_locate.h"
30 #include "pnd_pndfiles.h"
32 #include "pnd_logger.h"
33 #include "pnd_utility.h"
34 #include "pnd_notify.h"
38 unsigned char g_daemon_mode = 0;
39 unsigned int g_minimum_separation = 1;
43 pndn_rem, // will set default log level to here, so 'debug' is omitted
47 } pndnotify_loglevels_e;
49 // key/event definition
56 keycode_t keycodes[] = {
58 { KEY_MENU, "pandora" },
59 { KEY_POWER, "power" },
69 generic_event_t generics[] = {
70 { EV_SW, 0, "lid-toggle" }, // expecting value 1 (lid close) or 0 (lid open)
74 // event-to-sh mapping
78 unsigned char key_p; // 1 if its a key, otherwise an event
80 /* template information
82 void *reqs; // scancode/etc for the event in question
84 char *script; // script to invoke
85 //unsigned int hold_min; // minimum hold-time to trigger
89 time_t last_trigger_time;
95 evmap_t g_evmap [ MAXEVENTS ];
96 unsigned int g_evmap_max = 0;
100 void dispatch_key ( int keycode, int val );
101 void dispatch_event ( int code, int val );
103 static void usage ( char *argv[] ) {
104 printf ( "%s [-d]\n", argv [ 0 ] );
105 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
106 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" );
107 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
111 int main ( int argc, char *argv[] ) {
113 int logall = -1; // -1 means normal logging rules; >=0 means log all!
115 for ( i = 1; i < argc; i++ ) {
117 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
118 //printf ( "Going daemon mode. Silent running.\n" );
120 } else if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'l' ) {
122 if ( isdigit ( argv [ i ][ 2 ] ) ) {
123 unsigned char x = atoi ( argv [ i ] + 2 );
141 pnd_log_set_pretext ( "pndevmapperd" );
142 pnd_log_set_flush ( 1 );
144 if ( logall == -1 ) {
145 // standard logging; non-daemon versus daemon
147 if ( g_daemon_mode ) {
150 pnd_log_set_filter ( pndn_rem );
157 f = fopen ( "/tmp/pndevmapperd.log", "w" );
160 pnd_log_set_filter ( logall );
161 pnd_log_to_stream ( f );
162 pnd_log ( pndn_rem, "logall mode - logging to /tmp/pndevmapperd.log\n" );
165 if ( logall == pndn_debug ) {
166 pnd_log_set_buried_logging ( 1 ); // log the shit out of it
167 pnd_log ( pndn_rem, "logall mode 0 - turned on buried logging\n" );
172 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
174 // basic daemon set up
175 if ( g_daemon_mode ) {
177 // set a CWD somewhere else
180 // detach from terminal
181 if ( ( i = fork() ) < 0 ) {
182 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
186 exit ( 0 ); // exit parent
191 umask ( 022 ); // emitted files can be rwxr-xr-x
195 /* hmm, seems to not like working right after boot.. do we depend on another daemon or
196 * on giving kernel time to init something, or ... wtf?
197 * -- lets give the system some time to wake up
201 // this one works for pndnotifyd, which actually needs INOTIFYH..
204 // check if inotify is awake yet; if not, try waiting for awhile to see if it does
205 pnd_log ( pndn_rem, "Starting INOTIFY test; should be instant, but may take awhile...\n" );
207 if ( ! pnd_notify_wait_until_ready ( 120 /* seconds */ ) ) {
208 pnd_log ( pndn_error, "ERROR: INOTIFY refuses to be useful and quite awhile has passed. Bailing out.\n" );
212 pnd_log ( pndn_rem, "INOTIFY seems to be useful, whew.\n" );
214 // pndnotifyd also waits for user to log in .. pretty excessive, especially since
215 // what if user wants to close the lid while at the log in screen? for now play the
216 // odds as thats pretty unliekly usage scenariom but is clearly not acceptible :/
219 // wait for a user to be logged in - we should probably get hupped when a user logs in, so we can handle
220 // log-out and back in again, with SDs popping in and out between..
221 pnd_log ( pndn_rem, "Checking to see if a user is logged in\n" );
222 char tmp_username [ 128 ];
224 if ( pnd_check_login ( tmp_username, 127 ) ) {
227 pnd_log ( pndn_debug, " No one logged in yet .. spinning.\n" );
230 pnd_log ( pndn_rem, "Looks like user '%s' is in, continue.\n", tmp_username );
234 /* inhale config or die trying
238 // attempt to fetch a sensible default searchpath for configs
239 configpath = pnd_conf_query_searchpath();
241 // attempt to fetch the apps config. since it finds us the runscript
242 pnd_conf_handle evmaph;
244 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
247 // couldn't locate conf, just bail
248 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
252 /* iterate across conf, stocking the event map
254 void *n = pnd_box_get_head ( evmaph );
257 char *k = pnd_box_get_key ( n );
258 //printf ( "key %s\n", k );
260 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
263 // keys should really push push generic-events onto the table, since they;'re just a special case of them
264 // to make things easier to read
266 // figure out which keycode we're talking about
267 keycode_t *p = keycodes;
268 while ( p -> keycode != -1 ) {
269 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
275 if ( p -> keycode != -1 ) {
276 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
277 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
278 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
279 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s\n", p -> keyname, p -> keycode, (char*) n );
282 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
285 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
288 // yes, key events could really be defined in this generic sense, and really we could just let people
289 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
290 // add a 'generic' section to conf file and just let folks redefine random events that way
291 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
292 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
293 // should just exist as scripts rather than daemons, but whose counting?
295 // figure out which keycode we're talking about
296 generic_event_t *p = generics;
297 while ( p -> code != -1 ) {
298 if ( strcasecmp ( p -> name, k ) == 0 ) {
304 if ( p -> code != -1 ) {
305 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
306 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
307 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
308 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
311 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
314 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
315 // not consumed here, skip silently
319 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
322 n = pnd_box_get_next ( n );
325 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
326 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
327 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
330 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
331 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
332 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
335 /* do we have anything to do?
337 if ( ! g_evmap_max ) {
338 // uuuh, nothing to do?
339 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
343 /* actually try to do something useful
346 // stolen in part from notaz :)
348 // try to locate the appropriate devices
350 int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
353 for ( id = 0; ; id++ ) {
355 char name[256] = { 0, };
358 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
359 fd = open ( fname, O_RDONLY );
365 ioctl (fd, EVIOCGNAME(sizeof(name)), name );
367 if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
369 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
371 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
374 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
379 if (imaxfd < fd) imaxfd = fd;
382 if ( fds [ 0 ] == -1 ) {
383 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
386 if ( fds [ 1 ] == -1 ) {
387 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
390 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
391 pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
395 /* loop forever, watching for events
399 struct input_event ev[64];
401 int fd = -1, rd, ret;
406 for (i = 0; i < 2; i++) {
407 if ( fds [ i ] != -1 ) {
408 FD_SET( fds [ i ], &fdset );
412 ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
415 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
419 for ( i = 0; i < 2; i++ ) {
420 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
425 /* buttons or keypad */
426 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
427 if ( rd < (int) sizeof(struct input_event) ) {
428 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
432 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
434 if ( ev[i].type == EV_SYN ) {
436 } else if ( ev[i].type == EV_KEY ) {
438 // do we even know about this key at all?
439 keycode_t *p = keycodes;
440 while ( p -> keycode != -1 ) {
441 if ( p -> keycode == ev [ i ].code ) {
447 // if we do, hand it off to dispatcher to look up if we actually do something with it
448 if ( p -> keycode != -1 ) {
449 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
450 dispatch_key ( p -> keycode, ev [ i ].value );
452 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
455 } else if ( ev[i].type == EV_SW ) {
457 // do we even know about this event at all?
458 generic_event_t *p = generics;
459 while ( p -> code != -1 ) {
460 if ( p -> code == ev [ i ].code ) {
466 // if we do, hand it off to dispatcher to look up if we actually do something with it
467 if ( p -> code != -1 ) {
468 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
469 dispatch_event ( p -> code, ev [ i ].value );
471 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
475 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
483 for (i = 0; i < 2; i++) {
484 if ( i != 2 && fds [ i ] != -1 ) {
492 // this should really register the keystate and time, and then another func to monitor
493 // time-passage and check the registered list and act on the event..
494 void dispatch_key ( int keycode, int val ) {
498 // 1 - down (pressed)
499 // 2 - down again (hold)
502 for ( i = 0; i < g_evmap_max; i++ ) {
504 if ( ( g_evmap [ i ].key_p ) &&
505 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
506 ( g_evmap [ i ].script ) )
509 // is this a keydown or a keyup?
512 g_evmap [ i ].keydown_time = time ( NULL );
514 } else if ( val == 0 ) {
517 char holdtime [ 128 ];
518 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
520 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
523 g_evmap [ i ].last_trigger_time = time ( NULL );
525 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
527 if ( ( x = fork() ) < 0 ) {
528 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
533 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
534 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
539 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
545 } // found matching event for keycode
552 void dispatch_event ( int code, int val ) {
555 // LID val decodes as:
559 for ( i = 0; i < g_evmap_max; i++ ) {
561 if ( ( g_evmap [ i ].key_p == 0 ) &&
562 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
563 ( g_evmap [ i ].script ) )
566 // just hand the code to the script (ie: 0 or 1 to script)
567 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
571 sprintf ( value, "%d", val );
573 g_evmap [ i ].last_trigger_time = time ( NULL );
575 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
577 if ( ( x = fork() ) < 0 ) {
578 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
583 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
584 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
589 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
593 } // found matching event for keycode