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