'*****************************************************************************
'
' 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
'*****************************************************************************
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Imports System.Text

Imports Matrox.MatroxImagingLibrary

Namespace SystemDetection
   Friend Class Program

      Private Const IMAGE_SIZEX As Integer = 640
      Private Const IMAGE_SIZEY As Integer = 480

      Shared Sub Main(ByVal args() As String)
         Dim MilApplication As MIL_ID = MIL.M_NULL
         Dim NbAvailableSystems As MIL_INT = MIL.M_NULL

         MIL.MappAlloc(MIL.M_NULL, MIL.M_DEFAULT, MilApplication)
         MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_COUNT, NbAvailableSystems)
         Dim MilSystem(Convert.ToInt32(NbAvailableSystems)) As MIL_ID
         Dim MilDisplay(Convert.ToInt32(NbAvailableSystems)) As MIL_ID
         Dim MilBuffer(Convert.ToInt32(NbAvailableSystems)) As MIL_ID

         Dim CurrentBoardName As StringBuilder = New StringBuilder()
         Dim CurrentDisplayableBoardName As StringBuilder = New StringBuilder()
            Dim CurrentSystemType As MIL_INT = 0
            Dim DeviceCount As MIL_INT = 0
         MIL.MappControl(MIL.M_DEFAULT, MIL.M_ERROR, MIL.M_PRINT_DISABLE)

         Console.Write(NbAvailableSystems.ToString() + " system(s) found" + Constants.vbLf + Constants.vbLf + Constants.vbLf)
         For counter As MIL_INT = 0 To (NbAvailableSystems - 1)
            Dim IntCounter As Int32 = Convert.ToInt32(counter)
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_DESCRIPTOR + counter, CurrentBoardName)
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_PRINT_NAME + counter, CurrentDisplayableBoardName)
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_TYPE + counter, CurrentSystemType)
            MIL.MappInquire(MilApplication, MIL.M_INSTALLED_SYSTEM_DEVICE_COUNT + counter, DeviceCount)

            'Console.Write("Calling MsysAlloc(" + CurrentBoardName.ToString() + ", ...) to allocate the (" + CurrentSystemType.ToString() + ") " + CurrentDisplayableBoardName + " ... " + Constants.vbLf)
            Console.Write(vbNewLine + "Calling MsysAlloc() with System Descriptor: " + CurrentBoardName.ToString() + " .")
            Console.Write(vbNewLine + "                         System Type      : (" + CurrentSystemType.ToString() + ") .")
            If (DeviceCount <> MIL.M_UNKNOWN) Then
               Console.Write(vbNewLine + "                         System Count     : (" + DeviceCount.ToString() + ") .")
            End If
            Console.Write(vbNewLine + "                         System Print Name: " + CurrentDisplayableBoardName.ToString() + " ." + vbNewLine)

            MIL.MsysAlloc(MilApplication, CurrentBoardName.ToString(), MIL.M_DEFAULT, MIL.M_DEFAULT, MilSystem(Convert.ToInt32(counter)))

            If (MilSystem(IntCounter) <> MIL.M_NULL) Then
               Dim DisplayableSystemName As StringBuilder = New StringBuilder()
               Dim DispFormat As String = "M_DEFAULT"
               MIL.MsysInquire(MilSystem(IntCounter), MIL.M_SYSTEM_DESCRIPTOR, DisplayableSystemName)
               MIL.MbufAlloc2d(MilSystem(IntCounter), IMAGE_SIZEX, IMAGE_SIZEY, 8 + MIL.M_UNSIGNED, MIL.M_IMAGE + MIL.M_DISP, MilBuffer(IntCounter))
               MIL.MdispAlloc(MilSystem(IntCounter), MIL.M_DEFAULT, DispFormat, MIL.M_DEFAULT, MilDisplay(IntCounter))
               MIL.MdispSelect(MilDisplay(IntCounter), MilBuffer(IntCounter))

               Dim WriteMessage As String = "Allocated " + DisplayableSystemName.ToString()

               MIL.MgraText(MIL.M_DEFAULT, MilBuffer(IntCounter), IMAGE_SIZEX / 5, IMAGE_SIZEY / 3, WriteMessage)
            Else
               If (DeviceCount <> 0) Then
                  Console.WriteLine("\nBoard driver for " + CurrentBoardName.ToString() + " failed to initialize.")
               Else
                  Console.WriteLine("\nNo " + CurrentBoardName.ToString() + " are present in the system.")
               End If

            End If
         Next counter
         Console.WriteLine(vbNewLine + vbNewLine + "Press <Enter> to quit the application")
         Console.ReadKey()

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

         For SystemCount As MIL_INT = 0 To (NbAvailableSystems - 1)
            Dim IntSystemCount As Int32 = Convert.ToInt32(SystemCount)

            If (MilBuffer(IntSystemCount) <> MIL.M_NULL) Then
               MIL.MbufFree(MilBuffer(IntSystemCount))
            End If

            If (MilDisplay(IntSystemCount) <> MIL.M_NULL) Then
               MIL.MdispFree(MilDisplay(IntSystemCount))
            End If

            If (MilSystem(IntSystemCount) <> MIL.M_NULL) Then
               MIL.MsysFree(MilSystem(IntSystemCount))
            End If

         Next SystemCount

         MIL.MappFree(MilApplication)
      End Sub
   End Class
End Namespace