GUI/SoundGraphDisplay.cs
branchMiniDisplay
changeset 433 090259cfd699
child 434 480652d72031
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SoundGraphDisplay.cs	Sun Feb 03 18:01:50 2013 +0100
     1.3 @@ -0,0 +1,221 @@
     1.4 +/*
     1.5 + 
     1.6 +  This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +  License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +  file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 + 
    1.10 +  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
    1.11 +	
    1.12 +*/
    1.13 +
    1.14 +using System;
    1.15 +using System.Collections.Generic;
    1.16 +using System.Drawing;
    1.17 +using System.Text;
    1.18 +using System.Windows.Forms;
    1.19 +using OpenHardwareMonitor.Hardware;
    1.20 +using OpenHardwareMonitor.Utilities;
    1.21 +using System.Runtime.InteropServices;
    1.22 +
    1.23 +
    1.24 +
    1.25 +static class NativeMethods
    1.26 +{
    1.27 +    [System.Flags]
    1.28 +    public enum LoadLibraryFlags : uint
    1.29 +    {
    1.30 +        DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
    1.31 +        LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
    1.32 +        LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
    1.33 +        LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
    1.34 +        LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
    1.35 +        LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
    1.36 +    }
    1.37 +
    1.38 +
    1.39 +    [DllImport("kernel32.dll", SetLastError = true)]
    1.40 +    public static extern IntPtr LoadLibrary(string dllToLoad);
    1.41 +
    1.42 +    [DllImport("kernel32.dll")]
    1.43 +    public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
    1.44 +
    1.45 +    [DllImport("kernel32.dll", SetLastError = true)]
    1.46 +    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    1.47 +
    1.48 +    [DllImport("kernel32.dll", SetLastError = true)]
    1.49 +    public static extern bool FreeLibrary(IntPtr hModule);
    1.50 +}
    1.51 +
    1.52 +
    1.53 +namespace OpenHardwareMonitor.GUI
    1.54 +{
    1.55 +    public class SoundGraphDisplay : IDisposable
    1.56 +    {
    1.57 +        private IComputer computer;
    1.58 +        private PersistentSettings settings;
    1.59 +        private UnitManager unitManager;
    1.60 +        private List<SensorFrontView> list = new List<SensorFrontView>();
    1.61 +        //private bool mainIconEnabled = false;
    1.62 +        //private NotifyIconAdv mainIcon;
    1.63 +        IntPtr iSoundGraphDll;
    1.64 +
    1.65 +        public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
    1.66 +          UnitManager unitManager)
    1.67 +        {
    1.68 +            this.computer = computer;
    1.69 +            this.settings = settings;
    1.70 +            this.unitManager = unitManager;
    1.71 +            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    1.72 +            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    1.73 +
    1.74 +            //Try loading SoundGraph iMON Disaply DLL
    1.75 +            iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
    1.76 +            int err=Marshal.GetLastWin32Error(); //If you 193  it means you need build for x86
    1.77 +            if (err == 193)
    1.78 +            {
    1.79 +                Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
    1.80 +            }
    1.81 +            else if (err != 0)
    1.82 +            {
    1.83 +                Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
    1.84 +            }
    1.85 +            else
    1.86 +            {
    1.87 +                Console.Write(@"iMON: DLL loaded.");
    1.88 +            }
    1.89 +
    1.90 +
    1.91 +        }
    1.92 +
    1.93 +        private void HardwareRemoved(IHardware hardware)
    1.94 +        {
    1.95 +            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    1.96 +            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    1.97 +            foreach (ISensor sensor in hardware.Sensors)
    1.98 +                SensorRemoved(sensor);
    1.99 +            foreach (IHardware subHardware in hardware.SubHardware)
   1.100 +                HardwareRemoved(subHardware);
   1.101 +        }
   1.102 +
   1.103 +        private void HardwareAdded(IHardware hardware)
   1.104 +        {
   1.105 +            foreach (ISensor sensor in hardware.Sensors)
   1.106 +                SensorAdded(sensor);
   1.107 +            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
   1.108 +            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
   1.109 +            foreach (IHardware subHardware in hardware.SubHardware)
   1.110 +                HardwareAdded(subHardware);
   1.111 +        }
   1.112 +
   1.113 +        private void SensorAdded(ISensor sensor)
   1.114 +        {
   1.115 +            if (settings.GetValue(new Identifier(sensor.Identifier,
   1.116 +              "tray").ToString(), false))
   1.117 +                Add(sensor, false);
   1.118 +        }
   1.119 +
   1.120 +        private void SensorRemoved(ISensor sensor)
   1.121 +        {
   1.122 +            if (Contains(sensor))
   1.123 +                Remove(sensor, false);
   1.124 +        }
   1.125 +
   1.126 +        public void Dispose()
   1.127 +        {
   1.128 +            foreach (SensorFrontView icon in list)
   1.129 +                icon.Dispose();
   1.130 +
   1.131 +            //Unload our DLL
   1.132 +            if (iSoundGraphDll != IntPtr.Zero)
   1.133 +            {
   1.134 +                bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
   1.135 +            }
   1.136 +
   1.137 +        }
   1.138 +
   1.139 +        public void Redraw()
   1.140 +        {
   1.141 +            foreach (SensorFrontView icon in list)
   1.142 +                icon.Update();
   1.143 +        }
   1.144 +
   1.145 +        public bool Contains(ISensor sensor)
   1.146 +        {
   1.147 +            foreach (SensorFrontView icon in list)
   1.148 +                if (icon.Sensor == sensor)
   1.149 +                    return true;
   1.150 +            return false;
   1.151 +        }
   1.152 +
   1.153 +        public void Add(ISensor sensor, bool balloonTip)
   1.154 +        {
   1.155 +            if (Contains(sensor))
   1.156 +            {
   1.157 +                return;
   1.158 +            }
   1.159 +            else
   1.160 +            {
   1.161 +                //SL:
   1.162 +                //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   1.163 +                //UpdateMainIconVisibilty();
   1.164 +                //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
   1.165 +            }
   1.166 +        }
   1.167 +
   1.168 +        public void Remove(ISensor sensor)
   1.169 +        {
   1.170 +            Remove(sensor, true);
   1.171 +        }
   1.172 +
   1.173 +        private void Remove(ISensor sensor, bool deleteConfig)
   1.174 +        {
   1.175 +            if (deleteConfig)
   1.176 +            {
   1.177 +                settings.Remove(
   1.178 +                  new Identifier(sensor.Identifier, "tray").ToString());
   1.179 +                settings.Remove(
   1.180 +                  new Identifier(sensor.Identifier, "traycolor").ToString());
   1.181 +            }
   1.182 +            SensorFrontView instance = null;
   1.183 +            foreach (SensorFrontView icon in list)
   1.184 +                if (icon.Sensor == sensor)
   1.185 +                    instance = icon;
   1.186 +            if (instance != null)
   1.187 +            {
   1.188 +                list.Remove(instance);
   1.189 +                UpdateMainIconVisibilty();
   1.190 +                instance.Dispose();
   1.191 +            }
   1.192 +        }
   1.193 +
   1.194 +
   1.195 +
   1.196 +        private void UpdateMainIconVisibilty()
   1.197 +        {
   1.198 +            /*
   1.199 +            if (mainIconEnabled)
   1.200 +            {
   1.201 +                mainIcon.Visible = list.Count == 0;
   1.202 +            }
   1.203 +            else
   1.204 +            {
   1.205 +                mainIcon.Visible = false;
   1.206 +            }
   1.207 +             */
   1.208 +        }
   1.209 +
   1.210 +        /*
   1.211 +        public bool IsMainIconEnabled
   1.212 +        {
   1.213 +            get { return mainIconEnabled; }
   1.214 +            set
   1.215 +            {
   1.216 +                if (mainIconEnabled != value)
   1.217 +                {
   1.218 +                    mainIconEnabled = value;
   1.219 +                    UpdateMainIconVisibilty();
   1.220 +                }
   1.221 +            }
   1.222 +        }*/
   1.223 +    }
   1.224 +}