home *** CD-ROM | disk | FTP | other *** search
- /* Title: FileTalk.c
- */
-
- #define TRACE 0
-
- #include "alarm.h"
- #include "wimp.h"
- #include "wimpt.h"
- #include "win.h"
- #include "event.h"
- #include "baricon.h"
- #include "res.h"
- #include "resspr.h"
- #include "menu.h"
- #include "template.h"
- #include "dbox.h"
- #include "werr.h"
- #include "xferrecv.h"
- #include "flex.h"
-
- #include <stdlib.h>
- #include <string.h>
-
- #include "h.DT-Talk"
-
- #define OS_ReadVarVal (0x23)
-
- /* Menu items */
-
- typedef enum {
- menu_info = 1,
- menu_quiet = 2,
- menu_quit = 3
- } iconbar_menu_t;
-
- #define Version_info_field 4
-
- /* Application version */
- static char *Version_string = "1.00 (03 Sep 1990)";
-
- menu iconbar_menu;
- flex_ptr text;
- int text_size;
- int text_len;
- int text_pos;
-
- BOOL LoadCheck(filename, filetype)
-
- char *filename;
- int filetype;
- {
- os_filestr file;
-
- file.action = 5;
- file.name = filename;
- if (wimpt_complain(os_file(&file)) != 0) return(FALSE);
-
- switch (file.action)
- {
- case 0: werr(0, "File not found"); return(FALSE);
- case 2: werr(0, "You cannot load a directory"); return(FALSE);
- }
-
- if ((file.loadaddr & 0xFFF00000) != 0xFFF00000) {
- werr(0, "File cannot be loaded");
- return(FALSE);
- }
-
- if (filetype != 0xFFF) {
- werr(0, "Not a text file");
- return(FALSE);
- }
- return(TRUE);
- }
-
- /* Import function for ram-to-ram transfer. */
-
- BOOL ImportFileProc(char **buf, int *size)
-
- {
- if (flex_extend(text, text_size+1024) == 0) {
- werr(FALSE, "FileTalk has run out of room");
- flex_free(text);
- return(FALSE);
- }
- text_size += 1024;
- *buf += 1024;
- *size = 1024;
- return(TRUE);
- }
-
- BOOL LoadFile(event)
-
- wimp_eventstr *event;
- {
- char *filename;
- int filetype;
- int file_size;
- FILE *in_file;
-
- if (text_size != 0) {
- werr(FALSE, "FileTalk is busy");
- return(FALSE);
- }
-
- filetype = xferrecv_checkinsert(&filename);
- if (filetype == -1) {
- filetype = xferrecv_checkimport(&file_size);
- if (filetype == -1) return(FALSE);
- if (filetype != 0xFFF) {
- werr(FALSE, "Not a text file");
- return(FALSE);
- }
- if (flex_alloc(text, 1024) == 0) {
- werr(FALSE, "FileTalk has run out of room");
- return(FALSE);
- }
- text_size = 1024;
- text_len = xferrecv_doimport(*text, 1024, &ImportFileProc);
-
- if (text_len < 0) {
- werr(FALSE, "Load failed");
- flex_free(text);
- return(FALSE);
- }
- return(TRUE);
- }
-
- if (LoadCheck(filename, filetype)) {
- in_file = fopen(filename, "r");
- if (in_file == NULL) {
- werr(0, "File could not be opened");
- return(FALSE);
- }
- {
- int i = 0;
- while (!feof(in_file)) {
- fgetc(in_file);
- i++;
- }
- if (flex_alloc(text, i) == 0) {
- werr(FALSE, "FileTalk has run out of room");
- return(FALSE);
- }
- text_size = i; text_len = i;
- rewind(in_file);
- i=0;
- while (!feof(in_file)) {
- ((char *)(*text))[i++] = fgetc(in_file);
- }
- fclose(in_file);
- }
- return(TRUE);
- }
- return(FALSE);
- }
-
- /* Speak the file loaded */
-
- FILE *dd;
-
- void SpeakBack(int at, void *handle)
- {
- int i, wst, wend;
- char cd[1024];
- os_regset r;
-
- os_swi2r(VoiceSynth_FIFO | os_X, 0, 0, &(r.r[0]), &(r.r[1]));
- if (r.r[1] < 2) {
- wst = text_pos;
- i = 1;
- while (((char *)(*text))[wst+i] != ' ') i++;
- if (i > 240) {
- werr(FALSE, "Suspect word found in file");
- text_size = 0;
- flex_free(text);
- baricon_newsprite("filetalk");
- return;
- }
- wend = wst+i;
- strcpy(cd, "*say ");
- for (i=0; i<wend-wst; i++) cd[i+5] = ((char *)(*text))[wst+i];
- cd[i+5]=0;
- system(cd);
- text_pos = wend;
- if (text_pos >= text_len) {
- text_size = 0;
- flex_free(text);
- baricon_newsprite("filetalk");
- return;
- }
- }
- alarm_set(alarm_timenow()+20, &SpeakBack, 0);
- }
-
- void SpeakText(void)
- {
- int i, wst;
-
- wst = 0;
- for (i=0;i<text_len; i++) {
- if (((char*)(*text))[i] == 10) ((char *)(*text))[i] = ' ';
- if (isprint(((char *)(*text))[i]))
- ((char*)(*text))[wst++] = ((char *)(*text))[i];
- }
- text_len = wst;
- ((char *)(*text))[text_len] = ' ';
- text_pos = 0;
- alarm_set(alarm_timenow()+5, &SpeakBack, 0);
- baricon_newsprite("filetalking");
- }
-
- /***************************** WINDOW FUNCTIONS ****************************/
-
- /*--- Display the program info box - called from the menu processor. ---*/
-
- void ProgramInfo(void)
- {
- dbox d;
-
- d = dbox_new("info");
- dbox_setfield(d, Version_info_field, Version_string);
- dbox_show(d);
- dbox_fillin(d);
- dbox_dispose(&d);
- }
-
- /*--- Event handler for the menu. ---*/
-
- void MenuProc(void *handle, char *hit)
- {
- handle = handle; /* We don't need the handle: this stops compiler warning */
-
- /* Find which menu item was hit and take action as appropriate */
- switch ((iconbar_menu_t) hit[0])
- {
- case menu_info:
- ProgramInfo();
- break;
-
- case menu_quiet:
- if (text_size > 0) {
- flex_free(text);
- text_size = 0;
- alarm_removeall(0);
- baricon_newsprite("filetalk");
- }
- break;
-
- case menu_quit:
- /* Exit from the program. The wimp gets rid of the window and icon */
- exit(0);
- break;
- }
- }
-
- /*--- Event handler for file drops on the icon ---*/
-
- void IconFileDrop(event, handle)
-
- wimp_eventstr *event;
- void *handle;
- {
- handle = handle;
-
- switch (event->e) {
- case wimp_ESEND:
- case wimp_ESENDWANTACK:
- if (LoadFile(event)) {
- xferrecv_insertfileok();
- SpeakText();
- }
- break;
- }
- }
-
- void IconClick(wimp_i w)
- {
- w=w;
- }
-
- /****************************** INITIALISATION ******************************/
-
- /*--- Initialise the program, returning TRUE if it was all OK. ---*/
-
- BOOL Initialise(void)
- {
- /* WimpLib initialisation */
-
- wimpt_init("FileTalk"); /* Main wimp initialisation */
- res_init("FileTalk"); /* Resources */
- resspr_init(); /* Application sprites */
- template_init(); /* Templates */
- dbox_init(); /* Dialogue boxes */
- flex_init();
- alarm_init();
-
- /* Create the menu tree */
-
- if (iconbar_menu = menu_new("FileTalk", ">Info,Quiet,Quit"), iconbar_menu == NULL)
- return FALSE; /* Menu create failed */
-
- /* Set up the icon on the icon bar, and declare its event handlers */
-
- baricon("filetalk", (int)resspr_area(), &IconClick);
-
- if (!event_attachmenu(win_ICONBAR, iconbar_menu, MenuProc, 0))
- return FALSE; /* Unable to attach menu */
- win_register_event_handler(win_ICONBARLOAD, &IconFileDrop, NULL);
-
- /* All went ok */
-
- text_size = 0;
- return TRUE;
- }
-
- /******************************* MAIN PROGRAM ********************************/
-
- /*--- Main entry point. ---*/
- int main()
- {
-
- if (Initialise())
- {
- /* The main event loop */
- while (TRUE)
- event_process();
- }
-
- return 0;
- }
-
-