GUI/SoundGraphDisplay.cs
author StephaneLenclud
Sat, 13 Apr 2013 00:43:25 +0200
changeset 396 21a9e2325617
parent 395 df649224ba4d
child 397 2dc91822f312
permissions -rw-r--r--
Sensors can now be displayed in FrontView.
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@396
    39
        private int nextSensorToDisplay=0;
StephaneLenclud@396
    40
sl@391
    41
sl@391
    42
        public SoundGraphDisplay(IComputer computer, PersistentSettings settings,
sl@391
    43
          UnitManager unitManager)
sl@391
    44
        {
sl@391
    45
            this.computer = computer;
sl@391
    46
            this.settings = settings;
sl@391
    47
            this.unitManager = unitManager;
sl@391
    48
            computer.HardwareAdded += new HardwareEventHandler(HardwareAdded);
sl@391
    49
            computer.HardwareRemoved += new HardwareEventHandler(HardwareRemoved);
sl@391
    50
StephaneLenclud@394
    51
            //Start our client if needed
StephaneLenclud@394
    52
            Process[] processes = Process.GetProcessesByName("SoundGraphAccess");
StephaneLenclud@394
    53
            if (!(processes.Length > 0))
StephaneLenclud@394
    54
            {
StephaneLenclud@394
    55
StephaneLenclud@394
    56
                //Process client = UserAccountControl.CreateProcessAsStandardUser(@"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe","");
StephaneLenclud@394
    57
                    /*
StephaneLenclud@394
    58
                Process client = new Process();
StephaneLenclud@394
    59
                client.StartInfo.FileName = @"D:\Dev\SoundGraphAccess\Debug\SoundGraphAccess.exe";
StephaneLenclud@394
    60
                client.StartInfo.WorkingDirectory = @"D:\Dev\SoundGraphAccess";
StephaneLenclud@394
    61
                client.Start();*/
StephaneLenclud@394
    62
            }
StephaneLenclud@394
    63
StephaneLenclud@395
    64
            //Start our SoundGraph server
StephaneLenclud@395
    65
            iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
StephaneLenclud@394
    66
            iServer.Start();
StephaneLenclud@394
    67
            //iServer.SendMessage("init:");
sl@391
    68
        }
sl@391
    69
sl@391
    70
        private void HardwareRemoved(IHardware hardware)
sl@391
    71
        {
sl@391
    72
            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
sl@391
    73
            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
sl@391
    74
            foreach (ISensor sensor in hardware.Sensors)
sl@391
    75
                SensorRemoved(sensor);
sl@391
    76
            foreach (IHardware subHardware in hardware.SubHardware)
sl@391
    77
                HardwareRemoved(subHardware);
sl@391
    78
        }
sl@391
    79
sl@391
    80
        private void HardwareAdded(IHardware hardware)
sl@391
    81
        {
sl@391
    82
            foreach (ISensor sensor in hardware.Sensors)
sl@391
    83
                SensorAdded(sensor);
sl@391
    84
            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
sl@391
    85
            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
sl@391
    86
            foreach (IHardware subHardware in hardware.SubHardware)
sl@391
    87
                HardwareAdded(subHardware);
sl@391
    88
        }
sl@391
    89
sl@391
    90
        private void SensorAdded(ISensor sensor)
sl@391
    91
        {
sl@391
    92
            if (settings.GetValue(new Identifier(sensor.Identifier,
StephaneLenclud@396
    93
              "FrontView").ToString(), false))
sl@391
    94
                Add(sensor, false);
sl@391
    95
        }
sl@391
    96
sl@391
    97
        private void SensorRemoved(ISensor sensor)
sl@391
    98
        {
sl@391
    99
            if (Contains(sensor))
sl@391
   100
                Remove(sensor, false);
sl@391
   101
        }
sl@391
   102
sl@391
   103
        public void Dispose()
sl@391
   104
        {
sl@391
   105
            foreach (SensorFrontView icon in list)
sl@391
   106
                icon.Dispose();
sl@391
   107
StephaneLenclud@395
   108
            Quit();
StephaneLenclud@394
   109
            iServer.Stop();
sl@391
   110
sl@391
   111
        }
sl@391
   112
sl@391
   113
        public void Redraw()
sl@391
   114
        {
StephaneLenclud@396
   115
            //TODO: construct our two lines of texts, scroll or alternate sensors
StephaneLenclud@396
   116
            foreach (SensorFrontView sensor in list)
StephaneLenclud@396
   117
            {
StephaneLenclud@396
   118
                sensor.Update();
StephaneLenclud@396
   119
                //SetText(sensor.iFirstLine, sensor.iSecondLine);
StephaneLenclud@396
   120
            }
StephaneLenclud@396
   121
StephaneLenclud@396
   122
            //Alternate between sensors 
StephaneLenclud@396
   123
            if (list.Count > 0)
StephaneLenclud@396
   124
            {
StephaneLenclud@396
   125
                SetText(list[nextSensorToDisplay].iFirstLine, list[nextSensorToDisplay].iSecondLine);
StephaneLenclud@396
   126
                nextSensorToDisplay++;
StephaneLenclud@396
   127
            }
StephaneLenclud@396
   128
StephaneLenclud@396
   129
            if (nextSensorToDisplay == list.Count)
StephaneLenclud@396
   130
            {
StephaneLenclud@396
   131
                //Go back to first sensor
StephaneLenclud@396
   132
                nextSensorToDisplay = 0;
StephaneLenclud@396
   133
            }
StephaneLenclud@396
   134
StephaneLenclud@396
   135
StephaneLenclud@396
   136
            //
StephaneLenclud@396
   137
            
sl@391
   138
        }
sl@391
   139
sl@391
   140
        public bool Contains(ISensor sensor)
sl@391
   141
        {
sl@391
   142
            foreach (SensorFrontView icon in list)
sl@391
   143
                if (icon.Sensor == sensor)
sl@391
   144
                    return true;
sl@391
   145
            return false;
sl@391
   146
        }
sl@391
   147
sl@391
   148
        public void Add(ISensor sensor, bool balloonTip)
sl@391
   149
        {
sl@391
   150
            if (Contains(sensor))
sl@391
   151
            {
sl@391
   152
                return;
sl@391
   153
            }
sl@391
   154
            else
sl@391
   155
            {
sl@391
   156
                //SL:
StephaneLenclud@396
   157
                list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
sl@391
   158
                //UpdateMainIconVisibilty();
StephaneLenclud@396
   159
                settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
StephaneLenclud@396
   160
                nextSensorToDisplay = 0;
sl@391
   161
            }
StephaneLenclud@396
   162
sl@391
   163
        }
sl@391
   164
sl@391
   165
        public void Remove(ISensor sensor)
sl@391
   166
        {
sl@391
   167
            Remove(sensor, true);
StephaneLenclud@396
   168
            nextSensorToDisplay = 0;
sl@391
   169
        }
sl@391
   170
sl@391
   171
        private void Remove(ISensor sensor, bool deleteConfig)
sl@391
   172
        {
sl@391
   173
            if (deleteConfig)
sl@391
   174
            {
sl@391
   175
                settings.Remove(
StephaneLenclud@396
   176
                  new Identifier(sensor.Identifier, "FrontView").ToString());
sl@391
   177
            }
sl@391
   178
            SensorFrontView instance = null;
sl@391
   179
            foreach (SensorFrontView icon in list)
sl@391
   180
                if (icon.Sensor == sensor)
sl@391
   181
                    instance = icon;
sl@391
   182
            if (instance != null)
sl@391
   183
            {
sl@391
   184
                list.Remove(instance);
StephaneLenclud@396
   185
                //UpdateMainIconVisibilty();
sl@391
   186
                instance.Dispose();
sl@391
   187
            }
sl@391
   188
        }
sl@391
   189
sl@391
   190
sl@391
   191
sl@391
   192
        private void UpdateMainIconVisibilty()
sl@391
   193
        {
sl@391
   194
            /*
sl@391
   195
            if (mainIconEnabled)
sl@391
   196
            {
sl@391
   197
                mainIcon.Visible = list.Count == 0;
sl@391
   198
            }
sl@391
   199
            else
sl@391
   200
            {
sl@391
   201
                mainIcon.Visible = false;
sl@391
   202
            }
sl@391
   203
             */
sl@391
   204
        }
sl@391
   205
StephaneLenclud@394
   206
        public void Init()
sl@392
   207
        {
StephaneLenclud@394
   208
            iServer.SendMessage("init:");
sl@392
   209
        }
sl@392
   210
sl@393
   211
        public void Uninit()
sl@392
   212
        {
StephaneLenclud@394
   213
            iServer.SendMessage("uninit:");
sl@392
   214
        }
sl@392
   215
sl@393
   216
        public void SetText(string aUpperLine, string aLowerLine)
sl@392
   217
        {
StephaneLenclud@396
   218
            iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
sl@392
   219
        }
sl@392
   220
StephaneLenclud@395
   221
        public void Quit()
StephaneLenclud@395
   222
        {
StephaneLenclud@395
   223
            iServer.SendMessage("quit:");
StephaneLenclud@395
   224
        }
StephaneLenclud@395
   225
sl@391
   226
        /*
sl@391
   227
        public bool IsMainIconEnabled
sl@391
   228
        {
sl@391
   229
            get { return mainIconEnabled; }
sl@391
   230
            set
sl@391
   231
            {
sl@391
   232
                if (mainIconEnabled != value)
sl@391
   233
                {
sl@391
   234
                    mainIconEnabled = value;
sl@391
   235
                    UpdateMainIconVisibilty();
sl@391
   236
                }
sl@391
   237
            }
sl@391
   238
        }*/
sl@392
   239
sl@393
   240
sl@391
   241
    }
sl@391
   242
}