Set the inotify fd to non-blocking and read file changes

This commit is contained in:
Dimitri Lozeve 2021-02-26 19:55:08 +01:00
parent 1d6319e928
commit 2b683cb169
2 changed files with 20 additions and 1 deletions

View file

@ -2,6 +2,7 @@
#include <GLFW/glfw3.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <fcntl.h>
#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 =

View file

@ -1,14 +1,18 @@
#include <FreeImage.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#include <errno.h>
#include <stdlib.h>
#include <sys/inotify.h>
#include <time.h>
#include <unistd.h>
#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;