main_linux.c (1892B)
1 /* See LICENSE for license details. */ 2 #include "compiler.h" 3 4 #if !OS_LINUX 5 #error This file is only meant to be compiled for Linux 6 #endif 7 8 #include "os_linux.c" 9 #include "common.c" 10 11 function void 12 dispatch_file_watch_events(OS *os, Arena arena) 13 { 14 FileWatchContext *fwctx = &os->file_watch_context; 15 u8 *mem = arena_alloc(&arena, 4096, 16, 1); 16 Stream path = stream_alloc(&arena, 256); 17 struct inotify_event *event; 18 19 sz rlen; 20 while ((rlen = read(fwctx->handle, mem, 4096)) > 0) { 21 for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) { 22 event = (struct inotify_event *)data; 23 for (u32 i = 0; i < fwctx->count; i++) { 24 FileWatchDirectory *dir = fwctx->data + i; 25 if (event->wd != dir->handle) 26 continue; 27 28 str8 file = c_str_to_str8(event->name); 29 u64 hash = str8_hash(file); 30 for (u32 i = 0; i < dir->count; i++) { 31 FileWatch *fw = dir->data + i; 32 if (fw->hash == hash) { 33 stream_append_str8s(&path, dir->name, str8("/"), file); 34 stream_append_byte(&path, 0); 35 stream_commit(&path, -1); 36 fw->callback(os, stream_to_str8(&path), 37 fw->user_data, arena); 38 stream_reset(&path, 0); 39 break; 40 } 41 } 42 } 43 } 44 } 45 } 46 47 extern s32 48 main(void) 49 { 50 Arena memory = os_alloc_arena(GB(1)); 51 ViewerContext *ctx = push_struct(&memory, ViewerContext); 52 ctx->arena = memory; 53 54 ctx->os.file_watch_context.handle = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); 55 ctx->os.error_handle = STDERR_FILENO; 56 57 init_viewer(ctx); 58 59 struct pollfd fds[1] = {{0}}; 60 fds[0].fd = ctx->os.file_watch_context.handle; 61 fds[0].events = POLLIN; 62 63 while (!ctx->should_exit) { 64 poll(fds, countof(fds), 0); 65 if (fds[0].revents & POLLIN) 66 dispatch_file_watch_events(&ctx->os, ctx->arena); 67 viewer_frame_step(ctx, get_frame_time_step(ctx)); 68 glfwSwapBuffers(ctx->window); 69 glfwPollEvents(); 70 } 71 }