applet: print strerror and exit if execlp() fails
[espeaktime] / src / daemon.c
index 87d816f..065c990 100644 (file)
@@ -1,5 +1,6 @@
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
+#include <time.h>
 #include <glib.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
 #include <glib.h>
 #include <dbus/dbus-glib.h>
 #include <dbus/dbus-glib-lowlevel.h>
@@ -18,8 +19,16 @@ struct app_data {
 
        gboolean mode_locked;
        gboolean display_on;
 
        gboolean mode_locked;
        gboolean display_on;
+       time_t last_press;
+       unsigned press_count;
 };
 
 };
 
+static void speak_time(struct app_data *app)
+{
+       int res = system("espeaktime-now.sh");
+       g_debug("speak script: %d", res);
+}
+
 static void sig_tklock_mode(DBusGProxy *proxy, const char *mode, gpointer user_data)
 {
        struct app_data *app = user_data;
 static void sig_tklock_mode(DBusGProxy *proxy, const char *mode, gpointer user_data)
 {
        struct app_data *app = user_data;
@@ -32,6 +41,13 @@ static void sig_display_status(DBusGProxy *proxy, const char *status, gpointer u
        struct app_data *app = user_data;
        g_debug("sig_display_status [%s]", status);
        app->display_on = !strcmp(status, MCE_DISPLAY_ON_STRING);
        struct app_data *app = user_data;
        g_debug("sig_display_status [%s]", status);
        app->display_on = !strcmp(status, MCE_DISPLAY_ON_STRING);
+
+       /* Double-pressing the button at a normal rate will usually result
+        * in the two ButtonPress events being received before the display_status==on
+        * event.  Check here if that's the case.
+        */
+       if (app->display_on && app->press_count > 1 && time(NULL) - app->last_press <= 1)
+               speak_time(app);
 }
 
 static void debug_log(const gchar *log_domain,
 }
 
 static void debug_log(const gchar *log_domain,
@@ -42,11 +58,17 @@ static void debug_log(const gchar *log_domain,
 
 static void power_button(struct app_data *app)
 {
 
 static void power_button(struct app_data *app)
 {
-       g_debug("power button");
-       if (app->mode_locked && app->display_on) {
-               int res = system("espeaktime-now.sh");
-               g_debug("speak script: %d", res);
-       }
+       time_t now;
+
+       time(&now);
+       if (now - app->last_press > 1)
+               app->press_count = 0;
+       app->press_count++;
+       app->last_press = now;
+
+       g_debug("power button: count=%d", app->press_count);
+       if (app->mode_locked && app->display_on)
+               speak_time(app);
 }
 
 /**
 }
 
 /**
@@ -152,7 +174,7 @@ int main(int argc, char *argv[])
        GError *err = NULL;
        struct app_data app;
 
        GError *err = NULL;
        struct app_data app;
 
-       if (argc > 1 && !strcmp(argv[1], "-d"))
+       if (argc > 1 && !strcmp(argv[1], "-v"))
                g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, debug_log, NULL);
 
        g_debug("init");
                g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, debug_log, NULL);
 
        g_debug("init");