-
Notifications
You must be signed in to change notification settings - Fork 0
/
timefier.c
91 lines (77 loc) · 2.07 KB
/
timefier.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
#include <stdlib.h>
#include <stdio.h>
#include <getopt.h>
#include <errno.h>
#include <time.h>
#include <libnotify/notify.h>
struct args_t {
char *title;
char *message;
unsigned long delay;
};
void usage()
{
fprintf(stderr,
"-t | --title : Title for the notification.\n"
"-m | --message : Message (body) off the notification.\n"
"-d | --delay : Seconds to fire the notification.\n"
"\nExample:\n"
"timefier -t 'Coffe!' -m 'Water should be ready now.' -d 120\n");
}
int main(int argc, char *argv[])
{
NotifyNotification *nt;
unsigned int ch;
char *appname = argv[0];
struct args_t args = {
.title = "Timer",
.message = "Ready!",
.delay = 5,
};
struct option longopts[] = {
{"title", required_argument, NULL, 't'},
{"message", required_argument, NULL, 'm'},
{"delay", required_argument, NULL, 'd'},
{NULL, 0, NULL, 0}
};
if (argc <= 1) {
usage();
return 1;
}
while ((ch = getopt_long(argc, argv, "t:m:d:", longopts, NULL)) != -1) {
char *n;
switch (ch) {
case 't':
args.title = optarg;
break;
case 'm':
args.message = optarg;
break;
case 'd':
args.delay = strtoul(optarg, &n, 10);
if (errno == ERANGE) {
fprintf(stderr, "Out of range at %d.\n", __LINE__);
return 1;
}
break;
default:
usage();
break;
}
}
if (!notify_init(appname)) {
fprintf(stderr, "Couldn't create notify_init at %d\n.", __LINE__);
return 1;
}
if (!(nt = notify_notification_new(args.title, args.message, NULL))) {
fprintf(stderr, "Couldn't create notify_notification at %d\n.", __LINE__);
return 1;
}
notify_notification_set_urgency(nt, NOTIFY_URGENCY_CRITICAL);
if (fork() == 0) {
sleep(args.delay);
notify_notification_show(nt, NULL);
}
notify_uninit();
return 0;
}