| @@ -21,35 +21,59 @@ |
| 21 | 21 | | #include <unistd.h> |
| 22 | 22 | | #include <sys/inotify.h> |
| 23 | 23 | | #include <libgen.h> |
| 24 | | − | #include <time.h> |
| 25 | 24 | | |
| 26 | 25 | | #include "util.h" |
| 27 | 26 | | #include "autoreload.h" |
| 28 | 27 | | |
| 29 | | − | const struct timespec ten_ms = {0, 10000000}; |
| 30 | | − | |
| 31 | | − | void arl_cleanup(void) |
| 28 | + | CLEANUP void arl_cleanup(arl_t *arl) |
| 32 | 29 | | { |
| 33 | | − | if (autoreload.fd != -1 && autoreload.wd != -1) |
| 30 | + | if (arl->fd != -1 && arl->wd != -1) |
| 34 | 31 | | { |
| 35 | | − | if(inotify_rm_watch(autoreload.fd, autoreload.wd)) |
| 32 | + | if(inotify_rm_watch(arl->fd, arl->wd)) |
| 36 | 33 | | error(0, 0, "Failed to remove inotify watch."); |
| 37 | 34 | | } |
| 38 | 35 | | } |
| 39 | 36 | | |
| 40 | | − | void arl_handle(void) |
| 37 | + | static void arl_setup_dir(arl_t *arl, const char *filepath) |
| 38 | + | { |
| 39 | + | char *dntmp, *dn; |
| 40 | + | |
| 41 | + | if (arl->fd == -1) |
| 42 | + | { |
| 43 | + | error(0, 0, "Uninitialized, could not add inotify watch on directory."); |
| 44 | + | return; |
| 45 | + | } |
| 46 | + | |
| 47 | + | /* get dirname */ |
| 48 | + | dntmp = (char*) strdup(filepath); |
| 49 | + | dn = (char*) dirname(dntmp); |
| 50 | + | |
| 51 | + | /* this is not one-shot as other stuff may be created too |
| 52 | + | note: we won't handle deletion of the directory itself, |
| 53 | + | this is a design decision */ |
| 54 | + | arl->wd = inotify_add_watch(arl->fd, dn,IN_CREATE); |
| 55 | + | if (arl->wd == -1) |
| 56 | + | error(0, 0, "Failed to add inotify watch on directory '%s'.", dn); |
| 57 | + | else |
| 58 | + | arl->watching_dir = true; |
| 59 | + | |
| 60 | + | free(dntmp); |
| 61 | + | } |
| 62 | + | |
| 63 | + | bool arl_handle(arl_t *arl, const char *filepath) |
| 41 | 64 | | { |
| 65 | + | bool reload = false; |
| 42 | 66 | | ssize_t len; |
| 43 | 67 | | char buf[4096] __attribute__ ((aligned(__alignof__(struct inotify_event)))); |
| 44 | 68 | | const struct inotify_event *event; |
| 45 | 69 | | char *ptr; |
| 46 | 70 | | char *fntmp, *fn; |
| 47 | 71 | | |
| 48 | | − | len = read(autoreload.fd, buf, sizeof buf); |
| 72 | + | len = read(arl->fd, buf, sizeof buf); |
| 49 | 73 | | if (len == -1) |
| 50 | 74 | | { |
| 51 | 75 | | error(0, 0, "Failed to read inotify events."); |
| 52 | | − | return; |
| 76 | + | return false; |
| 53 | 77 | | } |
| 54 | 78 | | |
| 55 | 79 | | for (ptr = buf; ptr < buf + len; |
| @@ -61,17 +85,16 @@ void arl_handle(void) |
| 61 | 85 | | /* events from watching the file itself */ |
| 62 | 86 | | if (event->mask & IN_CLOSE_WRITE) |
| 63 | 87 | | { |
| 64 | | − | load_image(fileidx); |
| 65 | | − | redraw(); |
| 88 | + | reload = true; |
| 66 | 89 | | } |
| 67 | 90 | | |
| 68 | 91 | | if (event->mask & IN_DELETE_SELF) |
| 69 | | − | arl_setup_dir(); |
| 92 | + | arl_setup_dir(arl, filepath); |
| 70 | 93 | | |
| 71 | 94 | | /* events from watching the file's directory */ |
| 72 | 95 | | if (event->mask & IN_CREATE) |
| 73 | 96 | | { |
| 74 | | − | fntmp = strdup(files[fileidx].path); |
| 97 | + | fntmp = strdup(filepath); |
| 75 | 98 | | fn = basename(fntmp); |
| 76 | 99 | | |
| 77 | 100 | | if (0 == strcmp(event->name, fn)) |
| @@ -79,76 +102,49 @@ void arl_handle(void) |
| 79 | 102 | | /* this is the file we're looking for */ |
| 80 | 103 | | |
| 81 | 104 | | /* cleanup, this has not been one-shot */ |
| 82 | | − | if (autoreload.watching_dir) |
| 105 | + | if (arl->watching_dir) |
| 83 | 106 | | { |
| 84 | | − | if(inotify_rm_watch(autoreload.fd, autoreload.wd)) |
| 107 | + | if(inotify_rm_watch(arl->fd, arl->wd)) |
| 85 | 108 | | error(0, 0, "Failed to remove inotify watch."); |
| 86 | | − | autoreload.watching_dir = false; |
| 109 | + | arl->watching_dir = false; |
| 87 | 110 | | } |
| 88 | 111 | | |
| 89 | | − | /* when too fast, imlib2 can't load the image */ |
| 90 | | − | nanosleep(&ten_ms, NULL); |
| 91 | | − | load_image(fileidx); |
| 92 | | − | redraw(); |
| 112 | + | reload = true; |
| 93 | 113 | | } |
| 94 | 114 | | free(fntmp); |
| 95 | 115 | | } |
| 96 | 116 | | } |
| 117 | + | return reload; |
| 97 | 118 | | } |
| 98 | 119 | | |
| 99 | | − | void arl_init(void) |
| 120 | + | void arl_init(arl_t *arl) |
| 100 | 121 | | { |
| 101 | 122 | | /* this needs to be done only once */ |
| 102 | | − | autoreload.fd = inotify_init(); |
| 103 | | − | autoreload.watching_dir = false; |
| 104 | | − | if (autoreload.fd == -1) |
| 123 | + | arl->fd = inotify_init(); |
| 124 | + | arl->watching_dir = false; |
| 125 | + | if (arl->fd == -1) |
| 105 | 126 | | error(0, 0, "Could not initialize inotify."); |
| 106 | 127 | | } |
| 107 | 128 | | |
| 108 | | − | void arl_setup(void) |
| 129 | + | void arl_setup(arl_t *arl, const char *filepath) |
| 109 | 130 | | { |
| 110 | | − | if (autoreload.fd == -1) |
| 131 | + | if (arl->fd == -1) |
| 111 | 132 | | { |
| 112 | 133 | | error(0, 0, "Uninitialized, could not add inotify watch."); |
| 113 | 134 | | return; |
| 114 | 135 | | } |
| 115 | 136 | | |
| 116 | 137 | | /* may have switched from a deleted to another image */ |
| 117 | | − | if (autoreload.watching_dir) |
| 138 | + | if (arl->watching_dir) |
| 118 | 139 | | { |
| 119 | | − | if (inotify_rm_watch(autoreload.fd, autoreload.wd)) |
| 140 | + | if (inotify_rm_watch(arl->fd, arl->wd)) |
| 120 | 141 | | error(0, 0, "Failed to remove inotify watch."); |
| 121 | | − | autoreload.watching_dir = false; |
| 142 | + | arl->watching_dir = false; |
| 122 | 143 | | } |
| 123 | 144 | | |
| 124 | | − | autoreload.wd = inotify_add_watch(autoreload.fd, files[fileidx].path, |
| 145 | + | arl->wd = inotify_add_watch(arl->fd, filepath, |
| 125 | 146 | | IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF); |
| 126 | | − | if (autoreload.wd == -1) |
| 127 | | − | error(0, 0, "Failed to add inotify watch on file '%s'.", files[fileidx].path); |
| 147 | + | if (arl->wd == -1) |
| 148 | + | error(0, 0, "Failed to add inotify watch on file '%s'.", filepath); |
| 128 | 149 | | } |
| 129 | 150 | | |
| 130 | | − | void arl_setup_dir(void) |
| 131 | | − | { |
| 132 | | − | char *dntmp, *dn; |
| 133 | | − | |
| 134 | | − | if (autoreload.fd == -1) |
| 135 | | − | { |
| 136 | | − | error(0, 0, "Uninitialized, could not add inotify watch on directory."); |
| 137 | | − | return; |
| 138 | | − | } |
| 139 | | − | |
| 140 | | − | /* get dirname */ |
| 141 | | − | dntmp = (char*) strdup(files[fileidx].path); |
| 142 | | − | dn = (char*) dirname(dntmp); |
| 143 | | − | |
| 144 | | − | /* this is not one-shot as other stuff may be created too |
| 145 | | − | note: we won't handle deletion of the directory itself, |
| 146 | | − | this is a design decision */ |
| 147 | | − | autoreload.wd = inotify_add_watch(autoreload.fd, dn,IN_CREATE); |
| 148 | | − | if (autoreload.wd == -1) |
| 149 | | − | error(0, 0, "Failed to add inotify watch on directory '%s'.", dn); |
| 150 | | − | else |
| 151 | | − | autoreload.watching_dir = true; |
| 152 | | − | |
| 153 | | − | free(dntmp); |
| 154 | | − | } |