/* Copyright (c) 2016, Matthias Schiffer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #include "common.h" #include #include #include #include #include #include #include #include #include #include #include #ifdef USE_INOTIFY # include # include # include #endif static int init_param0 = 0; static int init_param1 = 0; static int init_param2 = 0; static float init_time = 0; static float init_speed = 1.0; static bool paused = false; static float speed = 1.0; static unsigned previous_ticks = 0; #ifdef USE_INOTIFY static int inotify_fd = -1; static int inotify_watch = -1; static void reset_watch(void) { if (inotify_watch >= 0) { if (inotify_rm_watch(inotify_fd, inotify_watch)) { fprintf(stderr, "unable to remove watch: %s\n", strerror(errno)); exit(1); } } inotify_watch = inotify_add_watch(inotify_fd, filename, IN_CLOSE_WRITE); if (inotify_watch < 0) fprintf(stderr, "unable to watch '%s' for changes: %s\n", filename, strerror(errno)); } static void init_watch(void) { inotify_fd = inotify_init(); if (inotify_fd < 0) { fprintf(stderr, "unable to initialize inotify: %s\n", strerror(errno)); exit(1); } fcntl(inotify_fd, F_SETFL, fcntl(inotify_fd, F_GETFL)|O_NONBLOCK); reset_watch(); } static void check_reload(void) { char buf[sizeof(struct inotify_event) + NAME_MAX + 1]; if (read(inotify_fd, buf, sizeof(buf)) > 0) load(); } #else static void reset_watch(void) {} static void init_watch(void) {} static void check_reload(void) {} #endif static void update(void) { unsigned ticks = SDL_GetTicks(); if (!paused) current_time += speed * (ticks - previous_ticks); previous_ticks = ticks; } static void set_speed(float v) { speed = v; printf("speed: %f\n", v); } static void print_params(void) { printf("params: %i %i %i\n", (int)param0, (int)param1, (int)param2); } static void handle_input(const char *input) { for (; *input; input++) { switch (*input) { case 'l': reset_watch(); load(); break; case 'r': current_time = init_time; break; case 'R': param0 = init_param0; param1 = init_param1; param2 = init_param2; print_params(); break; case '+': set_speed(speed * 1.1); break; case '-': set_speed(speed / 1.1); break; case '=': if (speed < 0) set_speed(-init_speed); else set_speed(init_speed); break; case '<': if (speed > 0) set_speed(-speed); break; case '>': if (speed < 0) set_speed(-speed); break; case ' ': paused = !paused; } } } static void usage(void) { fprintf(stderr, "Usage: glslview [-w ] [-h ] [-t