# HG changeset patch # User paulwerelds # Date 1287235746 0 # Node ID 39f73ac8c2f4625e9bd5727050064181451c3da1 # Parent ba64bb91ebe4e38f29758f2e17a38b581c13d275 Added a WMI provider, documentation to follow. diff -r ba64bb91ebe4 -r 39f73ac8c2f4 GUI/MainForm.Designer.cs --- a/GUI/MainForm.Designer.cs Thu Oct 14 17:30:51 2010 +0000 +++ b/GUI/MainForm.Designer.cs Sat Oct 16 13:29:06 2010 +0000 @@ -100,6 +100,8 @@ this.fahrenheitMenuItem = new System.Windows.Forms.MenuItem(); this.MenuItem4 = new System.Windows.Forms.MenuItem(); this.hddMenuItem = new System.Windows.Forms.MenuItem(); + this.menuItem5 = new System.Windows.Forms.MenuItem(); + this.wmiMenuItem = new System.Windows.Forms.MenuItem(); this.helpMenuItem = new System.Windows.Forms.MenuItem(); this.aboutMenuItem = new System.Windows.Forms.MenuItem(); this.sensorContextMenu = new System.Windows.Forms.ContextMenu(); @@ -308,7 +310,9 @@ this.separatorMenuItem, this.temperatureUnitsMenuItem, this.MenuItem4, - this.hddMenuItem}); + this.hddMenuItem, + this.menuItem5, + this.wmiMenuItem}); this.optionsMenuItem.Text = "Options"; // // startMinMenuItem @@ -366,6 +370,16 @@ this.hddMenuItem.Index = 7; this.hddMenuItem.Text = "Read HDD sensors"; // + // menuItem5 + // + this.menuItem5.Index = 8; + this.menuItem5.Text = "-"; + // + // wmiMenuItem + // + this.wmiMenuItem.Index = 9; + this.wmiMenuItem.Text = "Enable WMI Provider"; + // // helpMenuItem // this.helpMenuItem.Index = 3; @@ -522,6 +536,8 @@ private System.Windows.Forms.MenuItem MenuItem3; private System.Windows.Forms.MenuItem gadgetMenuItem; private System.Windows.Forms.MenuItem minCloseMenuItem; + private System.Windows.Forms.MenuItem menuItem5; + private System.Windows.Forms.MenuItem wmiMenuItem; } } diff -r ba64bb91ebe4 -r 39f73ac8c2f4 GUI/MainForm.cs --- a/GUI/MainForm.cs Thu Oct 14 17:30:51 2010 +0000 +++ b/GUI/MainForm.cs Sat Oct 16 13:29:06 2010 +0000 @@ -19,7 +19,7 @@ Portions created by the Initial Developer are Copyright (C) 2009-2010 the Initial Developer. All Rights Reserved. - Contributor(s): + Contributor(s): Paul Werelds Alternatively, the contents of this file may be used under the terms of either the GNU General Public License Version 2 or later (the "GPL"), or @@ -38,15 +38,13 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Configuration; using System.Drawing; using System.IO; -using System.Text; using System.Windows.Forms; using Aga.Controls.Tree; using Aga.Controls.Tree.NodeControls; using OpenHardwareMonitor.Hardware; -using OpenHardwareMonitor.Utilities; +using OpenHardwareMonitor.WMIProvider; namespace OpenHardwareMonitor.GUI { public partial class MainForm : Form { @@ -75,6 +73,9 @@ private UserOption autoStart; private UserOption readHddSensors; private UserOption showGadget; + private UserOption enableWmiProvider; + + private WmiProvider wmiProvider; public MainForm() { InitializeComponent(); @@ -133,6 +134,10 @@ computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); + + if (settings.GetValue("enableWmiProvider", false)) + wmiProvider = new WmiProvider(computer); + computer.Open(); timer.Enabled = true; @@ -152,7 +157,8 @@ plotColorPalette[11] = Color.Olive; plotColorPalette[12] = Color.Firebrick; - showHiddenSensors = new UserOption("hiddenMenuItem", false, hiddenMenuItem, settings); + showHiddenSensors = new UserOption("hiddenMenuItem", false, + hiddenMenuItem, settings); showHiddenSensors.Changed += delegate(object sender, EventArgs e) { treeModel.ForceVisible = showHiddenSensors.Value; }; @@ -163,7 +169,8 @@ treeView.Invalidate(); }; - showValue = new UserOption("valueMenuItem", true, valueMenuItem, settings); + showValue = new UserOption("valueMenuItem", true, valueMenuItem, + settings); showValue.Changed += delegate(object sender, EventArgs e) { treeView.Columns[1].IsVisible = showValue.Value; }; @@ -178,16 +185,20 @@ treeView.Columns[3].IsVisible = showMax.Value; }; - startMinimized = new UserOption("startMinMenuItem", false, startMinMenuItem, settings); + startMinimized = new UserOption("startMinMenuItem", false, + startMinMenuItem, settings); - minimizeToTray = new UserOption("minTrayMenuItem", true, minTrayMenuItem, settings); + minimizeToTray = new UserOption("minTrayMenuItem", true, + minTrayMenuItem, settings); minimizeToTray.Changed += delegate(object sender, EventArgs e) { systemTray.IsMainIconEnabled = minimizeToTray.Value; }; - minimizeOnClose = new UserOption("minCloseMenuItem", false, minCloseMenuItem, settings); + minimizeOnClose = new UserOption("minCloseMenuItem", false, + minCloseMenuItem, settings); - autoStart = new UserOption(null, startupManager.Startup, startupMenuItem, settings); + autoStart = new UserOption(null, startupManager.Startup, + startupMenuItem, settings); autoStart.Changed += delegate(object sender, EventArgs e) { try { startupManager.Startup = autoStart.Value; @@ -198,18 +209,31 @@ } }; - readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, settings); + readHddSensors = new UserOption("hddMenuItem", true, hddMenuItem, + settings); readHddSensors.Changed += delegate(object sender, EventArgs e) { computer.HDDEnabled = readHddSensors.Value; UpdatePlotSelection(null, null); }; - showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, settings); + showGadget = new UserOption("gadgetMenuItem", false, gadgetMenuItem, + settings); showGadget.Changed += delegate(object sender, EventArgs e) { if (gadget != null) gadget.Visible = showGadget.Value; }; + enableWmiProvider = new UserOption("enableWmiProvider", false, + wmiMenuItem, settings); + enableWmiProvider.Changed += delegate { + if (enableWmiProvider.Value && wmiProvider == null) + wmiProvider = new WmiProvider(computer); + else if (!enableWmiProvider.Value && wmiProvider != null) { + wmiProvider.Dispose(); + wmiProvider = null; + } + }; + celciusMenuItem.Checked = unitManager.TemperatureUnit == TemperatureUnit.Celcius; fahrenheitMenuItem.Checked = !celciusMenuItem.Checked; @@ -229,10 +253,9 @@ IntPtr handle = Handle; // Make sure the settings are saved when the user logs off - Microsoft.Win32.SystemEvents.SessionEnded += - delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) { - SaveConfiguration(); - }; + Microsoft.Win32.SystemEvents.SessionEnded += delegate { + SaveConfiguration(); + }; } private void SubHardwareAdded(IHardware hardware, Node node) { @@ -297,7 +320,8 @@ plotPanel.SetSensors(selected, colors); } - private void nodeTextBoxText_EditorShowing(object sender, CancelEventArgs e) + private void nodeTextBoxText_EditorShowing(object sender, + CancelEventArgs e) { e.Cancel = !(treeView.CurrentNode != null && treeView.CurrentNode.Tag is SensorNode); @@ -319,9 +343,12 @@ computer.Accept(updateVisitor); treeView.Invalidate(); plotPanel.Invalidate(); - systemTray.Redraw(); + systemTray.Redraw(); if (gadget != null) gadget.Redraw(); + + if (wmiProvider != null) + wmiProvider.Update(); } private void SaveConfiguration() { @@ -355,13 +382,13 @@ Height = settings.GetValue("mainForm.Height", 640) }; - Rectangle totalWorkingArea = new Rectangle(int.MaxValue, int.MaxValue, + Rectangle fullWorkingArea = new Rectangle(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue); foreach (Screen screen in Screen.AllScreens) - totalWorkingArea = Rectangle.Union(totalWorkingArea, screen.Bounds); + fullWorkingArea = Rectangle.Union(fullWorkingArea, screen.Bounds); - Rectangle intersection = Rectangle.Intersect(totalWorkingArea, newBounds); + Rectangle intersection = Rectangle.Intersect(fullWorkingArea, newBounds); if (intersection.Width < 20 || intersection.Height < 20 || !settings.Contains("mainForm.Location.X") ) { @@ -394,7 +421,9 @@ if (m == null || m.Button != MouseButtons.Right) return; - NodeControlInfo info = treeView.GetNodeControlInfoAt(new Point(m.X, m.Y)); + NodeControlInfo info = treeView.GetNodeControlInfoAt( + new Point(m.X, m.Y) + ); treeView.SelectedNode = info.Node; if (info.Node != null) { SensorNode node = info.Node.Tag as SensorNode; diff -r ba64bb91ebe4 -r 39f73ac8c2f4 OpenHardwareMonitor.csproj --- a/OpenHardwareMonitor.csproj Thu Oct 14 17:30:51 2010 +0000 +++ b/OpenHardwareMonitor.csproj Sat Oct 16 13:29:06 2010 +0000 @@ -58,7 +58,9 @@ + + @@ -126,6 +128,12 @@ + + + + + Component + diff -r ba64bb91ebe4 -r 39f73ac8c2f4 WMIProvider/Hardware.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WMIProvider/Hardware.cs Sat Oct 16 13:29:06 2010 +0000 @@ -0,0 +1,19 @@ +using System.Management.Instrumentation; +using OpenHardwareMonitor.Hardware; + +namespace OpenHardwareMonitor.WMIProvider { + [InstrumentationClass(InstrumentationType.Instance)] + public class Hardware : IWmiClass { + public string HardwareType { get; private set; } + public string Identifier { get; private set; } + public string Name { get; private set; } + + public Hardware(IHardware hardware) { + Name = hardware.Name; + Identifier = hardware.Identifier.ToString(); + HardwareType = hardware.HardwareType.ToString(); + } + + public void Update() { } + } +} diff -r ba64bb91ebe4 -r 39f73ac8c2f4 WMIProvider/IWmiClass.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WMIProvider/IWmiClass.cs Sat Oct 16 13:29:06 2010 +0000 @@ -0,0 +1,8 @@ +namespace OpenHardwareMonitor.WMIProvider { + interface IWmiClass { + string Name { get; } + string Identifier { get; } + + void Update(); + } +} diff -r ba64bb91ebe4 -r 39f73ac8c2f4 WMIProvider/Sensor.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WMIProvider/Sensor.cs Sat Oct 16 13:29:06 2010 +0000 @@ -0,0 +1,40 @@ +using System.Management.Instrumentation; +using OpenHardwareMonitor.Hardware; + +namespace OpenHardwareMonitor.WMIProvider { + [InstrumentationClass(InstrumentationType.Instance)] + public class Sensor : IWmiClass { + + private ISensor _sensor; + + public string SensorType { get; private set; } + public string Identifier { get; private set; } + public string Parent { get; private set; } + public string Name { get; private set; } + public float Value { get; private set; } + public float Min { get; private set; } + public float Max { get; private set; } + public int Index { get; private set; } + + public Sensor(ISensor sensor) { + Name = sensor.Name; + Index = sensor.Index; + + SensorType = sensor.SensorType.ToString(); + Identifier = sensor.Identifier.ToString(); + Parent = sensor.Hardware.Identifier.ToString(); + + _sensor = sensor; + } + + public void Update() { + Value = (_sensor.Value != null) ? (float)_sensor.Value : 0; + + if (_sensor.Min != null) + Min = (float)_sensor.Min; + + if (_sensor.Max != null) + Max = (float)_sensor.Max; + } + } +} diff -r ba64bb91ebe4 -r 39f73ac8c2f4 WMIProvider/WMIProvider.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/WMIProvider/WMIProvider.cs Sat Oct 16 13:29:06 2010 +0000 @@ -0,0 +1,100 @@ +using System; +using System.Collections.Generic; +using System.Management.Instrumentation; +using OpenHardwareMonitor.Hardware; + +[assembly: Instrumented("root/OpenHardwareMonitor")] + +[System.ComponentModel.RunInstaller(true)] +public class InstanceInstaller : DefaultManagementProjectInstaller { } + +namespace OpenHardwareMonitor.WMIProvider { + public class WmiProvider : IDisposable { + private List _activeInstances; + + public WmiProvider(IComputer computer) { + _activeInstances = new List(); + + foreach (IHardware hardware in computer.Hardware) + ComputerHardwareAdded(hardware); + + computer.HardwareAdded += ComputerHardwareAdded; + computer.HardwareRemoved += ComputerHardwareRemoved; + } + + public void Update() { + foreach (IWmiClass instance in _activeInstances) + instance.Update(); + } + + #region Eventhandlers + + private void ComputerHardwareAdded(IHardware hardware) { + if (!Exists(hardware.Identifier.ToString())) { + foreach (ISensor sensor in hardware.Sensors) + HardwareSensorAdded(sensor); + + hardware.SensorAdded += HardwareSensorAdded; + hardware.SensorRemoved += HardwareSensorRemoved; + + Hardware hw = new Hardware(hardware); + _activeInstances.Add(hw); + + Instrumentation.Publish(hw); + } + + foreach (IHardware subHardware in hardware.SubHardware) + ComputerHardwareAdded(subHardware); + } + + private void HardwareSensorAdded(ISensor data) { + Sensor sensor = new Sensor(data); + _activeInstances.Add(sensor); + + Instrumentation.Publish(sensor); + } + + private void ComputerHardwareRemoved(IHardware hardware) { + hardware.SensorAdded -= HardwareSensorAdded; + hardware.SensorRemoved -= HardwareSensorRemoved; + + foreach (ISensor sensor in hardware.Sensors) + HardwareSensorRemoved(sensor); + + foreach (IHardware subHardware in hardware.SubHardware) + ComputerHardwareRemoved(subHardware); + + RevokeInstance(hardware.Identifier.ToString()); + } + + private void HardwareSensorRemoved(ISensor sensor) { + RevokeInstance(sensor.Identifier.ToString()); + } + + #endregion + + #region Helpers + + private bool Exists(string identifier) { + return _activeInstances.Exists(h => h.Identifier == identifier); + } + + private void RevokeInstance(string identifier) { + int instanceIndex = _activeInstances.FindIndex( + item => item.Identifier == identifier.ToString() + ); + + Instrumentation.Revoke(_activeInstances[instanceIndex]); + + _activeInstances.RemoveAt(instanceIndex); + } + + #endregion + + public void Dispose() { + foreach (IWmiClass instance in _activeInstances) + Instrumentation.Revoke(instance); + _activeInstances = null; + } + } +}