summaryrefslogtreecommitdiff
path: root/ted.c
diff options
context:
space:
mode:
authorpommicket <pommicket@gmail.com>2025-06-16 12:19:17 -0400
committerpommicket <pommicket@gmail.com>2025-06-16 12:19:29 -0400
commit55b42079488e0c3e8aaf85828ad028491934f9d3 (patch)
treee69c7ec5fc8ba7ec82f1f8ddef692bc209b46d7b /ted.c
parent5ad0bd2422e5b8bb23e50256837ae0c2538a85e0 (diff)
better inotify
Diffstat (limited to 'ted.c')
-rw-r--r--ted.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/ted.c b/ted.c
index 7cddd68..3fcf266 100644
--- a/ted.c
+++ b/ted.c
@@ -3,8 +3,11 @@
#include "ted-internal.h"
#if _WIN32
#include <SDL_syswm.h>
+#elif __unix__
+ #include <unistd.h>
#endif
+
struct LoadedFont {
char *path;
Font *font;
@@ -978,3 +981,42 @@ void ted_test(Ted *ted) {
#undef run_test
printf("all good as far as i know :3\n");
}
+
+#if HAS_INOTIFY
+void ted_check_inotify(Ted *ted) {
+ // see buffer_externally_changed definition for why this exists
+ if (ted->inotify_fd == -1) return;
+ int *watches_modified = NULL;
+ char events[16384 * sizeof(struct inotify_event)];
+ ssize_t bytes = read(ted->inotify_fd, events, sizeof events);
+ for (int i = 0; i + (int)sizeof(struct inotify_event) <= bytes; i += (int)sizeof(struct inotify_event)) {
+ struct inotify_event event;
+ memcpy(&event, &events[i * (int)sizeof event], sizeof event);
+ if (event.mask & INOTIFY_MASK) {
+ bool new = true;
+ arr_foreach_ptr(watches_modified, int, w) {
+ if (*w == event.wd) {
+ new = false;
+ break;
+ }
+ }
+ if (new) arr_add(watches_modified, event.wd);
+ }
+ i += (int)event.len; // should always be 0 in theory...
+ }
+ // ideally we'd read more events in a loop here but then we'd hang if someone is constantly
+ // changing the file. probably no good way of dealing with that though.
+ // for what it's worth, 16384 is the default max inotify queue size, so we should always
+ // read all the events with that one read call.
+ arr_foreach_ptr(ted->buffers, TextBufferPtr, pbuffer) {
+ int watch = buffer_inotify_watch(*pbuffer);
+ arr_foreach_ptr(watches_modified, int, w) {
+ if (*w == watch) {
+ buffer_set_inotify_modified(*pbuffer);
+ break;
+ }
+ }
+ }
+ arr_free(watches_modified);
+}
+#endif