{************************************************************************
 *									*
 *  Unit KL_API	  KEYBIT Lite API access module				*
 *  Public Domain 1996 Pino Navato					*
 *									*
 ************************************************************************}

Unit KL_API;		{ ** BP 7+ required ** }
{$X+}

interface

const KLACTIVE_FLAG	= 128;
      EMAILSUPPORT_FLAG = 64;

function Search4Keybit : word;
function GetSBptr : pointer;
procedure EnableEmailSupport;
procedure RestoreKeybitStatus;


implementation

uses Strings, DOS;


const AMIS_INSTCHECK	= 0;  	       { installation check }
      KLAPI_GET_SB_PTR	= $10;	       { get pointer to status byte }

      AMIS_SUCCESSFUL	= $FF;


var mpx        : byte;
    regs       : registers;



{ +-----  Search4Keybit  -----------------------------------------------+
  |  Purpose:	Search for KEYBIT Lite v5+ in memory.			|
  |  Note:	This function sets the global variable mpx used		|
  |		by GetSBptr.						|
  |  Return:	Full version number if found, 0 if not found.		|
  +---------------------------------------------------------------------+ }

function Search4Keybit: word;
   const KL_sig : PChar = 'Pino NavKeybit  ';	{ KEYBIT's AMIS signature }
   var	 sig	: PChar;			{ generic AMIS signature }
   begin
   { loop through all 256 multiplex numbers, checking signatures we find }
      for mpx := 0 to 255 do
	 begin
	    regs.ah := mpx;
	    regs.al := AMIS_INSTCHECK;		{ installation check }
	    intr($2D, regs);
	    if regs.al = AMIS_SUCCESSFUL then	{ installed? }
	       begin
		  sig := ptr(regs.dx, regs.di);
		  if StrComp(sig, KL_sig) = 0 then   { KEYBIT Lite? }
		     if regs.ch >= 5 then   	     { v5+ ? }
			begin
			   Search4Keybit := regs.cx; { YES! Return full v. number }
			   exit
			end
		     else
			break	{ NO, it's an older version.  Stop searching. }
	       end
	 end;
      Search4Keybit := 0
   end;



{ +-----  GetSBptr  ----------------------------------------------------+
  |  Purpose:	Get a far pointer to KEYBIT Lite status byte.		|
  |  Return:	Far pointer to KEYBIT Lite status byte			|
  |		or nil if KEYBIT Lite v5+ not installed.		|
  +---------------------------------------------------------------------+ }

function GetSBptr: pointer;
   begin
      if Search4Keybit = 0 then
	 GetSBptr := nil
      else
	 begin
	    regs.ah := mpx;
	    regs.al := KLAPI_GET_SB_PTR;
	    intr($2D, regs);
	    GetSBptr := ptr(regs.dx, regs.bx)
	 end
   end;



const status_ptr : ^byte = nil;
var   old_status : byte;

{ +-----  EnableEmailSupport  ------------------------------------------+
  |  Purpose:	Set e-mail support flag to 1 and save original status	|
  |		in the global variable old_status.			|
  |  Note:	This procedure has no effect if KEYBIT Lite isn't in	|
  |		memory.							|
  +---------------------------------------------------------------------+ }

procedure EnableEmailSupport;
   begin
      status_ptr := GetSBptr;
      if status_ptr <> nil then
	 begin
	    old_status := status_ptr^;
	    status_ptr^ := status_ptr^ or EMAILSUPPORT_FLAG
	 end
   end;



{ +-----  RestoreKeybitStatus  -----------------------------------------+
  |  Purpose:	Restore KEYBIT Lite's original status from the global	|
  |		variable old_status.					|
  |  Note:	This procedure has no effect if KEYBIT Lite isn't in	|
  |		memory or if EnableEmailSupport hasn't been called.	|
  +---------------------------------------------------------------------+ }

procedure RestoreKeybitStatus;
   begin
      if status_ptr <> nil then
	 status_ptr^ := old_status
   end;


end.
