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