From 7328b3f891102f87312a21313906bd427eca252c Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Mon, 11 Nov 2019 20:46:06 +0000 Subject: [PATCH] file_watch: only use inotify on linux Fixes build on *BSD platforms. Fixes #262, Closes #261 Signed-off-by: Yuxuan Shui --- src/file_watch.c | 25 ++++++++++++++++++++----- src/meson.build | 8 ++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/file_watch.c b/src/file_watch.c index f72659b..0bef173 100644 --- a/src/file_watch.c +++ b/src/file_watch.c @@ -1,6 +1,8 @@ #include #include +#ifdef HAS_INOTIFY #include +#endif #include #include @@ -26,9 +28,11 @@ struct file_watch_registry { static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) { auto fwr = (struct file_watch_registry *)w; - struct inotify_event inotify_event; while (true) { + int wd = -1; +#ifdef HAS_INOTIFY + struct inotify_event inotify_event; auto ret = read(w->fd, &inotify_event, sizeof(struct inotify_event)); if (ret < 0) { if (errno != EAGAIN) { @@ -37,9 +41,11 @@ static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) { } break; } + wd = inotify_event.wd; +#endif struct watched_file *wf = NULL; - HASH_FIND_INT(fwr->reg, &inotify_event.wd, wf); + HASH_FIND_INT(fwr->reg, &wd, wf); if (!wf) { log_warn("Got notification for a file I didn't watch."); continue; @@ -50,11 +56,17 @@ static void file_watch_ev_cb(EV_P_ struct ev_io *w, int revent attr_unused) { void *file_watch_init(EV_P) { log_debug("Starting watching for file changes"); - int fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); + int fd = -1; +#ifdef HAS_INOTIFY + fd = inotify_init1(IN_NONBLOCK | IN_CLOEXEC); if (fd < 0) { log_error("inotify_init1 failed: %s", strerror(errno)); return NULL; } +#else + log_info("No file watching support found on the host system."); + return NULL; +#endif auto fwr = ccalloc(1, struct file_watch_registry); ev_io_init(&fwr->w, file_watch_ev_cb, fd, EV_READ); ev_io_start(EV_A_ & fwr->w); @@ -80,12 +92,15 @@ void file_watch_destroy(EV_P_ void *_fwr) { bool file_watch_add(void *_fwr, const char *filename, file_watch_cb_t cb, void *ud) { log_debug("Adding \"%s\" to watched files", filename); auto fwr = (struct file_watch_registry *)_fwr; - int wd = inotify_add_watch(fwr->w.fd, filename, - IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF); + int wd = -1; +#ifdef HAS_INOTIFY + wd = inotify_add_watch(fwr->w.fd, filename, + IN_CLOSE_WRITE | IN_MOVE_SELF | IN_DELETE_SELF); if (wd < 0) { log_error("Failed to watch file \"%s\": %s", filename, strerror(errno)); return false; } +#endif auto w = ccalloc(1, struct watched_file); w->wd = wd; diff --git a/src/meson.build b/src/meson.build index e2e0e05..05626f5 100644 --- a/src/meson.build +++ b/src/meson.build @@ -78,6 +78,14 @@ if get_option('unittest') cflags += ['-DUNIT_TEST'] endif +host_system = host_machine.system() +if host_system == 'linux' + cflags += ['-DHAS_INOTIFY'] +elif host_system == 'freebsd' or host_system == 'netbsd' or + host_system == 'dragonfly' or host_system == 'openbsd' + cflags += ['-DHAS_KQUEUE'] +endif + subdir('backend') picom = executable('picom', srcs, c_args: cflags,