StephaneLenclud@433: /* StephaneLenclud@433: StephaneLenclud@433: This Source Code Form is subject to the terms of the Mozilla Public StephaneLenclud@433: License, v. 2.0. If a copy of the MPL was not distributed with this StephaneLenclud@433: file, You can obtain one at http://mozilla.org/MPL/2.0/. StephaneLenclud@433: StephaneLenclud@433: Copyright (C) 2009-2012 Michael Möller StephaneLenclud@433: StephaneLenclud@433: */ StephaneLenclud@433: StephaneLenclud@433: using System; StephaneLenclud@433: using System.Collections.Generic; StephaneLenclud@433: using System.Drawing; StephaneLenclud@433: using System.Text; StephaneLenclud@436: using System.Diagnostics; StephaneLenclud@433: using System.Windows.Forms; StephaneLenclud@435: using System.Windows; StephaneLenclud@433: using OpenHardwareMonitor.Hardware; StephaneLenclud@433: using OpenHardwareMonitor.Utilities; StephaneLenclud@433: using System.Runtime.InteropServices; StephaneLenclud@436: using UacHelpers; StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@434: StephaneLenclud@433: StephaneLenclud@433: namespace OpenHardwareMonitor.GUI StephaneLenclud@433: { StephaneLenclud@433: public class SoundGraphDisplay : IDisposable StephaneLenclud@433: { StephaneLenclud@433: private IComputer computer; StephaneLenclud@433: private PersistentSettings settings; StephaneLenclud@433: private UnitManager unitManager; StephaneLenclud@433: private List list = new List(); StephaneLenclud@436: private SoundGraph.Server iServer; StephaneLenclud@436: StephaneLenclud@439: private int iNextSensorToDisplay=0; StephaneLenclud@439: private int iTickCounter=0; StephaneLenclud@438: StephaneLenclud@433: StephaneLenclud@433: public SoundGraphDisplay(IComputer computer, PersistentSettings settings, StephaneLenclud@433: UnitManager unitManager) StephaneLenclud@433: { StephaneLenclud@433: this.computer = computer; StephaneLenclud@433: this.settings = settings; StephaneLenclud@433: this.unitManager = unitManager; StephaneLenclud@433: computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); StephaneLenclud@433: computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); StephaneLenclud@433: StephaneLenclud@436: //Start our client if needed StephaneLenclud@436: Process[] processes = Process.GetProcessesByName("SoundGraphAccess"); StephaneLenclud@436: if (!(processes.Length > 0)) StephaneLenclud@436: { StephaneLenclud@436: StephaneLenclud@436: //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe",""); StephaneLenclud@436: /* StephaneLenclud@436: Process client = new Process(); StephaneLenclud@436: client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe"; StephaneLenclud@436: client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess"; StephaneLenclud@436: client.Start();*/ StephaneLenclud@436: } StephaneLenclud@436: StephaneLenclud@437: //Start our SoundGraph server StephaneLenclud@437: iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound"); StephaneLenclud@436: iServer.Start(); StephaneLenclud@436: //iServer.SendMessage("init:"); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: private void HardwareRemoved(IHardware hardware) StephaneLenclud@433: { StephaneLenclud@433: hardware.SensorAdded -= new SensorEventHandler(SensorAdded); StephaneLenclud@433: hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved); StephaneLenclud@433: foreach (ISensor sensor in hardware.Sensors) StephaneLenclud@433: SensorRemoved(sensor); StephaneLenclud@433: foreach (IHardware subHardware in hardware.SubHardware) StephaneLenclud@433: HardwareRemoved(subHardware); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: private void HardwareAdded(IHardware hardware) StephaneLenclud@433: { StephaneLenclud@433: foreach (ISensor sensor in hardware.Sensors) StephaneLenclud@433: SensorAdded(sensor); StephaneLenclud@433: hardware.SensorAdded += new SensorEventHandler(SensorAdded); StephaneLenclud@433: hardware.SensorRemoved += new SensorEventHandler(SensorRemoved); StephaneLenclud@433: foreach (IHardware subHardware in hardware.SubHardware) StephaneLenclud@433: HardwareAdded(subHardware); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: private void SensorAdded(ISensor sensor) StephaneLenclud@433: { StephaneLenclud@433: if (settings.GetValue(new Identifier(sensor.Identifier, StephaneLenclud@438: "FrontView").ToString(), false)) StephaneLenclud@433: Add(sensor, false); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: private void SensorRemoved(ISensor sensor) StephaneLenclud@433: { StephaneLenclud@433: if (Contains(sensor)) StephaneLenclud@433: Remove(sensor, false); StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public void Dispose() StephaneLenclud@433: { StephaneLenclud@433: foreach (SensorFrontView icon in list) StephaneLenclud@433: icon.Dispose(); StephaneLenclud@433: StephaneLenclud@437: Quit(); StephaneLenclud@436: iServer.Stop(); StephaneLenclud@433: StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public void Redraw() StephaneLenclud@433: { StephaneLenclud@439: //Update all sensors from our front view StephaneLenclud@438: foreach (SensorFrontView sensor in list) StephaneLenclud@438: { StephaneLenclud@438: sensor.Update(); StephaneLenclud@438: //SetText(sensor.iFirstLine, sensor.iSecondLine); StephaneLenclud@438: } StephaneLenclud@438: StephaneLenclud@438: //Alternate between sensors StephaneLenclud@438: if (list.Count > 0) StephaneLenclud@438: { StephaneLenclud@439: //Display current sensor on our FrontView display StephaneLenclud@439: SetText(list[iNextSensorToDisplay].iFirstLine, list[iNextSensorToDisplay].iSecondLine); StephaneLenclud@439: iTickCounter++; StephaneLenclud@439: if (iTickCounter==2) //Move to the next sensor only every second tick StephaneLenclud@439: { StephaneLenclud@439: iTickCounter = 0; StephaneLenclud@439: iNextSensorToDisplay++; StephaneLenclud@439: } StephaneLenclud@438: } StephaneLenclud@438: StephaneLenclud@439: if (iNextSensorToDisplay == list.Count) StephaneLenclud@438: { StephaneLenclud@438: //Go back to first sensor StephaneLenclud@439: iNextSensorToDisplay = 0; StephaneLenclud@438: } StephaneLenclud@438: StephaneLenclud@438: StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public bool Contains(ISensor sensor) StephaneLenclud@433: { StephaneLenclud@433: foreach (SensorFrontView icon in list) StephaneLenclud@433: if (icon.Sensor == sensor) StephaneLenclud@433: return true; StephaneLenclud@433: return false; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public void Add(ISensor sensor, bool balloonTip) StephaneLenclud@433: { StephaneLenclud@433: if (Contains(sensor)) StephaneLenclud@433: { StephaneLenclud@433: return; StephaneLenclud@433: } StephaneLenclud@433: else StephaneLenclud@433: { StephaneLenclud@433: //SL: StephaneLenclud@438: list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager)); StephaneLenclud@433: //UpdateMainIconVisibilty(); StephaneLenclud@438: settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true); StephaneLenclud@439: iNextSensorToDisplay = 0; StephaneLenclud@433: } StephaneLenclud@438: StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: public void Remove(ISensor sensor) StephaneLenclud@433: { StephaneLenclud@433: Remove(sensor, true); StephaneLenclud@439: iNextSensorToDisplay = 0; StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: private void Remove(ISensor sensor, bool deleteConfig) StephaneLenclud@433: { StephaneLenclud@433: if (deleteConfig) StephaneLenclud@433: { StephaneLenclud@433: settings.Remove( StephaneLenclud@438: new Identifier(sensor.Identifier, "FrontView").ToString()); StephaneLenclud@433: } StephaneLenclud@433: SensorFrontView instance = null; StephaneLenclud@433: foreach (SensorFrontView icon in list) StephaneLenclud@433: if (icon.Sensor == sensor) StephaneLenclud@433: instance = icon; StephaneLenclud@433: if (instance != null) StephaneLenclud@433: { StephaneLenclud@433: list.Remove(instance); StephaneLenclud@438: //UpdateMainIconVisibilty(); StephaneLenclud@433: instance.Dispose(); StephaneLenclud@433: } StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: StephaneLenclud@433: private void UpdateMainIconVisibilty() StephaneLenclud@433: { StephaneLenclud@433: /* StephaneLenclud@433: if (mainIconEnabled) StephaneLenclud@433: { StephaneLenclud@433: mainIcon.Visible = list.Count == 0; StephaneLenclud@433: } StephaneLenclud@433: else StephaneLenclud@433: { StephaneLenclud@433: mainIcon.Visible = false; StephaneLenclud@433: } StephaneLenclud@433: */ StephaneLenclud@433: } StephaneLenclud@433: StephaneLenclud@436: public void Init() StephaneLenclud@434: { StephaneLenclud@436: iServer.SendMessage("init:"); StephaneLenclud@434: } StephaneLenclud@434: StephaneLenclud@435: public void Uninit() StephaneLenclud@434: { StephaneLenclud@436: iServer.SendMessage("uninit:"); StephaneLenclud@434: } StephaneLenclud@434: StephaneLenclud@435: public void SetText(string aUpperLine, string aLowerLine) StephaneLenclud@434: { StephaneLenclud@438: iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine); StephaneLenclud@434: } StephaneLenclud@434: StephaneLenclud@437: public void Quit() StephaneLenclud@437: { StephaneLenclud@437: iServer.SendMessage("quit:"); StephaneLenclud@437: } StephaneLenclud@437: StephaneLenclud@433: /* StephaneLenclud@433: public bool IsMainIconEnabled StephaneLenclud@433: { StephaneLenclud@433: get { return mainIconEnabled; } StephaneLenclud@433: set StephaneLenclud@433: { StephaneLenclud@433: if (mainIconEnabled != value) StephaneLenclud@433: { StephaneLenclud@433: mainIconEnabled = value; StephaneLenclud@433: UpdateMainIconVisibilty(); StephaneLenclud@433: } StephaneLenclud@433: } StephaneLenclud@433: }*/ StephaneLenclud@434: StephaneLenclud@435: StephaneLenclud@433: } StephaneLenclud@433: }