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