#include "mpp.h"
#include <graphics/displayinfo.h>



STRPTR GetModeName(ULONG DisplayID)
{
  ULONG   alllen,
          deflen,
          len,
          monlen;
  UBYTE   monname[40];
  STRPTR  retval=0;
  struct  NameInfo nameinfo;
  STRPTR  deflabel,
          all;
 
  deflabel=GetString(MSG_OLD_SCREENMODES);
  deflen  =strlen(deflabel);
  deflen  =((DisplayID & MONITOR_ID_MASK) ? 0 : deflen); 
  all     =GetString(MSG_ALL_SCREENMODES);
  alllen  =strlen(all);
  
  if(DisplayID == INVALID_ID)
  {
    return(0);
  }
  else
  {
    if((DisplayID & ALL_MODES) == ALL_MODES)
    {
      if(monlen=GetMonitorName(DisplayID,monname,40))
      {
        len=monlen + deflen + alllen + 1 + 1;
        //                             ^ from "%s:%s"
        
        if(retval=AllocVec(len,MEMF_PUBLIC))
        {
          if(deflen)
            strcpy(retval,deflabel);
          sprintf(&retval[deflen],"%s:%s", monname, all);
        }
      }
    }
    else
    {
      if(GetDisplayInfoData(0,(UBYTE *)&nameinfo,sizeof(nameinfo), DTAG_NAME, DisplayID)>0)
      {
        if(nameinfo.Name)
        {
          if(retval=AllocVec(strlen(nameinfo.Name)+1+deflen,MEMF_PUBLIC))
          {
            if(deflen)
              strcpy(retval,deflabel);
            strcpy(&retval[deflen], nameinfo.Name);
          }
        }
      }
    
      /* Attempt  Monitor: Width x Height */
      if(!retval)
      {
        struct DimensionInfo diminfo;
        struct  DisplayInfo dispinfo;
    
        if(monlen=GetMonitorName(DisplayID,monname,40))
        {
          if((GetDisplayInfoData(0,(UBYTE *)&diminfo ,sizeof(diminfo) , DTAG_DIMS, DisplayID)>0) && 
             (GetDisplayInfoData(0,(UBYTE *)&dispinfo,sizeof(dispinfo), DTAG_DISP, DisplayID)>0) )
          {
            len=monlen + deflen + 39 +1;
            // "#### x #### AA LACED HAM EHB DUALPF PF2" 39
            if(retval=AllocVec(len,MEMF_PUBLIC))
            {
              if(deflen)
                 strcpy(retval,deflabel);
              sprintf(&retval[deflen],"%s:%4d x %4d",
                              monname,
                              (diminfo.Nominal.MaxX+1) & 0xffff,
                              (diminfo.Nominal.MaxY+1) & 0xffff);
              /*
              if(dispinfo.PropertyFlags & DIPF_IS_)
                strcat(retval,"");
              */
              /*
              if(dispinfo.PropertyFlags & DIPF_IS_AA)
                strcat(retval," AA");
  */
              if(dispinfo.PropertyFlags & DIPF_IS_DUALPF)
                strcat(retval," DPF");
              
              if(dispinfo.PropertyFlags & DIPF_IS_PF2PRI)
                strcat(retval," PF2");
  
              if(dispinfo.PropertyFlags & DIPF_IS_HAM)
                strcat(retval," HAM");
                
              if(dispinfo.PropertyFlags & DIPF_IS_EXTRAHALFBRITE)
                strcat(retval," EHB");
                
              if(dispinfo.PropertyFlags & DIPF_IS_LACE)
                strcat(retval," Laced");
  
            }
          }
        }
      }
     
      /* Last ditch effort, just display the DisplayID in hex */
      if(!retval)  
        if(retval=AllocVec(12,MEMF_PUBLIC))
          sprintf(retval,"ID:%8x",DisplayID);
    }
  }
  return(retval);
}


ULONG GetMonitorName(ULONG DisplayID, STRPTR Buffer, ULONG BufferLen)
{
  struct MonitorSpec *monspec;
  ULONG rv=0;

  
  Buffer[0]=0;//                                                                                       (4.57.7)
  
  if(DisplayID == INVALID_ID)//                                                 (4.57.7)
  { // if DisplayID = INVALID_ID//                                                                     (4.57.7)
    strncpy(Buffer,GetString(MSG_ANY_MONITOR),BufferLen);//                                            (4.57.7)
    BufferLen--;//                                                                                     (4.57.7)
    Buffer[BufferLen]=0;//                                                                             (4.57.7)
    rv=strlen(Buffer);//                                                                               (4.57.7)
  }//                                                                                                  (4.57.7)
  else
  {
    if(monspec=OpenMonitor(0,DisplayID & MONITOR_ID_MASK))
    {
      strncpy(Buffer,monspec->ms_Node.xln_Name,BufferLen);
      BufferLen--;
      Buffer[BufferLen]=0;
        
      // copy mon name an capitalize     
      for(rv=0;rv<BufferLen && Buffer[rv]!=0;rv++)
      {
        Buffer[rv]=ToUpper(Buffer[rv]);
        if(Buffer[rv]=='.')
           Buffer[rv]=0;
      }
      Buffer[BufferLen]=0;
    
      CloseMonitor(monspec);
    }
  }
  return(rv);
}

