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