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