main_linux.c (2951B)
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 "beamformer.h" 9 10 #include "os_linux.c" 11 12 #define OS_DEBUG_LIB_NAME "./beamformer.so" 13 #define OS_DEBUG_LIB_TEMP_NAME "./beamformer_temp.so" 14 15 #define OS_CUDA_LIB_NAME "./external/cuda_toolkit.so" 16 #define OS_CUDA_LIB_TEMP_NAME "./external/cuda_toolkit_temp.so" 17 18 #define OS_RENDERDOC_SONAME "librenderdoc.so" 19 20 /* TODO(rnp): what do if not X11? */ 21 iptr glfwGetGLXContext(iptr); 22 function iptr 23 os_get_native_gl_context(iptr window) 24 { 25 return glfwGetGLXContext(window); 26 } 27 28 iptr glfwGetProcAddress(char *); 29 function iptr 30 os_gl_proc_address(char *name) 31 { 32 return glfwGetProcAddress(name); 33 } 34 35 #include "static.c" 36 37 function void 38 dispatch_file_watch_events(FileWatchDirectoryList *fwctx, Arena arena) 39 { 40 u8 *mem = arena_alloc(&arena, 4096, 16, 1); 41 Stream path = stream_alloc(&arena, 256); 42 struct inotify_event *event; 43 44 iz rlen; 45 while ((rlen = read(os_linux_context.inotify_handle, mem, 4096)) > 0) { 46 for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) { 47 event = (struct inotify_event *)data; 48 for (u32 i = 0; i < fwctx->count; i++) { 49 FileWatchDirectory *dir = fwctx->data + i; 50 if (event->wd != dir->handle) 51 continue; 52 53 s8 file = c_str_to_s8(event->name); 54 u64 hash = u64_hash_from_s8(file); 55 for (u32 j = 0; j < dir->count; j++) { 56 FileWatch *fw = dir->data + j; 57 if (fw->hash == hash) { 58 stream_append_s8s(&path, dir->name, s8("/"), file); 59 stream_append_byte(&path, 0); 60 stream_commit(&path, -1); 61 fw->callback(stream_to_s8(&path), fw->user_data, arena); 62 stream_reset(&path, 0); 63 break; 64 } 65 } 66 } 67 } 68 } 69 } 70 71 extern i32 72 main(void) 73 { 74 os_common_init(); 75 76 Arena program_memory = os_alloc_arena(MB(16) + KB(4)); 77 78 BeamformerCtx *ctx = 0; 79 BeamformerInput *input = 0; 80 81 os_linux_context.arena = sub_arena(&program_memory, KB(4), KB(4)); 82 os_linux_context.inotify_handle = inotify_init1(IN_NONBLOCK|IN_CLOEXEC); 83 84 setup_beamformer(&program_memory, &ctx, &input); 85 86 struct pollfd fds[1] = {{0}}; 87 fds[0].fd = os_linux_context.inotify_handle; 88 fds[0].events = POLLIN; 89 90 u64 last_time = os_get_timer_counter(); 91 while (!ctx->should_exit) { 92 poll(fds, countof(fds), 0); 93 if (fds[0].revents & POLLIN) 94 dispatch_file_watch_events(&ctx->file_watch_list, program_memory); 95 96 u64 now = os_get_timer_counter(); 97 input->last_mouse = input->mouse; 98 input->mouse.rl = GetMousePosition(); 99 input->dt = (f32)((f64)(now - last_time) / (f64)os_get_timer_frequency()); 100 last_time = now; 101 102 beamformer_frame_step(ctx, input); 103 104 input->executable_reloaded = 0; 105 } 106 107 beamformer_invalidate_shared_memory(ctx); 108 beamformer_debug_ui_deinit(ctx); 109 110 /* NOTE: make sure this will get cleaned up after external 111 * programs release their references */ 112 shm_unlink(OS_SHARED_MEMORY_NAME); 113 }