-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
161 lines (128 loc) · 3.98 KB
/
timer.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <ncurses.h>
#include "timer.h"
//Global module variables
int row, col;
void timer_init_tea(tea teas[], char *key, int minutes, int seconds, size_t position) {
teas[position].key = key;
teas[position].minutes = minutes;
teas[position].seconds = seconds;
}
void timer_resize() {
erase();
endwin();
refresh();
getmaxyx(stdscr, row, col); //Gets window width and height and stores it in row and col vars
}
//Max unit: 3 for hour, 2 for minute, 1 for second
void timer_start(int time_in_seconds, size_t max_unit, int is_tea) {
int input, i;
//size_t additional_time;
char sign, unit;
//additional_time = 1;
sign = '+';
unit = 's';
//timeout(1000); //delay in milliseconds for getch
signal(SIGWINCH, timer_resize);
initscr();
curs_set(0); //hides cursor
noecho(); //prevents user input from printing to the screen
cbreak();
getmaxyx(stdscr, row, col); //Gets window width and height and stores it in row and col vars
for (i = time_in_seconds; i >= 0; i--) {
size_t handled_input;
timeout(1000); //delay in milliseconds for getch
input = getch();
handled_input = 0;
switch (input) {
case 115: {
handled_input = 1;
unit = 's';
i++;
break;
}
case 109: {
handled_input = 1;
unit = 'm';
i++;
break;
}
case 104: {
handled_input = 1;
unit = 'h';
i++;
break;
}
case 43: {
handled_input = 1;
//sign = '+';
if (unit == 's') {
i = i + 1;
}
if (unit == 'm') {
i = i + 60;
}
if (unit == 'h') {
i = i + 3600;
}
i++;
break;
}
case 45: {
handled_input = 1;
//sign = '-';
if (unit == 's') {
i = i - 1;
}
if (unit == 'm') {
i = i - 60;
}
if (unit == 'h') {
i = i - 3600;
}
i++;
if (i <= 0) {
i = 1;
}
break;
}
}
if (input != -1 && handled_input == 0) {
i++;
};
clear();
refresh();
if (i > 3600) {
mvprintw(row/2, (col-4)/2, "%02d:%02d:%02d", i/3600, i%3600/60, i%3600%60);
mvprintw(row/2 + 1, (col-4)/2, "+/-: %c1%c", sign, unit);
//mvprintw(row/2 + 1, (col-4)/2, "+/-: %c%02dmin", sign, additional_time);
} else if (i % 3600 > 60) {
mvprintw(row/2, (col-2)/2, "%02d:%02d", i/60, i%60);
mvprintw(row/2 + 1, (col-2)/2, "+/-: %c1%c", sign, unit);
//mvprintw(row/2 + 1, (col-2)/2, "+/-: %c%02dmin", sign, additional_time);
} else {
mvprintw(row/2, (col-1)/2, "%02d", i);
mvprintw(row/2 + 1, (col-1)/2, "+/-: %c1%c", sign, unit);
//mvprintw(row/2 + 1, (col-1)/2, "+/-: %c%02dmin", sign, additional_time);
}
}
refresh();
if (is_tea == 0) {
mvprintw(row/2, (col-5)/2, "%s\n", "Time is up.");
} else {
mvprintw(row/2, (col-9)/2, "%s\n", "Your tea is ready.");
}
mvprintw(row/2 + 2, (col-11)/2, "%s\n", "Press any key to exit");
for (size_t j = 5; j > 0; j--) {
beep();
flash();
sleep(1);
}
timeout(10000000000000);
getch();
endwin();
exit(0);
}