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