/*
 * System.c :
 * ----------
 * Auteur        : Xavier Mertens
 * Version   $VER: 1.013
 * Création      : 11 Feb 1993 - 11:40
 * Modification  : -
 * Langage       : SAS/C V6.00
 * Compatibilité : 1.3 - 2.x - 3.0
 * Description   : Source principale.
 * Remarques     : Oml >system.lst -s -x system.lib l r system.o
 */

#include <exec/types.h>
#include <exec/execbase.h>
#include <exec/memory.h>
#include <devices/trackdisk.h>
#include <dos/dosextens.h>
#include <graphics/gfxbase.h>
#include <stdio.h>
#include <string.h>

#include <proto/exec.h>
#include <proto/dos.h>

#include "System.h" /* Fichier contenant les définitions standards */

static char *VER = "$VER: System C library 1.013 (11.02.93)";

/*
 * CheckOsVersion()
 * v1.000 - 11 Feb 1993 - 11:42
 * Renvoie la version du système présent en ROM.
 * Version : 33 -> Kickstart 1.2
 *           34 ->           1.3
 *           37 ->           2.0
 *           38 ->           2.1
 *           39 ->           3.0
 */

long CheckOsVersion(){
  extern struct ExecBase *SysBase;
  return(SysBase->LibNode.lib_Version);
}

/*
 * StartedFromWb()
 * v1.001 - 11 Feb 1993 - 11:52
 * Renvoie OUI si le programme est chargé depuis le Wbench et NON dans le
 * cas contraire.
 */

int StartedFromWb(){
  extern struct ExecBase *SysBase;
  if(((struct Process *)(SysBase->ThisTask))->pr_CLI)
    return(NON);
  else
    return(OUI);
}

/*
 * StrCheckSum(char *chaine)
 * v1.000 - 11 Feb 1993 - 11:57
 * Calcule la somme des valeurs ASCII contenues dans 'chaine'.
 */

long StrCheckSum(char chaine[]){
  register int i;
  register long cs=0;
  for(i=0;i<strlen(chaine);i++)
    cs+=chaine[i];
  return(cs);
}

/*
 * TestVideoMode()
 * v1.000 - 11 Feb 1993 - 12:00
 * Renvoie un code correspondant au mode d'affichage en cours :
 *  PAL,NTSC,ERREUR.
 */

long TestVideoMode(){
  long returncode;
  struct GfxBase *GfxBase;
  if(!(GfxBase = (struct GfxBase *)OpenLibrary("graphics.library",NULL)))
    return(ERREUR);
  else
    if(GfxBase->DisplayFlags & PAL)
      returncode = MODEPAL;
    else
      returncode = MODENTSC;
  CloseLibrary((struct Library *)GfxBase);
  return(returncode);
}

/*
 * ChipMem()
 * v1.000 - 11 Feb 1993 - 13:41
 * Renvoie la taille maximale ou courante de mémoire Chip dans le système.
 * Options: TOTAL/CURRENT
 * Renvoie la mémoire en Ko.
 */

long ChipMem(int mode){
  switch(mode){
    case TOTAL:
      return((long)AvailMem(MEMF_CHIP|MEMF_TOTAL)>>10);
    case CURRENT:
      return((long)AvailMem(MEMF_CHIP)>>10);
    default:
      return(ERREUR);
  }
}

/*
 * FastMem()
 * v1.000 - 11 Feb 1993 - 13:41
 * Renvoie la taille maximale ou courante de mémoire Fast dans le système.
 * Options: TOTAL/CURRENT
 * Renvoie la mémoire en Ko.
 */

long FastMem(int mode){
  switch(mode){
    case TOTAL:
      return((long)AvailMem(MEMF_FAST|MEMF_TOTAL)>>10);
    case CURRENT:
      return((long)AvailMem(MEMF_FAST)>>10);
    default:
      return(ERREUR);
  }
}

/*
 * ARexxPresent()
 * v1.000 - 11 Feb 1993 - 13:48
 * Renvoie OUI si ARexx est présent dans le système dans le cas contraire,
 * renvoie NON.
 *
 */

int ARexxPresent(){
  if(FindPort("REXX"))
    return(OUI);
  else
    return(NON);
}

/*
 * WaitMouse();
 * v1.000 - 11 Feb 1993 - 13:53
 * Attend un clic de souris.
 */
 
VOID WaitMouse(){
  char *Adresse;
  Adresse = (char *)0xBFE001;
  for(;;){
    if((*Adresse & 64) != 64)
      return;
  }
}

/*
 * WaitKey();
 * v1.000 - 11 Feb 1993 - 13:53
 * Attend l'appui sur une touche.
 */
 
VOID WaitKey(){
  char *Adresse;
  UWORD OldKey;
  Adresse = (char *)0xBFEC01;
  OldKey = *Adresse;
  for(;;){
    if(OldKey != *Adresse)
      return;
    OldKey = *Adresse;
  }
}

char *CurrentTime(){
  char Heure[10];
  struct DateStamp Maintenant;
  short Heures,
        Minutes,
        Secondes;
  DateStamp(&Maintenant);
  Heures = Maintenant.ds_Minute / 60;
  Minutes = Maintenant.ds_Minute % 60;
  Secondes = Maintenant.ds_Tick / 50;
  sprintf(Heure,"%2d:%2d:%2d",Heures,Minutes,Secondes);
  return((char *)Heure);
}

/*
 * FreeSpace()
 * v1.000 - 11 Feb 1993 - 14:37
 * Renvoie le nombre de blocs libres sur le device donné.
 */

long FreeSpace(char *Unite){
  struct InfoData *InfoData;
  BPTR lock;
  long Blocks;
  InfoData = AllocMem(sizeof(struct InfoData),MEMF_CLEAR);
  if(!(lock = Lock (Unite,ACCESS_READ)))
    return(ERREUR);
  if(!Info(lock,InfoData)){
    FreeMem(InfoData,sizeof(struct InfoData));
    return (ERREUR);
  }
  UnLock(lock);
  Blocks = (InfoData->id_NumBlocks) - (InfoData->id_NumBlocksUsed); 
  FreeMem(InfoData,sizeof(struct InfoData));
  return(Blocks);
}

/*
 * ProtectedDevice()
 * v1.000 - 11 Feb 1993 - 14:42
 * Renvoie OUI si le périphérique donné est protégé sinon,renvoie NON.
 */

int ProtectedDevice(char *Unite){
  struct InfoData *InfoData;
  BPTR lock;
  int Code=NON;
  if(InfoData=(struct InfoData *)AllocMem(sizeof(struct InfoData),MEMF_PUBLIC)){
    if(lock=Lock(Unite,ACCESS_READ)){
      if(Info(lock,InfoData)){
        if(InfoData->id_DiskState==ID_WRITE_PROTECTED)
          Code=OUI;
        else
          Code=NON;
      }
    }
    UnLock(lock);
  }
  FreeMem(InfoData,sizeof(struct InfoData));
  return(Code);
}

/*
 * DiskPresent()
 * v1.000 - 11 Feb 1993 - 14:58
 * Détecte la présence d'une disquette en DF[0-3]:
 */

int DiskPresent(int Unite){
  struct MsgPort *TDPort;
  struct IOExtTD TDReq;
  ULONG res;
  int Code=OUI;
  if(Unite < 0 || Unite > 3)
    return(ERREUR);
  TDPort = (struct MsgPort *)CreatePort("SystemLib.port",NULL);
  if(OpenDevice("trackdisk.device",Unite,(struct IORequest *)&TDReq,NULL))
    return(ERREUR);
  TDReq.iotd_Req.io_Message.mn_ReplyPort = TDPort;
  TDReq.iotd_Req.io_Command = TD_CHANGESTATE;
  DoIO((struct IORequest *)&TDReq);
  if(res = TDReq.iotd_Req.io_Actual){
    CloseDevice((struct IORequest *)&TDReq);
    Code = NON;
  }
  DeletePort(TDPort);
  return(Code);
}

/* Eof */
