Fixed some problems when compiling in Mono and running on Linux.
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;
44 namespace OpenHardwareMonitor.Hardware {
46 public class Computer : IComputer {
48 private readonly List<IGroup> groups = new List<IGroup>();
49 private readonly ISettings settings;
52 private bool hddEnabled;
55 this.settings = new Settings();
58 public Computer(ISettings settings) {
59 this.settings = settings ?? new Settings();
62 private void Add(IGroup group) {
63 if (groups.Contains(group))
68 if (HardwareAdded != null)
69 foreach (IHardware hardware in group.Hardware)
70 HardwareAdded(hardware);
73 private void Remove(IGroup group) {
74 if (!groups.Contains(group))
79 if (HardwareRemoved != null)
80 foreach (IHardware hardware in group.Hardware)
81 HardwareRemoved(hardware);
84 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
91 Add(new Mainboard.MainboardGroup(settings));
92 Add(new CPU.CPUGroup(settings));
93 Add(new ATI.ATIGroup(settings));
94 Add(new Nvidia.NvidiaGroup(settings));
95 Add(new TBalancer.TBalancerGroup(settings));
96 Add(new Heatmaster.HeatmasterGroup(settings));
99 Add(new HDD.HDDGroup(settings));
104 public bool HDDEnabled {
105 get { return hddEnabled; }
107 [SecurityPermission(SecurityAction.LinkDemand, UnmanagedCode = true)]
109 if (open && value && !hddEnabled) {
110 Add(new HDD.HDDGroup(settings));
111 } else if (open && !value && hddEnabled) {
112 List<IGroup> list = new List<IGroup>();
113 foreach (IGroup group in groups)
114 if (group is HDD.HDDGroup)
116 foreach (IGroup group in list)
123 public IHardware[] Hardware {
125 List<IHardware> list = new List<IHardware>();
126 foreach (IGroup group in groups)
127 foreach (IHardware hardware in group.Hardware)
129 return list.ToArray();
133 private static void NewSection(TextWriter writer) {
134 for (int i = 0; i < 8; i++)
135 writer.Write("----------");
140 private static int CompareSensor(ISensor a, ISensor b) {
141 int c = a.SensorType.CompareTo(b.SensorType);
143 return a.Index.CompareTo(b.Index);
148 private static void ReportHardwareSensorTree(
149 IHardware hardware, TextWriter w, string space)
151 w.WriteLine("{0}|", space);
152 w.WriteLine("{0}+-+ {1} ({2})",
153 space, hardware.Name, hardware.Identifier);
154 ISensor[] sensors = hardware.Sensors;
155 Array.Sort(sensors, CompareSensor);
156 foreach (ISensor sensor in sensors) {
157 w.WriteLine("{0}| +- {1}[{2}] : {3} : {4}",
158 space, sensor.SensorType, sensor.Index,
159 string.Format(CultureInfo.InvariantCulture, "{0} : {1} : {2}",
160 sensor.Value, sensor.Min, sensor.Max), sensor.Name);
162 foreach (IHardware subHardware in hardware.SubHardware)
163 ReportHardwareSensorTree(subHardware, w, "| ");
166 private static void ReportHardwareParameterTree(
167 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 if (sensor.Parameters.Length > 0) {
176 w.WriteLine("{0}| +- {1}[{2}] : {3}",
177 space, sensor.SensorType, sensor.Index, sensor.Name);
178 foreach (IParameter parameter in sensor.Parameters) {
179 w.WriteLine("{0}| +- {1} : {2}",
180 space, parameter.Name,
181 string.Format(CultureInfo.InvariantCulture, "{0} : {1}",
182 parameter.DefaultValue, parameter.Value));
186 foreach (IHardware subHardware in hardware.SubHardware)
187 ReportHardwareParameterTree(subHardware, w, "| ");
190 private static void ReportHardware(IHardware hardware, TextWriter w) {
191 string hardwareReport = hardware.GetReport();
192 if (!string.IsNullOrEmpty(hardwareReport)) {
194 w.Write(hardwareReport);
196 foreach (IHardware subHardware in hardware.SubHardware)
197 ReportHardware(subHardware, w);
200 public string GetReport() {
202 using (StringWriter w = new StringWriter(CultureInfo.InvariantCulture)) {
205 w.WriteLine("Open Hardware Monitor Report");
208 Version version = typeof(Computer).Assembly.GetName().Version;
211 w.Write("Version: "); w.WriteLine(version.ToString());
215 w.Write("Common Language Runtime: ");
216 w.WriteLine(Environment.Version.ToString());
217 w.Write("Operating System: ");
218 w.WriteLine(Environment.OSVersion.ToString());
219 w.Write("Process Type: ");
220 w.WriteLine(IntPtr.Size == 4 ? "32-Bit" : "64-Bit");
224 w.WriteLine("Sensors");
226 foreach (IGroup group in groups) {
227 foreach (IHardware hardware in group.Hardware)
228 ReportHardwareSensorTree(hardware, w, "");
233 w.WriteLine("Parameters");
235 foreach (IGroup group in groups) {
236 foreach (IHardware hardware in group.Hardware)
237 ReportHardwareParameterTree(hardware, w, "");
241 foreach (IGroup group in groups) {
242 string report = group.GetReport();
243 if (!string.IsNullOrEmpty(report)) {
248 IHardware[] hardwareArray = group.Hardware;
249 foreach (IHardware hardware in hardwareArray)
250 ReportHardware(hardware, w);
257 public void Close() {
261 foreach (IGroup group in groups)
270 public event HardwareEventHandler HardwareAdded;
271 public event HardwareEventHandler HardwareRemoved;
273 public void Accept(IVisitor visitor) {
275 throw new ArgumentNullException("visitor");
276 visitor.VisitComputer(this);
279 public void Traverse(IVisitor visitor) {
280 foreach (IGroup group in groups)
281 foreach (IHardware hardware in group.Hardware)
282 hardware.Accept(visitor);
285 private class Settings : ISettings {
287 public bool Contains(string name) {
291 public void SetValue(string name, string value) { }
293 public string GetValue(string name, string value) {
297 public void Remove(string name) { }