File: slideshw.c

package info (click to toggle)
xfractint 20.4.10-5
  • links: PTS, VCS
  • area: non-free
  • in suites: sid, trixie
  • size: 5,112 kB
  • sloc: ansic: 77,361; asm: 430; cpp: 425; makefile: 347; sh: 38
file content (410 lines) | stat: -rw-r--r-- 10,524 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/***********************************************************************/
/* These routines are called by getakey to allow keystrokes to control */
/* Fractint to be read from a file.                                    */
/***********************************************************************/

#include <ctype.h>
#include <time.h>
#include <string.h>
#ifndef XFRACT
#include <conio.h>
#endif

  /* see Fractint.c for a description of the "include"  hierarchy */
#include "port.h"
#include "prototyp.h"

static void sleep_secs(int);
static int  showtempmsg_txt(int,int,int,int,char *);
static void message(int secs, char far *buf);
static void slideshowerr(char far *msg);
static int  get_scancode(char *mn);
static void get_mnemonic(int code, char *mnemonic);

static FCODE s_ENTER     [] = "ENTER"     ;
static FCODE s_INSERT    [] = "INSERT"    ;
static FCODE s_DELETE    [] = "DELETE"    ;
static FCODE s_ESC       [] = "ESC"       ;
static FCODE s_TAB       [] = "TAB"       ;
static FCODE s_PAGEUP    [] = "PAGEUP"    ;
static FCODE s_PAGEDOWN  [] = "PAGEDOWN"  ;
static FCODE s_HOME      [] = "HOME"      ;
static FCODE s_END       [] = "END"       ;
static FCODE s_LEFT      [] = "LEFT"      ;
static FCODE s_RIGHT     [] = "RIGHT"     ;
static FCODE s_UP        [] = "UP"        ;
static FCODE s_DOWN      [] = "DOWN"      ;
static FCODE s_F1        [] = "F1"        ;
static FCODE s_CTRL_RIGHT[] = "CTRL_RIGHT";
static FCODE s_CTRL_LEFT [] = "CTRL_LEFT" ;
static FCODE s_CTRL_DOWN [] = "CTRL_DOWN" ;
static FCODE s_CTRL_UP   [] = "CTRL_UP"   ;
static FCODE s_CTRL_END  [] = "CTRL_END"  ;
static FCODE s_CTRL_HOME [] = "CTRL_HOME" ;

#define MAX_MNEMONIC    20   /* max size of any mnemonic string */

struct scancodes
{
   int code;
   FCODE *mnemonic;
};

static struct scancodes far scancodes[] =
{
   {  ENTER,         s_ENTER     },
   {  INSERT,        s_INSERT    },
   {  DELETE,        s_DELETE    },
   {  ESC,           s_ESC       },
   {  TAB,           s_TAB       },
   {  PAGE_UP,       s_PAGEUP    },
   {  PAGE_DOWN,     s_PAGEDOWN  },
   {  HOME,          s_HOME      },
   {  END,           s_END       },
   {  LEFT_ARROW,    s_LEFT      },
   {  RIGHT_ARROW,   s_RIGHT     },
   {  UP_ARROW,      s_UP        },
   {  DOWN_ARROW,    s_DOWN      },
   {  F1,            s_F1        },
   {  RIGHT_ARROW_2, s_CTRL_RIGHT},
   {  LEFT_ARROW_2,  s_CTRL_LEFT },
   {  DOWN_ARROW_2,  s_CTRL_DOWN },
   {  UP_ARROW_2,    s_CTRL_UP   },
   {  CTL_END,       s_CTRL_END  },
   {  CTL_HOME,      s_CTRL_HOME },
   {  -1,             NULL       }
};
#define stop sizeof(scancodes)/sizeof(struct scancodes)-1

static int get_scancode(char *mn)
{
   int i;
   i = 0;
   for(i=0;i< stop;i++)
      if(far_strcmp((char far *)mn,scancodes[i].mnemonic)==0)
         break;
   return(scancodes[i].code);
}

static void get_mnemonic(int code,char *mnemonic)
{
   int i;
   i = 0;
   *mnemonic = 0;
   for(i=0;i< stop;i++)
      if(code == scancodes[i].code)
      {
         far_strcpy(mnemonic,scancodes[i].mnemonic);
         break;
      }   
}
#undef stop

char busy = 0;
static FILE *fpss = NULL;
static long starttick;
static long ticks;
static int slowcount;
static unsigned int quotes;
static char calcwait = 0;
static int repeats = 0;
static int last1 = 0;
static FCODE smsg[] = "MESSAGE";
static FCODE sgoto[] = "GOTO";
static FCODE scalcwait[] = "CALCWAIT";
static FCODE swait[] = "WAIT";

/* places a temporary message on the screen in text mode */
static int showtempmsg_txt(int row, int col, int attr,int secs,char *txt)
{
   int savescrn[80];
   int i;
   if(text_type > 1)
      return(1);
   for(i=0;i<80;i++)
   {
      movecursor(row,i);
      savescrn[i] = get_a_char();
   }
   putstring(row,col,attr,txt);
   movecursor(25,80);
   sleep_secs(secs);
   for(i=0;i<80;i++)
   {
      movecursor(row,i);
      put_a_char(savescrn[i]);
   }
   return(0);
}

static void message(int secs, char far *buf)
{
   int i;
   char nearbuf[41];
   i = -1;
   while(buf[++i] && i< 40)
      nearbuf[i] = buf[i];
   nearbuf[i] = 0;
   if(text_type < 2)
      showtempmsg_txt(0,0,7,secs,nearbuf);
   else if (showtempmsg(nearbuf) == 0)
      {
         sleep_secs(secs);
         cleartempmsg();
      }
}

/* this routine reads the file autoname and returns keystrokes */
int slideshw()
{
   int out,err,i;
   char buffer[81];
   if(calcwait)
   {
      if(calc_status == 1 || busy) /* restart timer - process not done */
         return(0); /* wait for calc to finish before reading more keystrokes */
      calcwait = 0;
   }
   if(fpss==NULL)   /* open files first time through */
      if(startslideshow()==0)
         {
         stopslideshow();
         return (0);
         }

   if(ticks) /* if waiting, see if waited long enough */
   {
      if(clock_ticks() - starttick < ticks) /* haven't waited long enough */
         return(0);
      ticks = 0;
   }
   if (++slowcount <= 18)
   {
      starttick = clock_ticks();
      ticks = CLK_TCK/5; /* a slight delay so keystrokes are visible */
      if (slowcount > 10)
         ticks /= 2;
   }
   if(repeats>0)
   {
      repeats--;
      return(last1);
   }
start:
   if(quotes) /* reading a quoted string */
   {
      if((out=fgetc(fpss)) != '\"' && out != EOF)
         return(last1 = out);
      quotes = 0;
   }
   /* skip white space: */
   while ((out=fgetc(fpss)) == ' ' || out == '\t' || out == '\n') { }
   switch(out)
   {
      case EOF:
         stopslideshow();
         return(0);
      case '\"':        /* begin quoted string */
         quotes = 1;
         goto start;
      case ';':         /* comment from here to end of line, skip it */
         while((out=fgetc(fpss)) != '\n' && out != EOF) { }
         goto start;
      case '*':
         if (fscanf(fpss,"%d",&repeats) != 1
           || repeats <= 1 || repeats >= 256 || feof(fpss))
         {
            static FCODE msg[] = "error in * argument";
            slideshowerr(msg);
            last1 = repeats = 0;
         }
         repeats -= 2;
         return(out = last1);
   }

   i = 0;
   for(;;) /* get a token */
   {
      if(i < 80)
         buffer[i++] = (char)out;
      if((out=fgetc(fpss)) == ' ' || out == '\t' || out == '\n' || out == EOF)
         break;
   }
   buffer[i] = 0;
   if(buffer[i-1] == ':')
      goto start;
   out = -12345;
   if(isdigit(buffer[0]))       /* an arbitrary scan code number - use it */
         out=atoi(buffer);
   else if(far_strcmp((char far *)buffer,smsg)==0)
      {
         int secs;
         out = 0;
         if (fscanf(fpss,"%d",&secs) != 1)
         {
            static FCODE msg[] = "MESSAGE needs argument";
            slideshowerr(msg);
         }
         else
         {
            int len;
            char buf[41];
            char *dummy; /* to quiet compiler */
            buf[40] = 0;
            dummy = fgets(buf,40,fpss);
            len = strlen(buf);
            buf[len-1]=0; /* zap newline */
            message(secs,(char far *)buf);
         }
         out = 0;
      }
   else if(far_strcmp((char far *)buffer,sgoto)==0)
      {
         if (fscanf(fpss,"%s",buffer) != 1)
         {
            static FCODE msg[] = "GOTO needs target";
            slideshowerr(msg);
            out = 0;
         }
         else
         {
            char buffer1[80];
            rewind(fpss);
            strcat(buffer,":");
            do
            {
               err = fscanf(fpss,"%s",buffer1);
            } while( err == 1 && strcmp(buffer1,buffer) != 0);
            if(feof(fpss))
            {
               static FCODE msg[] = "GOTO target not found";
               slideshowerr(msg);
               return(0);
            }
            goto start;
         }
      }
   else if((i = get_scancode(buffer)) > 0)
         out = i;
   else if(far_strcmp(swait,(char far *)buffer)==0)
      {
         float fticks;
         err = fscanf(fpss,"%f",&fticks); /* how many ticks to wait */
         fticks *= CLK_TCK;             /* convert from seconds to ticks */
         if(err==1)
         {
            ticks = (long)fticks;
            starttick = clock_ticks();  /* start timing */
         }
         else
         {
            static FCODE msg[] = "WAIT needs argument";
            slideshowerr(msg);
         }
         slowcount = out = 0;
      }
   else if(far_strcmp(scalcwait,(char far *)buffer)==0) /* wait for calc to finish */
      {
         calcwait = 1;
         slowcount = out = 0;
      }
   else if((i=check_vidmode_keyname(buffer)) != 0)
      out = i;
   if(out == -12345)
   {
      char msg[MSGLEN];
      sprintf(msg,s_cantunderstand,buffer);
      slideshowerr(msg);
      out = 0;
   }
   return(last1 = out);
}

int
startslideshow()
{
   if((fpss=fopen(autoname,"r"))==NULL)
      slides = 0;
   ticks = 0;
   quotes = 0;
   calcwait = 0;
   slowcount = 0;
   return(slides);
}

void stopslideshow()
{
   if(fpss)
      fclose(fpss);
   fpss = NULL;
   slides = 0;
}

void recordshw(int key)
{
   char mn[MAX_MNEMONIC];
   float dt;
   dt = (float)ticks;      /* save time of last call */
   ticks=clock_ticks();  /* current time */
   if(fpss==NULL)
      if((fpss=fopen(autoname,"w"))==NULL)
         return;
   dt = ticks-dt;
   dt /= CLK_TCK;  /* dt now in seconds */
   if(dt > .5) /* don't bother with less than half a second */
   {
      if(quotes) /* close quotes first */
      {
         quotes=0;
         fprintf(fpss,"\"\n");
      }
      fprintf(fpss,"WAIT %4.1f\n",dt);
   }
   if(key >= 32 && key < 128)
   {
      if(!quotes)
      {
         quotes=1;
         fputc('\"',fpss);
      }
      fputc(key,fpss);
   }
   else
   {
      if(quotes) /* not an ASCII character - turn off quotes */
      {
         fprintf(fpss,"\"\n");
         quotes=0;
      }
      get_mnemonic(key,mn);
      if(*mn)
          fprintf(fpss,"%s",mn);
      else if (check_vidmode_key(0,key) >= 0)
         {
            char buf[10];
            vidmode_keyname(key,buf);
            fprintf(fpss,buf);
         }
      else /* not ASCII and not FN key */
         fprintf(fpss,"%4d",key);
      fputc('\n',fpss);
   }
}

/* suspend process # of seconds */
static void sleep_secs(int secs)
{
   long stop;
   stop = clock_ticks() + (long)secs*CLK_TCK;
   while(clock_ticks() < stop && kbhit() == 0) { } /* bailout if key hit */
}

static void slideshowerr(char far *msg)
{
   char msgbuf[300];
   static FCODE errhdg[] = "Slideshow error:\n";
   stopslideshow();
   far_strcpy(msgbuf,errhdg);
   far_strcat(msgbuf,msg);
   stopmsg(0,msgbuf);
}