diff --git a/src/main.c b/src/main.c index a652611..1fb9985 100644 --- a/src/main.c +++ b/src/main.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "log.h" #include "renderer.h" @@ -25,6 +26,8 @@ int main(int argc, char *argv[]) { perror("inotify_init"); return EXIT_FAILURE; } + /* 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 = diff --git a/src/renderer.c b/src/renderer.c index fb7b7d9..a96994d 100644 --- a/src/renderer.c +++ b/src/renderer.c @@ -1,14 +1,18 @@ #include #include #include +#include #include +#include #include +#include #include "log.h" #include "renderer.h" #include "shaders.h" #define UNUSED(a) (void)a +#define BUF_LEN (10 * (sizeof(struct inotify_event) + 1)) /** * @brief Initialize GLFW and OpenGL, and create a window. @@ -189,10 +193,22 @@ void capture_screenshot() { * @param state The current state of the renderer. */ 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; + } + if (glfwGetKey(state->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { log_debug("Quitting"); glfwSetWindowShouldClose(state->window, true); - } else if (glfwGetKey(state->window, GLFW_KEY_R) == GLFW_PRESS) { + } else if (should_reload || + glfwGetKey(state->window, GLFW_KEY_R) == GLFW_PRESS) { // reinitialize time and frame count state->frame_count = 0; state->prev_frame_count = 0;