From 55b42079488e0c3e8aaf85828ad028491934f9d3 Mon Sep 17 00:00:00 2001 From: pommicket Date: Mon, 16 Jun 2025 12:19:17 -0400 Subject: better inotify --- ted.c | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'ted.c') 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 +#elif __unix__ + #include #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 -- cgit v1.2.3