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