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 <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 */
|
||||||
|
if (!fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK)) {
|
||||||
|
log_debug("[inotify] Initialized successfully");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* 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.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);
|
||||||
|
|
|
@ -194,14 +194,20 @@ 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;
|
||||||
char buf[BUF_LEN] = {0};
|
|
||||||
int num_read = read(state->inotify_fd, buf, BUF_LEN);
|
// Skip inotify checking if it's not available
|
||||||
if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
if (state->inotify_fd != -1 &&
|
||||||
// No event, do nothing
|
(state->screen_shader.wd != -1 || state->buffer_shader.wd != -1)) {
|
||||||
} else if (num_read <= 0) {
|
char buf[BUF_LEN] = {0};
|
||||||
log_error("Could not read inotify state");
|
int num_read = read(state->inotify_fd, buf, BUF_LEN);
|
||||||
} else {
|
if (num_read == -1 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
|
||||||
should_reload = true;
|
// No event, do nothing
|
||||||
|
} else if (num_read <= 0) {
|
||||||
|
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) {
|
if (glfwGetKey(state->window, GLFW_KEY_ESCAPE) == GLFW_PRESS) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue