GUI/SoundGraphDisplay.cs
author StephaneLenclud
Sun, 03 Feb 2013 18:01:50 +0100
branchMiniDisplay
changeset 433 090259cfd699
child 434 480652d72031
permissions -rw-r--r--
Adding SoundGraphDisplay and SensorFrontView classes.
They were respectively based on SystemTray and SensorNotifyIcon.
SoundGraphDisplay is now able to load iMONDisplay.dll providing it lives on your PATH.
Adding option to sensor context menu for adding it into FrontView.
StephaneLenclud@433
     1
/*
StephaneLenclud@433
     2
 
StephaneLenclud@433
     3
  This Source Code Form is subject to the terms of the Mozilla Public
StephaneLenclud@433
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
StephaneLenclud@433
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
StephaneLenclud@433
     6
 
StephaneLenclud@433
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
StephaneLenclud@433
     8
	
StephaneLenclud@433
     9
*/
StephaneLenclud@433
    10
StephaneLenclud@433
    11
using System;
StephaneLenclud@433
    12
using System.Collections.Generic;
StephaneLenclud@433
    13
using System.Drawing;
StephaneLenclud@433
    14
using System.Text;
StephaneLenclud@433
    15
using System.Windows.Forms;
StephaneLenclud@433
    16
using OpenHardwareMonitor.Hardware;
StephaneLenclud@433
    17
using OpenHardwareMonitor.Utilities;
StephaneLenclud@433
    18
using System.Runtime.InteropServices;
StephaneLenclud@433
    19
StephaneLenclud@433
    20
StephaneLenclud@433
    21
StephaneLenclud@433
    22
static class NativeMethods
StephaneLenclud@433
    23
{
StephaneLenclud@433
    24
    [System.Flags]
StephaneLenclud@433
    25
    public enum LoadLibraryFlags : uint
StephaneLenclud@433
    26
    {
StephaneLenclud@433
    27
        DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
StephaneLenclud@433
    28
        LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
StephaneLenclud@433
    29
        LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
StephaneLenclud@433
    30
        LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
StephaneLenclud@433
    31
        LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
StephaneLenclud@433
    32
        LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
StephaneLenclud@433
    33
    }
StephaneLenclud@433
    34
StephaneLenclud@433
    35
StephaneLenclud@433
    36
    [DllImport("kernel32.dll", SetLastError = true)]
StephaneLenclud@433
    37
    public static extern IntPtr LoadLibrary(string dllToLoad);
StephaneLenclud@433
    38
StephaneLenclud@433
    39
    [DllImport("kernel32.dll")]
StephaneLenclud@433
    40
    public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
StephaneLenclud@433
    41
StephaneLenclud@433
    42
    [DllImport("kernel32.dll", SetLastError = true)]
StephaneLenclud@433
    43
    public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
StephaneLenclud@433
    44
StephaneLenclud@433
    45
    [DllImport("kernel32.dll", SetLastError = true)]
StephaneLenclud@433
    46
    public static extern bool FreeLibrary(IntPtr hModule);
StephaneLenclud@433
    47
}
StephaneLenclud@433
    48
StephaneLenclud@433
    49
StephaneLenclud@433
    50
namespace OpenHardwareMonitor.GUI
StephaneLenclud@433
    51
{
StephaneLenclud@433
    52
    public class SoundGraphDisplay : IDisposable
StephaneLenclud@433
    53
    {
StephaneLenclud@433
    54
        private IComputer computer;
StephaneLenclud@433
    55
        private PersistentSettings settings;
StephaneLenclud@433
    56
        private UnitManager unitManager;
StephaneLenclud@433
    57
        private List<SensorFrontView> list = new List<SensorFrontView>();
StephaneLenclud@433
    58
        //private bool mainIconEnabled = false;
StephaneLenclud@433
    59
        //private NotifyIconAdv mainIcon;
StephaneLenclud@433
    60
        IntPtr iSoundGraphDll;
StephaneLenclud@433
    61
StephaneLenclud@433
    62
        public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
StephaneLenclud@433
    63
          UnitManager unitManager)
StephaneLenclud@433
    64
        {
StephaneLenclud@433
    65
            this.computer = computer;
StephaneLenclud@433
    66
            this.settings = settings;
StephaneLenclud@433
    67
            this.unitManager = unitManager;
StephaneLenclud@433
    68
            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
StephaneLenclud@433
    69
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
StephaneLenclud@433
    70
StephaneLenclud@433
    71
            //Try loading SoundGraph iMON Disaply DLL
StephaneLenclud@433
    72
            iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
StephaneLenclud@433
    73
            int err=Marshal.GetLastWin32Error(); //If you 193  it means you need build for x86
StephaneLenclud@433
    74
            if (err == 193)
StephaneLenclud@433
    75
            {
StephaneLenclud@433
    76
                Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
StephaneLenclud@433
    77
            }
StephaneLenclud@433
    78
            else if (err != 0)
StephaneLenclud@433
    79
            {
StephaneLenclud@433
    80
                Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
StephaneLenclud@433
    81
            }
StephaneLenclud@433
    82
            else
StephaneLenclud@433
    83
            {
StephaneLenclud@433
    84
                Console.Write(@"iMON: DLL loaded.");
StephaneLenclud@433
    85
            }
StephaneLenclud@433
    86
StephaneLenclud@433
    87
StephaneLenclud@433
    88
        }
StephaneLenclud@433
    89
StephaneLenclud@433
    90
        private void HardwareRemoved(IHardware hardware)
StephaneLenclud@433
    91
        {
StephaneLenclud@433
    92
            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
StephaneLenclud@433
    93
            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
StephaneLenclud@433
    94
            foreach (ISensor sensor in hardware.Sensors)
StephaneLenclud@433
    95
                SensorRemoved(sensor);
StephaneLenclud@433
    96
            foreach (IHardware subHardware in hardware.SubHardware)
StephaneLenclud@433
    97
                HardwareRemoved(subHardware);
StephaneLenclud@433
    98
        }
StephaneLenclud@433
    99
StephaneLenclud@433
   100
        private void HardwareAdded(IHardware hardware)
StephaneLenclud@433
   101
        {
StephaneLenclud@433
   102
            foreach (ISensor sensor in hardware.Sensors)
StephaneLenclud@433
   103
                SensorAdded(sensor);
StephaneLenclud@433
   104
            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
StephaneLenclud@433
   105
            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
StephaneLenclud@433
   106
            foreach (IHardware subHardware in hardware.SubHardware)
StephaneLenclud@433
   107
                HardwareAdded(subHardware);
StephaneLenclud@433
   108
        }
StephaneLenclud@433
   109
StephaneLenclud@433
   110
        private void SensorAdded(ISensor sensor)
StephaneLenclud@433
   111
        {
StephaneLenclud@433
   112
            if (settings.GetValue(new Identifier(sensor.Identifier,
StephaneLenclud@433
   113
              "tray").ToString(), false))
StephaneLenclud@433
   114
                Add(sensor, false);
StephaneLenclud@433
   115
        }
StephaneLenclud@433
   116
StephaneLenclud@433
   117
        private void SensorRemoved(ISensor sensor)
StephaneLenclud@433
   118
        {
StephaneLenclud@433
   119
            if (Contains(sensor))
StephaneLenclud@433
   120
                Remove(sensor, false);
StephaneLenclud@433
   121
        }
StephaneLenclud@433
   122
StephaneLenclud@433
   123
        public void Dispose()
StephaneLenclud@433
   124
        {
StephaneLenclud@433
   125
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   126
                icon.Dispose();
StephaneLenclud@433
   127
StephaneLenclud@433
   128
            //Unload our DLL
StephaneLenclud@433
   129
            if (iSoundGraphDll != IntPtr.Zero)
StephaneLenclud@433
   130
            {
StephaneLenclud@433
   131
                bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
StephaneLenclud@433
   132
            }
StephaneLenclud@433
   133
StephaneLenclud@433
   134
        }
StephaneLenclud@433
   135
StephaneLenclud@433
   136
        public void Redraw()
StephaneLenclud@433
   137
        {
StephaneLenclud@433
   138
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   139
                icon.Update();
StephaneLenclud@433
   140
        }
StephaneLenclud@433
   141
StephaneLenclud@433
   142
        public bool Contains(ISensor sensor)
StephaneLenclud@433
   143
        {
StephaneLenclud@433
   144
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   145
                if (icon.Sensor == sensor)
StephaneLenclud@433
   146
                    return true;
StephaneLenclud@433
   147
            return false;
StephaneLenclud@433
   148
        }
StephaneLenclud@433
   149
StephaneLenclud@433
   150
        public void Add(ISensor sensor, bool balloonTip)
StephaneLenclud@433
   151
        {
StephaneLenclud@433
   152
            if (Contains(sensor))
StephaneLenclud@433
   153
            {
StephaneLenclud@433
   154
                return;
StephaneLenclud@433
   155
            }
StephaneLenclud@433
   156
            else
StephaneLenclud@433
   157
            {
StephaneLenclud@433
   158
                //SL:
StephaneLenclud@433
   159
                //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
StephaneLenclud@433
   160
                //UpdateMainIconVisibilty();
StephaneLenclud@433
   161
                //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
StephaneLenclud@433
   162
            }
StephaneLenclud@433
   163
        }
StephaneLenclud@433
   164
StephaneLenclud@433
   165
        public void Remove(ISensor sensor)
StephaneLenclud@433
   166
        {
StephaneLenclud@433
   167
            Remove(sensor, true);
StephaneLenclud@433
   168
        }
StephaneLenclud@433
   169
StephaneLenclud@433
   170
        private void Remove(ISensor sensor, bool deleteConfig)
StephaneLenclud@433
   171
        {
StephaneLenclud@433
   172
            if (deleteConfig)
StephaneLenclud@433
   173
            {
StephaneLenclud@433
   174
                settings.Remove(
StephaneLenclud@433
   175
                  new Identifier(sensor.Identifier, "tray").ToString());
StephaneLenclud@433
   176
                settings.Remove(
StephaneLenclud@433
   177
                  new Identifier(sensor.Identifier, "traycolor").ToString());
StephaneLenclud@433
   178
            }
StephaneLenclud@433
   179
            SensorFrontView instance = null;
StephaneLenclud@433
   180
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   181
                if (icon.Sensor == sensor)
StephaneLenclud@433
   182
                    instance = icon;
StephaneLenclud@433
   183
            if (instance != null)
StephaneLenclud@433
   184
            {
StephaneLenclud@433
   185
                list.Remove(instance);
StephaneLenclud@433
   186
                UpdateMainIconVisibilty();
StephaneLenclud@433
   187
                instance.Dispose();
StephaneLenclud@433
   188
            }
StephaneLenclud@433
   189
        }
StephaneLenclud@433
   190
StephaneLenclud@433
   191
StephaneLenclud@433
   192
StephaneLenclud@433
   193
        private void UpdateMainIconVisibilty()
StephaneLenclud@433
   194
        {
StephaneLenclud@433
   195
            /*
StephaneLenclud@433
   196
            if (mainIconEnabled)
StephaneLenclud@433
   197
            {
StephaneLenclud@433
   198
                mainIcon.Visible = list.Count == 0;
StephaneLenclud@433
   199
            }
StephaneLenclud@433
   200
            else
StephaneLenclud@433
   201
            {
StephaneLenclud@433
   202
                mainIcon.Visible = false;
StephaneLenclud@433
   203
            }
StephaneLenclud@433
   204
             */
StephaneLenclud@433
   205
        }
StephaneLenclud@433
   206
StephaneLenclud@433
   207
        /*
StephaneLenclud@433
   208
        public bool IsMainIconEnabled
StephaneLenclud@433
   209
        {
StephaneLenclud@433
   210
            get { return mainIconEnabled; }
StephaneLenclud@433
   211
            set
StephaneLenclud@433
   212
            {
StephaneLenclud@433
   213
                if (mainIconEnabled != value)
StephaneLenclud@433
   214
                {
StephaneLenclud@433
   215
                    mainIconEnabled = value;
StephaneLenclud@433
   216
                    UpdateMainIconVisibilty();
StephaneLenclud@433
   217
                }
StephaneLenclud@433
   218
            }
StephaneLenclud@433
   219
        }*/
StephaneLenclud@433
   220
    }
StephaneLenclud@433
   221
}