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