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)
21 #include <linux/input.h> // for keys
22 //#include "../../kernel-rip/input.h" // for keys
25 #include "pnd_container.h"
27 #include "pnd_discovery.h"
28 #include "pnd_locate.h"
29 #include "pnd_pndfiles.h"
31 #include "pnd_logger.h"
35 unsigned char g_daemon_mode = 0;
36 unsigned int g_minimum_separation = 1;
40 pndn_rem, // will set default log level to here, so 'debug' is omitted
44 } pndnotify_loglevels_e;
46 // key/event definition
53 keycode_t keycodes[] = {
55 { KEY_MENU, "pandora" },
56 { KEY_POWER, "power" },
66 generic_event_t generics[] = {
67 { EV_SW, 0, "lid-toggle" }, // expecting value 1 (lid close) or 0 (lid open)
71 // event-to-sh mapping
75 unsigned char key_p; // 1 if its a key, otherwise an event
77 /* template information
79 void *reqs; // scancode/etc for the event in question
81 char *script; // script to invoke
82 //unsigned int hold_min; // minimum hold-time to trigger
86 time_t last_trigger_time;
92 evmap_t g_evmap [ MAXEVENTS ];
93 unsigned int g_evmap_max = 0;
97 void dispatch_key ( int keycode, int val );
98 void dispatch_event ( int code, int val );
100 static void usage ( char *argv[] ) {
101 printf ( "%s [-d]\n", argv [ 0 ] );
102 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
103 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
107 int main ( int argc, char *argv[] ) {
110 for ( i = 1; i < argc; i++ ) {
112 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
113 //printf ( "Going daemon mode. Silent running.\n" );
124 if ( g_daemon_mode ) {
127 pnd_log_set_filter ( pndn_rem );
128 pnd_log_set_pretext ( "pndevmapperd" );
130 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
133 // basic daemon set up
134 if ( g_daemon_mode ) {
136 // set a CWD somewhere else
139 // detach from terminal
140 if ( ( i = fork() ) < 0 ) {
141 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
145 exit ( 0 ); // exit parent
150 umask ( 022 ); // emitted files can be rwxr-xr-x
154 /* inhale config or die trying
158 // attempt to fetch a sensible default searchpath for configs
159 configpath = pnd_conf_query_searchpath();
161 // attempt to fetch the apps config. since it finds us the runscript
162 pnd_conf_handle evmaph;
164 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
167 // couldn't locate conf, just bail
168 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
172 /* iterate across conf, stocking the event map
174 void *n = pnd_box_get_head ( evmaph );
177 char *k = pnd_box_get_key ( n );
178 //printf ( "key %s\n", k );
180 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
183 // keys should really push push generic-events onto the table, since they;'re just a special case of them
184 // to make things easier to read
186 // figure out which keycode we're talking about
187 keycode_t *p = keycodes;
188 while ( p -> keycode != -1 ) {
189 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
195 if ( p -> keycode != -1 ) {
196 g_evmap [ g_evmap_max ].key_p = 1; // its a key, not an event
197 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
198 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
199 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s\n", p -> keyname, p -> keycode, (char*) n );
202 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
205 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
208 // yes, key events could really be defined in this generic sense, and really we could just let people
209 // put the code and so on right in the conf, but trying to keep it easy on people; maybe should
210 // add a 'generic' section to conf file and just let folks redefine random events that way
211 // Really, it'd be nice if the /dev/input/events could spit out useful text, and just use scripts
212 // to respond without a daemon per se; for that matter, pnd-ls and pnd-map pnd-dotdesktopemitter
213 // should just exist as scripts rather than daemons, but whose counting?
215 // figure out which keycode we're talking about
216 generic_event_t *p = generics;
217 while ( p -> code != -1 ) {
218 if ( strcasecmp ( p -> name, k ) == 0 ) {
224 if ( p -> code != -1 ) {
225 g_evmap [ g_evmap_max ].key_p = 0; // its an event, not a key
226 g_evmap [ g_evmap_max ].reqs = p; // note the keycode
227 g_evmap [ g_evmap_max ].script = n; // note the script to activate in response
228 pnd_log ( pndn_rem, "Registered generic event %s [%d] to script %s\n", p -> name, p -> code, (char*) n );
231 pnd_log ( pndn_warning, "WARNING! Generic event '%s' is not handled by pndevmapperd yet! Skipping.", k );
234 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
235 // not consumed here, skip silently
239 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
242 n = pnd_box_get_next ( n );
245 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
246 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
247 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
250 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
251 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
252 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
255 /* do we have anything to do?
257 if ( ! g_evmap_max ) {
258 // uuuh, nothing to do?
259 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
263 /* actually try to do something useful
266 // stolen in part from notaz :)
268 // try to locate the appropriate devices
270 int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
273 for ( id = 0; ; id++ ) {
275 char name[256] = { 0, };
278 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
279 fd = open ( fname, O_RDONLY );
285 ioctl (fd, EVIOCGNAME(sizeof(name)), name );
287 if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
289 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
291 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
294 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
299 if (imaxfd < fd) imaxfd = fd;
302 if ( fds [ 0 ] == -1 ) {
303 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
306 if ( fds [ 1 ] == -1 ) {
307 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
310 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
311 pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
315 /* loop forever, watching for events
319 struct input_event ev[64];
321 int fd = -1, rd, ret;
326 for (i = 0; i < 2; i++) {
327 if ( fds [ i ] != -1 ) {
328 FD_SET( fds [ i ], &fdset );
332 ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
335 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
339 for ( i = 0; i < 2; i++ ) {
340 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
345 /* buttons or keypad */
346 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
347 if ( rd < (int) sizeof(struct input_event) ) {
348 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
352 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
354 if ( ev[i].type == EV_SYN ) {
356 } else if ( ev[i].type == EV_KEY ) {
358 // do we even know about this key at all?
359 keycode_t *p = keycodes;
360 while ( p -> keycode != -1 ) {
361 if ( p -> keycode == ev [ i ].code ) {
367 // if we do, hand it off to dispatcher to look up if we actually do something with it
368 if ( p -> keycode != -1 ) {
369 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
370 dispatch_key ( p -> keycode, ev [ i ].value );
372 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
375 } else if ( ev[i].type == EV_SW ) {
377 // do we even know about this event at all?
378 generic_event_t *p = generics;
379 while ( p -> code != -1 ) {
380 if ( p -> code == ev [ i ].code ) {
386 // if we do, hand it off to dispatcher to look up if we actually do something with it
387 if ( p -> code != -1 ) {
388 pnd_log ( pndn_debug, "Generic Event: event %s [%d] value %d\n", p -> name, p -> code, ev [ i ].value );
389 dispatch_event ( p -> code, ev [ i ].value );
391 pnd_log ( pndn_warning, "Unknown Generic Event: code %d value %d\n", ev [ i ].code, ev [ i ].value );
395 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
403 for (i = 0; i < 2; i++) {
404 if ( i != 2 && fds [ i ] != -1 ) {
412 // this should really register the keystate and time, and then another func to monitor
413 // time-passage and check the registered list and act on the event..
414 void dispatch_key ( int keycode, int val ) {
418 // 1 - down (pressed)
419 // 2 - down again (hold)
422 for ( i = 0; i < g_evmap_max; i++ ) {
424 if ( ( g_evmap [ i ].key_p ) &&
425 ( ((keycode_t*) (g_evmap [ i ].reqs)) -> keycode == keycode ) &&
426 ( g_evmap [ i ].script ) )
429 // is this a keydown or a keyup?
432 g_evmap [ i ].keydown_time = time ( NULL );
434 } else if ( val == 0 ) {
437 char holdtime [ 128 ];
438 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
440 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
443 g_evmap [ i ].last_trigger_time = time ( NULL );
445 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
447 if ( ( x = fork() ) < 0 ) {
448 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
453 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
454 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
459 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
465 } // found matching event for keycode
472 void dispatch_event ( int code, int val ) {
475 // LID val decodes as:
479 for ( i = 0; i < g_evmap_max; i++ ) {
481 if ( ( g_evmap [ i ].key_p == 0 ) &&
482 ( ((generic_event_t*) (g_evmap [ i ].reqs)) -> code == code ) &&
483 ( g_evmap [ i ].script ) )
486 // just hand the code to the script (ie: 0 or 1 to script)
487 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
491 sprintf ( value, "%d", val );
493 g_evmap [ i ].last_trigger_time = time ( NULL );
495 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, value );
497 if ( ( x = fork() ) < 0 ) {
498 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
503 execl ( g_evmap [ i ].script, g_evmap [ i ].script, value, (char*)NULL );
504 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
509 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
513 } // found matching event for keycode