]> mj.ucw.cz Git - teatimer.git/commitdiff
Implemented the `--run' option
authorMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 19:22:31 +0000 (21:22 +0200)
committerMartin Mares <mj@ucw.cz>
Sat, 17 Jul 2010 19:22:31 +0000 (21:22 +0200)
README
teatimer.c

diff --git a/README b/README
index 9711f264e74b4c247986b6dd28882ae427a6d38a..f3b69e1044ab54ae30d53109e4fdcf85e561d59d 100644 (file)
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-Trivial Tea Timer (c) 2002 Martin Mares <mj@ucw.cz>
+Trivial Tea Timer (c) 2002, 2010 Martin Mares <mj@ucw.cz>
 
 
 This program is free software; you can redistribute it and/or modify
index bf8d0e37b8f3fed40a28c0a26a3b8e1e40bfddb3..881a8c0fbf2e9c37666b42fd6a36102229ad64c8 100644 (file)
@@ -1,28 +1,48 @@
 /*
  *     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
@@ -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 [<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();