-
Notifications
You must be signed in to change notification settings - Fork 5
/
main.c
211 lines (162 loc) · 4.14 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include <fcntl.h>
#include <limits.h>
#include <poll.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdatomic.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include "draw.h"
#include "common.h"
#include "io.h"
#include "timer.h"
static atomic_bool _g_should_exit;
static vtk_window _g_win;
static void int_handler(int signal) {
vtk_window_close(_g_win);
}
static void close_handler(vtk_event ev, void *u) {
vtk_window_close(_g_win);
}
static int input_main(void *u) {
struct state *s = u;
int pipefd[2];
if (pipe(pipefd) == -1) {
fputs("Failed to create pipe\n", stderr);
exit(1);
}
pid_t pid = 0;
pid = fork();
if (pid == 0) {
// Child
close(pipefd[0]);
dup2(pipefd[1], STDOUT_FILENO);
execlp("./splitter", "./splitter", NULL);
fputs("Failed to exec splitter\n", stderr);
exit(1);
} else if (pid == -1) {
fputs("Failed to fork\n", stderr);
exit(1);
}
// Parent
close(pipefd[1]);
int fl = fcntl(pipefd[0], F_GETFL);
if (fl == -1) fl = 0;
fcntl(pipefd[0], F_SETFL, fl | O_NONBLOCK);
FILE *f = fdopen(pipefd[0], "r");
struct pollfd fds[] = {
{ pipefd[0], POLLIN },
};
while (!_g_should_exit) {
// Use poll rather than getline directly; that way, we can routinely
// check if we should exit
if (poll(fds, sizeof fds / sizeof fds[0], 500) > 0) {
char *line = NULL;
size_t n = 0;
if (getline(&line, &n, f) == -1) {
free(line);
break;
}
line[strlen(line) - 1] = 0; // Remove newline
timer_parse(s, line);
free(line);
vtk_window_trigger_update(s->win);
}
}
close(pipefd[0]);
if (pid) {
kill(pid, SIGINT);
}
return 0;
}
void update_handler(vtk_event ev, void *u) {
struct state *s = u;
vtk_window_redraw(s->win);
}
int main(int argc, char **argv) {
if (argc > 2 || (argc == 2 && !strcmp(argv[1], "-h"))) {
fprintf(stderr, "Usage: %s [path]\n", argv[0]);
return argc > 2;
}
if (argc == 2) {
if (chdir(argv[1])) {
fprintf(stderr, "Failed to chdir to %s\n", argv[1]);
return 1;
}
}
enum widget_type widgets[] = {
WIDGET_GAME_NAME,
WIDGET_CATEGORY_NAME,
WIDGET_SUM_OF_BEST,
WIDGET_BEST_POSSIBLE_TIME,
WIDGET_TIMER,
WIDGET_SPLIT_TIMER,
WIDGET_SPLITS,
};
struct split *splits;
ssize_t nsplits = read_splits_file("splits", &splits);
if (nsplits == -1) {
return 1;
}
if (!read_times(splits, nsplits, "pb", offsetof(struct times, pb))) {
fputs("Warning: could not read PB\n", stderr);
}
if (!read_times(splits, nsplits, "golds", offsetof(struct times, best))) {
fputs("Warning: could not read golds\n", stderr);
}
struct cfgdict *cfg = cfgdict_new();
if (!read_config("config", cfg)) {
fputs("Warning: could not read config\n", stderr);
}
int err;
vtk vtk;
err = vtk_new(&vtk);
if (err) {
fprintf(stderr, "Error initializing vtk: %s\n", vtk_strerr(err));
return 1;
}
vtk_window win;
err = vtk_window_new(&win, vtk, "Adrift", 0, 0, config_get_int(cfg, "window_width", 350), config_get_int(cfg, "window_height", 650));
if (err) {
fprintf(stderr, "Error initializing vtk window: %s\n", vtk_strerr(err));
vtk_destroy(vtk);
return 1;
}
cairo_t *cr = vtk_window_get_cairo(win);
struct state s = {
.win = win,
.cr = cr,
.game_name = config_get_str(cfg, "game", "Portal 2"),
.category_name = config_get_str(cfg, "category", "Inbounds NoSLA"),
.nwidgets = sizeof widgets / sizeof widgets[0],
.widgets = widgets,
.nsplits = nsplits,
.splits = splits,
.active_split = -1,
.timer = 0,
.split_time = 0,
.cfg = cfg,
};
_g_win = win;
thrd_t inp_thrd;
if (thrd_create(&inp_thrd, &input_main, &s) != thrd_success) {
fputs("Error creating thread\n", stderr);
vtk_window_destroy(win);
vtk_destroy(vtk);
return 1;
}
vtk_window_set_event_handler(win, VTK_EV_DRAW, draw_handler, &s);
vtk_window_set_event_handler(win, VTK_EV_CLOSE, close_handler, &s);
vtk_window_set_event_handler(win, VTK_EV_UPDATE, update_handler, &s);
signal(SIGINT, &int_handler);
vtk_window_mainloop(win);
vtk_window_destroy(win);
vtk_destroy(vtk);
_g_should_exit = true;
thrd_join(inp_thrd, NULL);
save_times(splits, nsplits, "golds", offsetof(struct times, best));
cfgdict_free(cfg);
return 0;
}