Improved the CPU load sensors. The values displayed for the load per core and the total load should now be more accurate.
     3   Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
     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
 
     9   http://www.mozilla.org/MPL/
 
    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.
 
    15   The Original Code is the Open Hardware Monitor code.
 
    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.
 
    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.
 
    39 using System.Collections.Generic;
 
    40 using System.Globalization;
 
    42 using System.Security.Permissions;
 
    43 using System.Reflection;
 
    45 namespace OpenHardwareMonitor.Hardware {
 
    47   public class Computer : IComputer {
 
    49     private readonly List<IGroup> groups = new List<IGroup>();
 
    50     private readonly ISettings settings;
 
    53     private bool hddEnabled;    
 
    56       this.settings = new Settings();
 
    59     public Computer(ISettings settings) {
 
    60       this.settings = settings ?? new Settings();
 
    63     private void Add(IGroup group) {
 
    64       if (groups.Contains(group))
 
    69       if (HardwareAdded != null)
 
    70         foreach (IHardware hardware in group.Hardware)
 
    71           HardwareAdded(hardware);
 
    74     private void Remove(IGroup group) {
 
    75       if (!groups.Contains(group))
 
    80       if (HardwareRemoved != null)
 
    81         foreach (IHardware hardware in group.Hardware)
 
    82           HardwareRemoved(hardware);
 
    85     [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
 
    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));
 
   101         Add(new HDD.HDDGroup(settings));
 
   106     public bool HDDEnabled {
 
   107       get { return hddEnabled; }
 
   109       [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
 
   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)
 
   118           foreach (IGroup group in list)
 
   125     public IHardware[] Hardware {
 
   127         List<IHardware> list = new List<IHardware>();
 
   128         foreach (IGroup group in groups)
 
   129           foreach (IHardware hardware in group.Hardware)
 
   131         return list.ToArray();
 
   135     private static void NewSection(TextWriter writer) {
 
   136       for (int i = 0; i < 8; i++)
 
   137         writer.Write("----------");
 
   142     private static int CompareSensor(ISensor a, ISensor b) {
 
   143       int c = a.SensorType.CompareTo(b.SensorType);
 
   145         return a.Index.CompareTo(b.Index);
 
   150     private static void ReportHardwareSensorTree(
 
   151       IHardware hardware, TextWriter w, string space) 
 
   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,-14} : {2,8:G6} {3,8:G6} {4,8:G6} ({5})", 
 
   160           space, sensor.Name, sensor.Value, sensor.Min, sensor.Max, 
 
   163       foreach (IHardware subHardware in hardware.SubHardware)
 
   164         ReportHardwareSensorTree(subHardware, w, "|  ");
 
   167     private static void ReportHardwareParameterTree(
 
   168       IHardware hardware, TextWriter w, string space) {
 
   169       w.WriteLine("{0}|", space);
 
   170       w.WriteLine("{0}+- {1} ({2})",
 
   171         space, hardware.Name, hardware.Identifier);
 
   172       ISensor[] sensors = hardware.Sensors;
 
   173       Array.Sort(sensors, CompareSensor);
 
   174       foreach (ISensor sensor in sensors) {
 
   175         string innerSpace = space + "|  ";
 
   176         if (sensor.Parameters.Length > 0) {
 
   177           w.WriteLine("{0}|", innerSpace);
 
   178           w.WriteLine("{0}+- {1} ({2})",
 
   179             innerSpace, sensor.Name, sensor.Identifier);
 
   180           foreach (IParameter parameter in sensor.Parameters) {
 
   181             string innerInnerSpace = innerSpace + "|  ";
 
   182             w.WriteLine("{0}+- {1} : {2}",
 
   183               innerInnerSpace, parameter.Name,
 
   184               string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
 
   185                 parameter.DefaultValue, parameter.Value));
 
   189       foreach (IHardware subHardware in hardware.SubHardware)
 
   190         ReportHardwareParameterTree(subHardware, w, "|  ");
 
   193     private static void ReportHardware(IHardware hardware, TextWriter w) {
 
   194       string hardwareReport = hardware.GetReport();
 
   195       if (!string.IsNullOrEmpty(hardwareReport)) {
 
   197         w.Write(hardwareReport);
 
   199       foreach (IHardware subHardware in hardware.SubHardware)
 
   200         ReportHardware(subHardware, w);
 
   203     public string GetReport() {
 
   205       using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
 
   208         w.WriteLine("Open Hardware Monitor Report");
 
   211         Version version = typeof(Computer).Assembly.GetName().Version;
 
   214         w.Write("Version: "); w.WriteLine(version.ToString());
 
   218         w.Write("Common Language Runtime: ");
 
   219         w.WriteLine(Environment.Version.ToString());
 
   220         w.Write("Operating System: ");
 
   221         w.WriteLine(Environment.OSVersion.ToString());
 
   222         w.Write("Process Type: ");
 
   223         w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
 
   226         string r = Ring0.GetReport();
 
   234         w.WriteLine("Sensors");
 
   236         foreach (IGroup group in groups) {
 
   237           foreach (IHardware hardware in group.Hardware)
 
   238             ReportHardwareSensorTree(hardware, w, "");
 
   243         w.WriteLine("Parameters");
 
   245         foreach (IGroup group in groups) {
 
   246           foreach (IHardware hardware in group.Hardware)
 
   247             ReportHardwareParameterTree(hardware, w, "");
 
   251         foreach (IGroup group in groups) {
 
   252           string report = group.GetReport();
 
   253           if (!string.IsNullOrEmpty(report)) {
 
   258           IHardware[] hardwareArray = group.Hardware;
 
   259           foreach (IHardware hardware in hardwareArray)
 
   260             ReportHardware(hardware, w);
 
   267     public void Close() {      
 
   271       while (groups.Count > 0) {
 
   272         IGroup group = groups[groups.Count - 1];
 
   283     public event HardwareEventHandler HardwareAdded;
 
   284     public event HardwareEventHandler HardwareRemoved;
 
   286     public void Accept(IVisitor visitor) {
 
   288         throw new ArgumentNullException("visitor");
 
   289       visitor.VisitComputer(this);
 
   292     public void Traverse(IVisitor visitor) {
 
   293       foreach (IGroup group in groups)
 
   294         foreach (IHardware hardware in group.Hardware) 
 
   295           hardware.Accept(visitor);
 
   298     private class Settings : ISettings {
 
   300       public bool Contains(string name) {
 
   304       public void SetValue(string name, string value) { }
 
   306       public string GetValue(string name, string value) {
 
   310       public void Remove(string name) { }