4 * (c) 2002, 2010, 2013 Martin Mares <mj@ucw.cz>
5 * (c) 2021 Jiri Kalvoda <jirikalvoda@kam.mff.cuni.cz>
19 #define UNUSED __attribute__((unused))
21 static guint second_timer;
22 static char old_text[16];
23 static GtkWidget *win, *hbox1, *vbox1, *timebox, *namebox, *togglebutton1;
24 static time_t alarm_time;
29 it_tolls_for_thee(void)
36 g_spawn_command_line_async(run_cmd, &err);
39 fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
50 on_second_timeout(gpointer data UNUSED)
53 time_t now = time(NULL);
54 int delta = alarm_time - now;
62 if (delta >= 100*60*60)
63 delta = 100*60*60 - 1;
65 sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
67 sprintf(buf, "%s%02d:%02d:%02d", sign, delta/3600, (delta%3600)/60, delta%60);
68 gtk_entry_set_text(GTK_ENTRY(timebox), buf);
69 if (now >= alarm_time)
77 on_box_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
79 if (!strcmp(ev->string, "\r"))
80 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
81 else if (!strcmp(ev->string, "\033"))
98 while (*c >= '0' && *c <= '9')
99 m = 10*m + *c++ - '0';
118 on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
120 if (togglebutton->active)
123 strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
124 t = parse_time(old_text);
127 gtk_toggle_button_set_active(togglebutton, 0);
130 alarm_time = time(NULL) + t;
131 gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
132 on_second_timeout(NULL);
133 second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
139 gtk_timeout_remove(second_timer);
142 gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
143 gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
144 gtk_widget_grab_focus(timebox);
149 on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
157 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
158 gtk_window_set_title(GTK_WINDOW (win), "Tea Timer");
159 gtk_window_set_policy(GTK_WINDOW (win), TRUE, TRUE, TRUE);
161 hbox1 = gtk_hbox_new(FALSE, 0);
162 gtk_widget_show(hbox1);
163 gtk_container_add(GTK_CONTAINER (win), hbox1);
165 vbox1 = gtk_vbox_new(FALSE, 0);
166 gtk_widget_show(vbox1);
167 gtk_box_pack_start(GTK_BOX(hbox1), vbox1, TRUE, TRUE, 0);
169 namebox = gtk_entry_new_with_max_length(30);
170 gtk_widget_show(namebox);
171 gtk_box_pack_start(GTK_BOX(vbox1), namebox, TRUE, TRUE, 0);
172 gtk_entry_set_text(GTK_ENTRY(namebox), "Tea");
174 timebox = gtk_entry_new_with_max_length(9);
175 gtk_widget_show(timebox);
176 gtk_box_pack_start(GTK_BOX(vbox1), timebox, TRUE, TRUE, 0);
177 gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
179 togglebutton1 = gtk_toggle_button_new_with_label("Run");
180 gtk_widget_show(togglebutton1);
181 gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
183 gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
184 gtk_signal_connect(GTK_OBJECT(namebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
185 gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
186 gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
188 gtk_widget_grab_focus(timebox);
190 // Do not focus button
191 GList *focus_chain = NULL;
192 focus_chain = g_list_append(focus_chain, vbox1);
193 gtk_container_set_focus_chain(GTK_CONTAINER (hbox1), focus_chain);
195 gtk_widget_show(win);
198 static const char short_opts[] = "r:";
200 static const struct option long_opts[] = {
201 { "run", required_argument, NULL, 'r' },
202 { NULL, 0, NULL, 0 },
208 fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
210 -r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
216 main(int argc, char **argv)
219 gtk_init(&argc, &argv);
222 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
231 if (optind != argc && optind+1 != argc)
237 gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
238 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);