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