applet: call espeaktime-now.sh instead of espeak directly
[espeaktime] / src / applet.c
1 #include <unistd.h>
2 #include <errno.h>
3 #include <gtk/gtk.h>
4 #include <hildon/hildon.h>
5 #include <hildon-cp-plugin/hildon-cp-plugin-interface.h>
6 #include <gconf/gconf.h>
7 #include <gconf/gconf-client.h>
8
9 #define ESPEAKTIME_BIN "espeaktime-now.sh"
10 #define GCONF_KEY "/apps/Maemo/espeaktime"
11
12 /* TODO: read these from disk */
13
14 static const struct voice {
15         const char *id;
16         const char *name;
17 } voices[] = {
18         { "",           "Default" },
19         { "en",         "English" },
20         { "en-us",      "English (American)" },
21         { "en-sc",      "English (Scottish)" },
22         { "af",         "Afrikaans" },
23         { "bs",         "Bosnian" },
24         { "ca",         "Catalan" },
25         { "cs",         "Czech" },
26         { "de",         "German" },
27         { "el",         "Greek" },
28         { "eo",         "Esperanto" },
29         { "es",         "Spanish" },
30         { "es-la",      "Spanish (Latin America)" },
31         { "fi",         "Finnish" },
32         { "fr",         "French" },
33         { "hr",         "Croatian" },
34         { "hu",         "Hungarian" },
35         { "it",         "Italian" },
36         { "ku",         "Kurdish" },
37         { "lv",         "Latvian" },
38         { "pl",         "Polish" },
39         { "pt",         "Portuguese (Brazil)" },
40         { "pt-pt",      "Portuguese (European)" },
41         { "ro",         "Romanian" },
42         { "sk",         "Slovak" },
43         { "sr",         "Serbian" },
44         { "sv",         "Swedish" },
45         { "sw",         "Swahihi" },
46         { "ta",         "Tamil" },
47         { "tr",         "Turkish" },
48         { "zh",         "Mandarin Chinese" },
49 };
50 static const int num_voices = sizeof(voices) / sizeof(voices[0]);
51
52 static const struct effect {
53         const char *id;
54         const char *name;
55 } effects[] = {
56         { "",           "(none)" },
57         { "f1",         "Female 1" },
58         { "f2",         "Female 2" },
59         { "f3",         "Female 3" },
60         { "f4",         "Female 4" },
61         { "m1",         "Male 1" },
62         { "m2",         "Male 2" },
63         { "m3",         "Male 3" },
64         { "m4",         "Male 4" },
65         { "m5",         "Male 5" },
66         { "m6",         "Male 6" },
67         { "m7",         "Male 7" },
68         { "croak",      "Croak" },
69         { "fast",       "Fast" },
70         { "klatt",      "Klatt" },
71         { "klatt2",     "Klatt 2" },
72         { "klatt3",     "Klatt 3" },
73         { "whisper",    "Whisper" },
74 };
75 static const int num_effects = sizeof(effects) / sizeof(effects[0]);
76
77 struct espeaktime_settings {
78         gchar *voice;
79         gchar *effect;
80         gchar *text;
81         gint amplitude;
82         gint pitch;
83         gint speed;
84         gboolean ignore_silent;
85 };
86
87 static void cfg_read(GConfClient *client, struct espeaktime_settings *cfg)
88 {
89         cfg->voice = gconf_client_get_string(client, GCONF_KEY "/voice", NULL);
90         cfg->effect = gconf_client_get_string(client, GCONF_KEY "/effect", NULL);
91         cfg->text = gconf_client_get_string(client, GCONF_KEY "/text", NULL);
92         cfg->amplitude = gconf_client_get_int(client, GCONF_KEY "/amplitude", NULL);
93         cfg->pitch = gconf_client_get_int(client, GCONF_KEY "/pitch", NULL);
94         cfg->speed = gconf_client_get_int(client, GCONF_KEY "/speed", NULL);
95         cfg->ignore_silent = gconf_client_get_bool(client, GCONF_KEY "/ignore_silent", NULL);
96         if (!cfg->voice)
97                 cfg->voice = g_strdup("en-us");
98         if (!cfg->effect)
99                 cfg->effect = g_strdup("");
100         if (!cfg->text)
101                 cfg->text = g_strdup("%H:%M");
102         if (!cfg->amplitude)
103                 cfg->amplitude = 100;
104         if (!cfg->pitch)
105                 cfg->pitch = 50;
106         if (!cfg->speed)
107                 cfg->speed = 170;
108         /* TODO: default ignore_silent to TRUE */
109 }
110
111 static void cfg_write(GConfClient *client, struct espeaktime_settings *cfg)
112 {
113         gconf_client_add_dir(client, GCONF_KEY, GCONF_CLIENT_PRELOAD_NONE, NULL);
114         gconf_client_set_string(client, GCONF_KEY "/voice", cfg->voice, NULL);
115         gconf_client_set_string(client, GCONF_KEY "/effect", cfg->effect, NULL);
116         gconf_client_set_string(client, GCONF_KEY "/text", cfg->text, NULL);
117         gconf_client_set_int(client, GCONF_KEY "/amplitude", cfg->amplitude, NULL);
118         gconf_client_set_int(client, GCONF_KEY "/pitch", cfg->pitch, NULL);
119         gconf_client_set_int(client, GCONF_KEY "/speed", cfg->speed, NULL);
120         gconf_client_set_bool(client, GCONF_KEY "/ignore_silent", cfg->ignore_silent, NULL);
121 }
122
123 static void cfg_free(struct espeaktime_settings *cfg)
124 {
125         g_free(cfg->voice);
126         g_free(cfg->effect);
127         g_free(cfg->text);
128 }
129
130 static void cfg_speak(struct espeaktime_settings *cfg, gboolean test_mode)
131 {
132         gchar astr[16], pstr[16], sstr[16];
133         gchar vstr[64];
134         gchar text[4096];
135         time_t t;
136         struct tm *tm;
137         int res;
138         pid_t pid;
139
140         time(&t);
141         tm = localtime(&t);
142
143         g_snprintf(astr, sizeof(astr), "%d", cfg->amplitude);
144         g_snprintf(pstr, sizeof(pstr), "%d", cfg->pitch);
145         g_snprintf(sstr, sizeof(sstr), "%d", cfg->speed);
146         g_snprintf(vstr, sizeof(vstr), "%s%s%s", cfg->voice,
147                 (*cfg->effect) ? "+" : "", cfg->effect);
148         strftime(text, sizeof(text), cfg->text, tm);
149
150         setenv("EST_TESTMODE", test_mode ? "1" : "0", 1);
151         setenv("EST_IGN_SILENT", cfg->ignore_silent ? "1" : "0", 1);
152
153         pid = fork();
154         if (pid < 0) {
155                 perror("fork");
156                 return;
157         }
158         if (pid)
159                 return;
160         g_print("execlp: -a '%s' -p '%s' -s '%s' -v '%s' '%s'\n",
161                 astr, pstr, sstr, vstr, text);
162         res = execlp(ESPEAKTIME_BIN, ESPEAKTIME_BIN,
163                 "-a", astr, "-p", pstr, "-s", sstr, "-v", vstr,
164                 text, NULL);
165         g_print("execlp: %d (%s)\n", res, g_strerror(errno));
166         exit(res);
167 }
168
169
170 static void add_scale(GtkVBox *vbox, GtkSizeGroup *size_group, const char *caption, GtkAdjustment *adjustment)
171 {
172         GtkWidget *scale = gtk_hscale_new(adjustment);
173         gtk_scale_set_digits(GTK_SCALE(scale), 0);
174         gtk_box_pack_start(GTK_BOX(vbox),
175                 hildon_caption_new(size_group, caption, scale, NULL, HILDON_CAPTION_MANDATORY),
176                 FALSE, FALSE, 0);
177 }
178
179 osso_return_t execute(osso_context_t *osso, gpointer data, gboolean user_activated)
180 {
181         struct espeaktime_settings cfg = {
182                 .voice = "en-us",
183                 .effect = "",
184                 .amplitude = 100,
185                 .pitch = 50,
186                 .speed = 170,
187                 .ignore_silent = TRUE,
188         };
189
190         GConfClient *client = gconf_client_get_default();
191         cfg_read(client, &cfg);
192
193         GtkWidget *dialog;
194         dialog = gtk_dialog_new_with_buttons(
195                 "eSpeakTime Settings",
196                 GTK_WINDOW(data),
197                 GTK_DIALOG_MODAL | GTK_DIALOG_NO_SEPARATOR,
198                 "Test", 1,
199                 GTK_STOCK_SAVE, GTK_RESPONSE_OK,
200                 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
201                 NULL);
202
203         int k, voice_sel = 0, effect_sel = 0;
204         GtkVBox *vbox = GTK_VBOX(gtk_vbox_new(FALSE, 0));
205         GtkSizeGroup *title_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
206         GtkSizeGroup *value_group = gtk_size_group_new(GTK_SIZE_GROUP_HORIZONTAL);
207
208         /* selectors */
209         HildonTouchSelector *voice_selector = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
210         for (k = 0; k < num_voices; k++) {
211                 hildon_touch_selector_append_text(voice_selector, voices[k].name);
212                 if (!strcmp(voices[k].id, cfg.voice))
213                         voice_sel = k;
214         }
215
216         HildonTouchSelector *effect_selector = HILDON_TOUCH_SELECTOR(hildon_touch_selector_new_text());
217         for (k = 0; k < num_effects; k++) {
218                 hildon_touch_selector_append_text(effect_selector, effects[k].name);
219                 if (!strcmp(effects[k].id, cfg.effect))
220                         effect_sel = k;
221         }
222
223         HildonPickerButton *voice = HILDON_PICKER_BUTTON(hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
224         hildon_button_set_title(HILDON_BUTTON(voice), "Voice");
225         hildon_picker_button_set_selector(voice, voice_selector);
226         hildon_picker_button_set_active(voice, voice_sel);
227         hildon_button_add_size_groups(HILDON_BUTTON(voice), title_group, value_group, NULL);
228         gtk_button_set_alignment(GTK_BUTTON(voice), 0.0, 0.5);
229         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(voice), FALSE, FALSE, 0);
230
231         HildonPickerButton *effect = HILDON_PICKER_BUTTON(hildon_picker_button_new(HILDON_SIZE_FINGER_HEIGHT, HILDON_BUTTON_ARRANGEMENT_HORIZONTAL));
232         hildon_button_set_title(HILDON_BUTTON(effect), "Effect");
233         hildon_picker_button_set_selector(effect, effect_selector);
234         hildon_picker_button_set_active(effect, effect_sel);
235         hildon_button_add_size_groups(HILDON_BUTTON(effect), title_group, value_group, NULL);
236         gtk_button_set_alignment(GTK_BUTTON(effect), 0.0, 0.5);
237         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(effect), FALSE, FALSE, 0);
238
239         HildonEntry *text = HILDON_ENTRY(hildon_entry_new(HILDON_SIZE_AUTO));
240         gtk_entry_set_text(GTK_ENTRY(text), "%H:%M");
241         gtk_box_pack_start(GTK_BOX(vbox),
242                 hildon_caption_new(title_group, "Speech string", GTK_WIDGET(text), NULL, HILDON_CAPTION_MANDATORY),
243                 FALSE, FALSE, 0);
244
245         GtkAdjustment *amplitude_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.amplitude, 0, 200, 1, 10, 0));
246         add_scale(vbox, title_group, "Amplitude", amplitude_adj);
247
248         GtkAdjustment *pitch_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.pitch, 00, 99, 1, 10, 0));
249         add_scale(vbox, title_group, "Pitch", pitch_adj);
250
251         GtkAdjustment *speed_adj = GTK_ADJUSTMENT(gtk_adjustment_new(cfg.speed, 80, 390, 1, 10, 0));
252         add_scale(vbox, title_group, "Words per minute", speed_adj);
253
254         HildonCheckButton *ignore_silent = HILDON_CHECK_BUTTON(hildon_check_button_new(HILDON_SIZE_FINGER_HEIGHT));
255         hildon_check_button_set_active(ignore_silent, cfg.ignore_silent);
256         gtk_button_set_label(GTK_BUTTON(ignore_silent), "Ignore silent profile");
257         gtk_box_pack_start(GTK_BOX(vbox), GTK_WIDGET(ignore_silent), FALSE, FALSE, 0);
258
259         GtkWidget *panarea = hildon_pannable_area_new();
260         gtk_widget_set_size_request(panarea, -1, 800);
261         hildon_pannable_area_add_with_viewport(HILDON_PANNABLE_AREA(panarea), GTK_WIDGET(vbox));
262
263         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), panarea);
264         gtk_widget_show_all(dialog);
265         while (1) {
266                 int result = gtk_dialog_run(GTK_DIALOG(dialog));
267
268                 g_free(cfg.voice);
269                 g_free(cfg.effect);
270                 g_free(cfg.text);
271                 cfg.voice = g_strdup(voices[hildon_picker_button_get_active(voice)].id);
272                 cfg.effect = g_strdup(effects[hildon_picker_button_get_active(effect)].id);
273                 cfg.text = g_strdup(gtk_entry_get_text(GTK_ENTRY(text)));
274                 cfg.amplitude = gtk_adjustment_get_value(amplitude_adj);
275                 cfg.pitch = gtk_adjustment_get_value(pitch_adj);
276                 cfg.speed = gtk_adjustment_get_value(speed_adj);
277                 cfg.ignore_silent = hildon_check_button_get_active(ignore_silent);
278
279                 switch (result) {
280                 case 1:
281                         g_print("Test button\n");
282                         cfg_speak(&cfg, TRUE);
283                         continue;
284                 case GTK_RESPONSE_OK:
285                         g_print("Save\n");
286                         cfg_write(client, &cfg);
287                         break;
288                 }
289                 break;
290         }
291         gtk_widget_destroy(GTK_WIDGET(dialog));
292
293         cfg_free(&cfg);
294         g_object_unref(G_OBJECT(client));
295
296         return OSSO_OK;
297 }
298
299 osso_return_t save_state(osso_context_t *osso, gpointer data)
300 {
301         g_print("save_state called\n");
302         return OSSO_OK;
303 }
304
305 #ifdef TEST
306 int main(int argc, char *argv[])
307 {
308         gtk_init(&argc, &argv);
309         return execute(NULL, NULL, 0);
310 }
311 #endif