-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeout.c
156 lines (124 loc) · 2.31 KB
/
timeout.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
#include <ncurses.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "config.h"
#include "draw.h"
#include "input.h"
#include "state.h"
#include "utils.h"
void init_window_simple();
void init_window_main();
struct state *init_state(struct config *conf);
void intro(struct config *conf)
{
move(1, 0);
printw(conf->intro);
printw("\n\nPress enter to continue to the terminal...\n\n");
refresh();
char str[1] = {0};
getnstr(str, 0);
}
void begin(struct config *conf)
{
int size_y, size_x, y, x;
getmaxyx(stdscr, size_y, size_x);
y = size_y / 2 - 5;
x = size_x / 2 - 26;
char str[32] = {0};
clear();
refresh();
move(y, x);
print_str_slowly("enter your last name to continue...");
while (1) {
move(y + 2, x);
clrtoeol();
addch('>');
move(y + 2, x + 2);
refresh();
getnstr(str, 32);
int len = strlen(conf->lname);
if (strlen(str) == len && !strncicmp(conf->lname, str, len)) {
break;
} else if (strlen(str) <= 0) {
move(y + 4, x);
clrtoeol();
} else {
mvaddstr(y + 4, x, "unknown user");
}
}
}
void fail(struct config *conf)
{
int size_y, size_x, y, x;
getmaxyx(stdscr, size_y, size_x);
y = size_y / 2 - 5;
x = size_x / 2 - 32;
mvprintw(y, x, conf->fail);
mvprintw(y + 2, x, conf->fail2);
curs_set(0);
refresh();
while (1) {
millisleep(100000);
}
}
int run(struct config *conf)
{
time_t start, now;
time(&start);
struct state *st = init_state(conf);
while (1) {
if (!st->decrypted) {
time(&now);
}
st->time_left = st->time_total - (now - start);
if (st->time_left < 0) {
return st->decrypted;
}
draw_page(st);
accept_input(st);
millisleep(10);
}
}
int main(int argc, char **argv)
{
struct config *conf = load_config();
initscr();
init_window_simple();
intro(conf);
begin(conf);
init_window_main();
int success = run(conf);
init_window_simple();
if (!success) {
fail(conf);
}
endwin();
return 0;
}
void init_window_simple()
{
nocbreak();
echo();
nodelay(stdscr, FALSE);
curs_set(1);
clear();
refresh();
}
void init_window_main()
{
cbreak();
noecho();
nodelay(stdscr, TRUE);
curs_set(0);
clear();
refresh();
}
struct state *init_state(struct config *conf)
{
struct state *st = calloc(1, sizeof(struct state));
st->conf = conf;
st->time_total = conf->stime;
st->num_pages = conf->num_pages;
return st;
}