diff -r 7b859a06eecb -r 090259cfd699 GUI/SoundGraphDisplay.cs --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/GUI/SoundGraphDisplay.cs Sun Feb 03 18:01:50 2013 +0100 @@ -0,0 +1,221 @@ +/* + + This Source Code Form is subject to the terms of the Mozilla Public + License, v. 2.0. If a copy of the MPL was not distributed with this + file, You can obtain one at http://mozilla.org/MPL/2.0/. + + Copyright (C) 2009-2012 Michael Möller + +*/ + +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Text; +using System.Windows.Forms; +using OpenHardwareMonitor.Hardware; +using OpenHardwareMonitor.Utilities; +using System.Runtime.InteropServices; + + + +static class NativeMethods +{ + [System.Flags] + public enum LoadLibraryFlags : uint + { + DONT_RESOLVE_DLL_REFERENCES = 0x00000001, + LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010, + LOAD_LIBRARY_AS_DATAFILE = 0x00000002, + LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040, + LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020, + LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008 + } + + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr LoadLibrary(string dllToLoad); + + [DllImport("kernel32.dll")] + public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags); + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName); + + [DllImport("kernel32.dll", SetLastError = true)] + public static extern bool FreeLibrary(IntPtr hModule); +} + + +namespace OpenHardwareMonitor.GUI +{ + public class SoundGraphDisplay : IDisposable + { + private IComputer computer; + private PersistentSettings settings; + private UnitManager unitManager; + private List list = new List(); + //private bool mainIconEnabled = false; + //private NotifyIconAdv mainIcon; + IntPtr iSoundGraphDll; + + public SoundGraphDisplay(IComputer computer, PersistentSettings settings, + UnitManager unitManager) + { + this.computer = computer; + this.settings = settings; + this.unitManager = unitManager; + computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); + computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); + + //Try loading SoundGraph iMON Disaply DLL + iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH); + int err=Marshal.GetLastWin32Error(); //If you 193 it means you need build for x86 + if (err == 193) + { + Console.Write(@"iMON: Cannot load x86 DLL from x64 process."); + } + else if (err != 0) + { + Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err); + } + else + { + Console.Write(@"iMON: DLL loaded."); + } + + + } + + private void HardwareRemoved(IHardware hardware) + { + hardware.SensorAdded -= new SensorEventHandler(SensorAdded); + hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved); + foreach (ISensor sensor in hardware.Sensors) + SensorRemoved(sensor); + foreach (IHardware subHardware in hardware.SubHardware) + HardwareRemoved(subHardware); + } + + private void HardwareAdded(IHardware hardware) + { + foreach (ISensor sensor in hardware.Sensors) + SensorAdded(sensor); + hardware.SensorAdded += new SensorEventHandler(SensorAdded); + hardware.SensorRemoved += new SensorEventHandler(SensorRemoved); + foreach (IHardware subHardware in hardware.SubHardware) + HardwareAdded(subHardware); + } + + private void SensorAdded(ISensor sensor) + { + if (settings.GetValue(new Identifier(sensor.Identifier, + "tray").ToString(), false)) + Add(sensor, false); + } + + private void SensorRemoved(ISensor sensor) + { + if (Contains(sensor)) + Remove(sensor, false); + } + + public void Dispose() + { + foreach (SensorFrontView icon in list) + icon.Dispose(); + + //Unload our DLL + if (iSoundGraphDll != IntPtr.Zero) + { + bool result = NativeMethods.FreeLibrary(iSoundGraphDll); + } + + } + + public void Redraw() + { + foreach (SensorFrontView icon in list) + icon.Update(); + } + + public bool Contains(ISensor sensor) + { + foreach (SensorFrontView icon in list) + if (icon.Sensor == sensor) + return true; + return false; + } + + public void Add(ISensor sensor, bool balloonTip) + { + if (Contains(sensor)) + { + return; + } + else + { + //SL: + //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager)); + //UpdateMainIconVisibilty(); + //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true); + } + } + + public void Remove(ISensor sensor) + { + Remove(sensor, true); + } + + private void Remove(ISensor sensor, bool deleteConfig) + { + if (deleteConfig) + { + settings.Remove( + new Identifier(sensor.Identifier, "tray").ToString()); + settings.Remove( + new Identifier(sensor.Identifier, "traycolor").ToString()); + } + SensorFrontView instance = null; + foreach (SensorFrontView icon in list) + if (icon.Sensor == sensor) + instance = icon; + if (instance != null) + { + list.Remove(instance); + UpdateMainIconVisibilty(); + instance.Dispose(); + } + } + + + + private void UpdateMainIconVisibilty() + { + /* + if (mainIconEnabled) + { + mainIcon.Visible = list.Count == 0; + } + else + { + mainIcon.Visible = false; + } + */ + } + + /* + public bool IsMainIconEnabled + { + get { return mainIconEnabled; } + set + { + if (mainIconEnabled != value) + { + mainIconEnabled = value; + UpdateMainIconVisibilty(); + } + } + }*/ + } +}