diff options
Diffstat (limited to 'src/festivy.c')
-rw-r--r-- | src/festivy.c | 129 |
1 files changed, 129 insertions, 0 deletions
diff --git a/src/festivy.c b/src/festivy.c new file mode 100644 index 0000000..ab6a896 --- /dev/null +++ b/src/festivy.c @@ -0,0 +1,129 @@ +/* + * Festival - Ivy relay + * + * Copyright (C) 2002 + * IntuiLab + * + * Main and only file + * + * Authors: Stéphane Chatty <chatty@intuilab.com> + * + * $Id$ + * + * Please refer to file version.h for the + * copyright notice regarding this software + */ + + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/time.h> +#include <unistd.h> +#include <netinet/in.h> +#include <netdb.h> + +#include "ivyloop.h" +#include "ivysocket.h" +#include "ivy.h" + +const char* default_host = "localhost"; +const default_port = 1314; +int festival_fd; + +int +ServerOpen () +{ + int sock; + static struct sockaddr_in addr; + struct hostent *hp; + const char* host; + const char* port_number; + int port; + + /* determine host name and port */ + host = getenv ("FESTIVAL_HOST"); + if (!host) + host = default_host; + + port_number = getenv ("FESTIVAL_PORT"); + if (port_number) { + port = atoi (port_number); + } else { + port = default_port; + } + + hp = gethostbyname (host); + if (hp == 0) { + fprintf (stderr, "Unknown host %s\n", host); + return 0; + } + + /* create socket */ + sock = socket (AF_INET, SOCK_STREAM, 0); + if (sock < 0) { + perror ("Can't create socket: "); + return 0; + } + + /* build address from host addr and port */ + memcpy (&addr.sin_addr, hp->h_addr, hp->h_length); + addr.sin_family = AF_INET; + addr.sin_port = htons (port); + + /* connect socket on address */ + if (connect (sock, &addr, sizeof (addr)) < 0) { + perror ("Can't connect socket: "); + return 0; + } + + return sock; +} + +void +HandleServerReplies (Channel channel, HANDLE fd, void *data) +{ + /* basically ignore replies... */ + char reply[1024]; + read (festival_fd, reply, 1024); +} + +void +HandleIvyEvents (IvyClientPtr app, void *user_data, int argc, char *argv[]) +{ + write (festival_fd, "(SayText \"", 10); + write (festival_fd, argv[0], strlen (argv[0])); + write (festival_fd, "\")", 2); +} + + +int +main (int argc, char *argv[]) +{ + int c; + char busbuf [1024] = ""; + const char* bus = 0; + + /* handle arguments */ + while ((c = getopt (argc, argv, "b:")) != EOF) + switch (c) { + case 'b': + strcpy (busbuf, optarg); + bus = busbuf; + break; + } + + /* Ivy side */ + IvyInit ("Festivy", "Festivy ready", 0, 0, 0, 0); + IvyBindMsg (HandleIvyEvents, 0, "Say (.*)"); + + /* Festival side */ + festival_fd = ServerOpen (); + IvyChannelSetUp (festival_fd, 0, 0, HandleServerReplies); + + /* Let's get going */ + IvyStart (bus); + IvyMainLoop (0); + + return 0; +} |