-
Notifications
You must be signed in to change notification settings - Fork 3
/
gtamca.c
147 lines (121 loc) · 4.17 KB
/
gtamca.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
/**
* This program is licensed under the 2-clause BSD License, see the
* LICENSE file
*/
#include <time.h>
#include <gtk/gtk.h>
#include <AL/alut.h>
static int POMODORO_TIME = 25*60;
static int PAUSE_TIME = 5*60;
static char *FORMAT = "<span font=\"35\">%d:%.2d</span>";
#define FILENAME "ding.wav";
#ifdef PREFIX
static char *FILEPATH = PREFIX "/share/tamca/" FILENAME;
#else
static char *FILEPATH = FILENAME;
#endif
static GOptionEntry options[] = {
{ "time", 't', 0, G_OPTION_ARG_INT, &POMODORO_TIME, "Time of a pomodoro (in seconds)", "N" },
{ "pause", 'p', 0, G_OPTION_ARG_INT, &PAUSE_TIME, "Time of a pause (in seconds)", "N" },
{ "sound", 's', 0, G_OPTION_ARG_STRING, &FILEPATH, "File to use as sound", "PATH" },
{ "format", 'f', 0, G_OPTION_ARG_STRING, &FORMAT, "Format of the time", "FMT" },
{ NULL }
};
struct {
GtkWidget *window; /* window */
GtkWidget *time_label; /* label */
GtkWidget *start, *pause; /* buttons */
GtkWidget *vbox, *hbox; /* boxes */
ALuint sound_buffer, sound_source;
gboolean started;
int time;
time_t last_update;
} gtamca;
/**
* Convert a time in second into a markup string using FORMAT
*/
gchar *seconds_to_markup(int seconds)
{
return g_markup_printf_escaped(FORMAT, seconds/60, seconds%60);
}
static void destroy(GtkWidget *widget, gpointer data)
{
if (!alutExit()) {
g_error("ALUT error: %s\n", alutGetErrorString(alutGetError()));
}
gtk_main_quit();
}
static void start_timer(GtkWidget *widget, gpointer data)
{
int seconds = (int) data;
gtk_label_set_markup(GTK_LABEL(gtamca.time_label),
seconds_to_markup(seconds));
gtamca.started = TRUE;
gtamca.time = seconds;
gtamca.last_update = time(NULL);
}
static gboolean update_timer(gpointer data)
{
time_t current_time;
if (gtamca.time == 0)
return TRUE;
if (gtamca.started && time(NULL) - gtamca.last_update >= 1) {
current_time = time(NULL);
gtamca.time -= current_time - gtamca.last_update;
gtamca.last_update = current_time;
if (gtamca.time <= 0) {
gtamca.time = 0;
alSourcePlay(gtamca.sound_source);
}
gtk_label_set_markup(GTK_LABEL(gtamca.time_label),
seconds_to_markup(gtamca.time));
}
return TRUE;
}
int main(int argc, char *argv[])
{
GError *error = NULL;
GOptionContext *context;
gtk_init(&argc, &argv);
/* Parse options */
context = g_option_context_new("- A pomodoro technique timer");
g_option_context_add_main_entries(context, options, NULL);
g_option_context_add_group(context, gtk_get_option_group(TRUE));
if (!g_option_context_parse(context, &argc, &argv, &error)) {
g_error("Can't parse options: %s", error->message);
return 1;
}
if (!alutInit(&argc, argv)) {
g_error("ALUT error: %s", alutGetErrorString(alutGetError()));
return 1;
}
/* ALUT stuff */
g_debug("Using sound file: %s", FILEPATH);
gtamca.sound_buffer = alutCreateBufferFromFile(FILEPATH);
alGenSources(1, >amca.sound_source);
alSourcei(gtamca.sound_source, AL_BUFFER, gtamca.sound_buffer);
/* GTK stuff */
gtamca.window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(gtamca.window, "destroy", G_CALLBACK(destroy), NULL);
gtamca.time_label = gtk_label_new(NULL);
gtamca.start = gtk_button_new_with_label("New pomodoro");
gtamca.pause = gtk_button_new_with_label("Pause");
gtamca.vbox = gtk_vbox_new(TRUE, 2);
gtamca.hbox = gtk_hbox_new(FALSE, 2);
gtk_label_set_markup(GTK_LABEL(gtamca.time_label), seconds_to_markup(0));
g_signal_connect(gtamca.start, "clicked",
G_CALLBACK(start_timer), (gpointer) POMODORO_TIME);
g_signal_connect(gtamca.pause, "clicked",
G_CALLBACK(start_timer), (gpointer) PAUSE_TIME);
gtk_container_add(GTK_CONTAINER(gtamca.hbox), gtamca.start);
gtk_container_add(GTK_CONTAINER(gtamca.hbox), gtamca.pause);
gtk_container_add(GTK_CONTAINER(gtamca.vbox), gtamca.time_label);
gtk_container_add(GTK_CONTAINER(gtamca.vbox), gtamca.hbox);
gtk_container_add(GTK_CONTAINER(gtamca.window), gtamca.vbox);
gtk_widget_show_all(gtamca.hbox);
gtk_widget_show_all(gtamca.vbox);
gtk_widget_show_all(gtamca.window);
g_timeout_add(500, update_timer, NULL);
gtk_main();
return 0;
}