/*
* Trivial Tea Timer
*
- * (c) 2002 Martin Mares <mj@ucw.cz>
+ * (c) 2002, 2010 Martin Mares <mj@ucw.cz>
*
* GPL'ed
*/
#include <stdio.h>
#include <string.h>
+#include <stdlib.h>
#include <time.h>
+#include <getopt.h>
+#include <glib.h>
#include <gtk/gtk.h>
#define UNUSED __attribute__((unused))
static guint second_timer;
-static unsigned char old_text[8];
+static char old_text[8];
static GtkWidget *win, *hbox1, *timebox, *togglebutton1;
static time_t alarm_time;
+static char *run_cmd;
+static int expired;
static void
it_tolls_for_thee(void)
{
- gdk_beep();
+ if (run_cmd)
+ {
+ if (!expired)
+ {
+ GError *err = NULL;
+ g_spawn_command_line_async(run_cmd, &err);
+ if (err)
+ {
+ fprintf(stderr, "teatimer: Unable to run command: %s\n", err->message);
+ g_error_free(err);
+ }
+ expired = 1;
+ }
+ }
+ else
+ gdk_beep();
}
static gint
gtk_entry_set_text(GTK_ENTRY(timebox), buf);
if (now >= alarm_time)
it_tolls_for_thee();
+ else
+ expired = 0;
return 1;
}
gtk_widget_show(win);
}
+static const char short_opts[] = "r:";
+
+static const struct option long_opts[] = {
+ { "run", required_argument, NULL, 'r' },
+ { NULL, 0, NULL, 0 },
+};
+
+static void
+usage(void)
+{
+ fprintf(stderr, "Usage: teatimer [<options>] [<mm:ss>]\n\n\
+Options:\n\
+-r, --run=<cmd>\t\tRun a given program when the tea is ready\n\
+");
+ exit(1);
+}
+
int
main(int argc, char **argv)
{
gtk_set_locale();
gtk_init(&argc, &argv);
+
+ int opt;
+ while ((opt = getopt_long(argc, argv, short_opts, long_opts, NULL)) >= 0)
+ switch (opt)
+ {
+ case 'r':
+ run_cmd = optarg;
+ break;
+ default:
+ usage();
+ }
+ if (optind != argc && optind+1 != argc)
+ usage();
+
open_window();
- if (argc > 1)
+ if (optind < argc)
{
- gtk_entry_set_text(GTK_ENTRY(timebox), argv[1]);
+ gtk_entry_set_text(GTK_ENTRY(timebox), argv[optind]);
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(togglebutton1), 1);
}
gtk_main();