//*****************************************************************************
//
// File name: SystemDetection.cs
// Location: See Matrox Example Launcher in the MIL Control Center
// 
//
// Synopsis:  This program shows how to use the MappInquire(M_INSTALLED_... inquires to detect
//            installed systems types and display their names using SystemDetection.
//
// Copyright (C) Matrox Electronic Systems Ltd., 1992-2020.
// All Rights Reserved
//*****************************************************************************
using System;
using System.Runtime.InteropServices;
using System.Text;

using Matrox.MatroxImagingLibrary;

namespace SystemDetection
   {
   class Program
      {
      static readonly MIL_INT IMAGE_SIZEX = 640;
      static readonly MIL_INT IMAGE_SIZEY = 480;

      static void Main(string[] args)
         {
         MIL_ID MilApplication = MIL.M_NULL;
         MIL_INT NbAvailableSystems = MIL.M_NULL;

         MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, ref MilApplication);
         MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_COUNT, ref NbAvailableSystems);

         MIL_ID [] MilSystem  = new MIL_ID[NbAvailableSystems];
         MIL_ID [] MilDisplay = new MIL_ID[NbAvailableSystems];
         MIL_ID [] MilBuffer  = new MIL_ID[NbAvailableSystems];

         StringBuilder CurrentBoardName = new StringBuilder();
         StringBuilder CurrentDisplayableBoardName = new StringBuilder();
         MIL_INT CurrentSystemType = 0;
         MIL_INT DeviceCount = 0;
         MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE);

         Console.Write(NbAvailableSystems + " system(s) found\n\n\n");

         for(MIL_INT i = 0; i < NbAvailableSystems; i++)
            {
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_DESCRIPTOR + i, CurrentBoardName);
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_PRINT_NAME + i, CurrentDisplayableBoardName);
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_TYPE + i, ref CurrentSystemType);
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_DEVICE_COUNT + i, ref DeviceCount);      
      
            //Console.Write("Calling MsysAlloc("+CurrentBoardName+", ...) to allocate the ("+CurrentSystemType+") "+CurrentDisplayableBoardName+" ... \n");
            
            Console.Write("\nCalling MsysAlloc() with System Descriptor: "+CurrentBoardName+" .");
            Console.Write("\n                         System Type      : (" + CurrentSystemType + ") .");
            if(DeviceCount != MIL.M_UNKNOWN)
                Console.Write("\n                         System Count     : (" + DeviceCount + ") .");
            Console.Write("\n                         System Print Name: "+CurrentDisplayableBoardName+" .\n");

            MIL.MsysAlloc(MilApplication, CurrentBoardName.ToString(), MIL.M_DEFAULT, (long)MIL.M_DEFAULT, ref MilSystem[i]);

            if(MilSystem[i] != MIL.M_NULL)
               {
               StringBuilder DisplayableSystemName = new StringBuilder();
               String DispFormat = "M_DEFAULT";
               MIL.MsysInquire(MilSystem[i], MIL.M_SYSTEM_DESCRIPTOR, DisplayableSystemName);
               MIL.MbufAlloc2d(MilSystem[i], IMAGE_SIZEX, IMAGE_SIZEY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_DISP, ref MilBuffer[i]);
               MIL.MdispAlloc(MilSystem[i], MIL.M_DEFAULT, DispFormat, MIL.M_DEFAULT, ref MilDisplay[i]);
               MIL.MdispSelect(MilDisplay[i], MilBuffer[i]);

               String WriteMessage = "Allocated " + DisplayableSystemName;           
         
               MIL.MgraText(MIL.M_DEFAULT, MilBuffer[i], IMAGE_SIZEX / 5, IMAGE_SIZEY / 3, WriteMessage);
               }
            else
               {
               if(DeviceCount != 0)
                   Console.WriteLine("\nBoard driver for " + CurrentBoardName + " failed to initialize.");
               else
                   Console.WriteLine("\nNo " + CurrentBoardName + " are present in the system.");
               }
            }
         Console.WriteLine("\n\nPress <Enter> to quit the application");
         Console.ReadKey();

         MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_ENABLE);

         for(MIL_INT SysCount = 0; SysCount < NbAvailableSystems; SysCount++)
            {
            if(MilBuffer[SysCount] != MIL.M_NULL)
               {
               MIL.MbufFree(MilBuffer[SysCount]);
               }
            if(MilDisplay[SysCount] != MIL.M_NULL)
               {
               MIL.MdispFree(MilDisplay[SysCount]);
               }
            if (MilSystem[SysCount] != MIL.M_NULL)
               {
               MIL.MsysFree(MilSystem[SysCount]);
               }
            }

         MIL.MappFree(MilApplication);
         }
      }
   }