autoreload_inotify.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /* Copyright 2017 Max Voit, Bert Muennich
  2. *
  3. * This file is part of sxiv.
  4. *
  5. * sxiv is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published
  7. * by the Free Software Foundation; either version 2 of the License,
  8. * or (at your option) any later version.
  9. *
  10. * sxiv is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with sxiv. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "sxiv.h"
  19. #include <errno.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include <sys/inotify.h>
  24. void arl_init(arl_t *arl)
  25. {
  26. arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
  27. arl->wd_dir = arl->wd_file = -1;
  28. if (arl->fd == -1)
  29. error(0, 0, "Could not initialize inotify, no automatic image reloading");
  30. }
  31. CLEANUP void arl_cleanup(arl_t *arl)
  32. {
  33. if (arl->fd != -1)
  34. close(arl->fd);
  35. free(arl->filename);
  36. }
  37. static void rm_watch(int fd, int *wd)
  38. {
  39. if (*wd != -1) {
  40. inotify_rm_watch(fd, *wd);
  41. *wd = -1;
  42. }
  43. }
  44. static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
  45. {
  46. *wd = inotify_add_watch(fd, path, mask);
  47. if (*wd == -1)
  48. error(0, errno, "inotify: %s", path);
  49. }
  50. void arl_setup(arl_t *arl, const char *filepath)
  51. {
  52. char *base = strrchr(filepath, '/');
  53. if (arl->fd == -1)
  54. return;
  55. rm_watch(arl->fd, &arl->wd_dir);
  56. rm_watch(arl->fd, &arl->wd_file);
  57. add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
  58. free(arl->filename);
  59. arl->filename = estrdup(filepath);
  60. if (base != NULL) {
  61. arl->filename[++base - filepath] = '\0';
  62. add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
  63. strcpy(arl->filename, base);
  64. }
  65. }
  66. union {
  67. char d[4096]; /* aligned buffer */
  68. struct inotify_event e;
  69. } buf;
  70. bool arl_handle(arl_t *arl)
  71. {
  72. bool reload = false;
  73. char *ptr;
  74. const struct inotify_event *e;
  75. for (;;) {
  76. ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
  77. if (len == -1) {
  78. if (errno == EINTR)
  79. continue;
  80. break;
  81. }
  82. for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*e) + e->len) {
  83. e = (const struct inotify_event*) ptr;
  84. if (e->wd == arl->wd_file && (e->mask & IN_CLOSE_WRITE)) {
  85. reload = true;
  86. } else if (e->wd == arl->wd_file && (e->mask & IN_DELETE_SELF)) {
  87. rm_watch(arl->fd, &arl->wd_file);
  88. } else if (e->wd == arl->wd_dir && (e->mask & (IN_CREATE | IN_MOVED_TO))) {
  89. if (STREQ(e->name, arl->filename))
  90. reload = true;
  91. }
  92. }
  93. }
  94. return reload;
  95. }