A Simple X Image Viewer
 
 
 
 
 
 

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