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.
 
 
 
 
 
 

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