Hardware/Nvidia/NvidiaGroup.cs
author moel.mich
Thu, 12 Aug 2010 20:53:27 +0000
changeset 166 fa9dfbfc4145
parent 165 813d8bc3192f
child 182 4801e9eaf979
permissions -rw-r--r--
Changed the project files to Visual Studio 2010. Fixed some Code Analysis warnings.
     1 /*
     2   
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
     4 
     5   The contents of this file are subject to the Mozilla Public License Version
     6   1.1 (the "License"); you may not use this file except in compliance with
     7   the License. You may obtain a copy of the License at
     8  
     9   http://www.mozilla.org/MPL/
    10 
    11   Software distributed under the License is distributed on an "AS IS" basis,
    12   WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
    13   for the specific language governing rights and limitations under the License.
    14 
    15   The Original Code is the Open Hardware Monitor code.
    16 
    17   The Initial Developer of the Original Code is 
    18   Michael Möller <m.moeller@gmx.ch>.
    19   Portions created by the Initial Developer are Copyright (C) 2009-2010
    20   the Initial Developer. All Rights Reserved.
    21 
    22   Contributor(s):
    23 
    24   Alternatively, the contents of this file may be used under the terms of
    25   either the GNU General Public License Version 2 or later (the "GPL"), or
    26   the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
    27   in which case the provisions of the GPL or the LGPL are applicable instead
    28   of those above. If you wish to allow use of your version of this file only
    29   under the terms of either the GPL or the LGPL, and not to allow others to
    30   use your version of this file under the terms of the MPL, indicate your
    31   decision by deleting the provisions above and replace them with the notice
    32   and other provisions required by the GPL or the LGPL. If you do not delete
    33   the provisions above, a recipient may use your version of this file under
    34   the terms of any one of the MPL, the GPL or the LGPL.
    35  
    36 */
    37 
    38 using System;
    39 using System.Collections.Generic;
    40 using System.Globalization;
    41 using System.Text;
    42 
    43 namespace OpenHardwareMonitor.Hardware.Nvidia {
    44 
    45   internal class NvidiaGroup : IGroup {
    46    
    47     private List<IHardware> hardware = new List<IHardware>();
    48     private StringBuilder report = new StringBuilder();
    49 
    50     public NvidiaGroup(ISettings settings) {
    51       if (!NVAPI.IsAvailable)
    52         return;
    53 
    54       report.AppendLine("NVAPI");
    55       report.AppendLine();
    56 
    57       string version;
    58       if (NVAPI.NvAPI_GetInterfaceVersionString(out version) == NvStatus.OK) {
    59         report.Append("Version: ");
    60         report.AppendLine(version);
    61       }
    62 
    63       NvPhysicalGpuHandle[] handles = 
    64         new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    65       int count;
    66       if (NVAPI.NvAPI_EnumPhysicalGPUs == null) {
    67         report.AppendLine("Error: NvAPI_EnumPhysicalGPUs not available");
    68         report.AppendLine();
    69         return;
    70       } else {        
    71         NvStatus status = NVAPI.NvAPI_EnumPhysicalGPUs(handles, out count);
    72         if (status != NvStatus.OK) {
    73           report.AppendLine("Status: " + status.ToString());
    74           report.AppendLine();
    75           return;
    76         }
    77       }
    78 
    79       IDictionary<NvPhysicalGpuHandle, NvDisplayHandle> displayHandles =
    80         new Dictionary<NvPhysicalGpuHandle, NvDisplayHandle>();
    81 
    82       if (NVAPI.NvAPI_EnumNvidiaDisplayHandle != null &&
    83         NVAPI.NvAPI_GetPhysicalGPUsFromDisplay != null) 
    84       {
    85         NvStatus status = NvStatus.OK;
    86         int i = 0;
    87         while (status == NvStatus.OK) {
    88           NvDisplayHandle displayHandle = new NvDisplayHandle();
    89           status = NVAPI.NvAPI_EnumNvidiaDisplayHandle(i, ref displayHandle);
    90           i++;
    91 
    92           if (status == NvStatus.OK) {
    93             NvPhysicalGpuHandle[] handlesFromDisplay =
    94               new NvPhysicalGpuHandle[NVAPI.MAX_PHYSICAL_GPUS];
    95             uint countFromDisplay;
    96             if (NVAPI.NvAPI_GetPhysicalGPUsFromDisplay(displayHandle,
    97               handlesFromDisplay, out countFromDisplay) == NvStatus.OK) {
    98               for (int j = 0; j < countFromDisplay; j++) {
    99                 if (!displayHandles.ContainsKey(handlesFromDisplay[j]))
   100                   displayHandles.Add(handlesFromDisplay[j], displayHandle);
   101               }
   102             }
   103           }
   104         }
   105       }
   106 
   107       report.Append("Number of GPUs: ");
   108       report.AppendLine(count.ToString(CultureInfo.InvariantCulture));      
   109       
   110       for (int i = 0; i < count; i++) {    
   111         NvDisplayHandle displayHandle;
   112         if (displayHandles.TryGetValue(handles[i], out displayHandle))
   113           hardware.Add(new NvidiaGPU(i, handles[i], displayHandle, settings));                            
   114         else
   115           hardware.Add(new NvidiaGPU(i, handles[i], null, settings));   
   116       }
   117 
   118       report.AppendLine();
   119     }
   120 
   121     public IHardware[] Hardware {
   122       get {
   123         return hardware.ToArray();
   124       }
   125     }
   126 
   127     public string GetReport() {
   128       return report.ToString();
   129     }
   130 
   131     public void Close() { }
   132   }
   133 }