GUI/SoundGraphDisplay.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
parent 445 fe4c711fd7f8
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     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.Diagnostics;
    16 using System.Windows.Forms;
    17 using System.Windows;
    18 using OpenHardwareMonitor.Hardware;
    19 using OpenHardwareMonitor.Utilities;
    20 using System.Runtime.InteropServices;
    21 using UacHelpers;
    22 
    23 
    24 namespace OpenHardwareMonitor.GUI
    25 {
    26     public class SoundGraphDisplay : IDisposable
    27     {
    28         private IComputer computer;
    29         private PersistentSettings settings;
    30         private UnitManager unitManager;
    31         private List<SensorFrontView> list = new List<SensorFrontView>();
    32         private SoundGraph.Server iServer;
    33 
    34         private int iNextSensorToDisplay=0;
    35         private int iTickCounter=0;
    36 
    37 
    38         public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
    39           UnitManager unitManager)
    40         {
    41             this.computer = computer;
    42             this.settings = settings;
    43             this.unitManager = unitManager;
    44             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    45             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    46 
    47             //Start our client if needed
    48             Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
    49             if (!(processes.Length > 0))
    50             {
    51               try
    52               {
    53                 //Try to launch the sound graph process from the same folder as this executable
    54                 string exeName = System.IO.Path.GetDirectoryName(Application.ExecutablePath);
    55                 exeName += @"\SoundGraphAccess.exe";
    56                 Process client = UserAccountControl.CreateProcessAsStandardUser(exeName, "");
    57               }
    58               catch (Exception e)
    59               {
    60                 Debug.WriteLine("SoundGraphAccess.exe start-up failed: " + e.ToString());
    61               }
    62             }
    63 
    64             //Start our SoundGraph server
    65             iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
    66             iServer.Start();
    67         }
    68 
    69         private void HardwareRemoved(IHardware hardware)
    70         {
    71             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    72             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    73             foreach (ISensor sensor in hardware.Sensors)
    74                 SensorRemoved(sensor);
    75             foreach (IHardware subHardware in hardware.SubHardware)
    76                 HardwareRemoved(subHardware);
    77         }
    78 
    79         private void HardwareAdded(IHardware hardware)
    80         {
    81             foreach (ISensor sensor in hardware.Sensors)
    82                 SensorAdded(sensor);
    83             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    84             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    85             foreach (IHardware subHardware in hardware.SubHardware)
    86                 HardwareAdded(subHardware);
    87         }
    88 
    89         private void SensorAdded(ISensor sensor)
    90         {
    91             if (settings.GetValue(new Identifier(sensor.Identifier,
    92               "FrontView").ToString(), false))
    93                 Add(sensor, false);
    94         }
    95 
    96         private void SensorRemoved(ISensor sensor)
    97         {
    98             if (Contains(sensor))
    99                 Remove(sensor, false);
   100         }
   101 
   102         public void Dispose()
   103         {
   104             foreach (SensorFrontView icon in list)
   105                 icon.Dispose();
   106 
   107             Quit();
   108             iServer.Stop();
   109 
   110         }
   111 
   112         public void Redraw(bool aPacked, bool aDisplayTime)
   113         {
   114             const int KNumberOfTickBeforeSwitch = 4;
   115             const int KMaxCharacterPerLine = 16;
   116             string packedFirstLine=""; //We have 16 chars per line on our VFD
   117             string packedSecondLine="";
   118             int count = 0;
   119 
   120             string time = DateTime.Now.ToShortTimeString();
   121 
   122             //Update all sensors from our front view
   123             foreach (SensorFrontView sensor in list)
   124             {
   125                 count++;
   126                 sensor.Update();
   127 
   128                 if (aDisplayTime && count == 1)
   129                 {
   130                     //First slot is take by time display
   131                     count++;
   132                     packedFirstLine = time + " ";
   133                 }
   134 
   135                 if (aPacked)
   136                 {
   137                     //Build strings for packed mode
   138                     string packedText = "";
   139                     packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
   140                     if (count == 1)
   141                     {
   142                         packedFirstLine = packedText + " "; //Minimum one space to separate sensors on the same line
   143                     }
   144                     else if (count == 2)
   145                     {
   146                         //Add enough spaces to align to right hand side
   147                         while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
   148                         {
   149                             packedFirstLine += " ";
   150                         }
   151                         packedFirstLine += packedText;
   152                     }
   153                     else if (count == 3)
   154                     {
   155                         packedSecondLine = packedText + " "; //Minimum one space to separate sensors on the same line
   156                     }
   157                     else if (count == 4)
   158                     {
   159                         //Add enough spaces to align to right hand side
   160                         while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
   161                         {
   162                             packedSecondLine += " ";
   163                         }
   164                         packedSecondLine += packedText;
   165                     }
   166                 }
   167                 //SetText(sensor.iFirstLine, sensor.iSecondLine);
   168             }
   169 
   170             //Alternate between sensors 
   171             if (list.Count > 0)
   172             {
   173                 if (aPacked)
   174                 {
   175                     //string packedLine = "";
   176                     iTickCounter++;
   177                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   178                     {
   179                         iTickCounter = 0;
   180                         if (iNextSensorToDisplay==1)
   181                         {
   182                             iNextSensorToDisplay=0;
   183                         }
   184                         else
   185                         {
   186                             iNextSensorToDisplay=1;
   187                         }
   188                     }
   189 
   190                     //TODO: Do something like that to cycle lines if ever we want to
   191                     //SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
   192 
   193                     //Display packed sensors on our FrontView display
   194                     SetText(packedFirstLine, packedSecondLine);
   195                 }
   196                 else
   197                 {
   198                     string secondLine = list[iNextSensorToDisplay].iSecondLine;
   199                     if (aDisplayTime)
   200                     {
   201                         //Add enough spaces
   202                         while (secondLine.Length + time.Length < KMaxCharacterPerLine)
   203                         {
   204                             secondLine += " ";
   205                         }
   206                         secondLine += time;
   207                     }
   208                     //Display current sensor on our FrontView display
   209                     SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
   210                     iTickCounter++;
   211                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   212                     {
   213                         iTickCounter = 0;
   214                         iNextSensorToDisplay++;
   215                     }
   216                 }
   217             }
   218 
   219             if (iNextSensorToDisplay == list.Count)
   220             {
   221                 //Go back to first sensor
   222                 iNextSensorToDisplay = 0;
   223             }
   224 
   225             
   226         }
   227 
   228         public bool Contains(ISensor sensor)
   229         {
   230             foreach (SensorFrontView icon in list)
   231                 if (icon.Sensor == sensor)
   232                     return true;
   233             return false;
   234         }
   235 
   236         public void Add(ISensor sensor, bool balloonTip)
   237         {
   238             if (Contains(sensor))
   239             {
   240                 return;
   241             }
   242             else
   243             {
   244                 //SL:
   245                 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   246                 //UpdateMainIconVisibilty();
   247                 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
   248                 iNextSensorToDisplay = 0;
   249                 if (list.Count == 1)
   250                 {
   251                     //Just added first sensor in FrontView, unable FrontView plug-in mode
   252                     Init();
   253                 }
   254             }
   255 
   256         }
   257 
   258         public void Remove(ISensor sensor)
   259         {
   260             Remove(sensor, true);
   261             iNextSensorToDisplay = 0;
   262             if (list.Count == 0)
   263             {
   264                 //No sensor to display in FrontView, just disable FrontView plug-in mode
   265                 Uninit();
   266             }
   267 
   268         }
   269 
   270         private void Remove(ISensor sensor, bool deleteConfig)
   271         {
   272             if (deleteConfig)
   273             {
   274                 settings.Remove(
   275                   new Identifier(sensor.Identifier, "FrontView").ToString());
   276             }
   277             SensorFrontView instance = null;
   278             foreach (SensorFrontView icon in list)
   279                 if (icon.Sensor == sensor)
   280                     instance = icon;
   281             if (instance != null)
   282             {
   283                 list.Remove(instance);
   284                 //UpdateMainIconVisibilty();
   285                 instance.Dispose();
   286             }
   287         }
   288 
   289 
   290 
   291         private void UpdateMainIconVisibilty()
   292         {
   293             /*
   294             if (mainIconEnabled)
   295             {
   296                 mainIcon.Visible = list.Count == 0;
   297             }
   298             else
   299             {
   300                 mainIcon.Visible = false;
   301             }
   302              */
   303         }
   304 
   305         public void Init()
   306         {
   307             iServer.SendMessage("init:");
   308         }
   309 
   310         public void Uninit()
   311         {
   312             iServer.SendMessage("uninit:");
   313         }
   314 
   315         public void SetText(string aUpperLine, string aLowerLine)
   316         {
   317             iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   318         }
   319 
   320         public void Quit()
   321         {
   322             iServer.SendMessage("quit:");
   323         }
   324 
   325         /*
   326         public bool IsMainIconEnabled
   327         {
   328             get { return mainIconEnabled; }
   329             set
   330             {
   331                 if (mainIconEnabled != value)
   332                 {
   333                     mainIconEnabled = value;
   334                     UpdateMainIconVisibilty();
   335                 }
   336             }
   337         }*/
   338 
   339 
   340     }
   341 }