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;
26 static char *default_name = "Tea";
30 expand_and_exec(char *cmd)
32 GString *expanded_cmd = g_string_new("");
33 for (int i=0; cmd[i]; i++)
35 if (cmd[i]=='%' && cmd[i+1]=='%')
38 g_string_append_c(expanded_cmd, '%');
41 if (cmd[i]=='%' && cmd[i+1]=='n')
44 const gchar *name = gtk_entry_get_text(GTK_ENTRY(namebox));
45 g_string_append(expanded_cmd, name);
48 g_string_append_c(expanded_cmd, cmd[i]);
52 g_spawn_command_line_async(expanded_cmd->str, &err);
53 g_string_free(expanded_cmd, 1);
56 fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
62 it_tolls_for_thee(void)
68 expand_and_exec(run_cmd);
77 on_second_timeout(gpointer data UNUSED)
80 time_t now = time(NULL);
81 int delta = alarm_time - now;
89 if (delta >= 100*60*60)
90 delta = 100*60*60 - 1;
92 sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
94 sprintf(buf, "%s%02d:%02d:%02d", sign, delta/3600, (delta%3600)/60, delta%60);
95 gtk_entry_set_text(GTK_ENTRY(timebox), buf);
96 if (now >= alarm_time)
104 on_box_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
106 if (!strcmp(ev->string, "\r"))
107 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
108 else if (!strcmp(ev->string, "\033"))
125 while (*c >= '0' && *c <= '9')
126 m = 10*m + *c++ - '0';
145 on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
147 if (togglebutton->active)
150 strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
151 t = parse_time(old_text);
154 gtk_toggle_button_set_active(togglebutton, 0);
157 alarm_time = time(NULL) + t;
158 gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
159 on_second_timeout(NULL);
160 second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
166 gtk_timeout_remove(second_timer);
169 gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
170 gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
171 gtk_widget_grab_focus(timebox);
176 on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
184 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
185 gtk_window_set_title(GTK_WINDOW (win), "Tea Timer");
186 gtk_window_set_policy(GTK_WINDOW (win), TRUE, TRUE, TRUE);
188 hbox1 = gtk_hbox_new(FALSE, 0);
189 gtk_widget_show(hbox1);
190 gtk_container_add(GTK_CONTAINER (win), hbox1);
192 vbox1 = gtk_vbox_new(FALSE, 0);
193 gtk_widget_show(vbox1);
194 gtk_box_pack_start(GTK_BOX(hbox1), vbox1, TRUE, TRUE, 0);
196 namebox = gtk_entry_new_with_max_length(30);
197 gtk_widget_show(namebox);
198 gtk_box_pack_start(GTK_BOX(vbox1), namebox, TRUE, TRUE, 0);
199 gtk_entry_set_text(GTK_ENTRY(namebox), default_name);
201 timebox = gtk_entry_new_with_max_length(9);
202 gtk_widget_show(timebox);
203 gtk_box_pack_start(GTK_BOX(vbox1), timebox, TRUE, TRUE, 0);
204 gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
206 togglebutton1 = gtk_toggle_button_new_with_label("Run");
207 gtk_widget_show(togglebutton1);
208 gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
210 gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
211 gtk_signal_connect(GTK_OBJECT(namebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
212 gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_box_key), NULL);
213 gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
215 gtk_widget_grab_focus(timebox);
217 // Do not focus button
218 GList *focus_chain = NULL;
219 focus_chain = g_list_append(focus_chain, vbox1);
220 gtk_container_set_focus_chain(GTK_CONTAINER (hbox1), focus_chain);
222 gtk_widget_show(win);
225 static const char short_opts[] = "r:n:";
227 static const struct option long_opts[] = {
228 { "run", required_argument, NULL, 'r' },
229 { "timer-name", required_argument, NULL, 'n' },
230 { NULL, 0, NULL, 0 },
236 fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
238 -r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
239 \t\t\t\t%%d will be expanded to timer name\n\
240 \t\t\t\t%%%% will be expanded to %%\n\
241 -n, --timer-name=<str>\tFill name box with <str>\n\
247 main(int argc, char **argv)
250 gtk_init(&argc, &argv);
253 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
260 default_name = optarg;
265 if (optind != argc && optind+1 != argc)
271 gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
272 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);