GUI/SoundGraphDisplay.cs
author StephaneLenclud
Sun, 14 Apr 2013 22:38:41 +0200
branchMiniDisplay
changeset 439 06369ace500d
parent 438 f590956d3234
child 442 782b48689513
permissions -rw-r--r--
Better formatting FrontView lines.
Now cycling through each FrontView sensor every 2 seconds.
Fixing character conversion when sending VFD text.
Using special FrontView characters for Celsius and Farenheit.
StephaneLenclud@433
     1
/*
StephaneLenclud@433
     2
 
StephaneLenclud@433
     3
  This Source Code Form is subject to the terms of the Mozilla Public
StephaneLenclud@433
     4
  License, v. 2.0. If a copy of the MPL was not distributed with this
StephaneLenclud@433
     5
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
StephaneLenclud@433
     6
 
StephaneLenclud@433
     7
  Copyright (C) 2009-2012 Michael Möller <mmoeller@openhardwaremonitor.org>
StephaneLenclud@433
     8
	
StephaneLenclud@433
     9
*/
StephaneLenclud@433
    10
StephaneLenclud@433
    11
using System;
StephaneLenclud@433
    12
using System.Collections.Generic;
StephaneLenclud@433
    13
using System.Drawing;
StephaneLenclud@433
    14
using System.Text;
StephaneLenclud@436
    15
using System.Diagnostics;
StephaneLenclud@433
    16
using System.Windows.Forms;
StephaneLenclud@435
    17
using System.Windows;
StephaneLenclud@433
    18
using OpenHardwareMonitor.Hardware;
StephaneLenclud@433
    19
using OpenHardwareMonitor.Utilities;
StephaneLenclud@433
    20
using System.Runtime.InteropServices;
StephaneLenclud@436
    21
using UacHelpers;
StephaneLenclud@433
    22
StephaneLenclud@433
    23
StephaneLenclud@433
    24
StephaneLenclud@433
    25
StephaneLenclud@433
    26
StephaneLenclud@434
    27
StephaneLenclud@433
    28
StephaneLenclud@433
    29
namespace OpenHardwareMonitor.GUI
StephaneLenclud@433
    30
{
StephaneLenclud@433
    31
    public class SoundGraphDisplay : IDisposable
StephaneLenclud@433
    32
    {
StephaneLenclud@433
    33
        private IComputer computer;
StephaneLenclud@433
    34
        private PersistentSettings settings;
StephaneLenclud@433
    35
        private UnitManager unitManager;
StephaneLenclud@433
    36
        private List<SensorFrontView> list = new List<SensorFrontView>();
StephaneLenclud@436
    37
        private SoundGraph.Server iServer;
StephaneLenclud@436
    38
StephaneLenclud@439
    39
        private int iNextSensorToDisplay=0;
StephaneLenclud@439
    40
        private int iTickCounter=0;
StephaneLenclud@438
    41
StephaneLenclud@433
    42
StephaneLenclud@433
    43
        public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
StephaneLenclud@433
    44
          UnitManager unitManager)
StephaneLenclud@433
    45
        {
StephaneLenclud@433
    46
            this.computer = computer;
StephaneLenclud@433
    47
            this.settings = settings;
StephaneLenclud@433
    48
            this.unitManager = unitManager;
StephaneLenclud@433
    49
            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
StephaneLenclud@433
    50
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
StephaneLenclud@433
    51
StephaneLenclud@436
    52
            //Start our client if needed
StephaneLenclud@436
    53
            Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
StephaneLenclud@436
    54
            if (!(processes.Length > 0))
StephaneLenclud@436
    55
            {
StephaneLenclud@436
    56
StephaneLenclud@436
    57
                //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
StephaneLenclud@436
    58
                    /*
StephaneLenclud@436
    59
                Process client = new Process();
StephaneLenclud@436
    60
                client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
StephaneLenclud@436
    61
                client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
StephaneLenclud@436
    62
                client.Start();*/
StephaneLenclud@436
    63
            }
StephaneLenclud@436
    64
StephaneLenclud@437
    65
            //Start our SoundGraph server
StephaneLenclud@437
    66
            iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
StephaneLenclud@436
    67
            iServer.Start();
StephaneLenclud@436
    68
            //iServer.SendMessage("init:");
StephaneLenclud@433
    69
        }
StephaneLenclud@433
    70
StephaneLenclud@433
    71
        private void HardwareRemoved(IHardware hardware)
StephaneLenclud@433
    72
        {
StephaneLenclud@433
    73
            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
StephaneLenclud@433
    74
            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
StephaneLenclud@433
    75
            foreach (ISensor sensor in hardware.Sensors)
StephaneLenclud@433
    76
                SensorRemoved(sensor);
StephaneLenclud@433
    77
            foreach (IHardware subHardware in hardware.SubHardware)
StephaneLenclud@433
    78
                HardwareRemoved(subHardware);
StephaneLenclud@433
    79
        }
StephaneLenclud@433
    80
StephaneLenclud@433
    81
        private void HardwareAdded(IHardware hardware)
StephaneLenclud@433
    82
        {
StephaneLenclud@433
    83
            foreach (ISensor sensor in hardware.Sensors)
StephaneLenclud@433
    84
                SensorAdded(sensor);
StephaneLenclud@433
    85
            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
StephaneLenclud@433
    86
            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
StephaneLenclud@433
    87
            foreach (IHardware subHardware in hardware.SubHardware)
StephaneLenclud@433
    88
                HardwareAdded(subHardware);
StephaneLenclud@433
    89
        }
StephaneLenclud@433
    90
StephaneLenclud@433
    91
        private void SensorAdded(ISensor sensor)
StephaneLenclud@433
    92
        {
StephaneLenclud@433
    93
            if (settings.GetValue(new Identifier(sensor.Identifier,
StephaneLenclud@438
    94
              "FrontView").ToString(), false))
StephaneLenclud@433
    95
                Add(sensor, false);
StephaneLenclud@433
    96
        }
StephaneLenclud@433
    97
StephaneLenclud@433
    98
        private void SensorRemoved(ISensor sensor)
StephaneLenclud@433
    99
        {
StephaneLenclud@433
   100
            if (Contains(sensor))
StephaneLenclud@433
   101
                Remove(sensor, false);
StephaneLenclud@433
   102
        }
StephaneLenclud@433
   103
StephaneLenclud@433
   104
        public void Dispose()
StephaneLenclud@433
   105
        {
StephaneLenclud@433
   106
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   107
                icon.Dispose();
StephaneLenclud@433
   108
StephaneLenclud@437
   109
            Quit();
StephaneLenclud@436
   110
            iServer.Stop();
StephaneLenclud@433
   111
StephaneLenclud@433
   112
        }
StephaneLenclud@433
   113
StephaneLenclud@433
   114
        public void Redraw()
StephaneLenclud@433
   115
        {
StephaneLenclud@439
   116
            //Update all sensors from our front view
StephaneLenclud@438
   117
            foreach (SensorFrontView sensor in list)
StephaneLenclud@438
   118
            {
StephaneLenclud@438
   119
                sensor.Update();
StephaneLenclud@438
   120
                //SetText(sensor.iFirstLine, sensor.iSecondLine);
StephaneLenclud@438
   121
            }
StephaneLenclud@438
   122
StephaneLenclud@438
   123
            //Alternate between sensors 
StephaneLenclud@438
   124
            if (list.Count > 0)
StephaneLenclud@438
   125
            {
StephaneLenclud@439
   126
                //Display current sensor on our FrontView display
StephaneLenclud@439
   127
                SetText(list[iNextSensorToDisplay].iFirstLine, list[iNextSensorToDisplay].iSecondLine);
StephaneLenclud@439
   128
                iTickCounter++;
StephaneLenclud@439
   129
                if (iTickCounter==2) //Move to the next sensor only every second tick
StephaneLenclud@439
   130
                {
StephaneLenclud@439
   131
                    iTickCounter = 0;
StephaneLenclud@439
   132
                    iNextSensorToDisplay++;
StephaneLenclud@439
   133
                }
StephaneLenclud@438
   134
            }
StephaneLenclud@438
   135
StephaneLenclud@439
   136
            if (iNextSensorToDisplay == list.Count)
StephaneLenclud@438
   137
            {
StephaneLenclud@438
   138
                //Go back to first sensor
StephaneLenclud@439
   139
                iNextSensorToDisplay = 0;
StephaneLenclud@438
   140
            }
StephaneLenclud@438
   141
StephaneLenclud@438
   142
            
StephaneLenclud@433
   143
        }
StephaneLenclud@433
   144
StephaneLenclud@433
   145
        public bool Contains(ISensor sensor)
StephaneLenclud@433
   146
        {
StephaneLenclud@433
   147
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   148
                if (icon.Sensor == sensor)
StephaneLenclud@433
   149
                    return true;
StephaneLenclud@433
   150
            return false;
StephaneLenclud@433
   151
        }
StephaneLenclud@433
   152
StephaneLenclud@433
   153
        public void Add(ISensor sensor, bool balloonTip)
StephaneLenclud@433
   154
        {
StephaneLenclud@433
   155
            if (Contains(sensor))
StephaneLenclud@433
   156
            {
StephaneLenclud@433
   157
                return;
StephaneLenclud@433
   158
            }
StephaneLenclud@433
   159
            else
StephaneLenclud@433
   160
            {
StephaneLenclud@433
   161
                //SL:
StephaneLenclud@438
   162
                list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
StephaneLenclud@433
   163
                //UpdateMainIconVisibilty();
StephaneLenclud@438
   164
                settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
StephaneLenclud@439
   165
                iNextSensorToDisplay = 0;
StephaneLenclud@433
   166
            }
StephaneLenclud@438
   167
StephaneLenclud@433
   168
        }
StephaneLenclud@433
   169
StephaneLenclud@433
   170
        public void Remove(ISensor sensor)
StephaneLenclud@433
   171
        {
StephaneLenclud@433
   172
            Remove(sensor, true);
StephaneLenclud@439
   173
            iNextSensorToDisplay = 0;
StephaneLenclud@433
   174
        }
StephaneLenclud@433
   175
StephaneLenclud@433
   176
        private void Remove(ISensor sensor, bool deleteConfig)
StephaneLenclud@433
   177
        {
StephaneLenclud@433
   178
            if (deleteConfig)
StephaneLenclud@433
   179
            {
StephaneLenclud@433
   180
                settings.Remove(
StephaneLenclud@438
   181
                  new Identifier(sensor.Identifier, "FrontView").ToString());
StephaneLenclud@433
   182
            }
StephaneLenclud@433
   183
            SensorFrontView instance = null;
StephaneLenclud@433
   184
            foreach (SensorFrontView icon in list)
StephaneLenclud@433
   185
                if (icon.Sensor == sensor)
StephaneLenclud@433
   186
                    instance = icon;
StephaneLenclud@433
   187
            if (instance != null)
StephaneLenclud@433
   188
            {
StephaneLenclud@433
   189
                list.Remove(instance);
StephaneLenclud@438
   190
                //UpdateMainIconVisibilty();
StephaneLenclud@433
   191
                instance.Dispose();
StephaneLenclud@433
   192
            }
StephaneLenclud@433
   193
        }
StephaneLenclud@433
   194
StephaneLenclud@433
   195
StephaneLenclud@433
   196
StephaneLenclud@433
   197
        private void UpdateMainIconVisibilty()
StephaneLenclud@433
   198
        {
StephaneLenclud@433
   199
            /*
StephaneLenclud@433
   200
            if (mainIconEnabled)
StephaneLenclud@433
   201
            {
StephaneLenclud@433
   202
                mainIcon.Visible = list.Count == 0;
StephaneLenclud@433
   203
            }
StephaneLenclud@433
   204
            else
StephaneLenclud@433
   205
            {
StephaneLenclud@433
   206
                mainIcon.Visible = false;
StephaneLenclud@433
   207
            }
StephaneLenclud@433
   208
             */
StephaneLenclud@433
   209
        }
StephaneLenclud@433
   210
StephaneLenclud@436
   211
        public void Init()
StephaneLenclud@434
   212
        {
StephaneLenclud@436
   213
            iServer.SendMessage("init:");
StephaneLenclud@434
   214
        }
StephaneLenclud@434
   215
StephaneLenclud@435
   216
        public void Uninit()
StephaneLenclud@434
   217
        {
StephaneLenclud@436
   218
            iServer.SendMessage("uninit:");
StephaneLenclud@434
   219
        }
StephaneLenclud@434
   220
StephaneLenclud@435
   221
        public void SetText(string aUpperLine, string aLowerLine)
StephaneLenclud@434
   222
        {
StephaneLenclud@438
   223
            iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
StephaneLenclud@434
   224
        }
StephaneLenclud@434
   225
StephaneLenclud@437
   226
        public void Quit()
StephaneLenclud@437
   227
        {
StephaneLenclud@437
   228
            iServer.SendMessage("quit:");
StephaneLenclud@437
   229
        }
StephaneLenclud@437
   230
StephaneLenclud@433
   231
        /*
StephaneLenclud@433
   232
        public bool IsMainIconEnabled
StephaneLenclud@433
   233
        {
StephaneLenclud@433
   234
            get { return mainIconEnabled; }
StephaneLenclud@433
   235
            set
StephaneLenclud@433
   236
            {
StephaneLenclud@433
   237
                if (mainIconEnabled != value)
StephaneLenclud@433
   238
                {
StephaneLenclud@433
   239
                    mainIconEnabled = value;
StephaneLenclud@433
   240
                    UpdateMainIconVisibilty();
StephaneLenclud@433
   241
                }
StephaneLenclud@433
   242
            }
StephaneLenclud@433
   243
        }*/
StephaneLenclud@434
   244
StephaneLenclud@435
   245
StephaneLenclud@433
   246
    }
StephaneLenclud@433
   247
}