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