#include <stdio.h>
#include <dos.h>
#include <bios.h>
#include <conio.h>

#define KB  0x09  /* use keyboard interrupt to active TSR       */
#define END 0x61  /* use reserved interrupt to uninstall ISR    */
#define ALT_FLAG   0X08
#define CTRL_FLAG  0X04

int active=0;
char far *KBF=(char far *)0x417;

void far interrupt  (*oldint_9h)(void);
void far interrupt  (*oldint_60h)(void);
void far interrupt  (*int_61h)(void);
void far interrupt  active_work(void);
void far interrupt  (*oldintend)(unsigned bp,unsigned di,unsigned si,
			     unsigned ds,unsigned es,unsigned dx,
			     unsigned cx,unsigned bx,unsigned ax);

void far interrupt active_work(void)
{
  int xx,yy;

  xx = wherex(); /* save old cursor position */
  yy = wherey();

  (*oldint_9h)();
  if((*KBF&ALT_FLAG)&&(*KBF&CTRL_FLAG))  {
    if(active==0)  {
      active=1;
      gotoxy(1,2);
      cprintf(" Now your Press ALT+CTRL to active program Success \n");
      cprintf(" Press ALT+CTRL again to Abort TSR ");
    }
    else  {
      gotoxy(1,2);
      cprintf("                                                   \n");
      cprintf("                            ");
      active=0;
    }
  }
  gotoxy(xx,yy); /* move cursor to old position */
}

void far interrupt uninstall(unsigned bp,unsigned di,unsigned si,
			 unsigned ds,unsigned es,unsigned dx,
			 unsigned cx,unsigned bx,unsigned ax)
{
   /*  Restore old interrupt vector */
   setvect(KB,oldint_9h);
   setvect(0x60,oldint_60h);
   setvect(END,oldintend);
   es = _psp;  /* send PSP segment address to uninstall section */
}


void main(int argc,char *argv[])
{
   unsigned int ch,oldseg;

   printf("Key checking program version1.0\n");
   printf("===============================\n");

   /*******   install routine *******/

   oldint_60h = getvect(0x60); /* get INT 60H        */
   int_61h = getvect(0x61); /* get INT 61H        */

   if(argc == 1)  /*  no parameter */
     {
       if(oldint_60h==int_61h)  {
	   oldint_9h = getvect(KB); /* save old timer interrupt */
	   setvect(KB,active_work); /* set new interrupt  */
	   setvect(0x60,active_work); /* set new interrupt  */
	   oldintend = getvect(END);/* save old interrupt */
	   setvect(END,uninstall);  /* set new interrupt  */

	   printf("Stay resident now...\n");

	   keep(0, (_SS + (_SP/16) - _psp)); /* Use _SS and _psp
					     to calculate program area */
	   return;
	 }
       else
	 {
	   printf("** Program Already Installed **\n");
	   printf("-------------------------------\n");
	 }
     }
   /*  Uninstall  resident  */
   if((argc == 2)&&(argv[1][0]=='/')&&((argv[1][1]=='u')||(argv[1][2]=='U')))
     {
       if(getvect(END)== NULL) /* No resident was installed */
         printf("Program was not installed\n");
       else
         {
           geninterrupt(END);  /* return old PSP  segment in ES */

           oldseg = _ES;  /* store old PSP segment */
           ch = peek(_ES,0x2c);   /* read environment segment */
           _ES = ch;      /* prepare parameter to free program memory*/
           _AH = 0x49;    /*  Free memory function */
           geninterrupt(0x21); /* call DOS API (Kernel) */
           _ES = oldseg;  /*  Free PSP memory */
           _AH = 0x49;
           geninterrupt(0x21);

           printf("Uninstalled resident program... \n");
         }
     }
   else
     printf("Use tsr4 /u to uninstall program\n");
}
