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