Switched to SharpLibDisplay.
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");
68 private void HardwareRemoved(IHardware hardware)
70 hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
71 hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
72 foreach (ISensor sensor in hardware.Sensors)
73 SensorRemoved(sensor);
74 foreach (IHardware subHardware in hardware.SubHardware)
75 HardwareRemoved(subHardware);
78 private void HardwareAdded(IHardware hardware)
80 foreach (ISensor sensor in hardware.Sensors)
82 hardware.SensorAdded += new SensorEventHandler(SensorAdded);
83 hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
84 foreach (IHardware subHardware in hardware.SubHardware)
85 HardwareAdded(subHardware);
88 private void SensorAdded(ISensor sensor)
90 if (settings.GetValue(new Identifier(sensor.Identifier,
91 "SharpDisplay").ToString(), false))
95 private void SensorRemoved(ISensor sensor)
98 Remove(sensor, false);
101 public void Dispose()
103 foreach (SensorSharpDisplay icon in iSensors)
112 private void CreateFields()
116 //We just switched to packed mode
117 //Make sure our layout is proper
118 TableLayout layout = new TableLayout(2, 2);
119 iClient.SetLayout(layout);
120 iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom, iTextFieldTopRight, iTextFieldBottomRight };
121 iClient.CreateFields(iTextFields);
126 TableLayout layout = new TableLayout(1, 2);
127 iClient.SetLayout(layout);
128 iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom };
129 iClient.CreateFields(iTextFields);
133 public void Redraw(bool aPacked, bool aDisplayTime)
135 const int KNumberOfTickBeforeSwitch = 4;
136 const int KMaxCharacterPerLine = 16;
137 string packedFirstLine = ""; //We have 16 chars per line on our VFD
138 string packedSecondLine = "";
141 //string time = DateTime.Now.ToShortTimeString();
142 string time = DateTime.Now.ToLongTimeString();
144 if (iSensors.Count > 0)
146 if (iPacked != aPacked)
157 //Update all sensors from our front view
158 foreach (SensorSharpDisplay sensor in iSensors)
163 if (aDisplayTime && count == 1)
165 //First slot is taken by time display
167 iTextFieldTop.Text = time;
168 iClient.SetField(iTextFieldTop);
173 //Build strings for packed mode
174 string packedText = "";
175 packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
178 iTextFieldTop.Text = packedText;
179 iClient.SetField(iTextFieldTop);
183 iTextFieldBottom.Text = packedText;
184 iClient.SetField(iTextFieldBottom);
188 iTextFieldTopRight.Text = packedText;
189 iClient.SetField(iTextFieldTopRight);
193 iTextFieldBottomRight.Text = packedText;
194 iClient.SetField(iTextFieldBottomRight);
199 //Alternate between sensors
200 if (iSensors.Count > 0)
204 //Review that stuff cause as it is it's probably useless
205 //string packedLine = "";
207 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
210 if (iNextSensorToDisplay == 1)
212 iNextSensorToDisplay = 0;
216 iNextSensorToDisplay = 1;
222 string secondLine = iSensors[iNextSensorToDisplay].iSecondLine;
226 while (secondLine.Length + time.Length < KMaxCharacterPerLine)
232 //Display current sensor on our FrontView display
233 SetText(iSensors[iNextSensorToDisplay].iFirstLine, secondLine);
235 if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
238 iNextSensorToDisplay++;
243 if (iNextSensorToDisplay == iSensors.Count)
245 //Go back to first sensor
246 iNextSensorToDisplay = 0;
252 public bool Contains(ISensor sensor)
254 foreach (SensorSharpDisplay icon in iSensors)
255 if (icon.Sensor == sensor)
260 public void Add(ISensor sensor, bool balloonTip)
262 if (Contains(sensor))
269 iSensors.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
270 //UpdateMainIconVisibilty();
271 settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
272 iNextSensorToDisplay = 0;
273 if (iSensors.Count == 1)
275 //Just added first sensor in FrontView, unable FrontView plug-in mode
282 public void Remove(ISensor sensor)
284 Remove(sensor, true);
285 iNextSensorToDisplay = 0;
286 if (iSensors.Count == 0)
288 //No sensor to display in FrontView, just disable FrontView plug-in mode
294 private void Remove(ISensor sensor, bool deleteConfig)
299 new Identifier(sensor.Identifier, "SharpDisplay").ToString());
301 SensorSharpDisplay instance = null;
302 foreach (SensorSharpDisplay icon in iSensors)
303 if (icon.Sensor == sensor)
305 if (instance != null)
307 iSensors.Remove(instance);
308 //UpdateMainIconVisibilty();
315 private void UpdateMainIconVisibilty()
320 mainIcon.Visible = list.Count == 0;
324 mainIcon.Visible = false;
331 //iServer.SendMessage("init:");
336 //iServer.SendMessage("uninit:");
339 public void SetText(string aUpperLine, string aLowerLine)
341 //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
342 iTextFieldTop.Text = aUpperLine;
343 iTextFieldBottom.Text = aLowerLine;
344 iClient.SetFields(iTextFields);
350 //iServer.SendMessage("quit:");
354 public bool IsMainIconEnabled
356 get { return mainIconEnabled; }
359 if (mainIconEnabled != value)
361 mainIconEnabled = value;
362 UpdateMainIconVisibilty();