GUI/SoundGraphDisplay.cs
author StephaneLenclud
Thu, 18 Apr 2013 19:51:05 +0200
changeset 401 70d865489ff3
parent 400 5bc1000c7e1a
child 402 ded1323b61ee
permissions -rw-r--r--
FrontView can now display time when not packed.
Now waiting 4 ticks before cycling.
     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 
    57                 //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
    58                     /*
    59                 Process client = new Process();
    60                 client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
    61                 client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
    62                 client.Start();*/
    63             }
    64 
    65             //Start our SoundGraph server
    66             iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
    67             iServer.Start();
    68             //iServer.SendMessage("init:");
    69         }
    70 
    71         private void HardwareRemoved(IHardware hardware)
    72         {
    73             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    74             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    75             foreach (ISensor sensor in hardware.Sensors)
    76                 SensorRemoved(sensor);
    77             foreach (IHardware subHardware in hardware.SubHardware)
    78                 HardwareRemoved(subHardware);
    79         }
    80 
    81         private void HardwareAdded(IHardware hardware)
    82         {
    83             foreach (ISensor sensor in hardware.Sensors)
    84                 SensorAdded(sensor);
    85             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    86             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    87             foreach (IHardware subHardware in hardware.SubHardware)
    88                 HardwareAdded(subHardware);
    89         }
    90 
    91         private void SensorAdded(ISensor sensor)
    92         {
    93             if (settings.GetValue(new Identifier(sensor.Identifier,
    94               "FrontView").ToString(), false))
    95                 Add(sensor, false);
    96         }
    97 
    98         private void SensorRemoved(ISensor sensor)
    99         {
   100             if (Contains(sensor))
   101                 Remove(sensor, false);
   102         }
   103 
   104         public void Dispose()
   105         {
   106             foreach (SensorFrontView icon in list)
   107                 icon.Dispose();
   108 
   109             Quit();
   110             iServer.Stop();
   111 
   112         }
   113 
   114         public void Redraw(bool aPacked, bool aDisplayTime)
   115         {
   116             const int KNumberOfTickBeforeSwitch = 4;
   117             const int KMaxCharacterPerLine = 16;
   118             string packedFirstLine=""; //We have 16 chars per line on our VFD
   119             string packedSecondLine="";
   120             int count = 0;
   121 
   122             string time = DateTime.Now.ToShortTimeString();
   123 
   124             //Update all sensors from our front view
   125             foreach (SensorFrontView sensor in list)
   126             {
   127                 count++;
   128                 sensor.Update();
   129 
   130                 if (aPacked)
   131                 {
   132                     string packedText = "";
   133                     packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
   134                     if (count == 1)
   135                     {
   136                         packedFirstLine = packedText;
   137                     }
   138                     else if (count == 2)
   139                     {
   140                         //Add enough spaces
   141                         while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
   142                         {
   143                             packedFirstLine += " ";
   144                         }
   145                         packedFirstLine += packedText;
   146                     }
   147                     else if (count == 3)
   148                     {
   149                         packedSecondLine = packedText;
   150                     }
   151                     else if (count == 4)
   152                     {
   153                         //Add enough spaces
   154                         while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
   155                         {
   156                             packedSecondLine += " ";
   157                         }
   158                         packedSecondLine += packedText;
   159                     }
   160 
   161 
   162                 }
   163                 //SetText(sensor.iFirstLine, sensor.iSecondLine);
   164             }
   165 
   166             //Alternate between sensors 
   167             if (list.Count > 0)
   168             {
   169                 if (aPacked)
   170                 {
   171                     //string packedLine = "";
   172                     iTickCounter++;
   173                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   174                     {
   175                         iTickCounter = 0;
   176                         if (iNextSensorToDisplay==1)
   177                         {
   178                             iNextSensorToDisplay=0;
   179                         }
   180                         else
   181                         {
   182                             iNextSensorToDisplay=1;
   183                         }
   184                     }
   185 
   186                     if (aDisplayTime)
   187                     {
   188                         SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
   189                     }
   190                     else
   191                     {
   192                         //Display packed sensors on our FrontView display
   193                         SetText(packedFirstLine, packedSecondLine);
   194                     }
   195 
   196 
   197                 }
   198                 else
   199                 {
   200                     string secondLine = list[iNextSensorToDisplay].iSecondLine;
   201                     if (aDisplayTime)
   202                     {
   203                         //Add enough spaces
   204                         while (secondLine.Length + time.Length < KMaxCharacterPerLine)
   205                         {
   206                             secondLine += " ";
   207                         }
   208                         secondLine += time;
   209                     }
   210                     //Display current sensor on our FrontView display
   211                     SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
   212                     iTickCounter++;
   213                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   214                     {
   215                         iTickCounter = 0;
   216                         iNextSensorToDisplay++;
   217                     }
   218                 }
   219             }
   220 
   221             if (iNextSensorToDisplay == list.Count)
   222             {
   223                 //Go back to first sensor
   224                 iNextSensorToDisplay = 0;
   225             }
   226 
   227             
   228         }
   229 
   230         public bool Contains(ISensor sensor)
   231         {
   232             foreach (SensorFrontView icon in list)
   233                 if (icon.Sensor == sensor)
   234                     return true;
   235             return false;
   236         }
   237 
   238         public void Add(ISensor sensor, bool balloonTip)
   239         {
   240             if (Contains(sensor))
   241             {
   242                 return;
   243             }
   244             else
   245             {
   246                 //SL:
   247                 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   248                 //UpdateMainIconVisibilty();
   249                 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
   250                 iNextSensorToDisplay = 0;
   251             }
   252 
   253         }
   254 
   255         public void Remove(ISensor sensor)
   256         {
   257             Remove(sensor, true);
   258             iNextSensorToDisplay = 0;
   259         }
   260 
   261         private void Remove(ISensor sensor, bool deleteConfig)
   262         {
   263             if (deleteConfig)
   264             {
   265                 settings.Remove(
   266                   new Identifier(sensor.Identifier, "FrontView").ToString());
   267             }
   268             SensorFrontView instance = null;
   269             foreach (SensorFrontView icon in list)
   270                 if (icon.Sensor == sensor)
   271                     instance = icon;
   272             if (instance != null)
   273             {
   274                 list.Remove(instance);
   275                 //UpdateMainIconVisibilty();
   276                 instance.Dispose();
   277             }
   278         }
   279 
   280 
   281 
   282         private void UpdateMainIconVisibilty()
   283         {
   284             /*
   285             if (mainIconEnabled)
   286             {
   287                 mainIcon.Visible = list.Count == 0;
   288             }
   289             else
   290             {
   291                 mainIcon.Visible = false;
   292             }
   293              */
   294         }
   295 
   296         public void Init()
   297         {
   298             iServer.SendMessage("init:");
   299         }
   300 
   301         public void Uninit()
   302         {
   303             iServer.SendMessage("uninit:");
   304         }
   305 
   306         public void SetText(string aUpperLine, string aLowerLine)
   307         {
   308             iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   309         }
   310 
   311         public void Quit()
   312         {
   313             iServer.SendMessage("quit:");
   314         }
   315 
   316         /*
   317         public bool IsMainIconEnabled
   318         {
   319             get { return mainIconEnabled; }
   320             set
   321             {
   322                 if (mainIconEnabled != value)
   323                 {
   324                     mainIconEnabled = value;
   325                     UpdateMainIconVisibilty();
   326                 }
   327             }
   328         }*/
   329 
   330 
   331     }
   332 }