/*****************************************************************************
 SOURCE NAME : PANEL.CC
 part of the Atari File Designer (AFD) source files
 for the Interface module.
------------------------------------------------------------------------------
 Written by Philippe VUILLERME (c) PVBest 2001
------------------------------------------------------------------------------
MAIN FEATURES:
  Main graphic interface using com.cc and prompts.cc
  prompts.cc and prompts.h taken from the source of the Atari 800 Emulator
  by David Firth and his Emulator development team.

HISTORY:
  PANEL.CC REV 2.00 (01 MARCH 2001):
    INTERFACE MODULE for the AFD.

******************************************************************************/

#include <stdio.h>
#include <strings.h>

#include <go32.h>
#include <dpmi.h>
#include <pc.h>
#include <conio.h>

#include "com.h"

/*Simple MS-DOS windows color attributes*/
// bits de l'octet couleur = XBBBCCCC  avec
// X = clignotement 0:NON / 1:OUI
// BBB = couleur de fond sur 3 bits : couleurs de 0 a 7
// CCCC = bits couleur de caracteres : de 0 a 15

#define FONDNOIR     0x00 // Noir
#define FONDBLEU     0x10 // Bleu
#define ENTETE       0x0e // Jaune
#define MENUTEXT     0x0b // Turquoise clair
#define MENUSELECT   0x0f // Blanc
#define MENUPOURTOUR 0x0e // Jaune

CMDTEXT fxcmd[10] =
{ {2, 3, "  F1:DISPLAY ATARI DRIVE STATUS ($53)  "},
  {2, 4, "  F2:READ SECTORS TO PC FILE ($52)     "},
  {2, 5, "  F3:WRITE SECTORS TO PC FILE ($57)    "},
  {2, 6, "  F4:FORMAT ATARI DISK ENHANCED ($22)  "},
  {2, 7, "  F5:                                  "},
  {41,3, "  F6:LOAD PC FILE IN MEMORY            "},
  {41,4, "  F7:READ & COMPARE ATARI DISK TO FILE "},
  {41,5, "  F8:READ ATARI DISK WITH VERIFICATION "},
  {41,6, "  F9:                                  "},
  {41,7, " F10:QUIT ATARI TO PC FILE DESIGNER    "} };

/*-----------------------------------------------------------------------*/

void Screen(char *title)
{
  int i;
  window(1,1,80,24);
  textattr(FONDNOIR+ENTETE);
  clrscr(); // En Fond Noir
  gotoxy((80-strlen(title))/2,1); // Jaune sur noir
  cputs(title); // afficher le titre
  textattr(FONDBLEU+MENUPOURTOUR); // Blanc sur Bleu pour le contour
  gotoxy(1,2);
  cputs("ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿");
  for (i=3;i<8;i++)
  {
    gotoxy(1,i);cputs("³");
    gotoxy(80,i);cputs("³");
  }
  gotoxy(1,8);
  cputs("ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ");

  textattr(FONDBLEU+MENUTEXT); // Turquoise clair sur bleu pour le texte menu
  for (i=0;i<10;i++)
  {
    gotoxy(fxcmd[i].fxposX,fxcmd[i].fxposY);
    cputs(fxcmd[i].fxtext);
  }

  textattr(FONDNOIR+VERT_CLAIR);
  gotoxy(1,21);
  cputs("ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ");

  //window(1,9,80,20);
  //window(1,22,80,24);  
}

int SelectionMenu(char position)
{
 window(1,1,80,10);
 textattr(FONDNOIR+MENUSELECT); //  sur Noir 
 gotoxy(fxcmd[position].fxposX,fxcmd[position].fxposY);
 cputs(fxcmd[position].fxtext);
 window(1,9,80,20);
 textattr(FONDNOIR + BLANC);
 return 0;
} 
int DeselectionMenu(char position)
{
 cprintf ("Press any key to return to the menu >");
 getch();
 clrscr();
 window(1,1,80,10);
 textattr(FONDBLEU+MENUTEXT);
 gotoxy(fxcmd[position].fxposX,fxcmd[position].fxposY);
 cputs(fxcmd[position].fxtext);
 gotoxy(1,10);
 return 0;
} 


/***************************************************************************/
int Panel_Menu(void)
{
  int key;
  int exitmenu;
  int resultat;
  char MenuItem;

  MenuItem = 0;
  exitmenu = FALSE;
  do{
    key=getch();
    if (key==0) key=256+getch();
    switch(key)
    {
      case 0x13B : /*F1 : Atari Drive Status*/
        MenuItem = 0;
        SelectionMenu(MenuItem);
        resultat = Display_Atari_DriveStatus();
        Display_Error_Log(resultat);
        DeselectionMenu(MenuItem);
        break;

      case 0x13C : /*F2 : Lecture de secteurs */
        MenuItem = 1;
        SelectionMenu(MenuItem);
        resultat = Read_Sector_To_PC_File();
        //si -1 ou -2 il y a eu une erreur de com
        Display_Error_Log(resultat);
        PC_File_Writing("Y");
        // 1 = pas de sauvegarde et -3 = erreur sur le fichier 
        DeselectionMenu(MenuItem);
        break;

      case 0x13D : /*F3 : Ecriture de secteurs */
        MenuItem = 2;
        SelectionMenu(MenuItem);
        resultat = Write_Sector_From_PC_File();
        if (resultat != -3)
           {
            Display_Error_Log(resultat); //afficher les erreurs de com possibles
           }
        DeselectionMenu(MenuItem);
        break;

      case 0x13E : /*F4 : Formatage Atari Drive disk*/
        MenuItem = 3;
        SelectionMenu(MenuItem);
        resultat = Format_Atari_Disk();
        Display_Error_Log(resultat);
        DeselectionMenu(MenuItem);
        break;

      case 0x13F : /*F5 : Verify Sector ???*/
        MenuItem = 4;
        SelectionMenu(MenuItem);
        // ???
        cprintf ("Verify Atari Disk Sectors / PAS IMPLEMENTE !!\n\r");
        DeselectionMenu(MenuItem);
        break;

      case 0x140 : /*F6 : Load PC file in memory*/
        MenuItem = 5;
        SelectionMenu(MenuItem);
        Load_Memory_With_PC_File(); //retourne -3 en cas d'erreur
        DeselectionMenu(MenuItem);
        break;

      case 0x141 : /*F7 : Read and Compare Atari disk with file in memory*/
        MenuItem = 6;
        SelectionMenu(MenuItem);
        resultat = Compare_Atari_Disk_To_Memory();
        // si pas d'erreur sur l'ouverture du fichier de comparaison
        if (resultat != -3)
           {
            Display_Error_Log(resultat); //afficher les erreurs de com possibles
            PC_File_Writing("N"); //sauvegarder les secteurs lus
           }
        DeselectionMenu(MenuItem);
        break;

      case 0x142 : /*F8 : Read and Verify Atari Disk to Memory */
        MenuItem = 7;
        SelectionMenu(MenuItem);
        resultat = Read_Verify_Atari_Disk();
        // si pas d'erreur sur l'ouverture du fichier de comparaison
        if (resultat != -3)
           {
            Display_Error_Log(resultat); //afficher les erreurs de com possibles
            PC_File_Writing("Y"); //sauvegarder les secteurs lus
           }        
        DeselectionMenu(MenuItem);
        break;

      case 0x143 : /*F9 : Change Parameters of AFD*/
        MenuItem = 8;
        SelectionMenu(MenuItem);
        // ???
        cprintf ("Changer les parametres de AFD : PAS IMPLEMENTE !!\n\r");
        DeselectionMenu(MenuItem);
        break;

      default:  //F10 ou ESC = Sortir...
        if (key ==27 || key==0x144) {exitmenu = TRUE;}
     }
  }while (exitmenu == FALSE);
return(0);
}

      /*Fx: File designer menu Make ATR/Edit/Extract ... */
     //cprintf ("AFD menu Make ATR / Edit File / View Directory : PAS IMPLEMENTE !!\n\r");
/*------------------------------------------------------------------------*/

_go32_dpmi_seginfo old_tick_handler, new_tick_handler;

void tick_handler(void)
{
	asm("cli; pusha");
      // do the stuff here...
	outportb(0x20, 0x20);
	asm("popa; sti");
}

void tick_init(void)
{
        //lo=hi=0;
	new_tick_handler.rm_offset = (int) tick_handler;
	//new_tick_handler.rm_segment = _go32_dos_cs(); !!!
        //if (_allocate_realmode_memory(&tick_handler) < 0)
        //    { // error !!
        //    }
	_go32_dpmi_get_protected_mode_interrupt_vector(0x08, &old_tick_handler);
	// ??? _go32_dpmi_allocate_iret_wrapper(&new_tick_handler);
	_go32_dpmi_set_real_mode_interrupt_vector(0x08, &new_tick_handler);
}
void key_done(void)
{
	_go32_dpmi_set_protected_mode_interrupt_vector(0x08, &old_tick_handler);
}

int main(void)
{
  int ok;

  /* Afficher l'ecran de control */
  _set_screen_lines(25);
  //_setcursortype(_NOCURSOR);
  Screen("ATARI TO PC FILE DESIGNER REV 1.70 by Phil (c) 2001");

  /* Initialiser le port */
  Init_Com_Port();

  /* Attendre et executer les commandes */
  ok = Panel_Menu();

  /* Ferme le port */
  Exit_Com_Port();

  /* Restauration de l'ecran initial */
  window(1,1,80,25);
  textattr(0xf);
  clrscr();
  _setcursortype(_NORMALCURSOR);
  
}


