From: Yong Shen <yong.s...@linaro.org>

add a inotify watch on clock tree, only when clock tree is changed,
the clock info structure is rebuild and screen will be refreshed.

Signed-off-by: Yong Shen <yong.s...@linaro.org>
---
 clocks.c     |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 powerdebug.c |    6 +++++-
 powerdebug.h |    4 ++++
 3 files changed, 60 insertions(+), 1 deletions(-)

diff --git a/clocks.c b/clocks.c
index 9a76c19..4c21f4f 100644
--- a/clocks.c
+++ b/clocks.c
@@ -13,12 +13,17 @@
  *       - initial API and implementation
  
*******************************************************************************/
 
+#include <sys/inotify.h>
+#include <poll.h>
 #include "powerdebug.h"
 #include "clocks.h"
 
 static char clk_dir_path[PATH_MAX];
 static char clk_name[NAME_MAX];
 static int  bold[MAX_LINES];
+int inotify_fd;
+int inotify_wd;
+static struct pollfd fds;
 
 int init_clock_details(void)
 {
@@ -58,6 +63,19 @@ int init_clock_details(void)
                                " information at %s.\n", clk_dir_path);
                        exit(1);
                }
+       } else {
+               inotify_fd = inotify_init();
+               if ( inotify_fd < 0 ) {
+                       fprintf(stderr, "inotify_init error.\n" );
+                       exit(1);
+               }
+
+               inotify_wd = inotify_add_watch(inotify_fd, clk_dir_path, 
+                       IN_ALL_EVENTS );
+
+               fds.fd = inotify_fd;
+               fds.events = POLLIN;
+
        }
        strcpy(clk_name, "");
        return(0);
@@ -98,6 +116,39 @@ void find_parents_for_clock(char *clkname, int complete)
        dump_all_parents(clkname);
 }
 
+#define EVENT_SIZE  ( sizeof (struct inotify_event) )
+#define BUF_LEN     ( 1024 * ( EVENT_SIZE + 16 ) )
+
+int need_refresh(void)
+{
+       int length, i = 0;
+       char buffer[BUF_LEN];
+
+       if (inotify_fd <= 0) {
+               return 1;
+       }
+
+       poll(&fds, 1, 1);
+       if (fds.revents != POLLIN) {
+               return 0;
+       }
+
+       length = read(inotify_fd, buffer, BUF_LEN);
+
+       if (length < 0)
+               return 0;
+
+       while (i < length) {
+               struct inotify_event *event = (struct inotify_event *) 
&buffer[i];
+               if (event->mask & IN_ALL_EVENTS)
+                       return 1;
+
+               i += EVENT_SIZE + event->len;
+       }
+
+       return 0;
+}
+
 int read_and_print_clock_info(int verbose, int hrow, int selected)
 {
        print_one_clock(0, "Reading Clock Tree ...", 1, 1);
diff --git a/powerdebug.c b/powerdebug.c
index 3f4d60c..d142a04 100644
--- a/powerdebug.c
+++ b/powerdebug.c
@@ -14,6 +14,7 @@
  
*******************************************************************************/
 
 #include <getopt.h>
+#include <sys/inotify.h>
 #include "powerdebug.h"
 
 int dump;
@@ -199,7 +200,7 @@ int main(int argc, char **argv)
 
                                        if (enter_hit)
                                                command = CLOCK_SELECTED;
-                                       if (refreshwin)
+                                       if (refreshwin && need_refresh())
                                                command = REFRESH_WINDOW;
                                        hrow = read_and_print_clock_info(
                                                verbose,
@@ -313,5 +314,8 @@ int main(int argc, char **argv)
                                refreshwin = 0;
                }
        }
+       inotify_rm_watch(inotify_fd, inotify_wd);
+       close(inotify_fd);
+
        exit(0);
 }
diff --git a/powerdebug.h b/powerdebug.h
index 8a3b5bd..c0eb316 100644
--- a/powerdebug.h
+++ b/powerdebug.h
@@ -71,3 +71,7 @@ extern void show_header(void);
 extern void create_windows(void);
 extern void create_selectedwindow(void);
 extern void show_regulator_info(int verbose);
+
+extern int inotify_fd;
+extern int inotify_wd;
+extern int need_refresh(void);
-- 
1.7.1


_______________________________________________
linaro-dev mailing list
linaro-dev@lists.linaro.org
http://lists.linaro.org/mailman/listinfo/linaro-dev

Reply via email to