Hardware/Nvidia/NvidiaGroup.cs
author StephaneLenclud
Thu, 18 Apr 2013 23:25:10 +0200
changeset 402 ded1323b61ee
parent 298 96263190189a
permissions -rw-r--r--
Front View plug-in does not init if no sensor added.
Fixing some format to make strings shorter.
Now trying to start SoundGraphAccess.exe process from same directory.
Packed mode now can display three sensors along with the current time.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2011 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System.Collections.Generic;
    12 using System.Globalization;
    13 using System.Text;
    14 
    15 namespace OpenHardwareMonitor.Hardware.Nvidia {
    16 
    17   internal class NvidiaGroup : IGroup {
    18    
    19     private readonly List<Hardware> hardware = new List<Hardware>();
    20     private readonly StringBuilder report = new StringBuilder();
    21 
    22     public NvidiaGroup(ISettings settings) {
    23       if (!NVAPI.IsAvailable)
    24         return;
    25 
    26       report.AppendLine("NVAPI");
    27       report.AppendLine();
    28 
    29       string version;
    30       if (NVAPI.NvAPI_GetInterfaceVersionString(out version) == NvStatus.OK) {
    31         report.Append("Version: ");
    32         report.AppendLine(version);
    33       }
    34 
    35       NvPhysicalGpuHandle[] handles = 
    36         new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    37       int count;
    38       if (NVAPI.NvAPI_EnumPhysicalGPUs == null) {
    39         report.AppendLine("Error: NvAPI_EnumPhysicalGPUs not available");
    40         report.AppendLine();
    41         return;
    42       } else {        
    43         NvStatus status = NVAPI.NvAPI_EnumPhysicalGPUs(handles, out count);
    44         if (status != NvStatus.OK) {
    45           report.AppendLine("Status: " + status);
    46           report.AppendLine();
    47           return;
    48         }
    49       }
    50 
    51       IDictionary<NvPhysicalGpuHandle, NvDisplayHandle> displayHandles =
    52         new Dictionary<NvPhysicalGpuHandle, NvDisplayHandle>();
    53 
    54       if (NVAPI.NvAPI_EnumNvidiaDisplayHandle != null &&
    55         NVAPI.NvAPI_GetPhysicalGPUsFromDisplay != null) 
    56       {
    57         NvStatus status = NvStatus.OK;
    58         int i = 0;
    59         while (status == NvStatus.OK) {
    60           NvDisplayHandle displayHandle = new NvDisplayHandle();
    61           status = NVAPI.NvAPI_EnumNvidiaDisplayHandle(i, ref displayHandle);
    62           i++;
    63 
    64           if (status == NvStatus.OK) {
    65             NvPhysicalGpuHandle[] handlesFromDisplay =
    66               new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    67             uint countFromDisplay;
    68             if (NVAPI.NvAPI_GetPhysicalGPUsFromDisplay(displayHandle,
    69               handlesFromDisplay, out countFromDisplay) == NvStatus.OK) {
    70               for (int j = 0; j < countFromDisplay; j++) {
    71                 if (!displayHandles.ContainsKey(handlesFromDisplay[j]))
    72                   displayHandles.Add(handlesFromDisplay[j], displayHandle);
    73               }
    74             }
    75           }
    76         }
    77       }
    78 
    79       report.Append("Number of GPUs: ");
    80       report.AppendLine(count.ToString(CultureInfo.InvariantCulture));
    81 
    82       for (int i = 0; i < count; i++) {
    83         NvDisplayHandle displayHandle;
    84         displayHandles.TryGetValue(handles[i], out displayHandle);
    85         hardware.Add(new NvidiaGPU(i, handles[i], displayHandle, settings));
    86       }
    87 
    88       report.AppendLine();
    89     }
    90 
    91     public IHardware[] Hardware {
    92       get {
    93         return hardware.ToArray();
    94       }
    95     }
    96 
    97     public string GetReport() {
    98       return report.ToString();
    99     }
   100 
   101     public void Close() {
   102       foreach (Hardware gpu in hardware)
   103         gpu.Close();      
   104     }
   105   }
   106 }