home *** CD-ROM | disk | FTP | other *** search
/ ARM Club 1 / ARM_CLUB_CD.iso / contents / apps / audio / a / filetalk / !FileTalk / c / FileTalk
Encoding:
Text File  |  1990-09-04  |  7.0 KB  |  333 lines

  1. /* Title:   FileTalk.c
  2.  */
  3.  
  4. #define TRACE 0
  5.  
  6. #include "alarm.h"
  7. #include "wimp.h"
  8. #include "wimpt.h"
  9. #include "win.h"
  10. #include "event.h"
  11. #include "baricon.h"
  12. #include "res.h"
  13. #include "resspr.h"
  14. #include "menu.h"
  15. #include "template.h"
  16. #include "dbox.h"
  17. #include "werr.h"
  18. #include "xferrecv.h"
  19. #include "flex.h"
  20.  
  21. #include <stdlib.h>
  22. #include <string.h>
  23.  
  24. #include "h.DT-Talk"
  25.  
  26. #define OS_ReadVarVal (0x23)
  27.  
  28. /* Menu items */
  29.  
  30. typedef enum {
  31.        menu_info = 1,
  32.        menu_quiet = 2,
  33.        menu_quit = 3
  34. } iconbar_menu_t;
  35.  
  36. #define Version_info_field    4
  37.  
  38. /* Application version */
  39. static char *Version_string = "1.00 (03 Sep 1990)";
  40.  
  41. menu iconbar_menu;
  42. flex_ptr text;
  43. int text_size;
  44. int text_len;
  45. int text_pos;
  46.  
  47. BOOL LoadCheck(filename, filetype)
  48.  
  49. char *filename;
  50. int filetype;
  51. {
  52.    os_filestr file;
  53.   
  54.    file.action = 5;
  55.    file.name = filename;
  56.    if (wimpt_complain(os_file(&file)) != 0) return(FALSE);
  57.  
  58.    switch (file.action)
  59.    {
  60.       case 0: werr(0, "File not found");              return(FALSE);
  61.       case 2: werr(0, "You cannot load a directory"); return(FALSE);
  62.    }
  63.  
  64.    if ((file.loadaddr & 0xFFF00000) != 0xFFF00000) {
  65.       werr(0, "File cannot be loaded");
  66.       return(FALSE);
  67.    }
  68.  
  69.    if (filetype != 0xFFF) {
  70.       werr(0, "Not a text file");
  71.       return(FALSE);
  72.    }
  73.    return(TRUE);
  74. }
  75.  
  76. /* Import function for ram-to-ram transfer. */ 
  77.  
  78. BOOL ImportFileProc(char **buf, int *size)
  79.  
  80. {
  81.    if (flex_extend(text, text_size+1024) == 0) {
  82.       werr(FALSE, "FileTalk has run out of room");
  83.       flex_free(text);
  84.       return(FALSE);
  85.    }
  86.    text_size += 1024;
  87.    *buf += 1024;
  88.    *size = 1024;
  89.    return(TRUE);
  90. }
  91.  
  92. BOOL LoadFile(event)
  93.  
  94. wimp_eventstr *event;
  95. {
  96.    char *filename;
  97.    int filetype;
  98.    int file_size;
  99.    FILE *in_file;
  100.  
  101.    if (text_size != 0) {
  102.       werr(FALSE, "FileTalk is busy");
  103.       return(FALSE);
  104.    }
  105.       
  106.    filetype = xferrecv_checkinsert(&filename);
  107.    if (filetype == -1) {
  108.       filetype = xferrecv_checkimport(&file_size);
  109.       if (filetype == -1) return(FALSE);
  110.       if (filetype != 0xFFF) {
  111.          werr(FALSE, "Not a text file");
  112.          return(FALSE);
  113.       }
  114.       if (flex_alloc(text, 1024) == 0) {
  115.          werr(FALSE, "FileTalk has run out of room");
  116.          return(FALSE);
  117.       }
  118.       text_size = 1024;
  119.       text_len = xferrecv_doimport(*text, 1024, &ImportFileProc);
  120.  
  121.       if (text_len < 0) {
  122.          werr(FALSE, "Load failed");
  123.          flex_free(text);
  124.          return(FALSE);
  125.       }
  126.       return(TRUE);
  127.    }
  128.  
  129.    if (LoadCheck(filename, filetype)) {
  130.       in_file = fopen(filename, "r");
  131.       if (in_file == NULL) {
  132.          werr(0, "File could not be opened");
  133.          return(FALSE);
  134.       }
  135.       {
  136.          int i = 0;
  137.          while (!feof(in_file)) {
  138.             fgetc(in_file);
  139.             i++;
  140.          }
  141.          if (flex_alloc(text, i) == 0) {
  142.             werr(FALSE, "FileTalk has run out of room");
  143.             return(FALSE);
  144.          }
  145.          text_size = i; text_len = i;
  146.          rewind(in_file);
  147.          i=0;
  148.          while (!feof(in_file)) {
  149.             ((char *)(*text))[i++] = fgetc(in_file);
  150.          }
  151.          fclose(in_file);
  152.       }
  153.       return(TRUE);
  154.    }
  155.    return(FALSE);
  156. }
  157.  
  158. /* Speak the file loaded */
  159.  
  160. FILE *dd;
  161.  
  162. void SpeakBack(int at, void *handle)
  163. {
  164.    int i, wst, wend;
  165.    char cd[1024];
  166.    os_regset r;
  167.  
  168.    os_swi2r(VoiceSynth_FIFO | os_X, 0, 0, &(r.r[0]), &(r.r[1]));
  169.    if (r.r[1] < 2) {
  170.       wst = text_pos;
  171.       i = 1;
  172.       while (((char *)(*text))[wst+i] != ' ') i++;
  173.       if (i > 240) {
  174.          werr(FALSE, "Suspect word found in file");
  175.          text_size = 0;
  176.          flex_free(text);
  177.          baricon_newsprite("filetalk");
  178.          return;
  179.       }
  180.       wend = wst+i;
  181.       strcpy(cd, "*say ");
  182.       for (i=0; i<wend-wst; i++) cd[i+5] = ((char *)(*text))[wst+i];
  183.       cd[i+5]=0;
  184.       system(cd);
  185.       text_pos = wend;
  186.       if (text_pos >= text_len) {
  187.          text_size = 0;
  188.          flex_free(text);
  189.          baricon_newsprite("filetalk");
  190.          return;
  191.       }
  192.    }
  193.    alarm_set(alarm_timenow()+20, &SpeakBack, 0);
  194. }
  195.  
  196. void SpeakText(void)
  197. {
  198.    int i, wst;
  199.  
  200.    wst = 0;
  201.    for (i=0;i<text_len; i++) {
  202.       if (((char*)(*text))[i] == 10) ((char *)(*text))[i] = ' ';
  203.       if (isprint(((char *)(*text))[i])) 
  204.          ((char*)(*text))[wst++] = ((char *)(*text))[i];
  205.    }
  206.    text_len = wst;
  207.    ((char *)(*text))[text_len] = ' ';
  208.    text_pos = 0;
  209.    alarm_set(alarm_timenow()+5, &SpeakBack, 0);
  210.    baricon_newsprite("filetalking");
  211. }
  212.  
  213. /***************************** WINDOW FUNCTIONS ****************************/
  214.  
  215. /*--- Display the program info box - called from the menu processor. ---*/
  216.  
  217. void ProgramInfo(void)
  218. {
  219.    dbox d;
  220.  
  221.    d = dbox_new("info");
  222.    dbox_setfield(d, Version_info_field, Version_string);
  223.    dbox_show(d);
  224.    dbox_fillin(d);
  225.    dbox_dispose(&d);
  226. }
  227.  
  228. /*--- Event handler for the menu. ---*/
  229.  
  230. void MenuProc(void *handle, char *hit)
  231. {
  232.    handle = handle; /* We don't need the handle: this stops compiler warning */
  233.  
  234.    /* Find which menu item was hit and take action as appropriate */
  235.    switch ((iconbar_menu_t) hit[0])
  236.    {
  237.       case menu_info:
  238.          ProgramInfo();
  239.       break;
  240.  
  241.       case menu_quiet:
  242.          if (text_size > 0) {
  243.             flex_free(text);
  244.             text_size = 0;
  245.             alarm_removeall(0);
  246.             baricon_newsprite("filetalk");
  247.          }
  248.          break;
  249.  
  250.       case menu_quit:
  251.       /* Exit from the program. The wimp gets rid of the window and icon */
  252.          exit(0);
  253.       break;
  254.    }
  255. }
  256.  
  257. /*--- Event handler for file drops on the icon ---*/
  258.  
  259. void IconFileDrop(event, handle)
  260.  
  261. wimp_eventstr *event;
  262. void *handle;
  263. {
  264.    handle = handle;
  265.  
  266.    switch (event->e) {
  267.       case wimp_ESEND:
  268.       case wimp_ESENDWANTACK:
  269.          if (LoadFile(event)) {
  270.             xferrecv_insertfileok();
  271.             SpeakText();
  272.          }
  273.       break;
  274.    }
  275. }
  276.  
  277. void IconClick(wimp_i w)
  278. {
  279.    w=w;
  280. }
  281.  
  282. /****************************** INITIALISATION ******************************/
  283.  
  284. /*--- Initialise the program, returning TRUE if it was all OK. ---*/
  285.  
  286. BOOL Initialise(void)
  287. {
  288. /* WimpLib initialisation */
  289.  
  290.    wimpt_init("FileTalk");          /* Main wimp initialisation */
  291.    res_init("FileTalk");            /* Resources */
  292.    resspr_init();                   /* Application sprites */
  293.    template_init();                 /* Templates */
  294.    dbox_init();                     /* Dialogue boxes */
  295.    flex_init();
  296.    alarm_init();
  297.  
  298. /* Create the menu tree */
  299.  
  300.    if (iconbar_menu = menu_new("FileTalk", ">Info,Quiet,Quit"), iconbar_menu == NULL)
  301.       return FALSE; /* Menu create failed */
  302.  
  303. /* Set up the icon on the icon bar, and declare its event handlers */
  304.  
  305.    baricon("filetalk", (int)resspr_area(), &IconClick);
  306.  
  307.    if (!event_attachmenu(win_ICONBAR, iconbar_menu, MenuProc, 0))
  308.       return FALSE; /* Unable to attach menu */
  309.    win_register_event_handler(win_ICONBARLOAD, &IconFileDrop, NULL);
  310.    
  311. /* All went ok */
  312.  
  313.    text_size = 0;
  314.    return TRUE;
  315. }
  316.  
  317. /******************************* MAIN PROGRAM ********************************/
  318.  
  319. /*--- Main entry point. ---*/
  320. int main()
  321. {
  322.  
  323.   if (Initialise())
  324.   {
  325.     /* The main event loop */
  326.     while (TRUE)
  327.       event_process();
  328.   }
  329.  
  330.   return 0;
  331. }
  332.  
  333.