GUI/SoundGraphDisplay.cs
author StephaneLenclud
Sat, 13 Apr 2013 00:43:25 +0200
changeset 396 21a9e2325617
parent 395 df649224ba4d
child 397 2dc91822f312
permissions -rw-r--r--
Sensors can now be displayed in FrontView.
     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 nextSensorToDisplay=0;
    40 
    41 
    42         public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
    43           UnitManager unitManager)
    44         {
    45             this.computer = computer;
    46             this.settings = settings;
    47             this.unitManager = unitManager;
    48             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    49             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    50 
    51             //Start our client if needed
    52             Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
    53             if (!(processes.Length > 0))
    54             {
    55 
    56                 //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
    57                     /*
    58                 Process client = new Process();
    59                 client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
    60                 client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
    61                 client.Start();*/
    62             }
    63 
    64             //Start our SoundGraph server
    65             iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
    66             iServer.Start();
    67             //iServer.SendMessage("init:");
    68         }
    69 
    70         private void HardwareRemoved(IHardware hardware)
    71         {
    72             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    73             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    74             foreach (ISensor sensor in hardware.Sensors)
    75                 SensorRemoved(sensor);
    76             foreach (IHardware subHardware in hardware.SubHardware)
    77                 HardwareRemoved(subHardware);
    78         }
    79 
    80         private void HardwareAdded(IHardware hardware)
    81         {
    82             foreach (ISensor sensor in hardware.Sensors)
    83                 SensorAdded(sensor);
    84             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    85             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    86             foreach (IHardware subHardware in hardware.SubHardware)
    87                 HardwareAdded(subHardware);
    88         }
    89 
    90         private void SensorAdded(ISensor sensor)
    91         {
    92             if (settings.GetValue(new Identifier(sensor.Identifier,
    93               "FrontView").ToString(), false))
    94                 Add(sensor, false);
    95         }
    96 
    97         private void SensorRemoved(ISensor sensor)
    98         {
    99             if (Contains(sensor))
   100                 Remove(sensor, false);
   101         }
   102 
   103         public void Dispose()
   104         {
   105             foreach (SensorFrontView icon in list)
   106                 icon.Dispose();
   107 
   108             Quit();
   109             iServer.Stop();
   110 
   111         }
   112 
   113         public void Redraw()
   114         {
   115             //TODO: construct our two lines of texts, scroll or alternate sensors
   116             foreach (SensorFrontView sensor in list)
   117             {
   118                 sensor.Update();
   119                 //SetText(sensor.iFirstLine, sensor.iSecondLine);
   120             }
   121 
   122             //Alternate between sensors 
   123             if (list.Count > 0)
   124             {
   125                 SetText(list[nextSensorToDisplay].iFirstLine, list[nextSensorToDisplay].iSecondLine);
   126                 nextSensorToDisplay++;
   127             }
   128 
   129             if (nextSensorToDisplay == list.Count)
   130             {
   131                 //Go back to first sensor
   132                 nextSensorToDisplay = 0;
   133             }
   134 
   135 
   136             //
   137             
   138         }
   139 
   140         public bool Contains(ISensor sensor)
   141         {
   142             foreach (SensorFrontView icon in list)
   143                 if (icon.Sensor == sensor)
   144                     return true;
   145             return false;
   146         }
   147 
   148         public void Add(ISensor sensor, bool balloonTip)
   149         {
   150             if (Contains(sensor))
   151             {
   152                 return;
   153             }
   154             else
   155             {
   156                 //SL:
   157                 list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
   158                 //UpdateMainIconVisibilty();
   159                 settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
   160                 nextSensorToDisplay = 0;
   161             }
   162 
   163         }
   164 
   165         public void Remove(ISensor sensor)
   166         {
   167             Remove(sensor, true);
   168             nextSensorToDisplay = 0;
   169         }
   170 
   171         private void Remove(ISensor sensor, bool deleteConfig)
   172         {
   173             if (deleteConfig)
   174             {
   175                 settings.Remove(
   176                   new Identifier(sensor.Identifier, "FrontView").ToString());
   177             }
   178             SensorFrontView instance = null;
   179             foreach (SensorFrontView icon in list)
   180                 if (icon.Sensor == sensor)
   181                     instance = icon;
   182             if (instance != null)
   183             {
   184                 list.Remove(instance);
   185                 //UpdateMainIconVisibilty();
   186                 instance.Dispose();
   187             }
   188         }
   189 
   190 
   191 
   192         private void UpdateMainIconVisibilty()
   193         {
   194             /*
   195             if (mainIconEnabled)
   196             {
   197                 mainIcon.Visible = list.Count == 0;
   198             }
   199             else
   200             {
   201                 mainIcon.Visible = false;
   202             }
   203              */
   204         }
   205 
   206         public void Init()
   207         {
   208             iServer.SendMessage("init:");
   209         }
   210 
   211         public void Uninit()
   212         {
   213             iServer.SendMessage("uninit:");
   214         }
   215 
   216         public void SetText(string aUpperLine, string aLowerLine)
   217         {
   218             iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   219         }
   220 
   221         public void Quit()
   222         {
   223             iServer.SendMessage("quit:");
   224         }
   225 
   226         /*
   227         public bool IsMainIconEnabled
   228         {
   229             get { return mainIconEnabled; }
   230             set
   231             {
   232                 if (mainIconEnabled != value)
   233                 {
   234                     mainIconEnabled = value;
   235                     UpdateMainIconVisibilty();
   236                 }
   237             }
   238         }*/
   239 
   240 
   241     }
   242 }