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
17 #include <linux/input.h> // for keys
18 //#include "../../kernel-rip/input.h" // for keys
21 #include "pnd_container.h"
23 #include "pnd_discovery.h"
24 #include "pnd_locate.h"
25 #include "pnd_pndfiles.h"
27 #include "pnd_logger.h"
31 unsigned char g_daemon_mode = 0;
35 pndn_rem, // will set default log level to here, so 'debug' is omitted
39 } pndnotify_loglevels_e;
41 // event-to-sh mapping
44 unsigned char key_p; // 1 if its a key, otherwise an event
45 int keycode; // scancode for the key in question
46 char *script; // script to invoke
47 //unsigned int hold_min; // minimum hold-time to trigger
51 evmap_t g_evmap [ MAXEVENTS ];
52 unsigned int g_evmap_max = 0;
61 keycode_t keycodes[] = {
63 { KEY_MENU, "pandora" },
69 void dispatch_key ( int keycode, int val );
71 static void usage ( char *argv[] ) {
72 printf ( "%s [-d]\n", argv [ 0 ] );
73 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
74 printf ( "Signal: HUP the process to force reload of configuration and reset the notifier watch paths\n" );
78 int main ( int argc, char *argv[] ) {
81 for ( i = 1; i < argc; i++ ) {
83 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
84 //printf ( "Going daemon mode. Silent running.\n" );
95 if ( g_daemon_mode ) {
98 pnd_log_set_filter ( pndn_rem );
99 pnd_log_set_pretext ( "pndevmapperd" );
101 pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
104 // basic daemon set up
105 if ( g_daemon_mode ) {
107 // set a CWD somewhere else
110 // detach from terminal
111 if ( ( i = fork() ) < 0 ) {
112 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
116 exit ( 0 ); // exit parent
121 umask ( 022 ); // emitted files can be rwxr-xr-x
125 /* inhale config or die trying
129 // attempt to fetch a sensible default searchpath for configs
130 configpath = pnd_conf_query_searchpath();
132 // attempt to fetch the apps config. since it finds us the runscript
133 pnd_conf_handle evmaph;
135 evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
138 // couldn't locate conf, just bail
139 pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
143 /* iterate across conf, stocking the event map
145 void *n = pnd_box_get_head ( evmaph );
148 char *k = pnd_box_get_key ( n );
149 //printf ( "key %s\n", k );
151 if ( strncmp ( k, "keys.", 5 ) == 0 ) {
154 // figure out which keycode we're talking about
155 keycode_t *p = keycodes;
156 while ( p -> keycode != -1 ) {
157 if ( strcasecmp ( p -> keyname, k ) == 0 ) {
163 if ( p -> keycode != -1 ) {
164 g_evmap [ g_evmap_max ].key_p = 1;
165 g_evmap [ g_evmap_max ].keycode = p -> keycode;
166 g_evmap [ g_evmap_max ].script = n;
167 pnd_log ( pndn_debug, "Registered key %s [%d] to script %s\n", p -> keyname, p -> keycode, (char*) n );
170 pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
173 } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
178 pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
181 n = pnd_box_get_next ( n );
184 if ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) != PND_CONF_BADNUM ) {
185 pnd_log_set_filter ( pnd_conf_get_as_int ( evmaph, "pndevmapperd.loglevel" ) );
186 pnd_log ( pndn_rem, "config file causes loglevel to change to %u", pnd_log_get_filter() );
189 /* do we have anything to do?
191 if ( ! g_evmap_max ) {
192 // uuuh, nothing to do?
193 pnd_log ( pndn_warning, "WARNING! No events configured to watch, so just spinning wheels...\n" );
197 /* actually try to do something useful
200 // stolen in part from notaz :)
202 // try to locate the appropriate devices
204 int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
207 for ( id = 0; ; id++ ) {
209 char name[256] = { 0, };
212 snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
213 fd = open ( fname, O_RDONLY );
219 ioctl (fd, EVIOCGNAME(sizeof(name)), name );
221 if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
223 } else if (strcmp(name, "gpio-keys") == 0) {
230 if (imaxfd < fd) imaxfd = fd;
233 if ( fds [ 0 ] == -1 ) {
234 pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
237 if ( fds [ 1 ] == -1 ) {
238 pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
241 if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
242 pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
246 /* loop forever, watching for events
250 struct input_event ev[64];
252 int fd = -1, rd, ret;
257 for (i = 0; i < 2; i++) {
258 if ( fds [ i ] != -1 ) {
259 FD_SET( fds [ i ], &fdset );
263 ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
266 pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
270 for ( i = 0; i < 2; i++ ) {
271 if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
276 /* buttons or keypad */
277 rd = read ( fd, ev, sizeof(struct input_event) * 64 );
278 if ( rd < (int) sizeof(struct input_event) ) {
279 pnd_log ( pndn_error, "ERROR! read(2) input_event failed with: %s\n", strerror ( errno ) );
283 for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
285 if ( ev[i].type == EV_SYN ) {
287 } else if ( ev[i].type == EV_KEY ) {
289 keycode_t *p = keycodes;
290 while ( p -> keycode != -1 ) {
291 if ( p -> keycode == ev [ i ].code ) {
297 if ( p -> keycode != -1 ) {
298 pnd_log ( pndn_debug, "Key Event: key %s [%d] value %d\n", p -> keyname, p -> keycode, ev [ i ].value );
299 dispatch_key ( p -> keycode, ev [ i ].value );
301 pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n", ev [ i ].code, ev [ i ].value );
305 pnd_log ( pndn_warning, "WARNING: Unexpected event type %i received\n", ev[i].type );
313 for (i = 0; i < 2; i++) {
314 if ( i != 2 && fds [ i ] != -1 ) {
322 // this should really register the keystate and time, and then another func to monitor
323 // time-passage and check the registered list and act on the event..
324 void dispatch_key ( int keycode, int val ) {
327 while ( i < g_evmap_max ) {
329 if ( ( g_evmap [ i ].key_p ) &&
330 ( g_evmap [ i ].keycode == keycode ) )
333 if ( g_evmap [ i ].script ) {
336 if ( ( x = fork() ) < 0 ) {
337 pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
342 pnd_log ( pndn_debug, "REM: Invoking %s\n", g_evmap [ i ].script );
343 execl ( g_evmap [ i ].script, g_evmap [ i ].script, (char*)NULL );
344 pnd_log ( pndn_error, "ERROR: Couldn't exec(%s)\n", g_evmap [ i ].script );