StephaneLenclud@445: /* StephaneLenclud@445: StephaneLenclud@445: This Source Code Form is subject to the terms of the Mozilla Public StephaneLenclud@445: License, v. 2.0. If a copy of the MPL was not distributed with this StephaneLenclud@445: file, You can obtain one at http://mozilla.org/MPL/2.0/. StephaneLenclud@445: StephaneLenclud@445: Copyright (C) 2009-2012 Michael Möller StephaneLenclud@445: StephaneLenclud@445: */ StephaneLenclud@445: StephaneLenclud@445: using System; StephaneLenclud@445: using System.Collections.Generic; StephaneLenclud@445: using System.Drawing; StephaneLenclud@445: using System.Text; StephaneLenclud@445: using System.Diagnostics; StephaneLenclud@445: using System.Windows.Forms; StephaneLenclud@445: using System.Windows; StephaneLenclud@445: using OpenHardwareMonitor.Hardware; StephaneLenclud@445: using OpenHardwareMonitor.Utilities; StephaneLenclud@445: using System.Runtime.InteropServices; StephaneLenclud@445: using System.ServiceModel; Stephane@451: using SharpLib.Display; StephaneLenclud@445: StephaneLenclud@445: StephaneLenclud@445: namespace OpenHardwareMonitor.GUI StephaneLenclud@445: { StephaneLenclud@447: public class SharpDisplay : IDisposable StephaneLenclud@445: { StephaneLenclud@445: private IComputer computer; StephaneLenclud@445: private PersistentSettings settings; StephaneLenclud@445: private UnitManager unitManager; StephaneLenclud@446: private List iSensors = new List(); Stephane@451: private Client iClient; Stephane@451: TextField iTextFieldTop; Stephane@451: TextField iTextFieldBottom; Stephane@451: TextField iTextFieldTopRight; Stephane@451: TextField iTextFieldBottomRight; StephaneLenclud@446: Stephane@451: DataField[] iTextFields; StephaneLenclud@445: StephaneLenclud@445: private int iNextSensorToDisplay = 0; StephaneLenclud@445: private int iTickCounter = 0; StephaneLenclud@446: bool iPacked = false; StephaneLenclud@445: StephaneLenclud@445: public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager) StephaneLenclud@445: { StephaneLenclud@445: this.computer = computer; StephaneLenclud@445: this.settings = settings; StephaneLenclud@445: this.unitManager = unitManager; StephaneLenclud@445: computer.HardwareAdded += new HardwareEventHandler(HardwareAdded); StephaneLenclud@445: computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved); StephaneLenclud@445: StephaneLenclud@445: //Connect our client StephaneLenclud@445: //Instance context is then managed by our client class Stephane@451: iClient = new Client(); StephaneLenclud@453: iClient.CloseOrderEvent += OnCloseOrder; StephaneLenclud@445: // StephaneLenclud@453: iTextFieldTop = new TextField("", ContentAlignment.MiddleLeft,0,0); Stephane@451: iTextFieldBottom = new TextField("", ContentAlignment.MiddleLeft, 0, 1); Stephane@451: iTextFieldTopRight = new TextField("", ContentAlignment.MiddleRight,1,0); Stephane@451: iTextFieldBottomRight = new TextField("", ContentAlignment.MiddleRight,1,1); StephaneLenclud@447: // StephaneLenclud@447: iClient.Open(); StephaneLenclud@447: iClient.SetName("Open Hardware Monitor"); StephaneLenclud@453: iClient.SetPriority(Priorities.SystemMonitor); StephaneLenclud@452: StephaneLenclud@453: CreateFields(); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@453: public void OnCloseOrder() StephaneLenclud@453: { StephaneLenclud@453: iClient.Close(); StephaneLenclud@453: } StephaneLenclud@453: StephaneLenclud@453: StephaneLenclud@445: private void HardwareRemoved(IHardware hardware) StephaneLenclud@445: { StephaneLenclud@445: hardware.SensorAdded -= new SensorEventHandler(SensorAdded); StephaneLenclud@445: hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved); StephaneLenclud@445: foreach (ISensor sensor in hardware.Sensors) StephaneLenclud@445: SensorRemoved(sensor); StephaneLenclud@445: foreach (IHardware subHardware in hardware.SubHardware) StephaneLenclud@445: HardwareRemoved(subHardware); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: private void HardwareAdded(IHardware hardware) StephaneLenclud@445: { StephaneLenclud@445: foreach (ISensor sensor in hardware.Sensors) StephaneLenclud@445: SensorAdded(sensor); StephaneLenclud@445: hardware.SensorAdded += new SensorEventHandler(SensorAdded); StephaneLenclud@445: hardware.SensorRemoved += new SensorEventHandler(SensorRemoved); StephaneLenclud@445: foreach (IHardware subHardware in hardware.SubHardware) StephaneLenclud@445: HardwareAdded(subHardware); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: private void SensorAdded(ISensor sensor) StephaneLenclud@445: { StephaneLenclud@445: if (settings.GetValue(new Identifier(sensor.Identifier, StephaneLenclud@445: "SharpDisplay").ToString(), false)) StephaneLenclud@445: Add(sensor, false); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: private void SensorRemoved(ISensor sensor) StephaneLenclud@445: { StephaneLenclud@445: if (Contains(sensor)) StephaneLenclud@445: Remove(sensor, false); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Dispose() StephaneLenclud@445: { StephaneLenclud@446: foreach (SensorSharpDisplay icon in iSensors) StephaneLenclud@445: icon.Dispose(); StephaneLenclud@445: StephaneLenclud@445: Quit(); StephaneLenclud@445: //iServer.Stop(); StephaneLenclud@445: iClient.Close(); StephaneLenclud@445: StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@447: private void CreateFields() StephaneLenclud@447: { StephaneLenclud@447: if (iPacked) StephaneLenclud@447: { StephaneLenclud@447: //We just switched to packed mode StephaneLenclud@447: //Make sure our layout is proper StephaneLenclud@447: TableLayout layout = new TableLayout(2, 2); StephaneLenclud@447: iClient.SetLayout(layout); StephaneLenclud@447: iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom, iTextFieldTopRight, iTextFieldBottomRight }; StephaneLenclud@447: iClient.CreateFields(iTextFields); StephaneLenclud@447: } StephaneLenclud@447: else StephaneLenclud@447: { StephaneLenclud@447: //Non packed mode StephaneLenclud@447: TableLayout layout = new TableLayout(1, 2); StephaneLenclud@447: iClient.SetLayout(layout); StephaneLenclud@447: iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom }; StephaneLenclud@447: iClient.CreateFields(iTextFields); StephaneLenclud@447: } StephaneLenclud@447: } StephaneLenclud@447: StephaneLenclud@445: public void Redraw(bool aPacked, bool aDisplayTime) StephaneLenclud@445: { StephaneLenclud@445: const int KNumberOfTickBeforeSwitch = 4; StephaneLenclud@445: const int KMaxCharacterPerLine = 16; StephaneLenclud@445: int count = 0; StephaneLenclud@445: StephaneLenclud@446: //string time = DateTime.Now.ToShortTimeString(); StephaneLenclud@446: string time = DateTime.Now.ToLongTimeString(); StephaneLenclud@446: StephaneLenclud@446: if (iSensors.Count > 0) StephaneLenclud@446: { StephaneLenclud@446: if (iPacked != aPacked) StephaneLenclud@446: { StephaneLenclud@446: //Remember mode StephaneLenclud@446: iPacked = aPacked; StephaneLenclud@446: StephaneLenclud@447: CreateFields(); StephaneLenclud@447: StephaneLenclud@446: } StephaneLenclud@446: } StephaneLenclud@446: StephaneLenclud@445: StephaneLenclud@445: //Update all sensors from our front view StephaneLenclud@446: foreach (SensorSharpDisplay sensor in iSensors) StephaneLenclud@445: { StephaneLenclud@445: count++; StephaneLenclud@445: sensor.Update(); StephaneLenclud@445: StephaneLenclud@445: if (aDisplayTime && count == 1) StephaneLenclud@445: { StephaneLenclud@446: //First slot is taken by time display StephaneLenclud@445: count++; StephaneLenclud@446: iTextFieldTop.Text = time; StephaneLenclud@447: iClient.SetField(iTextFieldTop); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: if (aPacked) StephaneLenclud@445: { StephaneLenclud@445: //Build strings for packed mode StephaneLenclud@445: string packedText = ""; StephaneLenclud@445: packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine; StephaneLenclud@445: if (count == 1) StephaneLenclud@445: { StephaneLenclud@446: iTextFieldTop.Text = packedText; StephaneLenclud@447: iClient.SetField(iTextFieldTop); StephaneLenclud@445: } StephaneLenclud@445: else if (count == 2) StephaneLenclud@445: { StephaneLenclud@446: iTextFieldBottom.Text = packedText; StephaneLenclud@447: iClient.SetField(iTextFieldBottom); StephaneLenclud@445: } StephaneLenclud@445: else if (count == 3) StephaneLenclud@445: { StephaneLenclud@446: iTextFieldTopRight.Text = packedText; StephaneLenclud@447: iClient.SetField(iTextFieldTopRight); StephaneLenclud@445: } StephaneLenclud@445: else if (count == 4) StephaneLenclud@445: { StephaneLenclud@446: iTextFieldBottomRight.Text = packedText; StephaneLenclud@447: iClient.SetField(iTextFieldBottomRight); StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: //Alternate between sensors StephaneLenclud@446: if (iSensors.Count > 0) StephaneLenclud@445: { StephaneLenclud@445: if (aPacked) StephaneLenclud@445: { StephaneLenclud@446: //Review that stuff cause as it is it's probably useless StephaneLenclud@445: //string packedLine = ""; StephaneLenclud@445: iTickCounter++; StephaneLenclud@445: if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick StephaneLenclud@445: { StephaneLenclud@445: iTickCounter = 0; StephaneLenclud@445: if (iNextSensorToDisplay == 1) StephaneLenclud@445: { StephaneLenclud@445: iNextSensorToDisplay = 0; StephaneLenclud@445: } StephaneLenclud@445: else StephaneLenclud@445: { StephaneLenclud@445: iNextSensorToDisplay = 1; StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: else StephaneLenclud@445: { StephaneLenclud@446: string secondLine = iSensors[iNextSensorToDisplay].iSecondLine; StephaneLenclud@445: if (aDisplayTime) StephaneLenclud@445: { StephaneLenclud@445: //Add enough spaces StephaneLenclud@445: while (secondLine.Length + time.Length < KMaxCharacterPerLine) StephaneLenclud@445: { StephaneLenclud@445: secondLine += " "; StephaneLenclud@445: } StephaneLenclud@445: secondLine += time; StephaneLenclud@445: } StephaneLenclud@445: //Display current sensor on our FrontView display StephaneLenclud@446: SetText(iSensors[iNextSensorToDisplay].iFirstLine, secondLine); StephaneLenclud@445: iTickCounter++; StephaneLenclud@445: if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick StephaneLenclud@445: { StephaneLenclud@445: iTickCounter = 0; StephaneLenclud@445: iNextSensorToDisplay++; StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@446: if (iNextSensorToDisplay == iSensors.Count) StephaneLenclud@445: { StephaneLenclud@445: //Go back to first sensor StephaneLenclud@445: iNextSensorToDisplay = 0; StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public bool Contains(ISensor sensor) StephaneLenclud@445: { StephaneLenclud@446: foreach (SensorSharpDisplay icon in iSensors) StephaneLenclud@445: if (icon.Sensor == sensor) StephaneLenclud@445: return true; StephaneLenclud@445: return false; StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Add(ISensor sensor, bool balloonTip) StephaneLenclud@445: { StephaneLenclud@445: if (Contains(sensor)) StephaneLenclud@445: { StephaneLenclud@445: return; StephaneLenclud@445: } StephaneLenclud@445: else StephaneLenclud@445: { StephaneLenclud@445: //SL: StephaneLenclud@446: iSensors.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager)); StephaneLenclud@445: //UpdateMainIconVisibilty(); StephaneLenclud@445: settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true); StephaneLenclud@445: iNextSensorToDisplay = 0; StephaneLenclud@446: if (iSensors.Count == 1) StephaneLenclud@445: { StephaneLenclud@445: //Just added first sensor in FrontView, unable FrontView plug-in mode StephaneLenclud@445: Init(); StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Remove(ISensor sensor) StephaneLenclud@445: { StephaneLenclud@445: Remove(sensor, true); StephaneLenclud@445: iNextSensorToDisplay = 0; StephaneLenclud@446: if (iSensors.Count == 0) StephaneLenclud@445: { StephaneLenclud@445: //No sensor to display in FrontView, just disable FrontView plug-in mode StephaneLenclud@445: Uninit(); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: private void Remove(ISensor sensor, bool deleteConfig) StephaneLenclud@445: { StephaneLenclud@445: if (deleteConfig) StephaneLenclud@445: { StephaneLenclud@445: settings.Remove( StephaneLenclud@445: new Identifier(sensor.Identifier, "SharpDisplay").ToString()); StephaneLenclud@445: } StephaneLenclud@445: SensorSharpDisplay instance = null; StephaneLenclud@446: foreach (SensorSharpDisplay icon in iSensors) StephaneLenclud@445: if (icon.Sensor == sensor) StephaneLenclud@445: instance = icon; StephaneLenclud@445: if (instance != null) StephaneLenclud@445: { StephaneLenclud@446: iSensors.Remove(instance); StephaneLenclud@445: //UpdateMainIconVisibilty(); StephaneLenclud@445: instance.Dispose(); StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: StephaneLenclud@445: StephaneLenclud@445: private void UpdateMainIconVisibilty() StephaneLenclud@445: { StephaneLenclud@445: /* StephaneLenclud@445: if (mainIconEnabled) StephaneLenclud@445: { StephaneLenclud@445: mainIcon.Visible = list.Count == 0; StephaneLenclud@445: } StephaneLenclud@445: else StephaneLenclud@445: { StephaneLenclud@445: mainIcon.Visible = false; StephaneLenclud@445: } StephaneLenclud@445: */ StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Init() StephaneLenclud@445: { StephaneLenclud@445: //iServer.SendMessage("init:"); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Uninit() StephaneLenclud@445: { StephaneLenclud@445: //iServer.SendMessage("uninit:"); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void SetText(string aUpperLine, string aLowerLine) StephaneLenclud@445: { StephaneLenclud@445: //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine); StephaneLenclud@445: iTextFieldTop.Text = aUpperLine; StephaneLenclud@445: iTextFieldBottom.Text = aLowerLine; StephaneLenclud@447: iClient.SetFields(iTextFields); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: public void Quit() StephaneLenclud@445: { StephaneLenclud@445: //iServer.SendMessage("quit:"); StephaneLenclud@445: } StephaneLenclud@445: StephaneLenclud@445: /* StephaneLenclud@445: public bool IsMainIconEnabled StephaneLenclud@445: { StephaneLenclud@445: get { return mainIconEnabled; } StephaneLenclud@445: set StephaneLenclud@445: { StephaneLenclud@445: if (mainIconEnabled != value) StephaneLenclud@445: { StephaneLenclud@445: mainIconEnabled = value; StephaneLenclud@445: UpdateMainIconVisibilty(); StephaneLenclud@445: } StephaneLenclud@445: } StephaneLenclud@445: }*/ StephaneLenclud@445: StephaneLenclud@445: StephaneLenclud@445: } StephaneLenclud@445: }