A Simple X Image Viewer
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

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