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 expand_and_exec(char *cmd)
31 GString * expanded_cmd = g_string_new("");
32 for (int i=0; cmd[i]; i++)
34 if (cmd[i]=='%' && cmd[i+1]=='%')
37 g_string_append_c(expanded_cmd, '%');
40 if (cmd[i]=='%' && cmd[i+1]=='n')
43 const gchar * name = gtk_entry_get_text(GTK_ENTRY(namebox));
44 g_string_append(expanded_cmd, name);
47 g_string_append_c(expanded_cmd, cmd[i]);
51 g_spawn_command_line_async(expanded_cmd->str, &err);
52 g_string_free(expanded_cmd, 1);
55 fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
61 it_tolls_for_thee(void)
67 expand_and_exec(run_cmd);
76 on_second_timeout(gpointer data UNUSED)
79 time_t now = time(NULL);
80 int delta = alarm_time - now;
88 if (delta >= 100*60*60)
89 delta = 100*60*60 - 1;
91 sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
93 sprintf(buf, "%s%02d:%02d:%02d", sign, delta/3600, (delta%3600)/60, delta%60);
94 gtk_entry_set_text(GTK_ENTRY(timebox), buf);
95 if (now >= alarm_time)
103 on_box_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
105 if (!strcmp(ev->string, "\r"))
106 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
107 else if (!strcmp(ev->string, "\033"))
124 while (*c >= '0' && *c <= '9')
125 m = 10*m + *c++ - '0';
144 on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
146 if (togglebutton->active)
149 strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
150 t = parse_time(old_text);
153 gtk_toggle_button_set_active(togglebutton, 0);
156 alarm_time = time(NULL) + t;
157 gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
158 on_second_timeout(NULL);
159 second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
165 gtk_timeout_remove(second_timer);
168 gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
169 gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
170 gtk_widget_grab_focus(timebox);
175 on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
183 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
184 gtk_window_set_title(GTK_WINDOW (win), "Tea Timer");
185 gtk_window_set_policy(GTK_WINDOW (win), TRUE, TRUE, TRUE);
187 hbox1 = gtk_hbox_new(FALSE, 0);
188 gtk_widget_show(hbox1);
189 gtk_container_add(GTK_CONTAINER (win), hbox1);
191 vbox1 = gtk_vbox_new(FALSE, 0);
192 gtk_widget_show(vbox1);
193 gtk_box_pack_start(GTK_BOX(hbox1), vbox1, TRUE, TRUE, 0);
195 namebox = gtk_entry_new_with_max_length(30);
196 gtk_widget_show(namebox);
197 gtk_box_pack_start(GTK_BOX(vbox1), namebox, TRUE, TRUE, 0);
198 gtk_entry_set_text(GTK_ENTRY(namebox), "Tea");
200 timebox = gtk_entry_new_with_max_length(9);
201 gtk_widget_show(timebox);
202 gtk_box_pack_start(GTK_BOX(vbox1), timebox, TRUE, TRUE, 0);
203 gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
205 togglebutton1 = gtk_toggle_button_new_with_label("Run");
206 gtk_widget_show(togglebutton1);
207 gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
209 gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
210 gtk_signal_connect(GTK_OBJECT(namebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
211 gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
212 gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
214 gtk_widget_grab_focus(timebox);
216 // Do not focus button
217 GList *focus_chain = NULL;
218 focus_chain = g_list_append(focus_chain, vbox1);
219 gtk_container_set_focus_chain(GTK_CONTAINER (hbox1), focus_chain);
221 gtk_widget_show(win);
224 static const char short_opts[] = "r:";
226 static const struct option long_opts[] = {
227 { "run", required_argument, NULL, 'r' },
228 { NULL, 0, NULL, 0 },
234 fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
236 -r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
237 \t\t\t\t%%d will be expanded to timer name\n\
238 \t\t\t\t%%%% will be expanded to %%\n\
244 main(int argc, char **argv)
247 gtk_init(&argc, &argv);
250 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
259 if (optind != argc && optind+1 != argc)
265 gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
266 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);