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