home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / fractint / fras1611.zip / SLIDESHW.C < prev    next >
Text File  |  1991-03-13  |  9KB  |  381 lines

  1. /***********************************************************************/
  2. /* These routines are called by getakey to allow keystrokes to control */
  3. /* Fractint to be read from a file.                       */
  4. /***********************************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9. #include <time.h>
  10. #include "fractint.h"
  11.  
  12. int slideshw(void);
  13. int startslideshow(void);
  14. void stopslideshow(void);
  15. void recordshw(int key);
  16.  
  17. static void sleep(int);
  18. static int  showtempmsg_txt(int,int,int,int,char *);
  19. static void message(int secs, char far *buf);
  20. static void slideshowerr(char far *msg);
  21. static int  get_scancode(char *mn);
  22. static char far *get_mnemonic(int code);
  23.  
  24. struct scancodes
  25. {
  26.    int code;
  27.    char far *mnemonic;
  28. };
  29. static struct scancodes far scancodes[] =
  30. {
  31.    {  ENTER,      "ENTER"       },
  32.    {  INSERT,      "INSERT"      },
  33.    {  DELETE,      "DELETE"      },
  34.    {  ESC,      "ESC"         },
  35.    {  TAB,      "TAB"         },
  36.    {  PAGE_UP,      "PAGEUP"      },
  37.    {  PAGE_DOWN,  "PAGEDOWN"    },
  38.    {  HOME,      "HOME"        },
  39.    {  END,      "END"         },
  40.    {  LEFT_ARROW, "LEFT"        },
  41.    {  RIGHT_ARROW,"RIGHT"       },
  42.    {  UP_ARROW,   "UP"          },
  43.    {  DOWN_ARROW, "DOWN"        },
  44.    {  F1,      "F1"          },
  45.    {  -1,      NULL        }
  46. };
  47. #define stop sizeof(scancodes)/sizeof(struct scancodes)-1
  48.  
  49. static int get_scancode(char *mn)
  50. {
  51.    int i;
  52.    i = 0;
  53.    for(i=0;i< stop;i++)
  54.       if(far_strcmp((char far *)mn,scancodes[i].mnemonic)==0)
  55.      break;
  56.    return(scancodes[i].code);
  57. }
  58.  
  59. static char far *get_mnemonic(int code)
  60. {
  61.    int i;
  62.    i = 0;
  63.    for(i=0;i< stop;i++)
  64.       if(code == scancodes[i].code)
  65.      break;
  66.    return(scancodes[i].mnemonic);
  67. }
  68. #undef stop
  69.  
  70. char busy = 0;
  71. static FILE *fp = NULL;
  72. extern int slides;
  73. extern int text_type;
  74. extern int calc_status;
  75. extern char autoname[];
  76. static long starttick;
  77. static long ticks;
  78. static int slowcount;
  79. static unsigned int quotes;
  80. static char calcwait = 0;
  81. static int repeats = 0;
  82. static int last = 0;
  83. static char far smsg[] = "MESSAGE";
  84. static char far sgoto[] = "GOTO";
  85. static char far scalcwait[] = "CALCWAIT";
  86. static char far swait[] = "WAIT";
  87.  
  88. /* places a temporary message on the screen in text mode */
  89. static int showtempmsg_txt(int row, int col, int attr,int secs,char *txt)
  90. {
  91.    int savescrn[80];
  92.    int i;
  93.    if(text_type > 1)
  94.       return(1);
  95.    for(i=0;i<80;i++)
  96.    {
  97.       movecursor(row,i);
  98.       savescrn[i] = get_a_char();
  99.    }
  100.    putstring(row,col,attr,txt);
  101.    movecursor(25,80);
  102.    sleep(secs);
  103.    for(i=0;i<80;i++)
  104.    {
  105.       movecursor(row,i);
  106.       put_a_char(savescrn[i]);
  107.    }
  108.    return(0);
  109. }
  110.  
  111. static void message(int secs, char far *buf)
  112. {
  113.    int i;
  114.    char nearbuf[41];
  115.    i = -1;
  116.    while(buf[++i] && i< 40)
  117.       nearbuf[i] = buf[i];
  118.    nearbuf[i] = 0;
  119.    if(text_type < 2)
  120.       showtempmsg_txt(0,0,7,secs,nearbuf);
  121.    else if (showtempmsg(nearbuf) == 0)
  122.       {
  123.      sleep(secs);
  124.      cleartempmsg();
  125.       }
  126. }
  127.  
  128. /* this routine reads the file autoname and returns keystrokes */
  129. int slideshw()
  130. {
  131.    int out,err,i;
  132.    char buffer[81];
  133.    if(calcwait)
  134.    {
  135.       if(calc_status == 1 || busy) /* restart timer - process not done */
  136.      return(0); /* wait for calc to finish before reading more keystrokes */
  137.       calcwait = 0;
  138.    }
  139.    if(fp==NULL)   /* open files first time through */
  140.       if(startslideshow()==0)
  141.      {
  142.      stopslideshow();
  143.      return (0);
  144.      }
  145.  
  146.    if(ticks) /* if waiting, see if waited long enough */
  147.    {
  148.       if(clock() - starttick < ticks) /* haven't waited long enough */
  149.      return(0);
  150.       ticks = 0;
  151.    }
  152.    if (++slowcount <= 18)
  153.    {
  154.       starttick = clock();
  155.       ticks = CLK_TCK/5; /* a slight delay so keystrokes are visible */
  156.       if (slowcount > 10)
  157.      ticks /= 2;
  158.    }
  159.    if(repeats>0)
  160.    {
  161.       repeats--;
  162.       return(last);
  163.    }
  164. start:
  165.    if(quotes) /* reading a quoted string */
  166.    {
  167.       if((out=fgetc(fp)) != '\"' && out != EOF)
  168.      return(last=out);
  169.       quotes = 0;
  170.    }
  171.    /* skip white space: */
  172.    while ((out=fgetc(fp)) == ' ' || out == '\t' || out == '\n') { }
  173.    switch(out)
  174.    {
  175.       case EOF:
  176.      stopslideshow();
  177.      return(0);
  178.       case '\"':        /* begin quoted string */
  179.      quotes = 1;
  180.      goto start;
  181.       case ';':         /* comment from here to end of line, skip it */
  182.      while((out=fgetc(fp)) != '\n' && out != EOF) { }
  183.      goto start;
  184.       case '*':
  185.      if (fscanf(fp,"%d",&repeats) != 1
  186.        || repeats <= 1 || repeats >= 256 || feof(fp))
  187.      {
  188.         static char far msg[] = "error in * argument";
  189.         slideshowerr(msg);
  190.         last = repeats = 0;
  191.      }
  192.      repeats -= 2;
  193.      return(out = last);
  194.    }
  195.  
  196.    i = 0;
  197.    while(1) /* get a token */
  198.    {
  199.       if(i < 80)
  200.      buffer[i++] = out;
  201.       if((out=fgetc(fp)) == ' ' || out == '\t' || out == '\n' || out == EOF)
  202.      break;
  203.    }
  204.    buffer[i] = 0;
  205.    if(buffer[i-1] == ':')
  206.       goto start;
  207.    out = -12345;
  208.    if(isdigit(buffer[0]))    /* an arbitrary scan code number - use it */
  209.      out=atoi(buffer);
  210.    else if(far_strcmp((char far *)buffer,smsg)==0)
  211.       {
  212.      int secs;
  213.      out = 0;
  214.      if (fscanf(fp,"%d",&secs) != 1)
  215.      {
  216.         static char far msg[] = "MESSAGE needs argument";
  217.         slideshowerr(msg);
  218.      }
  219.      else
  220.      {
  221.         int len;
  222.         char buf[41];
  223.         buf[40] = 0;
  224.         fgets(buf,40,fp);
  225.         len = strlen(buf);
  226.         buf[len-1]=0; /* zap newline */
  227.         message(secs,(char far *)buf);
  228.      }
  229.      out = 0;
  230.       }
  231.    else if(far_strcmp((char far *)buffer,sgoto)==0)
  232.       {
  233.      if (fscanf(fp,"%s",buffer) != 1)
  234.      {
  235.         static char far msg[] = "GOTO needs target";
  236.         slideshowerr(msg);
  237.         out = 0;
  238.      }
  239.      else
  240.      {
  241.         char buffer1[80];
  242.         rewind(fp);
  243.         strcat(buffer,":");
  244.         do
  245.         {
  246.            err = fscanf(fp,"%s",buffer1);
  247.         } while( err == 1 && strcmp(buffer1,buffer) != 0);
  248.         if(feof(fp))
  249.         {
  250.            static char far msg[] = "GOTO target not found";
  251.            slideshowerr(msg);
  252.            return(0);
  253.         }
  254.         goto start;
  255.      }
  256.       }
  257.    else if((i = get_scancode(buffer)) > 0)
  258.      out = i;
  259.    else if(far_strcmp(swait,(char far *)buffer)==0)
  260.       {
  261.      float fticks;
  262.      err = fscanf(fp,"%f",&fticks); /* how many ticks to wait */
  263.      fticks *= CLK_TCK;        /* convert from seconds to ticks */
  264.      if(err==1)
  265.      {
  266.         ticks = fticks;
  267.         starttick = clock();    /* start timing */
  268.      }
  269.      else
  270.      {
  271.         static char far msg[] = "WAIT needs argument";
  272.         slideshowerr(msg);
  273.      }
  274.      slowcount = out = 0;
  275.       }
  276.    else if(far_strcmp(scalcwait,(char far *)buffer)==0) /* wait for calc to finish */
  277.       {
  278.      calcwait = 1;
  279.      slowcount = out = 0;
  280.       }
  281.    else if((i=check_vidmode_keyname(buffer)))
  282.       out = i;
  283.    if(out == -12345)
  284.    {
  285.       char msg[80];
  286.       sprintf(msg,"Can't understand %s",buffer);
  287.       slideshowerr(msg);
  288.       out = 0;
  289.    }
  290.    return(last=out);
  291. }
  292.  
  293. startslideshow()
  294. {
  295.    if((fp=fopen(autoname,"r"))==NULL)
  296.       slides = 0;
  297.    ticks = 0;
  298.    quotes = 0;
  299.    calcwait = 0;
  300.    slowcount = 0;
  301.    return(slides);
  302. }
  303.  
  304. void stopslideshow()
  305. {
  306.    if(fp)
  307.       fclose(fp);
  308.    fp = NULL;
  309.    slides = 0;
  310. }
  311.  
  312. void recordshw(int key)
  313. {
  314.    char far *mn;
  315.    float dt;
  316.    int i;
  317.    dt = ticks;       /* save time of last call */
  318.    ticks=clock();  /* current time */
  319.    if(fp==NULL)
  320.       if((fp=fopen(autoname,"w"))==NULL)
  321.      return;
  322.    dt = ticks-dt;
  323.    dt /= CLK_TCK;  /* dt now in seconds */
  324.    if(dt > .5) /* don't bother with less than half a second */
  325.    {
  326.       if(quotes) /* close quotes first */
  327.       {
  328.      quotes=0;
  329.      fprintf(fp,"\"\n",dt);
  330.       }
  331.       fprintf(fp,"WAIT %4.1f\n",dt);
  332.    }
  333.    if(key >= 32 && key < 128)
  334.    {
  335.       if(!quotes)
  336.       {
  337.      quotes=1;
  338.      fputc('\"',fp);
  339.       }
  340.       fputc(key,fp);
  341.    }
  342.    else
  343.    {
  344.       if(quotes) /* not an ASCII character - turn off quotes */
  345.       {
  346.      fprintf(fp,"\"\n");
  347.      quotes=0;
  348.       }
  349.       if((mn=get_mnemonic(key)) != NULL)
  350.       fprintf(fp,"%Fs",mn);
  351.       else if (check_vidmode_key(0,key) >= 0)
  352.      {
  353.         char buf[10];
  354.         vidmode_keyname(key,buf);
  355.         fprintf(fp,buf);
  356.      }
  357.       else /* not ASCII and not FN key */
  358.      fprintf(fp,"%4d",key);
  359.       fputc('\n',fp);
  360.    }
  361. }
  362.  
  363. /* suspend process # of seconds */
  364. static void sleep(int secs)
  365. {
  366.    long stop;
  367.    stop = clock() + (long)secs*CLK_TCK;
  368.    while(clock() < stop && kbhit() == 0) { } /* bailout if key hit */
  369. }
  370.  
  371. static void slideshowerr(char far *msg)
  372. {
  373.    char msgbuf[300];
  374.    static char far errhdg[] = "Slideshow error:\n";
  375.    stopslideshow();
  376.    far_strcpy(msgbuf,errhdg);
  377.    far_strcat(msgbuf,msg);
  378.    stopmsg(0,msgbuf);
  379. }
  380.  
  381.