Server/FormMain.cs
author StephaneLenclud
Wed, 04 Jan 2017 18:43:28 +0100
changeset 274 920fea7a6427
parent 273 e5f85a895a62
child 275 a4a341accc89
permissions -rw-r--r--
Proper basic support for Audio Visualizer.
StephaneLenclud@123
     1
//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 Stéphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@0
    21
using System.Collections.Generic;
sl@0
    22
using System.ComponentModel;
sl@0
    23
using System.Data;
sl@0
    24
using System.Drawing;
sl@0
    25
using System.Linq;
sl@0
    26
using System.Text;
sl@0
    27
using System.Threading.Tasks;
sl@0
    28
using System.Windows.Forms;
sl@14
    29
using System.IO;
sl@0
    30
using CodeProject.Dialog;
sl@14
    31
using System.Drawing.Imaging;
sl@17
    32
using System.ServiceModel;
sl@25
    33
using System.Threading;
sl@31
    34
using System.Diagnostics;
sl@88
    35
using System.Deployment.Application;
sl@94
    36
using System.Reflection;
StephaneLenclud@112
    37
using System.Runtime.InteropServices;
StephaneLenclud@268
    38
using System.Security;
StephaneLenclud@272
    39
//CSCore
StephaneLenclud@273
    40
using CSCore;
StephaneLenclud@273
    41
using CSCore.Win32;
StephaneLenclud@273
    42
using CSCore.DSP;
StephaneLenclud@273
    43
using CSCore.Streams;
StephaneLenclud@272
    44
using CSCore.CoreAudioAPI;
StephaneLenclud@273
    45
using CSCore.SoundIn;
StephaneLenclud@273
    46
// Visualization
StephaneLenclud@273
    47
using Visualization;
StephaneLenclud@272
    48
// CEC
StephaneLenclud@206
    49
using CecSharp;
StephaneLenclud@117
    50
//Network
StephaneLenclud@117
    51
using NETWORKLIST;
sl@25
    52
//
sl@25
    53
using SharpDisplayClient;
sl@55
    54
using SharpDisplay;
StephaneLenclud@135
    55
using MiniDisplayInterop;
StephaneLenclud@171
    56
using SharpLib.Display;
StephaneLenclud@234
    57
using Ear = SharpLib.Ear;
StephaneLenclud@273
    58
StephaneLenclud@124
    59
sl@0
    60
namespace SharpDisplayManager
sl@0
    61
{
sl@58
    62
    //Types declarations
sl@58
    63
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
StephaneLenclud@223
    64
sl@58
    65
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
StephaneLenclud@223
    66
sl@62
    67
    //Delegates are used for our thread safe method
sl@62
    68
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
StephaneLenclud@223
    69
sl@62
    70
    public delegate void RemoveClientDelegate(string aSessionId);
StephaneLenclud@223
    71
sl@79
    72
    public delegate void SetFieldDelegate(string SessionId, DataField aField);
StephaneLenclud@223
    73
sl@79
    74
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
StephaneLenclud@223
    75
sl@62
    76
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
StephaneLenclud@223
    77
sl@62
    78
    public delegate void SetClientNameDelegate(string aSessionId, string aName);
StephaneLenclud@223
    79
StephaneLenclud@184
    80
    public delegate void SetClientPriorityDelegate(string aSessionId, uint aPriority);
StephaneLenclud@223
    81
StephaneLenclud@184
    82
    public delegate void PlainUpdateDelegate();
StephaneLenclud@223
    83
StephaneLenclud@167
    84
    public delegate void WndProcDelegate(ref Message aMessage);
sl@58
    85
sl@58
    86
    /// <summary>
sl@58
    87
    /// Our Display manager main form
sl@58
    88
    /// </summary>
StephaneLenclud@223
    89
    [System.ComponentModel.DesignerCategory("Form")]
StephaneLenclud@226
    90
    public partial class FormMain : FormMainHid, IMMNotificationClient
sl@0
    91
    {
StephaneLenclud@234
    92
        //public Manager iManager = new Manager();        
sl@2
    93
        DateTime LastTickTime;
sl@3
    94
        Display iDisplay;
sl@14
    95
        System.Drawing.Bitmap iBmp;
sl@14
    96
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@17
    97
        ServiceHost iServiceHost;
sl@65
    98
        // Our collection of clients sorted by session id.
sl@33
    99
        public Dictionary<string, ClientData> iClients;
sl@65
   100
        // The name of the client which informations are currently displayed.
sl@65
   101
        public string iCurrentClientSessionId;
sl@65
   102
        ClientData iCurrentClientData;
sl@65
   103
        //
sl@29
   104
        public bool iClosing;
StephaneLenclud@223
   105
        //
StephaneLenclud@223
   106
        public bool iSkipFrameRendering;
sl@65
   107
        //Function pointer for pixel color filtering
sl@58
   108
        ColorProcessingDelegate iColorFx;
sl@65
   109
        //Function pointer for pixel X coordinate intercept
sl@58
   110
        CoordinateTranslationDelegate iScreenX;
sl@65
   111
        //Function pointer for pixel Y coordinate intercept
sl@58
   112
        CoordinateTranslationDelegate iScreenY;
StephaneLenclud@272
   113
        //CSCore
StephaneLenclud@273
   114
        // Volume management
StephaneLenclud@223
   115
        private MMDeviceEnumerator iMultiMediaDeviceEnumerator;
StephaneLenclud@223
   116
        private MMDevice iMultiMediaDevice;
StephaneLenclud@272
   117
        private AudioEndpointVolume iAudioEndpointVolume;
StephaneLenclud@273
   118
        // Audio visualization
StephaneLenclud@273
   119
        private WasapiCapture iSoundIn;
StephaneLenclud@273
   120
        private IWaveSource iWaveSource;
StephaneLenclud@273
   121
        private LineSpectrum iLineSpectrum;
StephaneLenclud@273
   122
StephaneLenclud@223
   123
        //Network
StephaneLenclud@223
   124
        private NetworkManager iNetworkManager;
StephaneLenclud@167
   125
StephaneLenclud@167
   126
        /// <summary>
StephaneLenclud@167
   127
        /// CEC - Consumer Electronic Control.
StephaneLenclud@167
   128
        /// Notably used to turn TV on and off as Windows broadcast monitor on and off notifications.
StephaneLenclud@167
   129
        /// </summary>
StephaneLenclud@167
   130
        private ConsumerElectronicControl iCecManager;
StephaneLenclud@223
   131
StephaneLenclud@223
   132
        /// <summary>
StephaneLenclud@223
   133
        /// Manage run when Windows startup option
StephaneLenclud@223
   134
        /// </summary>
StephaneLenclud@223
   135
        private StartupManager iStartupManager;
StephaneLenclud@223
   136
StephaneLenclud@223
   137
        /// <summary>
StephaneLenclud@223
   138
        /// System notification icon used to hide our application from the task bar.
StephaneLenclud@223
   139
        /// </summary>
StephaneLenclud@223
   140
        private SharpLib.Notification.Control iNotifyIcon;
sl@94
   141
StephaneLenclud@167
   142
        /// <summary>
StephaneLenclud@194
   143
        /// System recording notification icon.
StephaneLenclud@179
   144
        /// </summary>
StephaneLenclud@179
   145
        private SharpLib.Notification.Control iRecordingNotification;
StephaneLenclud@179
   146
Stephane@202
   147
        /// <summary>
Stephane@202
   148
        /// 
Stephane@202
   149
        /// </summary>
StephaneLenclud@253
   150
        RichTextBoxTraceListener iWriter;
Stephane@202
   151
StephaneLenclud@179
   152
StephaneLenclud@179
   153
        /// <summary>
StephaneLenclud@167
   154
        /// Allow user to receive window messages;
StephaneLenclud@167
   155
        /// </summary>
StephaneLenclud@167
   156
        public event WndProcDelegate OnWndProc;
StephaneLenclud@167
   157
StephaneLenclud@226
   158
        public FormMain()
sl@0
   159
        {
StephaneLenclud@235
   160
            if (Properties.Settings.Default.EarManager == null)
Stephane@212
   161
            {
Stephane@212
   162
                //No actions in our settings yet
StephaneLenclud@235
   163
                Properties.Settings.Default.EarManager = new EarManager();
Stephane@212
   164
            }
Stephane@212
   165
            else
Stephane@212
   166
            {
StephaneLenclud@235
   167
                // We loaded events and actions from our settings
StephaneLenclud@235
   168
                // Internalizer apparently skips constructor so we need to initialize it here
StephaneLenclud@235
   169
                // Though I reckon that should only be needed when loading an empty EAR manager I guess.
Stephane@243
   170
                Properties.Settings.Default.EarManager.Construct();
Stephane@212
   171
            }
StephaneLenclud@210
   172
            iSkipFrameRendering = false;
StephaneLenclud@223
   173
            iClosing = false;
sl@65
   174
            iCurrentClientSessionId = "";
sl@65
   175
            iCurrentClientData = null;
sl@2
   176
            LastTickTime = DateTime.Now;
StephaneLenclud@223
   177
            //Instantiate our display and register for events notifications
sl@3
   178
            iDisplay = new Display();
StephaneLenclud@223
   179
            iDisplay.OnOpened += OnDisplayOpened;
StephaneLenclud@223
   180
            iDisplay.OnClosed += OnDisplayClosed;
StephaneLenclud@223
   181
            //
StephaneLenclud@223
   182
            iClients = new Dictionary<string, ClientData>();
StephaneLenclud@223
   183
            iStartupManager = new StartupManager();
StephaneLenclud@223
   184
            iNotifyIcon = new SharpLib.Notification.Control();
StephaneLenclud@179
   185
            iRecordingNotification = new SharpLib.Notification.Control();
sl@2
   186
StephaneLenclud@179
   187
            //Have our designer initialize its controls
sl@0
   188
            InitializeComponent();
StephaneLenclud@104
   189
StephaneLenclud@201
   190
            //Redirect console output
StephaneLenclud@253
   191
            iWriter = new RichTextBoxTraceListener(richTextBoxLogs);
StephaneLenclud@253
   192
            Trace.Listeners.Add(iWriter);
StephaneLenclud@201
   193
StephaneLenclud@201
   194
            //Populate device types
StephaneLenclud@201
   195
            PopulateDeviceTypes();
StephaneLenclud@104
   196
StephaneLenclud@223
   197
            //Initial status update 
sl@7
   198
            UpdateStatus();
StephaneLenclud@104
   199
sl@14
   200
            //We have a bug when drawing minimized and reusing our bitmap
StephaneLenclud@158
   201
            //Though I could not reproduce it on Windows 10
StephaneLenclud@223
   202
            iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
   203
                PixelFormat.Format32bppArgb);
sl@14
   204
            iCreateBitmap = false;
sl@94
   205
StephaneLenclud@223
   206
            //Minimize our window if desired
StephaneLenclud@223
   207
            if (Properties.Settings.Default.StartMinimized)
StephaneLenclud@223
   208
            {
StephaneLenclud@223
   209
                WindowState = FormWindowState.Minimized;
StephaneLenclud@223
   210
            }
sl@94
   211
sl@0
   212
        }
sl@0
   213
StephaneLenclud@223
   214
        /// <summary>
StephaneLenclud@223
   215
        ///
StephaneLenclud@223
   216
        /// </summary>
StephaneLenclud@223
   217
        /// <param name="sender"></param>
StephaneLenclud@223
   218
        /// <param name="e"></param>
StephaneLenclud@106
   219
        private void MainForm_Load(object sender, EventArgs e)
StephaneLenclud@106
   220
        {
StephaneLenclud@223
   221
            //Check if we are running a Click Once deployed application
StephaneLenclud@223
   222
            if (ApplicationDeployment.IsNetworkDeployed)
StephaneLenclud@223
   223
            {
StephaneLenclud@223
   224
                //This is a proper Click Once installation, fetch and show our version number
StephaneLenclud@223
   225
                this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion;
StephaneLenclud@223
   226
            }
StephaneLenclud@223
   227
            else
StephaneLenclud@223
   228
            {
StephaneLenclud@223
   229
                //Not a proper Click Once installation, assuming development build then
StephaneLenclud@223
   230
                this.Text += " - development";
StephaneLenclud@223
   231
            }
StephaneLenclud@223
   232
StephaneLenclud@272
   233
            //CSCore
StephaneLenclud@223
   234
            iMultiMediaDeviceEnumerator = new MMDeviceEnumerator();
StephaneLenclud@223
   235
            iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);
StephaneLenclud@223
   236
            UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@223
   237
StephaneLenclud@223
   238
            //Network
StephaneLenclud@223
   239
            iNetworkManager = new NetworkManager();
StephaneLenclud@223
   240
            iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;
StephaneLenclud@223
   241
            UpdateNetworkStatus();
StephaneLenclud@117
   242
StephaneLenclud@167
   243
            //CEC
StephaneLenclud@167
   244
            iCecManager = new ConsumerElectronicControl();
StephaneLenclud@167
   245
            OnWndProc += iCecManager.OnWndProc;
StephaneLenclud@168
   246
            ResetCec();
StephaneLenclud@168
   247
StephaneLenclud@233
   248
            //Harmony
StephaneLenclud@255
   249
            ResetHarmonyAsync();
StephaneLenclud@233
   250
StephaneLenclud@211
   251
            //Setup Events
StephaneLenclud@260
   252
            PopulateTreeViewEvents();
StephaneLenclud@167
   253
StephaneLenclud@167
   254
            //Setup notification icon
StephaneLenclud@167
   255
            SetupTrayIcon();
StephaneLenclud@106
   256
StephaneLenclud@179
   257
            //Setup recording notification
StephaneLenclud@179
   258
            SetupRecordingNotification();
StephaneLenclud@179
   259
StephaneLenclud@179
   260
            // To make sure start up with minimize to tray works
StephaneLenclud@179
   261
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@223
   262
            {
StephaneLenclud@223
   263
                Visible = false;
StephaneLenclud@223
   264
            }
StephaneLenclud@106
   265
StephaneLenclud@106
   266
#if !DEBUG
StephaneLenclud@223
   267
    //When not debugging we want the screen to be empty until a client takes over
StephaneLenclud@106
   268
			ClearLayout();
StephaneLenclud@106
   269
#else
StephaneLenclud@223
   270
            //When developing we want at least one client for testing
StephaneLenclud@223
   271
            StartNewClient("abcdefghijklmnopqrst-0123456789", "ABCDEFGHIJKLMNOPQRST-0123456789");
StephaneLenclud@106
   272
#endif
StephaneLenclud@106
   273
StephaneLenclud@223
   274
            //Open display connection on start-up if needed
StephaneLenclud@223
   275
            if (Properties.Settings.Default.DisplayConnectOnStartup)
StephaneLenclud@223
   276
            {
StephaneLenclud@223
   277
                OpenDisplayConnection();
StephaneLenclud@223
   278
            }
StephaneLenclud@223
   279
StephaneLenclud@223
   280
            //Start our server so that we can get client requests
StephaneLenclud@223
   281
            StartServer();
StephaneLenclud@223
   282
StephaneLenclud@223
   283
            //Register for HID events
StephaneLenclud@223
   284
            RegisterHidDevices();
StephaneLenclud@194
   285
StephaneLenclud@194
   286
            //Start Idle client if needed
StephaneLenclud@194
   287
            if (Properties.Settings.Default.StartIdleClient)
StephaneLenclud@194
   288
            {
StephaneLenclud@194
   289
                StartIdleClient();
StephaneLenclud@194
   290
            }
StephaneLenclud@106
   291
        }
StephaneLenclud@106
   292
StephaneLenclud@223
   293
        /// <summary>
StephaneLenclud@223
   294
        /// Called when our display is opened.
StephaneLenclud@223
   295
        /// </summary>
StephaneLenclud@223
   296
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   297
        private void OnDisplayOpened(Display aDisplay)
StephaneLenclud@223
   298
        {
StephaneLenclud@187
   299
            //Make sure we resume frame rendering
StephaneLenclud@187
   300
            iSkipFrameRendering = false;
StephaneLenclud@122
   301
StephaneLenclud@223
   302
            //Set our screen size now that our display is connected
StephaneLenclud@223
   303
            //Our panelDisplay is the container of our tableLayoutPanel
StephaneLenclud@223
   304
            //tableLayoutPanel will resize itself to fit the client size of our panelDisplay
StephaneLenclud@223
   305
            //panelDisplay needs an extra 2 pixels for borders on each sides
StephaneLenclud@223
   306
            //tableLayoutPanel will eventually be the exact size of our display
StephaneLenclud@223
   307
            Size size = new Size(iDisplay.WidthInPixels() + 2, iDisplay.HeightInPixels() + 2);
StephaneLenclud@223
   308
            panelDisplay.Size = size;
StephaneLenclud@223
   309
StephaneLenclud@223
   310
            //Our display was just opened, update our UI
StephaneLenclud@223
   311
            UpdateStatus();
StephaneLenclud@223
   312
            //Initiate asynchronous request
StephaneLenclud@223
   313
            iDisplay.RequestFirmwareRevision();
StephaneLenclud@223
   314
StephaneLenclud@223
   315
            //Audio
StephaneLenclud@223
   316
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@223
   317
            //Network
StephaneLenclud@223
   318
            UpdateNetworkStatus();
StephaneLenclud@112
   319
StephaneLenclud@108
   320
#if DEBUG
StephaneLenclud@223
   321
            //Testing icon in debug, no arm done if icon not supported
StephaneLenclud@223
   322
            //iDisplay.SetIconStatus(Display.TMiniDisplayIconType.EMiniDisplayIconRecording, 0, 1);
StephaneLenclud@223
   323
            //iDisplay.SetAllIconsStatus(2);
StephaneLenclud@108
   324
#endif
StephaneLenclud@108
   325
StephaneLenclud@223
   326
        }
StephaneLenclud@104
   327
StephaneLenclud@265
   328
StephaneLenclud@265
   329
        private static void AddActionsToTreeNode(TreeNode aParentNode, Ear.Object aObject)
StephaneLenclud@265
   330
        {
StephaneLenclud@265
   331
            foreach (Ear.Action a in aObject.Objects.OfType<Ear.Action>())
StephaneLenclud@265
   332
            {
StephaneLenclud@265
   333
                //Create action node
StephaneLenclud@265
   334
                TreeNode actionNode = aParentNode.Nodes.Add(a.Brief());
StephaneLenclud@265
   335
                actionNode.Tag = a;
StephaneLenclud@265
   336
                //Use color from parent unless our action itself is disabled
StephaneLenclud@265
   337
                actionNode.ForeColor = a.Enabled ? aParentNode.ForeColor : Color.DimGray;
StephaneLenclud@265
   338
                //Go recursive
StephaneLenclud@265
   339
                AddActionsToTreeNode(actionNode,a);
StephaneLenclud@265
   340
            }
StephaneLenclud@265
   341
        }
StephaneLenclud@265
   342
StephaneLenclud@265
   343
StephaneLenclud@265
   344
        /// <summary>
StephaneLenclud@265
   345
        /// 
StephaneLenclud@265
   346
        /// </summary>
StephaneLenclud@265
   347
        /// <param name="aObject"></param>
StephaneLenclud@265
   348
        /// <param name="aNode"></param>
StephaneLenclud@265
   349
        private static TreeNode FindTreeNodeForEarObject(Ear.Object aObject, TreeNode aNode)
StephaneLenclud@265
   350
        {
StephaneLenclud@265
   351
            if (aNode.Tag == aObject)
StephaneLenclud@265
   352
            {
StephaneLenclud@265
   353
                return aNode;
StephaneLenclud@265
   354
            }
StephaneLenclud@265
   355
StephaneLenclud@265
   356
            foreach (TreeNode n in aNode.Nodes)
StephaneLenclud@265
   357
            {
StephaneLenclud@265
   358
                TreeNode found = FindTreeNodeForEarObject(aObject,n);
StephaneLenclud@265
   359
                if (found != null)
StephaneLenclud@265
   360
                {
StephaneLenclud@265
   361
                    return found;
StephaneLenclud@265
   362
                }
StephaneLenclud@265
   363
            }
StephaneLenclud@265
   364
StephaneLenclud@265
   365
            return null;
StephaneLenclud@265
   366
        }
StephaneLenclud@265
   367
StephaneLenclud@265
   368
StephaneLenclud@265
   369
        /// <summary>
StephaneLenclud@265
   370
        /// 
StephaneLenclud@265
   371
        /// </summary>
StephaneLenclud@265
   372
        /// <param name="aObject"></param>
StephaneLenclud@265
   373
        private void SelectEarObject(Ear.Object aObject)
StephaneLenclud@265
   374
        {
StephaneLenclud@265
   375
            foreach (TreeNode n in iTreeViewEvents.Nodes)
StephaneLenclud@265
   376
            {
StephaneLenclud@265
   377
                TreeNode found = FindTreeNodeForEarObject(aObject, n);
StephaneLenclud@265
   378
                if (found != null)
StephaneLenclud@265
   379
                {
StephaneLenclud@265
   380
                    iTreeViewEvents.SelectedNode=found;
StephaneLenclud@265
   381
                    iTreeViewEvents.Focus();
StephaneLenclud@265
   382
                    return;
StephaneLenclud@265
   383
                }
StephaneLenclud@265
   384
            }
StephaneLenclud@265
   385
        }
StephaneLenclud@265
   386
StephaneLenclud@211
   387
        /// <summary>
StephaneLenclud@218
   388
        /// Populate tree view with events and actions
StephaneLenclud@211
   389
        /// </summary>
StephaneLenclud@266
   390
        private void PopulateTreeViewEvents(Ear.Object aSelectedObject=null)
StephaneLenclud@211
   391
        {
StephaneLenclud@211
   392
            //Reset our tree
StephaneLenclud@211
   393
            iTreeViewEvents.Nodes.Clear();
StephaneLenclud@211
   394
            //Populate registered events
StephaneLenclud@235
   395
            foreach (Ear.Event e in Properties.Settings.Default.EarManager.Events)
StephaneLenclud@211
   396
            {
StephaneLenclud@231
   397
                //Create our event node
StephaneLenclud@260
   398
                //Work out the name of our node
StephaneLenclud@260
   399
                string eventNodeName = "";
StephaneLenclud@260
   400
                if (!string.IsNullOrEmpty(e.Name))
StephaneLenclud@260
   401
                {
StephaneLenclud@260
   402
                    //That event has a proper name, use it then
StephaneLenclud@260
   403
                    eventNodeName = $"{e.Name} - {e.Brief()}";
StephaneLenclud@260
   404
                }
StephaneLenclud@260
   405
                else
StephaneLenclud@260
   406
                {
StephaneLenclud@260
   407
                    //Unnamed events just use brief
StephaneLenclud@260
   408
                    eventNodeName = e.Brief();
StephaneLenclud@260
   409
                }
StephaneLenclud@260
   410
                
StephaneLenclud@260
   411
                TreeNode eventNode = iTreeViewEvents.Nodes.Add(eventNodeName);
StephaneLenclud@231
   412
                eventNode.Tag = e; //For easy access to our event
StephaneLenclud@231
   413
                if (!e.Enabled)
StephaneLenclud@231
   414
                {
StephaneLenclud@231
   415
                    //Dim our nodes if disabled
StephaneLenclud@231
   416
                    eventNode.ForeColor = Color.DimGray;
StephaneLenclud@231
   417
                }
StephaneLenclud@231
   418
StephaneLenclud@231
   419
                //Add event description as child node
StephaneLenclud@260
   420
                eventNode.Nodes.Add(e.AttributeDescription).ForeColor = eventNode.ForeColor; 
StephaneLenclud@231
   421
                //Create child node for actions root
StephaneLenclud@265
   422
                TreeNode actionsNode = eventNode.Nodes.Add("Actions");
StephaneLenclud@265
   423
                actionsNode.ForeColor = eventNode.ForeColor;
StephaneLenclud@265
   424
StephaneLenclud@265
   425
                // Recursively add our actions for that event
StephaneLenclud@265
   426
                AddActionsToTreeNode(actionsNode,e);
StephaneLenclud@211
   427
            }
StephaneLenclud@211
   428
StephaneLenclud@214
   429
            iTreeViewEvents.ExpandAll();
StephaneLenclud@231
   430
StephaneLenclud@266
   431
            if (aSelectedObject != null)
StephaneLenclud@266
   432
            {
StephaneLenclud@266
   433
                SelectEarObject(aSelectedObject);
StephaneLenclud@266
   434
            }            
StephaneLenclud@266
   435
StephaneLenclud@266
   436
            // Just to be safe in case the selection did not work
StephaneLenclud@231
   437
            UpdateEventView();
StephaneLenclud@211
   438
        }
StephaneLenclud@211
   439
StephaneLenclud@223
   440
        /// <summary>
StephaneLenclud@223
   441
        /// Called when our display is closed.
StephaneLenclud@223
   442
        /// </summary>
StephaneLenclud@223
   443
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   444
        private void OnDisplayClosed(Display aDisplay)
StephaneLenclud@223
   445
        {
StephaneLenclud@187
   446
            //Our display was just closed, update our UI consequently
StephaneLenclud@187
   447
            UpdateStatus();
StephaneLenclud@223
   448
        }
StephaneLenclud@223
   449
StephaneLenclud@223
   450
        public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@223
   451
        {
StephaneLenclud@223
   452
            //Update network status
StephaneLenclud@223
   453
            UpdateNetworkStatus();
StephaneLenclud@223
   454
        }
StephaneLenclud@223
   455
StephaneLenclud@223
   456
        /// <summary>
StephaneLenclud@223
   457
        /// Update our Network Status
StephaneLenclud@223
   458
        /// </summary>
StephaneLenclud@223
   459
        private void UpdateNetworkStatus()
StephaneLenclud@223
   460
        {
StephaneLenclud@223
   461
            if (iDisplay.IsOpen())
StephaneLenclud@223
   462
            {
StephaneLenclud@223
   463
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Internet,
StephaneLenclud@223
   464
                    iNetworkManager.NetworkListManager.IsConnectedToInternet);
StephaneLenclud@135
   465
                iDisplay.SetIconOnOff(MiniDisplay.IconType.NetworkSignal, iNetworkManager.NetworkListManager.IsConnected);
StephaneLenclud@223
   466
            }
StephaneLenclud@223
   467
        }
StephaneLenclud@223
   468
StephaneLenclud@223
   469
StephaneLenclud@223
   470
        int iLastNetworkIconIndex = 0;
StephaneLenclud@223
   471
        int iUpdateCountSinceLastNetworkAnimation = 0;
StephaneLenclud@223
   472
StephaneLenclud@223
   473
        /// <summary>
StephaneLenclud@223
   474
        /// 
StephaneLenclud@223
   475
        /// </summary>
StephaneLenclud@223
   476
        private void UpdateNetworkSignal(DateTime aLastTickTime, DateTime aNewTickTime)
StephaneLenclud@223
   477
        {
StephaneLenclud@223
   478
            iUpdateCountSinceLastNetworkAnimation++;
StephaneLenclud@223
   479
            iUpdateCountSinceLastNetworkAnimation = iUpdateCountSinceLastNetworkAnimation%4;
StephaneLenclud@223
   480
StephaneLenclud@223
   481
            if (iDisplay.IsOpen() && iNetworkManager.NetworkListManager.IsConnected &&
StephaneLenclud@223
   482
                iUpdateCountSinceLastNetworkAnimation == 0)
StephaneLenclud@223
   483
            {
StephaneLenclud@135
   484
                int iconCount = iDisplay.IconCount(MiniDisplay.IconType.NetworkSignal);
StephaneLenclud@223
   485
                if (iconCount <= 0)
StephaneLenclud@223
   486
                {
StephaneLenclud@223
   487
                    //Prevents div by zero and other undefined behavior
StephaneLenclud@223
   488
                    return;
StephaneLenclud@223
   489
                }
StephaneLenclud@223
   490
                iLastNetworkIconIndex++;
StephaneLenclud@223
   491
                iLastNetworkIconIndex = iLastNetworkIconIndex%(iconCount*2);
StephaneLenclud@223
   492
                for (int i = 0; i < iconCount; i++)
StephaneLenclud@223
   493
                {
StephaneLenclud@223
   494
                    if (i < iLastNetworkIconIndex && !(i == 0 && iLastNetworkIconIndex > 3) &&
StephaneLenclud@223
   495
                        !(i == 1 && iLastNetworkIconIndex > 4))
StephaneLenclud@223
   496
                    {
StephaneLenclud@135
   497
                        iDisplay.SetIconOn(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   498
                    }
StephaneLenclud@223
   499
                    else
StephaneLenclud@223
   500
                    {
StephaneLenclud@135
   501
                        iDisplay.SetIconOff(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   502
                    }
StephaneLenclud@223
   503
                }
StephaneLenclud@223
   504
            }
StephaneLenclud@223
   505
        }
StephaneLenclud@118
   506
StephaneLenclud@118
   507
StephaneLenclud@118
   508
StephaneLenclud@112
   509
        /// <summary>
StephaneLenclud@112
   510
        /// Receive volume change notification and reflect changes on our slider.
StephaneLenclud@112
   511
        /// </summary>
StephaneLenclud@112
   512
        /// <param name="data"></param>
StephaneLenclud@272
   513
        public void OnVolumeNotificationThreadSafe(object sender, AudioEndpointVolumeCallbackEventArgs aEvent)
StephaneLenclud@112
   514
        {
StephaneLenclud@223
   515
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   516
        }
StephaneLenclud@112
   517
StephaneLenclud@112
   518
        /// <summary>
StephaneLenclud@112
   519
        /// Update master volume when user moves our slider.
StephaneLenclud@112
   520
        /// </summary>
StephaneLenclud@112
   521
        /// <param name="sender"></param>
StephaneLenclud@112
   522
        /// <param name="e"></param>
StephaneLenclud@112
   523
        private void trackBarMasterVolume_Scroll(object sender, EventArgs e)
StephaneLenclud@112
   524
        {
StephaneLenclud@223
   525
            //Just like Windows Volume Mixer we unmute if the volume is adjusted
StephaneLenclud@272
   526
            iAudioEndpointVolume.IsMuted = false;
StephaneLenclud@223
   527
            //Set volume level according to our volume slider new position
StephaneLenclud@272
   528
            iAudioEndpointVolume.MasterVolumeLevelScalar = trackBarMasterVolume.Value/100.0f;
StephaneLenclud@112
   529
        }
StephaneLenclud@112
   530
StephaneLenclud@113
   531
StephaneLenclud@223
   532
        /// <summary>
StephaneLenclud@223
   533
        /// Mute check box changed.
StephaneLenclud@223
   534
        /// </summary>
StephaneLenclud@223
   535
        /// <param name="sender"></param>
StephaneLenclud@223
   536
        /// <param name="e"></param>
StephaneLenclud@223
   537
        private void checkBoxMute_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
   538
        {
StephaneLenclud@272
   539
            iAudioEndpointVolume.IsMuted = checkBoxMute.Checked;
StephaneLenclud@223
   540
        }
StephaneLenclud@113
   541
StephaneLenclud@112
   542
        /// <summary>
StephaneLenclud@112
   543
        /// Device State Changed
StephaneLenclud@112
   544
        /// </summary>
StephaneLenclud@223
   545
        public void OnDeviceStateChanged([MarshalAs(UnmanagedType.LPWStr)] string deviceId,
StephaneLenclud@223
   546
            [MarshalAs(UnmanagedType.I4)] DeviceState newState)
StephaneLenclud@223
   547
        {
StephaneLenclud@223
   548
        }
StephaneLenclud@112
   549
StephaneLenclud@112
   550
        /// <summary>
StephaneLenclud@112
   551
        /// Device Added
StephaneLenclud@112
   552
        /// </summary>
StephaneLenclud@223
   553
        public void OnDeviceAdded([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId)
StephaneLenclud@223
   554
        {
StephaneLenclud@223
   555
        }
StephaneLenclud@112
   556
StephaneLenclud@112
   557
        /// <summary>
StephaneLenclud@112
   558
        /// Device Removed
StephaneLenclud@112
   559
        /// </summary>
StephaneLenclud@223
   560
        public void OnDeviceRemoved([MarshalAs(UnmanagedType.LPWStr)] string deviceId)
StephaneLenclud@223
   561
        {
StephaneLenclud@223
   562
        }
StephaneLenclud@112
   563
StephaneLenclud@112
   564
        /// <summary>
StephaneLenclud@112
   565
        /// Default Device Changed
StephaneLenclud@112
   566
        /// </summary>
StephaneLenclud@223
   567
        public void OnDefaultDeviceChanged(DataFlow flow, Role role,
StephaneLenclud@223
   568
            [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId)
StephaneLenclud@112
   569
        {
StephaneLenclud@112
   570
            if (role == Role.Multimedia && flow == DataFlow.Render)
StephaneLenclud@112
   571
            {
StephaneLenclud@112
   572
                UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@112
   573
            }
StephaneLenclud@112
   574
        }
StephaneLenclud@112
   575
StephaneLenclud@112
   576
        /// <summary>
StephaneLenclud@112
   577
        /// Property Value Changed
StephaneLenclud@112
   578
        /// </summary>
StephaneLenclud@112
   579
        /// <param name="pwstrDeviceId"></param>
StephaneLenclud@112
   580
        /// <param name="key"></param>
StephaneLenclud@223
   581
        public void OnPropertyValueChanged([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId, PropertyKey key)
StephaneLenclud@223
   582
        {
StephaneLenclud@223
   583
        }
StephaneLenclud@223
   584
StephaneLenclud@223
   585
StephaneLenclud@223
   586
StephaneLenclud@223
   587
StephaneLenclud@223
   588
        /// <summary>
StephaneLenclud@223
   589
        /// Update master volume indicators based our current system states.
StephaneLenclud@223
   590
        /// This typically includes volume levels and mute status.
StephaneLenclud@223
   591
        /// </summary>
StephaneLenclud@223
   592
        private void UpdateMasterVolumeThreadSafe()
StephaneLenclud@223
   593
        {
StephaneLenclud@223
   594
            if (this.InvokeRequired)
StephaneLenclud@223
   595
            {
StephaneLenclud@223
   596
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   597
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateMasterVolumeThreadSafe);
StephaneLenclud@223
   598
                this.Invoke(d, new object[] {});
StephaneLenclud@223
   599
                return;
StephaneLenclud@223
   600
            }
StephaneLenclud@223
   601
StephaneLenclud@223
   602
            //Update volume slider
StephaneLenclud@272
   603
            float volumeLevelScalar = iAudioEndpointVolume.MasterVolumeLevelScalar;
StephaneLenclud@223
   604
            trackBarMasterVolume.Value = Convert.ToInt32(volumeLevelScalar*100);
StephaneLenclud@223
   605
            //Update mute checkbox
StephaneLenclud@272
   606
            checkBoxMute.Checked = iAudioEndpointVolume.IsMuted;
StephaneLenclud@223
   607
StephaneLenclud@223
   608
            //If our display connection is open we need to update its icons
StephaneLenclud@223
   609
            if (iDisplay.IsOpen())
StephaneLenclud@223
   610
            {
StephaneLenclud@223
   611
                //First take care our our volume level icons
StephaneLenclud@135
   612
                int volumeIconCount = iDisplay.IconCount(MiniDisplay.IconType.Volume);
StephaneLenclud@223
   613
                if (volumeIconCount > 0)
StephaneLenclud@223
   614
                {
StephaneLenclud@223
   615
                    //Compute current volume level from system level and the number of segments in our display volume bar.
StephaneLenclud@223
   616
                    //That tells us how many segments in our volume bar needs to be turned on.
StephaneLenclud@223
   617
                    float currentVolume = volumeLevelScalar*volumeIconCount;
StephaneLenclud@223
   618
                    int segmentOnCount = Convert.ToInt32(currentVolume);
StephaneLenclud@223
   619
                    //Check if our segment count was rounded up, this will later be used for half brightness segment
StephaneLenclud@223
   620
                    bool roundedUp = segmentOnCount > currentVolume;
StephaneLenclud@223
   621
StephaneLenclud@223
   622
                    for (int i = 0; i < volumeIconCount; i++)
StephaneLenclud@223
   623
                    {
StephaneLenclud@223
   624
                        if (i < segmentOnCount)
StephaneLenclud@223
   625
                        {
StephaneLenclud@223
   626
                            //If we are dealing with our last segment and our segment count was rounded up then we will use half brightness.
StephaneLenclud@223
   627
                            if (i == segmentOnCount - 1 && roundedUp)
StephaneLenclud@223
   628
                            {
StephaneLenclud@223
   629
                                //Half brightness
StephaneLenclud@223
   630
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   631
                                    (iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1)/2);
StephaneLenclud@223
   632
                            }
StephaneLenclud@223
   633
                            else
StephaneLenclud@223
   634
                            {
StephaneLenclud@223
   635
                                //Full brightness
StephaneLenclud@223
   636
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   637
                                    iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1);
StephaneLenclud@223
   638
                            }
StephaneLenclud@223
   639
                        }
StephaneLenclud@223
   640
                        else
StephaneLenclud@223
   641
                        {
StephaneLenclud@135
   642
                            iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, 0);
StephaneLenclud@223
   643
                        }
StephaneLenclud@223
   644
                    }
StephaneLenclud@223
   645
                }
StephaneLenclud@223
   646
StephaneLenclud@223
   647
                //Take care of our mute icon
StephaneLenclud@272
   648
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Mute, iAudioEndpointVolume.IsMuted);
StephaneLenclud@223
   649
            }
StephaneLenclud@223
   650
StephaneLenclud@223
   651
        }
StephaneLenclud@112
   652
StephaneLenclud@112
   653
        /// <summary>
StephaneLenclud@112
   654
        /// 
StephaneLenclud@112
   655
        /// </summary>
StephaneLenclud@273
   656
        private void StartAudioVisualization()
StephaneLenclud@273
   657
        {
StephaneLenclud@273
   658
            StopAudioVisualization();
StephaneLenclud@273
   659
            //Open the default device 
StephaneLenclud@273
   660
            iSoundIn = new WasapiLoopbackCapture();
StephaneLenclud@273
   661
            //Our loopback capture opens the default render device by default so the following is not needed
StephaneLenclud@273
   662
            //iSoundIn.Device = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
StephaneLenclud@273
   663
            iSoundIn.Initialize();
StephaneLenclud@273
   664
StephaneLenclud@273
   665
            SoundInSource soundInSource = new SoundInSource(iSoundIn);
StephaneLenclud@273
   666
            ISampleSource source = soundInSource.ToSampleSource();
StephaneLenclud@273
   667
StephaneLenclud@273
   668
            const FftSize fftSize = FftSize.Fft4096;
StephaneLenclud@273
   669
            //create a spectrum provider which provides fft data based on some input
StephaneLenclud@273
   670
            BasicSpectrumProvider spectrumProvider = new BasicSpectrumProvider(source.WaveFormat.Channels, source.WaveFormat.SampleRate, fftSize);
StephaneLenclud@273
   671
StephaneLenclud@273
   672
            //linespectrum and voiceprint3dspectrum used for rendering some fft data
StephaneLenclud@273
   673
            //in oder to get some fft data, set the previously created spectrumprovider 
StephaneLenclud@273
   674
            iLineSpectrum = new LineSpectrum(fftSize)
StephaneLenclud@273
   675
            {
StephaneLenclud@273
   676
                SpectrumProvider = spectrumProvider,
StephaneLenclud@274
   677
                UseAverage = false,
StephaneLenclud@274
   678
                BarCount = 16,
StephaneLenclud@274
   679
                BarSpacing = 1,
StephaneLenclud@273
   680
                IsXLogScale = true,
StephaneLenclud@274
   681
                ScalingStrategy = ScalingStrategy.Decibel
StephaneLenclud@273
   682
            };
StephaneLenclud@273
   683
StephaneLenclud@273
   684
StephaneLenclud@273
   685
            //the SingleBlockNotificationStream is used to intercept the played samples
StephaneLenclud@273
   686
            var notificationSource = new SingleBlockNotificationStream(source);
StephaneLenclud@273
   687
            //pass the intercepted samples as input data to the spectrumprovider (which will calculate a fft based on them)
StephaneLenclud@273
   688
            notificationSource.SingleBlockRead += (s, a) => spectrumProvider.Add(a.Left, a.Right);
StephaneLenclud@273
   689
StephaneLenclud@273
   690
            iWaveSource = notificationSource.ToWaveSource(16);
StephaneLenclud@273
   691
StephaneLenclud@273
   692
StephaneLenclud@273
   693
            // We need to read from our source otherwise SingleBlockRead is never called and our spectrum provider is not populated
StephaneLenclud@273
   694
            byte[] buffer = new byte[iWaveSource.WaveFormat.BytesPerSecond / 2];
StephaneLenclud@273
   695
            soundInSource.DataAvailable += (s, aEvent) =>
StephaneLenclud@273
   696
            {
StephaneLenclud@273
   697
                int read;
StephaneLenclud@273
   698
                while ((read = iWaveSource.Read(buffer, 0, buffer.Length)) > 0) ;
StephaneLenclud@273
   699
            };
StephaneLenclud@273
   700
StephaneLenclud@273
   701
StephaneLenclud@273
   702
            //Start recording
StephaneLenclud@273
   703
            iSoundIn.Start();
StephaneLenclud@273
   704
        }
StephaneLenclud@273
   705
StephaneLenclud@273
   706
        /// <summary>
StephaneLenclud@273
   707
        /// 
StephaneLenclud@273
   708
        /// </summary>
StephaneLenclud@273
   709
        private void StopAudioVisualization()
StephaneLenclud@273
   710
        {
StephaneLenclud@273
   711
StephaneLenclud@273
   712
            if (iSoundIn != null)
StephaneLenclud@273
   713
            {
StephaneLenclud@273
   714
                iSoundIn.Stop();
StephaneLenclud@273
   715
                iSoundIn.Dispose();
StephaneLenclud@273
   716
                iSoundIn = null;
StephaneLenclud@273
   717
            }
StephaneLenclud@273
   718
            if (iWaveSource != null)
StephaneLenclud@273
   719
            {
StephaneLenclud@273
   720
                iWaveSource.Dispose();
StephaneLenclud@273
   721
                iWaveSource = null;
StephaneLenclud@273
   722
            }
StephaneLenclud@273
   723
StephaneLenclud@273
   724
        }
StephaneLenclud@273
   725
StephaneLenclud@273
   726
StephaneLenclud@273
   727
        /// <summary>
StephaneLenclud@273
   728
        /// 
StephaneLenclud@273
   729
        /// </summary>
StephaneLenclud@274
   730
        private void UpdateAudioVisualization()
StephaneLenclud@273
   731
        {
StephaneLenclud@273
   732
            // For demo draft purposes just fetch the firt picture box control and update it with current audio spectrum
StephaneLenclud@274
   733
StephaneLenclud@274
   734
            if (iCurrentClientData == null)
StephaneLenclud@273
   735
            {
StephaneLenclud@274
   736
                return;
StephaneLenclud@274
   737
            }
StephaneLenclud@274
   738
StephaneLenclud@274
   739
            // Update our math
StephaneLenclud@274
   740
            if (!iLineSpectrum.Update())
StephaneLenclud@274
   741
            {
StephaneLenclud@274
   742
                //Nothing changed no need to render
StephaneLenclud@274
   743
                return;
StephaneLenclud@274
   744
            }
StephaneLenclud@274
   745
StephaneLenclud@274
   746
            // Check if our current client has an Audio Visualizer field
StephaneLenclud@274
   747
            // and render them as needed
StephaneLenclud@274
   748
            foreach (DataField f in iCurrentClientData.Fields)
StephaneLenclud@274
   749
            {
StephaneLenclud@274
   750
                if (f is AudioVisualizerField)
StephaneLenclud@273
   751
                {
StephaneLenclud@274
   752
                    AudioVisualizerField avf = (AudioVisualizerField)f;
StephaneLenclud@274
   753
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(avf.Column, avf.Row);
StephaneLenclud@274
   754
StephaneLenclud@274
   755
                    if (ctrl is PictureBox)
StephaneLenclud@273
   756
                    {
StephaneLenclud@274
   757
                        PictureBox pb = (PictureBox)ctrl;
StephaneLenclud@274
   758
                        Image image = pb.Image;
StephaneLenclud@274
   759
                        // TODO: recycle images
StephaneLenclud@274
   760
                        var newImage = iLineSpectrum.Render(pb.Size, Color.Black, Color.Black, Color.White, false);
StephaneLenclud@274
   761
                        if (newImage != null)
StephaneLenclud@274
   762
                        {
StephaneLenclud@274
   763
                            pb.Image = newImage;
StephaneLenclud@274
   764
                            if (image != null)
StephaneLenclud@274
   765
                                image.Dispose();
StephaneLenclud@274
   766
                        }
StephaneLenclud@273
   767
                    }
StephaneLenclud@273
   768
                }
StephaneLenclud@273
   769
            }
StephaneLenclud@273
   770
        }
StephaneLenclud@273
   771
StephaneLenclud@273
   772
StephaneLenclud@273
   773
        /// <summary>
StephaneLenclud@273
   774
        /// 
StephaneLenclud@273
   775
        /// </summary>
StephaneLenclud@112
   776
        private void UpdateAudioDeviceAndMasterVolumeThreadSafe()
StephaneLenclud@112
   777
        {
StephaneLenclud@112
   778
            if (this.InvokeRequired)
StephaneLenclud@112
   779
            {
StephaneLenclud@112
   780
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   781
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateAudioDeviceAndMasterVolumeThreadSafe);
StephaneLenclud@223
   782
                this.Invoke(d, new object[] {});
StephaneLenclud@112
   783
                return;
StephaneLenclud@112
   784
            }
StephaneLenclud@223
   785
StephaneLenclud@112
   786
            //We are in the correct thread just go ahead.
StephaneLenclud@112
   787
            try
StephaneLenclud@223
   788
            {
StephaneLenclud@272
   789
                //Get our master volume
StephaneLenclud@223
   790
                iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
StephaneLenclud@272
   791
                iAudioEndpointVolume = AudioEndpointVolume.FromDevice(iMultiMediaDevice);
StephaneLenclud@273
   792
                
StephaneLenclud@223
   793
                //Update our label
StephaneLenclud@223
   794
                labelDefaultAudioDevice.Text = iMultiMediaDevice.FriendlyName;
StephaneLenclud@116
   795
StephaneLenclud@112
   796
                //Show our volume in our track bar
StephaneLenclud@223
   797
                UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   798
StephaneLenclud@112
   799
                //Register to get volume modifications
StephaneLenclud@272
   800
                AudioEndpointVolumeCallback callback = new AudioEndpointVolumeCallback();
StephaneLenclud@272
   801
                callback.NotifyRecived += OnVolumeNotificationThreadSafe;
StephaneLenclud@272
   802
                // Do we need to unregister?
StephaneLenclud@273
   803
                iAudioEndpointVolume.RegisterControlChangeNotify(callback);
StephaneLenclud@273
   804
                //
StephaneLenclud@273
   805
                StartAudioVisualization();
StephaneLenclud@112
   806
                //
StephaneLenclud@223
   807
                trackBarMasterVolume.Enabled = true;
StephaneLenclud@112
   808
            }
StephaneLenclud@112
   809
            catch (Exception ex)
StephaneLenclud@112
   810
            {
StephaneLenclud@112
   811
                Debug.WriteLine("Exception thrown in UpdateAudioDeviceAndMasterVolume");
StephaneLenclud@112
   812
                Debug.WriteLine(ex.ToString());
StephaneLenclud@112
   813
                //Something went wrong S/PDIF device ca throw exception I guess
StephaneLenclud@223
   814
                trackBarMasterVolume.Enabled = false;
StephaneLenclud@112
   815
            }
StephaneLenclud@112
   816
        }
StephaneLenclud@104
   817
StephaneLenclud@223
   818
        /// <summary>
StephaneLenclud@223
   819
        /// 
StephaneLenclud@223
   820
        /// </summary>
StephaneLenclud@223
   821
        private void PopulateDeviceTypes()
StephaneLenclud@223
   822
        {
StephaneLenclud@223
   823
            int count = Display.TypeCount();
StephaneLenclud@223
   824
StephaneLenclud@223
   825
            for (int i = 0; i < count; i++)
StephaneLenclud@223
   826
            {
StephaneLenclud@223
   827
                comboBoxDisplayType.Items.Add(Display.TypeName((MiniDisplay.Type) i));
StephaneLenclud@223
   828
            }
StephaneLenclud@223
   829
        }
StephaneLenclud@104
   830
StephaneLenclud@152
   831
        /// <summary>
StephaneLenclud@223
   832
        ///
StephaneLenclud@223
   833
        /// </summary>
StephaneLenclud@223
   834
        private void SetupTrayIcon()
StephaneLenclud@223
   835
        {
StephaneLenclud@223
   836
            iNotifyIcon.Icon = GetIcon("vfd.ico");
StephaneLenclud@223
   837
            iNotifyIcon.Text = "Sharp Display Manager";
StephaneLenclud@223
   838
            iNotifyIcon.Visible = true;
StephaneLenclud@223
   839
StephaneLenclud@223
   840
            //Double click toggles visibility - typically brings up the application
StephaneLenclud@223
   841
            iNotifyIcon.DoubleClick += delegate(object obj, EventArgs args)
StephaneLenclud@223
   842
            {
StephaneLenclud@223
   843
                SysTrayHideShow();
StephaneLenclud@223
   844
            };
StephaneLenclud@223
   845
StephaneLenclud@223
   846
            //Adding a context menu, useful to be able to exit the application
StephaneLenclud@223
   847
            ContextMenu contextMenu = new ContextMenu();
StephaneLenclud@223
   848
            //Context menu item to toggle visibility
StephaneLenclud@223
   849
            MenuItem hideShowItem = new MenuItem("Hide/Show");
StephaneLenclud@223
   850
            hideShowItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   851
            {
StephaneLenclud@223
   852
                SysTrayHideShow();
StephaneLenclud@223
   853
            };
StephaneLenclud@223
   854
            contextMenu.MenuItems.Add(hideShowItem);
StephaneLenclud@223
   855
StephaneLenclud@223
   856
            //Context menu item separator
StephaneLenclud@223
   857
            contextMenu.MenuItems.Add(new MenuItem("-"));
StephaneLenclud@223
   858
StephaneLenclud@223
   859
            //Context menu exit item
StephaneLenclud@223
   860
            MenuItem exitItem = new MenuItem("Exit");
StephaneLenclud@223
   861
            exitItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   862
            {
StephaneLenclud@223
   863
                Application.Exit();
StephaneLenclud@223
   864
            };
StephaneLenclud@223
   865
            contextMenu.MenuItems.Add(exitItem);
StephaneLenclud@223
   866
StephaneLenclud@223
   867
            iNotifyIcon.ContextMenu = contextMenu;
StephaneLenclud@223
   868
        }
sl@95
   869
StephaneLenclud@178
   870
        /// <summary>
StephaneLenclud@179
   871
        ///
StephaneLenclud@179
   872
        /// </summary>
StephaneLenclud@179
   873
        private void SetupRecordingNotification()
StephaneLenclud@179
   874
        {
StephaneLenclud@179
   875
            iRecordingNotification.Icon = GetIcon("record.ico");
StephaneLenclud@179
   876
            iRecordingNotification.Text = "No recording";
StephaneLenclud@180
   877
            iRecordingNotification.Visible = false;
StephaneLenclud@179
   878
        }
StephaneLenclud@179
   879
StephaneLenclud@179
   880
        /// <summary>
StephaneLenclud@178
   881
        /// Access icons from embedded resources.
StephaneLenclud@178
   882
        /// </summary>
StephaneLenclud@178
   883
        /// <param name="aName"></param>
StephaneLenclud@178
   884
        /// <returns></returns>
StephaneLenclud@178
   885
        public static Icon GetIcon(string aName)
StephaneLenclud@223
   886
        {
StephaneLenclud@223
   887
            string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
StephaneLenclud@223
   888
            foreach (string name in names)
StephaneLenclud@223
   889
            {
StephaneLenclud@178
   890
                //Find a resource name that ends with the given name
StephaneLenclud@223
   891
                if (name.EndsWith(aName))
StephaneLenclud@223
   892
                {
StephaneLenclud@223
   893
                    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
StephaneLenclud@223
   894
                    {
StephaneLenclud@223
   895
                        return new Icon(stream);
StephaneLenclud@223
   896
                    }
StephaneLenclud@223
   897
                }
StephaneLenclud@223
   898
            }
StephaneLenclud@223
   899
StephaneLenclud@223
   900
            return null;
StephaneLenclud@223
   901
        }
sl@94
   902
sl@94
   903
sl@65
   904
        /// <summary>
sl@65
   905
        /// Set our current client.
sl@65
   906
        /// This will take care of applying our client layout and set data fields.
sl@65
   907
        /// </summary>
sl@65
   908
        /// <param name="aSessionId"></param>
StephaneLenclud@223
   909
        void SetCurrentClient(string aSessionId, bool aForce = false)
sl@57
   910
        {
sl@65
   911
            if (aSessionId == iCurrentClientSessionId)
sl@57
   912
            {
sl@65
   913
                //Given client is already the current one.
sl@65
   914
                //Don't bother changing anything then.
sl@65
   915
                return;
sl@65
   916
            }
sl@57
   917
StephaneLenclud@185
   918
            ClientData requestedClientData = iClients[aSessionId];
StephaneLenclud@141
   919
StephaneLenclud@141
   920
            //Check when was the last time we switched to that client
StephaneLenclud@142
   921
            if (iCurrentClientData != null)
StephaneLenclud@141
   922
            {
StephaneLenclud@185
   923
                //Do not switch client if priority of current client is higher 
StephaneLenclud@185
   924
                if (!aForce && requestedClientData.Priority < iCurrentClientData.Priority)
StephaneLenclud@185
   925
                {
StephaneLenclud@185
   926
                    return;
StephaneLenclud@185
   927
                }
StephaneLenclud@185
   928
StephaneLenclud@185
   929
StephaneLenclud@142
   930
                double lastSwitchToClientSecondsAgo = (DateTime.Now - iCurrentClientData.LastSwitchTime).TotalSeconds;
StephaneLenclud@142
   931
                //TODO: put that hard coded value as a client property
StephaneLenclud@142
   932
                //Clients should be able to define how often they can be interrupted
StephaneLenclud@142
   933
                //Thus a background client can set this to zero allowing any other client to interrupt at any time
StephaneLenclud@142
   934
                //We could also compute this delay by looking at the requests frequencies?
StephaneLenclud@185
   935
                if (!aForce &&
StephaneLenclud@223
   936
                    requestedClientData.Priority == iCurrentClientData.Priority &&
StephaneLenclud@223
   937
                    //Time sharing is only if clients have the same priority
StephaneLenclud@185
   938
                    (lastSwitchToClientSecondsAgo < 30)) //Make sure a client is on for at least 30 seconds
StephaneLenclud@142
   939
                {
StephaneLenclud@142
   940
                    //Don't switch clients too often
StephaneLenclud@142
   941
                    return;
StephaneLenclud@142
   942
                }
StephaneLenclud@141
   943
            }
StephaneLenclud@141
   944
sl@65
   945
            //Set current client ID.
sl@65
   946
            iCurrentClientSessionId = aSessionId;
StephaneLenclud@141
   947
            //Set the time we last switched to that client
StephaneLenclud@141
   948
            iClients[aSessionId].LastSwitchTime = DateTime.Now;
sl@65
   949
            //Fetch and set current client data.
StephaneLenclud@185
   950
            iCurrentClientData = requestedClientData;
sl@65
   951
            //Apply layout and set data fields.
sl@65
   952
            UpdateTableLayoutPanel(iCurrentClientData);
sl@57
   953
        }
sl@57
   954
sl@0
   955
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
   956
        {
sl@0
   957
            //fontDialog.ShowColor = true;
sl@0
   958
            //fontDialog.ShowApply = true;
sl@0
   959
            fontDialog.ShowEffects = true;
sl@99
   960
            fontDialog.Font = cds.Font;
sl@28
   961
sl@28
   962
            fontDialog.FixedPitchOnly = checkBoxFixedPitchFontOnly.Checked;
sl@28
   963
sl@0
   964
            //fontDialog.ShowHelp = true;
sl@0
   965
sl@0
   966
            //fontDlg.MaxSize = 40;
sl@0
   967
            //fontDlg.MinSize = 22;
sl@0
   968
sl@0
   969
            //fontDialog.Parent = this;
sl@0
   970
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
   971
sl@0
   972
            //DlgBox.ShowDialog(fontDialog);
sl@0
   973
sl@0
   974
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
   975
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
   976
            {
sl@99
   977
                //Set the fonts to all our labels in our layout
StephaneLenclud@175
   978
                foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@99
   979
                {
sl@99
   980
                    if (ctrl is MarqueeLabel)
sl@99
   981
                    {
StephaneLenclud@223
   982
                        ((MarqueeLabel) ctrl).Font = fontDialog.Font;
sl@99
   983
                    }
sl@99
   984
                }
sl@0
   985
sl@99
   986
                //Save font settings
sl@48
   987
                cds.Font = fontDialog.Font;
sl@8
   988
                Properties.Settings.Default.Save();
sl@36
   989
                //
sl@37
   990
                CheckFontHeight();
sl@37
   991
            }
sl@37
   992
        }
sl@36
   993
sl@37
   994
        /// <summary>
sl@38
   995
        ///
sl@37
   996
        /// </summary>
sl@37
   997
        void CheckFontHeight()
sl@37
   998
        {
sl@54
   999
            //Show font height and width
sl@54
  1000
            labelFontHeight.Text = "Font height: " + cds.Font.Height;
sl@54
  1001
            float charWidth = IsFixedWidth(cds.Font);
sl@54
  1002
            if (charWidth == 0.0f)
sl@54
  1003
            {
sl@54
  1004
                labelFontWidth.Visible = false;
sl@54
  1005
            }
sl@54
  1006
            else
sl@54
  1007
            {
sl@54
  1008
                labelFontWidth.Visible = true;
sl@54
  1009
                labelFontWidth.Text = "Font width: " + charWidth;
sl@54
  1010
            }
sl@54
  1011
sl@70
  1012
            MarqueeLabel label = null;
sl@68
  1013
            //Get the first label control we can find
StephaneLenclud@175
  1014
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@68
  1015
            {
sl@68
  1016
                if (ctrl is MarqueeLabel)
sl@68
  1017
                {
StephaneLenclud@223
  1018
                    label = (MarqueeLabel) ctrl;
sl@68
  1019
                    break;
sl@68
  1020
                }
sl@68
  1021
            }
sl@68
  1022
sl@54
  1023
            //Now check font height and show a warning if needed.
sl@68
  1024
            if (label != null && label.Font.Height > label.Height)
sl@37
  1025
            {
StephaneLenclud@223
  1026
                labelWarning.Text = "WARNING: Selected font is too height by " + (label.Font.Height - label.Height) +
StephaneLenclud@223
  1027
                                    " pixels!";
sl@37
  1028
                labelWarning.Visible = true;
sl@0
  1029
            }
sl@37
  1030
            else
sl@37
  1031
            {
sl@37
  1032
                labelWarning.Visible = false;
sl@37
  1033
            }
sl@37
  1034
sl@0
  1035
        }
sl@0
  1036
sl@0
  1037
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
  1038
        {
StephaneLenclud@175
  1039
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
  1040
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
  1041
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
  1042
            bmp.Save("D:\\capture.png");
sl@14
  1043
StephaneLenclud@223
  1044
            ((MarqueeLabel) iTableLayoutPanel.Controls[0]).Text = "Captured";
sl@17
  1045
sl@14
  1046
            /*
sl@14
  1047
            string outputFileName = "d:\\capture.png";
sl@14
  1048
            using (MemoryStream memory = new MemoryStream())
sl@14
  1049
            {
sl@14
  1050
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
  1051
                {
sl@14
  1052
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
  1053
                    byte[] bytes = memory.ToArray();
sl@14
  1054
                    fs.Write(bytes, 0, bytes.Length);
sl@14
  1055
                }
sl@14
  1056
            }
sl@14
  1057
             */
sl@14
  1058
sl@0
  1059
        }
sl@2
  1060
sl@12
  1061
        private void CheckForRequestResults()
sl@12
  1062
        {
sl@12
  1063
            if (iDisplay.IsRequestPending())
sl@12
  1064
            {
sl@12
  1065
                switch (iDisplay.AttemptRequestCompletion())
sl@12
  1066
                {
StephaneLenclud@135
  1067
                    case MiniDisplay.Request.FirmwareRevision:
sl@51
  1068
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@51
  1069
                        //Issue next request then
sl@51
  1070
                        iDisplay.RequestPowerSupplyStatus();
sl@51
  1071
                        break;
sl@51
  1072
StephaneLenclud@135
  1073
                    case MiniDisplay.Request.PowerSupplyStatus:
sl@12
  1074
                        if (iDisplay.PowerSupplyStatus())
sl@12
  1075
                        {
sl@12
  1076
                            toolStripStatusLabelPower.Text = "ON";
sl@12
  1077
                        }
sl@12
  1078
                        else
sl@12
  1079
                        {
sl@12
  1080
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
  1081
                        }
sl@12
  1082
                        //Issue next request then
sl@12
  1083
                        iDisplay.RequestDeviceId();
sl@12
  1084
                        break;
sl@12
  1085
StephaneLenclud@135
  1086
                    case MiniDisplay.Request.DeviceId:
sl@12
  1087
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
  1088
                        //No more request to issue
sl@12
  1089
                        break;
sl@12
  1090
                }
sl@12
  1091
            }
sl@12
  1092
        }
sl@12
  1093
sl@58
  1094
        public static uint ColorWhiteIsOn(int aX, int aY, uint aPixel)
sl@58
  1095
        {
sl@58
  1096
            if ((aPixel & 0x00FFFFFF) == 0x00FFFFFF)
sl@58
  1097
            {
sl@58
  1098
                return 0xFFFFFFFF;
sl@58
  1099
            }
sl@58
  1100
            return 0x00000000;
sl@58
  1101
        }
sl@16
  1102
sl@58
  1103
        public static uint ColorUntouched(int aX, int aY, uint aPixel)
sl@57
  1104
        {
sl@57
  1105
            return aPixel;
sl@57
  1106
        }
sl@57
  1107
sl@58
  1108
        public static uint ColorInversed(int aX, int aY, uint aPixel)
sl@57
  1109
        {
sl@57
  1110
            return ~aPixel;
sl@57
  1111
        }
sl@57
  1112
sl@58
  1113
        public static uint ColorChessboard(int aX, int aY, uint aPixel)
sl@58
  1114
        {
StephaneLenclud@223
  1115
            if ((aX%2 == 0) && (aY%2 == 0))
sl@58
  1116
            {
sl@58
  1117
                return ~aPixel;
sl@58
  1118
            }
StephaneLenclud@223
  1119
            else if ((aX%2 != 0) && (aY%2 != 0))
sl@58
  1120
            {
sl@58
  1121
                return ~aPixel;
sl@58
  1122
            }
sl@58
  1123
            return 0x00000000;
sl@58
  1124
        }
sl@58
  1125
sl@16
  1126
sl@16
  1127
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1128
        {
sl@16
  1129
            return aBmp.Width - aX - 1;
sl@16
  1130
        }
sl@16
  1131
sl@16
  1132
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1133
        {
sl@16
  1134
            return iBmp.Height - aY - 1;
sl@16
  1135
        }
sl@16
  1136
sl@16
  1137
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1138
        {
sl@16
  1139
            return aX;
sl@16
  1140
        }
sl@16
  1141
sl@16
  1142
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1143
        {
sl@16
  1144
            return aY;
sl@16
  1145
        }
sl@16
  1146
sl@58
  1147
        /// <summary>
sl@58
  1148
        /// Select proper pixel delegates according to our current settings.
sl@58
  1149
        /// </summary>
sl@58
  1150
        private void SetupPixelDelegates()
sl@58
  1151
        {
sl@59
  1152
            //Select our pixel processing routine
sl@58
  1153
            if (cds.InverseColors)
sl@58
  1154
            {
sl@58
  1155
                //iColorFx = ColorChessboard;
sl@58
  1156
                iColorFx = ColorInversed;
sl@58
  1157
            }
sl@58
  1158
            else
sl@58
  1159
            {
sl@58
  1160
                iColorFx = ColorWhiteIsOn;
sl@58
  1161
            }
sl@58
  1162
sl@58
  1163
            //Select proper coordinate translation functions
sl@58
  1164
            //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@58
  1165
            if (cds.ReverseScreen)
sl@58
  1166
            {
sl@58
  1167
                iScreenX = ScreenReversedX;
sl@58
  1168
                iScreenY = ScreenReversedY;
sl@58
  1169
            }
sl@58
  1170
            else
sl@58
  1171
            {
sl@58
  1172
                iScreenX = ScreenX;
sl@58
  1173
                iScreenY = ScreenY;
sl@58
  1174
            }
sl@58
  1175
sl@58
  1176
        }
sl@16
  1177
StephaneLenclud@270
  1178
        /// <summary>
StephaneLenclud@270
  1179
        /// This is our timer tick responsible to perform our render
StephaneLenclud@270
  1180
        /// TODO: Use a threading timer instead of a Windows form timer.
StephaneLenclud@270
  1181
        /// </summary>
StephaneLenclud@270
  1182
        /// <param name="sender"></param>
StephaneLenclud@270
  1183
        /// <param name="e"></param>
sl@2
  1184
        private void timer_Tick(object sender, EventArgs e)
sl@14
  1185
        {
sl@2
  1186
            //Update our animations
sl@2
  1187
            DateTime NewTickTime = DateTime.Now;
sl@2
  1188
StephaneLenclud@223
  1189
            UpdateNetworkSignal(LastTickTime, NewTickTime);
StephaneLenclud@118
  1190
sl@60
  1191
            //Update animation for all our marquees
StephaneLenclud@175
  1192
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@60
  1193
            {
sl@68
  1194
                if (ctrl is MarqueeLabel)
sl@68
  1195
                {
StephaneLenclud@223
  1196
                    ((MarqueeLabel) ctrl).UpdateAnimation(LastTickTime, NewTickTime);
sl@68
  1197
                }
sl@60
  1198
            }
sl@60
  1199
sl@4
  1200
            //Update our display
sl@4
  1201
            if (iDisplay.IsOpen())
sl@4
  1202
            {
sl@12
  1203
                CheckForRequestResults();
sl@12
  1204
StephaneLenclud@223
  1205
                //Check if frame rendering is needed
StephaneLenclud@223
  1206
                //Typically used when showing clock
StephaneLenclud@223
  1207
                if (!iSkipFrameRendering)
StephaneLenclud@223
  1208
                {
StephaneLenclud@223
  1209
                    //Draw to bitmap
StephaneLenclud@223
  1210
                    if (iCreateBitmap)
StephaneLenclud@223
  1211
                    {
StephaneLenclud@223
  1212
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
  1213
                            PixelFormat.Format32bppArgb);
StephaneLenclud@158
  1214
                        iCreateBitmap = false;
StephaneLenclud@158
  1215
                    }
StephaneLenclud@223
  1216
                    iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.ClientRectangle);
StephaneLenclud@223
  1217
                    //iBmp.Save("D:\\capture.png");
StephaneLenclud@223
  1218
StephaneLenclud@223
  1219
                    //Send it to our display
StephaneLenclud@223
  1220
                    for (int i = 0; i < iBmp.Width; i++)
StephaneLenclud@223
  1221
                    {
StephaneLenclud@223
  1222
                        for (int j = 0; j < iBmp.Height; j++)
StephaneLenclud@223
  1223
                        {
StephaneLenclud@223
  1224
                            unchecked
StephaneLenclud@223
  1225
                            {
StephaneLenclud@223
  1226
                                //Get our processed pixel coordinates
StephaneLenclud@223
  1227
                                int x = iScreenX(iBmp, i);
StephaneLenclud@223
  1228
                                int y = iScreenY(iBmp, j);
StephaneLenclud@223
  1229
                                //Get pixel color
StephaneLenclud@223
  1230
                                uint color = (uint) iBmp.GetPixel(i, j).ToArgb();
StephaneLenclud@223
  1231
                                //Apply color effects
StephaneLenclud@223
  1232
                                color = iColorFx(x, y, color);
StephaneLenclud@223
  1233
                                //Now set our pixel
StephaneLenclud@223
  1234
                                iDisplay.SetPixel(x, y, color);
StephaneLenclud@223
  1235
                            }
StephaneLenclud@223
  1236
                        }
StephaneLenclud@223
  1237
                    }
StephaneLenclud@223
  1238
StephaneLenclud@223
  1239
                    iDisplay.SwapBuffers();
StephaneLenclud@223
  1240
                }
sl@4
  1241
            }
sl@8
  1242
StephaneLenclud@274
  1243
            UpdateAudioVisualization();
StephaneLenclud@273
  1244
sl@8
  1245
            //Compute instant FPS
StephaneLenclud@274
  1246
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " +
StephaneLenclud@270
  1247
                                           (1000/iTimerDisplay.Interval).ToString() + " FPS";
sl@8
  1248
sl@8
  1249
            LastTickTime = NewTickTime;
sl@8
  1250
sl@2
  1251
        }
sl@3
  1252
StephaneLenclud@223
  1253
        /// <summary>
StephaneLenclud@223
  1254
        /// Attempt to establish connection with our display hardware.
StephaneLenclud@223
  1255
        /// </summary>
sl@46
  1256
        private void OpenDisplayConnection()
sl@3
  1257
        {
sl@46
  1258
            CloseDisplayConnection();
sl@46
  1259
StephaneLenclud@223
  1260
            if (!iDisplay.Open((MiniDisplay.Type) cds.DisplayType))
StephaneLenclud@223
  1261
            {
StephaneLenclud@223
  1262
                UpdateStatus();
StephaneLenclud@223
  1263
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
  1264
            }
sl@46
  1265
        }
sl@7
  1266
sl@46
  1267
        private void CloseDisplayConnection()
sl@46
  1268
        {
StephaneLenclud@223
  1269
            //Status will be updated upon receiving the closed event
StephaneLenclud@223
  1270
StephaneLenclud@223
  1271
            if (iDisplay == null || !iDisplay.IsOpen())
StephaneLenclud@223
  1272
            {
StephaneLenclud@223
  1273
                return;
StephaneLenclud@223
  1274
            }
StephaneLenclud@223
  1275
StephaneLenclud@223
  1276
            //Do not clear if we gave up on rendering already.
StephaneLenclud@223
  1277
            //This means we will keep on displaying clock on MDM166AA for instance.
StephaneLenclud@223
  1278
            if (!iSkipFrameRendering)
StephaneLenclud@223
  1279
            {
StephaneLenclud@223
  1280
                iDisplay.Clear();
StephaneLenclud@223
  1281
                iDisplay.SwapBuffers();
StephaneLenclud@223
  1282
            }
StephaneLenclud@223
  1283
StephaneLenclud@223
  1284
            iDisplay.SetAllIconsStatus(0); //Turn off all icons
sl@46
  1285
            iDisplay.Close();
sl@46
  1286
        }
sl@46
  1287
sl@46
  1288
        private void buttonOpen_Click(object sender, EventArgs e)
sl@46
  1289
        {
sl@46
  1290
            OpenDisplayConnection();
sl@3
  1291
        }
sl@3
  1292
sl@3
  1293
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
  1294
        {
sl@46
  1295
            CloseDisplayConnection();
sl@3
  1296
        }
sl@3
  1297
sl@3
  1298
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
  1299
        {
sl@3
  1300
            iDisplay.Clear();
sl@3
  1301
            iDisplay.SwapBuffers();
sl@3
  1302
        }
sl@3
  1303
sl@3
  1304
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
  1305
        {
sl@3
  1306
            iDisplay.Fill();
sl@3
  1307
            iDisplay.SwapBuffers();
sl@3
  1308
        }
sl@3
  1309
sl@3
  1310
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
  1311
        {
sl@48
  1312
            cds.Brightness = trackBarBrightness.Value;
sl@9
  1313
            Properties.Settings.Default.Save();
sl@3
  1314
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
  1315
sl@3
  1316
        }
sl@7
  1317
sl@48
  1318
sl@48
  1319
        /// <summary>
sl@48
  1320
        /// CDS stands for Current Display Settings
sl@48
  1321
        /// </summary>
sl@50
  1322
        private DisplaySettings cds
sl@48
  1323
        {
sl@48
  1324
            get
sl@48
  1325
            {
sl@65
  1326
                DisplaysSettings settings = Properties.Settings.Default.DisplaysSettings;
sl@51
  1327
                if (settings == null)
sl@51
  1328
                {
sl@51
  1329
                    settings = new DisplaysSettings();
sl@51
  1330
                    settings.Init();
sl@65
  1331
                    Properties.Settings.Default.DisplaysSettings = settings;
sl@51
  1332
                }
sl@48
  1333
sl@48
  1334
                //Make sure all our settings have been created
sl@48
  1335
                while (settings.Displays.Count <= Properties.Settings.Default.CurrentDisplayIndex)
sl@48
  1336
                {
sl@50
  1337
                    settings.Displays.Add(new DisplaySettings());
sl@48
  1338
                }
sl@48
  1339
sl@50
  1340
                DisplaySettings displaySettings = settings.Displays[Properties.Settings.Default.CurrentDisplayIndex];
sl@48
  1341
                return displaySettings;
sl@48
  1342
            }
sl@48
  1343
        }
sl@48
  1344
sl@54
  1345
        /// <summary>
sl@54
  1346
        /// Check if the given font has a fixed character pitch.
sl@54
  1347
        /// </summary>
sl@54
  1348
        /// <param name="ft"></param>
sl@54
  1349
        /// <returns>0.0f if this is not a monospace font, otherwise returns the character width.</returns>
sl@54
  1350
        public float IsFixedWidth(Font ft)
sl@54
  1351
        {
sl@54
  1352
            Graphics g = CreateGraphics();
StephaneLenclud@223
  1353
            char[] charSizes = new char[] {'i', 'a', 'Z', '%', '#', 'a', 'B', 'l', 'm', ',', '.'};
sl@54
  1354
            float charWidth = g.MeasureString("I", ft, Int32.MaxValue, StringFormat.GenericTypographic).Width;
sl@54
  1355
sl@54
  1356
            bool fixedWidth = true;
sl@54
  1357
sl@54
  1358
            foreach (char c in charSizes)
StephaneLenclud@223
  1359
                if (g.MeasureString(c.ToString(), ft, Int32.MaxValue, StringFormat.GenericTypographic).Width !=
StephaneLenclud@223
  1360
                    charWidth)
sl@54
  1361
                    fixedWidth = false;
sl@54
  1362
sl@54
  1363
            if (fixedWidth)
sl@54
  1364
            {
sl@54
  1365
                return charWidth;
sl@54
  1366
            }
sl@54
  1367
sl@54
  1368
            return 0.0f;
sl@54
  1369
        }
sl@54
  1370
StephaneLenclud@223
  1371
        /// <summary>
StephaneLenclud@223
  1372
        /// Synchronize UI with settings
StephaneLenclud@223
  1373
        /// </summary>
sl@7
  1374
        private void UpdateStatus()
StephaneLenclud@223
  1375
        {
sl@48
  1376
            //Load settings
sl@54
  1377
            checkBoxShowBorders.Checked = cds.ShowBorders;
StephaneLenclud@223
  1378
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders
StephaneLenclud@223
  1379
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1380
                : TableLayoutPanelCellBorderStyle.None);
sl@60
  1381
sl@60
  1382
            //Set the proper font to each of our labels
StephaneLenclud@175
  1383
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
sl@60
  1384
            {
sl@60
  1385
                ctrl.Font = cds.Font;
sl@60
  1386
            }
sl@60
  1387
sl@54
  1388
            CheckFontHeight();
StephaneLenclud@223
  1389
            //Check if "run on Windows startup" is enabled
StephaneLenclud@223
  1390
            checkBoxAutoStart.Checked = iStartupManager.Startup;
StephaneLenclud@268
  1391
            
StephaneLenclud@168
  1392
            //CEC settings
StephaneLenclud@168
  1393
            comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
StephaneLenclud@153
  1394
StephaneLenclud@168
  1395
            //Mini Display settings
sl@48
  1396
            checkBoxReverseScreen.Checked = cds.ReverseScreen;
sl@57
  1397
            checkBoxInverseColors.Checked = cds.InverseColors;
StephaneLenclud@223
  1398
            checkBoxShowVolumeLabel.Checked = cds.ShowVolumeLabel;
sl@100
  1399
            checkBoxScaleToFit.Checked = cds.ScaleToFit;
sl@100
  1400
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1401
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1402
            maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
StephaneLenclud@223
  1403
            maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
sl@48
  1404
            comboBoxDisplayType.SelectedIndex = cds.DisplayType;
StephaneLenclud@270
  1405
            iTimerDisplay.Interval = cds.TimerInterval;
sl@48
  1406
            maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
sl@100
  1407
            textBoxScrollLoopSeparator.Text = cds.Separator;
sl@58
  1408
            //
sl@58
  1409
            SetupPixelDelegates();
sl@48
  1410
sl@7
  1411
            if (iDisplay.IsOpen())
sl@7
  1412
            {
StephaneLenclud@187
  1413
                //We have a display connection
StephaneLenclud@187
  1414
                //Reflect that in our UI
StephaneLenclud@187
  1415
                StartTimer();
StephaneLenclud@103
  1416
StephaneLenclud@223
  1417
                iTableLayoutPanel.Enabled = true;
StephaneLenclud@223
  1418
                panelDisplay.Enabled = true;
StephaneLenclud@103
  1419
sl@48
  1420
                //Only setup brightness if display is open
sl@48
  1421
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1422
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@223
  1423
                if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@223
  1424
                {
StephaneLenclud@223
  1425
                    //Brightness out of range, this can occur when using auto-detect
StephaneLenclud@223
  1426
                    //Use max brightness instead
StephaneLenclud@223
  1427
                    trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@223
  1428
                    iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@223
  1429
                }
StephaneLenclud@223
  1430
                else
StephaneLenclud@223
  1431
                {
StephaneLenclud@223
  1432
                    trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@223
  1433
                    iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@223
  1434
                }
StephaneLenclud@223
  1435
StephaneLenclud@223
  1436
                //Try compute the steps to something that makes sense
StephaneLenclud@223
  1437
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@48
  1438
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@223
  1439
sl@48
  1440
                //
sl@7
  1441
                buttonFill.Enabled = true;
sl@7
  1442
                buttonClear.Enabled = true;
sl@7
  1443
                buttonOpen.Enabled = false;
sl@7
  1444
                buttonClose.Enabled = true;
sl@7
  1445
                trackBarBrightness.Enabled = true;
sl@10
  1446
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1447
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1448
sl@52
  1449
                if (iDisplay.SupportPowerOnOff())
sl@52
  1450
                {
sl@52
  1451
                    buttonPowerOn.Enabled = true;
sl@52
  1452
                    buttonPowerOff.Enabled = true;
sl@52
  1453
                }
sl@52
  1454
                else
sl@52
  1455
                {
sl@52
  1456
                    buttonPowerOn.Enabled = false;
sl@52
  1457
                    buttonPowerOff.Enabled = false;
sl@52
  1458
                }
sl@53
  1459
sl@53
  1460
                if (iDisplay.SupportClock())
sl@53
  1461
                {
sl@53
  1462
                    buttonShowClock.Enabled = true;
sl@53
  1463
                    buttonHideClock.Enabled = true;
sl@53
  1464
                }
sl@53
  1465
                else
sl@53
  1466
                {
sl@53
  1467
                    buttonShowClock.Enabled = false;
sl@53
  1468
                    buttonHideClock.Enabled = false;
sl@53
  1469
                }
StephaneLenclud@115
  1470
StephaneLenclud@223
  1471
StephaneLenclud@223
  1472
                //Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@223
  1473
                checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel) > 0;
StephaneLenclud@223
  1474
StephaneLenclud@223
  1475
                if (cds.ShowVolumeLabel)
StephaneLenclud@223
  1476
                {
StephaneLenclud@135
  1477
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1478
                }
StephaneLenclud@223
  1479
                else
StephaneLenclud@223
  1480
                {
StephaneLenclud@135
  1481
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1482
                }
sl@7
  1483
            }
sl@7
  1484
            else
sl@7
  1485
            {
StephaneLenclud@187
  1486
                //Display connection not available
StephaneLenclud@187
  1487
                //Reflect that in our UI
StephaneLenclud@187
  1488
#if DEBUG
StephaneLenclud@187
  1489
                //In debug start our timer even if we don't have a display connection
StephaneLenclud@187
  1490
                StartTimer();
StephaneLenclud@187
  1491
#else
StephaneLenclud@223
  1492
    //In production environment we don't need our timer if no display connection
StephaneLenclud@187
  1493
                StopTimer();
StephaneLenclud@187
  1494
#endif
StephaneLenclud@187
  1495
                checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@223
  1496
                iTableLayoutPanel.Enabled = false;
StephaneLenclud@223
  1497
                panelDisplay.Enabled = false;
sl@7
  1498
                buttonFill.Enabled = false;
sl@7
  1499
                buttonClear.Enabled = false;
sl@7
  1500
                buttonOpen.Enabled = true;
sl@7
  1501
                buttonClose.Enabled = false;
sl@7
  1502
                trackBarBrightness.Enabled = false;
sl@52
  1503
                buttonPowerOn.Enabled = false;
sl@52
  1504
                buttonPowerOff.Enabled = false;
sl@53
  1505
                buttonShowClock.Enabled = false;
sl@53
  1506
                buttonHideClock.Enabled = false;
sl@9
  1507
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1508
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1509
            }
StephaneLenclud@106
  1510
sl@7
  1511
        }
sl@9
  1512
sl@13
  1513
StephaneLenclud@223
  1514
        /// <summary>
StephaneLenclud@223
  1515
        /// 
StephaneLenclud@223
  1516
        /// </summary>
StephaneLenclud@223
  1517
        /// <param name="sender"></param>
StephaneLenclud@223
  1518
        /// <param name="e"></param>
StephaneLenclud@223
  1519
        private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1520
        {
StephaneLenclud@223
  1521
            cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@223
  1522
            Properties.Settings.Default.Save();
StephaneLenclud@223
  1523
            UpdateStatus();
StephaneLenclud@223
  1524
        }
sl@13
  1525
sl@9
  1526
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1527
        {
sl@16
  1528
            //Save our show borders setting
StephaneLenclud@223
  1529
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked
StephaneLenclud@223
  1530
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1531
                : TableLayoutPanelCellBorderStyle.None);
sl@48
  1532
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1533
            Properties.Settings.Default.Save();
sl@57
  1534
            CheckFontHeight();
sl@9
  1535
        }
sl@13
  1536
StephaneLenclud@194
  1537
        private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1538
        {
StephaneLenclud@223
  1539
            iStartupManager.Startup = checkBoxAutoStart.Checked;
StephaneLenclud@223
  1540
        }
sl@94
  1541
sl@94
  1542
sl@16
  1543
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1544
        {
sl@16
  1545
            //Save our reverse screen setting
sl@48
  1546
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1547
            Properties.Settings.Default.Save();
sl@58
  1548
            SetupPixelDelegates();
sl@16
  1549
        }
sl@16
  1550
sl@57
  1551
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1552
        {
sl@57
  1553
            //Save our inverse colors setting
sl@57
  1554
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1555
            Properties.Settings.Default.Save();
sl@58
  1556
            SetupPixelDelegates();
sl@57
  1557
        }
sl@57
  1558
sl@100
  1559
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1560
        {
sl@100
  1561
            //Save our scale to fit setting
sl@100
  1562
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1563
            Properties.Settings.Default.Save();
sl@100
  1564
            //
sl@100
  1565
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1566
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1567
        }
sl@100
  1568
sl@14
  1569
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1570
        {
sl@14
  1571
            if (WindowState == FormWindowState.Minimized)
sl@14
  1572
            {
StephaneLenclud@158
  1573
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1574
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1575
                iCreateBitmap = true;
sl@14
  1576
            }
sl@14
  1577
        }
sl@14
  1578
sl@17
  1579
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1580
        {
StephaneLenclud@273
  1581
            //TODO: discard other CSCore audio objects
StephaneLenclud@273
  1582
            StopAudioVisualization();
StephaneLenclud@167
  1583
            iCecManager.Stop();
StephaneLenclud@223
  1584
            iNetworkManager.Dispose();
StephaneLenclud@223
  1585
            CloseDisplayConnection();
sl@17
  1586
            StopServer();
sl@32
  1587
            e.Cancel = iClosing;
sl@17
  1588
        }
sl@17
  1589
sl@17
  1590
        public void StartServer()
sl@17
  1591
        {
sl@17
  1592
            iServiceHost = new ServiceHost
sl@17
  1593
                (
StephaneLenclud@223
  1594
                typeof(Session),
StephaneLenclud@223
  1595
                new Uri[] {new Uri("net.tcp://localhost:8001/")}
sl@17
  1596
                );
sl@17
  1597
StephaneLenclud@223
  1598
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true),
StephaneLenclud@223
  1599
                "DisplayService");
sl@17
  1600
            iServiceHost.Open();
sl@17
  1601
        }
sl@17
  1602
sl@17
  1603
        public void StopServer()
sl@17
  1604
        {
sl@32
  1605
            if (iClients.Count > 0 && !iClosing)
sl@29
  1606
            {
sl@29
  1607
                //Tell our clients
sl@32
  1608
                iClosing = true;
sl@29
  1609
                BroadcastCloseEvent();
sl@29
  1610
            }
sl@32
  1611
            else if (iClosing)
sl@32
  1612
            {
StephaneLenclud@223
  1613
                if (
StephaneLenclud@223
  1614
                    MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo,
StephaneLenclud@223
  1615
                        MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1616
                {
sl@32
  1617
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1618
                }
sl@32
  1619
            }
sl@32
  1620
            else
sl@36
  1621
            {
sl@32
  1622
                //We removed that as it often lags for some reason
sl@32
  1623
                //iServiceHost.Close();
sl@32
  1624
            }
sl@17
  1625
        }
sl@17
  1626
sl@21
  1627
        public void BroadcastCloseEvent()
sl@21
  1628
        {
sl@31
  1629
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1630
sl@21
  1631
            var inactiveClients = new List<string>();
sl@21
  1632
            foreach (var client in iClients)
sl@21
  1633
            {
sl@21
  1634
                //if (client.Key != eventData.ClientName)
sl@21
  1635
                {
sl@21
  1636
                    try
sl@21
  1637
                    {
sl@31
  1638
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
StephaneLenclud@223
  1639
                        client.Value.Callback.OnCloseOrder( /*eventData*/);
sl@21
  1640
                    }
sl@21
  1641
                    catch (Exception ex)
sl@21
  1642
                    {
sl@21
  1643
                        inactiveClients.Add(client.Key);
sl@21
  1644
                    }
sl@21
  1645
                }
sl@21
  1646
            }
sl@21
  1647
sl@21
  1648
            if (inactiveClients.Count > 0)
sl@21
  1649
            {
sl@21
  1650
                foreach (var client in inactiveClients)
sl@21
  1651
                {
sl@21
  1652
                    iClients.Remove(client);
StephaneLenclud@226
  1653
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1654
                        Program.iFormMain.iTreeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1655
                }
sl@21
  1656
            }
sl@97
  1657
StephaneLenclud@223
  1658
            if (iClients.Count == 0)
StephaneLenclud@223
  1659
            {
StephaneLenclud@223
  1660
                ClearLayout();
StephaneLenclud@223
  1661
            }
sl@21
  1662
        }
sl@21
  1663
StephaneLenclud@223
  1664
        /// <summary>
StephaneLenclud@223
  1665
        /// Just remove all our fields.
StephaneLenclud@223
  1666
        /// </summary>
StephaneLenclud@223
  1667
        private void ClearLayout()
StephaneLenclud@223
  1668
        {
StephaneLenclud@223
  1669
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@223
  1670
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@223
  1671
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@223
  1672
            iCurrentClientData = null;
StephaneLenclud@223
  1673
        }
StephaneLenclud@223
  1674
StephaneLenclud@223
  1675
        /// <summary>
StephaneLenclud@223
  1676
        /// Just launch a demo client.
StephaneLenclud@223
  1677
        /// </summary>
StephaneLenclud@223
  1678
        private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@223
  1679
        {
StephaneLenclud@223
  1680
            Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@223
  1681
            SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(
StephaneLenclud@223
  1682
                new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@223
  1683
            clientThread.Start(myParams);
StephaneLenclud@223
  1684
            BringToFront();
StephaneLenclud@223
  1685
        }
StephaneLenclud@106
  1686
StephaneLenclud@189
  1687
        /// <summary>
StephaneLenclud@189
  1688
        /// Just launch our idle client.
StephaneLenclud@189
  1689
        /// </summary>
StephaneLenclud@189
  1690
        private void StartIdleClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@189
  1691
        {
StephaneLenclud@225
  1692
            Thread clientThread = new Thread(SharpDisplayClientIdle.Program.MainWithParams);
StephaneLenclud@225
  1693
            SharpDisplayClientIdle.StartParams myParams =
StephaneLenclud@225
  1694
                new SharpDisplayClientIdle.StartParams(new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@189
  1695
            clientThread.Start(myParams);
StephaneLenclud@189
  1696
            BringToFront();
StephaneLenclud@189
  1697
        }
StephaneLenclud@189
  1698
StephaneLenclud@189
  1699
sl@25
  1700
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1701
        {
StephaneLenclud@223
  1702
            StartNewClient();
sl@25
  1703
        }
sl@25
  1704
sl@27
  1705
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1706
        {
StephaneLenclud@187
  1707
            ToggleTimer();
StephaneLenclud@187
  1708
        }
StephaneLenclud@187
  1709
StephaneLenclud@187
  1710
        private void StartTimer()
StephaneLenclud@187
  1711
        {
StephaneLenclud@187
  1712
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1713
            iTimerDisplay.Enabled = true;
StephaneLenclud@187
  1714
            UpdateSuspendButton();
StephaneLenclud@187
  1715
        }
StephaneLenclud@187
  1716
StephaneLenclud@187
  1717
        private void StopTimer()
StephaneLenclud@187
  1718
        {
StephaneLenclud@187
  1719
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1720
            iTimerDisplay.Enabled = false;
StephaneLenclud@187
  1721
            UpdateSuspendButton();
StephaneLenclud@187
  1722
        }
StephaneLenclud@187
  1723
StephaneLenclud@187
  1724
        private void ToggleTimer()
StephaneLenclud@187
  1725
        {
sl@52
  1726
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1727
            iTimerDisplay.Enabled = !iTimerDisplay.Enabled;
StephaneLenclud@187
  1728
            UpdateSuspendButton();
StephaneLenclud@187
  1729
        }
StephaneLenclud@187
  1730
StephaneLenclud@187
  1731
        private void UpdateSuspendButton()
StephaneLenclud@187
  1732
        {
StephaneLenclud@270
  1733
            if (!iTimerDisplay.Enabled)
sl@27
  1734
            {
sl@52
  1735
                buttonSuspend.Text = "Run";
sl@27
  1736
            }
sl@27
  1737
            else
sl@27
  1738
            {
sl@27
  1739
                buttonSuspend.Text = "Pause";
sl@27
  1740
            }
sl@27
  1741
        }
sl@27
  1742
StephaneLenclud@187
  1743
sl@29
  1744
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1745
        {
sl@29
  1746
            BroadcastCloseEvent();
sl@29
  1747
        }
sl@29
  1748
sl@30
  1749
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1750
        {
StephaneLenclud@141
  1751
            //Root node must have at least one child
StephaneLenclud@141
  1752
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1753
            {
StephaneLenclud@141
  1754
                return;
StephaneLenclud@141
  1755
            }
sl@21
  1756
StephaneLenclud@141
  1757
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@223
  1758
            string sessionId = e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1759
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1760
            {
StephaneLenclud@141
  1761
                //We have a valid session just switch to that client
StephaneLenclud@223
  1762
                SetCurrentClient(sessionId, true);
StephaneLenclud@141
  1763
            }
StephaneLenclud@223
  1764
sl@30
  1765
        }
sl@30
  1766
sl@36
  1767
sl@30
  1768
        /// <summary>
sl@36
  1769
        ///
sl@30
  1770
        /// </summary>
sl@30
  1771
        /// <param name="aSessionId"></param>
sl@30
  1772
        /// <param name="aCallback"></param>
sl@55
  1773
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1774
        {
sl@33
  1775
            if (this.InvokeRequired)
sl@30
  1776
            {
sl@30
  1777
                //Not in the proper thread, invoke ourselves
sl@30
  1778
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
StephaneLenclud@223
  1779
                this.Invoke(d, new object[] {aSessionId, aCallback});
sl@30
  1780
            }
sl@30
  1781
            else
sl@30
  1782
            {
sl@30
  1783
                //We are in the proper thread
sl@30
  1784
                //Add this session to our collection of clients
sl@33
  1785
                ClientData newClient = new ClientData(aSessionId, aCallback);
StephaneLenclud@226
  1786
                Program.iFormMain.iClients.Add(aSessionId, newClient);
sl@30
  1787
                //Add this session to our UI
sl@33
  1788
                UpdateClientTreeViewNode(newClient);
sl@30
  1789
            }
sl@30
  1790
        }
sl@30
  1791
Stephane@186
  1792
Stephane@186
  1793
        /// <summary>
Stephane@186
  1794
        /// Find the client with the highest priority if any.
Stephane@186
  1795
        /// </summary>
Stephane@186
  1796
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1797
        public ClientData FindHighestPriorityClient()
Stephane@186
  1798
        {
Stephane@186
  1799
            ClientData highestPriorityClient = null;
Stephane@186
  1800
            foreach (var client in iClients)
Stephane@186
  1801
            {
Stephane@186
  1802
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1803
                {
Stephane@186
  1804
                    highestPriorityClient = client.Value;
Stephane@186
  1805
                }
Stephane@186
  1806
            }
Stephane@186
  1807
Stephane@186
  1808
            return highestPriorityClient;
Stephane@186
  1809
        }
Stephane@186
  1810
sl@30
  1811
        /// <summary>
sl@36
  1812
        ///
sl@30
  1813
        /// </summary>
sl@30
  1814
        /// <param name="aSessionId"></param>
sl@30
  1815
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1816
        {
sl@33
  1817
            if (this.InvokeRequired)
sl@30
  1818
            {
sl@30
  1819
                //Not in the proper thread, invoke ourselves
sl@30
  1820
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
StephaneLenclud@223
  1821
                this.Invoke(d, new object[] {aSessionId});
sl@30
  1822
            }
sl@30
  1823
            else
sl@30
  1824
            {
sl@30
  1825
                //We are in the proper thread
sl@33
  1826
                //Remove this session from both client collection and UI tree view
StephaneLenclud@226
  1827
                if (Program.iFormMain.iClients.Keys.Contains(aSessionId))
sl@30
  1828
                {
StephaneLenclud@226
  1829
                    Program.iFormMain.iClients.Remove(aSessionId);
StephaneLenclud@226
  1830
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1831
                        Program.iFormMain.iTreeViewClients.Nodes.Find(aSessionId, false)[0]);
StephaneLenclud@188
  1832
                    //Update recording status too whenever a client is removed
StephaneLenclud@188
  1833
                    UpdateRecordingNotification();
sl@32
  1834
                }
sl@32
  1835
Stephane@186
  1836
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1837
                {
Stephane@186
  1838
                    //The current client is closing
Stephane@186
  1839
                    iCurrentClientData = null;
Stephane@186
  1840
                    //Find the client with the highest priority and set it as current
Stephane@186
  1841
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  1842
                    if (newCurrentClient != null)
Stephane@186
  1843
                    {
Stephane@186
  1844
                        SetCurrentClient(newCurrentClient.SessionId, true);
StephaneLenclud@223
  1845
                    }
Stephane@186
  1846
                }
Stephane@186
  1847
Stephane@186
  1848
                if (iClients.Count == 0)
StephaneLenclud@223
  1849
                {
StephaneLenclud@223
  1850
                    //Clear our screen when last client disconnects
StephaneLenclud@223
  1851
                    ClearLayout();
StephaneLenclud@223
  1852
StephaneLenclud@223
  1853
                    if (iClosing)
StephaneLenclud@223
  1854
                    {
StephaneLenclud@223
  1855
                        //We were closing our form
StephaneLenclud@223
  1856
                        //All clients are now closed
StephaneLenclud@223
  1857
                        //Just resume our close operation
StephaneLenclud@223
  1858
                        iClosing = false;
StephaneLenclud@223
  1859
                        Close();
StephaneLenclud@223
  1860
                    }
StephaneLenclud@223
  1861
                }
sl@30
  1862
            }
sl@30
  1863
        }
sl@30
  1864
sl@30
  1865
        /// <summary>
sl@36
  1866
        ///
sl@30
  1867
        /// </summary>
sl@62
  1868
        /// <param name="aSessionId"></param>
sl@72
  1869
        /// <param name="aLayout"></param>
sl@62
  1870
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1871
        {
sl@62
  1872
            if (this.InvokeRequired)
sl@62
  1873
            {
sl@62
  1874
                //Not in the proper thread, invoke ourselves
sl@62
  1875
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
StephaneLenclud@223
  1876
                this.Invoke(d, new object[] {aSessionId, aLayout});
sl@62
  1877
            }
sl@62
  1878
            else
sl@62
  1879
            {
sl@62
  1880
                ClientData client = iClients[aSessionId];
sl@62
  1881
                if (client != null)
sl@62
  1882
                {
StephaneLenclud@148
  1883
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1884
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1885
                    {
StephaneLenclud@158
  1886
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1887
                        //Set our client layout then
StephaneLenclud@148
  1888
                        client.Layout = aLayout;
StephaneLenclud@175
  1889
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1890
                        client.HasNewLayout = true;
StephaneLenclud@158
  1891
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1892
                        client.Fields.Clear();
StephaneLenclud@148
  1893
                        //
StephaneLenclud@148
  1894
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1895
                    }
StephaneLenclud@158
  1896
                    else
StephaneLenclud@158
  1897
                    {
StephaneLenclud@158
  1898
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1899
                    }
sl@62
  1900
                }
sl@62
  1901
            }
sl@62
  1902
        }
sl@62
  1903
sl@62
  1904
        /// <summary>
sl@62
  1905
        ///
sl@62
  1906
        /// </summary>
sl@67
  1907
        /// <param name="aSessionId"></param>
sl@72
  1908
        /// <param name="aField"></param>
sl@75
  1909
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1910
        {
sl@33
  1911
            if (this.InvokeRequired)
sl@30
  1912
            {
sl@30
  1913
                //Not in the proper thread, invoke ourselves
sl@79
  1914
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
StephaneLenclud@223
  1915
                this.Invoke(d, new object[] {aSessionId, aField});
sl@30
  1916
            }
sl@30
  1917
            else
sl@30
  1918
            {
sl@75
  1919
                //We are in the proper thread
sl@75
  1920
                //Call the non-thread-safe variant
sl@75
  1921
                SetClientField(aSessionId, aField);
sl@75
  1922
            }
sl@75
  1923
        }
sl@75
  1924
StephaneLenclud@176
  1925
StephaneLenclud@176
  1926
StephaneLenclud@176
  1927
sl@75
  1928
        /// <summary>
StephaneLenclud@175
  1929
        /// Set a data field in the given client.
sl@75
  1930
        /// </summary>
sl@75
  1931
        /// <param name="aSessionId"></param>
sl@75
  1932
        /// <param name="aField"></param>
sl@75
  1933
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@223
  1934
        {
StephaneLenclud@141
  1935
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1936
sl@75
  1937
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1938
            bool layoutChanged = false;
StephaneLenclud@148
  1939
            bool contentChanged = true;
StephaneLenclud@141
  1940
StephaneLenclud@176
  1941
            //Fetch our field index
StephaneLenclud@176
  1942
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1943
StephaneLenclud@176
  1944
            if (fieldIndex < 0)
sl@75
  1945
            {
StephaneLenclud@176
  1946
                //No corresponding field, just bail out
StephaneLenclud@176
  1947
                return;
StephaneLenclud@141
  1948
            }
sl@76
  1949
StephaneLenclud@175
  1950
            //Keep our previous field in there
StephaneLenclud@176
  1951
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1952
            //Just update that field then 
StephaneLenclud@176
  1953
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1954
StephaneLenclud@176
  1955
            if (!aField.IsTableField)
StephaneLenclud@176
  1956
            {
StephaneLenclud@176
  1957
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1958
                return;
StephaneLenclud@176
  1959
            }
StephaneLenclud@176
  1960
StephaneLenclud@176
  1961
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1962
StephaneLenclud@175
  1963
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1964
            {
StephaneLenclud@141
  1965
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1966
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1967
                {
StephaneLenclud@223
  1968
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1969
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1970
                    {
StephaneLenclud@274
  1971
                        TextField textField = (TextField)aField;
sl@75
  1972
                        //Text field control already in place, just change the text
StephaneLenclud@274
  1973
                        MarqueeLabel label = (MarqueeLabel)ctrl;
StephaneLenclud@172
  1974
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1975
                        label.Text = textField.Text;
StephaneLenclud@172
  1976
                        label.TextAlign = textField.Alignment;
sl@68
  1977
                    }
StephaneLenclud@176
  1978
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1979
                    {
StephaneLenclud@274
  1980
                        BitmapField bitmapField = (BitmapField)aField;
StephaneLenclud@148
  1981
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1982
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@274
  1983
                        PictureBox pictureBox = (PictureBox)ctrl;
StephaneLenclud@172
  1984
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1985
                    }
StephaneLenclud@274
  1986
                    else if (aField is AudioVisualizerField && ctrl is PictureBox)
StephaneLenclud@274
  1987
                    {
StephaneLenclud@274
  1988
                        contentChanged = false; // Since nothing was changed
StephaneLenclud@274
  1989
                    }
sl@68
  1990
                    else
sl@68
  1991
                    {
StephaneLenclud@141
  1992
                        layoutChanged = true;
sl@68
  1993
                    }
sl@30
  1994
                }
StephaneLenclud@141
  1995
            }
StephaneLenclud@141
  1996
            else
StephaneLenclud@223
  1997
            {
StephaneLenclud@141
  1998
                layoutChanged = true;
StephaneLenclud@141
  1999
            }
StephaneLenclud@141
  2000
StephaneLenclud@148
  2001
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  2002
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  2003
            {
StephaneLenclud@141
  2004
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  2005
                //
StephaneLenclud@148
  2006
                if (layoutChanged)
sl@75
  2007
                {
StephaneLenclud@148
  2008
                    Debug.Print("Layout changed");
StephaneLenclud@148
  2009
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  2010
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  2011
                    {
StephaneLenclud@148
  2012
                        //Apply layout and set data fields.
StephaneLenclud@148
  2013
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  2014
                    }
sl@75
  2015
                }
StephaneLenclud@158
  2016
                else
StephaneLenclud@158
  2017
                {
StephaneLenclud@158
  2018
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  2019
                }
StephaneLenclud@158
  2020
            }
StephaneLenclud@158
  2021
            else
StephaneLenclud@158
  2022
            {
StephaneLenclud@158
  2023
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  2024
            }
sl@75
  2025
StephaneLenclud@141
  2026
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  2027
            SetCurrentClient(aSessionId);
sl@30
  2028
        }
sl@30
  2029
sl@30
  2030
        /// <summary>
sl@36
  2031
        ///
sl@30
  2032
        /// </summary>
sl@30
  2033
        /// <param name="aTexts"></param>
sl@75
  2034
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  2035
        {
sl@33
  2036
            if (this.InvokeRequired)
sl@30
  2037
            {
sl@30
  2038
                //Not in the proper thread, invoke ourselves
sl@75
  2039
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
StephaneLenclud@223
  2040
                this.Invoke(d, new object[] {aSessionId, aFields});
sl@30
  2041
            }
sl@30
  2042
            else
sl@30
  2043
            {
StephaneLenclud@175
  2044
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  2045
StephaneLenclud@175
  2046
                if (client.HasNewLayout)
sl@30
  2047
                {
StephaneLenclud@175
  2048
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  2049
                    //Our layout was just changed
StephaneLenclud@175
  2050
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  2051
                    client.HasNewLayout = false;
StephaneLenclud@175
  2052
                    //Just set all our fields then
StephaneLenclud@175
  2053
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  2054
                    //Try switch to that client
StephaneLenclud@175
  2055
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  2056
StephaneLenclud@175
  2057
                    //If we are updating the current client update our panel
StephaneLenclud@175
  2058
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  2059
                    {
StephaneLenclud@175
  2060
                        //Apply layout and set data fields.
StephaneLenclud@175
  2061
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  2062
                    }
StephaneLenclud@176
  2063
StephaneLenclud@176
  2064
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  2065
                }
StephaneLenclud@175
  2066
                else
StephaneLenclud@175
  2067
                {
StephaneLenclud@175
  2068
                    //Put each our text fields in a label control
StephaneLenclud@175
  2069
                    foreach (DataField field in aFields)
StephaneLenclud@175
  2070
                    {
StephaneLenclud@175
  2071
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  2072
                    }
sl@30
  2073
                }
sl@30
  2074
            }
sl@32
  2075
        }
sl@30
  2076
sl@67
  2077
        /// <summary>
sl@67
  2078
        ///
sl@67
  2079
        /// </summary>
sl@67
  2080
        /// <param name="aSessionId"></param>
sl@32
  2081
        /// <param name="aName"></param>
sl@32
  2082
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  2083
        {
sl@32
  2084
            if (this.InvokeRequired)
sl@32
  2085
            {
sl@32
  2086
                //Not in the proper thread, invoke ourselves
sl@32
  2087
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
StephaneLenclud@223
  2088
                this.Invoke(d, new object[] {aSessionId, aName});
sl@32
  2089
            }
sl@32
  2090
            else
sl@32
  2091
            {
sl@32
  2092
                //We are in the proper thread
sl@33
  2093
                //Get our client
sl@33
  2094
                ClientData client = iClients[aSessionId];
sl@33
  2095
                if (client != null)
sl@32
  2096
                {
sl@33
  2097
                    //Set its name
sl@33
  2098
                    client.Name = aName;
sl@33
  2099
                    //Update our tree-view
sl@33
  2100
                    UpdateClientTreeViewNode(client);
sl@33
  2101
                }
sl@33
  2102
            }
sl@33
  2103
        }
sl@33
  2104
StephaneLenclud@184
  2105
        ///
StephaneLenclud@184
  2106
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  2107
        {
StephaneLenclud@184
  2108
            if (this.InvokeRequired)
StephaneLenclud@184
  2109
            {
StephaneLenclud@184
  2110
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  2111
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@223
  2112
                this.Invoke(d, new object[] {aSessionId, aPriority});
StephaneLenclud@184
  2113
            }
StephaneLenclud@184
  2114
            else
StephaneLenclud@184
  2115
            {
StephaneLenclud@184
  2116
                //We are in the proper thread
StephaneLenclud@184
  2117
                //Get our client
StephaneLenclud@184
  2118
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  2119
                if (client != null)
StephaneLenclud@184
  2120
                {
StephaneLenclud@184
  2121
                    //Set its name
StephaneLenclud@184
  2122
                    client.Priority = aPriority;
StephaneLenclud@184
  2123
                    //Update our tree-view
StephaneLenclud@184
  2124
                    UpdateClientTreeViewNode(client);
Stephane@186
  2125
                    //Change our current client as per new priority
Stephane@186
  2126
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  2127
                    if (newCurrentClient != null)
Stephane@186
  2128
                    {
Stephane@186
  2129
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  2130
                    }
StephaneLenclud@184
  2131
                }
StephaneLenclud@184
  2132
            }
StephaneLenclud@184
  2133
        }
StephaneLenclud@184
  2134
sl@33
  2135
        /// <summary>
StephaneLenclud@183
  2136
        /// 
StephaneLenclud@183
  2137
        /// </summary>
StephaneLenclud@183
  2138
        /// <param name="value"></param>
StephaneLenclud@183
  2139
        /// <param name="maxChars"></param>
StephaneLenclud@183
  2140
        /// <returns></returns>
StephaneLenclud@183
  2141
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  2142
        {
StephaneLenclud@223
  2143
            return value.Length <= maxChars ? value : value.Substring(0, maxChars - 3) + "...";
StephaneLenclud@183
  2144
        }
StephaneLenclud@183
  2145
StephaneLenclud@183
  2146
        /// <summary>
StephaneLenclud@180
  2147
        /// Update our recording notification.
StephaneLenclud@180
  2148
        /// </summary>
StephaneLenclud@180
  2149
        private void UpdateRecordingNotification()
StephaneLenclud@180
  2150
        {
StephaneLenclud@180
  2151
            //Go through each 
StephaneLenclud@180
  2152
            bool activeRecording = false;
StephaneLenclud@223
  2153
            string text = "";
StephaneLenclud@223
  2154
            RecordingField recField = new RecordingField();
StephaneLenclud@180
  2155
            foreach (var client in iClients)
StephaneLenclud@180
  2156
            {
StephaneLenclud@223
  2157
                RecordingField rec = (RecordingField) client.Value.FindSameFieldAs(recField);
StephaneLenclud@223
  2158
                if (rec != null && rec.IsActive)
StephaneLenclud@180
  2159
                {
StephaneLenclud@180
  2160
                    activeRecording = true;
StephaneLenclud@183
  2161
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  2162
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  2163
                    {
StephaneLenclud@183
  2164
                        text += (rec.Text + "\n");
StephaneLenclud@183
  2165
                    }
StephaneLenclud@183
  2166
                    else
StephaneLenclud@183
  2167
                    {
StephaneLenclud@183
  2168
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  2169
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  2170
                    }
StephaneLenclud@223
  2171
StephaneLenclud@180
  2172
                }
StephaneLenclud@180
  2173
            }
StephaneLenclud@180
  2174
StephaneLenclud@183
  2175
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@223
  2176
            iRecordingNotification.Text = Truncate(text, 63);
StephaneLenclud@183
  2177
StephaneLenclud@180
  2178
            //Change visibility of notification if needed
StephaneLenclud@180
  2179
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@223
  2180
            {
StephaneLenclud@180
  2181
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  2182
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  2183
                //Take care of our REC icon
StephaneLenclud@183
  2184
                if (iDisplay.IsOpen())
StephaneLenclud@183
  2185
                {
StephaneLenclud@183
  2186
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@223
  2187
                }
StephaneLenclud@180
  2188
            }
StephaneLenclud@180
  2189
        }
StephaneLenclud@180
  2190
StephaneLenclud@180
  2191
        /// <summary>
sl@36
  2192
        ///
sl@33
  2193
        /// </summary>
sl@33
  2194
        /// <param name="aClient"></param>
sl@33
  2195
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  2196
        {
StephaneLenclud@148
  2197
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  2198
sl@33
  2199
            if (aClient == null)
sl@33
  2200
            {
sl@33
  2201
                return;
sl@33
  2202
            }
sl@33
  2203
StephaneLenclud@180
  2204
            //Hook in record icon update too
StephaneLenclud@180
  2205
            UpdateRecordingNotification();
StephaneLenclud@180
  2206
sl@33
  2207
            TreeNode node = null;
sl@33
  2208
            //Check that our client node already exists
sl@33
  2209
            //Get our client root node using its key which is our session ID
StephaneLenclud@188
  2210
            TreeNode[] nodes = iTreeViewClients.Nodes.Find(aClient.SessionId, false);
StephaneLenclud@223
  2211
            if (nodes.Count() > 0)
sl@33
  2212
            {
sl@33
  2213
                //We already have a node for that client
sl@33
  2214
                node = nodes[0];
sl@33
  2215
                //Clear children as we are going to recreate them below
sl@33
  2216
                node.Nodes.Clear();
sl@33
  2217
            }
sl@33
  2218
            else
sl@33
  2219
            {
sl@33
  2220
                //Client node does not exists create a new one
StephaneLenclud@188
  2221
                iTreeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
StephaneLenclud@188
  2222
                node = iTreeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  2223
            }
sl@33
  2224
sl@33
  2225
            if (node != null)
sl@33
  2226
            {
sl@33
  2227
                //Change its name
StephaneLenclud@184
  2228
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  2229
                {
StephaneLenclud@184
  2230
                    //We have a name, use it as text for our root node
sl@33
  2231
                    node.Text = aClient.Name;
sl@32
  2232
                    //Add a child with SessionId
sl@33
  2233
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  2234
                }
sl@33
  2235
                else
sl@33
  2236
                {
sl@33
  2237
                    //No name, use session ID instead
sl@33
  2238
                    node.Text = aClient.SessionId;
sl@33
  2239
                }
sl@36
  2240
StephaneLenclud@184
  2241
                //Display client priority
StephaneLenclud@184
  2242
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  2243
sl@67
  2244
                if (aClient.Fields.Count > 0)
sl@33
  2245
                {
sl@33
  2246
                    //Create root node for our texts
sl@70
  2247
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  2248
                    node.Nodes.Add(textsRoot);
sl@33
  2249
                    //For each text add a new entry
sl@67
  2250
                    foreach (DataField field in aClient.Fields)
sl@33
  2251
                    {
StephaneLenclud@172
  2252
                        if (field.IsTextField)
sl@67
  2253
                        {
StephaneLenclud@223
  2254
                            TextField textField = (TextField) field;
sl@70
  2255
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  2256
                        }
StephaneLenclud@172
  2257
                        else if (field.IsBitmapField)
sl@67
  2258
                        {
sl@72
  2259
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  2260
                        }
StephaneLenclud@274
  2261
                        else if (field is AudioVisualizerField)
StephaneLenclud@274
  2262
                        {
StephaneLenclud@274
  2263
                            textsRoot.Nodes.Add(new TreeNode("[Audio Visualizer]"));
StephaneLenclud@274
  2264
                        }
StephaneLenclud@172
  2265
                        else if (field.IsRecordingField)
StephaneLenclud@172
  2266
                        {
StephaneLenclud@223
  2267
                            RecordingField recordingField = (RecordingField) field;
StephaneLenclud@177
  2268
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  2269
                        }
sl@33
  2270
                    }
sl@32
  2271
                }
sl@34
  2272
sl@34
  2273
                node.ExpandAll();
sl@32
  2274
            }
sl@30
  2275
        }
sl@17
  2276
sl@60
  2277
        /// <summary>
sl@60
  2278
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  2279
        /// </summary>
sl@60
  2280
        private void UpdateTableLayoutRowStyles()
sl@60
  2281
        {
StephaneLenclud@175
  2282
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  2283
            {
sl@60
  2284
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@223
  2285
                rowStyle.Height = 100/iTableLayoutPanel.RowCount;
sl@60
  2286
            }
sl@60
  2287
        }
sl@60
  2288
sl@63
  2289
        /// <summary>
sl@63
  2290
        /// Update our display table layout.
StephaneLenclud@175
  2291
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  2292
        /// Fields must be specified by rows from the left.
sl@63
  2293
        /// </summary>
sl@63
  2294
        /// <param name="aLayout"></param>
sl@65
  2295
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  2296
        {
StephaneLenclud@175
  2297
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  2298
StephaneLenclud@223
  2299
            if (aClient == null)
StephaneLenclud@223
  2300
            {
StephaneLenclud@223
  2301
                //Just drop it
StephaneLenclud@223
  2302
                return;
StephaneLenclud@223
  2303
            }
StephaneLenclud@106
  2304
StephaneLenclud@106
  2305
sl@65
  2306
            TableLayout layout = aClient.Layout;
sl@70
  2307
StephaneLenclud@141
  2308
            //First clean our current panel
StephaneLenclud@175
  2309
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2310
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2311
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2312
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2313
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2314
StephaneLenclud@175
  2315
            //Then recreate our rows...
StephaneLenclud@175
  2316
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2317
            {
StephaneLenclud@175
  2318
                iTableLayoutPanel.RowCount++;
sl@63
  2319
            }
sl@63
  2320
StephaneLenclud@175
  2321
            // ...and columns 
StephaneLenclud@175
  2322
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2323
            {
StephaneLenclud@175
  2324
                iTableLayoutPanel.ColumnCount++;
sl@63
  2325
            }
sl@63
  2326
StephaneLenclud@175
  2327
            //For each column
StephaneLenclud@175
  2328
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2329
            {
sl@63
  2330
                //Create our column styles
StephaneLenclud@175
  2331
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2332
StephaneLenclud@175
  2333
                //For each rows
StephaneLenclud@175
  2334
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2335
                {
sl@63
  2336
                    if (i == 0)
sl@63
  2337
                    {
sl@63
  2338
                        //Create our row styles
StephaneLenclud@175
  2339
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2340
                    }
StephaneLenclud@176
  2341
                    else
sl@70
  2342
                    {
sl@70
  2343
                        continue;
sl@70
  2344
                    }
sl@63
  2345
                }
sl@63
  2346
            }
sl@63
  2347
StephaneLenclud@176
  2348
            //For each field
StephaneLenclud@176
  2349
            foreach (DataField field in aClient.Fields)
sl@70
  2350
            {
StephaneLenclud@176
  2351
                if (!field.IsTableField)
StephaneLenclud@176
  2352
                {
StephaneLenclud@176
  2353
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2354
                    continue;
StephaneLenclud@176
  2355
                }
StephaneLenclud@176
  2356
StephaneLenclud@223
  2357
                TableField tableField = (TableField) field;
StephaneLenclud@176
  2358
StephaneLenclud@176
  2359
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2360
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2361
StephaneLenclud@176
  2362
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2363
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2364
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2365
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2366
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2367
            }
sl@70
  2368
StephaneLenclud@176
  2369
sl@63
  2370
            CheckFontHeight();
sl@63
  2371
        }
sl@63
  2372
sl@68
  2373
        /// <summary>
sl@70
  2374
        /// Check our type of data field and create corresponding control
sl@68
  2375
        /// </summary>
sl@68
  2376
        /// <param name="aField"></param>
sl@69
  2377
        private Control CreateControlForDataField(DataField aField)
sl@68
  2378
        {
StephaneLenclud@223
  2379
            Control control = null;
StephaneLenclud@172
  2380
            if (aField.IsTextField)
sl@68
  2381
            {
sl@68
  2382
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2383
                label.AutoEllipsis = true;
sl@68
  2384
                label.AutoSize = true;
sl@68
  2385
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2386
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2387
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2388
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2389
                label.Name = "marqueeLabel" + aField;
sl@68
  2390
                label.OwnTimer = false;
StephaneLenclud@106
  2391
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2392
                label.Separator = cds.Separator;
sl@100
  2393
                label.MinFontSize = cds.MinFontSize;
sl@100
  2394
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2395
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2396
                //control.TabIndex = 2;
sl@68
  2397
                label.Font = cds.Font;
sl@68
  2398
StephaneLenclud@274
  2399
                TextField field = (TextField)aField;
StephaneLenclud@172
  2400
                label.TextAlign = field.Alignment;
sl@68
  2401
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2402
                label.Text = field.Text;
sl@68
  2403
                //
sl@68
  2404
                control = label;
sl@68
  2405
            }
StephaneLenclud@172
  2406
            else if (aField.IsBitmapField)
sl@68
  2407
            {
sl@68
  2408
                //Create picture box
sl@68
  2409
                PictureBox picture = new PictureBox();
sl@68
  2410
                picture.AutoSize = true;
sl@68
  2411
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2412
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2413
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2414
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2415
                picture.Name = "pictureBox" + aField;
sl@68
  2416
                //Set our image
StephaneLenclud@274
  2417
                BitmapField field = (BitmapField)aField;
StephaneLenclud@172
  2418
                picture.Image = field.Bitmap;
sl@68
  2419
                //
sl@68
  2420
                control = picture;
sl@68
  2421
            }
StephaneLenclud@274
  2422
            else if (aField is AudioVisualizerField)
StephaneLenclud@274
  2423
            {
StephaneLenclud@274
  2424
                //Create picture box
StephaneLenclud@274
  2425
                PictureBox picture = new PictureBox();
StephaneLenclud@274
  2426
                picture.AutoSize = true;
StephaneLenclud@274
  2427
                picture.BackColor = System.Drawing.Color.Transparent;
StephaneLenclud@274
  2428
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
StephaneLenclud@274
  2429
                picture.Location = new System.Drawing.Point(1, 1);
StephaneLenclud@274
  2430
                picture.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@274
  2431
                picture.Name = "pictureBox" + aField;
StephaneLenclud@274
  2432
                control = picture;
StephaneLenclud@274
  2433
            }
StephaneLenclud@274
  2434
StephaneLenclud@172
  2435
            //TODO: Handle recording field?
sl@68
  2436
sl@69
  2437
            return control;
sl@68
  2438
        }
sl@68
  2439
StephaneLenclud@223
  2440
        /// <summary>
StephaneLenclud@223
  2441
        /// Called when the user selected a new display type.
StephaneLenclud@223
  2442
        /// </summary>
StephaneLenclud@223
  2443
        /// <param name="sender"></param>
StephaneLenclud@223
  2444
        /// <param name="e"></param>
sl@46
  2445
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2446
        {
StephaneLenclud@223
  2447
            //Store the selected display type in our settings
sl@48
  2448
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2449
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2450
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2451
StephaneLenclud@223
  2452
            //Try re-opening the display connection if we were already connected.
StephaneLenclud@223
  2453
            //Otherwise just update our status to reflect display type change.
sl@51
  2454
            if (iDisplay.IsOpen())
sl@51
  2455
            {
sl@51
  2456
                OpenDisplayConnection();
sl@51
  2457
            }
sl@51
  2458
            else
sl@51
  2459
            {
sl@51
  2460
                UpdateStatus();
sl@51
  2461
            }
sl@46
  2462
        }
sl@46
  2463
sl@47
  2464
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2465
        {
sl@47
  2466
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2467
            {
sl@51
  2468
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2469
sl@51
  2470
                if (interval > 0)
sl@51
  2471
                {
StephaneLenclud@270
  2472
                    iTimerDisplay.Interval = interval;
StephaneLenclud@270
  2473
                    cds.TimerInterval = iTimerDisplay.Interval;
sl@51
  2474
                    Properties.Settings.Default.Save();
sl@51
  2475
                }
sl@47
  2476
            }
sl@47
  2477
        }
sl@47
  2478
sl@100
  2479
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2480
        {
sl@100
  2481
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2482
            {
sl@100
  2483
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2484
sl@100
  2485
                if (minFontSize > 0)
sl@100
  2486
                {
sl@100
  2487
                    cds.MinFontSize = minFontSize;
sl@100
  2488
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2489
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2490
                    UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2491
                }
sl@100
  2492
            }
sl@100
  2493
        }
sl@100
  2494
StephaneLenclud@106
  2495
StephaneLenclud@223
  2496
        private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@223
  2497
        {
StephaneLenclud@223
  2498
            if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@223
  2499
            {
StephaneLenclud@223
  2500
                int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@223
  2501
StephaneLenclud@223
  2502
                if (scrollingSpeed > 0)
StephaneLenclud@223
  2503
                {
StephaneLenclud@223
  2504
                    cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@223
  2505
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2506
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2507
                    UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@223
  2508
                }
StephaneLenclud@223
  2509
            }
StephaneLenclud@223
  2510
        }
StephaneLenclud@106
  2511
sl@100
  2512
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2513
        {
sl@100
  2514
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2515
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2516
StephaneLenclud@223
  2517
            //Update our text fields
StephaneLenclud@223
  2518
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@223
  2519
            {
StephaneLenclud@223
  2520
                ctrl.Separator = cds.Separator;
StephaneLenclud@223
  2521
            }
StephaneLenclud@110
  2522
sl@100
  2523
        }
sl@100
  2524
sl@52
  2525
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2526
        {
sl@52
  2527
            iDisplay.PowerOn();
sl@52
  2528
        }
sl@52
  2529
sl@52
  2530
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2531
        {
sl@52
  2532
            iDisplay.PowerOff();
sl@52
  2533
        }
sl@52
  2534
sl@53
  2535
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2536
        {
StephaneLenclud@223
  2537
            ShowClock();
sl@53
  2538
        }
sl@53
  2539
sl@53
  2540
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2541
        {
StephaneLenclud@223
  2542
            HideClock();
sl@53
  2543
        }
sl@88
  2544
sl@88
  2545
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2546
        {
sl@88
  2547
            InstallUpdateSyncWithInfo();
sl@88
  2548
        }
sl@88
  2549
StephaneLenclud@223
  2550
        /// <summary>
StephaneLenclud@223
  2551
        /// 
StephaneLenclud@223
  2552
        /// </summary>
StephaneLenclud@223
  2553
        void ShowClock()
StephaneLenclud@223
  2554
        {
StephaneLenclud@223
  2555
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2556
            {
StephaneLenclud@223
  2557
                return;
StephaneLenclud@223
  2558
            }
StephaneLenclud@223
  2559
StephaneLenclud@223
  2560
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2561
            iSkipFrameRendering = true;
StephaneLenclud@223
  2562
            //Clear our screen 
StephaneLenclud@223
  2563
            iDisplay.Clear();
StephaneLenclud@223
  2564
            iDisplay.SwapBuffers();
StephaneLenclud@223
  2565
            //Then show our clock
StephaneLenclud@223
  2566
            iDisplay.ShowClock();
StephaneLenclud@223
  2567
        }
StephaneLenclud@223
  2568
StephaneLenclud@223
  2569
        /// <summary>
StephaneLenclud@223
  2570
        /// 
StephaneLenclud@223
  2571
        /// </summary>
StephaneLenclud@223
  2572
        void HideClock()
StephaneLenclud@223
  2573
        {
StephaneLenclud@223
  2574
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2575
            {
StephaneLenclud@223
  2576
                return;
StephaneLenclud@223
  2577
            }
StephaneLenclud@223
  2578
StephaneLenclud@223
  2579
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2580
            iSkipFrameRendering = false;
StephaneLenclud@223
  2581
            iDisplay.HideClock();
StephaneLenclud@223
  2582
        }
sl@88
  2583
sl@88
  2584
        private void InstallUpdateSyncWithInfo()
sl@88
  2585
        {
sl@88
  2586
            UpdateCheckInfo info = null;
sl@88
  2587
sl@88
  2588
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2589
            {
sl@88
  2590
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2591
sl@88
  2592
                try
sl@88
  2593
                {
sl@88
  2594
                    info = ad.CheckForDetailedUpdate();
sl@88
  2595
sl@88
  2596
                }
sl@88
  2597
                catch (DeploymentDownloadException dde)
sl@88
  2598
                {
StephaneLenclud@223
  2599
                    MessageBox.Show(
StephaneLenclud@223
  2600
                        "The new version of the application cannot be downloaded at this time. \n\nPlease check your network connection, or try again later. Error: " +
StephaneLenclud@223
  2601
                        dde.Message);
sl@88
  2602
                    return;
sl@88
  2603
                }
sl@88
  2604
                catch (InvalidDeploymentException ide)
sl@88
  2605
                {
StephaneLenclud@223
  2606
                    MessageBox.Show(
StephaneLenclud@223
  2607
                        "Cannot check for a new version of the application. The ClickOnce deployment is corrupt. Please redeploy the application and try again. Error: " +
StephaneLenclud@223
  2608
                        ide.Message);
sl@88
  2609
                    return;
sl@88
  2610
                }
sl@88
  2611
                catch (InvalidOperationException ioe)
sl@88
  2612
                {
StephaneLenclud@223
  2613
                    MessageBox.Show(
StephaneLenclud@223
  2614
                        "This application cannot be updated. It is likely not a ClickOnce application. Error: " +
StephaneLenclud@223
  2615
                        ioe.Message);
sl@88
  2616
                    return;
sl@88
  2617
                }
sl@88
  2618
StephaneLenclud@223
  2619
                if (info.UpdateAvailable)
StephaneLenclud@223
  2620
                {
StephaneLenclud@223
  2621
                    Boolean doUpdate = true;
StephaneLenclud@223
  2622
StephaneLenclud@223
  2623
                    if (!info.IsUpdateRequired)
StephaneLenclud@223
  2624
                    {
StephaneLenclud@223
  2625
                        DialogResult dr =
StephaneLenclud@223
  2626
                            MessageBox.Show("An update is available. Would you like to update the application now?",
StephaneLenclud@223
  2627
                                "Update Available", MessageBoxButtons.OKCancel);
StephaneLenclud@223
  2628
                        if (!(DialogResult.OK == dr))
StephaneLenclud@223
  2629
                        {
StephaneLenclud@223
  2630
                            doUpdate = false;
StephaneLenclud@223
  2631
                        }
StephaneLenclud@223
  2632
                    }
StephaneLenclud@223
  2633
                    else
StephaneLenclud@223
  2634
                    {
StephaneLenclud@223
  2635
                        // Display a message that the application MUST reboot. Display the minimum required version.
StephaneLenclud@223
  2636
                        MessageBox.Show("This application has detected a mandatory update from your current " +
StephaneLenclud@223
  2637
                                        "version to version " + info.MinimumRequiredVersion.ToString() +
StephaneLenclud@223
  2638
                                        ". The application will now install the update and restart.",
StephaneLenclud@223
  2639
                            "Update Available", MessageBoxButtons.OK,
StephaneLenclud@223
  2640
                            MessageBoxIcon.Information);
StephaneLenclud@223
  2641
                    }
StephaneLenclud@223
  2642
StephaneLenclud@223
  2643
                    if (doUpdate)
StephaneLenclud@223
  2644
                    {
StephaneLenclud@223
  2645
                        try
StephaneLenclud@223
  2646
                        {
StephaneLenclud@223
  2647
                            ad.Update();
StephaneLenclud@223
  2648
                            MessageBox.Show("The application has been upgraded, and will now restart.");
StephaneLenclud@223
  2649
                            Application.Restart();
StephaneLenclud@223
  2650
                        }
StephaneLenclud@223
  2651
                        catch (DeploymentDownloadException dde)
StephaneLenclud@223
  2652
                        {
StephaneLenclud@223
  2653
                            MessageBox.Show(
StephaneLenclud@223
  2654
                                "Cannot install the latest version of the application. \n\nPlease check your network connection, or try again later. Error: " +
StephaneLenclud@223
  2655
                                dde);
StephaneLenclud@223
  2656
                            return;
StephaneLenclud@223
  2657
                        }
StephaneLenclud@223
  2658
                    }
StephaneLenclud@223
  2659
                }
StephaneLenclud@223
  2660
                else
StephaneLenclud@223
  2661
                {
StephaneLenclud@223
  2662
                    MessageBox.Show("You are already running the latest version.", "Application up-to-date");
StephaneLenclud@223
  2663
                }
sl@88
  2664
            }
sl@88
  2665
        }
sl@92
  2666
sl@94
  2667
StephaneLenclud@223
  2668
        /// <summary>
StephaneLenclud@223
  2669
        /// Used to
StephaneLenclud@223
  2670
        /// </summary>
StephaneLenclud@223
  2671
        private void SysTrayHideShow()
StephaneLenclud@223
  2672
        {
StephaneLenclud@223
  2673
            Visible = !Visible;
StephaneLenclud@223
  2674
            if (Visible)
StephaneLenclud@223
  2675
            {
StephaneLenclud@223
  2676
                Activate();
StephaneLenclud@223
  2677
                WindowState = FormWindowState.Normal;
StephaneLenclud@223
  2678
            }
StephaneLenclud@223
  2679
        }
StephaneLenclud@223
  2680
StephaneLenclud@223
  2681
        /// <summary>
StephaneLenclud@223
  2682
        /// Use to handle minimize events.
StephaneLenclud@223
  2683
        /// </summary>
StephaneLenclud@223
  2684
        /// <param name="sender"></param>
StephaneLenclud@223
  2685
        /// <param name="e"></param>
StephaneLenclud@223
  2686
        private void MainForm_SizeChanged(object sender, EventArgs e)
StephaneLenclud@223
  2687
        {
StephaneLenclud@223
  2688
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@223
  2689
            {
StephaneLenclud@223
  2690
                if (Visible)
StephaneLenclud@223
  2691
                {
StephaneLenclud@223
  2692
                    SysTrayHideShow();
StephaneLenclud@223
  2693
                }
StephaneLenclud@223
  2694
            }
StephaneLenclud@223
  2695
        }
StephaneLenclud@223
  2696
StephaneLenclud@223
  2697
        /// <summary>
StephaneLenclud@223
  2698
        /// 
StephaneLenclud@223
  2699
        /// </summary>
StephaneLenclud@223
  2700
        /// <param name="sender"></param>
StephaneLenclud@223
  2701
        /// <param name="e"></param>
StephaneLenclud@223
  2702
        private void tableLayoutPanel_SizeChanged(object sender, EventArgs e)
StephaneLenclud@223
  2703
        {
StephaneLenclud@223
  2704
            //Our table layout size has changed which means our display size has changed.
StephaneLenclud@223
  2705
            //We need to re-create our bitmap.
StephaneLenclud@223
  2706
            iCreateBitmap = true;
StephaneLenclud@223
  2707
        }
StephaneLenclud@223
  2708
StephaneLenclud@223
  2709
        /// <summary>
StephaneLenclud@167
  2710
        /// Broadcast messages to subscribers.
StephaneLenclud@167
  2711
        /// </summary>
StephaneLenclud@167
  2712
        /// <param name="message"></param>
StephaneLenclud@167
  2713
        protected override void WndProc(ref Message aMessage)
StephaneLenclud@167
  2714
        {
StephaneLenclud@223
  2715
            if (OnWndProc != null)
StephaneLenclud@167
  2716
            {
StephaneLenclud@167
  2717
                OnWndProc(ref aMessage);
StephaneLenclud@167
  2718
            }
StephaneLenclud@223
  2719
StephaneLenclud@167
  2720
            base.WndProc(ref aMessage);
StephaneLenclud@167
  2721
        }
StephaneLenclud@168
  2722
Stephane@252
  2723
        private void iCheckBoxCecEnabled_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@168
  2724
        {
StephaneLenclud@168
  2725
            //
StephaneLenclud@168
  2726
            ResetCec();
StephaneLenclud@168
  2727
        }
StephaneLenclud@168
  2728
StephaneLenclud@168
  2729
        private void comboBoxHdmiPort_SelectedIndexChanged(object sender, EventArgs e)
StephaneLenclud@168
  2730
        {
StephaneLenclud@168
  2731
            //Save CEC HDMI port
StephaneLenclud@168
  2732
            Properties.Settings.Default.CecHdmiPort = Convert.ToByte(comboBoxHdmiPort.SelectedIndex);
StephaneLenclud@168
  2733
            Properties.Settings.Default.CecHdmiPort++;
StephaneLenclud@168
  2734
            Properties.Settings.Default.Save();
StephaneLenclud@168
  2735
            //
StephaneLenclud@168
  2736
            ResetCec();
StephaneLenclud@168
  2737
        }
StephaneLenclud@168
  2738
StephaneLenclud@168
  2739
        /// <summary>
StephaneLenclud@168
  2740
        /// 
StephaneLenclud@168
  2741
        /// </summary>
StephaneLenclud@168
  2742
        private void ResetCec()
StephaneLenclud@168
  2743
        {
StephaneLenclud@223
  2744
            if (iCecManager == null)
StephaneLenclud@168
  2745
            {
StephaneLenclud@168
  2746
                //Thus skipping initial UI setup
StephaneLenclud@168
  2747
                return;
StephaneLenclud@168
  2748
            }
StephaneLenclud@168
  2749
StephaneLenclud@168
  2750
            iCecManager.Stop();
StephaneLenclud@168
  2751
            //
StephaneLenclud@168
  2752
            if (Properties.Settings.Default.CecEnabled)
StephaneLenclud@168
  2753
            {
StephaneLenclud@168
  2754
                iCecManager.Start(Handle, "CEC",
StephaneLenclud@223
  2755
                    Properties.Settings.Default.CecHdmiPort);
StephaneLenclud@206
  2756
Stephane@207
  2757
                SetupCecLogLevel();
Stephane@207
  2758
            }
Stephane@207
  2759
        }
StephaneLenclud@206
  2760
Stephane@207
  2761
        /// <summary>
Stephane@207
  2762
        /// 
Stephane@207
  2763
        /// </summary>
StephaneLenclud@255
  2764
        private async void ResetHarmonyAsync(bool aForceAuth=false)
StephaneLenclud@233
  2765
        {
StephaneLenclud@233
  2766
            // ConnectAsync already if we have an existing session cookie
Stephane@252
  2767
            if (Properties.Settings.Default.HarmonyEnabled)
StephaneLenclud@233
  2768
            {
StephaneLenclud@233
  2769
                try
StephaneLenclud@233
  2770
                {
StephaneLenclud@255
  2771
                    iButtonHarmonyConnect.Enabled = false;
Stephane@252
  2772
                    await ConnectHarmonyAsync(aForceAuth);
StephaneLenclud@233
  2773
                }
Stephane@252
  2774
                catch (Exception ex)
StephaneLenclud@233
  2775
                {
StephaneLenclud@253
  2776
                    Trace.WriteLine("Exception thrown by ConnectHarmonyAsync");
StephaneLenclud@253
  2777
                    Trace.WriteLine(ex.ToString());
StephaneLenclud@233
  2778
                }
StephaneLenclud@255
  2779
                finally
StephaneLenclud@255
  2780
                {
StephaneLenclud@255
  2781
                    iButtonHarmonyConnect.Enabled = true;
StephaneLenclud@255
  2782
                }
StephaneLenclud@233
  2783
            }
StephaneLenclud@233
  2784
        }
StephaneLenclud@233
  2785
StephaneLenclud@233
  2786
        /// <summary>
StephaneLenclud@233
  2787
        /// 
StephaneLenclud@233
  2788
        /// </summary>
Stephane@252
  2789
        /// <param name="sender"></param>
Stephane@252
  2790
        /// <param name="e"></param>
Stephane@252
  2791
        private void iButtonHarmonyConnect_Click(object sender, EventArgs e)
Stephane@252
  2792
        {
Stephane@252
  2793
            // User is explicitaly trying to connect
Stephane@252
  2794
            //Reset Harmony Hub connection forcing authentication
StephaneLenclud@255
  2795
            ResetHarmonyAsync(true);
Stephane@252
  2796
        }
Stephane@252
  2797
Stephane@252
  2798
        /// <summary>
Stephane@252
  2799
        /// 
Stephane@252
  2800
        /// </summary>
Stephane@252
  2801
        /// <param name="sender"></param>
Stephane@252
  2802
        /// <param name="e"></param>
Stephane@252
  2803
        private void iCheckBoxHarmonyEnabled_CheckedChanged(object sender, EventArgs e)
Stephane@252
  2804
        {
Stephane@252
  2805
            iButtonHarmonyConnect.Enabled = iCheckBoxHarmonyEnabled.Checked;
Stephane@252
  2806
        }
Stephane@252
  2807
Stephane@252
  2808
        /// <summary>
Stephane@252
  2809
        /// 
Stephane@252
  2810
        /// </summary>
Stephane@207
  2811
        private void SetupCecLogLevel()
Stephane@207
  2812
        {
Stephane@207
  2813
            //Setup log level
Stephane@207
  2814
            iCecManager.Client.LogLevel = 0;
StephaneLenclud@206
  2815
Stephane@207
  2816
            if (checkBoxCecLogError.Checked)
StephaneLenclud@223
  2817
                iCecManager.Client.LogLevel |= (int) CecLogLevel.Error;
StephaneLenclud@206
  2818
Stephane@207
  2819
            if (checkBoxCecLogWarning.Checked)
StephaneLenclud@223
  2820
                iCecManager.Client.LogLevel |= (int) CecLogLevel.Warning;
StephaneLenclud@206
  2821
Stephane@207
  2822
            if (checkBoxCecLogNotice.Checked)
StephaneLenclud@223
  2823
                iCecManager.Client.LogLevel |= (int) CecLogLevel.Notice;
StephaneLenclud@206
  2824
Stephane@207
  2825
            if (checkBoxCecLogTraffic.Checked)
StephaneLenclud@223
  2826
                iCecManager.Client.LogLevel |= (int) CecLogLevel.Traffic;
StephaneLenclud@206
  2827
Stephane@207
  2828
            if (checkBoxCecLogDebug.Checked)
StephaneLenclud@223
  2829
                iCecManager.Client.LogLevel |= (int) CecLogLevel.Debug;
StephaneLenclud@206
  2830
Stephane@207
  2831
            iCecManager.Client.FilterOutPollLogs = checkBoxCecLogNoPoll.Checked;
Stephane@207
  2832
StephaneLenclud@168
  2833
        }
StephaneLenclud@189
  2834
StephaneLenclud@189
  2835
        private void ButtonStartIdleClient_Click(object sender, EventArgs e)
StephaneLenclud@189
  2836
        {
StephaneLenclud@189
  2837
            StartIdleClient();
StephaneLenclud@189
  2838
        }
StephaneLenclud@204
  2839
StephaneLenclud@206
  2840
        private void buttonClearLogs_Click(object sender, EventArgs e)
StephaneLenclud@206
  2841
        {
StephaneLenclud@206
  2842
            richTextBoxLogs.Clear();
StephaneLenclud@206
  2843
        }
StephaneLenclud@204
  2844
StephaneLenclud@206
  2845
        private void checkBoxCecLogs_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@206
  2846
        {
Stephane@207
  2847
            SetupCecLogLevel();
StephaneLenclud@206
  2848
        }
StephaneLenclud@211
  2849
StephaneLenclud@223
  2850
StephaneLenclud@218
  2851
StephaneLenclud@219
  2852
StephaneLenclud@219
  2853
StephaneLenclud@217
  2854
        /// <summary>
StephaneLenclud@219
  2855
        /// Get the current event based on event tree view selection.
StephaneLenclud@217
  2856
        /// </summary>
StephaneLenclud@217
  2857
        /// <returns></returns>
StephaneLenclud@234
  2858
        private Ear.Event CurrentEvent()
StephaneLenclud@217
  2859
        {
StephaneLenclud@217
  2860
            //Walk up the tree from the selected node to find our event
StephaneLenclud@217
  2861
            TreeNode node = iTreeViewEvents.SelectedNode;
StephaneLenclud@234
  2862
            Ear.Event selectedEvent = null;
StephaneLenclud@217
  2863
            while (node != null)
StephaneLenclud@217
  2864
            {
StephaneLenclud@234
  2865
                if (node.Tag is Ear.Event)
StephaneLenclud@217
  2866
                {
StephaneLenclud@234
  2867
                    selectedEvent = (Ear.Event) node.Tag;
StephaneLenclud@217
  2868
                    break;
StephaneLenclud@217
  2869
                }
StephaneLenclud@217
  2870
                node = node.Parent;
StephaneLenclud@217
  2871
            }
StephaneLenclud@217
  2872
StephaneLenclud@217
  2873
            return selectedEvent;
StephaneLenclud@217
  2874
        }
StephaneLenclud@217
  2875
StephaneLenclud@217
  2876
        /// <summary>
StephaneLenclud@219
  2877
        /// Get the current action based on event tree view selection
StephaneLenclud@217
  2878
        /// </summary>
StephaneLenclud@217
  2879
        /// <returns></returns>
StephaneLenclud@234
  2880
        private Ear.Action CurrentAction()
StephaneLenclud@217
  2881
        {
StephaneLenclud@217
  2882
            TreeNode node = iTreeViewEvents.SelectedNode;
StephaneLenclud@234
  2883
            if (node != null && node.Tag is Ear.Action)
StephaneLenclud@217
  2884
            {
StephaneLenclud@234
  2885
                return (Ear.Action) node.Tag;
StephaneLenclud@217
  2886
            }
StephaneLenclud@217
  2887
StephaneLenclud@217
  2888
            return null;
StephaneLenclud@217
  2889
        }
StephaneLenclud@217
  2890
StephaneLenclud@217
  2891
        /// <summary>
StephaneLenclud@217
  2892
        /// 
StephaneLenclud@217
  2893
        /// </summary>
StephaneLenclud@265
  2894
        /// <returns></returns>
StephaneLenclud@265
  2895
        private Ear.Object CurrentEarObject()
StephaneLenclud@265
  2896
        {
StephaneLenclud@265
  2897
            Ear.Action a = CurrentAction();
StephaneLenclud@265
  2898
            Ear.Event e = CurrentEvent();
StephaneLenclud@265
  2899
StephaneLenclud@265
  2900
            if (a != null)
StephaneLenclud@265
  2901
            {
StephaneLenclud@265
  2902
                return a;
StephaneLenclud@265
  2903
            }
StephaneLenclud@265
  2904
StephaneLenclud@265
  2905
            return e;
StephaneLenclud@265
  2906
        }
StephaneLenclud@265
  2907
StephaneLenclud@265
  2908
        /// <summary>
StephaneLenclud@265
  2909
        /// Get the current action based on event tree view selection
StephaneLenclud@265
  2910
        /// </summary>
StephaneLenclud@265
  2911
        /// <returns></returns>
StephaneLenclud@265
  2912
        private Ear.Object CurrentEarParent()
StephaneLenclud@265
  2913
        {
StephaneLenclud@265
  2914
            TreeNode node = iTreeViewEvents.SelectedNode;
StephaneLenclud@265
  2915
            if (node == null || node.Parent == null)
StephaneLenclud@265
  2916
            {
StephaneLenclud@265
  2917
                return null;
StephaneLenclud@265
  2918
            }
StephaneLenclud@265
  2919
StephaneLenclud@265
  2920
            if (node.Parent.Tag is Ear.Object)
StephaneLenclud@265
  2921
            {
StephaneLenclud@265
  2922
                return (Ear.Object)node.Parent.Tag;
StephaneLenclud@265
  2923
            }
StephaneLenclud@265
  2924
StephaneLenclud@265
  2925
            if (node.Parent.Parent != null && node.Parent.Parent.Tag is Ear.Object)
StephaneLenclud@265
  2926
            {
StephaneLenclud@265
  2927
                //Can be the case for events
StephaneLenclud@265
  2928
                return (Ear.Object)node.Parent.Parent.Tag;
StephaneLenclud@265
  2929
            }
StephaneLenclud@265
  2930
StephaneLenclud@265
  2931
            return null;
StephaneLenclud@265
  2932
        }
StephaneLenclud@265
  2933
StephaneLenclud@265
  2934
StephaneLenclud@265
  2935
        /// <summary>
StephaneLenclud@265
  2936
        /// 
StephaneLenclud@265
  2937
        /// </summary>
StephaneLenclud@217
  2938
        /// <param name="sender"></param>
StephaneLenclud@217
  2939
        /// <param name="e"></param>
StephaneLenclud@227
  2940
        private void buttonActionAdd_Click(object sender, EventArgs e)
StephaneLenclud@211
  2941
        {
StephaneLenclud@265
  2942
            Ear.Object parent = CurrentEarObject();
StephaneLenclud@265
  2943
            if (parent == null )
Stephane@212
  2944
            {
StephaneLenclud@265
  2945
                //We did not find a corresponding event or action
StephaneLenclud@211
  2946
                return;
StephaneLenclud@211
  2947
            }
StephaneLenclud@211
  2948
StephaneLenclud@234
  2949
            FormEditObject<Ear.Action> ea = new FormEditObject<Ear.Action>();
StephaneLenclud@228
  2950
            ea.Text = "Add action";
StephaneLenclud@211
  2951
            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
StephaneLenclud@211
  2952
            if (res == DialogResult.OK)
StephaneLenclud@211
  2953
            {
StephaneLenclud@265
  2954
                parent.Objects.Add(ea.Object);               
Stephane@212
  2955
                Properties.Settings.Default.Save();
StephaneLenclud@266
  2956
                // We want to select the parent so that one can easily add another action to the same collection
StephaneLenclud@266
  2957
                PopulateTreeViewEvents(parent);
StephaneLenclud@211
  2958
            }
StephaneLenclud@211
  2959
        }
StephaneLenclud@214
  2960
StephaneLenclud@217
  2961
        /// <summary>
StephaneLenclud@217
  2962
        /// 
StephaneLenclud@217
  2963
        /// </summary>
StephaneLenclud@217
  2964
        /// <param name="sender"></param>
StephaneLenclud@217
  2965
        /// <param name="e"></param>
StephaneLenclud@227
  2966
        private void buttonActionEdit_Click(object sender, EventArgs e)
StephaneLenclud@227
  2967
        {
StephaneLenclud@234
  2968
            Ear.Action selectedAction = CurrentAction();
StephaneLenclud@265
  2969
            Ear.Object parent = CurrentEarParent()
StephaneLenclud@265
  2970
            ;
StephaneLenclud@265
  2971
            if (parent == null || selectedAction == null)
StephaneLenclud@227
  2972
            {
StephaneLenclud@265
  2973
                //We did not find a corresponding parent
StephaneLenclud@227
  2974
                return;
StephaneLenclud@227
  2975
            }
StephaneLenclud@227
  2976
StephaneLenclud@234
  2977
            FormEditObject<Ear.Action> ea = new FormEditObject<Ear.Action>();
StephaneLenclud@228
  2978
            ea.Text = "Edit action";
StephaneLenclud@231
  2979
            ea.Object = selectedAction;
StephaneLenclud@227
  2980
            int actionIndex = iTreeViewEvents.SelectedNode.Index;
StephaneLenclud@227
  2981
            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
StephaneLenclud@227
  2982
            if (res == DialogResult.OK)
StephaneLenclud@227
  2983
            {
StephaneLenclud@265
  2984
                //Make sure we keep the same children as before
StephaneLenclud@265
  2985
                ea.Object.Objects = parent.Objects[actionIndex].Objects;
StephaneLenclud@227
  2986
                //Update our action
StephaneLenclud@265
  2987
                parent.Objects[actionIndex]=ea.Object;
StephaneLenclud@227
  2988
                //Save and rebuild our event tree view
StephaneLenclud@227
  2989
                Properties.Settings.Default.Save();
StephaneLenclud@266
  2990
                PopulateTreeViewEvents(ea.Object);
StephaneLenclud@227
  2991
            }
StephaneLenclud@227
  2992
        }
StephaneLenclud@227
  2993
StephaneLenclud@227
  2994
        /// <summary>
StephaneLenclud@227
  2995
        /// 
StephaneLenclud@227
  2996
        /// </summary>
StephaneLenclud@227
  2997
        /// <param name="sender"></param>
StephaneLenclud@227
  2998
        /// <param name="e"></param>
StephaneLenclud@227
  2999
        private void buttonActionDelete_Click(object sender, EventArgs e)
StephaneLenclud@214
  3000
        {
StephaneLenclud@234
  3001
            Ear.Action action = CurrentAction();
StephaneLenclud@214
  3002
            if (action == null)
StephaneLenclud@214
  3003
            {
StephaneLenclud@214
  3004
                //Must select action node
StephaneLenclud@214
  3005
                return;
StephaneLenclud@214
  3006
            }
StephaneLenclud@214
  3007
StephaneLenclud@235
  3008
            Properties.Settings.Default.EarManager.RemoveAction(action);
StephaneLenclud@214
  3009
            Properties.Settings.Default.Save();
StephaneLenclud@260
  3010
            PopulateTreeViewEvents();
StephaneLenclud@214
  3011
        }
StephaneLenclud@217
  3012
StephaneLenclud@223
  3013
        /// <summary>
StephaneLenclud@223
  3014
        /// 
StephaneLenclud@223
  3015
        /// </summary>
StephaneLenclud@223
  3016
        /// <param name="sender"></param>
StephaneLenclud@223
  3017
        /// <param name="e"></param>
StephaneLenclud@227
  3018
        private void buttonActionTest_Click(object sender, EventArgs e)
StephaneLenclud@223
  3019
        {
StephaneLenclud@234
  3020
            Ear.Action a = CurrentAction();
StephaneLenclud@223
  3021
            if (a != null)
StephaneLenclud@223
  3022
            {
StephaneLenclud@231
  3023
                a.Test();
StephaneLenclud@223
  3024
            }
StephaneLenclud@223
  3025
            iTreeViewEvents.Focus();
StephaneLenclud@223
  3026
        }
StephaneLenclud@223
  3027
StephaneLenclud@223
  3028
        /// <summary>
StephaneLenclud@223
  3029
        /// 
StephaneLenclud@223
  3030
        /// </summary>
StephaneLenclud@223
  3031
        /// <param name="sender"></param>
StephaneLenclud@223
  3032
        /// <param name="e"></param>
StephaneLenclud@223
  3033
        private void buttonActionMoveUp_Click(object sender, EventArgs e)
StephaneLenclud@223
  3034
        {
StephaneLenclud@234
  3035
            Ear.Action a = CurrentAction();
StephaneLenclud@223
  3036
            if (a == null || 
StephaneLenclud@223
  3037
                //Action already at the top of the list
StephaneLenclud@223
  3038
                iTreeViewEvents.SelectedNode.Index == 0)
StephaneLenclud@223
  3039
            {
StephaneLenclud@223
  3040
                return;
StephaneLenclud@223
  3041
            }
StephaneLenclud@223
  3042
StephaneLenclud@223
  3043
            //Swap actions in event's action list
StephaneLenclud@265
  3044
            Ear.Object parent = CurrentEarParent();
StephaneLenclud@223
  3045
            int currentIndex = iTreeViewEvents.SelectedNode.Index;
StephaneLenclud@265
  3046
            Ear.Action movingUp = parent.Objects[currentIndex] as Ear.Action;
StephaneLenclud@265
  3047
            Ear.Action movingDown = parent.Objects[currentIndex-1] as Ear.Action;
StephaneLenclud@265
  3048
            parent.Objects[currentIndex] = movingDown;
StephaneLenclud@265
  3049
            parent.Objects[currentIndex-1] = movingUp;
StephaneLenclud@223
  3050
StephaneLenclud@223
  3051
            //Save and populate our tree again
StephaneLenclud@223
  3052
            Properties.Settings.Default.Save();
StephaneLenclud@266
  3053
            PopulateTreeViewEvents(a);
StephaneLenclud@223
  3054
        }
StephaneLenclud@223
  3055
StephaneLenclud@223
  3056
        /// <summary>
StephaneLenclud@223
  3057
        /// 
StephaneLenclud@223
  3058
        /// </summary>
StephaneLenclud@223
  3059
        /// <param name="sender"></param>
StephaneLenclud@223
  3060
        /// <param name="e"></param>
StephaneLenclud@223
  3061
        private void buttonActionMoveDown_Click(object sender, EventArgs e)
StephaneLenclud@223
  3062
        {
StephaneLenclud@234
  3063
            Ear.Action a = CurrentAction();
StephaneLenclud@223
  3064
            if (a == null ||
StephaneLenclud@223
  3065
                //Action already at the bottom of the list
StephaneLenclud@223
  3066
                iTreeViewEvents.SelectedNode.Index == iTreeViewEvents.SelectedNode.Parent.Nodes.Count - 1)
StephaneLenclud@223
  3067
            {
StephaneLenclud@223
  3068
                return;
StephaneLenclud@223
  3069
            }
StephaneLenclud@223
  3070
StephaneLenclud@223
  3071
            //Swap actions in event's action list
StephaneLenclud@265
  3072
            Ear.Object parent = CurrentEarParent();
StephaneLenclud@223
  3073
            int currentIndex = iTreeViewEvents.SelectedNode.Index;
StephaneLenclud@265
  3074
            Ear.Action movingDown = parent.Objects[currentIndex] as Ear.Action;
StephaneLenclud@265
  3075
            Ear.Action movingUp = parent.Objects[currentIndex + 1] as Ear.Action;
StephaneLenclud@265
  3076
            parent.Objects[currentIndex] = movingUp;
StephaneLenclud@265
  3077
            parent.Objects[currentIndex + 1] = movingDown;
StephaneLenclud@223
  3078
StephaneLenclud@223
  3079
            //Save and populate our tree again
StephaneLenclud@223
  3080
            Properties.Settings.Default.Save();
StephaneLenclud@266
  3081
            PopulateTreeViewEvents(a);
StephaneLenclud@217
  3082
        }
StephaneLenclud@227
  3083
StephaneLenclud@227
  3084
StephaneLenclud@227
  3085
        /// <summary>
StephaneLenclud@229
  3086
        /// 
StephaneLenclud@229
  3087
        /// </summary>
StephaneLenclud@229
  3088
        /// <param name="sender"></param>
StephaneLenclud@229
  3089
        /// <param name="e"></param>
StephaneLenclud@229
  3090
        private void buttonEventTest_Click(object sender, EventArgs e)
StephaneLenclud@229
  3091
        {
StephaneLenclud@234
  3092
            Ear.Event earEvent = CurrentEvent();
StephaneLenclud@229
  3093
            if (earEvent != null)
StephaneLenclud@229
  3094
            {
StephaneLenclud@231
  3095
                earEvent.Test();
StephaneLenclud@229
  3096
            }
StephaneLenclud@229
  3097
        }
StephaneLenclud@229
  3098
StephaneLenclud@229
  3099
        /// <summary>
StephaneLenclud@227
  3100
        /// Manages events and actions buttons according to selected item in event tree.
StephaneLenclud@227
  3101
        /// </summary>
StephaneLenclud@227
  3102
        /// <param name="sender"></param>
StephaneLenclud@227
  3103
        /// <param name="e"></param>
StephaneLenclud@227
  3104
        private void iTreeViewEvents_AfterSelect(object sender, TreeViewEventArgs e)
StephaneLenclud@227
  3105
        {
StephaneLenclud@231
  3106
            UpdateEventView();
StephaneLenclud@231
  3107
        }
StephaneLenclud@231
  3108
StephaneLenclud@231
  3109
        /// <summary>
StephaneLenclud@231
  3110
        /// 
StephaneLenclud@231
  3111
        /// </summary>
StephaneLenclud@231
  3112
        private void UpdateEventView()
StephaneLenclud@231
  3113
        {
StephaneLenclud@231
  3114
            //One can always add an event
StephaneLenclud@231
  3115
            buttonEventAdd.Enabled = true;
StephaneLenclud@231
  3116
StephaneLenclud@227
  3117
            //Enable buttons according to selected item
StephaneLenclud@229
  3118
            buttonActionAdd.Enabled =
StephaneLenclud@229
  3119
            buttonEventTest.Enabled =
StephaneLenclud@231
  3120
            buttonEventDelete.Enabled =
StephaneLenclud@231
  3121
            buttonEventEdit.Enabled =
StephaneLenclud@229
  3122
                CurrentEvent() != null;
StephaneLenclud@227
  3123
StephaneLenclud@234
  3124
            Ear.Action currentAction = CurrentAction();
StephaneLenclud@227
  3125
            //If an action is selected enable the following buttons
StephaneLenclud@227
  3126
            buttonActionTest.Enabled =
StephaneLenclud@227
  3127
            buttonActionDelete.Enabled =
StephaneLenclud@227
  3128
            buttonActionMoveUp.Enabled =
StephaneLenclud@227
  3129
            buttonActionMoveDown.Enabled =
StephaneLenclud@231
  3130
            buttonActionEdit.Enabled =
StephaneLenclud@227
  3131
                    currentAction != null;
StephaneLenclud@227
  3132
StephaneLenclud@227
  3133
            if (currentAction != null)
StephaneLenclud@227
  3134
            {
StephaneLenclud@227
  3135
                //If an action is selected enable move buttons if needed
StephaneLenclud@227
  3136
                buttonActionMoveUp.Enabled = iTreeViewEvents.SelectedNode.Index != 0;
StephaneLenclud@227
  3137
                buttonActionMoveDown.Enabled = iTreeViewEvents.SelectedNode.Index <
StephaneLenclud@227
  3138
                                               iTreeViewEvents.SelectedNode.Parent.Nodes.Count - 1;
StephaneLenclud@227
  3139
            }
StephaneLenclud@227
  3140
        }
StephaneLenclud@227
  3141
StephaneLenclud@250
  3142
        /// <summary>
StephaneLenclud@250
  3143
        /// 
StephaneLenclud@250
  3144
        /// </summary>
StephaneLenclud@250
  3145
        /// <param name="sender"></param>
StephaneLenclud@250
  3146
        /// <param name="e"></param>
StephaneLenclud@231
  3147
        private void buttonEventAdd_Click(object sender, EventArgs e)
StephaneLenclud@231
  3148
        {
StephaneLenclud@234
  3149
            FormEditObject<Ear.Event> ea = new FormEditObject<Ear.Event>();
StephaneLenclud@231
  3150
            ea.Text = "Add event";
StephaneLenclud@231
  3151
            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
StephaneLenclud@231
  3152
            if (res == DialogResult.OK)
StephaneLenclud@231
  3153
            {
StephaneLenclud@235
  3154
                Properties.Settings.Default.EarManager.Events.Add(ea.Object);
StephaneLenclud@231
  3155
                Properties.Settings.Default.Save();
StephaneLenclud@266
  3156
                PopulateTreeViewEvents(ea.Object);
StephaneLenclud@231
  3157
            }
StephaneLenclud@231
  3158
        }
StephaneLenclud@231
  3159
StephaneLenclud@250
  3160
        /// <summary>
StephaneLenclud@250
  3161
        /// 
StephaneLenclud@250
  3162
        /// </summary>
StephaneLenclud@250
  3163
        /// <param name="sender"></param>
StephaneLenclud@250
  3164
        /// <param name="e"></param>
StephaneLenclud@231
  3165
        private void buttonEventDelete_Click(object sender, EventArgs e)
StephaneLenclud@231
  3166
        {
StephaneLenclud@234
  3167
            Ear.Event currentEvent = CurrentEvent();
StephaneLenclud@231
  3168
            if (currentEvent == null)
StephaneLenclud@231
  3169
            {
StephaneLenclud@231
  3170
                //Must select action node
StephaneLenclud@231
  3171
                return;
StephaneLenclud@231
  3172
            }
StephaneLenclud@231
  3173
StephaneLenclud@235
  3174
            Properties.Settings.Default.EarManager.Events.Remove(currentEvent);
StephaneLenclud@231
  3175
            Properties.Settings.Default.Save();
StephaneLenclud@260
  3176
            PopulateTreeViewEvents();
StephaneLenclud@231
  3177
        }
StephaneLenclud@231
  3178
StephaneLenclud@250
  3179
        /// <summary>
StephaneLenclud@250
  3180
        /// 
StephaneLenclud@250
  3181
        /// </summary>
StephaneLenclud@250
  3182
        /// <param name="sender"></param>
StephaneLenclud@250
  3183
        /// <param name="e"></param>
StephaneLenclud@231
  3184
        private void buttonEventEdit_Click(object sender, EventArgs e)
StephaneLenclud@231
  3185
        {
StephaneLenclud@234
  3186
            Ear.Event selectedEvent = CurrentEvent();
StephaneLenclud@231
  3187
            if (selectedEvent == null)
StephaneLenclud@231
  3188
            {
StephaneLenclud@231
  3189
                //We did not find a corresponding event
StephaneLenclud@231
  3190
                return;
StephaneLenclud@231
  3191
            }
StephaneLenclud@231
  3192
StephaneLenclud@234
  3193
            FormEditObject<Ear.Event> ea = new FormEditObject<Ear.Event>();
StephaneLenclud@231
  3194
            ea.Text = "Edit event";
StephaneLenclud@231
  3195
            ea.Object = selectedEvent;
StephaneLenclud@248
  3196
            int index = iTreeViewEvents.SelectedNode.Index;
StephaneLenclud@231
  3197
            DialogResult res = CodeProject.Dialog.DlgBox.ShowDialog(ea);
StephaneLenclud@231
  3198
            if (res == DialogResult.OK)
StephaneLenclud@248
  3199
            {                
StephaneLenclud@248
  3200
                //Make sure we keep the same actions as before
StephaneLenclud@265
  3201
                ea.Object.Objects = Properties.Settings.Default.EarManager.Events[index].Objects;
StephaneLenclud@248
  3202
                //Update our event
StephaneLenclud@248
  3203
                Properties.Settings.Default.EarManager.Events[index] = ea.Object;
StephaneLenclud@231
  3204
                //Save and rebuild our event tree view
StephaneLenclud@231
  3205
                Properties.Settings.Default.Save();
StephaneLenclud@266
  3206
                PopulateTreeViewEvents(ea.Object);
StephaneLenclud@231
  3207
            }
StephaneLenclud@231
  3208
        }
StephaneLenclud@231
  3209
StephaneLenclud@250
  3210
        /// <summary>
StephaneLenclud@250
  3211
        /// 
StephaneLenclud@250
  3212
        /// </summary>
StephaneLenclud@250
  3213
        /// <param name="sender"></param>
StephaneLenclud@250
  3214
        /// <param name="e"></param>
StephaneLenclud@231
  3215
        private void iTreeViewEvents_Leave(object sender, EventArgs e)
StephaneLenclud@231
  3216
        {
StephaneLenclud@231
  3217
            //Make sure our event tree never looses focus
StephaneLenclud@231
  3218
            ((TreeView) sender).Focus();
StephaneLenclud@231
  3219
        }
StephaneLenclud@233
  3220
StephaneLenclud@250
  3221
        /// <summary>
StephaneLenclud@256
  3222
        /// Called whenever we loose connection with our HarmonyHub.
StephaneLenclud@256
  3223
        /// </summary>
StephaneLenclud@256
  3224
        /// <param name="aRequestWasCancelled"></param>
St?phane@271
  3225
        private void HarmonyConnectionClosed(object aSender, bool aClosedByServer)
StephaneLenclud@256
  3226
        {
St?phane@271
  3227
            if (aClosedByServer)
St?phane@271
  3228
            {
St?phane@271
  3229
                //Try reconnect then
StephaneLenclud@256
  3230
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
St?phane@271
  3231
                BeginInvoke(new MethodInvoker(delegate () { ResetHarmonyAsync(); }));
StephaneLenclud@256
  3232
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
St?phane@271
  3233
            }
StephaneLenclud@256
  3234
        }
StephaneLenclud@256
  3235
StephaneLenclud@270
  3236
StephaneLenclud@270
  3237
StephaneLenclud@270
  3238
        int iHarmonyReconnectTries = 0;
StephaneLenclud@270
  3239
        const int KHarmonyMaxReconnectTries = 10;
StephaneLenclud@270
  3240
StephaneLenclud@256
  3241
        /// <summary>
StephaneLenclud@250
  3242
        /// 
StephaneLenclud@250
  3243
        /// </summary>
StephaneLenclud@250
  3244
        /// <returns></returns>
Stephane@252
  3245
        private async Task ConnectHarmonyAsync(bool aForceAuth=false)
StephaneLenclud@233
  3246
        {
StephaneLenclud@236
  3247
            if (Program.HarmonyClient != null)
StephaneLenclud@236
  3248
            {
StephaneLenclud@254
  3249
                await Program.HarmonyClient.CloseAsync();
StephaneLenclud@236
  3250
            }
StephaneLenclud@236
  3251
StephaneLenclud@270
  3252
            bool success = false;
StephaneLenclud@270
  3253
StephaneLenclud@236
  3254
            //Reset Harmony client & config
StephaneLenclud@236
  3255
            Program.HarmonyClient = null;
StephaneLenclud@236
  3256
            Program.HarmonyConfig = null;
StephaneLenclud@270
  3257
            iTreeViewHarmony.Nodes.Clear();
StephaneLenclud@236
  3258
StephaneLenclud@253
  3259
            Trace.WriteLine("Harmony: Connecting... ");
StephaneLenclud@256
  3260
            //First create our client and login
StephaneLenclud@259
  3261
            //Tip: Set keep-alive to false when testing reconnection process
StephaneLenclud@259
  3262
            Program.HarmonyClient = new HarmonyHub.Client(iTextBoxHarmonyHubAddress.Text, true);
St?phane@271
  3263
            Program.HarmonyClient.OnConnectionClosed += HarmonyConnectionClosed;
StephaneLenclud@266
  3264
StephaneLenclud@266
  3265
            string authToken = Properties.Settings.Default.LogitechAuthToken;
StephaneLenclud@266
  3266
            if (!string.IsNullOrEmpty(authToken) && !aForceAuth)
StephaneLenclud@233
  3267
            {
StephaneLenclud@266
  3268
                Trace.WriteLine("Harmony: Reusing token: {0}", authToken);
StephaneLenclud@270
  3269
                success = await Program.HarmonyClient.TryOpenAsync(authToken);
StephaneLenclud@233
  3270
            }
StephaneLenclud@256
  3271
StephaneLenclud@270
  3272
            if (!Program.HarmonyClient.IsReady || !success 
StephaneLenclud@270
  3273
                // Only first failure triggers new Harmony server AUTH
StephaneLenclud@270
  3274
                // That's to avoid calling upon Logitech servers too often
StephaneLenclud@270
  3275
                && iHarmonyReconnectTries == 0 )
StephaneLenclud@233
  3276
            {
StephaneLenclud@256
  3277
                //We failed to connect using our token
StephaneLenclud@256
  3278
                //Delete it then
StephaneLenclud@270
  3279
                Trace.WriteLine("Harmony: Reseting authentication token!");
StephaneLenclud@266
  3280
                Properties.Settings.Default.LogitechAuthToken = "";
StephaneLenclud@266
  3281
                Properties.Settings.Default.Save();
StephaneLenclud@256
  3282
StephaneLenclud@253
  3283
                Trace.WriteLine("Harmony: Authenticating with Logitech servers...");
St?phane@271
  3284
                success = await Program.HarmonyClient.TryOpenAsync();
StephaneLenclud@266
  3285
                //Persist our authentication token in our setting
StephaneLenclud@270
  3286
                if (success)
StephaneLenclud@270
  3287
                {
StephaneLenclud@270
  3288
                    Trace.WriteLine("Harmony: Saving authentication token.");
StephaneLenclud@270
  3289
                    Properties.Settings.Default.LogitechAuthToken = Program.HarmonyClient.Token;
StephaneLenclud@270
  3290
                    Properties.Settings.Default.Save();
StephaneLenclud@270
  3291
                }
StephaneLenclud@233
  3292
            }
StephaneLenclud@270
  3293
           
StephaneLenclud@270
  3294
            // I've seen this failing with "Policy lookup failed on server".
StephaneLenclud@270
  3295
            Program.HarmonyConfig = await Program.HarmonyClient.TryGetConfigAsync();
StephaneLenclud@270
  3296
            if (Program.HarmonyConfig == null)
StephaneLenclud@270
  3297
            {
StephaneLenclud@270
  3298
                success = false;
StephaneLenclud@270
  3299
            }
StephaneLenclud@270
  3300
            else
StephaneLenclud@270
  3301
            {
StephaneLenclud@270
  3302
                // So we now have our Harmony Configuration
StephaneLenclud@270
  3303
                PopulateTreeViewHarmony(Program.HarmonyConfig);
StephaneLenclud@270
  3304
                // Make sure harmony command actions are showing device name instead of device id
StephaneLenclud@270
  3305
                PopulateTreeViewEvents(CurrentEarObject());
StephaneLenclud@270
  3306
            }
StephaneLenclud@270
  3307
StephaneLenclud@270
  3308
            // TODO: Consider putting the retry logic one level higher in ResetHarmonyAsync
StephaneLenclud@270
  3309
            if (!success)
StephaneLenclud@270
  3310
            {
StephaneLenclud@270
  3311
                // See if we need to keep trying 
StephaneLenclud@270
  3312
                if (iHarmonyReconnectTries < KHarmonyMaxReconnectTries)
StephaneLenclud@270
  3313
                {
StephaneLenclud@270
  3314
                    iHarmonyReconnectTries++;
StephaneLenclud@270
  3315
                    Trace.WriteLine("Harmony: Failed to connect, try again: " + iHarmonyReconnectTries);
StephaneLenclud@270
  3316
                    await ConnectHarmonyAsync();
StephaneLenclud@270
  3317
                }
StephaneLenclud@270
  3318
                else
StephaneLenclud@270
  3319
                {
StephaneLenclud@270
  3320
                    Trace.WriteLine("Harmony: Failed to connect, giving up!");
StephaneLenclud@270
  3321
                    iHarmonyReconnectTries = 0;
StephaneLenclud@270
  3322
                    // TODO: Could use a data member as timer rather than a new instance.
StephaneLenclud@270
  3323
                    // Try that again in 5 minutes then.
StephaneLenclud@270
  3324
                    // Using Windows Form timer to make sure we run in the UI thread.
StephaneLenclud@270
  3325
                    System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer();
StephaneLenclud@270
  3326
                    timer.Tick += async delegate (object sender, EventArgs e)
StephaneLenclud@270
  3327
                    {
StephaneLenclud@270
  3328
                        // Stop our timer first as we won't need it anymore
StephaneLenclud@270
  3329
                        (sender as System.Windows.Forms.Timer).Stop();
StephaneLenclud@270
  3330
                        // Then try to connect again
StephaneLenclud@270
  3331
                        await ConnectHarmonyAsync();
StephaneLenclud@270
  3332
                    };
StephaneLenclud@270
  3333
                    timer.Interval = 300000;
StephaneLenclud@270
  3334
                    timer.Start();
StephaneLenclud@270
  3335
                }
StephaneLenclud@270
  3336
            }
StephaneLenclud@270
  3337
            else
StephaneLenclud@270
  3338
            {
StephaneLenclud@270
  3339
                // We are connected with a valid Harmony Configuration
StephaneLenclud@270
  3340
                // Reset our tries counter then    
StephaneLenclud@270
  3341
                iHarmonyReconnectTries = 0;
StephaneLenclud@270
  3342
            }
StephaneLenclud@233
  3343
        }
StephaneLenclud@233
  3344
StephaneLenclud@233
  3345
        /// <summary>
StephaneLenclud@233
  3346
        /// 
StephaneLenclud@233
  3347
        /// </summary>
StephaneLenclud@233
  3348
        /// <param name="aConfig"></param>
StephaneLenclud@236
  3349
        private void PopulateTreeViewHarmony(HarmonyHub.Config aConfig)
StephaneLenclud@233
  3350
        {
StephaneLenclud@233
  3351
            iTreeViewHarmony.Nodes.Clear();
StephaneLenclud@233
  3352
            //Add our devices
StephaneLenclud@236
  3353
            foreach (HarmonyHub.Device device in aConfig.Devices)
StephaneLenclud@233
  3354
            {
StephaneLenclud@233
  3355
                TreeNode deviceNode = iTreeViewHarmony.Nodes.Add(device.Id, $"{device.Label} ({device.DeviceTypeDisplayName}/{device.Model})");
StephaneLenclud@233
  3356
                deviceNode.Tag = device;
StephaneLenclud@233
  3357
StephaneLenclud@236
  3358
                foreach (HarmonyHub.ControlGroup cg in device.ControlGroups)
StephaneLenclud@233
  3359
                {
StephaneLenclud@233
  3360
                    TreeNode cgNode = deviceNode.Nodes.Add(cg.Name);
StephaneLenclud@233
  3361
                    cgNode.Tag = cg;
StephaneLenclud@233
  3362
StephaneLenclud@236
  3363
                    foreach (HarmonyHub.Function f in cg.Functions)
StephaneLenclud@233
  3364
                    {
StephaneLenclud@257
  3365
                        TreeNode fNode = cgNode.Nodes.Add(f.Label);
StephaneLenclud@233
  3366
                        fNode.Tag = f;
StephaneLenclud@233
  3367
                    }
StephaneLenclud@233
  3368
                }
StephaneLenclud@233
  3369
            }
StephaneLenclud@233
  3370
StephaneLenclud@233
  3371
            //treeViewConfig.ExpandAll();
StephaneLenclud@233
  3372
        }
StephaneLenclud@233
  3373
StephaneLenclud@233
  3374
        private async void iTreeViewHarmony_NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
StephaneLenclud@233
  3375
        {
StephaneLenclud@233
  3376
            //Upon function node double click we execute it
StephaneLenclud@236
  3377
            var tag = e.Node.Tag as HarmonyHub.Function;
StephaneLenclud@236
  3378
            if (tag != null && e.Node.Parent.Parent.Tag is HarmonyHub.Device)
StephaneLenclud@233
  3379
            {
StephaneLenclud@236
  3380
                HarmonyHub.Function f = tag;
StephaneLenclud@236
  3381
                HarmonyHub.Device d = (HarmonyHub.Device)e.Node.Parent.Parent.Tag;
StephaneLenclud@233
  3382
StephaneLenclud@257
  3383
                Trace.WriteLine($"Harmony: Sending {f.Label} to {d.Label}...");
StephaneLenclud@257
  3384
StephaneLenclud@257
  3385
                await Program.HarmonyClient.TrySendKeyPressAsync(d.Id, f.Action.Command);
StephaneLenclud@233
  3386
            }
StephaneLenclud@233
  3387
        }
Stephane@252
  3388
sl@0
  3389
    }
sl@0
  3390
}