A Simple X Image Viewer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

136 lines
3.2 KiB

  1. /* Copyright 2017 Max Voit
  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 <errno.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include <unistd.h>
  22. #include <sys/inotify.h>
  23. #include <libgen.h>
  24. #include "util.h"
  25. #include "autoreload.h"
  26. CLEANUP void arl_cleanup(arl_t *arl)
  27. {
  28. if (arl->fd != -1)
  29. close(arl->fd);
  30. }
  31. static void arl_setup_dir(arl_t *arl, const char *filepath)
  32. {
  33. char *dntmp, *dn;
  34. if (arl->fd == -1)
  35. return;
  36. /* get dirname */
  37. dntmp = (char*) strdup(filepath);
  38. dn = (char*) dirname(dntmp);
  39. /* this is not one-shot as other stuff may be created too
  40. * note: we won't handle deletion of the directory itself,
  41. * this is a design decision
  42. */
  43. arl->wd = inotify_add_watch(arl->fd, dn, IN_CREATE);
  44. if (arl->wd == -1)
  45. error(0, 0, "%s: Error watching directory", dn);
  46. else
  47. arl->watching_dir = true;
  48. free(dntmp);
  49. }
  50. union {
  51. char d[4096]; /* aligned buffer */
  52. struct inotify_event e;
  53. } buf;
  54. bool arl_handle(arl_t *arl, const char *filepath)
  55. {
  56. bool reload = false;
  57. char *ptr;
  58. const struct inotify_event *event;
  59. for (;;) {
  60. ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
  61. if (len == -1) {
  62. if (errno == EINTR)
  63. continue;
  64. break;
  65. }
  66. for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
  67. event = (const struct inotify_event*) ptr;
  68. /* events from watching the file itself */
  69. if (event->mask & IN_CLOSE_WRITE) {
  70. reload = true;
  71. }
  72. if (event->mask & IN_DELETE_SELF)
  73. arl_setup_dir(arl, filepath);
  74. /* events from watching the file's directory */
  75. if (event->mask & IN_CREATE) {
  76. char *fntmp = strdup(filepath);
  77. char *fn = basename(fntmp);
  78. if (STREQ(event->name, fn)) {
  79. /* this is the file we're looking for */
  80. /* cleanup, this has not been one-shot */
  81. if (arl->watching_dir) {
  82. inotify_rm_watch(arl->fd, arl->wd);
  83. arl->watching_dir = false;
  84. }
  85. reload = true;
  86. }
  87. free(fntmp);
  88. }
  89. }
  90. }
  91. return reload;
  92. }
  93. void arl_init(arl_t *arl)
  94. {
  95. /* this needs to be done only once */
  96. arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
  97. arl->watching_dir = false;
  98. if (arl->fd == -1)
  99. error(0, 0, "Could not initialize inotify, no automatic image reloading");
  100. }
  101. void arl_setup(arl_t *arl, const char *filepath)
  102. {
  103. if (arl->fd == -1)
  104. return;
  105. /* may have switched from a deleted to another image */
  106. if (arl->watching_dir) {
  107. inotify_rm_watch(arl->fd, arl->wd);
  108. arl->watching_dir = false;
  109. }
  110. arl->wd = inotify_add_watch(arl->fd, filepath,
  111. IN_ONESHOT | IN_CLOSE_WRITE | IN_DELETE_SELF);
  112. if (arl->wd == -1)
  113. error(0, 0, "%s: Error watching file", filepath);
  114. }