
/******************************/
/* Busy Buddy accessory       */
/* Copyright 1988 by ST-Log   */
/* Rat*Ware by                */
/* Matthew J. W. Ratcliff     */
/* AKA Mat*Rat, All Rights    */
/* Reserved.                  */
/******************************/

/**********************/
/* EXTERNALS          */
/**********************/

extern int gl_apid;


/*****************/
/* Include files */
/*****************/

#include <stdio.h>
#include <osbind.h>

/********************/
/* Global variables */
/********************/

char Back_Type[]=
"[2][ Busy Buddy by Mat*Rat| | Backspace select, ASCII? ][ 8| 126| 127]";

char Buzzoff[]=
 "[1][ | Busy Buddy is now OFF | ][ OK ]";

char Busy_Time[]=
"[2][ Busy Buddy Maximum | Time Limit? | | Minutes ][  5 | 15 | 60 ]";

char Busy_Tout[]=
"[2][ Busy Buddy TIMEOUT | | Shall I ? ][ Exit | Restart ]";

char Buddy_Byte, Backup;
int Time_Limit;

/*************/
/* Constants */
/*************/

#define EVNT_MSG        0x0010
#define EVNT_TIM        0x0020
#define TIME_H        (int)0
#define TIME_L        (int)2000
#define AC_OPEN         40      /* Startup accessory cmd*/
#define ASCII_8         1
#define ASCII_126       2
#define ASCII_127       3
#define BS              8       /* Backspace character  */
#define XE_DEL         126      /* Delete character-XE  */
#define ST_DEL         127      /* Delete char-ST       */
#define SPACE           32      /* Space character      */
#define PRT             0       /* Standard IO devices  */
#define AUX             1
#define CON             2
#define MIDI            3
#define KBD             4
#define FIVE_MIN        (int)150
#define FIFTEEN_MIN     (int)450
#define SIXTY_MIN       (int)1800
#define SEL_FIVE        1
#define SEL_FIFTEEN     2
#define SEL_SIXTY       3
#define BELL            7


/********************************************************/
/* Program: Busy Buddy                                  */
/* Type:    Desk accessory                              */
/* Purpose: Busy Buddy, when activated, initiates       */
/* a   2 second timed interval for sending alternating  */
/* space and backspace characters over the modem.       */
/* This feature will keep the system at the other       */
/* end BUSY. This will prevent most BBS and             */
/* telecommunication systems from TIMING OUT            */
/* on you, so you have time for a Budweiser break.      */
/********************************************************/

main()

{ /* Get busy BUDDY! */

int menu_id;
int mgin[8];
int dum,event,flag;
int forever;

/**********************/
/* Install accessory: */
/**********************/

appl_init();
menu_id = menu_register(gl_apid,"  Busy Buddy");

/******************************************************/
/* Event message only, until activated the first time */
/******************************************************/

flag    = EVNT_MSG;
forever = 1;

while (forever)
  { /* loop forever */

  dum   = 0;
  event = evnt_multi( flag, -1, -1, -1,
                      dum, dum, dum, dum, dum, /* de-daaa */
                      dum, dum, dum, dum, dum, /* de-dooo */
                      mgin, TIME_L, TIME_H,
                      &dum, &dum, &dum, &dum, &dum, &dum);
  if (event & EVNT_MSG)
    if (mgin[0] == AC_OPEN && mgin[4] == menu_id)
          if (flag & EVNT_TIM)
            {
            flag = EVNT_MSG;
            Buddy_Off();
            event = 0;
            }
          else
            {
            flag = EVNT_TIM | EVNT_MSG;
            Buddy_On();
            event = 0;
            }

  if (event & EVNT_TIM)
    if (flag & EVNT_TIM)
      {
      Buddy();
      if (Time_Limit <= 0)
        flag = Buddy_Toff();
      }


  } /* end loop forever */

} /* end main() */

/*************************************************************/
/* Function:    Buddy_On()                                   */
/* Description: Initialize Busy Buddy. Allow user to select  */
/* backspace or delete type of backup. Initialize Backup     */
/* variable and send first SPACE character to begin process. */
/*************************************************************/

Buddy_On()

{ /* begin Buddy_On() */

int bsel;

bsel = form_alert(1, Back_Type);        /* Get backspace or delete type */

switch (bsel)
  { /* begin switch */
  case (ASCII_8):
    Backup = BS;
    break;

  case (ASCII_126):
    Backup = XE_DEL;
    break;

  case (ASCII_127):
    Backup = ST_DEL;
    break;

  default:
    break;

  } /* end switch */

Get_Limit();                            /* Set time limit               */
Send_Byte( SPACE );                     /* Start the busy signal        */
Buddy_Byte = Backup;                    /* setup for backspace next time*/

} /* end Buddy_On() */



/*************************************************************/
/* Function:    Buddy_Off()                                  */
/* Description: Make certain that a backup character was the */
/* last thing sent (spaces must be matched with backups),    */
/* then inform the user that Busy Buddy is dormant.          */
/*************************************************************/

Buddy_Off ()

{ /* begin Buddy_Off() */

if (Buddy_Byte == Backup)               /* Make certain that backup was */
  Send_Byte( Buddy_Byte );              /* last character sent          */

form_alert(1, Buzzoff );                /* Tell user busy is off now    */

} /* end Buddy_Off() */


/*************************************************************/
/* Function:    Buddy()                                      */
/* Description: Send the current character from Buddy_Byte.  */
/* Then toggle from SPACE to BACKUP, or BACKUP to SPACE for  */
/* next 2 second interrupt.                                  */
/*************************************************************/

Buddy()

{ /* begin Buddy() */

Send_Byte( Buddy_Byte );                /* Send current space or backup */

if (Buddy_Byte == SPACE)                /* If space just sent, backup   */
  Buddy_Byte = Backup;                  /* next time.                   */
else
  Buddy_Byte = SPACE;                   /* If backup just sent, then    */
                                        /* send space next time.        */
Time_Limit--;

} /* end Buddy() */


/*************************************************************/
/* Function:    Send_Byte( char )                            */
/* Description: Send the character received to the AUX, RS232*/
/* port, using the Bconout function.                         */
/*************************************************************/

Send_Byte( dude )
char dude;

{ /* begin Send_Byte( char ) */

/***************************************************/
/* Send the caracter passed to the AUX, RS232 Port */
/***************************************************/

Bconout ( AUX, dude );

} /* end Send_Byte( char ) */



/*************************************************************/
/* Function:    Buddy_Toff()    returns ( INT )              */
/* Description: Make certain that a backup character was the */
/* last thing sent (spaces must be matched with backups),    */
/* then inform the user that Busy Buddy is timed out.        */
/* Return flag status for next event multi.                  */
/*************************************************************/

Buddy_Toff ()

{ /* begin Buddy_Toff() */

#define BEXIT     1
#define BRESTART  2

int sel, bflag;

if (Buddy_Byte == Backup)               /* Make certain that backup was */
  Send_Byte( Buddy_Byte );              /* last character sent          */

Bconout( CON, BELL);                    /* WAKE UP CALL!                */

sel = form_alert(1, Busy_Tout );        /* Tell user busy timed out     */

if (sel == BEXIT)
  {
  bflag = EVNT_MSG;
  form_alert(1, Buzzoff);
  }
else
  {
  bflag = EVNT_MSG | EVNT_TIM;
  Get_Limit();
  }

return ( bflag );


} /* end Buddy_Toff() */


/*************************************************************/
/* Function:    Get_Limit()                                  */
/* Description: Get maximum time limit for Busy Buddy to run */
/* unattended.                                               */
/*************************************************************/

Get_Limit()

{ /* begin Get_Limit */

int bsel;

bsel = form_alert (3, Busy_Time );

switch ( bsel )
  { /* begin switch */
  case (SEL_FIVE):
    Time_Limit = FIVE_MIN;
    break;

  case (SEL_FIFTEEN):
    Time_Limit = FIFTEEN_MIN;
    break;

  case (SEL_SIXTY):
    Time_Limit = SIXTY_MIN;
    break;

  default:
    break;
  } /* end switch */

} /* end Get_Limit */


