+
+static void
+simple_at_exit()
+{
+ deregisterGlobalEventListenerAll (focus_listener);
+ deregisterGlobalEventListenerAll (property_listener);
+ deregisterGlobalEventListenerAll (button_listener);
+ deregisterKeystrokeListener (key_listener, KEYMASK_ALT );
+
+ SPI_exit ();
+}
+
+static boolean
+is_command_key (KeyStroke *key)
+{
+ switch (key->keyID)
+ {
+ case 'Q':
+ case 'q':
+ simple_at_exit();
+ return TRUE; /* not reached */
+ case 'M':
+ case 'm':
+ use_magnifier = ! use_magnifier;
+ return TRUE;
+ case 'F':
+ case 'f':
+ use_festival = ! use_festival;
+ return TRUE;
+ default:
+ return FALSE;
+ }
+}
+
+static boolean
+report_key_event (void *p)
+{
+ KeyStroke *key = (KeyStroke *) p;
+ fprintf(stderr, "KeyEvent %s%c (keycode %d)\n",
+ (key->modifiers & KEYMASK_ALT)?"Alt-":"",
+ ((key->modifiers & KEYMASK_SHIFT)^(key->modifiers & KEYMASK_SHIFTLOCK))?
+ (char) toupper((int) key->keyID) : (char) tolower((int) key->keyID),
+ (int) key->keycode);
+ return is_command_key (key);
+}
+
+static int _festival_init ()
+{
+ int fd;
+ struct sockaddr_in name;
+ int tries = 2;
+
+ name.sin_family = AF_INET;
+ name.sin_port = htons (1314);
+ name.sin_addr.s_addr = htonl(INADDR_ANY);
+ fd = socket (PF_INET, SOCK_STREAM, 0);
+
+ while (connect(fd, (struct sockaddr *) &name, sizeof (name)) < 0) {
+ if (!tries--) {
+ perror ("connect");
+ return -1;
+ }
+ }
+
+ _festival_write ("(audio_mode'async)\n", fd);
+ _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
+ _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
+ return fd;
+}
+
+static void _festival_say (const char *text, const char *voice, boolean shutup)
+{
+ static int fd = 0;
+ gchar *quoted;
+ gchar *p;
+ gchar prefix[50];
+ static gchar voice_spec[32];
+
+ if (!fd)
+ {
+ fd = _festival_init ();
+ }
+
+ fprintf (stderr, "saying text: %s\n", text);
+
+ quoted = g_malloc(64+strlen(text)*2);
+
+ sprintf (prefix, "(SayText \"");
+
+ strncpy(quoted, prefix, 10);
+ p = quoted+strlen(prefix);
+ while (*text) {
+ if ( *text == '\\' || *text == '"' )
+ *p = '\\';
+ *p++ = *text++;
+ }
+ *p++ = '"';
+ *p++ = ')';
+ *p++ = '\n';
+ *p = 0;
+
+ if (shutup) _festival_write ("(audio_mode'shutup)\n", fd);
+ if (voice && (strncmp (voice, (char *) (voice_spec+1), strlen(voice))))
+ {
+ snprintf (voice_spec, 32, "(%s)\n", voice);
+ _festival_write (voice_spec, fd);
+ _festival_write ("(Parameter.set 'Duration_Model 'Tree_ZScore)\n", fd);
+ _festival_write ("(Parameter.set 'Duration_Stretch 0.75)\n", fd);
+ }
+
+ _festival_write (quoted, fd);
+
+ g_free(quoted);
+}
+
+static void _festival_write (const gchar *command_string, int fd)
+{
+ fprintf(stderr, command_string);
+ if (fd < 0) {
+ perror("socket");
+ return;
+ }
+ write(fd, command_string, strlen(command_string));
+}
+