GUI/SoundGraphDisplay.cs
author sl
Sun, 21 Sep 2014 21:55:36 +0200
changeset 403 1d10b3a8a235
parent 401 70d865489ff3
permissions -rw-r--r--
Support for SharDisplayManager.
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@402
    56
                //Try to launch the sound graph process from the same folder as this executable
StephaneLenclud@402
    57
                string exeName=System.IO.Path.GetDirectoryName(Application.ExecutablePath);
StephaneLenclud@402
    58
                exeName += @"\SoundGraphAccess.exe";
StephaneLenclud@402
    59
                Process client = UserAccountControl.CreateProcessAsStandardUser(exeName,"");
StephaneLenclud@394
    60
            }
StephaneLenclud@394
    61
StephaneLenclud@395
    62
            //Start our SoundGraph server
StephaneLenclud@395
    63
            iServer = new SoundGraph.Server(@"\\.\pipe\sga-inbound", @"\\.\pipe\sga-outbound");
StephaneLenclud@394
    64
            iServer.Start();
sl@391
    65
        }
sl@391
    66
sl@391
    67
        private void HardwareRemoved(IHardware hardware)
sl@391
    68
        {
sl@391
    69
            hardware.SensorAdded -= new SensorEventHandler(SensorAdded);
sl@391
    70
            hardware.SensorRemoved -= new SensorEventHandler(SensorRemoved);
sl@391
    71
            foreach (ISensor sensor in hardware.Sensors)
sl@391
    72
                SensorRemoved(sensor);
sl@391
    73
            foreach (IHardware subHardware in hardware.SubHardware)
sl@391
    74
                HardwareRemoved(subHardware);
sl@391
    75
        }
sl@391
    76
sl@391
    77
        private void HardwareAdded(IHardware hardware)
sl@391
    78
        {
sl@391
    79
            foreach (ISensor sensor in hardware.Sensors)
sl@391
    80
                SensorAdded(sensor);
sl@391
    81
            hardware.SensorAdded += new SensorEventHandler(SensorAdded);
sl@391
    82
            hardware.SensorRemoved += new SensorEventHandler(SensorRemoved);
sl@391
    83
            foreach (IHardware subHardware in hardware.SubHardware)
sl@391
    84
                HardwareAdded(subHardware);
sl@391
    85
        }
sl@391
    86
sl@391
    87
        private void SensorAdded(ISensor sensor)
sl@391
    88
        {
sl@391
    89
            if (settings.GetValue(new Identifier(sensor.Identifier,
StephaneLenclud@396
    90
              "FrontView").ToString(), false))
sl@391
    91
                Add(sensor, false);
sl@391
    92
        }
sl@391
    93
sl@391
    94
        private void SensorRemoved(ISensor sensor)
sl@391
    95
        {
sl@391
    96
            if (Contains(sensor))
sl@391
    97
                Remove(sensor, false);
sl@391
    98
        }
sl@391
    99
sl@391
   100
        public void Dispose()
sl@391
   101
        {
sl@391
   102
            foreach (SensorFrontView icon in list)
sl@391
   103
                icon.Dispose();
sl@391
   104
StephaneLenclud@395
   105
            Quit();
StephaneLenclud@394
   106
            iServer.Stop();
sl@391
   107
sl@391
   108
        }
sl@391
   109
lenclud@400
   110
        public void Redraw(bool aPacked, bool aDisplayTime)
sl@391
   111
        {
StephaneLenclud@401
   112
            const int KNumberOfTickBeforeSwitch = 4;
StephaneLenclud@401
   113
            const int KMaxCharacterPerLine = 16;
lenclud@400
   114
            string packedFirstLine=""; //We have 16 chars per line on our VFD
lenclud@400
   115
            string packedSecondLine="";
lenclud@400
   116
            int count = 0;
lenclud@400
   117
StephaneLenclud@401
   118
            string time = DateTime.Now.ToShortTimeString();
StephaneLenclud@401
   119
StephaneLenclud@397
   120
            //Update all sensors from our front view
StephaneLenclud@396
   121
            foreach (SensorFrontView sensor in list)
StephaneLenclud@396
   122
            {
lenclud@400
   123
                count++;
StephaneLenclud@396
   124
                sensor.Update();
lenclud@400
   125
StephaneLenclud@402
   126
                if (aDisplayTime && count == 1)
StephaneLenclud@402
   127
                {
StephaneLenclud@402
   128
                    //First slot is take by time display
StephaneLenclud@402
   129
                    count++;
StephaneLenclud@402
   130
                    packedFirstLine = time + " ";
StephaneLenclud@402
   131
                }
StephaneLenclud@402
   132
lenclud@400
   133
                if (aPacked)
lenclud@400
   134
                {
StephaneLenclud@402
   135
                    //Build strings for packed mode
lenclud@400
   136
                    string packedText = "";
lenclud@400
   137
                    packedText = sensor.iFirstLine.Substring(0, 3) + ":" + sensor.iSecondLine;
lenclud@400
   138
                    if (count == 1)
lenclud@400
   139
                    {
StephaneLenclud@402
   140
                        packedFirstLine = packedText + " "; //Minimum one space to separate sensors on the same line
lenclud@400
   141
                    }
lenclud@400
   142
                    else if (count == 2)
lenclud@400
   143
                    {
StephaneLenclud@402
   144
                        //Add enough spaces to align to right hand side
StephaneLenclud@401
   145
                        while (packedFirstLine.Length + packedText.Length < KMaxCharacterPerLine)
lenclud@400
   146
                        {
lenclud@400
   147
                            packedFirstLine += " ";
lenclud@400
   148
                        }
lenclud@400
   149
                        packedFirstLine += packedText;
lenclud@400
   150
                    }
lenclud@400
   151
                    else if (count == 3)
lenclud@400
   152
                    {
StephaneLenclud@402
   153
                        packedSecondLine = packedText + " "; //Minimum one space to separate sensors on the same line
lenclud@400
   154
                    }
lenclud@400
   155
                    else if (count == 4)
lenclud@400
   156
                    {
StephaneLenclud@402
   157
                        //Add enough spaces to align to right hand side
StephaneLenclud@401
   158
                        while (packedSecondLine.Length + packedText.Length < KMaxCharacterPerLine)
lenclud@400
   159
                        {
lenclud@400
   160
                            packedSecondLine += " ";
lenclud@400
   161
                        }
lenclud@400
   162
                        packedSecondLine += packedText;
lenclud@400
   163
                    }
lenclud@400
   164
                }
StephaneLenclud@396
   165
                //SetText(sensor.iFirstLine, sensor.iSecondLine);
StephaneLenclud@396
   166
            }
StephaneLenclud@396
   167
StephaneLenclud@396
   168
            //Alternate between sensors 
StephaneLenclud@396
   169
            if (list.Count > 0)
StephaneLenclud@396
   170
            {
lenclud@400
   171
                if (aPacked)
StephaneLenclud@397
   172
                {
lenclud@400
   173
                    //string packedLine = "";
lenclud@400
   174
                    iTickCounter++;
StephaneLenclud@401
   175
                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
lenclud@400
   176
                    {
lenclud@400
   177
                        iTickCounter = 0;
lenclud@400
   178
                        if (iNextSensorToDisplay==1)
lenclud@400
   179
                        {
lenclud@400
   180
                            iNextSensorToDisplay=0;
lenclud@400
   181
                        }
lenclud@400
   182
                        else
lenclud@400
   183
                        {
lenclud@400
   184
                            iNextSensorToDisplay=1;
lenclud@400
   185
                        }
lenclud@400
   186
                    }
lenclud@400
   187
StephaneLenclud@402
   188
                    //TODO: Do something like that to cycle lines if ever we want to
StephaneLenclud@402
   189
                    //SetText(time, (iNextSensorToDisplay == 1 && packedSecondLine.Length > 0 ? packedSecondLine : packedFirstLine));
lenclud@400
   190
StephaneLenclud@402
   191
                    //Display packed sensors on our FrontView display
StephaneLenclud@402
   192
                    SetText(packedFirstLine, packedSecondLine);
lenclud@400
   193
                }
lenclud@400
   194
                else
lenclud@400
   195
                {
StephaneLenclud@401
   196
                    string secondLine = list[iNextSensorToDisplay].iSecondLine;
StephaneLenclud@401
   197
                    if (aDisplayTime)
StephaneLenclud@401
   198
                    {
StephaneLenclud@401
   199
                        //Add enough spaces
StephaneLenclud@401
   200
                        while (secondLine.Length + time.Length < KMaxCharacterPerLine)
StephaneLenclud@401
   201
                        {
StephaneLenclud@401
   202
                            secondLine += " ";
StephaneLenclud@401
   203
                        }
StephaneLenclud@401
   204
                        secondLine += time;
StephaneLenclud@401
   205
                    }
lenclud@400
   206
                    //Display current sensor on our FrontView display
StephaneLenclud@401
   207
                    SetText(list[iNextSensorToDisplay].iFirstLine, secondLine);
lenclud@400
   208
                    iTickCounter++;
StephaneLenclud@401
   209
                    if (iTickCounter == KNumberOfTickBeforeSwitch) //Move to the next sensor only every so many tick
lenclud@400
   210
                    {
lenclud@400
   211
                        iTickCounter = 0;
lenclud@400
   212
                        iNextSensorToDisplay++;
lenclud@400
   213
                    }
StephaneLenclud@397
   214
                }
StephaneLenclud@396
   215
            }
StephaneLenclud@396
   216
StephaneLenclud@397
   217
            if (iNextSensorToDisplay == list.Count)
StephaneLenclud@396
   218
            {
StephaneLenclud@396
   219
                //Go back to first sensor
StephaneLenclud@397
   220
                iNextSensorToDisplay = 0;
StephaneLenclud@396
   221
            }
StephaneLenclud@396
   222
StephaneLenclud@396
   223
            
sl@391
   224
        }
sl@391
   225
sl@391
   226
        public bool Contains(ISensor sensor)
sl@391
   227
        {
sl@391
   228
            foreach (SensorFrontView icon in list)
sl@391
   229
                if (icon.Sensor == sensor)
sl@391
   230
                    return true;
sl@391
   231
            return false;
sl@391
   232
        }
sl@391
   233
sl@391
   234
        public void Add(ISensor sensor, bool balloonTip)
sl@391
   235
        {
sl@391
   236
            if (Contains(sensor))
sl@391
   237
            {
sl@391
   238
                return;
sl@391
   239
            }
sl@391
   240
            else
sl@391
   241
            {
sl@391
   242
                //SL:
StephaneLenclud@396
   243
                list.Add(new SensorFrontView(this, sensor, balloonTip, settings, unitManager));
sl@391
   244
                //UpdateMainIconVisibilty();
StephaneLenclud@396
   245
                settings.SetValue(new Identifier(sensor.Identifier, "FrontView").ToString(), true);
StephaneLenclud@397
   246
                iNextSensorToDisplay = 0;
StephaneLenclud@402
   247
                if (list.Count == 1)
StephaneLenclud@402
   248
                {
StephaneLenclud@402
   249
                    //Just added first sensor in FrontView, unable FrontView plug-in mode
StephaneLenclud@402
   250
                    Init();
StephaneLenclud@402
   251
                }
sl@391
   252
            }
StephaneLenclud@396
   253
sl@391
   254
        }
sl@391
   255
sl@391
   256
        public void Remove(ISensor sensor)
sl@391
   257
        {
sl@391
   258
            Remove(sensor, true);
StephaneLenclud@397
   259
            iNextSensorToDisplay = 0;
StephaneLenclud@402
   260
            if (list.Count == 0)
StephaneLenclud@402
   261
            {
StephaneLenclud@402
   262
                //No sensor to display in FrontView, just disable FrontView plug-in mode
StephaneLenclud@402
   263
                Uninit();
StephaneLenclud@402
   264
            }
StephaneLenclud@402
   265
sl@391
   266
        }
sl@391
   267
sl@391
   268
        private void Remove(ISensor sensor, bool deleteConfig)
sl@391
   269
        {
sl@391
   270
            if (deleteConfig)
sl@391
   271
            {
sl@391
   272
                settings.Remove(
StephaneLenclud@396
   273
                  new Identifier(sensor.Identifier, "FrontView").ToString());
sl@391
   274
            }
sl@391
   275
            SensorFrontView instance = null;
sl@391
   276
            foreach (SensorFrontView icon in list)
sl@391
   277
                if (icon.Sensor == sensor)
sl@391
   278
                    instance = icon;
sl@391
   279
            if (instance != null)
sl@391
   280
            {
sl@391
   281
                list.Remove(instance);
StephaneLenclud@396
   282
                //UpdateMainIconVisibilty();
sl@391
   283
                instance.Dispose();
sl@391
   284
            }
sl@391
   285
        }
sl@391
   286
sl@391
   287
sl@391
   288
sl@391
   289
        private void UpdateMainIconVisibilty()
sl@391
   290
        {
sl@391
   291
            /*
sl@391
   292
            if (mainIconEnabled)
sl@391
   293
            {
sl@391
   294
                mainIcon.Visible = list.Count == 0;
sl@391
   295
            }
sl@391
   296
            else
sl@391
   297
            {
sl@391
   298
                mainIcon.Visible = false;
sl@391
   299
            }
sl@391
   300
             */
sl@391
   301
        }
sl@391
   302
StephaneLenclud@394
   303
        public void Init()
sl@392
   304
        {
StephaneLenclud@394
   305
            iServer.SendMessage("init:");
sl@392
   306
        }
sl@392
   307
sl@393
   308
        public void Uninit()
sl@392
   309
        {
StephaneLenclud@394
   310
            iServer.SendMessage("uninit:");
sl@392
   311
        }
sl@392
   312
sl@393
   313
        public void SetText(string aUpperLine, string aLowerLine)
sl@392
   314
        {
StephaneLenclud@396
   315
            iServer.SendMessage("set-vfd-text:" + aUpperLine + "\n" + aLowerLine);
sl@392
   316
        }
sl@392
   317
StephaneLenclud@395
   318
        public void Quit()
StephaneLenclud@395
   319
        {
StephaneLenclud@395
   320
            iServer.SendMessage("quit:");
StephaneLenclud@395
   321
        }
StephaneLenclud@395
   322
sl@391
   323
        /*
sl@391
   324
        public bool IsMainIconEnabled
sl@391
   325
        {
sl@391
   326
            get { return mainIconEnabled; }
sl@391
   327
            set
sl@391
   328
            {
sl@391
   329
                if (mainIconEnabled != value)
sl@391
   330
                {
sl@391
   331
                    mainIconEnabled = value;
sl@391
   332
                    UpdateMainIconVisibilty();
sl@391
   333
                }
sl@391
   334
            }
sl@391
   335
        }*/
sl@392
   336
sl@393
   337
sl@391
   338
    }
sl@391
   339
}