From 8336b0374b6cb7d97dbc95ad45c667df67c0593d Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Sat, 17 Jul 2010 21:22:31 +0200 Subject: [PATCH] Implemented the `--run' option --- README | 2 +- teatimer.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 59 insertions(+), 6 deletions(-) diff --git a/README b/README index 9711f26..f3b69e1 100644 --- a/README +++ b/README @@ -1,4 +1,4 @@ -Trivial Tea Timer (c) 2002 Martin Mares +Trivial Tea Timer (c) 2002, 2010 Martin Mares This program is free software; you can redistribute it and/or modify diff --git a/teatimer.c b/teatimer.c index bf8d0e3..881a8c0 100644 --- a/teatimer.c +++ b/teatimer.c @@ -1,28 +1,48 @@ /* * Trivial Tea Timer * - * (c) 2002 Martin Mares + * (c) 2002, 2010 Martin Mares * * GPL'ed */ #include #include +#include #include +#include +#include #include #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 @@ -44,6 +64,8 @@ on_second_timeout(gpointer data UNUSED) gtk_entry_set_text(GTK_ENTRY(timebox), buf); if (now >= alarm_time) it_tolls_for_thee(); + else + expired = 0; return 1; } @@ -153,15 +175,46 @@ open_window(void) 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 [] []\n\n\ +Options:\n\ +-r, --run=\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(); -- 2.39.2