Mostly changes to pndnotify, to facilitate HUP to reload conf, and to add watches...
authorskeezix <skeezix@flotsam-vm.(none)>
Sat, 7 Mar 2009 04:59:14 +0000 (23:59 -0500)
committerskeezix <skeezix@flotsam-vm.(none)>
Sat, 7 Mar 2009 04:59:14 +0000 (23:59 -0500)
Added backlight control to pnd_device as well as clock-get (all untested)
Added a free to pnd_notify to avoid leakage

TODO.txt
apps/pndnotifyd.c
include/pnd_device.h
lib/pnd_device.c
lib/pnd_notify.c

index 079fb6f..8d9c843 100644 (file)
--- a/TODO.txt
+++ b/TODO.txt
@@ -1,5 +1,5 @@
 
-Some things to be done..
+Some things to be done.. (see also the main wiki 'todo' page)
 
 - UNICODE everywhere.. this will be a lot of work I'm sure. (replacing all the char* and related consumers)
 
index 6e44c2b..4dbbe7d 100644 (file)
@@ -7,7 +7,6 @@
 // TODO: Catch HUP and reparse config
 // TODO: Should perhaps direct all printf's through a vsprintf handler to avoid redundant "if ! g_daemon_mode"
 // TODO: During daemon mode, should perhaps syslog or log errors
-// TODO: Removing stale .desktop checks that .desktop was created by libpnd; see 'TBD' below
 
 #include <stdio.h>     // for stdio
 #include <unistd.h>    // for exit()
@@ -19,6 +18,7 @@
 #include <sys/types.h> // for umask
 #include <sys/stat.h>  // for umask
 #include <dirent.h>    // for opendir()
+#include <signal.h>    // for sigaction
 
 #include "pnd_conf.h"
 #include "pnd_container.h"
 #include "pnd_locate.h"
 #include "pnd_utility.h"
 
+// this piece of code was simpler once; but need to grow it a bit and in a rush
+// moving all these to globals rather than refactor the code a bit; tsk tsk..
+
+// op mode; emitting stdout or no?
 static unsigned char g_daemon_mode = 0;
 
+// like discotest
+char *configpath;
+char *appspath;
+char *overridespath;
+// daemon stuff
+char *searchpath = NULL;
+char *dotdesktoppath = NULL;
+// pnd runscript
+char *run_searchpath;
+char *run_script;
+char *pndrun;
+// notifier handle
+pnd_notify_handle nh = 0;
+
+// decl's
+void consume_configuration ( void );
+void setup_notifications ( void );
+void sighup_handler ( int n );
+
 int main ( int argc, char *argv[] ) {
-  pnd_notify_handle nh;
-  // like discotest
-  char *configpath;
-  char *appspath;
-  char *overridespath;
-  // daemon stuff
-  char *searchpath = NULL;
-  char *dotdesktoppath = NULL;
   // behaviour
   unsigned char scanonlaunch = 1;
-  unsigned int interval_secs = 20;
-  // pnd runscript
-  char *run_searchpath;
-  char *run_script;
-  char *pndrun;
+  unsigned int interval_secs = 10;
   // misc
   int i;
 
@@ -102,89 +113,7 @@ int main ( int argc, char *argv[] ) {
   /* parse configs
    */
 
-  // attempt to fetch a sensible default searchpath for configs
-  configpath = pnd_conf_query_searchpath();
-
-  // attempt to fetch the apps config to pick up a searchpath
-  pnd_conf_handle apph;
-
-  apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
-
-  if ( apph ) {
-    appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
-
-    if ( ! appspath ) {
-      appspath = PND_APPS_SEARCHPATH;
-    }
-
-    overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
-
-    if ( ! overridespath ) {
-      overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
-    }
-
-  } else {
-    // couldn't find a useful app search path so use the default
-    appspath = PND_APPS_SEARCHPATH;
-    overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
-  }
-
-  // attempt to figure out where to drop dotfiles
-  pnd_conf_handle desktoph;
-
-  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
-
-  if ( desktoph ) {
-    dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
-
-    if ( ! dotdesktoppath ) {
-      dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
-    }
-
-  } else {
-    dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
-  }
-
-  // try to locate a runscript
-
-  if ( apph ) {
-    run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
-    run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
-    pndrun = NULL;
-
-    if ( ! run_searchpath ) {
-      run_searchpath = PND_APPS_SEARCHPATH;
-      run_script = PND_PNDRUN_FILENAME;
-    }
-
-  } else {
-    run_searchpath = NULL;
-    run_script = NULL;
-    pndrun = PND_PNDRUN_DEFAULT;
-  }
-
-  if ( ! pndrun ) {
-    pndrun = pnd_locate_filename ( run_searchpath, run_script );
-
-    if ( ! pndrun ) {
-      pndrun = PND_PNDRUN_DEFAULT;
-    }
-
-  }
-
-  if ( ! g_daemon_mode ) {
-    if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
-    if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
-    if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
-  }
-
-  /* handle globbing or variable substitution
-   */
-  dotdesktoppath = pnd_expand_tilde ( strdup ( dotdesktoppath ) );
-
-  /* validate paths
-   */
-  mkdir ( dotdesktoppath, 0777 );
+  consume_configuration();
 
   /* startup
    */
@@ -195,34 +124,21 @@ int main ( int argc, char *argv[] ) {
     printf ( ".desktop files emit to '%s'\n", dotdesktoppath );
   }
 
-  /* set up notifies
+  /* set up signal handler
    */
-  searchpath = appspath;
+  sigset_t ss;
+  sigemptyset ( &ss );
 
-  nh = pnd_notify_init();
+  struct sigaction siggy;
+  siggy.sa_handler = sighup_handler;
+  siggy.sa_mask = ss; /* implicitly blocks the origin signal */
+  siggy.sa_flags = 0; /* don't need anything */
 
-  if ( ! nh ) {
-    if ( ! g_daemon_mode ) {
-      printf ( "INOTIFY failed to init.\n" );
-    }
-    exit ( -1 );
-  }
-
-  if ( ! g_daemon_mode ) {
-    printf ( "INOTIFY is up.\n" );
-  }
-
-  SEARCHPATH_PRE
-  {
-
-    if ( ! g_daemon_mode ) {
-      printf ( "Watching path '%s' and its descendents.\n", buffer );
-    }
-
-    pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
+  sigaction ( SIGHUP, &siggy, NULL );
 
-  }
-  SEARCHPATH_POST
+  /* set up notifies
+   */
+  setup_notifications();
 
   /* daemon main loop
    */
@@ -245,7 +161,6 @@ int main ( int argc, char *argv[] ) {
       // discovery and spit out .desktop files..
       if ( ! g_daemon_mode ) {
        printf ( "Changes within watched paths .. performing re-discover!\n" );
-       printf ( "Path to emit .desktop files to: '%s'\n", dotdesktoppath );
       }
 
       // run the discovery
@@ -371,22 +286,28 @@ int main ( int argc, char *argv[] ) {
              }
            }
            if ( source_libpnd ) {
+#if 0
              if ( ! g_daemon_mode ) {
                printf ( "File '%s' appears to have been created by libpnd so candidate for delete: %u\n", buffer, source_libpnd );
              }
+#endif
            } else {
+#if 0
              if ( ! g_daemon_mode ) {
                printf ( "File '%s' appears NOT to have been created by libpnd, so leave it alone\n", buffer );
              }
+#endif
              continue; // skip deleting it
            }
 
            // file is 'new'?
            if ( stat ( buffer, &dirs ) == 0 ) {
              if ( dirs.st_mtime >= createtime ) {
+#if 0
                if ( ! g_daemon_mode ) {
                  printf ( "File '%s' seems 'new', so leave it alone.\n", buffer );
                }
+#endif
                continue; // skip deleting it
              }
            }
@@ -394,7 +315,7 @@ int main ( int argc, char *argv[] ) {
            // by this point, the .desktop file must be 'old' and created by pndnotifyd
            // previously, so can remove it
            if ( ! g_daemon_mode ) {
-             printf ( "File '%s' seems to not belong; removing it.\n", dirent -> d_name );
+             printf ( "File '%s' seems nolonger relevent; removing it.\n", dirent -> d_name );
            }
            unlink ( buffer );
 
@@ -405,6 +326,11 @@ int main ( int argc, char *argv[] ) {
 
       } // purge old .desktop files
 
+      // since its entirely likely new directories have been found (ie: SD with a directory structure was inserted)
+      // we should re-apply watches to catch all these new directories; ie: user might use on-device browser to
+      // drop in new applications, or use the shell to juggle them around, or any number of activities.
+      setup_notifications();
+
     } // need to rediscover?
 
     // lets not eat up all the CPU
@@ -419,3 +345,145 @@ int main ( int argc, char *argv[] ) {
 
   return ( 0 );
 }
+
+void consume_configuration ( void ) {
+
+  // attempt to fetch a sensible default searchpath for configs
+  configpath = pnd_conf_query_searchpath();
+
+  // attempt to fetch the apps config to pick up a searchpath
+  pnd_conf_handle apph;
+
+  apph = pnd_conf_fetch_by_id ( pnd_conf_apps, configpath );
+
+  if ( apph ) {
+    appspath = pnd_conf_get_as_char ( apph, PND_APPS_KEY );
+
+    if ( ! appspath ) {
+      appspath = PND_APPS_SEARCHPATH;
+    }
+
+    overridespath = pnd_conf_get_as_char ( apph, PND_PXML_OVERRIDE_KEY );
+
+    if ( ! overridespath ) {
+      overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
+    }
+
+  } else {
+    // couldn't find a useful app search path so use the default
+    appspath = PND_APPS_SEARCHPATH;
+    overridespath = PND_PXML_OVERRIDE_SEARCHPATH;
+  }
+
+  // attempt to figure out where to drop dotfiles
+  pnd_conf_handle desktoph;
+
+  desktoph = pnd_conf_fetch_by_id ( pnd_conf_desktop, configpath );
+
+  if ( desktoph ) {
+    dotdesktoppath = pnd_conf_get_as_char ( desktoph, PND_DOTDESKTOP_KEY );
+
+    if ( ! dotdesktoppath ) {
+      dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
+    }
+
+  } else {
+    dotdesktoppath = PND_DOTDESKTOP_DEFAULT;
+  }
+
+  // try to locate a runscript
+
+  if ( apph ) {
+    run_searchpath = pnd_conf_get_as_char ( apph, PND_PNDRUN_SEARCHPATH_KEY );
+    run_script = pnd_conf_get_as_char ( apph, PND_PNDRUN_KEY );
+    pndrun = NULL;
+
+    if ( ! run_searchpath ) {
+      run_searchpath = PND_APPS_SEARCHPATH;
+      run_script = PND_PNDRUN_FILENAME;
+    }
+
+  } else {
+    run_searchpath = NULL;
+    run_script = NULL;
+    pndrun = PND_PNDRUN_DEFAULT;
+  }
+
+  if ( ! pndrun ) {
+    pndrun = pnd_locate_filename ( run_searchpath, run_script );
+
+    if ( ! pndrun ) {
+      pndrun = PND_PNDRUN_DEFAULT;
+    }
+
+  }
+
+  if ( ! g_daemon_mode ) {
+    if ( run_searchpath ) printf ( "Locating pnd run in %s\n", run_searchpath );
+    if ( run_script ) printf ( "Locating pnd runscript as %s\n", run_script );
+    if ( pndrun ) printf ( "Default pndrun is %s\n", pndrun );
+  }
+
+  /* handle globbing or variable substitution
+   */
+  dotdesktoppath = pnd_expand_tilde ( strdup ( dotdesktoppath ) );
+
+  /* validate paths
+   */
+  mkdir ( dotdesktoppath, 0777 );
+
+  // done
+  return;
+}
+
+void setup_notifications ( void ) {
+  searchpath = appspath;
+
+  // if this is first time through, we can just set it up; for subsequent times
+  // through, we need to close existing fd and re-open it, since we're too lame
+  // to store the list of watches and 'rm' them
+  if ( nh ) {
+    pnd_notify_shutdown ( nh );
+  }
+
+  // set up a new set of notifies
+  nh = pnd_notify_init();
+
+  if ( ! nh ) {
+    if ( ! g_daemon_mode ) {
+      printf ( "INOTIFY failed to init.\n" );
+    }
+    exit ( -1 );
+  }
+
+#if 0
+  if ( ! g_daemon_mode ) {
+    printf ( "INOTIFY is up.\n" );
+  }
+#endif
+
+  SEARCHPATH_PRE
+  {
+
+    if ( ! g_daemon_mode ) {
+      printf ( "Watching path '%s' and its descendents.\n", buffer );
+    }
+
+    pnd_notify_watch_path ( nh, buffer, PND_NOTIFY_RECURSE );
+
+  }
+  SEARCHPATH_POST
+
+  return;
+}
+
+void sighup_handler ( int n ) {
+
+  // reparse config files
+  consume_configuration();
+
+  // re set up the notifier watches
+  setup_notifications();
+
+  return;
+}
index 97a80ee..383a66a 100644 (file)
@@ -13,16 +13,26 @@ extern "C" {
 // places to look:
 // http://pandorawiki.org/Kernel_interface
 
+/* utility
+ */
+unsigned char pnd_device_open_write_close ( char *name, char *v );
+unsigned char pnd_device_open_read_close ( char *name, char *r_buffer, unsigned int buffer_len );
+
 /* overall clock speed
  * WARN: No boundaries are checked, so try to avoid setting clock to 2GHz :)
  * NOTE: get-clock() is not implemented yet.
  */
 #define PND_DEVICE_PROC_CLOCK "/proc/pandora/cpu_mhz_max"
 unsigned char pnd_device_set_clock ( unsigned int c ); // returns >0 on success
-unsigned int pnd_device_get_clock ( void ); // not implemented, returns 0
+unsigned int pnd_device_get_clock ( void );
 
 // LCD to set on/off
 
+// Backlight control
+#define PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS "/sys/class/backlight/gpio-backlight/brightness"
+unsigned char pnd_device_set_backlight ( unsigned int v ); // value to set; 0 is off
+unsigned int pnd_device_get_backlight ( void );
+
 // set one or more LEDs on
 
 // suspend/hibernate/etc
index dc2fecc..f853ffc 100644 (file)
 
 #include "pnd_device.h"
 
-unsigned char pnd_device_set_clock ( unsigned int c ) {
-  char buffer [ 100 ];
+unsigned char pnd_device_open_write_close ( char *name, char *v ) {
   int f;
 
-  sprint ( buffer, "%u", c );
-
-  if ( ( f = open ( PND_DEVICE_PROC_CLOCK, O_RDONLY ) ) < 0 ) {
+  if ( ( f = open ( PND_DEVICE_PROC_CLOCK, O_WRONLY /*O_RDONLY*/ ) ) < 0 ) {
     return ( 0 );
   }
 
@@ -29,6 +26,57 @@ unsigned char pnd_device_set_clock ( unsigned int c ) {
   return ( 1 );
 }
 
+unsigned char pnd_device_open_read_close ( char *name, char *r_buffer, unsigned int buffer_len ) {
+  FILE *f;
+
+  f = fopen ( name, "r" );
+
+  if ( ! f ) {
+    return ( 0 );
+  }
+
+  if ( ! fgets ( r_buffer, buffer_len, f ) ) {
+    fclose ( f );
+    return ( 0 );
+  }
+
+  fclose ( f );
+
+  return ( 1 );
+}
+
+unsigned char pnd_device_set_clock ( unsigned int c ) {
+  char buffer [ 100 ];
+
+  sprint ( buffer, "%u", c );
+
+  return ( pnd_device_open_write_close ( PND_DEVICE_PROC_CLOCK, buffer ) );
+}
+
+unsigned int pnd_device_get_clock ( void ) {
+  char buffer [ 100 ];
+
+  if ( pnd_device_open_read_close ( PND_DEVICE_PROC_CLOCK, buffer, 100 ) ) {
+    return ( atoi ( buffer ) );
+  }
+
+  return ( 0 );
+}
+
+unsigned char pnd_device_set_backlight ( unsigned int c ) {
+  char buffer [ 100 ];
+
+  sprint ( buffer, "%u", c );
+
+  return ( pnd_device_open_write_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer ) );
+}
+
 unsigned int pnd_device_get_clock ( void ) {
+  char buffer [ 100 ];
+
+  if ( pnd_device_open_read_close ( PND_DEVICE_SYS_BACKLIGHT_BRIGHTNESS, buffer, 100 ) ) {
+    return ( atoi ( buffer ) );
+  }
+
   return ( 0 );
 }
index b6668d6..c9d16fd 100644 (file)
@@ -53,6 +53,8 @@ void pnd_notify_shutdown ( pnd_notify_handle h ) {
 
   close ( p -> fd );
 
+  free ( p );
+
   return;
 }