-
Notifications
You must be signed in to change notification settings - Fork 0
/
stopwatch.c
143 lines (113 loc) · 3.54 KB
/
stopwatch.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
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <ncurses.h>
#include "stopwatch.h"
//Global module variables
int row, col;
size_t hours, minutes, seconds;
struct tm start_time;
void stopwatch_resize() {
erase();
endwin();
refresh();
getmaxyx(stdscr, row, col); //Gets window width and height and stores it in row and col vars
}
void stopwatch_log() {
FILE *log;
time_t t;
t = time(NULL);
struct tm time = *localtime(&t);
endwin();
printf("Logged time: %lu hours, %lu minutes, %lu seconds\n", hours, minutes, seconds);
printf("Start time: %d:%d:%d\n", time.tm_hour, time.tm_min, time.tm_sec);
fflush(stdout);
log = fopen("/Users/cyee/Documents/practice/c/timer/log.txt", "a+");
fprintf(log, "%lu hours, %lu minutes, %lu seconds | %d/%d/%d %d:%d:%d\n", hours, minutes, seconds, time.tm_mon + 1, time.tm_mday, time.tm_year + 1900, time.tm_hour, time.tm_min, time.tm_sec);
fclose(log);
exit(0);
}
void stopwatch_start(int time_in_seconds) {
//size_t hours, minutes, seconds;
int paused;
time_t t;
start_time = *localtime(&t);
paused = 0;
signal(SIGWINCH, stopwatch_resize);
signal(SIGINT, stopwatch_log);
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
hours = time_in_seconds / 3600;
minutes = (time_in_seconds / 60) % 60;
seconds = time_in_seconds % 60;
while (hours < 24) {
timeout(1000); //delay in milliseconds for getch
++seconds;
int input = getch(); //replaces sleep(1) because it blocks for 1 second
if (input != -1 && paused == 0) {
paused = 1;
refresh();
if (hours != 0 || minutes != 0) {
mvprintw(row/2 - 1, (col-2)/2, "PAUSED");
} else {
mvprintw(row/2 - 1, (col-5)/2, "PAUSED");
}
timeout(10000000000000);
getch();
paused = 0;
erase();
} else if (input != -1 && paused == 1) {
paused = 0;
erase();
}
if (seconds == 60) {
seconds = 0;
++minutes;
}
if (minutes == 60) {
minutes = 0;
++hours;
}
refresh();
if (hours != 0) {
mvprintw(row/2, (col-4)/2, "%02d:%02d:%02d", hours, minutes, seconds);
} else if (minutes != 0) {
mvprintw(row/2, (col-2)/2, "%02d:%02d", minutes, seconds);
} else {
mvprintw(row/2, (col-1)/2, "%02d", seconds);
}
//after ++seconds so if time_in_seconds == 0 this will never hit
//if (time_in_seconds == (hours * 3600) + (minutes * 60) + seconds) {
// for (size_t j = 5; j > 0; j--) {
// beep();
// flash();
// if (j != 1) {
// sleep(1);
// }
// ++seconds;
// if (seconds == 60) {
// seconds = 0;
// ++minutes;
// }
// if (minutes == 60) {
// minutes = 0;
// ++hours;
// }
// }
// //seconds += 4; //To compensate for j = 5
//}
//sleep(1);
}
//TODO: add logging
// echo();
// getch();
// save getch to log
//
endwin();
exit(0);
}