Setup inotify to watch for modifications of buffer source files

This commit is contained in:
Dimitri Lozeve 2021-02-26 18:39:07 +01:00
parent ea6dfed522
commit 1d6319e928
2 changed files with 26 additions and 1 deletions

View file

@ -1,6 +1,7 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include "log.h"
#include "renderer.h"
@ -17,10 +18,32 @@ int main(int argc, char *argv[]) {
struct renderer_state state = {0};
/* Create inotify instance */
state.inotify_fd = inotify_init();
if (state.inotify_fd == -1) {
log_error("Cannot initialize inotify");
perror("inotify_init");
return EXIT_FAILURE;
}
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 (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);
}

View file

@ -9,6 +9,7 @@
struct shader_state {
unsigned int program; /**< Shader program ID. */
const char *filename; /**< Shader file name. */
int wd; /**< inotify watch descriptor. */
};
/**
@ -19,9 +20,10 @@ struct renderer_state {
GLFWwindow *window; /**< GLFW window where the shaders are rendered. */
struct shader_state screen_shader; /**< Shader for the main screen. */
struct shader_state buffer_shader; /**< Shader for the framebuffer. */
int inotify_fd; /**< inotify file descriptor. */
size_t frame_count; /**< Frame count since the start of the render loop. */
size_t prev_frame_count; /**< Frame count at the last log. */
double time; /**< Time in seconds since the beginning of the render loop. */
double time; /**< Time in seconds since the start of the render loop. */
double prev_time; /**< Time in seconds at the last log. */
};