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