-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgptimer.c
318 lines (231 loc) · 6.61 KB
/
gptimer.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
//SPDX-License-Identifier: GPL-2.0-or-later
/*
Copyright (C) 2022 Cyril Hrubis <[email protected]>
*/
#include <time.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/ioctl.h>
#include <utils/gp_utils.h>
#include <widgets/gp_widgets.h>
#define APP_NAME "gptimer"
static gp_widget *timer_time;
static gp_widget *timer_pbar;
static gp_widget *hours;
static gp_widget *mins;
static gp_widget *secs;
static gp_widget *wake;
static struct timespec start_time;
static uint64_t timer_elapsed_ms;
static uint64_t timer_duration_ms;
static timer_t wake_timer;
static int wake_timer_created;
#define HOURS_IN_MS (60 * 60 * 1000)
#define MINS_IN_MS (60 * 1000)
#define SECS_IN_MS (1000)
#define WAKEUP_MARGIN 5
static clockid_t timer_clock;
static void check_posix_timer_support(void)
{
struct timespec tmp;
if (!clock_gettime(CLOCK_BOOTTIME_ALARM, &tmp)) {
timer_clock = CLOCK_BOOTTIME_ALARM;
GP_DEBUG(1, "Selected CLOCK_BOOTTIME_ALARM");
return;
}
GP_DEBUG(1, "CLOCK_BOOTTIME_ALARM %s", strerror(errno));
if (wake)
gp_widget_disable(wake);
if (clock_gettime(CLOCK_MONOTONIC_RAW, &tmp)) {
GP_DEBUG(1, "Selected CLOCK_MONOTONIC_RAW");
timer_clock = CLOCK_MONOTONIC_RAW;
return;
}
GP_DEBUG(1, "CLOCK_MONOTONIC_RAW %s", strerror(errno));
timer_clock = CLOCK_MONOTONIC;
GP_DEBUG(1, "Selected CLOCK_MONOTONIC_RAW");
}
static void update_timer(uint64_t elapsed)
{
uint64_t cur_time = timer_duration_ms - elapsed;
int hours = cur_time / HOURS_IN_MS;
int mins = (cur_time % HOURS_IN_MS) / MINS_IN_MS;
int secs = (cur_time % MINS_IN_MS) / SECS_IN_MS;
int msecs = (cur_time % SECS_IN_MS) / 100;
gp_widget_label_printf(timer_time, "%02i:%02i:%02i.%1i", hours, mins, secs, msecs);
if (timer_pbar) {
gp_widget_pbar_max_set(timer_pbar, timer_duration_ms);
gp_widget_pbar_val_set(timer_pbar, cur_time);
}
}
static void update_duration(void)
{
timer_duration_ms = gp_widget_int_val_get(hours) * HOURS_IN_MS +
gp_widget_int_val_get(mins) * MINS_IN_MS +
gp_widget_int_val_get(secs) * SECS_IN_MS;
update_timer(0);
}
static int update_duration_callback(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
update_duration();
return 0;
}
static uint64_t timespec_diff_ms(struct timespec *end, struct timespec *start)
{
uint64_t diff_ms = 1000 * (end->tv_sec - start->tv_sec);
if (end->tv_nsec < start->tv_nsec)
diff_ms -= (start->tv_nsec - end->tv_nsec + 500)/1000000;
else
diff_ms += (end->tv_nsec - start->tv_nsec + 500)/1000000;
return diff_ms;
}
static char *alarm_cmdline = "aplay " ALARM_PATH;
static void play_alarm(void)
{
if (!alarm_cmdline)
return;
if (access(ALARM_PATH, F_OK))
alarm_cmdline = "aplay alarm.wav";
if (!fork()) {
if (system(alarm_cmdline))
GP_WARN("Failed to execute '%s'", alarm_cmdline);
exit(0);
}
}
static uint32_t timer_tick_callback(gp_timer *self)
{
struct timespec cur_time;
uint64_t elapsed_ms;
(void) self;
clock_gettime(timer_clock, &cur_time);
elapsed_ms = timespec_diff_ms(&cur_time, &start_time) + timer_elapsed_ms;
if (elapsed_ms >= timer_duration_ms) {
update_timer(timer_duration_ms);
play_alarm();
return GP_TIMER_STOP;
}
update_timer(elapsed_ms);
return self->period;
}
static gp_timer timer_tick = {
.period = 100,
.callback = timer_tick_callback,
.id = "timer",
};
static void start_wake_alarm(void)
{
signal(SIGALRM, SIG_IGN);
if (timer_create(timer_clock, NULL, &wake_timer)) {
gp_dialog_msg_printf_run(GP_DIALOG_MSG_ERR,
"Failed to create wake alarm",
"%s", strerror(errno));
return;
}
wake_timer_created = 1;
struct itimerspec tmr = {};
tmr.it_value.tv_sec = (timer_duration_ms - timer_elapsed_ms)/SECS_IN_MS;
if (tmr.it_value.tv_sec > WAKEUP_MARGIN)
tmr.it_value.tv_sec -= WAKEUP_MARGIN;
timer_settime(wake_timer, 0, &tmr, NULL);
}
static void stop_wake_alarm(void)
{
if (!wake_timer_created)
return;
timer_delete(&wake_timer);
wake_timer_created = 0;
}
int start_timer(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
clock_gettime(timer_clock, &start_time);
gp_widgets_timer_ins(&timer_tick);
if (wake && gp_widget_bool_get(wake))
start_wake_alarm();
return 0;
}
int stop_timer(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
stop_wake_alarm();
timer_elapsed_ms = 0;
update_timer(0);
gp_widgets_timer_rem(&timer_tick);
return 0;
}
int pause_timer(gp_widget_event *ev)
{
struct timespec cur_time;
if (ev->type != GP_WIDGET_EVENT_WIDGET)
return 0;
stop_wake_alarm();
clock_gettime(timer_clock, &cur_time);
timer_elapsed_ms += timespec_diff_ms(&cur_time, &start_time);
gp_widgets_timer_rem(&timer_tick);
return 0;
}
static void load_config(void)
{
int val_hrs, val_mins, val_secs;
if (gp_app_cfg_scanf(APP_NAME, "timeout.txt", "%i:%i:%i",
&val_hrs, &val_mins, &val_secs) != 3)
return;
if (hours)
gp_widget_int_val_set(hours, val_hrs);
if (mins)
gp_widget_int_val_set(mins, val_mins);
if (secs)
gp_widget_int_val_set(secs, val_secs);
}
static void save_config(void)
{
gp_app_cfg_printf(APP_NAME, "timeout.txt", "%02i:%02i:%02i\n",
(int)gp_widget_int_val_get(hours),
(int)gp_widget_int_val_get(mins),
(int)gp_widget_int_val_get(secs));
}
static int app_on_event(gp_widget_event *ev)
{
if (ev->type != GP_WIDGET_EVENT_FREE)
return 0;
save_config();
return 1;
}
gp_app_info app_info = {
.name = "gptimer",
.desc = "A simple timer app",
.version = "1.0",
.license = "GPL-2.0-or-later",
.url = "http://github.com/gfxprim/gptimer",
.authors = (gp_app_info_author []) {
{.name = "Cyril Hrubis", .email = "[email protected]", .years = "2022"},
{}
}
};
int main(int argc, char *argv[])
{
gp_htable *uids;
gp_widget *layout;
layout = gp_app_layout_load(APP_NAME, &uids);
timer_time = gp_widget_by_uid(uids, "timer_time", GP_WIDGET_LABEL);
timer_pbar = gp_widget_by_uid(uids, "timer_pbar", GP_WIDGET_PROGRESSBAR);
hours = gp_widget_by_cuid(uids, "hours", GP_WIDGET_CLASS_INT);
mins = gp_widget_by_cuid(uids, "mins", GP_WIDGET_CLASS_INT);
secs = gp_widget_by_cuid(uids, "secs", GP_WIDGET_CLASS_INT);
wake = gp_widget_by_cuid(uids, "wake", GP_WIDGET_CLASS_BOOL);
gp_htable_free(uids);
gp_widget_on_event_set(hours, update_duration_callback, NULL);
gp_widget_on_event_set(mins, update_duration_callback, NULL);
gp_widget_on_event_set(secs, update_duration_callback, NULL);
check_posix_timer_support();
load_config();
update_duration();
gp_app_on_event_set(app_on_event);
gp_widgets_main_loop(layout, NULL, argc, argv);
return 0;
}