GUI/SoundGraphDisplay.cs
author sl
Sun, 03 Feb 2013 19:06:01 +0100
changeset 392 8af90fa0940f
parent 391 ca4c0e7ae75d
child 393 4e41fb513d52
permissions -rw-r--r--
Now fetching some of our Sound Graph DLL function pointers.
     1 /*
     2  
     3   This Source Code Form is subject to the terms of the Mozilla Public
     4   License, v. 2.0. If a copy of the MPL was not distributed with this
     5   file, You can obtain one at http://mozilla.org/MPL/2.0/.
     6  
     7   Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
     8 	
     9 */
    10 
    11 using System;
    12 using System.Collections.Generic;
    13 using System.Drawing;
    14 using System.Text;
    15 using System.Windows.Forms;
    16 using OpenHardwareMonitor.Hardware;
    17 using OpenHardwareMonitor.Utilities;
    18 using System.Runtime.InteropServices;
    19 
    20 
    21 
    22 static class NativeMethods
    23 {
    24     [System.Flags]
    25     public enum LoadLibraryFlags : uint
    26     {
    27         DONT_RESOLVE_DLL_REFERENCES = 0x00000001,
    28         LOAD_IGNORE_CODE_AUTHZ_LEVEL = 0x00000010,
    29         LOAD_LIBRARY_AS_DATAFILE = 0x00000002,
    30         LOAD_LIBRARY_AS_DATAFILE_EXCLUSIVE = 0x00000040,
    31         LOAD_LIBRARY_AS_IMAGE_RESOURCE = 0x00000020,
    32         LOAD_WITH_ALTERED_SEARCH_PATH = 0x00000008
    33     }
    34 
    35 
    36     [DllImport("kernel32.dll", SetLastError = true)]
    37     public static extern IntPtr LoadLibrary(string dllToLoad);
    38 
    39     [DllImport("kernel32.dll")]
    40     public static extern IntPtr LoadLibraryEx(string lpFileName, IntPtr hReservedNull, LoadLibraryFlags dwFlags);
    41 
    42     [DllImport("kernel32.dll", SetLastError = true)]
    43     public static extern IntPtr GetProcAddress(IntPtr hModule, string procedureName);
    44 
    45     [DllImport("kernel32.dll", SetLastError = true)]
    46     public static extern bool FreeLibrary(IntPtr hModule);
    47 }
    48 
    49 /**
    50 Definitions taken from public Sound Graph APIs.
    51  */
    52 static class SoundGraph
    53 {
    54     /**DSPResult
    55     @brief	These enumeration values represent the returned result for iMON Display API function calls.\n
    56 			    All iMON Display API function calls return one of this result values.\n
    57 			    For meaning of each result, refer the comment of each line below*/
    58     public enum DSPResult : uint
    59     {
    60 	    DSP_SUCCEEDED = 0,				//// Function Call Succeeded Without Error
    61 	    DSP_E_FAIL,						//// Unspecified Failure
    62 	    DSP_E_OUTOFMEMORY,				//// Failed to Allocate Necessary Memory
    63 	    DSP_E_INVALIDARG,				//// One or More Arguments Are Not Valid
    64 	    DSP_E_NOT_INITED,				//// API is Not Initialized
    65 	    DSP_E_POINTER,					//// Pointer is Not Valid
    66 
    67 	    DSP_S_INITED = 0x1000,			//// API is Initialized
    68 	    DSP_S_NOT_INITED,				//// API is Not Initialized
    69 	    DSP_S_IN_PLUGIN_MODE,			//// API Can Control iMON Display (Display Plug-in Mode)
    70 	    DSP_S_NOT_IN_PLUGIN_MODE,		//// API Can't Control iMON Display
    71     };
    72 
    73 
    74     /**DSPNInitResult
    75     @brief	These enumeration values represent the result status for requesting Display Plug-in Mode to iMON.\n
    76 			    iMON Display API notifies one of this result values to the caller application after requesting Display Plug-in Mode to iMON.\n
    77 			    For more information, refer the comment of each line below*/
    78     public enum DSPNInitResult : uint
    79     {
    80 	    DSPN_SUCCEEDED = 0,				//// Display Plug-in Mode is Initialized Successfully
    81 	    DSPN_ERR_IN_USED = 0x0100,		//// Display Plug-in is Already Used by Other Application
    82 	    DSPN_ERR_HW_DISCONNECTED,		//// iMON HW is Not Connected
    83 	    DSPN_ERR_NOT_SUPPORTED_HW,		//// The Connected iMON HW doesn't Support Display Plug-in
    84 	    DSPN_ERR_PLUGIN_DISABLED,		//// Display Plug-in Mode Option is Disabled
    85 	    DSPN_ERR_IMON_NO_REPLY,			//// The Latest iMON is Not Installed or iMON Not Running
    86 	    DSPN_ERR_UNKNOWN = 0x0200,		//// Unknown Failure
    87     };
    88 
    89 
    90     /**DSPType
    91     @brief	These enumeration values represent display type.\n
    92 			    Currently iMON Display API supports VFD and LCD products.*/
    93     public enum DSPType : uint
    94     {
    95 	    DSPN_DSP_NONE	= 0,
    96 	    DSPN_DSP_VFD	= 0x01,			//// VFD products
    97 	    DSPN_DSP_LCD	= 0x02,			//// LCD products
    98     };
    99 
   100 
   101     /**DSPNotifyCode
   102     @brief	These enumeration values represent the notification codes.\n
   103 			    iMON Display API will send or post message to the caller application.\n
   104 			    The caller application should assign the message and the winodw handle to receivce message with IMON_Display_Init fucntion.\n
   105 			    These enumeration values are used with WPARAM parameter of the message.\n 
   106 			    For more information, see the explanation of each notification code below*/
   107     public enum DSPNotifyCode : uint
   108     {
   109 	    /**DSPNM_PLUGIN_SUCCEED
   110 	    @brief	When API succeeds to get the control for the display, API will post caller-specified message with DSPNM_PLUGIN_SUCCEED as WPARAM parameter.\n
   111 				    LPARAM represents DSPType. This value can be 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   112 	    DSPNM_PLUGIN_SUCCEED = 0,
   113 
   114 	    /**DSPNM_PLUGIN_FAILED
   115 	    @brief	When API fails to get the control for the display, API will post caller-specified message with DSPNM_PLUGIN_FAILED as WPARAM parameter.\n
   116 				    LPARAM represents error code with DSPNResult.*/
   117 	    DSPNM_PLUGIN_FAILED,
   118 
   119 	    /**DSPNM_IMON_RESTARTED
   120 	    @brief	When iMON starts, API will post caller-specified message with DSPNM_IMON_RESTARTED as WPARAM parameter.\n
   121 				    LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   122 	    DSPNM_IMON_RESTARTED,
   123 
   124 	    /**DSPNM_IMON_CLOSED
   125 	    @brief	When iMON closed, API will post caller-specified message with DSPNM_IMON_CLOSED as WPARAM parameter.\n
   126 				    LPARAM is not used.*/
   127 	    DSPNM_IMON_CLOSED,
   128 
   129 	    /**DSPNM_HW_CONNECTED
   130 	    @brief	When iMON HW newly connected, API will post caller-specified message with DSPNM_HW_CONNECTED as WPARAM parameter.\n
   131 				    LPARAM represents DSPType. This value can be 0 (No Display), 0x01 (VFD), 0x02 (LCD) or 0x03 (VFD+LCD).*/
   132 	    DSPNM_HW_CONNECTED,
   133 
   134 	    /**DSPNM_HW_DISCONNECTED
   135 	    @brief	When iMON HW disconnected, API will post caller-specified message with DSPNM_HW_DISCONNECTED as WPARAM parameter.\n
   136 				    LPARAM is DSPNResult value, DSPN_ERR_HW_DISCONNECTED.*/
   137 	    DSPNM_HW_DISCONNECTED,
   138 
   139 
   140 	    /**DSPNM_LCD_TEXT_SCROLL_DONE
   141 	    @brief	When iMON LCD finishes scrolling Text, API will post caller-specified message with DSPNM_LCD_TEXT_SCROLL_DONE as WPARAM parameter.\n
   142 				    The caller application may need to know when text scroll is finished, for sending next text.\n
   143 				    LPARAM is not used.*/
   144 	    DSPNM_LCD_TEXT_SCROLL_DONE = 0x1000,
   145     };
   146 
   147     /// Functions
   148     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   149     public delegate DSPResult IMON_Display_Uninit();
   150 
   151     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   152     public delegate DSPResult IMON_Display_Init(IntPtr hwndNoti, uint uMsgNotification);
   153 
   154     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   155     public delegate DSPResult IMON_Display_IsInited();
   156 
   157     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   158     public delegate DSPResult IMON_Display_IsPluginModeEnabled();
   159 
   160     [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
   161     public delegate DSPResult IMON_Display_SetVfdText(
   162         [MarshalAs(UnmanagedType.LPWStr)] string lpsz1stLine,
   163         [MarshalAs(UnmanagedType.LPWStr)] string lpsz2ndLine);
   164 
   165     //IMONDSPAPI DSPResult IMON_Display_SetVfdText(LPCTSTR lpsz1stLine, LPCTSTR lpsz2ndLine);
   166 
   167 }
   168 
   169 
   170 
   171 namespace OpenHardwareMonitor.GUI
   172 {
   173     public class SoundGraphDisplay : IDisposable
   174     {
   175         private IComputer computer;
   176         private PersistentSettings settings;
   177         private UnitManager unitManager;
   178         private List<SensorFrontView> list = new List<SensorFrontView>();
   179         //private bool mainIconEnabled = false;
   180         //private NotifyIconAdv mainIcon;
   181         IntPtr iSoundGraphDll;
   182         SoundGraph.IMON_Display_Uninit iIMON_Display_Uninit;
   183         SoundGraph.IMON_Display_Init iIMON_Display_Init;
   184         SoundGraph.IMON_Display_IsInited iIMON_Display_IsInited;
   185         SoundGraph.IMON_Display_IsPluginModeEnabled iIMON_Display_IsPluginModeEnabled;
   186         SoundGraph.IMON_Display_SetVfdText iIMON_Display_SetVfdText;
   187 
   188         public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
   189           UnitManager unitManager)
   190         {
   191             this.computer = computer;
   192             this.settings = settings;
   193             this.unitManager = unitManager;
   194             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
   195             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
   196 
   197             //Try loading SoundGraph iMON Disaply DLL
   198             iSoundGraphDll = NativeMethods.LoadLibraryEx(@"iMONDisplay.dll", IntPtr.Zero, NativeMethods.LoadLibraryFlags.LOAD_WITH_ALTERED_SEARCH_PATH);
   199             int err=Marshal.GetLastWin32Error(); //If you 193  it means you need build for x86
   200             if (err == 193)
   201             {
   202                 Console.Write(@"iMON: Cannot load x86 DLL from x64 process.");
   203             }
   204             else if (err != 0)
   205             {
   206                 Console.Write(@"iMON: Error: %i - Failed to load iMONDisplay.dll", err);
   207             }
   208             else
   209             {
   210                 Console.Write(@"iMON: DLL loaded.");
   211                 //Gather our function pointers
   212                 //TODO: Check returned pointers for validity
   213                 IntPtr functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Uninit");
   214                 iIMON_Display_Uninit = (SoundGraph.IMON_Display_Uninit)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Uninit));
   215                 
   216                 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_Init");
   217                 iIMON_Display_Init = (SoundGraph.IMON_Display_Init)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_Init));
   218                 
   219                 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsInited");
   220                 iIMON_Display_IsInited = (SoundGraph.IMON_Display_IsInited)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsInited));
   221                 
   222                 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_IsPluginModeEnabled");
   223                 iIMON_Display_IsPluginModeEnabled = (SoundGraph.IMON_Display_IsPluginModeEnabled)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_IsPluginModeEnabled));
   224 
   225                 functionPointer = NativeMethods.GetProcAddress(iSoundGraphDll, "IMON_Display_SetVfdText");
   226                 iIMON_Display_SetVfdText = (SoundGraph.IMON_Display_SetVfdText)Marshal.GetDelegateForFunctionPointer(functionPointer, typeof(SoundGraph.IMON_Display_SetVfdText));
   227 
   228             }
   229         }
   230 
   231         private void HardwareRemoved(IHardware hardware)
   232         {
   233             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
   234             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
   235             foreach (ISensor sensor in hardware.Sensors)
   236                 SensorRemoved(sensor);
   237             foreach (IHardware subHardware in hardware.SubHardware)
   238                 HardwareRemoved(subHardware);
   239         }
   240 
   241         private void HardwareAdded(IHardware hardware)
   242         {
   243             foreach (ISensor sensor in hardware.Sensors)
   244                 SensorAdded(sensor);
   245             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
   246             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
   247             foreach (IHardware subHardware in hardware.SubHardware)
   248                 HardwareAdded(subHardware);
   249         }
   250 
   251         private void SensorAdded(ISensor sensor)
   252         {
   253             if (settings.GetValue(new Identifier(sensor.Identifier,
   254               "tray").ToString(), false))
   255                 Add(sensor, false);
   256         }
   257 
   258         private void SensorRemoved(ISensor sensor)
   259         {
   260             if (Contains(sensor))
   261                 Remove(sensor, false);
   262         }
   263 
   264         public void Dispose()
   265         {
   266             foreach (SensorFrontView icon in list)
   267                 icon.Dispose();
   268 
   269             //Unload our DLL
   270             if (iSoundGraphDll != IntPtr.Zero)
   271             {
   272                 bool result = NativeMethods.FreeLibrary(iSoundGraphDll);
   273             }
   274 
   275         }
   276 
   277         public void Redraw()
   278         {
   279             foreach (SensorFrontView icon in list)
   280                 icon.Update();
   281         }
   282 
   283         public bool Contains(ISensor sensor)
   284         {
   285             foreach (SensorFrontView icon in list)
   286                 if (icon.Sensor == sensor)
   287                     return true;
   288             return false;
   289         }
   290 
   291         public void Add(ISensor sensor, bool balloonTip)
   292         {
   293             if (Contains(sensor))
   294             {
   295                 return;
   296             }
   297             else
   298             {
   299                 //SL:
   300                 //list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   301                 //UpdateMainIconVisibilty();
   302                 //settings.SetValue(new Identifier(sensor.Identifier, "tray").ToString(), true);
   303             }
   304         }
   305 
   306         public void Remove(ISensor sensor)
   307         {
   308             Remove(sensor, true);
   309         }
   310 
   311         private void Remove(ISensor sensor, bool deleteConfig)
   312         {
   313             if (deleteConfig)
   314             {
   315                 settings.Remove(
   316                   new Identifier(sensor.Identifier, "tray").ToString());
   317                 settings.Remove(
   318                   new Identifier(sensor.Identifier, "traycolor").ToString());
   319             }
   320             SensorFrontView instance = null;
   321             foreach (SensorFrontView icon in list)
   322                 if (icon.Sensor == sensor)
   323                     instance = icon;
   324             if (instance != null)
   325             {
   326                 list.Remove(instance);
   327                 UpdateMainIconVisibilty();
   328                 instance.Dispose();
   329             }
   330         }
   331 
   332 
   333 
   334         private void UpdateMainIconVisibilty()
   335         {
   336             /*
   337             if (mainIconEnabled)
   338             {
   339                 mainIcon.Visible = list.Count == 0;
   340             }
   341             else
   342             {
   343                 mainIcon.Visible = false;
   344             }
   345              */
   346         }
   347 
   348         private void Init(IntPtr aHWND)
   349         {
   350             //iIMON_Display_Init
   351         }
   352 
   353         private void Uninit()
   354         {
   355             iIMON_Display_Uninit();
   356         }
   357 
   358         private void SetText(string aUpperLine, string aLowerLine)
   359         {
   360 
   361         }
   362 
   363         /*
   364         public bool IsMainIconEnabled
   365         {
   366             get { return mainIconEnabled; }
   367             set
   368             {
   369                 if (mainIconEnabled != value)
   370                 {
   371                     mainIconEnabled = value;
   372                     UpdateMainIconVisibilty();
   373                 }
   374             }
   375         }*/
   376 
   377 
   378         public bool IsDllLoaded
   379         {
   380             get { return iSoundGraphDll!=IntPtr.Zero; }
   381         }
   382     }
   383 }