A Simple X Image Viewer
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

114 lignes
2.6 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 "util.h"
  24. #include "autoreload.h"
  25. void arl_init(arl_t *arl)
  26. {
  27. arl->fd = inotify_init1(IN_CLOEXEC | IN_NONBLOCK);
  28. arl->wd_dir = arl->wd_file = -1;
  29. if (arl->fd == -1)
  30. error(0, 0, "Could not initialize inotify, no automatic image reloading");
  31. }
  32. CLEANUP void arl_cleanup(arl_t *arl)
  33. {
  34. if (arl->fd != -1)
  35. close(arl->fd);
  36. free(arl->filename);
  37. }
  38. static void rm_watch(int fd, int *wd)
  39. {
  40. if (*wd != -1) {
  41. inotify_rm_watch(fd, *wd);
  42. *wd = -1;
  43. }
  44. }
  45. static void add_watch(int fd, int *wd, const char *path, uint32_t mask)
  46. {
  47. *wd = inotify_add_watch(fd, path, mask);
  48. if (*wd == -1)
  49. error(0, errno, "inotify: %s", path);
  50. }
  51. void arl_setup(arl_t *arl, const char *filepath)
  52. {
  53. char *base = strrchr(filepath, '/');
  54. if (arl->fd == -1)
  55. return;
  56. rm_watch(arl->fd, &arl->wd_dir);
  57. rm_watch(arl->fd, &arl->wd_file);
  58. add_watch(arl->fd, &arl->wd_file, filepath, IN_CLOSE_WRITE | IN_DELETE_SELF);
  59. free(arl->filename);
  60. arl->filename = estrdup(filepath);
  61. if (base != NULL) {
  62. arl->filename[++base - filepath] = '\0';
  63. add_watch(arl->fd, &arl->wd_dir, arl->filename, IN_CREATE | IN_MOVED_TO);
  64. strcpy(arl->filename, base);
  65. }
  66. }
  67. union {
  68. char d[4096]; /* aligned buffer */
  69. struct inotify_event e;
  70. } buf;
  71. bool arl_handle(arl_t *arl)
  72. {
  73. bool reload = false;
  74. char *ptr;
  75. const struct inotify_event *event;
  76. for (;;) {
  77. ssize_t len = read(arl->fd, buf.d, sizeof(buf.d));
  78. if (len == -1) {
  79. if (errno == EINTR)
  80. continue;
  81. break;
  82. }
  83. for (ptr = buf.d; ptr < buf.d + len; ptr += sizeof(*event) + event->len) {
  84. event = (const struct inotify_event*) ptr;
  85. if (event->mask & IN_CLOSE_WRITE) {
  86. reload = true;
  87. } else if (event->mask & IN_DELETE_SELF) {
  88. rm_watch(arl->fd, &arl->wd_file);
  89. } else if (event->mask & (IN_CREATE | IN_MOVED_TO)) {
  90. if (STREQ(event->name, arl->filename))
  91. reload = true;
  92. }
  93. }
  94. }
  95. return reload;
  96. }