Make inotify optional

If it fails, display a warning and allow manual reloading only. Also
add more logging for inotify-related stuff.
This commit is contained in:
Dimitri Lozeve 2021-02-26 20:16:17 +01:00
parent 2b683cb169
commit 5f5fe249f8
2 changed files with 45 additions and 27 deletions

View file

@ -1,8 +1,8 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <sys/inotify.h> #include <sys/inotify.h>
#include <fcntl.h>
#include "log.h" #include "log.h"
#include "renderer.h" #include "renderer.h"
@ -22,32 +22,44 @@ int main(int argc, char *argv[]) {
/* Create inotify instance */ /* Create inotify instance */
state.inotify_fd = inotify_init(); state.inotify_fd = inotify_init();
if (state.inotify_fd == -1) { if (state.inotify_fd == -1) {
log_error("Cannot initialize inotify"); log_warn("[inotify] Cannot initialize inotify");
perror("inotify_init"); perror("inotify_init");
return EXIT_FAILURE; } else {
}
/* Set the inotify file descriptor to be non-blocking */ /* Set the inotify file descriptor to be non-blocking */
fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK); if (!fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK)) {
log_debug("[inotify] Initialized successfully");
}
}
state.screen_shader.filename = argv[1]; 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); 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) { if (argc >= 3) {
state.buffer_shader.filename = argv[2]; 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); 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); state.window = initialize_window(WINDOW_WIDTH, WINDOW_HEIGHT);

View file

@ -194,15 +194,21 @@ void capture_screenshot() {
*/ */
void process_input(struct renderer_state *state) { void process_input(struct renderer_state *state) {
bool should_reload = false; bool should_reload = false;
// 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}; char buf[BUF_LEN] = {0};
int num_read = read(state->inotify_fd, buf, BUF_LEN); int num_read = read(state->inotify_fd, buf, BUF_LEN);
if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) { if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
// No event, do nothing // No event, do nothing
} else if (num_read <= 0) { } else if (num_read <= 0) {
log_error("Could not read inotify state"); log_error("[inotify] Could not read inotify state");
} else { } else {
log_debug("[inotify] File changed on disk, reloading shaders");
should_reload = true; should_reload = true;
} }
}
if (glfwGetKey(state->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) { if (glfwGetKey(state->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
log_debug("Quitting"); log_debug("Quitting");