Fix; introduced a core-dump that will occur when SD is not in place.
[pandora-libraries.git] / apps / pndevmapperd.c
1
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
4  */
5
6 // woot for writing code while sick.
7
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
17 #include <linux/input.h> // for keys
18 //#include "../../kernel-rip/input.h" // for keys
19
20 #include "pnd_conf.h"
21 #include "pnd_container.h"
22 #include "pnd_apps.h"
23 #include "pnd_discovery.h"
24 #include "pnd_locate.h"
25 #include "pnd_pndfiles.h"
26 #include "pnd_pxml.h"
27 #include "pnd_logger.h"
28
29 // daemon and logging
30 //
31 unsigned char g_daemon_mode = 0;
32
33 typedef enum {
34   pndn_debug = 0,
35   pndn_rem,          // will set default log level to here, so 'debug' is omitted
36   pndn_warning,
37   pndn_error,
38   pndn_none
39 } pndnotify_loglevels_e;
40
41 // event-to-sh mapping
42 //
43 typedef struct {
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
48 } evmap_t;
49
50 #define MAXEVENTS 255
51 evmap_t g_evmap [ MAXEVENTS ];
52 unsigned int g_evmap_max = 0;
53
54 // key definition
55 //
56 typedef struct {
57   int keycode;
58   char *keyname;
59 } keycode_t;
60
61 keycode_t keycodes[] = {
62   { KEY_A, "a" },
63   { KEY_MENU, "pandora" },
64   { -1, NULL }
65 };
66
67 /* get to it
68  */
69 void dispatch_key ( int keycode, int val );
70
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" );
75   return;
76 }
77
78 int main ( int argc, char *argv[] ) {
79   int i;
80
81   for ( i = 1; i < argc; i++ ) {
82
83     if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
84       //printf ( "Going daemon mode. Silent running.\n" );
85       g_daemon_mode = 1;
86     } else {
87       usage ( argv );
88       exit ( 0 );
89     }
90
91   } // for
92
93   /* enable logging?
94    */
95   if ( g_daemon_mode ) {
96     // nada
97   } else {
98     pnd_log_set_filter ( pndn_rem );
99     pnd_log_set_pretext ( "pndevmapperd" );
100     pnd_log_to_stdout();
101     pnd_log ( pndn_rem, "log level starting as %u", pnd_log_get_filter() );
102   }
103
104   // basic daemon set up
105   if ( g_daemon_mode ) {
106
107     // set a CWD somewhere else
108     chdir ( "/tmp" );
109
110     // detach from terminal
111     if ( ( i = fork() ) < 0 ) {
112       pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
113       exit ( i );
114     }
115     if ( i ) {
116       exit ( 0 ); // exit parent
117     }
118     setsid();
119
120     // umask
121     umask ( 022 ); // emitted files can be rwxr-xr-x
122     
123   } // set up daemon
124
125   /* inhale config or die trying
126    */
127   char *configpath;
128
129   // attempt to fetch a sensible default searchpath for configs
130   configpath = pnd_conf_query_searchpath();
131
132   // attempt to fetch the apps config. since it finds us the runscript
133   pnd_conf_handle evmaph;
134
135   evmaph = pnd_conf_fetch_by_id ( pnd_conf_evmap, configpath );
136
137   if ( ! evmaph ) {
138     // couldn't locate conf, just bail
139     pnd_log ( pndn_error, "ERROR: Couldn't locate conf file\n" );
140     exit ( -1 );
141   }
142
143   /* iterate across conf, stocking the event map
144    */
145   void *n = pnd_box_get_head ( evmaph );
146
147   while ( n ) {
148     char *k = pnd_box_get_key ( n );
149     //printf ( "key %s\n", k );
150
151     if ( strncmp ( k, "keys.", 5 ) == 0 ) {
152       k += 5;
153
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 ) {
158           break;
159         }
160         p++;
161       }
162
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 );
168         g_evmap_max++;
169       } else {
170         pnd_log ( pndn_warning, "WARNING! Key '%s' is not handled by pndevmapperd yet! Skipping.", k );
171       }
172
173     } else if ( strncmp ( k, "events.", 7 ) == 0 ) {
174       k += 7;
175
176     } else {
177       // uhhh
178       pnd_log ( pndn_warning, "Unknown config key '%s'; skipping.\n", k );
179     }
180
181     n = pnd_box_get_next ( n );
182   } // while
183
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() );
187   }
188
189   /* do we have anything to do?
190    */
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" );
194     exit ( -1 );
195   } // spin
196
197   /* actually try to do something useful
198    */
199
200   // stolen in part from notaz :)
201
202   // try to locate the appropriate devices
203   int id;
204   int fds [ 5 ] = { -1, -1, -1, -1, -1 }; // 0 = keypad, 1 = gpio keys
205   int imaxfd = 0;
206
207   for ( id = 0; ; id++ ) {
208     char fname[64];
209     char name[256] = { 0, };
210     int fd;
211
212     snprintf ( fname, sizeof ( fname ), "/dev/input/event%i", id );
213     fd = open ( fname, O_RDONLY );
214
215     if ( fd == -1 ) {
216       break;
217     }
218
219     ioctl (fd, EVIOCGNAME(sizeof(name)), name );
220
221     if ( strcmp ( name, "omap_twl4030keypad" ) == 0 ) {
222       fds [ 0 ] = fd;
223     } else if (strcmp(name, "gpio-keys") == 0) {
224       fds [ 1 ] = fd;
225     } else {
226       close ( fd );
227       continue;
228     }
229
230     if (imaxfd < fd) imaxfd = fd;
231   } // for
232
233   if ( fds [ 0 ] == -1 ) {
234     pnd_log ( pndn_warning, "WARNING! Couldn't find keypad device\n" );
235   }
236
237   if ( fds [ 1 ] == -1 ) {
238     pnd_log ( pndn_warning, "WARNING! couldn't find button device\n" );
239   }
240
241   if ( fds [ 0 ] == -1 && fds [ 1 ] == -1 ) {
242     pnd_log ( pndn_error, "ERROR! Couldn't find either device; exiting!\n" );
243     exit ( -2 );
244   }
245
246   /* loop forever, watching for events
247    */
248
249   while ( 1 ) {
250     struct input_event ev[64];
251
252     int fd = -1, rd, ret;
253     fd_set fdset;
254
255     FD_ZERO ( &fdset );
256
257     for (i = 0; i < 2; i++) {
258       if ( fds [ i ] != -1 ) {
259         FD_SET( fds [ i ], &fdset );
260       }
261     }
262
263     ret = select ( imaxfd + 1, &fdset, NULL, NULL, NULL );
264
265     if ( ret == -1 ) {
266       pnd_log ( pndn_error, "ERROR! select(2) failed with: %s\n", strerror ( errno ) );
267       break;
268     }
269
270     for ( i = 0; i < 2; i++ ) {
271       if ( fds [ i ] != -1 && FD_ISSET ( fds [ i ], &fdset ) ) {
272         fd = fds [ i ];
273       }
274     }
275
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 ) );
280       break;
281     }
282
283     for (i = 0; i < rd / sizeof(struct input_event); i++ ) {
284
285       if ( ev[i].type == EV_SYN ) {
286         continue;
287       } else if ( ev[i].type == EV_KEY ) {
288
289         keycode_t *p = keycodes;
290         while ( p -> keycode != -1 ) {
291           if ( p -> keycode == ev [ i ].code ) {
292             break;
293           }
294           p++;
295         }
296
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 );
300         } else {
301           pnd_log ( pndn_warning, "Unknown Key Event: keycode %d value %d\n",  ev [ i ].code, ev [ i ].value );
302         }
303
304       } else {
305         pnd_log ( pndn_warning, "WARNING: Unexpected event type %i received\n", ev[i].type );
306         continue;
307       }
308
309     } // for
310
311   } // while
312
313   for (i = 0; i < 2; i++) {
314     if ( i != 2 && fds [ i ] != -1 ) {
315       close (fds [ i ] );
316     }
317   }
318
319   return ( 0 );
320 } // main
321
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 ) {
325   unsigned int i;
326
327   while ( i < g_evmap_max ) {
328
329     if ( ( g_evmap [ i ].key_p ) &&
330          ( g_evmap [ i ].keycode == keycode ) )
331     {
332
333       if ( g_evmap [ i ].script ) {
334         int x;
335
336         if ( ( x = fork() ) < 0 ) {
337           pnd_log ( pndn_error, "ERROR: Couldn't fork()\n" );
338           exit ( -3 );
339         }
340
341         if ( x == 0 ) {
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 );
345           exit ( -4 );
346         }
347
348       }
349
350       return;
351     }
352
353   } // while
354
355   return;
356 }