2 /* pndnotifyd - a daemon whose job is to monitor searchpaths for app changes (appearing, disappearing, changing).
3 * If a change is found, the discovery code is invoked and apps registered for the launchers to see
7 // TODO: for daemon mode, need to detach from the terminal
8 // TODO: Catch HUP and reparse config
10 #include <stdio.h> // for stdio
11 #include <unistd.h> // for exit()
12 #include <stdlib.h> // for exit()
14 #include <time.h> // for time()
15 #include <ctype.h> // for isdigit()
18 #include "pnd_container.h"
20 #include "pnd_notify.h"
21 #include "../lib/pnd_pathiter.h"
23 static unsigned char g_daemon_mode = 0;
25 int main ( int argc, char *argv[] ) {
32 unsigned int interval_secs = 60;
34 /* iterate across args
36 for ( i = 1; i < argc; i++ ) {
38 if ( argv [ i ][ 0 ] == '-' && argv [ i ][ 1 ] == 'd' ) {
39 //printf ( "Going daemon mode. Silent running.\n" );
41 } else if ( isdigit ( argv [ i ][ 0 ] ) ) {
42 interval_secs = atoi ( argv [ i ] );
44 printf ( "%s [-d] [##]\n", argv [ 0 ] );
45 printf ( "-d\tDaemon mode; detach from terminal, chdir to /tmp, suppress output. Optional.\n" );
46 printf ( "##\tA numeric value is interpreted as number of seconds between checking for filesystem changes. Default %u.\n",
53 if ( ! g_daemon_mode ) {
54 printf ( "Interval between checks is %u seconds\n", interval_secs );
60 // attempt to fetch a sensible default searchpath for configs
61 configpath = pnd_conf_query_searchpath();
63 // attempt to fetch the apps config to pick up a searchpath
66 apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
69 appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
72 appspath = PND_APPS_SEARCHPATH;
76 // couldn't find a useful app search path so use the default
77 appspath = PND_APPS_SEARCHPATH;
80 if ( ! g_daemon_mode ) {
81 printf ( "Apps searchpath is '%s'\n", appspath );
86 searchpath = appspath;
88 nh = pnd_notify_init();
91 if ( ! g_daemon_mode ) {
92 printf ( "INOTIFY failed to init.\n" );
97 if ( ! g_daemon_mode ) {
98 printf ( "INOTIFY is up.\n" );
104 pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
106 if ( ! g_daemon_mode ) {
107 printf ( "Watching path '%s' and its descendents.\n", buffer );
117 // need to rediscover?
118 if ( pnd_notify_rediscover_p ( nh ) ) {
120 // by this point, the watched directories have notified us that something of relevent
121 // has occurred; we should be clever, but we're not, so just re-brute force the
122 // discovery and spit out .desktop files..
123 if ( ! g_daemon_mode ) {
124 printf ( "Time to re-discover!\n" );
127 // lets not eat up all the CPU
128 // should use an alarm or select() or something
129 sleep ( interval_secs );
131 } // need to rediscover?
137 pnd_notify_shutdown ( nh );