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