GUI/SharpDisplay.cs
branchMiniDisplay
changeset 445 fe4c711fd7f8
child 446 0cb7b9f6a6f8
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/GUI/SharpDisplay.cs	Sun Sep 21 21:55:36 2014 +0200
     1.3 @@ -0,0 +1,353 @@
     1.4 +/*
     1.5 + 
     1.6 +  This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +  License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +  file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.9 + 
    1.10 +  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
    1.11 +	
    1.12 +*/
    1.13 +
    1.14 +using System;
    1.15 +using System.Collections.Generic;
    1.16 +using System.Drawing;
    1.17 +using System.Text;
    1.18 +using System.Diagnostics;
    1.19 +using System.Windows.Forms;
    1.20 +using System.Windows;
    1.21 +using OpenHardwareMonitor.Hardware;
    1.22 +using OpenHardwareMonitor.Utilities;
    1.23 +using System.Runtime.InteropServices;
    1.24 +using UacHelpers;
    1.25 +using System.ServiceModel;
    1.26 +using SharpDisplay;
    1.27 +
    1.28 +
    1.29 +namespace OpenHardwareMonitor.GUI
    1.30 +{
    1.31 +    public class SharpDisplay : ICallback, IDisposable
    1.32 +    {
    1.33 +        private IComputer computer;
    1.34 +        private PersistentSettings settings;
    1.35 +        private UnitManager unitManager;
    1.36 +        private List<SensorSharpDisplay> list = new List<SensorSharpDisplay>();
    1.37 +        private global::SharpDisplay.Client iClient;
    1.38 +        TextField iTextFieldTop;
    1.39 +        TextField iTextFieldBottom;
    1.40 +        TextField[] iTextFields;
    1.41 +
    1.42 +        private int iNextSensorToDisplay = 0;
    1.43 +        private int iTickCounter = 0;
    1.44 +
    1.45 +
    1.46 +        public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager)
    1.47 +        {
    1.48 +            this.computer = computer;
    1.49 +            this.settings = settings;
    1.50 +            this.unitManager = unitManager;
    1.51 +            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
    1.52 +            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
    1.53 +
    1.54 +            //Connect our client
    1.55 +            //Instance context is then managed by our client class
    1.56 +            iClient = new global::SharpDisplay.Client(this);
    1.57 +            //
    1.58 +            iTextFieldTop = new TextField(0);
    1.59 +            iTextFieldBottom = new TextField(1);
    1.60 +            iTextFields = new TextField[] { iTextFieldTop, iTextFieldBottom };
    1.61 +
    1.62 +        }
    1.63 +
    1.64 +        //From ICallback
    1.65 +        public void OnConnected()
    1.66 +        {
    1.67 +            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
    1.68 +            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
    1.69 +            //MessageBox.Show("OnConnected()", "Client");
    1.70 +        }
    1.71 +
    1.72 +        //From ICallback
    1.73 +        public void OnCloseOrder()
    1.74 +        {
    1.75 +            iClient.Close();
    1.76 +        }
    1.77 +
    1.78 +
    1.79 +        private void HardwareRemoved(IHardware hardware)
    1.80 +        {
    1.81 +            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
    1.82 +            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
    1.83 +            foreach (ISensor sensor in hardware.Sensors)
    1.84 +                SensorRemoved(sensor);
    1.85 +            foreach (IHardware subHardware in hardware.SubHardware)
    1.86 +                HardwareRemoved(subHardware);
    1.87 +        }
    1.88 +
    1.89 +        private void HardwareAdded(IHardware hardware)
    1.90 +        {
    1.91 +            foreach (ISensor sensor in hardware.Sensors)
    1.92 +                SensorAdded(sensor);
    1.93 +            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
    1.94 +            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
    1.95 +            foreach (IHardware subHardware in hardware.SubHardware)
    1.96 +                HardwareAdded(subHardware);
    1.97 +        }
    1.98 +
    1.99 +        private void SensorAdded(ISensor sensor)
   1.100 +        {
   1.101 +            if (settings.GetValue(new Identifier(sensor.Identifier,
   1.102 +              "SharpDisplay").ToString(), false))
   1.103 +                Add(sensor, false);
   1.104 +        }
   1.105 +
   1.106 +        private void SensorRemoved(ISensor sensor)
   1.107 +        {
   1.108 +            if (Contains(sensor))
   1.109 +                Remove(sensor, false);
   1.110 +        }
   1.111 +
   1.112 +        public void Dispose()
   1.113 +        {
   1.114 +            foreach (SensorSharpDisplay icon in list)
   1.115 +                icon.Dispose();
   1.116 +
   1.117 +            Quit();            
   1.118 +            //iServer.Stop();
   1.119 +            iClient.Close();
   1.120 +
   1.121 +        }
   1.122 +
   1.123 +        public void Redraw(bool aPacked, bool aDisplayTime)
   1.124 +        {
   1.125 +            const int KNumberOfTickBeforeSwitch = 4;
   1.126 +            const int KMaxCharacterPerLine = 16;
   1.127 +            string packedFirstLine = ""; //We have 16 chars per line on our VFD
   1.128 +            string packedSecondLine = "";
   1.129 +            int count = 0;
   1.130 +
   1.131 +            string time = DateTime.Now.ToShortTimeString();
   1.132 +
   1.133 +            //Update all sensors from our front view
   1.134 +            foreach (SensorSharpDisplay sensor in list)
   1.135 +            {
   1.136 +                count++;
   1.137 +                sensor.Update();
   1.138 +
   1.139 +                if (aDisplayTime && count == 1)
   1.140 +                {
   1.141 +                    //First slot is take by time display
   1.142 +                    count++;
   1.143 +                    packedFirstLine = time + " ";
   1.144 +                }
   1.145 +
   1.146 +                if (aPacked)
   1.147 +                {
   1.148 +                    //Build strings for packed mode
   1.149 +                    string packedText = "";
   1.150 +                    packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
   1.151 +                    if (count == 1)
   1.152 +                    {
   1.153 +                        packedFirstLine = packedText + " "; //Minimum one space to separate sensors on the same line
   1.154 +                    }
   1.155 +                    else if (count == 2)
   1.156 +                    {
   1.157 +                        //Add enough spaces to align to right hand side
   1.158 +                        while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
   1.159 +                        {
   1.160 +                            packedFirstLine += " ";
   1.161 +                        }
   1.162 +                        packedFirstLine += packedText;
   1.163 +                    }
   1.164 +                    else if (count == 3)
   1.165 +                    {
   1.166 +                        packedSecondLine = packedText + " "; //Minimum one space to separate sensors on the same line
   1.167 +                    }
   1.168 +                    else if (count == 4)
   1.169 +                    {
   1.170 +                        //Add enough spaces to align to right hand side
   1.171 +                        while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
   1.172 +                        {
   1.173 +                            packedSecondLine += " ";
   1.174 +                        }
   1.175 +                        packedSecondLine += packedText;
   1.176 +                    }
   1.177 +                }
   1.178 +                //SetText(sensor.iFirstLine, sensor.iSecondLine);
   1.179 +            }
   1.180 +
   1.181 +            //Alternate between sensors 
   1.182 +            if (list.Count > 0)
   1.183 +            {
   1.184 +                if (aPacked)
   1.185 +                {
   1.186 +                    //string packedLine = "";
   1.187 +                    iTickCounter++;
   1.188 +                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   1.189 +                    {
   1.190 +                        iTickCounter = 0;
   1.191 +                        if (iNextSensorToDisplay == 1)
   1.192 +                        {
   1.193 +                            iNextSensorToDisplay = 0;
   1.194 +                        }
   1.195 +                        else
   1.196 +                        {
   1.197 +                            iNextSensorToDisplay = 1;
   1.198 +                        }
   1.199 +                    }
   1.200 +
   1.201 +                    //TODO: Do something like that to cycle lines if ever we want to
   1.202 +                    //SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
   1.203 +
   1.204 +                    //Display packed sensors on our FrontView display
   1.205 +                    SetText(packedFirstLine, packedSecondLine);
   1.206 +                }
   1.207 +                else
   1.208 +                {
   1.209 +                    string secondLine = list[iNextSensorToDisplay].iSecondLine;
   1.210 +                    if (aDisplayTime)
   1.211 +                    {
   1.212 +                        //Add enough spaces
   1.213 +                        while (secondLine.Length + time.Length < KMaxCharacterPerLine)
   1.214 +                        {
   1.215 +                            secondLine += " ";
   1.216 +                        }
   1.217 +                        secondLine += time;
   1.218 +                    }
   1.219 +                    //Display current sensor on our FrontView display
   1.220 +                    SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
   1.221 +                    iTickCounter++;
   1.222 +                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
   1.223 +                    {
   1.224 +                        iTickCounter = 0;
   1.225 +                        iNextSensorToDisplay++;
   1.226 +                    }
   1.227 +                }
   1.228 +            }
   1.229 +
   1.230 +            if (iNextSensorToDisplay == list.Count)
   1.231 +            {
   1.232 +                //Go back to first sensor
   1.233 +                iNextSensorToDisplay = 0;
   1.234 +            }
   1.235 +
   1.236 +
   1.237 +        }
   1.238 +
   1.239 +        public bool Contains(ISensor sensor)
   1.240 +        {
   1.241 +            foreach (SensorSharpDisplay icon in list)
   1.242 +                if (icon.Sensor == sensor)
   1.243 +                    return true;
   1.244 +            return false;
   1.245 +        }
   1.246 +
   1.247 +        public void Add(ISensor sensor, bool balloonTip)
   1.248 +        {
   1.249 +            if (Contains(sensor))
   1.250 +            {
   1.251 +                return;
   1.252 +            }
   1.253 +            else
   1.254 +            {
   1.255 +                //SL:
   1.256 +                list.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
   1.257 +                //UpdateMainIconVisibilty();
   1.258 +                settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
   1.259 +                iNextSensorToDisplay = 0;
   1.260 +                if (list.Count == 1)
   1.261 +                {
   1.262 +                    //Just added first sensor in FrontView, unable FrontView plug-in mode
   1.263 +                    Init();
   1.264 +                }
   1.265 +            }
   1.266 +
   1.267 +        }
   1.268 +
   1.269 +        public void Remove(ISensor sensor)
   1.270 +        {
   1.271 +            Remove(sensor, true);
   1.272 +            iNextSensorToDisplay = 0;
   1.273 +            if (list.Count == 0)
   1.274 +            {
   1.275 +                //No sensor to display in FrontView, just disable FrontView plug-in mode
   1.276 +                Uninit();
   1.277 +            }
   1.278 +
   1.279 +        }
   1.280 +
   1.281 +        private void Remove(ISensor sensor, bool deleteConfig)
   1.282 +        {
   1.283 +            if (deleteConfig)
   1.284 +            {
   1.285 +                settings.Remove(
   1.286 +                  new Identifier(sensor.Identifier, "SharpDisplay").ToString());
   1.287 +            }
   1.288 +            SensorSharpDisplay instance = null;
   1.289 +            foreach (SensorSharpDisplay icon in list)
   1.290 +                if (icon.Sensor == sensor)
   1.291 +                    instance = icon;
   1.292 +            if (instance != null)
   1.293 +            {
   1.294 +                list.Remove(instance);
   1.295 +                //UpdateMainIconVisibilty();
   1.296 +                instance.Dispose();
   1.297 +            }
   1.298 +        }
   1.299 +
   1.300 +
   1.301 +
   1.302 +        private void UpdateMainIconVisibilty()
   1.303 +        {
   1.304 +            /*
   1.305 +            if (mainIconEnabled)
   1.306 +            {
   1.307 +                mainIcon.Visible = list.Count == 0;
   1.308 +            }
   1.309 +            else
   1.310 +            {
   1.311 +                mainIcon.Visible = false;
   1.312 +            }
   1.313 +             */
   1.314 +        }
   1.315 +
   1.316 +        public void Init()
   1.317 +        {
   1.318 +            //iServer.SendMessage("init:");
   1.319 +        }
   1.320 +
   1.321 +        public void Uninit()
   1.322 +        {
   1.323 +            //iServer.SendMessage("uninit:");
   1.324 +        }
   1.325 +
   1.326 +        public void SetText(string aUpperLine, string aLowerLine)
   1.327 +        {
   1.328 +            //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
   1.329 +            iTextFieldTop.Text = aUpperLine;
   1.330 +            iTextFieldBottom.Text = aLowerLine;
   1.331 +            iClient.SetTexts(iTextFields);
   1.332 +
   1.333 +        }
   1.334 +
   1.335 +        public void Quit()
   1.336 +        {
   1.337 +            //iServer.SendMessage("quit:");
   1.338 +        }
   1.339 +
   1.340 +        /*
   1.341 +        public bool IsMainIconEnabled
   1.342 +        {
   1.343 +            get { return mainIconEnabled; }
   1.344 +            set
   1.345 +            {
   1.346 +                if (mainIconEnabled != value)
   1.347 +                {
   1.348 +                    mainIconEnabled = value;
   1.349 +                    UpdateMainIconVisibilty();
   1.350 +                }
   1.351 +            }
   1.352 +        }*/
   1.353 +
   1.354 +
   1.355 +    }
   1.356 +}