GUI/SharpDisplay.cs
author Stephane Lenclud
Sat, 30 Jan 2016 23:01:51 +0100
branchMiniDisplay
changeset 454 f84878f52cd9
parent 452 79a3f5946a87
permissions -rw-r--r--
Disabling Nuvoton NCT6791D because of fan full speed bug on Asus Z97 WS.
     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 System.ServiceModel;
    22 using SharpLib.Display;
    23 
    24 
    25 namespace OpenHardwareMonitor.GUI
    26 {
    27     public class SharpDisplay : IDisposable
    28     {
    29         private IComputer computer;
    30         private PersistentSettings settings;
    31         private UnitManager unitManager;
    32         private List<SensorSharpDisplay> iSensors = new List<SensorSharpDisplay>();
    33         private Client iClient;
    34         TextField iTextFieldTop;
    35         TextField iTextFieldBottom;
    36         TextField iTextFieldTopRight;
    37         TextField iTextFieldBottomRight;
    38 
    39         DataField[] iTextFields;
    40 
    41         private int iNextSensorToDisplay = 0;
    42         private int iTickCounter = 0;
    43         bool iPacked = false;
    44 
    45         public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager)
    46         {
    47             this.computer = computer;
    48             this.settings = settings;
    49             this.unitManager = unitManager;
    50             computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    51             computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    52 
    53             //Connect our client
    54             //Instance context is then managed by our client class
    55             iClient = new Client();
    56             iClient.CloseOrderEvent += OnCloseOrder;
    57             //
    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);
    62 			//
    63 			iClient.Open();
    64 			iClient.SetName("Open Hardware Monitor");
    65             iClient.SetPriority(Priorities.SystemMonitor);
    66 
    67             CreateFields();
    68         }
    69 
    70         public void OnCloseOrder()
    71         {            
    72             iClient.Close();
    73         }
    74 
    75 
    76         private void HardwareRemoved(IHardware hardware)
    77         {
    78             hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    79             hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    80             foreach (ISensor sensor in hardware.Sensors)
    81                 SensorRemoved(sensor);
    82             foreach (IHardware subHardware in hardware.SubHardware)
    83                 HardwareRemoved(subHardware);
    84         }
    85 
    86         private void HardwareAdded(IHardware hardware)
    87         {
    88             foreach (ISensor sensor in hardware.Sensors)
    89                 SensorAdded(sensor);
    90             hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    91             hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    92             foreach (IHardware subHardware in hardware.SubHardware)
    93                 HardwareAdded(subHardware);
    94         }
    95 
    96         private void SensorAdded(ISensor sensor)
    97         {
    98             if (settings.GetValue(new Identifier(sensor.Identifier,
    99               "SharpDisplay").ToString(), false))
   100                 Add(sensor, false);
   101         }
   102 
   103         private void SensorRemoved(ISensor sensor)
   104         {
   105             if (Contains(sensor))
   106                 Remove(sensor, false);
   107         }
   108 
   109         public void Dispose()
   110         {
   111             foreach (SensorSharpDisplay icon in iSensors)
   112                 icon.Dispose();
   113 
   114             Quit();            
   115             //iServer.Stop();
   116             iClient.Close();
   117 
   118         }
   119 
   120 		private void CreateFields()
   121 		{
   122 			if (iPacked)
   123             {
   124                 //We just switched to packed mode                    
   125                 //Make sure our layout is proper
   126                 TableLayout layout = new TableLayout(2, 2);
   127                 iClient.SetLayout(layout);
   128 				iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom, iTextFieldTopRight, iTextFieldBottomRight };
   129 				iClient.CreateFields(iTextFields);
   130             }
   131             else
   132             {
   133                 //Non packed mode
   134                 TableLayout layout = new TableLayout(1, 2);
   135                 iClient.SetLayout(layout);
   136 				iTextFields = new DataField[] { iTextFieldTop, iTextFieldBottom };
   137 				iClient.CreateFields(iTextFields);
   138             }
   139 		}
   140 
   141         public void Redraw(bool aPacked, bool aDisplayTime)
   142         {
   143             const int KNumberOfTickBeforeSwitch = 4;
   144             const int KMaxCharacterPerLine = 16;
   145             int count = 0;
   146 
   147             //string time = DateTime.Now.ToShortTimeString();
   148             string time = DateTime.Now.ToLongTimeString();
   149 
   150             if (iSensors.Count > 0)
   151             {
   152                 if (iPacked != aPacked)
   153                 {
   154                     //Remember mode
   155                     iPacked = aPacked;
   156 
   157 					CreateFields();
   158 
   159                 }
   160             }
   161 
   162 
   163             //Update all sensors from our front view
   164             foreach (SensorSharpDisplay sensor in iSensors)
   165             {
   166                 count++;
   167                 sensor.Update();
   168 
   169                 if (aDisplayTime && count == 1)
   170                 {
   171                     //First slot is taken by time display
   172                     count++;
   173                     iTextFieldTop.Text = time;
   174                     iClient.SetField(iTextFieldTop);
   175                 }
   176 
   177                 if (aPacked)
   178                 {
   179                     //Build strings for packed mode
   180                     string packedText = "";
   181                     packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
   182                     if (count == 1)
   183                     {
   184                         iTextFieldTop.Text = packedText;
   185 						iClient.SetField(iTextFieldTop);
   186                     }
   187                     else if (count == 2)
   188                     {
   189                         iTextFieldBottom.Text = packedText;
   190 						iClient.SetField(iTextFieldBottom);
   191                     }
   192                     else if (count == 3)
   193                     {
   194                         iTextFieldTopRight.Text = packedText;
   195 						iClient.SetField(iTextFieldTopRight);
   196                     }
   197                     else if (count == 4)
   198                     {
   199                         iTextFieldBottomRight.Text = packedText;
   200 						iClient.SetField(iTextFieldBottomRight);
   201                     }
   202                 }
   203             }
   204 
   205             //Alternate between sensors 
   206             if (iSensors.Count > 0)
   207             {
   208                 if (aPacked)
   209                 {
   210                     //Review that stuff cause as it is it's probably useless
   211                     //string packedLine = "";
   212                     iTickCounter++;
   213                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   214                     {
   215                         iTickCounter = 0;
   216                         if (iNextSensorToDisplay == 1)
   217                         {
   218                             iNextSensorToDisplay = 0;
   219                         }
   220                         else
   221                         {
   222                             iNextSensorToDisplay = 1;
   223                         }
   224                     }
   225                 }
   226                 else
   227                 {
   228                     string secondLine = iSensors[iNextSensorToDisplay].iSecondLine;
   229                     if (aDisplayTime)
   230                     {
   231                         //Add enough spaces
   232                         while (secondLine.Length + time.Length < KMaxCharacterPerLine)
   233                         {
   234                             secondLine += " ";
   235                         }
   236                         secondLine += time;
   237                     }
   238                     //Display current sensor on our FrontView display
   239                     SetText(iSensors[iNextSensorToDisplay].iFirstLine, secondLine);
   240                     iTickCounter++;
   241                     if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   242                     {
   243                         iTickCounter = 0;
   244                         iNextSensorToDisplay++;
   245                     }
   246                 }
   247             }
   248 
   249             if (iNextSensorToDisplay == iSensors.Count)
   250             {
   251                 //Go back to first sensor
   252                 iNextSensorToDisplay = 0;
   253             }
   254 
   255 
   256         }
   257 
   258         public bool Contains(ISensor sensor)
   259         {
   260             foreach (SensorSharpDisplay icon in iSensors)
   261                 if (icon.Sensor == sensor)
   262                     return true;
   263             return false;
   264         }
   265 
   266         public void Add(ISensor sensor, bool balloonTip)
   267         {
   268             if (Contains(sensor))
   269             {
   270                 return;
   271             }
   272             else
   273             {
   274                 //SL:
   275                 iSensors.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
   276                 //UpdateMainIconVisibilty();
   277                 settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
   278                 iNextSensorToDisplay = 0;
   279                 if (iSensors.Count == 1)
   280                 {
   281                     //Just added first sensor in FrontView, unable FrontView plug-in mode
   282                     Init();
   283                 }
   284             }
   285 
   286         }
   287 
   288         public void Remove(ISensor sensor)
   289         {
   290             Remove(sensor, true);
   291             iNextSensorToDisplay = 0;
   292             if (iSensors.Count == 0)
   293             {
   294                 //No sensor to display in FrontView, just disable FrontView plug-in mode
   295                 Uninit();
   296             }
   297 
   298         }
   299 
   300         private void Remove(ISensor sensor, bool deleteConfig)
   301         {
   302             if (deleteConfig)
   303             {
   304                 settings.Remove(
   305                   new Identifier(sensor.Identifier, "SharpDisplay").ToString());
   306             }
   307             SensorSharpDisplay instance = null;
   308             foreach (SensorSharpDisplay icon in iSensors)
   309                 if (icon.Sensor == sensor)
   310                     instance = icon;
   311             if (instance != null)
   312             {
   313                 iSensors.Remove(instance);
   314                 //UpdateMainIconVisibilty();
   315                 instance.Dispose();
   316             }
   317         }
   318 
   319 
   320 
   321         private void UpdateMainIconVisibilty()
   322         {
   323             /*
   324             if (mainIconEnabled)
   325             {
   326                 mainIcon.Visible = list.Count == 0;
   327             }
   328             else
   329             {
   330                 mainIcon.Visible = false;
   331             }
   332              */
   333         }
   334 
   335         public void Init()
   336         {
   337             //iServer.SendMessage("init:");
   338         }
   339 
   340         public void Uninit()
   341         {
   342             //iServer.SendMessage("uninit:");
   343         }
   344 
   345         public void SetText(string aUpperLine, string aLowerLine)
   346         {
   347             //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   348             iTextFieldTop.Text = aUpperLine;
   349             iTextFieldBottom.Text = aLowerLine;
   350             iClient.SetFields(iTextFields);
   351         }
   352 
   353         public void Quit()
   354         {
   355             //iServer.SendMessage("quit:");
   356         }
   357 
   358         /*
   359         public bool IsMainIconEnabled
   360         {
   361             get { return mainIconEnabled; }
   362             set
   363             {
   364                 if (mainIconEnabled != value)
   365                 {
   366                     mainIconEnabled = value;
   367                     UpdateMainIconVisibilty();
   368                 }
   369             }
   370         }*/
   371 
   372 
   373     }
   374 }