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 #include <stdio.h> /* for printf, NULL */
9 #include <stdlib.h> /* for free */
10 #include <string.h> /* for strdup */
11 #include <unistd.h> // for exit()
12 #include <sys/types.h> // for umask
13 #include <sys/stat.h> // for umask
14 #include <fcntl.h> // for open(2)
15 #include <errno.h> // for errno
16 #include <time.h> // for time(2)
18 #include <linux/input.h> // for keys
19 //#include "../../kernel-rip/input.h" // for keys
22 #include "pnd_container.h"
24 #include "pnd_discovery.h"
25 #include "pnd_locate.h"
26 #include "pnd_pndfiles.h"
28 #include "pnd_logger.h"
32 unsigned char g_daemon_mode = 0;
33 unsigned int g_minimum_separation = 1;
37 pndn_rem, // will set default log level to here, so 'debug' is omitted
41 } pndnotify_loglevels_e;
43 // event-to-sh mapping
47 /* template information
49 unsigned char key_p; // 1 if its a key, otherwise an event
50 int keycode; // scancode for the key in question
51 char *script; // script to invoke
52 //unsigned int hold_min; // minimum hold-time to trigger
56 time_t last_trigger_time;
62 evmap_t g_evmap [ MAXEVENTS ];
63 unsigned int g_evmap_max = 0;
72 keycode_t keycodes[] = {
74 { KEY_MENU, "pandora" },
80 void dispatch_key ( int keycode, int val );
82 static void usage ( char *argv[] ) {
83 printf ( "%s [-d]\n", argv [ 0 ] );
84 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
85 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
89 int main ( int argc, char *argv[] ) {
92 for ( i = 1; i < argc; i++ ) {
94 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
95 //printf ( "Going daemon mode. Silent running.\n" );
106 if ( g_daemon_mode ) {
109 pnd_log_set_filter ( pndn_rem );
110 pnd_log_set_pretext ( "pndevmapperd" );
112 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
115 // basic daemon set up
116 if ( g_daemon_mode ) {
118 // set a CWD somewhere else
121 // detach from terminal
122 if ( ( i = fork() ) < 0 ) {
123 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
127 exit ( 0 ); // exit parent
132 umask ( 022 ); // emitted files can be rwxr-xr-x
136 /* inhale config or die trying
140 // attempt to fetch a sensible default searchpath for configs
141 configpath = pnd_conf_query_searchpath();
143 // attempt to fetch the apps config. since it finds us the runscript
144 pnd_conf_handle evmaph;
146 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
149 // couldn't locate conf, just bail
150 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
154 /* iterate across conf, stocking the event map
156 void *n = pnd_box_get_head ( evmaph );
159 char *k = pnd_box_get_key ( n );
160 //printf ( "key %s\n", k );
162 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
165 // figure out which keycode we're talking about
166 keycode_t *p = keycodes;
167 while ( p -> keycode != -1 ) {
168 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
174 if ( p -> keycode != -1 ) {
175 g_evmap [ g_evmap_max ].key_p = 1;
176 g_evmap [ g_evmap_max ].keycode = p -> keycode;
177 g_evmap [ g_evmap_max ].script = n;
178 pnd_log ( pndn_rem, "Registered key %s [%d] to script %s\n", p -> keyname, p -> keycode, (char*) n );
181 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
184 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
187 } else if ( strncmp ( k, "pndevmapperd.", 7 ) == 0 ) {
188 // not consumed here, skip silently
192 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
195 n = pnd_box_get_next ( n );
198 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
199 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
200 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
203 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" ) != PND_CONF_BADNUM ) {
204 g_minimum_separation = pnd_conf_get_as_int ( evmaph, "pndevmapperd.minimum_separation" );
205 pnd_log ( pndn_rem, "config file causes minimum_separation to change to %u", g_minimum_separation );
208 /* do we have anything to do?
210 if ( ! g_evmap_max ) {
211 // uuuh, nothing to do?
212 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
216 /* actually try to do something useful
219 // stolen in part from notaz :)
221 // try to locate the appropriate devices
223 int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
226 for ( id = 0; ; id++ ) {
228 char name[256] = { 0, };
231 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
232 fd = open ( fname, O_RDONLY );
238 ioctl (fd, EVIOCGNAME(sizeof(name)), name );
240 if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
242 } else if ( strcmp ( name, "gpio-keys" ) == 0) {
244 } else if ( strcmp ( name, "AT Translated Set 2 keyboard" ) == 0) { // for vmware, my dev environment
247 pnd_log ( pndn_rem, "Ignoring unknown device '%s'\n", name );
252 if (imaxfd < fd) imaxfd = fd;
255 if ( fds [ 0 ] == -1 ) {
256 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
259 if ( fds [ 1 ] == -1 ) {
260 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
263 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
264 pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
268 /* loop forever, watching for events
272 struct input_event ev[64];
274 int fd = -1, rd, ret;
279 for (i = 0; i < 2; i++) {
280 if ( fds [ i ] != -1 ) {
281 FD_SET( fds [ i ], &fdset );
285 ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
288 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
292 for ( i = 0; i < 2; i++ ) {
293 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
298 /* buttons or keypad */
299 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
300 if ( rd < (int) sizeof(struct input_event) ) {
301 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
305 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
307 if ( ev[i].type == EV_SYN ) {
309 } else if ( ev[i].type == EV_KEY ) {
311 keycode_t *p = keycodes;
312 while ( p -> keycode != -1 ) {
313 if ( p -> keycode == ev [ i ].code ) {
319 if ( p -> keycode != -1 ) {
320 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
321 dispatch_key ( p -> keycode, ev [ i ].value );
323 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
327 pnd_log ( pndn_debug, "DEBUG: Unexpected event type %i received\n", ev[i].type );
335 for (i = 0; i < 2; i++) {
336 if ( i != 2 && fds [ i ] != -1 ) {
344 // this should really register the keystate and time, and then another func to monitor
345 // time-passage and check the registered list and act on the event..
346 void dispatch_key ( int keycode, int val ) {
350 // 1 - down (pressed)
351 // 2 - down again (hold)
354 for ( i = 0; i < g_evmap_max; i++ ) {
356 if ( ( g_evmap [ i ].key_p ) &&
357 ( g_evmap [ i ].keycode == keycode ) &&
358 ( g_evmap [ i ].script ) )
361 // is this a keydown or a keyup?
364 g_evmap [ i ].keydown_time = time ( NULL );
366 } else if ( val == 0 ) {
369 char holdtime [ 128 ];
370 sprintf ( holdtime, "%d", (int)( time(NULL) - g_evmap [ i ].keydown_time ) );
372 if ( time ( NULL ) - g_evmap [ i ].last_trigger_time >= g_minimum_separation ) {
375 g_evmap [ i ].last_trigger_time = time ( NULL );
377 pnd_log ( pndn_rem, "Will attempt to invoke: %s %s\n", g_evmap [ i ].script, holdtime );
379 if ( ( x = fork() ) < 0 ) {
380 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
385 execl ( g_evmap [ i ].script, g_evmap [ i ].script, holdtime, (char*)NULL );
386 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );
391 pnd_log ( pndn_rem, "Skipping invokation.. falls within minimum_separation threshold\n" );
397 } // found matching event for keycode