4 * (c) 2002, 2010, 2013 Martin Mares <mj@ucw.cz>
18 #define UNUSED __attribute__((unused))
20 static guint second_timer;
21 static char old_text[16];
22 static GtkWidget *win, *hbox1, *timebox, *togglebutton1;
23 static time_t alarm_time;
28 it_tolls_for_thee(void)
35 g_spawn_command_line_async(run_cmd, &err);
38 fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
49 on_second_timeout(gpointer data UNUSED)
52 time_t now = time(NULL);
53 int delta = alarm_time - now;
61 if (delta >= 100*60*60)
62 delta = 100*60*60 - 1;
64 sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
66 sprintf(buf, "%s%02d:%02d:%02d", sign, delta/3600, (delta%3600)/60, delta%60);
67 gtk_entry_set_text(GTK_ENTRY(timebox), buf);
68 if (now >= alarm_time)
76 on_timebox_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
78 if (!strcmp(ev->string, "\r"))
79 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
80 else if (!strcmp(ev->string, "\033"))
97 while (*c >= '0' && *c <= '9')
98 m = 10*m + *c++ - '0';
117 on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
119 if (togglebutton->active)
122 strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
123 t = parse_time(old_text);
126 gtk_toggle_button_set_active(togglebutton, 0);
129 alarm_time = time(NULL) + t;
130 gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
131 on_second_timeout(NULL);
132 second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
138 gtk_timeout_remove(second_timer);
141 gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
142 gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
143 gtk_widget_grab_focus(timebox);
148 on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
156 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
157 gtk_window_set_title(GTK_WINDOW (win), "Tea Timer");
158 gtk_window_set_policy(GTK_WINDOW (win), TRUE, TRUE, TRUE);
160 hbox1 = gtk_hbox_new(FALSE, 0);
161 gtk_widget_show(hbox1);
162 gtk_container_add(GTK_CONTAINER (win), hbox1);
164 timebox = gtk_entry_new_with_max_length(9);
165 gtk_widget_show(timebox);
166 gtk_box_pack_start(GTK_BOX(hbox1), timebox, TRUE, TRUE, 0);
167 gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
169 togglebutton1 = gtk_toggle_button_new_with_label("Run");
170 gtk_widget_show(togglebutton1);
171 gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
173 gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
174 gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_timebox_key), NULL);
175 gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
177 gtk_widget_grab_focus(timebox);
179 gtk_widget_show(win);
182 static const char short_opts[] = "r:";
184 static const struct option long_opts[] = {
185 { "run", required_argument, NULL, 'r' },
186 { NULL, 0, NULL, 0 },
192 fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
194 -r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
200 main(int argc, char **argv)
203 gtk_init(&argc, &argv);
206 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
215 if (optind != argc && optind+1 != argc)
221 gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
222 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);