#include <time.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>

#include "d:\dev\tcl\include\tcl.h"

#define FALSE 0
#define TRUE  1

typedef unsigned char BOOL;

int getMode(ClientData clientData, Tcl_Interp *interp,
         int argc, char *argv[]) {
   BOOL loopMode = FALSE, stepMode = FALSE, quit = FALSE, done = FALSE;
   char charIn, retstr[10];

   retstr[0] = 0;

   while (!done) {
      printf("\n[L]ooping ");
      if (loopMode)   printf("ON  ");
      else         printf("OFF ");

      printf("[S]tep mode");
      if (stepMode)   printf("ON  ");
      else         printf("OFF ");

      printf("[D]one [Q]uit:->");

      while (!_kbhit() );   
      while(_kbhit()) charIn = _getch();         
      
      switch (charIn) {
         case 'L':
         case 'l':
            loopMode ^= 1;
            break;
         case 'S':
         case 's':
            stepMode ^= 1;
            break;
         case 'Q':
         case 'q':
            quit = TRUE;
            done = TRUE;
            break;
         case 'D':
         case 'd':
            done = TRUE;
            break;

         default:
            printf("\ninvalid char %c", charIn);
      }
   }

   if (loopMode)   strcat(retstr, "LOOP");
   if (stepMode)   {
      if (retstr[0]) strcat(retstr, ",");
      strcat(retstr, "STEP");
   }
   interp->result = retstr;
   return TCL_OK;
}   

int kbTime(ClientData clientData, Tcl_Interp *interp,
         int argc, char *argv[]) {

   clock_t goal, delay;
   BOOL step;
   char retstr[2], charIn;
   
   if (argc != 2) {
      interp->result = "wrong # args - kbTime [msecs]";
      return TCL_ERROR;
   }   
   
   delay = (clock_t)atoi(argv[1])*CLOCKS_PER_SEC/1000;
   
   if (delay < 0)   step = TRUE;
   else         step = FALSE;
   
   goal = clock() + delay;

   while( goal > clock() || step) {
      if (_kbhit()) {
         
         while(_kbhit()) charIn = _getch();         

         goal =    clock() + 100;
         while( goal > clock() );   
            
         retstr[0] = charIn;
         retstr[1] = 0x00;
         interp->result = retstr;

         return TCL_OK;
      }
   }
   interp->result = "TIMEOUT";
   return TCL_OK;
}

void main(int argc, char *argv[]) {
   char cmd[100];
   Tcl_Interp *interp;
   int code;
      
/*   if (argc != 2) {
      fprintf(stderr, "Wrong # args: ");
      fprintf(stderr, "should be \"%s fileName\"\n",
         argv[0]);
      exit(1);
   }
*/   
   strcpy(cmd, "source source.tcl");
   
   interp = Tcl_CreateInterp();
   
   Tcl_CreateCommand(interp, "kbtime", kbTime,
            (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);

   Tcl_CreateCommand(interp, "getmode", getMode,
            (ClientData) NULL, (Tcl_CmdDeleteProc *) NULL);
   
   code = Tcl_Eval(interp, cmd);

   if (*interp->result != 0) {
      printf("%s\n", interp->result);
   }
   
   if (code != TCL_OK) {
      exit(1);
   }
   exit(0);
}
-- MattWalsh - 27 Feb 2004
Topic revision: r1 - 27 Feb 2004 - MattWalsh
 
This site is powered by the TWiki collaboration platformCopyright © 2008-2012 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback