4 * (c) 2002, 2010 Martin Mares <mj@ucw.cz>
18 #define UNUSED __attribute__((unused))
20 static guint second_timer;
21 static char old_text[8];
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;
63 sprintf(buf, "%s%02d:%02d", sign, delta/60, delta%60);
64 gtk_entry_set_text(GTK_ENTRY(timebox), buf);
65 if (now >= alarm_time)
73 on_timebox_key(GtkWidget *widget UNUSED, GdkEventKey *ev, gpointer user_data UNUSED)
75 if (!strcmp(ev->string, "\r"))
76 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), !GTK_TOGGLE_BUTTON(togglebutton1)->active);
77 else if (!strcmp(ev->string, "\033"))
87 while (*c && *c != ':')
89 if (*c >= '0' && *c <= '9')
90 t = 10*t + *c++ - '0';
100 if (*c >= '0' && *c <= '9')
101 m = 10*m + *c++ - '0';
113 on_togglebutton1_toggled(GtkToggleButton *togglebutton, gpointer user_data UNUSED)
115 if (togglebutton->active)
118 strcpy(old_text, gtk_entry_get_text(GTK_ENTRY(timebox)));
119 t = parse_time(old_text);
122 gtk_toggle_button_set_active(togglebutton, 0);
125 alarm_time = time(NULL) + t;
126 gtk_entry_set_editable(GTK_ENTRY(timebox), 0);
127 on_second_timeout(NULL);
128 second_timer = gtk_timeout_add(1000, on_second_timeout, NULL);
134 gtk_timeout_remove(second_timer);
137 gtk_entry_set_text(GTK_ENTRY(timebox), old_text);
138 gtk_entry_set_editable(GTK_ENTRY(timebox), 1);
139 gtk_widget_grab_focus(timebox);
144 on_window_remove(GtkContainer *container UNUSED, GtkWidget *widget UNUSED, gpointer user_data UNUSED)
152 win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
153 gtk_window_set_title(GTK_WINDOW (win), "Tea Timer");
154 gtk_window_set_policy(GTK_WINDOW (win), TRUE, TRUE, TRUE);
156 hbox1 = gtk_hbox_new(FALSE, 0);
157 gtk_widget_show(hbox1);
158 gtk_container_add(GTK_CONTAINER (win), hbox1);
160 timebox = gtk_entry_new_with_max_length(6);
161 gtk_widget_show(timebox);
162 gtk_box_pack_start(GTK_BOX(hbox1), timebox, TRUE, TRUE, 0);
163 gtk_entry_set_text(GTK_ENTRY(timebox), "00:00");
165 togglebutton1 = gtk_toggle_button_new_with_label("Run");
166 gtk_widget_show(togglebutton1);
167 gtk_box_pack_start(GTK_BOX(hbox1), togglebutton1, FALSE, FALSE, 0);
169 gtk_signal_connect(GTK_OBJECT(win), "remove", GTK_SIGNAL_FUNC(on_window_remove), NULL);
170 gtk_signal_connect(GTK_OBJECT(timebox), "key_press_event", GTK_SIGNAL_FUNC(on_timebox_key), NULL);
171 gtk_signal_connect(GTK_OBJECT(togglebutton1), "toggled", GTK_SIGNAL_FUNC(on_togglebutton1_toggled), NULL);
173 gtk_widget_grab_focus(timebox);
175 gtk_widget_show(win);
178 static const char short_opts[] = "r:";
180 static const struct option long_opts[] = {
181 { "run", required_argument, NULL, 'r' },
182 { NULL, 0, NULL, 0 },
188 fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
190 -r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
196 main(int argc, char **argv)
199 gtk_init(&argc, &argv);
202 while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
211 if (optind != argc && optind+1 != argc)
217 gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
218 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);