applet: print strerror and exit if execlp() fails
[espeaktime] / src / applet.c
index f9426df..9659081 100644 (file)
@@ -1,6 +1,13 @@
+#include <unistd.h>
+#include <errno.h>
 #include <gtk/gtk.h>
 #include <hildon/hildon.h>
 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
+#include <gconf/gconf.h>
+#include <gconf/gconf-client.h>
+
+#define ESPEAK_BIN "espeak"
+#define GCONF_KEY "/apps/Maemo/espeaktime"
 
 /* TODO: read these from disk */
 
@@ -67,6 +74,96 @@ static const struct effect {
 };
 static const int num_effects = sizeof(effects) / sizeof(effects[0]);
 
+struct espeaktime_settings {
+       gchar *voice;
+       gchar *effect;
+       gchar *text;
+       gint amplitude;
+       gint pitch;
+       gint speed;
+       gboolean ignore_silent;
+};
+
+static void cfg_read(GConfClient *client, struct espeaktime_settings *cfg)
+{
+       cfg->voice = gconf_client_get_string(client, GCONF_KEY "/voice", NULL);
+       cfg->effect = gconf_client_get_string(client, GCONF_KEY "/effect", NULL);
+       cfg->text = gconf_client_get_string(client, GCONF_KEY "/text", NULL);
+       cfg->amplitude = gconf_client_get_int(client, GCONF_KEY "/amplitude", NULL);
+       cfg->pitch = gconf_client_get_int(client, GCONF_KEY "/pitch", NULL);
+       cfg->speed = gconf_client_get_int(client, GCONF_KEY "/speed", NULL);
+       cfg->ignore_silent = gconf_client_get_bool(client, GCONF_KEY "/ignore_silent", NULL);
+       if (!cfg->voice)
+               cfg->voice = g_strdup("en-us");
+       if (!cfg->effect)
+               cfg->effect = g_strdup("");
+       if (!cfg->text)
+               cfg->text = g_strdup("%H:%M");
+       if (!cfg->amplitude)
+               cfg->amplitude = 100;
+       if (!cfg->pitch)
+               cfg->pitch = 50;
+       if (!cfg->speed)
+               cfg->speed = 170;
+       /* TODO: default ignore_silent to TRUE */
+}
+
+static void cfg_write(GConfClient *client, struct espeaktime_settings *cfg)
+{
+       gconf_client_add_dir(client, GCONF_KEY, GCONF_CLIENT_PRELOAD_NONE, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/voice", cfg->voice, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/effect", cfg->effect, NULL);
+       gconf_client_set_string(client, GCONF_KEY "/text", cfg->text, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/amplitude", cfg->amplitude, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/pitch", cfg->pitch, NULL);
+       gconf_client_set_int(client, GCONF_KEY "/speed", cfg->speed, NULL);
+       gconf_client_set_bool(client, GCONF_KEY "/ignore_silent", cfg->ignore_silent, NULL);
+}
+
+static void cfg_free(struct espeaktime_settings *cfg)
+{
+       g_free(cfg->voice);
+       g_free(cfg->effect);
+       g_free(cfg->text);
+}
+
+static void cfg_speak(struct espeaktime_settings *cfg, gboolean test_mode)
+{
+       gchar astr[16], pstr[16], sstr[16];
+       gchar vstr[64];
+       gchar text[4096];
+       time_t t;
+       struct tm *tm;
+       int res;
+       pid_t pid;
+
+       time(&t);
+       tm = localtime(&t);
+
+       g_snprintf(astr, sizeof(astr), "%d", cfg->amplitude);
+       g_snprintf(pstr, sizeof(pstr), "%d", cfg->pitch);
+       g_snprintf(sstr, sizeof(sstr), "%d", cfg->speed);
+       g_snprintf(vstr, sizeof(vstr), "%s%s%s", cfg->voice,
+               (*cfg->effect) ? "+" : "", cfg->effect);
+       strftime(text, sizeof(text), cfg->text, tm);
+
+       pid = fork();
+       if (pid < 0) {
+               perror("fork");
+               return;
+       }
+       if (pid)
+               return;
+       g_print("execlp: -a '%s' -p '%s' -s '%s' -v '%s' '%s'\n",
+               astr, pstr, sstr, vstr, text);
+       res = execlp(ESPEAK_BIN, ESPEAK_BIN,
+               "-a", astr, "-p", pstr, "-s", sstr, "-v", vstr,
+               text, NULL);
+       g_print("execlp: %d (%s)\n", res, g_strerror(errno));
+       exit(res);
+}
+
+
 static void add_scale(GtkVBox *vbox, GtkSizeGroup *size_group, const char *caption, GtkAdjustment *adjustment)
 {
        GtkWidget *scale = gtk_hscale_new(adjustment);
@@ -76,26 +173,54 @@ static void add_scale(GtkVBox *vbox, GtkSizeGroup *size_group, const char *capti
                FALSE, FALSE, 0);
 }
 
-static GtkWidget *build_ui(void)
+osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
 {
-       int k;
+       struct espeaktime_settings cfg = {
+               .voice = "en-us",
+               .effect = "",
+               .amplitude = 100,
+               .pitch = 50,
+               .speed = 170,
+               .ignore_silent = TRUE,
+       };
+
+       GConfClient *client = gconf_client_get_default();
+       cfg_read(client, &cfg);
+
+       GtkWidget *dialog;
+       dialog = gtk_dialog_new_with_buttons(
+               "eSpeakTime Settings",
+               GTK_WINDOW(data),
+               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
+               "Test", 1,
+               GTK_STOCK_SAVE, GTK_RESPONSE_OK,
+               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+               NULL);
+
+       int k, voice_sel = 0, effect_sel = 0;
        GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(FALSE, 0));
        GtkSizeGroup *title_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
        GtkSizeGroup *value_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
 
        /* selectors */
        HildonTouchSelector *voice_selector = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
-       for (k = 0; k < num_voices; k++)
+       for (k = 0; k < num_voices; k++) {
                hildon_touch_selector_append_text(voice_selector, voices[k].name);
+               if (!strcmp(voices[k].id, cfg.voice))
+                       voice_sel = k;
+       }
 
        HildonTouchSelector *effect_selector = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
-       for (k = 0; k < num_effects; k++)
+       for (k = 0; k < num_effects; k++) {
                hildon_touch_selector_append_text(effect_selector, effects[k].name);
+               if (!strcmp(effects[k].id, cfg.effect))
+                       effect_sel = k;
+       }
 
        HildonPickerButton *voice = HILDON_PICKER_BUTTON(hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
        hildon_button_set_title(HILDON_BUTTON(voice), "Voice");
        hildon_picker_button_set_selector(voice, voice_selector);
-       hildon_picker_button_set_active(voice, 0);
+       hildon_picker_button_set_active(voice, voice_sel);
        hildon_button_add_size_groups(HILDON_BUTTON(voice), title_group, value_group, NULL);
        gtk_button_set_alignment(GTK_BUTTON(voice), 0.0, 0.5);
        gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(voice), FALSE, FALSE, 0);
@@ -103,64 +228,68 @@ static GtkWidget *build_ui(void)
        HildonPickerButton *effect = HILDON_PICKER_BUTTON(hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
        hildon_button_set_title(HILDON_BUTTON(effect), "Effect");
        hildon_picker_button_set_selector(effect, effect_selector);
-       hildon_picker_button_set_active(effect, 0);
+       hildon_picker_button_set_active(effect, effect_sel);
        hildon_button_add_size_groups(HILDON_BUTTON(effect), title_group, value_group, NULL);
        gtk_button_set_alignment(GTK_BUTTON(effect), 0.0, 0.5);
        gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(effect), FALSE, FALSE, 0);
 
        HildonEntry *text = HILDON_ENTRY(hildon_entry_new(HILDON_SIZE_AUTO));
+       gtk_entry_set_text(GTK_ENTRY(text), "%H:%M");
        gtk_box_pack_start(GTK_BOX(vbox),
                hildon_caption_new(title_group, "Speech string", GTK_WIDGET(text), NULL, HILDON_CAPTION_MANDATORY),
                FALSE, FALSE, 0);
 
-       GtkAdjustment *amplitude_adj = GTK_ADJUSTMENT(gtk_adjustment_new(100, 0, 200, 1, 10, 0));
+       GtkAdjustment *amplitude_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.amplitude, 0, 200, 1, 10, 0));
        add_scale(vbox, title_group, "Amplitude", amplitude_adj);
 
-       GtkAdjustment *pitch_adj = GTK_ADJUSTMENT(gtk_adjustment_new(50, 00, 99, 1, 10, 0));
+       GtkAdjustment *pitch_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.pitch, 00, 99, 1, 10, 0));
        add_scale(vbox, title_group, "Pitch", pitch_adj);
 
-       GtkAdjustment *speed_adj = GTK_ADJUSTMENT(gtk_adjustment_new(170, 80, 390, 1, 10, 0));
+       GtkAdjustment *speed_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.speed, 80, 390, 1, 10, 0));
        add_scale(vbox, title_group, "Words per minute", speed_adj);
 
-       GtkWidget *ignore_silent = hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT);
+       HildonCheckButton *ignore_silent = HILDON_CHECK_BUTTON(hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT));
+       hildon_check_button_set_active(ignore_silent, cfg.ignore_silent);
        gtk_button_set_label(GTK_BUTTON(ignore_silent), "Ignore silent profile");
-       gtk_box_pack_start(GTK_BOX(vbox), ignore_silent, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(ignore_silent), FALSE, FALSE, 0);
 
        GtkWidget *panarea = hildon_pannable_area_new();
        gtk_widget_set_size_request(panarea, -1, 800);
        hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(panarea), GTK_WIDGET(vbox));
-       return panarea;
-}
-
-osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
-{
-       GtkWidget *dialog;
 
-       dialog = gtk_dialog_new_with_buttons(
-               "eSpeakTime Settings",
-               GTK_WINDOW(data),
-               GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
-               "Test", 1,
-               GTK_STOCK_SAVE, GTK_RESPONSE_OK,
-               GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
-               NULL);
-
-       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), build_ui());
+       gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panarea);
        gtk_widget_show_all(dialog);
        while (1) {
                int result = gtk_dialog_run(GTK_DIALOG(dialog));
+
+               g_free(cfg.voice);
+               g_free(cfg.effect);
+               g_free(cfg.text);
+               cfg.voice = g_strdup(voices[hildon_picker_button_get_active(voice)].id);
+               cfg.effect = g_strdup(effects[hildon_picker_button_get_active(effect)].id);
+               cfg.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(text)));
+               cfg.amplitude = gtk_adjustment_get_value(amplitude_adj);
+               cfg.pitch = gtk_adjustment_get_value(pitch_adj);
+               cfg.speed = gtk_adjustment_get_value(speed_adj);
+               cfg.ignore_silent = hildon_check_button_get_active(ignore_silent);
+
                switch (result) {
                case 1:
                        g_print("Test button\n");
+                       cfg_speak(&cfg, TRUE);
                        continue;
                case GTK_RESPONSE_OK:
                        g_print("Save\n");
+                       cfg_write(client, &cfg);
                        break;
                }
                break;
        }
        gtk_widget_destroy(GTK_WIDGET(dialog));
 
+       cfg_free(&cfg);
+       g_object_unref(G_OBJECT(client));
+
        return OSSO_OK;
 }