SharpLibDisplay update and priority support.
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/.
7 Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
12 using System.Collections.Generic;
15 using System.Diagnostics;
16 using System.Windows.Forms;
18 using OpenHardwareMonitor.Hardware;
19 using OpenHardwareMonitor.Utilities;
20 using System.Runtime.InteropServices;
22 using System.ServiceModel;
23 using SharpLib.Display;
26 namespace OpenHardwareMonitor.GUI
28 public class SharpDisplay : IDisposable
30 private IComputer computer;
31 private PersistentSettings settings;
32 private UnitManager unitManager;
33 private List<SensorSharpDisplay> iSensors = new List<SensorSharpDisplay>();
34 private Client iClient;
35 TextField iTextFieldTop;
36 TextField iTextFieldBottom;
37 TextField iTextFieldTopRight;
38 TextField iTextFieldBottomRight;
40 DataField[] iTextFields;
42 private int iNextSensorToDisplay = 0;
43 private int iTickCounter = 0;
46 public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager)
48 this.computer = computer;
49 this.settings = settings;
50 this.unitManager = unitManager;
51 computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
52 computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
55 //Instance context is then managed by our client class
56 iClient = new Client();
58 iTextFieldTop = new TextField("", ContentAlignment.MiddleLeft,0,0);
59 iTextFieldBottom = new TextField("", ContentAlignment.MiddleLeft, 0, 1);
60 iTextFieldTopRight = new TextField("", ContentAlignment.MiddleRight,1,0);
61 iTextFieldBottomRight = new TextField("", ContentAlignment.MiddleRight,1,1);
64 iClient.SetName("Open Hardware Monitor");
65 iClient.SetPriority(Priorities.SystemMonitor);
70 private void HardwareRemoved(IHardware hardware)
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);
80 private void HardwareAdded(IHardware hardware)
82 foreach (ISensor sensor in hardware.Sensors)
84 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
85 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
86 foreach (IHardware subHardware in hardware.SubHardware)
87 HardwareAdded(subHardware);
90 private void SensorAdded(ISensor sensor)
92 if (settings.GetValue(new Identifier(sensor.Identifier,
93 "SharpDisplay").ToString(), false))
97 private void SensorRemoved(ISensor sensor)
100 Remove(sensor, false);
103 public void Dispose()
105 foreach (SensorSharpDisplay icon in iSensors)
114 private void CreateFields()
118 //We just switched to packed mode
119 //Make sure our layout is proper
120 TableLayout layout = new TableLayout(2, 2);
121 iClient.SetLayout(layout);
122 iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom, iTextFieldTopRight, iTextFieldBottomRight };
123 iClient.CreateFields(iTextFields);
128 TableLayout layout = new TableLayout(1, 2);
129 iClient.SetLayout(layout);
130 iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom };
131 iClient.CreateFields(iTextFields);
135 public void Redraw(bool aPacked, bool aDisplayTime)
137 const int KNumberOfTickBeforeSwitch = 4;
138 const int KMaxCharacterPerLine = 16;
139 string packedFirstLine = ""; //We have 16 chars per line on our VFD
140 string packedSecondLine = "";
143 //string time = DateTime.Now.ToShortTimeString();
144 string time = DateTime.Now.ToLongTimeString();
146 if (iSensors.Count > 0)
148 if (iPacked != aPacked)
159 //Update all sensors from our front view
160 foreach (SensorSharpDisplay sensor in iSensors)
165 if (aDisplayTime && count == 1)
167 //First slot is taken by time display
169 iTextFieldTop.Text = time;
170 iClient.SetField(iTextFieldTop);
175 //Build strings for packed mode
176 string packedText = "";
177 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
180 iTextFieldTop.Text = packedText;
181 iClient.SetField(iTextFieldTop);
185 iTextFieldBottom.Text = packedText;
186 iClient.SetField(iTextFieldBottom);
190 iTextFieldTopRight.Text = packedText;
191 iClient.SetField(iTextFieldTopRight);
195 iTextFieldBottomRight.Text = packedText;
196 iClient.SetField(iTextFieldBottomRight);
201 //Alternate between sensors
202 if (iSensors.Count > 0)
206 //Review that stuff cause as it is it's probably useless
207 //string packedLine = "";
209 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
212 if (iNextSensorToDisplay == 1)
214 iNextSensorToDisplay = 0;
218 iNextSensorToDisplay = 1;
224 string secondLine = iSensors[iNextSensorToDisplay].iSecondLine;
228 while (secondLine.Length + time.Length < KMaxCharacterPerLine)
234 //Display current sensor on our FrontView display
235 SetText(iSensors[iNextSensorToDisplay].iFirstLine, secondLine);
237 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
240 iNextSensorToDisplay++;
245 if (iNextSensorToDisplay == iSensors.Count)
247 //Go back to first sensor
248 iNextSensorToDisplay = 0;
254 public bool Contains(ISensor sensor)
256 foreach (SensorSharpDisplay icon in iSensors)
257 if (icon.Sensor == sensor)
262 public void Add(ISensor sensor, bool balloonTip)
264 if (Contains(sensor))
271 iSensors.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
272 //UpdateMainIconVisibilty();
273 settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
274 iNextSensorToDisplay = 0;
275 if (iSensors.Count == 1)
277 //Just added first sensor in FrontView, unable FrontView plug-in mode
284 public void Remove(ISensor sensor)
286 Remove(sensor, true);
287 iNextSensorToDisplay = 0;
288 if (iSensors.Count == 0)
290 //No sensor to display in FrontView, just disable FrontView plug-in mode
296 private void Remove(ISensor sensor, bool deleteConfig)
301 new Identifier(sensor.Identifier, "SharpDisplay").ToString());
303 SensorSharpDisplay instance = null;
304 foreach (SensorSharpDisplay icon in iSensors)
305 if (icon.Sensor == sensor)
307 if (instance != null)
309 iSensors.Remove(instance);
310 //UpdateMainIconVisibilty();
317 private void UpdateMainIconVisibilty()
322 mainIcon.Visible = list.Count == 0;
326 mainIcon.Visible = false;
333 //iServer.SendMessage("init:");
338 //iServer.SendMessage("uninit:");
341 public void SetText(string aUpperLine, string aLowerLine)
343 //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
344 iTextFieldTop.Text = aUpperLine;
345 iTextFieldBottom.Text = aLowerLine;
346 iClient.SetFields(iTextFields);
352 //iServer.SendMessage("quit:");
356 public bool IsMainIconEnabled
358 get { return mainIconEnabled; }
361 if (mainIconEnabled != value)
363 mainIconEnabled = value;
364 UpdateMainIconVisibilty();