GUI/SoundGraphDisplay.cs
author StephaneLenclud
Thu, 18 Apr 2013 16:05:47 +0200
branchMiniDisplay
changeset 442 782b48689513
parent 439 06369ace500d
child 443 8c139748f179
permissions -rw-r--r--
Implementing FrontView support for packed mode and time display.
     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             string packedFirstLine=""; //We have 16 chars per line on our VFD
   117             string packedSecondLine="";
   118             int count = 0;
   119 
   120             //Update all sensors from our front view
   121             foreach (SensorFrontView sensor in list)
   122             {
   123                 count++;
   124                 sensor.Update();
   125 
   126                 if (aPacked)
   127                 {
   128                     string packedText = "";
   129                     packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
   130                     if (count == 1)
   131                     {
   132                         packedFirstLine = packedText;
   133                     }
   134                     else if (count == 2)
   135                     {
   136                         //Add enough spaces
   137                         while (packedFirstLine.Length + packedText.Length < 16)
   138                         {
   139                             packedFirstLine += " ";
   140                         }
   141                         packedFirstLine += packedText;
   142                     }
   143                     else if (count == 3)
   144                     {
   145                         packedSecondLine = packedText;
   146                     }
   147                     else if (count == 4)
   148                     {
   149                         //Add enough spaces
   150                         while (packedSecondLine.Length + packedText.Length < 16)
   151                         {
   152                             packedSecondLine += " ";
   153                         }
   154                         packedSecondLine += packedText;
   155                     }
   156 
   157 
   158                 }
   159                 //SetText(sensor.iFirstLine, sensor.iSecondLine);
   160             }
   161 
   162             //Alternate between sensors 
   163             if (list.Count > 0)
   164             {
   165                 if (aPacked)
   166                 {
   167                     //string packedLine = "";
   168                     iTickCounter++;
   169                     if (iTickCounter == 2) //Move to the next sensor only every second tick
   170                     {
   171                         iTickCounter = 0;
   172                         if (iNextSensorToDisplay==1)
   173                         {
   174                             iNextSensorToDisplay=0;
   175                         }
   176                         else
   177                         {
   178                             iNextSensorToDisplay=1;
   179                         }
   180                     }
   181 
   182                     if (aDisplayTime)
   183                     {
   184                         string time = DateTime.Now.ToShortTimeString();
   185                         SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
   186                     }
   187                     else
   188                     {
   189                         //Display packed sensors on our FrontView display
   190                         SetText(packedFirstLine, packedSecondLine);
   191                     }
   192 
   193 
   194                 }
   195                 else
   196                 {
   197                     //Display current sensor on our FrontView display
   198                     SetText(list[iNextSensorToDisplay].iFirstLine, list[iNextSensorToDisplay].iSecondLine);
   199                     iTickCounter++;
   200                     if (iTickCounter == 2) //Move to the next sensor only every second tick
   201                     {
   202                         iTickCounter = 0;
   203                         iNextSensorToDisplay++;
   204                     }
   205                 }
   206             }
   207 
   208             if (iNextSensorToDisplay == list.Count)
   209             {
   210                 //Go back to first sensor
   211                 iNextSensorToDisplay = 0;
   212             }
   213 
   214             
   215         }
   216 
   217         public bool Contains(ISensor sensor)
   218         {
   219             foreach (SensorFrontView icon in list)
   220                 if (icon.Sensor == sensor)
   221                     return true;
   222             return false;
   223         }
   224 
   225         public void Add(ISensor sensor, bool balloonTip)
   226         {
   227             if (Contains(sensor))
   228             {
   229                 return;
   230             }
   231             else
   232             {
   233                 //SL:
   234                 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   235                 //UpdateMainIconVisibilty();
   236                 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
   237                 iNextSensorToDisplay = 0;
   238             }
   239 
   240         }
   241 
   242         public void Remove(ISensor sensor)
   243         {
   244             Remove(sensor, true);
   245             iNextSensorToDisplay = 0;
   246         }
   247 
   248         private void Remove(ISensor sensor, bool deleteConfig)
   249         {
   250             if (deleteConfig)
   251             {
   252                 settings.Remove(
   253                   new Identifier(sensor.Identifier, "FrontView").ToString());
   254             }
   255             SensorFrontView instance = null;
   256             foreach (SensorFrontView icon in list)
   257                 if (icon.Sensor == sensor)
   258                     instance = icon;
   259             if (instance != null)
   260             {
   261                 list.Remove(instance);
   262                 //UpdateMainIconVisibilty();
   263                 instance.Dispose();
   264             }
   265         }
   266 
   267 
   268 
   269         private void UpdateMainIconVisibilty()
   270         {
   271             /*
   272             if (mainIconEnabled)
   273             {
   274                 mainIcon.Visible = list.Count == 0;
   275             }
   276             else
   277             {
   278                 mainIcon.Visible = false;
   279             }
   280              */
   281         }
   282 
   283         public void Init()
   284         {
   285             iServer.SendMessage("init:");
   286         }
   287 
   288         public void Uninit()
   289         {
   290             iServer.SendMessage("uninit:");
   291         }
   292 
   293         public void SetText(string aUpperLine, string aLowerLine)
   294         {
   295             iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   296         }
   297 
   298         public void Quit()
   299         {
   300             iServer.SendMessage("quit:");
   301         }
   302 
   303         /*
   304         public bool IsMainIconEnabled
   305         {
   306             get { return mainIconEnabled; }
   307             set
   308             {
   309                 if (mainIconEnabled != value)
   310                 {
   311                     mainIconEnabled = value;
   312                     UpdateMainIconVisibilty();
   313                 }
   314             }
   315         }*/
   316 
   317 
   318     }
   319 }