Inotify support.
authorBrenden Matthews <brenden@rty.ca>
Tue, 19 May 2009 23:14:34 +0000 (17:14 -0600)
committerBrenden Matthews <brenden@rty.ca>
Tue, 19 May 2009 23:14:34 +0000 (17:14 -0600)
Added inotify support to reload the config when modified automatically
on systems that support it

ChangeLog
configure.ac.in
src/conky.c

index 14d0dda..13f8a6d 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2009-05-19
+       * Added inotify support to reload the config when modified automatically
+       on systems that support it
+
 2009-05-17
        * Added support for the Lua programming language
        * Added support for rending images by way of IMLIB2
index 731de67..a1215db 100644 (file)
@@ -612,6 +612,8 @@ AC_CHECK_HEADERS([sys/mount.h], [], [],
       #include <sys/param.h>
       #endif
       ])
+# check if we have inotify support
+AC_CHECK_HEADERS([sys/inotify.h])
 
 dnl
 dnl Some defines
index ed1f3c2..0051499 100644 (file)
@@ -42,6 +42,9 @@
 #endif
 #include <sys/time.h>
 #include <sys/param.h>
+#ifdef HAVE_SYS_INOTIFY_H
+#include <sys/inotify.h>
+#endif /* HAVE_SYS_INOTIFY_H */
 #ifdef X11
 #include "x11.h"
 #include <X11/Xutil.h>
@@ -6386,6 +6389,13 @@ static void main_loop(void)
 #ifdef SIGNAL_BLOCKING
        sigset_t newmask, oldmask;
 #endif
+#ifdef HAVE_SYS_INOTIFY_H
+       int inotify_fd = 0, inotify_config_wd = 0;
+#define INOTIFY_EVENT_SIZE  (sizeof(struct inotify_event))
+#define INOTIFY_BUF_LEN     (20 * (INOTIFY_EVENT_SIZE + 16))
+       char inotify_buff[INOTIFY_BUF_LEN];
+#endif /* HAVE_SYS_INOTIFY_H */
+
        double t;
 #ifdef X11
        Region region;
@@ -6726,9 +6736,51 @@ static void main_loop(void)
                                }
                                break;
                }
+#ifdef HAVE_SYS_INOTIFY_H
+               if (!inotify_fd) {
+                       printf("'%s'\n", current_config);
+                       inotify_fd = inotify_init();
+                       inotify_config_wd = inotify_add_watch(inotify_fd,
+                                       current_config,
+                                       IN_MODIFY);
+               } else if (inotify_fd && inotify_config_wd) {
+                       int len = 0, idx = 0;
+                       fd_set descriptors;
+                       struct timeval time_to_wait;
+
+                       FD_ZERO (&descriptors);
+                       FD_SET(inotify_fd, &descriptors);
+
+                       time_to_wait.tv_sec = time_to_wait.tv_usec = 0;
+
+                       select(inotify_fd + 1, &descriptors, NULL, NULL, &time_to_wait);
+                       if (FD_ISSET(inotify_fd, &descriptors)) {
+                               /* process inotify events */
+                               len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN);
+                               while (len > 0 && idx < len) {
+                                       struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx];
+                                       if (ev->wd == inotify_config_wd) {
+                                               /* current_config should be reloaded */
+                                               ERR("'%s' modified, reloading...", current_config);
+                                               reload_config();
+                                       }
+                                       idx += INOTIFY_EVENT_SIZE + ev->len;
+                               }
+                       }
+               }
+#endif /* HAVE_SYS_INOTIFY_H */
+
                g_signal_pending = 0;
        }
 
+#ifdef HAVE_SYS_INOTIFY_H
+               if (inotify_fd) {
+                       inotify_rm_watch(inotify_fd, inotify_config_wd);
+                       close(inotify_fd);
+                       inotify_fd = inotify_config_wd = 0;
+               }
+#endif /* HAVE_SYS_INOTIFY_H */
+
 #if defined(X11) && defined(HAVE_XDAMAGE)
        if (output_methods & TO_X) {
                XDamageDestroy(display, damage);