From 5f5fe249f8cfbf2c1977ebfe4e55839eb9159507 Mon Sep 17 00:00:00 2001 From: Dimitri Lozeve Date: Fri, 26 Feb 2021 20:16:17 +0100 Subject: [PATCH] Make inotify optional If it fails, display a warning and allow manual reloading only. Also add more logging for inotify-related stuff. --- src/main.c | 50 +++++++++++++++++++++++++++++++------------------- src/renderer.c | 22 ++++++++++++++-------- 2 files changed, 45 insertions(+), 27 deletions(-) diff --git a/src/main.c b/src/main.c index 1fb9985..d49d518 100644 --- a/src/main.c +++ b/src/main.c @@ -1,8 +1,8 @@ #include #include +#include #include #include -#include #include "log.h" #include "renderer.h" @@ -22,32 +22,44 @@ int main(int argc, char *argv[]) { /* Create inotify instance */ state.inotify_fd = inotify_init(); if (state.inotify_fd == -1) { - log_error("Cannot initialize inotify"); + log_warn("[inotify] Cannot initialize inotify"); perror("inotify_init"); - return EXIT_FAILURE; + } else { + /* Set the inotify file descriptor to be non-blocking */ + if (!fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK)) { + log_debug("[inotify] Initialized successfully"); + } } - /* Set the inotify file descriptor to be non-blocking */ - fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK); state.screen_shader.filename = argv[1]; - state.screen_shader.wd = - inotify_add_watch(state.inotify_fd, state.screen_shader.filename, IN_MODIFY); - if (state.screen_shader.wd == -1) { - log_error("Cannot watch file %s", state.screen_shader.filename); - perror("inotify_add_watch"); - return EXIT_FAILURE; - } log_debug("Screen shader file: %s", state.screen_shader.filename); + + if (state.inotify_fd != -1) { + state.screen_shader.wd = inotify_add_watch( + state.inotify_fd, state.screen_shader.filename, IN_MODIFY); + if (state.screen_shader.wd == -1) { + log_warn("[inotify] Cannot watch file %s", state.screen_shader.filename); + perror("inotify_add_watch"); + } else { + log_debug("[inotify] Watching file %s", state.screen_shader.filename); + } + } + if (argc >= 3) { state.buffer_shader.filename = argv[2]; - state.buffer_shader.wd = - inotify_add_watch(state.inotify_fd, state.buffer_shader.filename, IN_MODIFY); - if (state.buffer_shader.wd == -1) { - log_error("Cannot watch file %s", state.buffer_shader.filename); - perror("inotify_add_watch"); - return EXIT_FAILURE; - } log_debug("Buffer shader file: %s", state.buffer_shader.filename); + + if (state.inotify_fd != -1) { + state.buffer_shader.wd = inotify_add_watch( + state.inotify_fd, state.buffer_shader.filename, IN_MODIFY); + if (state.buffer_shader.wd == -1) { + log_warn("[inotify] Cannot watch file %s", + state.buffer_shader.filename); + perror("inotify_add_watch"); + } else { + log_debug("[inotify] Watching file %s", state.buffer_shader.filename); + } + } } state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT); diff --git a/src/renderer.c b/src/renderer.c index a96994d..439569c 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -194,14 +194,20 @@ void capture_screenshot() { */ void process_input(struct renderer_state *state) { bool should_reload = false; - char buf[BUF_LEN] = {0}; - int num_read = read(state->inotify_fd, buf, BUF_LEN); - if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { - // No event, do nothing - } else if (num_read <= 0) { - log_error("Could not read inotify state"); - } else { - should_reload = true; + + // Skip inotify checking if it's not available + if (state->inotify_fd != -1 && + (state->screen_shader.wd != -1 || state->buffer_shader.wd != -1)) { + char buf[BUF_LEN] = {0}; + int num_read = read(state->inotify_fd, buf, BUF_LEN); + if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { + // No event, do nothing + } else if (num_read <= 0) { + log_error("[inotify] Could not read inotify state"); + } else { + log_debug("[inotify] File changed on disk, reloading shaders"); + should_reload = true; + } } if (glfwGetKey(state->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {