Add option to toggle auto-reloading of shaders

This commit is contained in:
Dimitri Lozeve 2021-03-05 18:52:54 +01:00
parent 01c26d207a
commit a1307a0b2f

View file

@ -25,14 +25,16 @@ static struct argp_option options[] = {
{"verbose", 'v', 0, 0, "Produce verbose output", 0}, {"verbose", 'v', 0, 0, "Produce verbose output", 0},
{"silent", 's', 0, 0, "Don't produce any output", 0}, {"silent", 's', 0, 0, "Don't produce any output", 0},
{"quiet", 'q', 0, OPTION_ALIAS, 0, 0}, {"quiet", 'q', 0, OPTION_ALIAS, 0, 0},
{"auto-reload", 'r', 0, 0, "Automatically reload on save", 0},
{"buffer", 'b', "FILE", 0, "Source file of the buffer fragment shader", 0}, {"buffer", 'b', "FILE", 0, "Source file of the buffer fragment shader", 0},
{0}, {0},
}; };
struct arguments { struct arguments {
char *shader_file; char *shader_file;
int verbose; bool verbose;
int silent; bool silent;
bool autoreload;
char *buffer_file; char *buffer_file;
}; };
@ -47,6 +49,9 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) {
case 'q': case 'q':
arguments->silent = true; arguments->silent = true;
break; break;
case 'r':
arguments->autoreload = true;
break;
case 'b': case 'b':
arguments->buffer_file = arg; arguments->buffer_file = arg;
break; break;
@ -80,6 +85,7 @@ int main(int argc, char *argv[]) {
/* Default values */ /* Default values */
arguments.verbose = false; arguments.verbose = false;
arguments.silent = false; arguments.silent = false;
arguments.autoreload = false;
arguments.buffer_file = 0; arguments.buffer_file = 0;
argp_parse(&argp_parser, argc, argv, 0, 0, &arguments); argp_parse(&argp_parser, argc, argv, 0, 0, &arguments);
@ -94,16 +100,20 @@ int main(int argc, char *argv[]) {
struct renderer_state state = {0}; struct renderer_state state = {0};
/* Create inotify instance */ if (arguments.autoreload) {
state.inotify_fd = inotify_init(); /* Create inotify instance */
if (state.inotify_fd == -1) { state.inotify_fd = inotify_init();
log_warn("[inotify] Cannot initialize inotify"); if (state.inotify_fd == -1) {
perror("inotify_init"); log_warn("[inotify] Cannot initialize inotify");
} else { perror("inotify_init");
/* Set the inotify file descriptor to be non-blocking */ } else {
if (!fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK)) { /* Set the inotify file descriptor to be non-blocking */
log_debug("[inotify] Initialized successfully"); if (!fcntl(state.inotify_fd, F_SETFL, O_NONBLOCK)) {
log_debug("[inotify] Initialized successfully");
}
} }
} else {
state.inotify_fd = -1;
} }
state.screen_shader.filename = arguments.shader_file; state.screen_shader.filename = arguments.shader_file;