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