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