Hardware/Computer.cs
author moel.mich
Tue, 17 Aug 2010 20:25:27 +0000
changeset 169 3b2bcba8c02d
parent 166 fa9dfbfc4145
child 171 81ab5e53122e
permissions -rw-r--r--
Restricted ITE detection to port 0x2E.
     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.IO;
    41 using System.Globalization;
    42 using System.Text;
    43 using System.Threading;
    44 using System.Security;
    45 using System.Security.Permissions;
    46 
    47 
    48 namespace OpenHardwareMonitor.Hardware {
    49 
    50   public class Computer : IComputer {
    51 
    52     private List<IGroup> groups = new List<IGroup>();
    53 
    54     private bool open = false;
    55     private bool hddEnabled = false;
    56     private ISettings settings;
    57 
    58     public Computer() {
    59       this.settings = new Settings();
    60     }
    61 
    62     public Computer(ISettings settings) {
    63       if (settings != null)
    64         this.settings = settings;
    65       else {
    66         this.settings = new Settings();
    67       }
    68     }
    69 
    70     private void Add(IGroup group) {
    71       if (groups.Contains(group))
    72         return;
    73 
    74       groups.Add(group);
    75 
    76       if (HardwareAdded != null)
    77         foreach (IHardware hardware in group.Hardware)
    78           HardwareAdded(hardware);
    79     }
    80 
    81     private void Remove(IGroup group) {
    82       if (!groups.Contains(group))
    83         return;
    84 
    85       groups.Remove(group);
    86 
    87       if (HardwareRemoved != null)
    88         foreach (IHardware hardware in group.Hardware)
    89           HardwareRemoved(hardware);
    90     }
    91 
    92     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
    93     public void Open() {
    94       if (open)
    95         return;
    96 
    97       WinRing0.Open();
    98 
    99       Add(new Mainboard.MainboardGroup(settings));
   100       Add(new CPU.CPUGroup(settings));
   101       Add(new ATI.ATIGroup(settings));
   102       Add(new Nvidia.NvidiaGroup(settings));
   103       Add(new TBalancer.TBalancerGroup(settings));
   104 
   105       if (hddEnabled)
   106         Add(new HDD.HDDGroup(settings));
   107 
   108       open = true;
   109     }
   110     
   111     public bool HDDEnabled {
   112       get { return hddEnabled; }
   113 
   114       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
   115       set {
   116         if (open && value && !hddEnabled) {
   117           Add(new HDD.HDDGroup(settings));
   118         } else if (open && !value && hddEnabled) {
   119           List<IGroup> list = new List<IGroup>();
   120           foreach (IGroup group in groups)
   121             if (group is HDD.HDDGroup)
   122               list.Add(group);
   123           foreach (IGroup group in list)
   124             Remove(group);
   125         }
   126         hddEnabled = value;
   127       }
   128     }
   129 
   130     public IHardware[] Hardware {
   131       get {
   132         List<IHardware> list = new List<IHardware>();
   133         foreach (IGroup group in groups)
   134           foreach (IHardware hardware in group.Hardware)
   135             list.Add(hardware);
   136         return list.ToArray();
   137       }
   138     }
   139 
   140     private static void NewSection(TextWriter writer) {
   141       for (int i = 0; i < 8; i++)
   142         writer.Write("----------");
   143       writer.WriteLine();
   144       writer.WriteLine();
   145     }
   146 
   147     private int CompareSensor(ISensor a, ISensor b) {
   148       int c = a.SensorType.CompareTo(b.SensorType);
   149       if (c == 0)
   150         return a.Index.CompareTo(b.Index);
   151       else
   152         return c;
   153     }
   154 
   155     private void ReportHardwareSensorTree(IHardware hardware, TextWriter w,
   156       string space) {
   157       w.WriteLine("{0}|", space);
   158       w.WriteLine("{0}+-+ {1} ({2})",
   159         space, hardware.Name, hardware.Identifier);
   160       ISensor[] sensors = hardware.Sensors;
   161       Array.Sort<ISensor>(sensors, CompareSensor);
   162       foreach (ISensor sensor in sensors) {
   163         w.WriteLine("{0}|   +- {1}[{2}] : {3} : {4}",
   164           space, sensor.SensorType, sensor.Index, 
   165             string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
   166             sensor.Value, sensor.Min, sensor.Max), sensor.Name);
   167       }
   168       foreach (IHardware subHardware in hardware.SubHardware)
   169         ReportHardwareSensorTree(subHardware, w, "|   ");
   170     }
   171 
   172     private void ReportHardwareParameterTree(IHardware hardware, TextWriter w,
   173       string space) {
   174       w.WriteLine("{0}|", space);
   175       w.WriteLine("{0}+-+ {1} ({2})",
   176         space, hardware.Name, hardware.Identifier);
   177       ISensor[] sensors = hardware.Sensors;
   178       Array.Sort<ISensor>(sensors, CompareSensor);
   179       foreach (ISensor sensor in sensors) {
   180         if (sensor.Parameters.Length > 0) {
   181           w.WriteLine("{0}|   +- {1}[{2}] : {3}",
   182             space, sensor.SensorType, sensor.Index, sensor.Name);
   183           foreach (IParameter parameter in sensor.Parameters) {
   184             w.WriteLine("{0}|      +- {1} : {2}",
   185               space, parameter.Name,
   186               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
   187                 parameter.DefaultValue, parameter.Value));
   188           }
   189         }
   190       }
   191       foreach (IHardware subHardware in hardware.SubHardware)
   192         ReportHardwareParameterTree(subHardware, w, "|   ");
   193     }
   194 
   195     private void ReportHardware(IHardware hardware, TextWriter w) {
   196       string hardwareReport = hardware.GetReport();
   197       if (!string.IsNullOrEmpty(hardwareReport)) {
   198         NewSection(w);
   199         w.Write(hardwareReport);
   200       }
   201       foreach (IHardware subHardware in hardware.SubHardware)
   202         ReportHardware(subHardware, w);
   203     }
   204 
   205     public string GetReport() {
   206 
   207       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
   208 
   209         w.WriteLine();
   210         w.WriteLine("Open Hardware Monitor Report");
   211         w.WriteLine();
   212 
   213         Version version = typeof(Computer).Assembly.GetName().Version;
   214 
   215         NewSection(w);
   216         w.Write("Version: "); w.WriteLine(version.ToString());
   217         w.WriteLine();
   218 
   219         NewSection(w);
   220         w.Write("Common Language Runtime: ");
   221         w.WriteLine(Environment.Version.ToString());
   222         w.Write("Operating System: ");
   223         w.WriteLine(Environment.OSVersion.ToString());
   224         w.Write("Process Type: ");
   225         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
   226         w.WriteLine();
   227 
   228         NewSection(w);
   229         w.WriteLine("Sensors");
   230         w.WriteLine();
   231         foreach (IGroup group in groups) {
   232           foreach (IHardware hardware in group.Hardware)
   233             ReportHardwareSensorTree(hardware, w, "");
   234         }
   235         w.WriteLine();
   236 
   237         NewSection(w);
   238         w.WriteLine("Parameters");
   239         w.WriteLine();
   240         foreach (IGroup group in groups) {
   241           foreach (IHardware hardware in group.Hardware)
   242             ReportHardwareParameterTree(hardware, w, "");
   243         }
   244         w.WriteLine();
   245 
   246         foreach (IGroup group in groups) {
   247           string report = group.GetReport();
   248           if (!string.IsNullOrEmpty(report)) {
   249             NewSection(w);
   250             w.Write(report);
   251           }
   252 
   253           IHardware[] hardwareArray = group.Hardware;
   254           foreach (IHardware hardware in hardwareArray)
   255             ReportHardware(hardware, w);
   256 
   257         }
   258         return w.ToString();
   259       }
   260     }
   261 
   262     public void Close() {      
   263       if (!open)
   264         return;
   265 
   266       foreach (IGroup group in groups)
   267         group.Close();
   268       groups.Clear();
   269 
   270       WinRing0.Close();
   271 
   272       open = false;
   273     }
   274 
   275     public event HardwareEventHandler HardwareAdded;
   276     public event HardwareEventHandler HardwareRemoved;
   277 
   278     public void Accept(IVisitor visitor) {
   279       if (visitor == null)
   280         throw new ArgumentNullException("visitor");
   281       visitor.VisitComputer(this);
   282     }
   283 
   284     public void Traverse(IVisitor visitor) {
   285       foreach (IGroup group in groups)
   286         foreach (IHardware hardware in group.Hardware) 
   287           hardware.Accept(visitor);
   288     }
   289 
   290     private class Settings : ISettings {
   291 
   292       public bool Contains(string name) {
   293         return false;
   294       }
   295 
   296       public void SetValue(string name, string value) { }
   297 
   298       public string GetValue(string name, string value) {
   299         return value;
   300       }
   301 
   302       public void Remove(string name) { }
   303     }
   304   }
   305 }