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:
parent
2b683cb169
commit
5f5fe249f8
2 changed files with 45 additions and 27 deletions
50
src/main.c
50
src/main.c
|
@ -1,8 +1,8 @@
|
|||
#include <GL/glew.h>
|
||||
#include <GLFW/glfw3.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdlib.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#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 */
|
||||
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.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);
|
||||
|
|
|
@ -194,15 +194,21 @@ void capture_screenshot() {
|
|||
*/
|
||||
void process_input(struct renderer_state *state) {
|
||||
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};
|
||||
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");
|
||||
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) {
|
||||
log_debug("Quitting");
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue