GUI/SharpDisplay.cs
author sl
Sun, 21 Sep 2014 21:55:36 +0200
changeset 403 1d10b3a8a235
child 404 7b7fad708159
permissions -rw-r--r--
Support for SharDisplayManager.
sl@403
     1
/*
sl@403
     2
 
sl@403
     3
  This Source Code Form is subject to the terms of the Mozilla Public
sl@403
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
sl@403
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
sl@403
     6
 
sl@403
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
sl@403
     8
	
sl@403
     9
*/
sl@403
    10
sl@403
    11
using System;
sl@403
    12
using System.Collections.Generic;
sl@403
    13
using System.Drawing;
sl@403
    14
using System.Text;
sl@403
    15
using System.Diagnostics;
sl@403
    16
using System.Windows.Forms;
sl@403
    17
using System.Windows;
sl@403
    18
using OpenHardwareMonitor.Hardware;
sl@403
    19
using OpenHardwareMonitor.Utilities;
sl@403
    20
using System.Runtime.InteropServices;
sl@403
    21
using UacHelpers;
sl@403
    22
using System.ServiceModel;
sl@403
    23
using SharpDisplay;
sl@403
    24
sl@403
    25
sl@403
    26
namespace OpenHardwareMonitor.GUI
sl@403
    27
{
sl@403
    28
    public class SharpDisplay : ICallback, IDisposable
sl@403
    29
    {
sl@403
    30
        private IComputer computer;
sl@403
    31
        private PersistentSettings settings;
sl@403
    32
        private UnitManager unitManager;
sl@403
    33
        private List<SensorSharpDisplay> list = new List<SensorSharpDisplay>();
sl@403
    34
        private global::SharpDisplay.Client iClient;
sl@403
    35
        TextField iTextFieldTop;
sl@403
    36
        TextField iTextFieldBottom;
sl@403
    37
        TextField[] iTextFields;
sl@403
    38
sl@403
    39
        private int iNextSensorToDisplay = 0;
sl@403
    40
        private int iTickCounter = 0;
sl@403
    41
sl@403
    42
sl@403
    43
        public SharpDisplay(IComputer computer, PersistentSettings settings, UnitManager unitManager)
sl@403
    44
        {
sl@403
    45
            this.computer = computer;
sl@403
    46
            this.settings = settings;
sl@403
    47
            this.unitManager = unitManager;
sl@403
    48
            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
sl@403
    49
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
sl@403
    50
sl@403
    51
            //Connect our client
sl@403
    52
            //Instance context is then managed by our client class
sl@403
    53
            iClient = new global::SharpDisplay.Client(this);
sl@403
    54
            //
sl@403
    55
            iTextFieldTop = new TextField(0);
sl@403
    56
            iTextFieldBottom = new TextField(1);
sl@403
    57
            iTextFields = new TextField[] { iTextFieldTop, iTextFieldBottom };
sl@403
    58
sl@403
    59
        }
sl@403
    60
sl@403
    61
        //From ICallback
sl@403
    62
        public void OnConnected()
sl@403
    63
        {
sl@403
    64
            //Debug.Assert(Thread.CurrentThread.IsThreadPoolThread);
sl@403
    65
            //Trace.WriteLine("Callback thread = " + Thread.CurrentThread.ManagedThreadId);
sl@403
    66
            //MessageBox.Show("OnConnected()", "Client");
sl@403
    67
        }
sl@403
    68
sl@403
    69
        //From ICallback
sl@403
    70
        public void OnCloseOrder()
sl@403
    71
        {
sl@403
    72
            iClient.Close();
sl@403
    73
        }
sl@403
    74
sl@403
    75
sl@403
    76
        private void HardwareRemoved(IHardware hardware)
sl@403
    77
        {
sl@403
    78
            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
sl@403
    79
            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
sl@403
    80
            foreach (ISensor sensor in hardware.Sensors)
sl@403
    81
                SensorRemoved(sensor);
sl@403
    82
            foreach (IHardware subHardware in hardware.SubHardware)
sl@403
    83
                HardwareRemoved(subHardware);
sl@403
    84
        }
sl@403
    85
sl@403
    86
        private void HardwareAdded(IHardware hardware)
sl@403
    87
        {
sl@403
    88
            foreach (ISensor sensor in hardware.Sensors)
sl@403
    89
                SensorAdded(sensor);
sl@403
    90
            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
sl@403
    91
            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
sl@403
    92
            foreach (IHardware subHardware in hardware.SubHardware)
sl@403
    93
                HardwareAdded(subHardware);
sl@403
    94
        }
sl@403
    95
sl@403
    96
        private void SensorAdded(ISensor sensor)
sl@403
    97
        {
sl@403
    98
            if (settings.GetValue(new Identifier(sensor.Identifier,
sl@403
    99
              "SharpDisplay").ToString(), false))
sl@403
   100
                Add(sensor, false);
sl@403
   101
        }
sl@403
   102
sl@403
   103
        private void SensorRemoved(ISensor sensor)
sl@403
   104
        {
sl@403
   105
            if (Contains(sensor))
sl@403
   106
                Remove(sensor, false);
sl@403
   107
        }
sl@403
   108
sl@403
   109
        public void Dispose()
sl@403
   110
        {
sl@403
   111
            foreach (SensorSharpDisplay icon in list)
sl@403
   112
                icon.Dispose();
sl@403
   113
sl@403
   114
            Quit();            
sl@403
   115
            //iServer.Stop();
sl@403
   116
            iClient.Close();
sl@403
   117
sl@403
   118
        }
sl@403
   119
sl@403
   120
        public void Redraw(bool aPacked, bool aDisplayTime)
sl@403
   121
        {
sl@403
   122
            const int KNumberOfTickBeforeSwitch = 4;
sl@403
   123
            const int KMaxCharacterPerLine = 16;
sl@403
   124
            string packedFirstLine = ""; //We have 16 chars per line on our VFD
sl@403
   125
            string packedSecondLine = "";
sl@403
   126
            int count = 0;
sl@403
   127
sl@403
   128
            string time = DateTime.Now.ToShortTimeString();
sl@403
   129
sl@403
   130
            //Update all sensors from our front view
sl@403
   131
            foreach (SensorSharpDisplay sensor in list)
sl@403
   132
            {
sl@403
   133
                count++;
sl@403
   134
                sensor.Update();
sl@403
   135
sl@403
   136
                if (aDisplayTime && count == 1)
sl@403
   137
                {
sl@403
   138
                    //First slot is take by time display
sl@403
   139
                    count++;
sl@403
   140
                    packedFirstLine = time + " ";
sl@403
   141
                }
sl@403
   142
sl@403
   143
                if (aPacked)
sl@403
   144
                {
sl@403
   145
                    //Build strings for packed mode
sl@403
   146
                    string packedText = "";
sl@403
   147
                    packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
sl@403
   148
                    if (count == 1)
sl@403
   149
                    {
sl@403
   150
                        packedFirstLine = packedText + " "; //Minimum one space to separate sensors on the same line
sl@403
   151
                    }
sl@403
   152
                    else if (count == 2)
sl@403
   153
                    {
sl@403
   154
                        //Add enough spaces to align to right hand side
sl@403
   155
                        while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
sl@403
   156
                        {
sl@403
   157
                            packedFirstLine += " ";
sl@403
   158
                        }
sl@403
   159
                        packedFirstLine += packedText;
sl@403
   160
                    }
sl@403
   161
                    else if (count == 3)
sl@403
   162
                    {
sl@403
   163
                        packedSecondLine = packedText + " "; //Minimum one space to separate sensors on the same line
sl@403
   164
                    }
sl@403
   165
                    else if (count == 4)
sl@403
   166
                    {
sl@403
   167
                        //Add enough spaces to align to right hand side
sl@403
   168
                        while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
sl@403
   169
                        {
sl@403
   170
                            packedSecondLine += " ";
sl@403
   171
                        }
sl@403
   172
                        packedSecondLine += packedText;
sl@403
   173
                    }
sl@403
   174
                }
sl@403
   175
                //SetText(sensor.iFirstLine, sensor.iSecondLine);
sl@403
   176
            }
sl@403
   177
sl@403
   178
            //Alternate between sensors 
sl@403
   179
            if (list.Count > 0)
sl@403
   180
            {
sl@403
   181
                if (aPacked)
sl@403
   182
                {
sl@403
   183
                    //string packedLine = "";
sl@403
   184
                    iTickCounter++;
sl@403
   185
                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
sl@403
   186
                    {
sl@403
   187
                        iTickCounter = 0;
sl@403
   188
                        if (iNextSensorToDisplay == 1)
sl@403
   189
                        {
sl@403
   190
                            iNextSensorToDisplay = 0;
sl@403
   191
                        }
sl@403
   192
                        else
sl@403
   193
                        {
sl@403
   194
                            iNextSensorToDisplay = 1;
sl@403
   195
                        }
sl@403
   196
                    }
sl@403
   197
sl@403
   198
                    //TODO: Do something like that to cycle lines if ever we want to
sl@403
   199
                    //SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
sl@403
   200
sl@403
   201
                    //Display packed sensors on our FrontView display
sl@403
   202
                    SetText(packedFirstLine, packedSecondLine);
sl@403
   203
                }
sl@403
   204
                else
sl@403
   205
                {
sl@403
   206
                    string secondLine = list[iNextSensorToDisplay].iSecondLine;
sl@403
   207
                    if (aDisplayTime)
sl@403
   208
                    {
sl@403
   209
                        //Add enough spaces
sl@403
   210
                        while (secondLine.Length + time.Length < KMaxCharacterPerLine)
sl@403
   211
                        {
sl@403
   212
                            secondLine += " ";
sl@403
   213
                        }
sl@403
   214
                        secondLine += time;
sl@403
   215
                    }
sl@403
   216
                    //Display current sensor on our FrontView display
sl@403
   217
                    SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
sl@403
   218
                    iTickCounter++;
sl@403
   219
                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
sl@403
   220
                    {
sl@403
   221
                        iTickCounter = 0;
sl@403
   222
                        iNextSensorToDisplay++;
sl@403
   223
                    }
sl@403
   224
                }
sl@403
   225
            }
sl@403
   226
sl@403
   227
            if (iNextSensorToDisplay == list.Count)
sl@403
   228
            {
sl@403
   229
                //Go back to first sensor
sl@403
   230
                iNextSensorToDisplay = 0;
sl@403
   231
            }
sl@403
   232
sl@403
   233
sl@403
   234
        }
sl@403
   235
sl@403
   236
        public bool Contains(ISensor sensor)
sl@403
   237
        {
sl@403
   238
            foreach (SensorSharpDisplay icon in list)
sl@403
   239
                if (icon.Sensor == sensor)
sl@403
   240
                    return true;
sl@403
   241
            return false;
sl@403
   242
        }
sl@403
   243
sl@403
   244
        public void Add(ISensor sensor, bool balloonTip)
sl@403
   245
        {
sl@403
   246
            if (Contains(sensor))
sl@403
   247
            {
sl@403
   248
                return;
sl@403
   249
            }
sl@403
   250
            else
sl@403
   251
            {
sl@403
   252
                //SL:
sl@403
   253
                list.Add(new SensorSharpDisplay(this, sensor, balloonTip, settings, unitManager));
sl@403
   254
                //UpdateMainIconVisibilty();
sl@403
   255
                settings.SetValue(new Identifier(sensor.Identifier, "SharpDisplay").ToString(), true);
sl@403
   256
                iNextSensorToDisplay = 0;
sl@403
   257
                if (list.Count == 1)
sl@403
   258
                {
sl@403
   259
                    //Just added first sensor in FrontView, unable FrontView plug-in mode
sl@403
   260
                    Init();
sl@403
   261
                }
sl@403
   262
            }
sl@403
   263
sl@403
   264
        }
sl@403
   265
sl@403
   266
        public void Remove(ISensor sensor)
sl@403
   267
        {
sl@403
   268
            Remove(sensor, true);
sl@403
   269
            iNextSensorToDisplay = 0;
sl@403
   270
            if (list.Count == 0)
sl@403
   271
            {
sl@403
   272
                //No sensor to display in FrontView, just disable FrontView plug-in mode
sl@403
   273
                Uninit();
sl@403
   274
            }
sl@403
   275
sl@403
   276
        }
sl@403
   277
sl@403
   278
        private void Remove(ISensor sensor, bool deleteConfig)
sl@403
   279
        {
sl@403
   280
            if (deleteConfig)
sl@403
   281
            {
sl@403
   282
                settings.Remove(
sl@403
   283
                  new Identifier(sensor.Identifier, "SharpDisplay").ToString());
sl@403
   284
            }
sl@403
   285
            SensorSharpDisplay instance = null;
sl@403
   286
            foreach (SensorSharpDisplay icon in list)
sl@403
   287
                if (icon.Sensor == sensor)
sl@403
   288
                    instance = icon;
sl@403
   289
            if (instance != null)
sl@403
   290
            {
sl@403
   291
                list.Remove(instance);
sl@403
   292
                //UpdateMainIconVisibilty();
sl@403
   293
                instance.Dispose();
sl@403
   294
            }
sl@403
   295
        }
sl@403
   296
sl@403
   297
sl@403
   298
sl@403
   299
        private void UpdateMainIconVisibilty()
sl@403
   300
        {
sl@403
   301
            /*
sl@403
   302
            if (mainIconEnabled)
sl@403
   303
            {
sl@403
   304
                mainIcon.Visible = list.Count == 0;
sl@403
   305
            }
sl@403
   306
            else
sl@403
   307
            {
sl@403
   308
                mainIcon.Visible = false;
sl@403
   309
            }
sl@403
   310
             */
sl@403
   311
        }
sl@403
   312
sl@403
   313
        public void Init()
sl@403
   314
        {
sl@403
   315
            //iServer.SendMessage("init:");
sl@403
   316
        }
sl@403
   317
sl@403
   318
        public void Uninit()
sl@403
   319
        {
sl@403
   320
            //iServer.SendMessage("uninit:");
sl@403
   321
        }
sl@403
   322
sl@403
   323
        public void SetText(string aUpperLine, string aLowerLine)
sl@403
   324
        {
sl@403
   325
            //iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
sl@403
   326
            iTextFieldTop.Text = aUpperLine;
sl@403
   327
            iTextFieldBottom.Text = aLowerLine;
sl@403
   328
            iClient.SetTexts(iTextFields);
sl@403
   329
sl@403
   330
        }
sl@403
   331
sl@403
   332
        public void Quit()
sl@403
   333
        {
sl@403
   334
            //iServer.SendMessage("quit:");
sl@403
   335
        }
sl@403
   336
sl@403
   337
        /*
sl@403
   338
        public bool IsMainIconEnabled
sl@403
   339
        {
sl@403
   340
            get { return mainIconEnabled; }
sl@403
   341
            set
sl@403
   342
            {
sl@403
   343
                if (mainIconEnabled != value)
sl@403
   344
                {
sl@403
   345
                    mainIconEnabled = value;
sl@403
   346
                    UpdateMainIconVisibilty();
sl@403
   347
                }
sl@403
   348
            }
sl@403
   349
        }*/
sl@403
   350
sl@403
   351
sl@403
   352
    }
sl@403
   353
}