ogl_beamforming

Ultrasound Beamforming Implemented with OpenGL
git clone anongit@rnpnr.xyz:ogl_beamforming.git
Log | Files | Refs | Feed | Submodules | LICENSE

main_linux.c (3110B)


      1 /* See LICENSE for license details. */
      2 #ifndef __linux__
      3 #error This file is only meant to be compiled for Linux
      4 #endif
      5 
      6 #include "beamformer.h"
      7 
      8 #include "os_unix.c"
      9 
     10 #define OS_DEBUG_LIB_NAME      "./beamformer.so"
     11 #define OS_DEBUG_LIB_TEMP_NAME "./beamformer_temp.so"
     12 
     13 #define OS_CUDA_LIB_NAME      "./external/cuda_toolkit.so"
     14 #define OS_CUDA_LIB_TEMP_NAME "./external/cuda_toolkit_temp.so"
     15 
     16 #define OS_PIPE_NAME "/tmp/beamformer_data_fifo"
     17 #define OS_SMEM_NAME "/ogl_beamformer_parameters"
     18 
     19 #define OS_PATH_SEPERATOR "/"
     20 
     21 #include "static.c"
     22 
     23 static void
     24 dispatch_file_watch_events(FileWatchContext *fwctx, Arena arena)
     25 {
     26 	u8 *mem     = alloc_(&arena, 4096, 64, 1);
     27 	Stream path = stream_alloc(&arena, 256);
     28 	struct inotify_event *event;
     29 
     30 	size rlen;
     31 	while ((rlen = read(fwctx->handle, mem, 4096)) > 0) {
     32 		for (u8 *data = mem; data < mem + rlen; data += sizeof(*event) + event->len) {
     33 			event = (struct inotify_event *)data;
     34 			for (u32 i = 0; i < fwctx->directory_watch_count; i++) {
     35 				FileWatchDirectory *dir = fwctx->directory_watches + i;
     36 				if (event->wd != dir->handle)
     37 					continue;
     38 
     39 				s8  file = cstr_to_s8(event->name);
     40 				u64 hash = s8_hash(file);
     41 				for (u32 i = 0; i < dir->file_watch_count; i++) {
     42 					FileWatch *fw = dir->file_watches + i;
     43 					if (fw->hash == hash) {
     44 						stream_append_s8(&path, dir->name);
     45 						stream_append_byte(&path, '/');
     46 						stream_append_s8(&path, file);
     47 						stream_append_byte(&path, 0);
     48 						path.widx--;
     49 						fw->callback(stream_to_s8(&path),
     50 						             fw->user_data, arena);
     51 						path.widx = 0;
     52 						break;
     53 					}
     54 				}
     55 			}
     56 		}
     57 	}
     58 }
     59 
     60 int
     61 main(void)
     62 {
     63 	BeamformerCtx   ctx   = {0};
     64 	BeamformerInput input = {.executable_reloaded = 1};
     65 	Arena temp_memory = os_alloc_arena((Arena){0}, 16 * MEGABYTE);
     66 	ctx.error_stream  = stream_alloc(&temp_memory, 1 * MEGABYTE);
     67 
     68 	ctx.ui_backing_store = sub_arena(&temp_memory, 2 * MEGABYTE, 4096);
     69 
     70 	Pipe data_pipe    = os_open_named_pipe(OS_PIPE_NAME);
     71 	input.pipe_handle = data_pipe.file;
     72 	ASSERT(data_pipe.file != INVALID_FILE);
     73 
     74 	#define X(name) ctx.platform.name = os_ ## name;
     75 	PLATFORM_FNS
     76 	#undef X
     77 
     78 	ctx.platform.file_watch_context.handle = inotify_init1(O_NONBLOCK|O_CLOEXEC);
     79 
     80 	setup_beamformer(&ctx, &temp_memory);
     81 	debug_init(&ctx.platform, (iptr)&input, &temp_memory);
     82 
     83 	struct pollfd fds[2] = {{0}, {0}};
     84 	fds[0].fd     = ctx.platform.file_watch_context.handle;
     85 	fds[0].events = POLLIN;
     86 	fds[1].fd     = data_pipe.file;
     87 	fds[1].events = POLLIN;
     88 
     89 	while (!(ctx.flags & SHOULD_EXIT)) {
     90 		poll(fds, 2, 0);
     91 		if (fds[0].revents & POLLIN)
     92 			dispatch_file_watch_events(&ctx.platform.file_watch_context, temp_memory);
     93 
     94 		input.pipe_data_available = !!(fds[1].revents & POLLIN);
     95 		input.last_mouse          = input.mouse;
     96 		input.mouse.rl            = GetMousePosition();
     97 
     98 		beamformer_frame_step(&ctx, &temp_memory, &input);
     99 
    100 		input.executable_reloaded = 0;
    101 	}
    102 
    103 	/* NOTE: make sure this will get cleaned up after external
    104 	 * programs release their references */
    105 	shm_unlink(OS_SMEM_NAME);
    106 
    107 	/* NOTE: garbage code needed for Linux */
    108 	close(data_pipe.file);
    109 	unlink(data_pipe.name);
    110 }