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.
 
 
 
 
 
 

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