diff options
author | Marcel Holtmann <marcel@holtmann.org> | 2007-01-13 21:39:57 +0000 |
---|---|---|
committer | Marcel Holtmann <marcel@holtmann.org> | 2007-01-13 21:39:57 +0000 |
commit | 84fa9983df320c0bd5c400735474b5c0e98e5efd (patch) | |
tree | d513c67ee2b10c91fab4c448350082cae92f30fa | |
parent | 603a99217dc455370b09967e7628412c13b6663b (diff) |
Add callback for notifications
-rw-r--r-- | common/notify-dummy.c | 2 | ||||
-rw-r--r-- | common/notify-inotify.c | 16 | ||||
-rw-r--r-- | common/notify.h | 10 |
3 files changed, 21 insertions, 7 deletions
diff --git a/common/notify-dummy.c b/common/notify-dummy.c index 3443a467..33f7909e 100644 --- a/common/notify-dummy.c +++ b/common/notify-dummy.c @@ -35,7 +35,7 @@ void notify_close(void) { } -void notify_add(const char *pathname) +void notify_add(const char *pathname, notify_func func, void *user_data) { } diff --git a/common/notify-inotify.c b/common/notify-inotify.c index 2b14b744..53ab7d55 100644 --- a/common/notify-inotify.c +++ b/common/notify-inotify.c @@ -41,6 +41,8 @@ static int wd = -1; static char *name = NULL; +static notify_func callback = NULL; + static gboolean io_event(GIOChannel *chan, GIOCondition cond, gpointer data) { unsigned char buf[129]; @@ -56,17 +58,17 @@ static gboolean io_event(GIOChannel *chan, GIOCondition cond, gpointer data) if (len < sizeof(struct inotify_event)) return TRUE; - if (evt->wd != wd) + if (evt->wd != wd || !callback) return TRUE; if (evt->mask & (IN_CREATE | IN_MOVED_TO)) - debug("File %s/%s created", name, evt->name); + callback(NOTIFY_CREATE, evt->name, NULL); if (evt->mask & (IN_DELETE | IN_MOVED_FROM)) - debug("File %s/%s deleted", name, evt->name); + callback(NOTIFY_DELETE, evt->name, NULL); if (evt->mask & IN_MODIFY) - debug("File %s/%s modified", name, evt->name); + callback(NOTIFY_MODIFY, evt->name, NULL); return TRUE; } @@ -115,7 +117,7 @@ void notify_close(void) } } -void notify_add(const char *pathname) +void notify_add(const char *pathname, notify_func func, void *user_data) { if (fd == -1 || wd != -1) return; @@ -131,6 +133,8 @@ void notify_add(const char *pathname) IN_ONLYDIR | IN_CREATE | IN_DELETE | IN_MODIFY | IN_MOVE); if (wd < 0) error("Creation of watch for %s failed", pathname); + + callback = func; } void notify_remove(const char *pathname) @@ -140,4 +144,6 @@ void notify_remove(const char *pathname) inotify_rm_watch(fd, wd); wd = -1; + + callback = NULL; } diff --git a/common/notify.h b/common/notify.h index b6b40034..3a58c508 100644 --- a/common/notify.h +++ b/common/notify.h @@ -21,7 +21,15 @@ * */ +typedef void (*notify_func)(int action, const char *name, void *user_data); + +enum { + NOTIFY_CREATE, + NOTIFY_DELETE, + NOTIFY_MODIFY, +}; + void notify_init(void); void notify_close(void); -void notify_add(const char *pathname); +void notify_add(const char *pathname, notify_func func, void *user_data); void notify_remove(const char *pathname); |