Server/FormMain.cs
author StephaneLenclud
Wed, 04 Jan 2017 18:43:28 +0100
changeset 274 920fea7a6427
parent 273 e5f85a895a62
child 275 a4a341accc89
permissions -rw-r--r--
Proper basic support for Audio Visualizer.
StephaneLenclud@123
     1
´╗┐//
StephaneLenclud@123
     2
// Copyright (C) 2014-2015 St├ęphane Lenclud.
StephaneLenclud@123
     3
//
StephaneLenclud@123
     4
// This file is part of SharpDisplayManager.
StephaneLenclud@123
     5
//
StephaneLenclud@123
     6
// SharpDisplayManager is free software: you can redistribute it and/or modify
StephaneLenclud@123
     7
// it under the terms of the GNU General Public License as published by
StephaneLenclud@123
     8
// the Free Software Foundation, either version 3 of the License, or
StephaneLenclud@123
     9
// (at your option) any later version.
StephaneLenclud@123
    10
//
StephaneLenclud@123
    11
// SharpDisplayManager is distributed in the hope that it will be useful,
StephaneLenclud@123
    12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
StephaneLenclud@123
    13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
StephaneLenclud@123
    14
// GNU General Public License for more details.
StephaneLenclud@123
    15
//
StephaneLenclud@123
    16
// You should have received a copy of the GNU General Public License
StephaneLenclud@123
    17
// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
StephaneLenclud@123
    18
//
StephaneLenclud@123
    19
StephaneLenclud@123
    20
using System;
sl@0
    21
using System.Collections.Generic;
sl@0
    22
using System.ComponentModel;
sl@0
    23
using System.Data;
sl@0
    24
using System.Drawing;
sl@0
    25
using System.Linq;
sl@0
    26
using System.Text;
sl@0
    27
using System.Threading.Tasks;
sl@0
    28
using System.Windows.Forms;
sl@14
    29
using System.IO;
sl@0
    30
using CodeProject.Dialog;
sl@14
    31
using System.Drawing.Imaging;
sl@17
    32
using System.ServiceModel;
sl@25
    33
using System.Threading;
sl@31
    34
using System.Diagnostics;
sl@88
    35
using System.Deployment.Application;
sl@94
    36
using System.Reflection;
StephaneLenclud@112
    37
using System.Runtime.InteropServices;
StephaneLenclud@268
    38
using System.Security;
StephaneLenclud@272
    39
//CSCore
StephaneLenclud@273
    40
using CSCore;
StephaneLenclud@273
    41
using CSCore.Win32;
StephaneLenclud@273
    42
using CSCore.DSP;
StephaneLenclud@273
    43
using CSCore.Streams;
StephaneLenclud@272
    44
using CSCore.CoreAudioAPI;
StephaneLenclud@273
    45
using CSCore.SoundIn;
StephaneLenclud@273
    46
// Visualization
StephaneLenclud@273
    47
using Visualization;
StephaneLenclud@272
    48
// CEC
StephaneLenclud@206
    49
using CecSharp;
StephaneLenclud@117
    50
//Network
StephaneLenclud@117
    51
using NETWORKLIST;
sl@25
    52
//
sl@25
    53
using SharpDisplayClient;
sl@55
    54
using SharpDisplay;
StephaneLenclud@135
    55
using MiniDisplayInterop;
StephaneLenclud@171
    56
using SharpLib.Display;
StephaneLenclud@234
    57
using Ear = SharpLib.Ear;
StephaneLenclud@273
    58
StephaneLenclud@124
    59
sl@0
    60
namespace SharpDisplayManager
sl@0
    61
{
sl@58
    62
    //Types declarations
sl@58
    63
    public delegate uint ColorProcessingDelegate(int aX, int aY, uint aPixel);
StephaneLenclud@223
    64
sl@58
    65
    public delegate int CoordinateTranslationDelegate(System.Drawing.Bitmap aBmp, int aInt);
StephaneLenclud@223
    66
sl@62
    67
    //Delegates are used for our thread safe method
sl@62
    68
    public delegate void AddClientDelegate(string aSessionId, ICallback aCallback);
StephaneLenclud@223
    69
sl@62
    70
    public delegate void RemoveClientDelegate(string aSessionId);
StephaneLenclud@223
    71
sl@79
    72
    public delegate void SetFieldDelegate(string SessionId, DataField aField);
StephaneLenclud@223
    73
sl@79
    74
    public delegate void SetFieldsDelegate(string SessionId, System.Collections.Generic.IList<DataField> aFields);
StephaneLenclud@223
    75
sl@62
    76
    public delegate void SetLayoutDelegate(string SessionId, TableLayout aLayout);
StephaneLenclud@223
    77
sl@62
    78
    public delegate void SetClientNameDelegate(string aSessionId, string aName);
StephaneLenclud@223
    79
StephaneLenclud@184
    80
    public delegate void SetClientPriorityDelegate(string aSessionId, uint aPriority);
StephaneLenclud@223
    81
StephaneLenclud@184
    82
    public delegate void PlainUpdateDelegate();
StephaneLenclud@223
    83
StephaneLenclud@167
    84
    public delegate void WndProcDelegate(ref Message aMessage);
sl@58
    85
sl@58
    86
    /// <summary>
sl@58
    87
    /// Our Display manager main form
sl@58
    88
    /// </summary>
StephaneLenclud@223
    89
    [System.ComponentModel.DesignerCategory("Form")]
StephaneLenclud@226
    90
    public partial class FormMain : FormMainHid, IMMNotificationClient
sl@0
    91
    {
StephaneLenclud@234
    92
        //public Manager iManager = new Manager();        
sl@2
    93
        DateTime LastTickTime;
sl@3
    94
        Display iDisplay;
sl@14
    95
        System.Drawing.Bitmap iBmp;
sl@14
    96
        bool iCreateBitmap; //Workaround render to bitmap issues when minimized
sl@17
    97
        ServiceHost iServiceHost;
sl@65
    98
        // Our collection of clients sorted by session id.
sl@33
    99
        public Dictionary<string, ClientData> iClients;
sl@65
   100
        // The name of the client which informations are currently displayed.
sl@65
   101
        public string iCurrentClientSessionId;
sl@65
   102
        ClientData iCurrentClientData;
sl@65
   103
        //
sl@29
   104
        public bool iClosing;
StephaneLenclud@223
   105
        //
StephaneLenclud@223
   106
        public bool iSkipFrameRendering;
sl@65
   107
        //Function pointer for pixel color filtering
sl@58
   108
        ColorProcessingDelegate iColorFx;
sl@65
   109
        //Function pointer for pixel X coordinate intercept
sl@58
   110
        CoordinateTranslationDelegate iScreenX;
sl@65
   111
        //Function pointer for pixel Y coordinate intercept
sl@58
   112
        CoordinateTranslationDelegate iScreenY;
StephaneLenclud@272
   113
        //CSCore
StephaneLenclud@273
   114
        // Volume management
StephaneLenclud@223
   115
        private MMDeviceEnumerator iMultiMediaDeviceEnumerator;
StephaneLenclud@223
   116
        private MMDevice iMultiMediaDevice;
StephaneLenclud@272
   117
        private AudioEndpointVolume iAudioEndpointVolume;
StephaneLenclud@273
   118
        // Audio visualization
StephaneLenclud@273
   119
        private WasapiCapture iSoundIn;
StephaneLenclud@273
   120
        private IWaveSource iWaveSource;
StephaneLenclud@273
   121
        private LineSpectrum iLineSpectrum;
StephaneLenclud@273
   122
StephaneLenclud@223
   123
        //Network
StephaneLenclud@223
   124
        private NetworkManager iNetworkManager;
StephaneLenclud@167
   125
StephaneLenclud@167
   126
        /// <summary>
StephaneLenclud@167
   127
        /// CEC - Consumer Electronic Control.
StephaneLenclud@167
   128
        /// Notably used to turn TV on and off as Windows broadcast monitor on and off notifications.
StephaneLenclud@167
   129
        /// </summary>
StephaneLenclud@167
   130
        private ConsumerElectronicControl iCecManager;
StephaneLenclud@223
   131
StephaneLenclud@223
   132
        /// <summary>
StephaneLenclud@223
   133
        /// Manage run when Windows startup option
StephaneLenclud@223
   134
        /// </summary>
StephaneLenclud@223
   135
        private StartupManager iStartupManager;
StephaneLenclud@223
   136
StephaneLenclud@223
   137
        /// <summary>
StephaneLenclud@223
   138
        /// System notification icon used to hide our application from the task bar.
StephaneLenclud@223
   139
        /// </summary>
StephaneLenclud@223
   140
        private SharpLib.Notification.Control iNotifyIcon;
sl@94
   141
StephaneLenclud@167
   142
        /// <summary>
StephaneLenclud@194
   143
        /// System recording notification icon.
StephaneLenclud@179
   144
        /// </summary>
StephaneLenclud@179
   145
        private SharpLib.Notification.Control iRecordingNotification;
StephaneLenclud@179
   146
Stephane@202
   147
        /// <summary>
Stephane@202
   148
        /// 
Stephane@202
   149
        /// </summary>
StephaneLenclud@253
   150
        RichTextBoxTraceListener iWriter;
Stephane@202
   151
StephaneLenclud@179
   152
StephaneLenclud@179
   153
        /// <summary>
StephaneLenclud@167
   154
        /// Allow user to receive window messages;
StephaneLenclud@167
   155
        /// </summary>
StephaneLenclud@167
   156
        public event WndProcDelegate OnWndProc;
StephaneLenclud@167
   157
StephaneLenclud@226
   158
        public FormMain()
sl@0
   159
        {
StephaneLenclud@235
   160
            if (Properties.Settings.Default.EarManager == null)
Stephane@212
   161
            {
Stephane@212
   162
                //No actions in our settings yet
StephaneLenclud@235
   163
                Properties.Settings.Default.EarManager = new EarManager();
Stephane@212
   164
            }
Stephane@212
   165
            else
Stephane@212
   166
            {
StephaneLenclud@235
   167
                // We loaded events and actions from our settings
StephaneLenclud@235
   168
                // Internalizer apparently skips constructor so we need to initialize it here
StephaneLenclud@235
   169
                // Though I reckon that should only be needed when loading an empty EAR manager I guess.
Stephane@243
   170
                Properties.Settings.Default.EarManager.Construct();
Stephane@212
   171
            }
StephaneLenclud@210
   172
            iSkipFrameRendering = false;
StephaneLenclud@223
   173
            iClosing = false;
sl@65
   174
            iCurrentClientSessionId = "";
sl@65
   175
            iCurrentClientData = null;
sl@2
   176
            LastTickTime = DateTime.Now;
StephaneLenclud@223
   177
            //Instantiate our display and register for events notifications
sl@3
   178
            iDisplay = new Display();
StephaneLenclud@223
   179
            iDisplay.OnOpened += OnDisplayOpened;
StephaneLenclud@223
   180
            iDisplay.OnClosed += OnDisplayClosed;
StephaneLenclud@223
   181
            //
StephaneLenclud@223
   182
            iClients = new Dictionary<string, ClientData>();
StephaneLenclud@223
   183
            iStartupManager = new StartupManager();
StephaneLenclud@223
   184
            iNotifyIcon = new SharpLib.Notification.Control();
StephaneLenclud@179
   185
            iRecordingNotification = new SharpLib.Notification.Control();
sl@2
   186
StephaneLenclud@179
   187
            //Have our designer initialize its controls
sl@0
   188
            InitializeComponent();
StephaneLenclud@104
   189
StephaneLenclud@201
   190
            //Redirect console output
StephaneLenclud@253
   191
            iWriter = new RichTextBoxTraceListener(richTextBoxLogs);
StephaneLenclud@253
   192
            Trace.Listeners.Add(iWriter);
StephaneLenclud@201
   193
StephaneLenclud@201
   194
            //Populate device types
StephaneLenclud@201
   195
            PopulateDeviceTypes();
StephaneLenclud@104
   196
StephaneLenclud@223
   197
            //Initial status update 
sl@7
   198
            UpdateStatus();
StephaneLenclud@104
   199
sl@14
   200
            //We have a bug when drawing minimized and reusing our bitmap
StephaneLenclud@158
   201
            //Though I could not reproduce it on Windows 10
StephaneLenclud@223
   202
            iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
   203
                PixelFormat.Format32bppArgb);
sl@14
   204
            iCreateBitmap = false;
sl@94
   205
StephaneLenclud@223
   206
            //Minimize our window if desired
StephaneLenclud@223
   207
            if (Properties.Settings.Default.StartMinimized)
StephaneLenclud@223
   208
            {
StephaneLenclud@223
   209
                WindowState = FormWindowState.Minimized;
StephaneLenclud@223
   210
            }
sl@94
   211
sl@0
   212
        }
sl@0
   213
StephaneLenclud@223
   214
        /// <summary>
StephaneLenclud@223
   215
        ///
StephaneLenclud@223
   216
        /// </summary>
StephaneLenclud@223
   217
        /// <param name="sender"></param>
StephaneLenclud@223
   218
        /// <param name="e"></param>
StephaneLenclud@106
   219
        private void MainForm_Load(object sender, EventArgs e)
StephaneLenclud@106
   220
        {
StephaneLenclud@223
   221
            //Check if we are running a Click Once deployed application
StephaneLenclud@223
   222
            if (ApplicationDeployment.IsNetworkDeployed)
StephaneLenclud@223
   223
            {
StephaneLenclud@223
   224
                //This is a proper Click Once installation, fetch and show our version number
StephaneLenclud@223
   225
                this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion;
StephaneLenclud@223
   226
            }
StephaneLenclud@223
   227
            else
StephaneLenclud@223
   228
            {
StephaneLenclud@223
   229
                //Not a proper Click Once installation, assuming development build then
StephaneLenclud@223
   230
                this.Text += " - development";
StephaneLenclud@223
   231
            }
StephaneLenclud@223
   232
StephaneLenclud@272
   233
            //CSCore
StephaneLenclud@223
   234
            iMultiMediaDeviceEnumerator = new MMDeviceEnumerator();
StephaneLenclud@223
   235
            iMultiMediaDeviceEnumerator.RegisterEndpointNotificationCallback(this);
StephaneLenclud@223
   236
            UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@223
   237
StephaneLenclud@223
   238
            //Network
StephaneLenclud@223
   239
            iNetworkManager = new NetworkManager();
StephaneLenclud@223
   240
            iNetworkManager.OnConnectivityChanged += OnConnectivityChanged;
StephaneLenclud@223
   241
            UpdateNetworkStatus();
StephaneLenclud@117
   242
StephaneLenclud@167
   243
            //CEC
StephaneLenclud@167
   244
            iCecManager = new ConsumerElectronicControl();
StephaneLenclud@167
   245
            OnWndProc += iCecManager.OnWndProc;
StephaneLenclud@168
   246
            ResetCec();
StephaneLenclud@168
   247
StephaneLenclud@233
   248
            //Harmony
StephaneLenclud@255
   249
            ResetHarmonyAsync();
StephaneLenclud@233
   250
StephaneLenclud@211
   251
            //Setup Events
StephaneLenclud@260
   252
            PopulateTreeViewEvents();
StephaneLenclud@167
   253
StephaneLenclud@167
   254
            //Setup notification icon
StephaneLenclud@167
   255
            SetupTrayIcon();
StephaneLenclud@106
   256
StephaneLenclud@179
   257
            //Setup recording notification
StephaneLenclud@179
   258
            SetupRecordingNotification();
StephaneLenclud@179
   259
StephaneLenclud@179
   260
            // To make sure start up with minimize to tray works
StephaneLenclud@179
   261
            if (WindowState == FormWindowState.Minimized && Properties.Settings.Default.MinimizeToTray)
StephaneLenclud@223
   262
            {
StephaneLenclud@223
   263
                Visible = false;
StephaneLenclud@223
   264
            }
StephaneLenclud@106
   265
StephaneLenclud@106
   266
#if !DEBUG
StephaneLenclud@223
   267
    //When not debugging we want the screen to be empty until a client takes over
StephaneLenclud@106
   268
			ClearLayout();
StephaneLenclud@106
   269
#else
StephaneLenclud@223
   270
            //When developing we want at least one client for testing
StephaneLenclud@223
   271
            StartNewClient("abcdefghijklmnopqrst-0123456789", "ABCDEFGHIJKLMNOPQRST-0123456789");
StephaneLenclud@106
   272
#endif
StephaneLenclud@106
   273
StephaneLenclud@223
   274
            //Open display connection on start-up if needed
StephaneLenclud@223
   275
            if (Properties.Settings.Default.DisplayConnectOnStartup)
StephaneLenclud@223
   276
            {
StephaneLenclud@223
   277
                OpenDisplayConnection();
StephaneLenclud@223
   278
            }
StephaneLenclud@223
   279
StephaneLenclud@223
   280
            //Start our server so that we can get client requests
StephaneLenclud@223
   281
            StartServer();
StephaneLenclud@223
   282
StephaneLenclud@223
   283
            //Register for HID events
StephaneLenclud@223
   284
            RegisterHidDevices();
StephaneLenclud@194
   285
StephaneLenclud@194
   286
            //Start Idle client if needed
StephaneLenclud@194
   287
            if (Properties.Settings.Default.StartIdleClient)
StephaneLenclud@194
   288
            {
StephaneLenclud@194
   289
                StartIdleClient();
StephaneLenclud@194
   290
            }
StephaneLenclud@106
   291
        }
StephaneLenclud@106
   292
StephaneLenclud@223
   293
        /// <summary>
StephaneLenclud@223
   294
        /// Called when our display is opened.
StephaneLenclud@223
   295
        /// </summary>
StephaneLenclud@223
   296
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   297
        private void OnDisplayOpened(Display aDisplay)
StephaneLenclud@223
   298
        {
StephaneLenclud@187
   299
            //Make sure we resume frame rendering
StephaneLenclud@187
   300
            iSkipFrameRendering = false;
StephaneLenclud@122
   301
StephaneLenclud@223
   302
            //Set our screen size now that our display is connected
StephaneLenclud@223
   303
            //Our panelDisplay is the container of our tableLayoutPanel
StephaneLenclud@223
   304
            //tableLayoutPanel will resize itself to fit the client size of our panelDisplay
StephaneLenclud@223
   305
            //panelDisplay needs an extra 2 pixels for borders on each sides
StephaneLenclud@223
   306
            //tableLayoutPanel will eventually be the exact size of our display
StephaneLenclud@223
   307
            Size size = new Size(iDisplay.WidthInPixels() + 2, iDisplay.HeightInPixels() + 2);
StephaneLenclud@223
   308
            panelDisplay.Size = size;
StephaneLenclud@223
   309
StephaneLenclud@223
   310
            //Our display was just opened, update our UI
StephaneLenclud@223
   311
            UpdateStatus();
StephaneLenclud@223
   312
            //Initiate asynchronous request
StephaneLenclud@223
   313
            iDisplay.RequestFirmwareRevision();
StephaneLenclud@223
   314
StephaneLenclud@223
   315
            //Audio
StephaneLenclud@223
   316
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@223
   317
            //Network
StephaneLenclud@223
   318
            UpdateNetworkStatus();
StephaneLenclud@112
   319
StephaneLenclud@108
   320
#if DEBUG
StephaneLenclud@223
   321
            //Testing icon in debug, no arm done if icon not supported
StephaneLenclud@223
   322
            //iDisplay.SetIconStatus(Display.TMiniDisplayIconType.EMiniDisplayIconRecording, 0, 1);
StephaneLenclud@223
   323
            //iDisplay.SetAllIconsStatus(2);
StephaneLenclud@108
   324
#endif
StephaneLenclud@108
   325
StephaneLenclud@223
   326
        }
StephaneLenclud@104
   327
StephaneLenclud@265
   328
StephaneLenclud@265
   329
        private static void AddActionsToTreeNode(TreeNode aParentNode, Ear.Object aObject)
StephaneLenclud@265
   330
        {
StephaneLenclud@265
   331
            foreach (Ear.Action a in aObject.Objects.OfType<Ear.Action>())
StephaneLenclud@265
   332
            {
StephaneLenclud@265
   333
                //Create action node
StephaneLenclud@265
   334
                TreeNode actionNode = aParentNode.Nodes.Add(a.Brief());
StephaneLenclud@265
   335
                actionNode.Tag = a;
StephaneLenclud@265
   336
                //Use color from parent unless our action itself is disabled
StephaneLenclud@265
   337
                actionNode.ForeColor = a.Enabled ? aParentNode.ForeColor : Color.DimGray;
StephaneLenclud@265
   338
                //Go recursive
StephaneLenclud@265
   339
                AddActionsToTreeNode(actionNode,a);
StephaneLenclud@265
   340
            }
StephaneLenclud@265
   341
        }
StephaneLenclud@265
   342
StephaneLenclud@265
   343
StephaneLenclud@265
   344
        /// <summary>
StephaneLenclud@265
   345
        /// 
StephaneLenclud@265
   346
        /// </summary>
StephaneLenclud@265
   347
        /// <param name="aObject"></param>
StephaneLenclud@265
   348
        /// <param name="aNode"></param>
StephaneLenclud@265
   349
        private static TreeNode FindTreeNodeForEarObject(Ear.Object aObject, TreeNode aNode)
StephaneLenclud@265
   350
        {
StephaneLenclud@265
   351
            if (aNode.Tag == aObject)
StephaneLenclud@265
   352
            {
StephaneLenclud@265
   353
                return aNode;
StephaneLenclud@265
   354
            }
StephaneLenclud@265
   355
StephaneLenclud@265
   356
            foreach (TreeNode n in aNode.Nodes)
StephaneLenclud@265
   357
            {
StephaneLenclud@265
   358
                TreeNode found = FindTreeNodeForEarObject(aObject,n);
StephaneLenclud@265
   359
                if (found != null)
StephaneLenclud@265
   360
                {
StephaneLenclud@265
   361
                    return found;
StephaneLenclud@265
   362
                }
StephaneLenclud@265
   363
            }
StephaneLenclud@265
   364
StephaneLenclud@265
   365
            return null;
StephaneLenclud@265
   366
        }
StephaneLenclud@265
   367
StephaneLenclud@265
   368
StephaneLenclud@265
   369
        /// <summary>
StephaneLenclud@265
   370
        /// 
StephaneLenclud@265
   371
        /// </summary>
StephaneLenclud@265
   372
        /// <param name="aObject"></param>
StephaneLenclud@265
   373
        private void SelectEarObject(Ear.Object aObject)
StephaneLenclud@265
   374
        {
StephaneLenclud@265
   375
            foreach (TreeNode n in iTreeViewEvents.Nodes)
StephaneLenclud@265
   376
            {
StephaneLenclud@265
   377
                TreeNode found = FindTreeNodeForEarObject(aObject, n);
StephaneLenclud@265
   378
                if (found != null)
StephaneLenclud@265
   379
                {
StephaneLenclud@265
   380
                    iTreeViewEvents.SelectedNode=found;
StephaneLenclud@265
   381
                    iTreeViewEvents.Focus();
StephaneLenclud@265
   382
                    return;
StephaneLenclud@265
   383
                }
StephaneLenclud@265
   384
            }
StephaneLenclud@265
   385
        }
StephaneLenclud@265
   386
StephaneLenclud@211
   387
        /// <summary>
StephaneLenclud@218
   388
        /// Populate tree view with events and actions
StephaneLenclud@211
   389
        /// </summary>
StephaneLenclud@266
   390
        private void PopulateTreeViewEvents(Ear.Object aSelectedObject=null)
StephaneLenclud@211
   391
        {
StephaneLenclud@211
   392
            //Reset our tree
StephaneLenclud@211
   393
            iTreeViewEvents.Nodes.Clear();
StephaneLenclud@211
   394
            //Populate registered events
StephaneLenclud@235
   395
            foreach (Ear.Event e in Properties.Settings.Default.EarManager.Events)
StephaneLenclud@211
   396
            {
StephaneLenclud@231
   397
                //Create our event node
StephaneLenclud@260
   398
                //Work out the name of our node
StephaneLenclud@260
   399
                string eventNodeName = "";
StephaneLenclud@260
   400
                if (!string.IsNullOrEmpty(e.Name))
StephaneLenclud@260
   401
                {
StephaneLenclud@260
   402
                    //That event has a proper name, use it then
StephaneLenclud@260
   403
                    eventNodeName = $"{e.Name} - {e.Brief()}";
StephaneLenclud@260
   404
                }
StephaneLenclud@260
   405
                else
StephaneLenclud@260
   406
                {
StephaneLenclud@260
   407
                    //Unnamed events just use brief
StephaneLenclud@260
   408
                    eventNodeName = e.Brief();
StephaneLenclud@260
   409
                }
StephaneLenclud@260
   410
                
StephaneLenclud@260
   411
                TreeNode eventNode = iTreeViewEvents.Nodes.Add(eventNodeName);
StephaneLenclud@231
   412
                eventNode.Tag = e; //For easy access to our event
StephaneLenclud@231
   413
                if (!e.Enabled)
StephaneLenclud@231
   414
                {
StephaneLenclud@231
   415
                    //Dim our nodes if disabled
StephaneLenclud@231
   416
                    eventNode.ForeColor = Color.DimGray;
StephaneLenclud@231
   417
                }
StephaneLenclud@231
   418
StephaneLenclud@231
   419
                //Add event description as child node
StephaneLenclud@260
   420
                eventNode.Nodes.Add(e.AttributeDescription).ForeColor = eventNode.ForeColor; 
StephaneLenclud@231
   421
                //Create child node for actions root
StephaneLenclud@265
   422
                TreeNode actionsNode = eventNode.Nodes.Add("Actions");
StephaneLenclud@265
   423
                actionsNode.ForeColor = eventNode.ForeColor;
StephaneLenclud@265
   424
StephaneLenclud@265
   425
                // Recursively add our actions for that event
StephaneLenclud@265
   426
                AddActionsToTreeNode(actionsNode,e);
StephaneLenclud@211
   427
            }
StephaneLenclud@211
   428
StephaneLenclud@214
   429
            iTreeViewEvents.ExpandAll();
StephaneLenclud@231
   430
StephaneLenclud@266
   431
            if (aSelectedObject != null)
StephaneLenclud@266
   432
            {
StephaneLenclud@266
   433
                SelectEarObject(aSelectedObject);
StephaneLenclud@266
   434
            }            
StephaneLenclud@266
   435
StephaneLenclud@266
   436
            // Just to be safe in case the selection did not work
StephaneLenclud@231
   437
            UpdateEventView();
StephaneLenclud@211
   438
        }
StephaneLenclud@211
   439
StephaneLenclud@223
   440
        /// <summary>
StephaneLenclud@223
   441
        /// Called when our display is closed.
StephaneLenclud@223
   442
        /// </summary>
StephaneLenclud@223
   443
        /// <param name="aDisplay"></param>
StephaneLenclud@223
   444
        private void OnDisplayClosed(Display aDisplay)
StephaneLenclud@223
   445
        {
StephaneLenclud@187
   446
            //Our display was just closed, update our UI consequently
StephaneLenclud@187
   447
            UpdateStatus();
StephaneLenclud@223
   448
        }
StephaneLenclud@223
   449
StephaneLenclud@223
   450
        public void OnConnectivityChanged(NetworkManager aNetwork, NLM_CONNECTIVITY newConnectivity)
StephaneLenclud@223
   451
        {
StephaneLenclud@223
   452
            //Update network status
StephaneLenclud@223
   453
            UpdateNetworkStatus();
StephaneLenclud@223
   454
        }
StephaneLenclud@223
   455
StephaneLenclud@223
   456
        /// <summary>
StephaneLenclud@223
   457
        /// Update our Network Status
StephaneLenclud@223
   458
        /// </summary>
StephaneLenclud@223
   459
        private void UpdateNetworkStatus()
StephaneLenclud@223
   460
        {
StephaneLenclud@223
   461
            if (iDisplay.IsOpen())
StephaneLenclud@223
   462
            {
StephaneLenclud@223
   463
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Internet,
StephaneLenclud@223
   464
                    iNetworkManager.NetworkListManager.IsConnectedToInternet);
StephaneLenclud@135
   465
                iDisplay.SetIconOnOff(MiniDisplay.IconType.NetworkSignal, iNetworkManager.NetworkListManager.IsConnected);
StephaneLenclud@223
   466
            }
StephaneLenclud@223
   467
        }
StephaneLenclud@223
   468
StephaneLenclud@223
   469
StephaneLenclud@223
   470
        int iLastNetworkIconIndex = 0;
StephaneLenclud@223
   471
        int iUpdateCountSinceLastNetworkAnimation = 0;
StephaneLenclud@223
   472
StephaneLenclud@223
   473
        /// <summary>
StephaneLenclud@223
   474
        /// 
StephaneLenclud@223
   475
        /// </summary>
StephaneLenclud@223
   476
        private void UpdateNetworkSignal(DateTime aLastTickTime, DateTime aNewTickTime)
StephaneLenclud@223
   477
        {
StephaneLenclud@223
   478
            iUpdateCountSinceLastNetworkAnimation++;
StephaneLenclud@223
   479
            iUpdateCountSinceLastNetworkAnimation = iUpdateCountSinceLastNetworkAnimation%4;
StephaneLenclud@223
   480
StephaneLenclud@223
   481
            if (iDisplay.IsOpen() && iNetworkManager.NetworkListManager.IsConnected &&
StephaneLenclud@223
   482
                iUpdateCountSinceLastNetworkAnimation == 0)
StephaneLenclud@223
   483
            {
StephaneLenclud@135
   484
                int iconCount = iDisplay.IconCount(MiniDisplay.IconType.NetworkSignal);
StephaneLenclud@223
   485
                if (iconCount <= 0)
StephaneLenclud@223
   486
                {
StephaneLenclud@223
   487
                    //Prevents div by zero and other undefined behavior
StephaneLenclud@223
   488
                    return;
StephaneLenclud@223
   489
                }
StephaneLenclud@223
   490
                iLastNetworkIconIndex++;
StephaneLenclud@223
   491
                iLastNetworkIconIndex = iLastNetworkIconIndex%(iconCount*2);
StephaneLenclud@223
   492
                for (int i = 0; i < iconCount; i++)
StephaneLenclud@223
   493
                {
StephaneLenclud@223
   494
                    if (i < iLastNetworkIconIndex && !(i == 0 && iLastNetworkIconIndex > 3) &&
StephaneLenclud@223
   495
                        !(i == 1 && iLastNetworkIconIndex > 4))
StephaneLenclud@223
   496
                    {
StephaneLenclud@135
   497
                        iDisplay.SetIconOn(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   498
                    }
StephaneLenclud@223
   499
                    else
StephaneLenclud@223
   500
                    {
StephaneLenclud@135
   501
                        iDisplay.SetIconOff(MiniDisplay.IconType.NetworkSignal, i);
StephaneLenclud@223
   502
                    }
StephaneLenclud@223
   503
                }
StephaneLenclud@223
   504
            }
StephaneLenclud@223
   505
        }
StephaneLenclud@118
   506
StephaneLenclud@118
   507
StephaneLenclud@118
   508
StephaneLenclud@112
   509
        /// <summary>
StephaneLenclud@112
   510
        /// Receive volume change notification and reflect changes on our slider.
StephaneLenclud@112
   511
        /// </summary>
StephaneLenclud@112
   512
        /// <param name="data"></param>
StephaneLenclud@272
   513
        public void OnVolumeNotificationThreadSafe(object sender, AudioEndpointVolumeCallbackEventArgs aEvent)
StephaneLenclud@112
   514
        {
StephaneLenclud@223
   515
            UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   516
        }
StephaneLenclud@112
   517
StephaneLenclud@112
   518
        /// <summary>
StephaneLenclud@112
   519
        /// Update master volume when user moves our slider.
StephaneLenclud@112
   520
        /// </summary>
StephaneLenclud@112
   521
        /// <param name="sender"></param>
StephaneLenclud@112
   522
        /// <param name="e"></param>
StephaneLenclud@112
   523
        private void trackBarMasterVolume_Scroll(object sender, EventArgs e)
StephaneLenclud@112
   524
        {
StephaneLenclud@223
   525
            //Just like Windows Volume Mixer we unmute if the volume is adjusted
StephaneLenclud@272
   526
            iAudioEndpointVolume.IsMuted = false;
StephaneLenclud@223
   527
            //Set volume level according to our volume slider new position
StephaneLenclud@272
   528
            iAudioEndpointVolume.MasterVolumeLevelScalar = trackBarMasterVolume.Value/100.0f;
StephaneLenclud@112
   529
        }
StephaneLenclud@112
   530
StephaneLenclud@113
   531
StephaneLenclud@223
   532
        /// <summary>
StephaneLenclud@223
   533
        /// Mute check box changed.
StephaneLenclud@223
   534
        /// </summary>
StephaneLenclud@223
   535
        /// <param name="sender"></param>
StephaneLenclud@223
   536
        /// <param name="e"></param>
StephaneLenclud@223
   537
        private void checkBoxMute_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
   538
        {
StephaneLenclud@272
   539
            iAudioEndpointVolume.IsMuted = checkBoxMute.Checked;
StephaneLenclud@223
   540
        }
StephaneLenclud@113
   541
StephaneLenclud@112
   542
        /// <summary>
StephaneLenclud@112
   543
        /// Device State Changed
StephaneLenclud@112
   544
        /// </summary>
StephaneLenclud@223
   545
        public void OnDeviceStateChanged([MarshalAs(UnmanagedType.LPWStr)] string deviceId,
StephaneLenclud@223
   546
            [MarshalAs(UnmanagedType.I4)] DeviceState newState)
StephaneLenclud@223
   547
        {
StephaneLenclud@223
   548
        }
StephaneLenclud@112
   549
StephaneLenclud@112
   550
        /// <summary>
StephaneLenclud@112
   551
        /// Device Added
StephaneLenclud@112
   552
        /// </summary>
StephaneLenclud@223
   553
        public void OnDeviceAdded([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId)
StephaneLenclud@223
   554
        {
StephaneLenclud@223
   555
        }
StephaneLenclud@112
   556
StephaneLenclud@112
   557
        /// <summary>
StephaneLenclud@112
   558
        /// Device Removed
StephaneLenclud@112
   559
        /// </summary>
StephaneLenclud@223
   560
        public void OnDeviceRemoved([MarshalAs(UnmanagedType.LPWStr)] string deviceId)
StephaneLenclud@223
   561
        {
StephaneLenclud@223
   562
        }
StephaneLenclud@112
   563
StephaneLenclud@112
   564
        /// <summary>
StephaneLenclud@112
   565
        /// Default Device Changed
StephaneLenclud@112
   566
        /// </summary>
StephaneLenclud@223
   567
        public void OnDefaultDeviceChanged(DataFlow flow, Role role,
StephaneLenclud@223
   568
            [MarshalAs(UnmanagedType.LPWStr)] string defaultDeviceId)
StephaneLenclud@112
   569
        {
StephaneLenclud@112
   570
            if (role == Role.Multimedia && flow == DataFlow.Render)
StephaneLenclud@112
   571
            {
StephaneLenclud@112
   572
                UpdateAudioDeviceAndMasterVolumeThreadSafe();
StephaneLenclud@112
   573
            }
StephaneLenclud@112
   574
        }
StephaneLenclud@112
   575
StephaneLenclud@112
   576
        /// <summary>
StephaneLenclud@112
   577
        /// Property Value Changed
StephaneLenclud@112
   578
        /// </summary>
StephaneLenclud@112
   579
        /// <param name="pwstrDeviceId"></param>
StephaneLenclud@112
   580
        /// <param name="key"></param>
StephaneLenclud@223
   581
        public void OnPropertyValueChanged([MarshalAs(UnmanagedType.LPWStr)] string pwstrDeviceId, PropertyKey key)
StephaneLenclud@223
   582
        {
StephaneLenclud@223
   583
        }
StephaneLenclud@223
   584
StephaneLenclud@223
   585
StephaneLenclud@223
   586
StephaneLenclud@223
   587
StephaneLenclud@223
   588
        /// <summary>
StephaneLenclud@223
   589
        /// Update master volume indicators based our current system states.
StephaneLenclud@223
   590
        /// This typically includes volume levels and mute status.
StephaneLenclud@223
   591
        /// </summary>
StephaneLenclud@223
   592
        private void UpdateMasterVolumeThreadSafe()
StephaneLenclud@223
   593
        {
StephaneLenclud@223
   594
            if (this.InvokeRequired)
StephaneLenclud@223
   595
            {
StephaneLenclud@223
   596
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   597
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateMasterVolumeThreadSafe);
StephaneLenclud@223
   598
                this.Invoke(d, new object[] {});
StephaneLenclud@223
   599
                return;
StephaneLenclud@223
   600
            }
StephaneLenclud@223
   601
StephaneLenclud@223
   602
            //Update volume slider
StephaneLenclud@272
   603
            float volumeLevelScalar = iAudioEndpointVolume.MasterVolumeLevelScalar;
StephaneLenclud@223
   604
            trackBarMasterVolume.Value = Convert.ToInt32(volumeLevelScalar*100);
StephaneLenclud@223
   605
            //Update mute checkbox
StephaneLenclud@272
   606
            checkBoxMute.Checked = iAudioEndpointVolume.IsMuted;
StephaneLenclud@223
   607
StephaneLenclud@223
   608
            //If our display connection is open we need to update its icons
StephaneLenclud@223
   609
            if (iDisplay.IsOpen())
StephaneLenclud@223
   610
            {
StephaneLenclud@223
   611
                //First take care our our volume level icons
StephaneLenclud@135
   612
                int volumeIconCount = iDisplay.IconCount(MiniDisplay.IconType.Volume);
StephaneLenclud@223
   613
                if (volumeIconCount > 0)
StephaneLenclud@223
   614
                {
StephaneLenclud@223
   615
                    //Compute current volume level from system level and the number of segments in our display volume bar.
StephaneLenclud@223
   616
                    //That tells us how many segments in our volume bar needs to be turned on.
StephaneLenclud@223
   617
                    float currentVolume = volumeLevelScalar*volumeIconCount;
StephaneLenclud@223
   618
                    int segmentOnCount = Convert.ToInt32(currentVolume);
StephaneLenclud@223
   619
                    //Check if our segment count was rounded up, this will later be used for half brightness segment
StephaneLenclud@223
   620
                    bool roundedUp = segmentOnCount > currentVolume;
StephaneLenclud@223
   621
StephaneLenclud@223
   622
                    for (int i = 0; i < volumeIconCount; i++)
StephaneLenclud@223
   623
                    {
StephaneLenclud@223
   624
                        if (i < segmentOnCount)
StephaneLenclud@223
   625
                        {
StephaneLenclud@223
   626
                            //If we are dealing with our last segment and our segment count was rounded up then we will use half brightness.
StephaneLenclud@223
   627
                            if (i == segmentOnCount - 1 && roundedUp)
StephaneLenclud@223
   628
                            {
StephaneLenclud@223
   629
                                //Half brightness
StephaneLenclud@223
   630
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   631
                                    (iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1)/2);
StephaneLenclud@223
   632
                            }
StephaneLenclud@223
   633
                            else
StephaneLenclud@223
   634
                            {
StephaneLenclud@223
   635
                                //Full brightness
StephaneLenclud@223
   636
                                iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i,
StephaneLenclud@223
   637
                                    iDisplay.IconStatusCount(MiniDisplay.IconType.Volume) - 1);
StephaneLenclud@223
   638
                            }
StephaneLenclud@223
   639
                        }
StephaneLenclud@223
   640
                        else
StephaneLenclud@223
   641
                        {
StephaneLenclud@135
   642
                            iDisplay.SetIconStatus(MiniDisplay.IconType.Volume, i, 0);
StephaneLenclud@223
   643
                        }
StephaneLenclud@223
   644
                    }
StephaneLenclud@223
   645
                }
StephaneLenclud@223
   646
StephaneLenclud@223
   647
                //Take care of our mute icon
StephaneLenclud@272
   648
                iDisplay.SetIconOnOff(MiniDisplay.IconType.Mute, iAudioEndpointVolume.IsMuted);
StephaneLenclud@223
   649
            }
StephaneLenclud@223
   650
StephaneLenclud@223
   651
        }
StephaneLenclud@112
   652
StephaneLenclud@112
   653
        /// <summary>
StephaneLenclud@112
   654
        /// 
StephaneLenclud@112
   655
        /// </summary>
StephaneLenclud@273
   656
        private void StartAudioVisualization()
StephaneLenclud@273
   657
        {
StephaneLenclud@273
   658
            StopAudioVisualization();
StephaneLenclud@273
   659
            //Open the default device 
StephaneLenclud@273
   660
            iSoundIn = new WasapiLoopbackCapture();
StephaneLenclud@273
   661
            //Our loopback capture opens the default render device by default so the following is not needed
StephaneLenclud@273
   662
            //iSoundIn.Device = MMDeviceEnumerator.DefaultAudioEndpoint(DataFlow.Render, Role.Console);
StephaneLenclud@273
   663
            iSoundIn.Initialize();
StephaneLenclud@273
   664
StephaneLenclud@273
   665
            SoundInSource soundInSource = new SoundInSource(iSoundIn);
StephaneLenclud@273
   666
            ISampleSource source = soundInSource.ToSampleSource();
StephaneLenclud@273
   667
StephaneLenclud@273
   668
            const FftSize fftSize = FftSize.Fft4096;
StephaneLenclud@273
   669
            //create a spectrum provider which provides fft data based on some input
StephaneLenclud@273
   670
            BasicSpectrumProvider spectrumProvider = new BasicSpectrumProvider(source.WaveFormat.Channels, source.WaveFormat.SampleRate, fftSize);
StephaneLenclud@273
   671
StephaneLenclud@273
   672
            //linespectrum and voiceprint3dspectrum used for rendering some fft data
StephaneLenclud@273
   673
            //in oder to get some fft data, set the previously created spectrumprovider 
StephaneLenclud@273
   674
            iLineSpectrum = new LineSpectrum(fftSize)
StephaneLenclud@273
   675
            {
StephaneLenclud@273
   676
                SpectrumProvider = spectrumProvider,
StephaneLenclud@274
   677
                UseAverage = false,
StephaneLenclud@274
   678
                BarCount = 16,
StephaneLenclud@274
   679
                BarSpacing = 1,
StephaneLenclud@273
   680
                IsXLogScale = true,
StephaneLenclud@274
   681
                ScalingStrategy = ScalingStrategy.Decibel
StephaneLenclud@273
   682
            };
StephaneLenclud@273
   683
StephaneLenclud@273
   684
StephaneLenclud@273
   685
            //the SingleBlockNotificationStream is used to intercept the played samples
StephaneLenclud@273
   686
            var notificationSource = new SingleBlockNotificationStream(source);
StephaneLenclud@273
   687
            //pass the intercepted samples as input data to the spectrumprovider (which will calculate a fft based on them)
StephaneLenclud@273
   688
            notificationSource.SingleBlockRead += (s, a) => spectrumProvider.Add(a.Left, a.Right);
StephaneLenclud@273
   689
StephaneLenclud@273
   690
            iWaveSource = notificationSource.ToWaveSource(16);
StephaneLenclud@273
   691
StephaneLenclud@273
   692
StephaneLenclud@273
   693
            // We need to read from our source otherwise SingleBlockRead is never called and our spectrum provider is not populated
StephaneLenclud@273
   694
            byte[] buffer = new byte[iWaveSource.WaveFormat.BytesPerSecond / 2];
StephaneLenclud@273
   695
            soundInSource.DataAvailable += (s, aEvent) =>
StephaneLenclud@273
   696
            {
StephaneLenclud@273
   697
                int read;
StephaneLenclud@273
   698
                while ((read = iWaveSource.Read(buffer, 0, buffer.Length)) > 0) ;
StephaneLenclud@273
   699
            };
StephaneLenclud@273
   700
StephaneLenclud@273
   701
StephaneLenclud@273
   702
            //Start recording
StephaneLenclud@273
   703
            iSoundIn.Start();
StephaneLenclud@273
   704
        }
StephaneLenclud@273
   705
StephaneLenclud@273
   706
        /// <summary>
StephaneLenclud@273
   707
        /// 
StephaneLenclud@273
   708
        /// </summary>
StephaneLenclud@273
   709
        private void StopAudioVisualization()
StephaneLenclud@273
   710
        {
StephaneLenclud@273
   711
StephaneLenclud@273
   712
            if (iSoundIn != null)
StephaneLenclud@273
   713
            {
StephaneLenclud@273
   714
                iSoundIn.Stop();
StephaneLenclud@273
   715
                iSoundIn.Dispose();
StephaneLenclud@273
   716
                iSoundIn = null;
StephaneLenclud@273
   717
            }
StephaneLenclud@273
   718
            if (iWaveSource != null)
StephaneLenclud@273
   719
            {
StephaneLenclud@273
   720
                iWaveSource.Dispose();
StephaneLenclud@273
   721
                iWaveSource = null;
StephaneLenclud@273
   722
            }
StephaneLenclud@273
   723
StephaneLenclud@273
   724
        }
StephaneLenclud@273
   725
StephaneLenclud@273
   726
StephaneLenclud@273
   727
        /// <summary>
StephaneLenclud@273
   728
        /// 
StephaneLenclud@273
   729
        /// </summary>
StephaneLenclud@274
   730
        private void UpdateAudioVisualization()
StephaneLenclud@273
   731
        {
StephaneLenclud@273
   732
            // For demo draft purposes just fetch the firt picture box control and update it with current audio spectrum
StephaneLenclud@274
   733
StephaneLenclud@274
   734
            if (iCurrentClientData == null)
StephaneLenclud@273
   735
            {
StephaneLenclud@274
   736
                return;
StephaneLenclud@274
   737
            }
StephaneLenclud@274
   738
StephaneLenclud@274
   739
            // Update our math
StephaneLenclud@274
   740
            if (!iLineSpectrum.Update())
StephaneLenclud@274
   741
            {
StephaneLenclud@274
   742
                //Nothing changed no need to render
StephaneLenclud@274
   743
                return;
StephaneLenclud@274
   744
            }
StephaneLenclud@274
   745
StephaneLenclud@274
   746
            // Check if our current client has an Audio Visualizer field
StephaneLenclud@274
   747
            // and render them as needed
StephaneLenclud@274
   748
            foreach (DataField f in iCurrentClientData.Fields)
StephaneLenclud@274
   749
            {
StephaneLenclud@274
   750
                if (f is AudioVisualizerField)
StephaneLenclud@273
   751
                {
StephaneLenclud@274
   752
                    AudioVisualizerField avf = (AudioVisualizerField)f;
StephaneLenclud@274
   753
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(avf.Column, avf.Row);
StephaneLenclud@274
   754
StephaneLenclud@274
   755
                    if (ctrl is PictureBox)
StephaneLenclud@273
   756
                    {
StephaneLenclud@274
   757
                        PictureBox pb = (PictureBox)ctrl;
StephaneLenclud@274
   758
                        Image image = pb.Image;
StephaneLenclud@274
   759
                        // TODO: recycle images
StephaneLenclud@274
   760
                        var newImage = iLineSpectrum.Render(pb.Size, Color.Black, Color.Black, Color.White, false);
StephaneLenclud@274
   761
                        if (newImage != null)
StephaneLenclud@274
   762
                        {
StephaneLenclud@274
   763
                            pb.Image = newImage;
StephaneLenclud@274
   764
                            if (image != null)
StephaneLenclud@274
   765
                                image.Dispose();
StephaneLenclud@274
   766
                        }
StephaneLenclud@273
   767
                    }
StephaneLenclud@273
   768
                }
StephaneLenclud@273
   769
            }
StephaneLenclud@273
   770
        }
StephaneLenclud@273
   771
StephaneLenclud@273
   772
StephaneLenclud@273
   773
        /// <summary>
StephaneLenclud@273
   774
        /// 
StephaneLenclud@273
   775
        /// </summary>
StephaneLenclud@112
   776
        private void UpdateAudioDeviceAndMasterVolumeThreadSafe()
StephaneLenclud@112
   777
        {
StephaneLenclud@112
   778
            if (this.InvokeRequired)
StephaneLenclud@112
   779
            {
StephaneLenclud@112
   780
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   781
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateAudioDeviceAndMasterVolumeThreadSafe);
StephaneLenclud@223
   782
                this.Invoke(d, new object[] {});
StephaneLenclud@112
   783
                return;
StephaneLenclud@112
   784
            }
StephaneLenclud@223
   785
StephaneLenclud@112
   786
            //We are in the correct thread just go ahead.
StephaneLenclud@112
   787
            try
StephaneLenclud@223
   788
            {
StephaneLenclud@272
   789
                //Get our master volume
StephaneLenclud@223
   790
                iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
StephaneLenclud@272
   791
                iAudioEndpointVolume = AudioEndpointVolume.FromDevice(iMultiMediaDevice);
StephaneLenclud@273
   792
                
StephaneLenclud@223
   793
                //Update our label
StephaneLenclud@223
   794
                labelDefaultAudioDevice.Text = iMultiMediaDevice.FriendlyName;
StephaneLenclud@116
   795
StephaneLenclud@112
   796
                //Show our volume in our track bar
StephaneLenclud@223
   797
                UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   798
StephaneLenclud@112
   799
                //Register to get volume modifications
StephaneLenclud@272
   800
                AudioEndpointVolumeCallback callback = new AudioEndpointVolumeCallback();
StephaneLenclud@272
   801
                callback.NotifyRecived += OnVolumeNotificationThreadSafe;
StephaneLenclud@272
   802
                // Do we need to unregister?
StephaneLenclud@273
   803
                iAudioEndpointVolume.RegisterControlChangeNotify(callback);
StephaneLenclud@273
   804
                //
StephaneLenclud@273
   805
                StartAudioVisualization();
StephaneLenclud@112
   806
                //
StephaneLenclud@223
   807
                trackBarMasterVolume.Enabled = true;
StephaneLenclud@112
   808
            }
StephaneLenclud@112
   809
            catch (Exception ex)
StephaneLenclud@112
   810
            {
StephaneLenclud@112
   811
                Debug.WriteLine("Exception thrown in UpdateAudioDeviceAndMasterVolume");
StephaneLenclud@112
   812
                Debug.WriteLine(ex.ToString());
StephaneLenclud@112
   813
                //Something went wrong S/PDIF device ca throw exception I guess
StephaneLenclud@223
   814
                trackBarMasterVolume.Enabled = false;
StephaneLenclud@112
   815
            }
StephaneLenclud@112
   816
        }
StephaneLenclud@104
   817
StephaneLenclud@223
   818
        /// <summary>
StephaneLenclud@223
   819
        /// 
StephaneLenclud@223
   820
        /// </summary>
StephaneLenclud@223
   821
        private void PopulateDeviceTypes()
StephaneLenclud@223
   822
        {
StephaneLenclud@223
   823
            int count = Display.TypeCount();
StephaneLenclud@223
   824
StephaneLenclud@223
   825
            for (int i = 0; i < count; i++)
StephaneLenclud@223
   826
            {
StephaneLenclud@223
   827
                comboBoxDisplayType.Items.Add(Display.TypeName((MiniDisplay.Type) i));
StephaneLenclud@223
   828
            }
StephaneLenclud@223
   829
        }
StephaneLenclud@104
   830
StephaneLenclud@152
   831
        /// <summary>
StephaneLenclud@223
   832
        ///
StephaneLenclud@223
   833
        /// </summary>
StephaneLenclud@223
   834
        private void SetupTrayIcon()
StephaneLenclud@223
   835
        {
StephaneLenclud@223
   836
            iNotifyIcon.Icon = GetIcon("vfd.ico");
StephaneLenclud@223
   837
            iNotifyIcon.Text = "Sharp Display Manager";
StephaneLenclud@223
   838
            iNotifyIcon.Visible = true;
StephaneLenclud@223
   839
StephaneLenclud@223
   840
            //Double click toggles visibility - typically brings up the application
StephaneLenclud@223
   841
            iNotifyIcon.DoubleClick += delegate(object obj, EventArgs args)
StephaneLenclud@223
   842
            {
StephaneLenclud@223
   843
                SysTrayHideShow();
StephaneLenclud@223
   844
            };
StephaneLenclud@223
   845
StephaneLenclud@223
   846
            //Adding a context menu, useful to be able to exit the application
StephaneLenclud@223
   847
            ContextMenu contextMenu = new ContextMenu();
StephaneLenclud@223
   848
            //Context menu item to toggle visibility
StephaneLenclud@223
   849
            MenuItem hideShowItem = new MenuItem("Hide/Show");
StephaneLenclud@223
   850
            hideShowItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   851
            {
StephaneLenclud@223
   852
                SysTrayHideShow();
StephaneLenclud@223
   853
            };
StephaneLenclud@223
   854
            contextMenu.MenuItems.Add(hideShowItem);
StephaneLenclud@223
   855
StephaneLenclud@223
   856
            //Context menu item separator
StephaneLenclud@223
   857
            contextMenu.MenuItems.Add(new MenuItem("-"));
StephaneLenclud@223
   858
StephaneLenclud@223
   859
            //Context menu exit item
StephaneLenclud@223
   860
            MenuItem exitItem = new MenuItem("Exit");
StephaneLenclud@223
   861
            exitItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   862
            {
StephaneLenclud@223
   863
                Application.Exit();
StephaneLenclud@223
   864
            };
StephaneLenclud@223
   865
            contextMenu.MenuItems.Add(exitItem);
StephaneLenclud@223
   866
StephaneLenclud@223
   867
            iNotifyIcon.ContextMenu = contextMenu;
StephaneLenclud@223
   868
        }
sl@95
   869
StephaneLenclud@178
   870
        /// <summary>
StephaneLenclud@179
   871
        ///
StephaneLenclud@179
   872
        /// </summary>
StephaneLenclud@179
   873
        private void SetupRecordingNotification()
StephaneLenclud@179
   874
        {
StephaneLenclud@179
   875
            iRecordingNotification.Icon = GetIcon("record.ico");
StephaneLenclud@179
   876
            iRecordingNotification.Text = "No recording";
StephaneLenclud@180
   877
            iRecordingNotification.Visible = false;
StephaneLenclud@179
   878
        }
StephaneLenclud@179
   879
StephaneLenclud@179
   880
        /// <summary>
StephaneLenclud@178
   881
        /// Access icons from embedded resources.
StephaneLenclud@178
   882
        /// </summary>
StephaneLenclud@178
   883
        /// <param name="aName"></param>
StephaneLenclud@178
   884
        /// <returns></returns>
StephaneLenclud@178
   885
        public static Icon GetIcon(string aName)
StephaneLenclud@223
   886
        {
StephaneLenclud@223
   887
            string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
StephaneLenclud@223
   888
            foreach (string name in names)
StephaneLenclud@223
   889
            {
StephaneLenclud@178
   890
                //Find a resource name that ends with the given name
StephaneLenclud@223
   891
                if (name.EndsWith(aName))
StephaneLenclud@223
   892
                {
StephaneLenclud@223
   893
                    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
StephaneLenclud@223
   894
                    {
StephaneLenclud@223
   895
                        return new Icon(stream);
StephaneLenclud@223
   896
                    }
StephaneLenclud@223
   897
                }
StephaneLenclud@223
   898
            }
StephaneLenclud@223
   899
StephaneLenclud@223
   900
            return null;
StephaneLenclud@223
   901
        }
sl@94
   902
sl@94
   903
sl@65
   904
        /// <summary>
sl@65
   905
        /// Set our current client.
sl@65
   906
        /// This will take care of applying our client layout and set data fields.
sl@65
   907
        /// </summary>
sl@65
   908
        /// <param name="aSessionId"></param>
StephaneLenclud@223
   909
        void SetCurrentClient(string aSessionId, bool aForce = false)
sl@57
   910
        {
sl@65
   911
            if (aSessionId == iCurrentClientSessionId)
sl@57
   912
            {
sl@65
   913
                //Given client is already the current one.
sl@65
   914
                //Don't bother changing anything then.
sl@65
   915
                return;
sl@65
   916
            }
sl@57
   917
StephaneLenclud@185
   918
            ClientData requestedClientData = iClients[aSessionId];
StephaneLenclud@141
   919
StephaneLenclud@141
   920
            //Check when was the last time we switched to that client
StephaneLenclud@142
   921
            if (iCurrentClientData != null)
StephaneLenclud@141
   922
            {
StephaneLenclud@185
   923
                //Do not switch client if priority of current client is higher 
StephaneLenclud@185
   924
                if (!aForce && requestedClientData.Priority < iCurrentClientData.Priority)
StephaneLenclud@185
   925
                {
StephaneLenclud@185
   926
                    return;
StephaneLenclud@185
   927
                }
StephaneLenclud@185
   928
StephaneLenclud@185
   929
StephaneLenclud@142
   930
                double lastSwitchToClientSecondsAgo = (DateTime.Now - iCurrentClientData.LastSwitchTime).TotalSeconds;
StephaneLenclud@142
   931
                //TODO: put that hard coded value as a client property
StephaneLenclud@142
   932
                //Clients should be able to define how often they can be interrupted
StephaneLenclud@142
   933
                //Thus a background client can set this to zero allowing any other client to interrupt at any time
StephaneLenclud@142
   934
                //We could also compute this delay by looking at the requests frequencies?
StephaneLenclud@185
   935
                if (!aForce &&
StephaneLenclud@223
   936
                    requestedClientData.Priority == iCurrentClientData.Priority &&
StephaneLenclud@223
   937
                    //Time sharing is only if clients have the same priority
StephaneLenclud@185
   938
                    (lastSwitchToClientSecondsAgo < 30)) //Make sure a client is on for at least 30 seconds
StephaneLenclud@142
   939
                {
StephaneLenclud@142
   940
                    //Don't switch clients too often
StephaneLenclud@142
   941
                    return;
StephaneLenclud@142
   942
                }
StephaneLenclud@141
   943
            }
StephaneLenclud@141
   944
sl@65
   945
            //Set current client ID.
sl@65
   946
            iCurrentClientSessionId = aSessionId;
StephaneLenclud@141
   947
            //Set the time we last switched to that client
StephaneLenclud@141
   948
            iClients[aSessionId].LastSwitchTime = DateTime.Now;
sl@65
   949
            //Fetch and set current client data.
StephaneLenclud@185
   950
            iCurrentClientData = requestedClientData;
sl@65
   951
            //Apply layout and set data fields.
sl@65
   952
            UpdateTableLayoutPanel(iCurrentClientData);
sl@57
   953
        }
sl@57
   954
sl@0
   955
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
   956
        {
sl@0
   957
            //fontDialog.ShowColor = true;
sl@0
   958
            //fontDialog.ShowApply = true;
sl@0
   959
            fontDialog.ShowEffects = true;
sl@99
   960
            fontDialog.Font = cds.Font;
sl@28
   961
sl@28
   962
            fontDialog.FixedPitchOnly = checkBoxFixedPitchFontOnly.Checked;
sl@28
   963
sl@0
   964
            //fontDialog.ShowHelp = true;
sl@0
   965
sl@0
   966
            //fontDlg.MaxSize = 40;
sl@0
   967
            //fontDlg.MinSize = 22;
sl@0
   968
sl@0
   969
            //fontDialog.Parent = this;
sl@0
   970
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
   971
sl@0
   972
            //DlgBox.ShowDialog(fontDialog);
sl@0
   973
sl@0
   974
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
   975
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
   976
            {
sl@99
   977
                //Set the fonts to all our labels in our layout
StephaneLenclud@175
   978
                foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@99
   979
                {
sl@99
   980
                    if (ctrl is MarqueeLabel)
sl@99
   981
                    {
StephaneLenclud@223
   982
                        ((MarqueeLabel) ctrl).Font = fontDialog.Font;
sl@99
   983
                    }
sl@99
   984
                }
sl@0
   985
sl@99
   986
                //Save font settings
sl@48
   987
                cds.Font = fontDialog.Font;
sl@8
   988
                Properties.Settings.Default.Save();
sl@36
   989
                //
sl@37
   990
                CheckFontHeight();
sl@37
   991
            }
sl@37
   992
        }
sl@36
   993
sl@37
   994
        /// <summary>
sl@38
   995
        ///
sl@37
   996
        /// </summary>
sl@37
   997
        void CheckFontHeight()
sl@37
   998
        {
sl@54
   999
            //Show font height and width
sl@54
  1000
            labelFontHeight.Text = "Font height: " + cds.Font.Height;
sl@54
  1001
            float charWidth = IsFixedWidth(cds.Font);
sl@54
  1002
            if (charWidth == 0.0f)
sl@54
  1003
            {
sl@54
  1004
                labelFontWidth.Visible = false;
sl@54
  1005
            }
sl@54
  1006
            else
sl@54
  1007
            {
sl@54
  1008
                labelFontWidth.Visible = true;
sl@54
  1009
                labelFontWidth.Text = "Font width: " + charWidth;
sl@54
  1010
            }
sl@54
  1011
sl@70
  1012
            MarqueeLabel label = null;
sl@68
  1013
            //Get the first label control we can find
StephaneLenclud@175
  1014
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@68
  1015
            {
sl@68
  1016
                if (ctrl is MarqueeLabel)
sl@68
  1017
                {
StephaneLenclud@223
  1018
                    label = (MarqueeLabel) ctrl;
sl@68
  1019
                    break;
sl@68
  1020
                }
sl@68
  1021
            }
sl@68
  1022
sl@54
  1023
            //Now check font height and show a warning if needed.
sl@68
  1024
            if (label != null && label.Font.Height > label.Height)
sl@37
  1025
            {
StephaneLenclud@223
  1026
                labelWarning.Text = "WARNING: Selected font is too height by " + (label.Font.Height - label.Height) +
StephaneLenclud@223
  1027
                                    " pixels!";
sl@37
  1028
                labelWarning.Visible = true;
sl@0
  1029
            }
sl@37
  1030
            else
sl@37
  1031
            {
sl@37
  1032
                labelWarning.Visible = false;
sl@37
  1033
            }
sl@37
  1034
sl@0
  1035
        }
sl@0
  1036
sl@0
  1037
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
  1038
        {
StephaneLenclud@175
  1039
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
  1040
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
  1041
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
  1042
            bmp.Save("D:\\capture.png");
sl@14
  1043
StephaneLenclud@223
  1044
            ((MarqueeLabel) iTableLayoutPanel.Controls[0]).Text = "Captured";
sl@17
  1045
sl@14
  1046
            /*
sl@14
  1047
            string outputFileName = "d:\\capture.png";
sl@14
  1048
            using (MemoryStream memory = new MemoryStream())
sl@14
  1049
            {
sl@14
  1050
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
  1051
                {
sl@14
  1052
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
  1053
                    byte[] bytes = memory.ToArray();
sl@14
  1054
                    fs.Write(bytes, 0, bytes.Length);
sl@14
  1055
                }
sl@14
  1056
            }
sl@14
  1057
             */
sl@14
  1058
sl@0
  1059
        }
sl@2
  1060
sl@12
  1061
        private void CheckForRequestResults()
sl@12
  1062
        {
sl@12
  1063
            if (iDisplay.IsRequestPending())
sl@12
  1064
            {
sl@12
  1065
                switch (iDisplay.AttemptRequestCompletion())
sl@12
  1066
                {
StephaneLenclud@135
  1067
                    case MiniDisplay.Request.FirmwareRevision:
sl@51
  1068
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@51
  1069
                        //Issue next request then
sl@51
  1070
                        iDisplay.RequestPowerSupplyStatus();
sl@51
  1071
                        break;
sl@51
  1072
StephaneLenclud@135
  1073
                    case MiniDisplay.Request.PowerSupplyStatus:
sl@12
  1074
                        if (iDisplay.PowerSupplyStatus())
sl@12
  1075
                        {
sl@12
  1076
                            toolStripStatusLabelPower.Text = "ON";
sl@12
  1077
                        }
sl@12
  1078
                        else
sl@12
  1079
                        {
sl@12
  1080
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
  1081
                        }
sl@12
  1082
                        //Issue next request then
sl@12
  1083
                        iDisplay.RequestDeviceId();
sl@12
  1084
                        break;
sl@12
  1085
StephaneLenclud@135
  1086
                    case MiniDisplay.Request.DeviceId:
sl@12
  1087
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
  1088
                        //No more request to issue
sl@12
  1089
                        break;
sl@12
  1090
                }
sl@12
  1091
            }
sl@12
  1092
        }
sl@12
  1093
sl@58
  1094
        public static uint ColorWhiteIsOn(int aX, int aY, uint aPixel)
sl@58
  1095
        {
sl@58
  1096
            if ((aPixel & 0x00FFFFFF) == 0x00FFFFFF)
sl@58
  1097
            {
sl@58
  1098
                return 0xFFFFFFFF;
sl@58
  1099
            }
sl@58
  1100
            return 0x00000000;
sl@58
  1101
        }
sl@16
  1102
sl@58
  1103
        public static uint ColorUntouched(int aX, int aY, uint aPixel)
sl@57
  1104
        {
sl@57
  1105
            return aPixel;
sl@57
  1106
        }
sl@57
  1107
sl@58
  1108
        public static uint ColorInversed(int aX, int aY, uint aPixel)
sl@57
  1109
        {
sl@57
  1110
            return ~aPixel;
sl@57
  1111
        }
sl@57
  1112
sl@58
  1113
        public static uint ColorChessboard(int aX, int aY, uint aPixel)
sl@58
  1114
        {
StephaneLenclud@223
  1115
            if ((aX%2 == 0) && (aY%2 == 0))
sl@58
  1116
            {
sl@58
  1117
                return ~aPixel;
sl@58
  1118
            }
StephaneLenclud@223
  1119
            else if ((aX%2 != 0) && (aY%2 != 0))
sl@58
  1120
            {
sl@58
  1121
                return ~aPixel;
sl@58
  1122
            }
sl@58
  1123
            return 0x00000000;
sl@58
  1124
        }
sl@58
  1125
sl@16
  1126
sl@16
  1127
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1128
        {
sl@16
  1129
            return aBmp.Width - aX - 1;
sl@16
  1130
        }
sl@16
  1131
sl@16
  1132
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1133
        {
sl@16
  1134
            return iBmp.Height - aY - 1;
sl@16
  1135
        }
sl@16
  1136
sl@16
  1137
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1138
        {
sl@16
  1139
            return aX;
sl@16
  1140
        }
sl@16
  1141
sl@16
  1142
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1143
        {
sl@16
  1144
            return aY;
sl@16
  1145
        }
sl@16
  1146
sl@58
  1147
        /// <summary>
sl@58
  1148
        /// Select proper pixel delegates according to our current settings.
sl@58
  1149
        /// </summary>
sl@58
  1150
        private void SetupPixelDelegates()
sl@58
  1151
        {
sl@59
  1152
            //Select our pixel processing routine
sl@58
  1153
            if (cds.InverseColors)
sl@58
  1154
            {
sl@58
  1155
                //iColorFx = ColorChessboard;
sl@58
  1156
                iColorFx = ColorInversed;
sl@58
  1157
            }
sl@58
  1158
            else
sl@58
  1159
            {
sl@58
  1160
                iColorFx = ColorWhiteIsOn;
sl@58
  1161
            }
sl@58
  1162
sl@58
  1163
            //Select proper coordinate translation functions
sl@58
  1164
            //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@58
  1165
            if (cds.ReverseScreen)
sl@58
  1166
            {
sl@58
  1167
                iScreenX = ScreenReversedX;
sl@58
  1168
                iScreenY = ScreenReversedY;
sl@58
  1169
            }
sl@58
  1170
            else
sl@58
  1171
            {
sl@58
  1172
                iScreenX = ScreenX;
sl@58
  1173
                iScreenY = ScreenY;
sl@58
  1174
            }
sl@58
  1175
sl@58
  1176
        }
sl@16
  1177
StephaneLenclud@270
  1178
        /// <summary>
StephaneLenclud@270
  1179
        /// This is our timer tick responsible to perform our render
StephaneLenclud@270
  1180
        /// TODO: Use a threading timer instead of a Windows form timer.
StephaneLenclud@270
  1181
        /// </summary>
StephaneLenclud@270
  1182
        /// <param name="sender"></param>
StephaneLenclud@270
  1183
        /// <param name="e"></param>
sl@2
  1184
        private void timer_Tick(object sender, EventArgs e)
sl@14
  1185
        {
sl@2
  1186
            //Update our animations
sl@2
  1187
            DateTime NewTickTime = DateTime.Now;
sl@2
  1188
StephaneLenclud@223
  1189
            UpdateNetworkSignal(LastTickTime, NewTickTime);
StephaneLenclud@118
  1190
sl@60
  1191
            //Update animation for all our marquees
StephaneLenclud@175
  1192
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@60
  1193
            {
sl@68
  1194
                if (ctrl is MarqueeLabel)
sl@68
  1195
                {
StephaneLenclud@223
  1196
                    ((MarqueeLabel) ctrl).UpdateAnimation(LastTickTime, NewTickTime);
sl@68
  1197
                }
sl@60
  1198
            }
sl@60
  1199
sl@4
  1200
            //Update our display
sl@4
  1201
            if (iDisplay.IsOpen())
sl@4
  1202
            {
sl@12
  1203
                CheckForRequestResults();
sl@12
  1204
StephaneLenclud@223
  1205
                //Check if frame rendering is needed
StephaneLenclud@223
  1206
                //Typically used when showing clock
StephaneLenclud@223
  1207
                if (!iSkipFrameRendering)
StephaneLenclud@223
  1208
                {
StephaneLenclud@223
  1209
                    //Draw to bitmap
StephaneLenclud@223
  1210
                    if (iCreateBitmap)
StephaneLenclud@223
  1211
                    {
StephaneLenclud@223
  1212
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
  1213
                            PixelFormat.Format32bppArgb);
StephaneLenclud@158
  1214
                        iCreateBitmap = false;
StephaneLenclud@158
  1215
                    }
StephaneLenclud@223
  1216
                    iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.ClientRectangle);
StephaneLenclud@223
  1217
                    //iBmp.Save("D:\\capture.png");
StephaneLenclud@223
  1218
StephaneLenclud@223
  1219
                    //Send it to our display
StephaneLenclud@223
  1220
                    for (int i = 0; i < iBmp.Width; i++)
StephaneLenclud@223
  1221
                    {
StephaneLenclud@223
  1222
                        for (int j = 0; j < iBmp.Height; j++)
StephaneLenclud@223
  1223
                        {
StephaneLenclud@223
  1224
                            unchecked
StephaneLenclud@223
  1225
                            {
StephaneLenclud@223
  1226
                                //Get our processed pixel coordinates
StephaneLenclud@223
  1227
                                int x = iScreenX(iBmp, i);
StephaneLenclud@223
  1228
                                int y = iScreenY(iBmp, j);
StephaneLenclud@223
  1229
                                //Get pixel color
StephaneLenclud@223
  1230
                                uint color = (uint) iBmp.GetPixel(i, j).ToArgb();
StephaneLenclud@223
  1231
                                //Apply color effects
StephaneLenclud@223
  1232
                                color = iColorFx(x, y, color);
StephaneLenclud@223
  1233
                                //Now set our pixel
StephaneLenclud@223
  1234
                                iDisplay.SetPixel(x, y, color);
StephaneLenclud@223
  1235
                            }
StephaneLenclud@223
  1236
                        }
StephaneLenclud@223
  1237
                    }
StephaneLenclud@223
  1238
StephaneLenclud@223
  1239
                    iDisplay.SwapBuffers();
StephaneLenclud@223
  1240
                }
sl@4
  1241
            }
sl@8
  1242
StephaneLenclud@274
  1243
            UpdateAudioVisualization();
StephaneLenclud@273
  1244
sl@8
  1245
            //Compute instant FPS
StephaneLenclud@274
  1246
            toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " +
StephaneLenclud@270
  1247
                                           (1000/iTimerDisplay.Interval).ToString() + " FPS";
sl@8
  1248
sl@8
  1249
            LastTickTime = NewTickTime;
sl@8
  1250
sl@2
  1251
        }
sl@3
  1252
StephaneLenclud@223
  1253
        /// <summary>
StephaneLenclud@223
  1254
        /// Attempt to establish connection with our display hardware.
StephaneLenclud@223
  1255
        /// </summary>
sl@46
  1256
        private void OpenDisplayConnection()
sl@3
  1257
        {
sl@46
  1258
            CloseDisplayConnection();
sl@46
  1259
StephaneLenclud@223
  1260
            if (!iDisplay.Open((MiniDisplay.Type) cds.DisplayType))
StephaneLenclud@223
  1261
            {
StephaneLenclud@223
  1262
                UpdateStatus();
StephaneLenclud@223
  1263
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
  1264
            }
sl@46
  1265
        }
sl@7
  1266
sl@46
  1267
        private void CloseDisplayConnection()
sl@46
  1268
        {
StephaneLenclud@223
  1269
            //Status will be updated upon receiving the closed event
StephaneLenclud@223
  1270
StephaneLenclud@223
  1271
            if (iDisplay == null || !iDisplay.IsOpen())
StephaneLenclud@223
  1272
            {
StephaneLenclud@223
  1273
                return;
StephaneLenclud@223
  1274
            }
StephaneLenclud@223
  1275
StephaneLenclud@223
  1276
            //Do not clear if we gave up on rendering already.
StephaneLenclud@223
  1277
            //This means we will keep on displaying clock on MDM166AA for instance.
StephaneLenclud@223
  1278
            if (!iSkipFrameRendering)
StephaneLenclud@223
  1279
            {
StephaneLenclud@223
  1280
                iDisplay.Clear();
StephaneLenclud@223
  1281
                iDisplay.SwapBuffers();
StephaneLenclud@223
  1282
            }
StephaneLenclud@223
  1283
StephaneLenclud@223
  1284
            iDisplay.SetAllIconsStatus(0); //Turn off all icons
sl@46
  1285
            iDisplay.Close();
sl@46
  1286
        }
sl@46
  1287
sl@46
  1288
        private void buttonOpen_Click(object sender, EventArgs e)
sl@46
  1289
        {
sl@46
  1290
            OpenDisplayConnection();
sl@3
  1291
        }
sl@3
  1292
sl@3
  1293
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
  1294
        {
sl@46
  1295
            CloseDisplayConnection();
sl@3
  1296
        }
sl@3
  1297
sl@3
  1298
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
  1299
        {
sl@3
  1300
            iDisplay.Clear();
sl@3
  1301
            iDisplay.SwapBuffers();
sl@3
  1302
        }
sl@3
  1303
sl@3
  1304
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
  1305
        {
sl@3
  1306
            iDisplay.Fill();
sl@3
  1307
            iDisplay.SwapBuffers();
sl@3
  1308
        }
sl@3
  1309
sl@3
  1310
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
  1311
        {
sl@48
  1312
            cds.Brightness = trackBarBrightness.Value;
sl@9
  1313
            Properties.Settings.Default.Save();
sl@3
  1314
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
  1315
sl@3
  1316
        }
sl@7
  1317
sl@48
  1318
sl@48
  1319
        /// <summary>
sl@48
  1320
        /// CDS stands for Current Display Settings
sl@48
  1321
        /// </summary>
sl@50
  1322
        private DisplaySettings cds
sl@48
  1323
        {
sl@48
  1324
            get
sl@48
  1325
            {
sl@65
  1326
                DisplaysSettings settings = Properties.Settings.Default.DisplaysSettings;
sl@51
  1327
                if (settings == null)
sl@51
  1328
                {
sl@51
  1329
                    settings = new DisplaysSettings();
sl@51
  1330
                    settings.Init();
sl@65
  1331
                    Properties.Settings.Default.DisplaysSettings = settings;
sl@51
  1332
                }
sl@48
  1333
sl@48
  1334
                //Make sure all our settings have been created
sl@48
  1335
                while (settings.Displays.Count <= Properties.Settings.Default.CurrentDisplayIndex)
sl@48
  1336
                {
sl@50
  1337
                    settings.Displays.Add(new DisplaySettings());
sl@48
  1338
                }
sl@48
  1339
sl@50
  1340
                DisplaySettings displaySettings = settings.Displays[Properties.Settings.Default.CurrentDisplayIndex];
sl@48
  1341
                return displaySettings;
sl@48
  1342
            }
sl@48
  1343
        }
sl@48
  1344
sl@54
  1345
        /// <summary>
sl@54
  1346
        /// Check if the given font has a fixed character pitch.
sl@54
  1347
        /// </summary>
sl@54
  1348
        /// <param name="ft"></param>
sl@54
  1349
        /// <returns>0.0f if this is not a monospace font, otherwise returns the character width.</returns>
sl@54
  1350
        public float IsFixedWidth(Font ft)
sl@54
  1351
        {
sl@54
  1352
            Graphics g = CreateGraphics();
StephaneLenclud@223
  1353
            char[] charSizes = new char[] {'i', 'a', 'Z', '%', '#', 'a', 'B', 'l', 'm', ',', '.'};
sl@54
  1354
            float charWidth = g.MeasureString("I", ft, Int32.MaxValue, StringFormat.GenericTypographic).Width;
sl@54
  1355
sl@54
  1356
            bool fixedWidth = true;
sl@54
  1357
sl@54
  1358
            foreach (char c in charSizes)
StephaneLenclud@223
  1359
                if (g.MeasureString(c.ToString(), ft, Int32.MaxValue, StringFormat.GenericTypographic).Width !=
StephaneLenclud@223
  1360
                    charWidth)
sl@54
  1361
                    fixedWidth = false;
sl@54
  1362
sl@54
  1363
            if (fixedWidth)
sl@54
  1364
            {
sl@54
  1365
                return charWidth;
sl@54
  1366
            }
sl@54
  1367
sl@54
  1368
            return 0.0f;
sl@54
  1369
        }
sl@54
  1370
StephaneLenclud@223
  1371
        /// <summary>
StephaneLenclud@223
  1372
        /// Synchronize UI with settings
StephaneLenclud@223
  1373
        /// </summary>
sl@7
  1374
        private void UpdateStatus()
StephaneLenclud@223
  1375
        {
sl@48
  1376
            //Load settings
sl@54
  1377
            checkBoxShowBorders.Checked = cds.ShowBorders;
StephaneLenclud@223
  1378
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders
StephaneLenclud@223
  1379
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1380
                : TableLayoutPanelCellBorderStyle.None);
sl@60
  1381
sl@60
  1382
            //Set the proper font to each of our labels
StephaneLenclud@175
  1383
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
sl@60
  1384
            {
sl@60
  1385
                ctrl.Font = cds.Font;
sl@60
  1386
            }
sl@60
  1387
sl@54
  1388
            CheckFontHeight();
StephaneLenclud@223
  1389
            //Check if "run on Windows startup" is enabled
StephaneLenclud@223
  1390
            checkBoxAutoStart.Checked = iStartupManager.Startup;
StephaneLenclud@268
  1391
            
StephaneLenclud@168
  1392
            //CEC settings
StephaneLenclud@168
  1393
            comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
StephaneLenclud@153
  1394
StephaneLenclud@168
  1395
            //Mini Display settings
sl@48
  1396
            checkBoxReverseScreen.Checked = cds.ReverseScreen;
sl@57
  1397
            checkBoxInverseColors.Checked = cds.InverseColors;
StephaneLenclud@223
  1398
            checkBoxShowVolumeLabel.Checked = cds.ShowVolumeLabel;
sl@100
  1399
            checkBoxScaleToFit.Checked = cds.ScaleToFit;
sl@100
  1400
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1401
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1402
            maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
StephaneLenclud@223
  1403
            maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
sl@48
  1404
            comboBoxDisplayType.SelectedIndex = cds.DisplayType;
StephaneLenclud@270
  1405
            iTimerDisplay.Interval = cds.TimerInterval;
sl@48
  1406
            maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
sl@100
  1407
            textBoxScrollLoopSeparator.Text = cds.Separator;
sl@58
  1408
            //
sl@58
  1409
            SetupPixelDelegates();
sl@48
  1410
sl@7
  1411
            if (iDisplay.IsOpen())
sl@7
  1412
            {
StephaneLenclud@187
  1413
                //We have a display connection
StephaneLenclud@187
  1414
                //Reflect that in our UI
StephaneLenclud@187
  1415
                StartTimer();
StephaneLenclud@103
  1416
StephaneLenclud@223
  1417
                iTableLayoutPanel.Enabled = true;
StephaneLenclud@223
  1418
                panelDisplay.Enabled = true;
StephaneLenclud@103
  1419
sl@48
  1420
                //Only setup brightness if display is open
sl@48
  1421
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1422
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@223
  1423
                if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@223
  1424
                {
StephaneLenclud@223
  1425
                    //Brightness out of range, this can occur when using auto-detect
StephaneLenclud@223
  1426
                    //Use max brightness instead
StephaneLenclud@223
  1427
                    trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@223
  1428
                    iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@223
  1429
                }
StephaneLenclud@223
  1430
                else
StephaneLenclud@223
  1431
                {
StephaneLenclud@223
  1432
                    trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@223
  1433
                    iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@223
  1434
                }
StephaneLenclud@223
  1435
StephaneLenclud@223
  1436
                //Try compute the steps to something that makes sense
StephaneLenclud@223
  1437
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@48
  1438
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@223
  1439
sl@48
  1440
                //
sl@7
  1441
                buttonFill.Enabled = true;
sl@7
  1442
                buttonClear.Enabled = true;
sl@7
  1443
                buttonOpen.Enabled = false;
sl@7
  1444
                buttonClose.Enabled = true;
sl@7
  1445
                trackBarBrightness.Enabled = true;
sl@10
  1446
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1447
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1448
sl@52
  1449
                if (iDisplay.SupportPowerOnOff())
sl@52
  1450
                {
sl@52
  1451
                    buttonPowerOn.Enabled = true;
sl@52
  1452
                    buttonPowerOff.Enabled = true;
sl@52
  1453
                }
sl@52
  1454
                else
sl@52
  1455
                {
sl@52
  1456
                    buttonPowerOn.Enabled = false;
sl@52
  1457
                    buttonPowerOff.Enabled = false;
sl@52
  1458
                }
sl@53
  1459
sl@53
  1460
                if (iDisplay.SupportClock())
sl@53
  1461
                {
sl@53
  1462
                    buttonShowClock.Enabled = true;
sl@53
  1463
                    buttonHideClock.Enabled = true;
sl@53
  1464
                }
sl@53
  1465
                else
sl@53
  1466
                {
sl@53
  1467
                    buttonShowClock.Enabled = false;
sl@53
  1468
                    buttonHideClock.Enabled = false;
sl@53
  1469
                }
StephaneLenclud@115
  1470
StephaneLenclud@223
  1471
StephaneLenclud@223
  1472
                //Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@223
  1473
                checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel) > 0;
StephaneLenclud@223
  1474
StephaneLenclud@223
  1475
                if (cds.ShowVolumeLabel)
StephaneLenclud@223
  1476
                {
StephaneLenclud@135
  1477
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1478
                }
StephaneLenclud@223
  1479
                else
StephaneLenclud@223
  1480
                {
StephaneLenclud@135
  1481
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1482
                }
sl@7
  1483
            }
sl@7
  1484
            else
sl@7
  1485
            {
StephaneLenclud@187
  1486
                //Display connection not available
StephaneLenclud@187
  1487
                //Reflect that in our UI
StephaneLenclud@187
  1488
#if DEBUG
StephaneLenclud@187
  1489
                //In debug start our timer even if we don't have a display connection
StephaneLenclud@187
  1490
                StartTimer();
StephaneLenclud@187
  1491
#else
StephaneLenclud@223
  1492
    //In production environment we don't need our timer if no display connection
StephaneLenclud@187
  1493
                StopTimer();
StephaneLenclud@187
  1494
#endif
StephaneLenclud@187
  1495
                checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@223
  1496
                iTableLayoutPanel.Enabled = false;
StephaneLenclud@223
  1497
                panelDisplay.Enabled = false;
sl@7
  1498
                buttonFill.Enabled = false;
sl@7
  1499
                buttonClear.Enabled = false;
sl@7
  1500
                buttonOpen.Enabled = true;
sl@7
  1501
                buttonClose.Enabled = false;
sl@7
  1502
                trackBarBrightness.Enabled = false;
sl@52
  1503
                buttonPowerOn.Enabled = false;
sl@52
  1504
                buttonPowerOff.Enabled = false;
sl@53
  1505
                buttonShowClock.Enabled = false;
sl@53
  1506
                buttonHideClock.Enabled = false;
sl@9
  1507
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1508
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1509
            }
StephaneLenclud@106
  1510
sl@7
  1511
        }
sl@9
  1512
sl@13
  1513
StephaneLenclud@223
  1514
        /// <summary>
StephaneLenclud@223
  1515
        /// 
StephaneLenclud@223
  1516
        /// </summary>
StephaneLenclud@223
  1517
        /// <param name="sender"></param>
StephaneLenclud@223
  1518
        /// <param name="e"></param>
StephaneLenclud@223
  1519
        private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1520
        {
StephaneLenclud@223
  1521
            cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@223
  1522
            Properties.Settings.Default.Save();
StephaneLenclud@223
  1523
            UpdateStatus();
StephaneLenclud@223
  1524
        }
sl@13
  1525
sl@9
  1526
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1527
        {
sl@16
  1528
            //Save our show borders setting
StephaneLenclud@223
  1529
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked
StephaneLenclud@223
  1530
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1531
                : TableLayoutPanelCellBorderStyle.None);
sl@48
  1532
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1533
            Properties.Settings.Default.Save();
sl@57
  1534
            CheckFontHeight();
sl@9
  1535
        }
sl@13
  1536
StephaneLenclud@194
  1537
        private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1538
        {
StephaneLenclud@223
  1539
            iStartupManager.Startup = checkBoxAutoStart.Checked;
StephaneLenclud@223
  1540
        }
sl@94
  1541
sl@94
  1542
sl@16
  1543
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1544
        {
sl@16
  1545
            //Save our reverse screen setting
sl@48
  1546
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1547
            Properties.Settings.Default.Save();
sl@58
  1548
            SetupPixelDelegates();
sl@16
  1549
        }
sl@16
  1550
sl@57
  1551
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1552
        {
sl@57
  1553
            //Save our inverse colors setting
sl@57
  1554
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1555
            Properties.Settings.Default.Save();
sl@58
  1556
            SetupPixelDelegates();
sl@57
  1557
        }
sl@57
  1558
sl@100
  1559
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1560
        {
sl@100
  1561
            //Save our scale to fit setting
sl@100
  1562
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1563
            Properties.Settings.Default.Save();
sl@100
  1564
            //
sl@100
  1565
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1566
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1567
        }
sl@100
  1568
sl@14
  1569
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1570
        {
sl@14
  1571
            if (WindowState == FormWindowState.Minimized)
sl@14
  1572
            {
StephaneLenclud@158
  1573
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1574
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1575
                iCreateBitmap = true;
sl@14
  1576
            }
sl@14
  1577
        }
sl@14
  1578
sl@17
  1579
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1580
        {
StephaneLenclud@273
  1581
            //TODO: discard other CSCore audio objects
StephaneLenclud@273
  1582
            StopAudioVisualization();
StephaneLenclud@167
  1583
            iCecManager.Stop();
StephaneLenclud@223
  1584
            iNetworkManager.Dispose();
StephaneLenclud@223
  1585
            CloseDisplayConnection();
sl@17
  1586
            StopServer();
sl@32
  1587
            e.Cancel = iClosing;
sl@17
  1588
        }
sl@17
  1589
sl@17
  1590
        public void StartServer()
sl@17
  1591
        {
sl@17
  1592
            iServiceHost = new ServiceHost
sl@17
  1593
                (
StephaneLenclud@223
  1594
                typeof(Session),
StephaneLenclud@223
  1595
                new Uri[] {new Uri("net.tcp://localhost:8001/")}
sl@17
  1596
                );
sl@17
  1597
StephaneLenclud@223
  1598
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true),
StephaneLenclud@223
  1599
                "DisplayService");
sl@17
  1600
            iServiceHost.Open();
sl@17
  1601
        }
sl@17
  1602
sl@17
  1603
        public void StopServer()
sl@17
  1604
        {
sl@32
  1605
            if (iClients.Count > 0 && !iClosing)
sl@29
  1606
            {
sl@29
  1607
                //Tell our clients
sl@32
  1608
                iClosing = true;
sl@29
  1609
                BroadcastCloseEvent();
sl@29
  1610
            }
sl@32
  1611
            else if (iClosing)
sl@32
  1612
            {
StephaneLenclud@223
  1613
                if (
StephaneLenclud@223
  1614
                    MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo,
StephaneLenclud@223
  1615
                        MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1616
                {
sl@32
  1617
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1618
                }
sl@32
  1619
            }
sl@32
  1620
            else
sl@36
  1621
            {
sl@32
  1622
                //We removed that as it often lags for some reason
sl@32
  1623
                //iServiceHost.Close();
sl@32
  1624
            }
sl@17
  1625
        }
sl@17
  1626
sl@21
  1627
        public void BroadcastCloseEvent()
sl@21
  1628
        {
sl@31
  1629
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1630
sl@21
  1631
            var inactiveClients = new List<string>();
sl@21
  1632
            foreach (var client in iClients)
sl@21
  1633
            {
sl@21
  1634
                //if (client.Key != eventData.ClientName)
sl@21
  1635
                {
sl@21
  1636
                    try
sl@21
  1637
                    {
sl@31
  1638
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
StephaneLenclud@223
  1639
                        client.Value.Callback.OnCloseOrder( /*eventData*/);
sl@21
  1640
                    }
sl@21
  1641
                    catch (Exception ex)
sl@21
  1642
                    {
sl@21
  1643
                        inactiveClients.Add(client.Key);
sl@21
  1644
                    }
sl@21
  1645
                }
sl@21
  1646
            }
sl@21
  1647
sl@21
  1648
            if (inactiveClients.Count > 0)
sl@21
  1649
            {
sl@21
  1650
                foreach (var client in inactiveClients)
sl@21
  1651
                {
sl@21
  1652
                    iClients.Remove(client);
StephaneLenclud@226
  1653
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1654
                        Program.iFormMain.iTreeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1655
                }
sl@21
  1656
            }
sl@97
  1657
StephaneLenclud@223
  1658
            if (iClients.Count == 0)
StephaneLenclud@223
  1659
            {
StephaneLenclud@223
  1660
                ClearLayout();
StephaneLenclud@223
  1661
            }
sl@21
  1662
        }
sl@21
  1663
StephaneLenclud@223
  1664
        /// <summary>
StephaneLenclud@223
  1665
        /// Just remove all our fields.
StephaneLenclud@223
  1666
        /// </summary>
StephaneLenclud@223
  1667
        private void ClearLayout()
StephaneLenclud@223
  1668
        {
StephaneLenclud@223
  1669
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@223
  1670
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@223
  1671
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@223
  1672
            iCurrentClientData = null;
StephaneLenclud@223
  1673
        }
StephaneLenclud@223
  1674
StephaneLenclud@223
  1675
        /// <summary>
StephaneLenclud@223
  1676
        /// Just launch a demo client.
StephaneLenclud@223
  1677
        /// </summary>
StephaneLenclud@223
  1678
        private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@223
  1679
        {
StephaneLenclud@223
  1680
            Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@223
  1681
            SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(
StephaneLenclud@223
  1682
                new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@223
  1683
            clientThread.Start(myParams);
StephaneLenclud@223
  1684
            BringToFront();
StephaneLenclud@223
  1685
        }
StephaneLenclud@106
  1686
StephaneLenclud@189
  1687
        /// <summary>
StephaneLenclud@189
  1688
        /// Just launch our idle client.
StephaneLenclud@189
  1689
        /// </summary>
StephaneLenclud@189
  1690
        private void StartIdleClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@189
  1691
        {
StephaneLenclud@225
  1692
            Thread clientThread = new Thread(SharpDisplayClientIdle.Program.MainWithParams);
StephaneLenclud@225
  1693
            SharpDisplayClientIdle.StartParams myParams =
StephaneLenclud@225
  1694
                new SharpDisplayClientIdle.StartParams(new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@189
  1695
            clientThread.Start(myParams);
StephaneLenclud@189
  1696
            BringToFront();
StephaneLenclud@189
  1697
        }
StephaneLenclud@189
  1698
StephaneLenclud@189
  1699
sl@25
  1700
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1701
        {
StephaneLenclud@223
  1702
            StartNewClient();
sl@25
  1703
        }
sl@25
  1704
sl@27
  1705
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1706
        {
StephaneLenclud@187
  1707
            ToggleTimer();
StephaneLenclud@187
  1708
        }
StephaneLenclud@187
  1709
StephaneLenclud@187
  1710
        private void StartTimer()
StephaneLenclud@187
  1711
        {
StephaneLenclud@187
  1712
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1713
            iTimerDisplay.Enabled = true;
StephaneLenclud@187
  1714
            UpdateSuspendButton();
StephaneLenclud@187
  1715
        }
StephaneLenclud@187
  1716
StephaneLenclud@187
  1717
        private void StopTimer()
StephaneLenclud@187
  1718
        {
StephaneLenclud@187
  1719
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1720
            iTimerDisplay.Enabled = false;
StephaneLenclud@187
  1721
            UpdateSuspendButton();
StephaneLenclud@187
  1722
        }
StephaneLenclud@187
  1723
StephaneLenclud@187
  1724
        private void ToggleTimer()
StephaneLenclud@187
  1725
        {
sl@52
  1726
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1727
            iTimerDisplay.Enabled = !iTimerDisplay.Enabled;
StephaneLenclud@187
  1728
            UpdateSuspendButton();
StephaneLenclud@187
  1729
        }
StephaneLenclud@187
  1730
StephaneLenclud@187
  1731
        private void UpdateSuspendButton()
StephaneLenclud@187
  1732
        {
StephaneLenclud@270
  1733
            if (!iTimerDisplay.Enabled)
sl@27
  1734
            {
sl@52
  1735
                buttonSuspend.Text = "Run";
sl@27
  1736
            }
sl@27
  1737
            else
sl@27
  1738
            {
sl@27
  1739
                buttonSuspend.Text = "Pause";
sl@27
  1740
            }
sl@27
  1741
        }
sl@27
  1742
StephaneLenclud@187
  1743
sl@29
  1744
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1745
        {
sl@29
  1746
            BroadcastCloseEvent();
sl@29
  1747
        }
sl@29
  1748
sl@30
  1749
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1750
        {
StephaneLenclud@141
  1751
            //Root node must have at least one child
StephaneLenclud@141
  1752
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1753
            {
StephaneLenclud@141
  1754
                return;
StephaneLenclud@141
  1755
            }
sl@21
  1756
StephaneLenclud@141
  1757
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@223
  1758
            string sessionId = e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1759
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1760
            {
StephaneLenclud@141
  1761
                //We have a valid session just switch to that client
StephaneLenclud@223
  1762
                SetCurrentClient(sessionId, true);
StephaneLenclud@141
  1763
            }
StephaneLenclud@223
  1764
sl@30
  1765
        }
sl@30
  1766
sl@36
  1767
sl@30
  1768
        /// <summary>
sl@36
  1769
        ///
sl@30
  1770
        /// </summary>
sl@30
  1771
        /// <param name="aSessionId"></param>
sl@30
  1772
        /// <param name="aCallback"></param>
sl@55
  1773
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1774
        {
sl@33
  1775
            if (this.InvokeRequired)
sl@30
  1776
            {
sl@30
  1777
                //Not in the proper thread, invoke ourselves
sl@30
  1778
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
StephaneLenclud@223
  1779
                this.Invoke(d, new object[] {aSessionId, aCallback});
sl@30
  1780
            }
sl@30
  1781
            else
sl@30
  1782
            {
sl@30
  1783
                //We are in the proper thread
sl@30
  1784
                //Add this session to our collection of clients
sl@33
  1785
                ClientData newClient = new ClientData(aSessionId, aCallback);
StephaneLenclud@226
  1786
                Program.iFormMain.iClients.Add(aSessionId, newClient);
sl@30
  1787
                //Add this session to our UI
sl@33
  1788
                UpdateClientTreeViewNode(newClient);
sl@30
  1789
            }
sl@30
  1790
        }
sl@30
  1791
Stephane@186
  1792
Stephane@186
  1793
        /// <summary>
Stephane@186
  1794
        /// Find the client with the highest priority if any.
Stephane@186
  1795
        /// </summary>
Stephane@186
  1796
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1797
        public ClientData FindHighestPriorityClient()
Stephane@186
  1798
        {
Stephane@186
  1799
            ClientData highestPriorityClient = null;
Stephane@186
  1800
            foreach (var client in iClients)
Stephane@186
  1801
            {
Stephane@186
  1802
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1803
                {
Stephane@186
  1804
                    highestPriorityClient = client.Value;
Stephane@186
  1805
                }
Stephane@186
  1806
            }
Stephane@186
  1807
Stephane@186
  1808
            return highestPriorityClient;
Stephane@186
  1809
        }
Stephane@186
  1810
sl@30
  1811
        /// <summary>
sl@36
  1812
        ///
sl@30
  1813
        /// </summary>
sl@30
  1814
        /// <param name="aSessionId"></param>
sl@30
  1815
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1816
        {
sl@33
  1817
            if (this.InvokeRequired)
sl@30
  1818
            {
sl@30
  1819
                //Not in the proper thread, invoke ourselves
sl@30
  1820
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
StephaneLenclud@223
  1821
                this.Invoke(d, new object[] {aSessionId});
sl@30
  1822
            }
sl@30
  1823
            else
sl@30
  1824
            {
sl@30
  1825
                //We are in the proper thread
sl@33
  1826
                //Remove this session from both client collection and UI tree view
StephaneLenclud@226
  1827
                if (Program.iFormMain.iClients.Keys.Contains(aSessionId))
sl@30
  1828
                {
StephaneLenclud@226
  1829
                    Program.iFormMain.iClients.Remove(aSessionId);
StephaneLenclud@226
  1830
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1831
                        Program.iFormMain.iTreeViewClients.Nodes.Find(aSessionId, false)[0]);
StephaneLenclud@188
  1832
                    //Update recording status too whenever a client is removed
StephaneLenclud@188
  1833
                    UpdateRecordingNotification();
sl@32
  1834
                }
sl@32
  1835
Stephane@186
  1836
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1837
                {
Stephane@186
  1838
                    //The current client is closing
Stephane@186
  1839
                    iCurrentClientData = null;
Stephane@186
  1840
                    //Find the client with the highest priority and set it as current
Stephane@186
  1841
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  1842
                    if (newCurrentClient != null)
Stephane@186
  1843
                    {
Stephane@186
  1844
                        SetCurrentClient(newCurrentClient.SessionId, true);
StephaneLenclud@223
  1845
                    }
Stephane@186
  1846
                }
Stephane@186
  1847
Stephane@186
  1848
                if (iClients.Count == 0)
StephaneLenclud@223
  1849
                {
StephaneLenclud@223
  1850
                    //Clear our screen when last client disconnects
StephaneLenclud@223
  1851
                    ClearLayout();
StephaneLenclud@223
  1852
StephaneLenclud@223
  1853
                    if (iClosing)
StephaneLenclud@223
  1854
                    {
StephaneLenclud@223
  1855
                        //We were closing our form
StephaneLenclud@223
  1856
                        //All clients are now closed
StephaneLenclud@223
  1857
                        //Just resume our close operation
StephaneLenclud@223
  1858
                        iClosing = false;
StephaneLenclud@223
  1859
                        Close();
StephaneLenclud@223
  1860
                    }
StephaneLenclud@223
  1861
                }
sl@30
  1862
            }
sl@30
  1863
        }
sl@30
  1864
sl@30
  1865
        /// <summary>
sl@36
  1866
        ///
sl@30
  1867
        /// </summary>
sl@62
  1868
        /// <param name="aSessionId"></param>
sl@72
  1869
        /// <param name="aLayout"></param>
sl@62
  1870
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1871
        {
sl@62
  1872
            if (this.InvokeRequired)
sl@62
  1873
            {
sl@62
  1874
                //Not in the proper thread, invoke ourselves
sl@62
  1875
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
StephaneLenclud@223
  1876
                this.Invoke(d, new object[] {aSessionId, aLayout});
sl@62
  1877
            }
sl@62
  1878
            else
sl@62
  1879
            {
sl@62
  1880
                ClientData client = iClients[aSessionId];
sl@62
  1881
                if (client != null)
sl@62
  1882
                {
StephaneLenclud@148
  1883
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1884
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1885
                    {
StephaneLenclud@158
  1886
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1887
                        //Set our client layout then
StephaneLenclud@148
  1888
                        client.Layout = aLayout;
StephaneLenclud@175
  1889
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1890
                        client.HasNewLayout = true;
StephaneLenclud@158
  1891
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1892
                        client.Fields.Clear();
StephaneLenclud@148
  1893
                        //
StephaneLenclud@148
  1894
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1895
                    }
StephaneLenclud@158
  1896
                    else
StephaneLenclud@158
  1897
                    {
StephaneLenclud@158
  1898
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1899
                    }
sl@62
  1900
                }
sl@62
  1901
            }
sl@62
  1902
        }
sl@62
  1903
sl@62
  1904
        /// <summary>
sl@62
  1905
        ///
sl@62
  1906
        /// </summary>
sl@67
  1907
        /// <param name="aSessionId"></param>
sl@72
  1908
        /// <param name="aField"></param>
sl@75
  1909
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1910
        {
sl@33
  1911
            if (this.InvokeRequired)
sl@30
  1912
            {
sl@30
  1913
                //Not in the proper thread, invoke ourselves
sl@79
  1914
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
StephaneLenclud@223
  1915
                this.Invoke(d, new object[] {aSessionId, aField});
sl@30
  1916
            }
sl@30
  1917
            else
sl@30
  1918
            {
sl@75
  1919
                //We are in the proper thread
sl@75
  1920
                //Call the non-thread-safe variant
sl@75
  1921
                SetClientField(aSessionId, aField);
sl@75
  1922
            }
sl@75
  1923
        }
sl@75
  1924
StephaneLenclud@176
  1925
StephaneLenclud@176
  1926
StephaneLenclud@176
  1927
sl@75
  1928
        /// <summary>
StephaneLenclud@175
  1929
        /// Set a data field in the given client.
sl@75
  1930
        /// </summary>
sl@75
  1931
        /// <param name="aSessionId"></param>
sl@75
  1932
        /// <param name="aField"></param>
sl@75
  1933
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@223
  1934
        {
StephaneLenclud@141
  1935
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1936
sl@75
  1937
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1938
            bool layoutChanged = false;
StephaneLenclud@148
  1939
            bool contentChanged = true;
StephaneLenclud@141
  1940
StephaneLenclud@176
  1941
            //Fetch our field index
StephaneLenclud@176
  1942
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1943
StephaneLenclud@176
  1944
            if (fieldIndex < 0)
sl@75
  1945
            {
StephaneLenclud@176
  1946
                //No corresponding field, just bail out
StephaneLenclud@176
  1947
                return;
StephaneLenclud@141
  1948
            }
sl@76
  1949
StephaneLenclud@175
  1950
            //Keep our previous field in there
StephaneLenclud@176
  1951
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1952
            //Just update that field then 
StephaneLenclud@176
  1953
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1954
StephaneLenclud@176
  1955
            if (!aField.IsTableField)
StephaneLenclud@176
  1956
            {
StephaneLenclud@176
  1957
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1958
                return;
StephaneLenclud@176
  1959
            }
StephaneLenclud@176
  1960
StephaneLenclud@176
  1961
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1962
StephaneLenclud@175
  1963
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1964
            {
StephaneLenclud@141
  1965
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1966
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1967
                {
StephaneLenclud@223
  1968
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1969
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1970
                    {
StephaneLenclud@274
  1971
                        TextField textField = (TextField)aField;
sl@75
  1972
                        //Text field control already in place, just change the text
StephaneLenclud@274
  1973
                        MarqueeLabel label = (MarqueeLabel)ctrl;
StephaneLenclud@172
  1974
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1975
                        label.Text = textField.Text;
StephaneLenclud@172
  1976
                        label.TextAlign = textField.Alignment;
sl@68
  1977
                    }
StephaneLenclud@176
  1978
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1979
                    {
StephaneLenclud@274
  1980
                        BitmapField bitmapField = (BitmapField)aField;
StephaneLenclud@148
  1981
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1982
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@274
  1983
                        PictureBox pictureBox = (PictureBox)ctrl;
StephaneLenclud@172
  1984
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1985
                    }
StephaneLenclud@274
  1986
                    else if (aField is AudioVisualizerField && ctrl is PictureBox)
StephaneLenclud@274
  1987
                    {
StephaneLenclud@274
  1988
                        contentChanged = false; // Since nothing was changed
StephaneLenclud@274
  1989
                    }
sl@68
  1990
                    else
sl@68
  1991
                    {
StephaneLenclud@141
  1992
                        layoutChanged = true;
sl@68
  1993
                    }
sl@30
  1994
                }
StephaneLenclud@141
  1995
            }
StephaneLenclud@141
  1996
            else
StephaneLenclud@223
  1997
            {
StephaneLenclud@141
  1998
                layoutChanged = true;
StephaneLenclud@141
  1999
            }
StephaneLenclud@141
  2000
StephaneLenclud@148
  2001
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  2002
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  2003
            {
StephaneLenclud@141
  2004
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  2005
                //
StephaneLenclud@148
  2006
                if (layoutChanged)
sl@75
  2007
                {
StephaneLenclud@148
  2008
                    Debug.Print("Layout changed");
StephaneLenclud@148
  2009
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  2010
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  2011
                    {
StephaneLenclud@148
  2012
                        //Apply layout and set data fields.
StephaneLenclud@148
  2013
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  2014
                    }
sl@75
  2015
                }
StephaneLenclud@158
  2016
                else
StephaneLenclud@158
  2017
                {
StephaneLenclud@158
  2018
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  2019
                }
StephaneLenclud@158
  2020
            }
StephaneLenclud@158
  2021
            else
StephaneLenclud@158
  2022
            {
StephaneLenclud@158
  2023
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  2024
            }
sl@75
  2025
StephaneLenclud@141
  2026
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  2027
            SetCurrentClient(aSessionId);
sl@30
  2028
        }
sl@30
  2029
sl@30
  2030
        /// <summary>
sl@36
  2031
        ///
sl@30
  2032
        /// </summary>
sl@30
  2033
        /// <param name="aTexts"></param>
sl@75
  2034
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  2035
        {
sl@33
  2036
            if (this.InvokeRequired)
sl@30
  2037
            {
sl@30
  2038
                //Not in the proper thread, invoke ourselves
sl@75
  2039
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
StephaneLenclud@223
  2040
                this.Invoke(d, new object[] {aSessionId, aFields});
sl@30
  2041
            }
sl@30
  2042
            else
sl@30
  2043
            {
StephaneLenclud@175
  2044
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  2045
StephaneLenclud@175
  2046
                if (client.HasNewLayout)
sl@30
  2047
                {
StephaneLenclud@175
  2048
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  2049
                    //Our layout was just changed
StephaneLenclud@175
  2050
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  2051
                    client.HasNewLayout = false;
StephaneLenclud@175
  2052
                    //Just set all our fields then
StephaneLenclud@175
  2053
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  2054
                    //Try switch to that client
StephaneLenclud@175
  2055
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  2056
StephaneLenclud@175
  2057
                    //If we are updating the current client update our panel
StephaneLenclud@175
  2058
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  2059
                    {
StephaneLenclud@175
  2060
                        //Apply layout and set data fields.
StephaneLenclud@175
  2061
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  2062
                    }
StephaneLenclud@176
  2063
StephaneLenclud@176
  2064
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  2065
                }
StephaneLenclud@175
  2066
                else
StephaneLenclud@175
  2067
                {
StephaneLenclud@175
  2068
                    //Put each our text fields in a label control
StephaneLenclud@175
  2069
                    foreach (DataField field in aFields)
StephaneLenclud@175
  2070
                    {
StephaneLenclud@175
  2071
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  2072
                    }
sl@30
  2073
                }
sl@30
  2074
            }
sl@32
  2075
        }
sl@30
  2076
sl@67
  2077
        /// <summary>
sl@67
  2078
        ///
sl@67
  2079
        /// </summary>
sl@67
  2080
        /// <param name="aSessionId"></param>
sl@32
  2081
        /// <param name="aName"></param>
sl@32
  2082
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  2083
        {
sl@32
  2084
            if (this.InvokeRequired)
sl@32
  2085
            {
sl@32
  2086
                //Not in the proper thread, invoke ourselves
sl@32
  2087
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
StephaneLenclud@223
  2088
                this.Invoke(d, new object[] {aSessionId, aName});
sl@32
  2089
            }
sl@32
  2090
            else
sl@32
  2091
            {
sl@32
  2092
                //We are in the proper thread
sl@33
  2093
                //Get our client
sl@33
  2094
                ClientData client = iClients[aSessionId];
sl@33
  2095
                if (client != null)
sl@32
  2096
                {
sl@33
  2097
                    //Set its name
sl@33
  2098
                    client.Name = aName;
sl@33
  2099
                    //Update our tree-view
sl@33
  2100
                    UpdateClientTreeViewNode(client);
sl@33
  2101
                }
sl@33
  2102
            }
sl@33
  2103
        }
sl@33
  2104
StephaneLenclud@184
  2105
        ///
StephaneLenclud@184
  2106
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  2107
        {
StephaneLenclud@184
  2108
            if (this.InvokeRequired)
StephaneLenclud@184
  2109
            {
StephaneLenclud@184
  2110
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  2111
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@223
  2112
                this.Invoke(d, new object[] {aSessionId, aPriority});
StephaneLenclud@184
  2113
            }
StephaneLenclud@184
  2114
            else
StephaneLenclud@184
  2115
            {
StephaneLenclud@184
  2116
                //We are in the proper thread
StephaneLenclud@184
  2117
                //Get our client
StephaneLenclud@184
  2118
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  2119
                if (client != null)
StephaneLenclud@184
  2120
                {
StephaneLenclud@184
  2121
                    //Set its name
StephaneLenclud@184
  2122
                    client.Priority = aPriority;
StephaneLenclud@184
  2123
                    //Update our tree-view
StephaneLenclud@184
  2124
                    UpdateClientTreeViewNode(client);
Stephane@186
  2125
                    //Change our current client as per new priority
Stephane@186
  2126
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  2127
                    if (newCurrentClient != null)
Stephane@186
  2128
                    {
Stephane@186
  2129
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  2130
                    }
StephaneLenclud@184
  2131
                }
StephaneLenclud@184
  2132
            }
StephaneLenclud@184
  2133
        }
StephaneLenclud@184
  2134
sl@33
  2135
        /// <summary>
StephaneLenclud@183
  2136
        /// 
StephaneLenclud@183
  2137
        /// </summary>
StephaneLenclud@183
  2138
        /// <param name="value"></param>
StephaneLenclud@183
  2139
        /// <param name="maxChars"></param>
StephaneLenclud@183
  2140
        /// <returns></returns>
StephaneLenclud@183
  2141
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  2142
        {
StephaneLenclud@223
  2143
            return value.Length <= maxChars ? value : value.Substring(0, maxChars - 3) + "...";
StephaneLenclud@183
  2144
        }
StephaneLenclud@183
  2145
StephaneLenclud@183
  2146
        /// <summary>
StephaneLenclud@180
  2147
        /// Update our recording notification.
StephaneLenclud@180
  2148
        /// </summary>
StephaneLenclud@180
  2149
        private void UpdateRecordingNotification()
StephaneLenclud@180
  2150
        {
StephaneLenclud@180
  2151
            //Go through each 
StephaneLenclud@180
  2152
            bool activeRecording = false;
StephaneLenclud@223
  2153
            string text = "";
StephaneLenclud@223
  2154
            RecordingField recField = new RecordingField();
StephaneLenclud@180
  2155
            foreach (var client in iClients)
StephaneLenclud@180
  2156
            {
StephaneLenclud@223
  2157
                RecordingField rec = (RecordingField) client.Value.FindSameFieldAs(recField);
StephaneLenclud@223
  2158
                if (rec != null && rec.IsActive)
StephaneLenclud@180
  2159
                {
StephaneLenclud@180
  2160
                    activeRecording = true;
StephaneLenclud@183
  2161
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  2162
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  2163
                    {
StephaneLenclud@183
  2164
                        text += (rec.Text + "\n");
StephaneLenclud@183
  2165
                    }
StephaneLenclud@183
  2166
                    else
StephaneLenclud@183
  2167
                    {
StephaneLenclud@183
  2168
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  2169
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  2170
                    }
StephaneLenclud@223
  2171
StephaneLenclud@180
  2172
                }
StephaneLenclud@180
  2173
            }
StephaneLenclud@180
  2174
StephaneLenclud@183
  2175
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@223
  2176
            iRecordingNotification.Text = Truncate(text, 63);
StephaneLenclud@183
  2177
StephaneLenclud@180
  2178
            //Change visibility of notification if needed
StephaneLenclud@180
  2179
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@223
  2180
            {
StephaneLenclud@180
  2181
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  2182
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  2183
                //Take care of our REC icon
StephaneLenclud@183
  2184
                if (iDisplay.IsOpen())
StephaneLenclud@183
  2185
                {
StephaneLenclud@183
  2186
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@223
  2187
                }
StephaneLenclud@180
  2188
            }
StephaneLenclud@180
  2189
        }
StephaneLenclud@180
  2190
StephaneLenclud@180
  2191
        /// <summary>
sl@36
  2192
        ///
sl@33
  2193
        /// </summary>
sl@33
  2194
        /// <param name="aClient"></param>
sl@33
  2195
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  2196
        {
StephaneLenclud@148
  2197
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  2198
sl@33
  2199
            if (aClient == null)
sl@33
  2200
            {
sl@33
  2201
                return;
sl@33
  2202
            }
sl@33
  2203
StephaneLenclud@180
  2204
            //Hook in record icon update too
StephaneLenclud@180
  2205
            UpdateRecordingNotification();
StephaneLenclud@180
  2206
sl@33
  2207
            TreeNode node = null;
sl@33
  2208
            //Check that our client node already exists
sl@33
  2209
            //Get our client root node using its key which is our session ID
StephaneLenclud@188
  2210
            TreeNode[] nodes = iTreeViewClients.Nodes.Find(aClient.SessionId, false);
StephaneLenclud@223
  2211
            if (nodes.Count() > 0)
sl@33
  2212
            {
sl@33
  2213
                //We already have a node for that client
sl@33
  2214
                node = nodes[0];
sl@33
  2215
                //Clear children as we are going to recreate them below
sl@33
  2216
                node.Nodes.Clear();
sl@33
  2217
            }
sl@33
  2218
            else
sl@33
  2219
            {
sl@33
  2220
                //Client node does not exists create a new one
StephaneLenclud@188
  2221
                iTreeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
StephaneLenclud@188
  2222
                node = iTreeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  2223
            }
sl@33
  2224
sl@33
  2225
            if (node != null)
sl@33
  2226
            {
sl@33
  2227
                //Change its name
StephaneLenclud@184
  2228
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  2229
                {
StephaneLenclud@184
  2230
                    //We have a name, use it as text for our root node
sl@33
  2231
                    node.Text = aClient.Name;
sl@32
  2232
                    //Add a child with SessionId
sl@33
  2233
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  2234
                }
sl@33
  2235
                else
sl@33
  2236
                {
sl@33
  2237
                    //No name, use session ID instead
sl@33
  2238
                    node.Text = aClient.SessionId;
sl@33
  2239
                }
sl@36
  2240
StephaneLenclud@184
  2241
                //Display client priority
StephaneLenclud@184
  2242
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  2243
sl@67
  2244
                if (aClient.Fields.Count > 0)
sl@33
  2245
                {
sl@33
  2246
                    //Create root node for our texts
sl@70
  2247
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  2248
                    node.Nodes.Add(textsRoot);
sl@33
  2249
                    //For each text add a new entry
sl@67
  2250
                    foreach (DataField field in aClient.Fields)
sl@33
  2251
                    {
StephaneLenclud@172
  2252
                        if (field.IsTextField)
sl@67
  2253
                        {
StephaneLenclud@223
  2254
                            TextField textField = (TextField) field;
sl@70
  2255
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  2256
                        }
StephaneLenclud@172
  2257
                        else if (field.IsBitmapField)
sl@67
  2258
                        {
sl@72
  2259
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  2260
                        }
StephaneLenclud@274
  2261
                        else if (field is AudioVisualizerField)
StephaneLenclud@274
  2262
                        {
StephaneLenclud@274
  2263
                            textsRoot.Nodes.Add(new TreeNode("[Audio Visualizer]"));
StephaneLenclud@274
  2264
                        }
StephaneLenclud@172
  2265
                        else if (field.IsRecordingField)
StephaneLenclud@172
  2266
                        {
StephaneLenclud@223
  2267
                            RecordingField recordingField = (RecordingField) field;
StephaneLenclud@177
  2268
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  2269
                        }
sl@33
  2270
                    }
sl@32
  2271
                }
sl@34
  2272
sl@34
  2273
                node.ExpandAll();
sl@32
  2274
            }
sl@30
  2275
        }
sl@17
  2276
sl@60
  2277
        /// <summary>
sl@60
  2278
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  2279
        /// </summary>
sl@60
  2280
        private void UpdateTableLayoutRowStyles()
sl@60
  2281
        {
StephaneLenclud@175
  2282
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  2283
            {
sl@60
  2284
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@223
  2285
                rowStyle.Height = 100/iTableLayoutPanel.RowCount;
sl@60
  2286
            }
sl@60
  2287
        }
sl@60
  2288
sl@63
  2289
        /// <summary>
sl@63
  2290
        /// Update our display table layout.
StephaneLenclud@175
  2291
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  2292
        /// Fields must be specified by rows from the left.
sl@63
  2293
        /// </summary>
sl@63
  2294
        /// <param name="aLayout"></param>
sl@65
  2295
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  2296
        {
StephaneLenclud@175
  2297
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  2298
StephaneLenclud@223
  2299
            if (aClient == null)
StephaneLenclud@223
  2300
            {
StephaneLenclud@223
  2301
                //Just drop it
StephaneLenclud@223
  2302
                return;
StephaneLenclud@223
  2303
            }
StephaneLenclud@106
  2304
StephaneLenclud@106
  2305
sl@65
  2306
            TableLayout layout = aClient.Layout;
sl@70
  2307
StephaneLenclud@141
  2308
            //First clean our current panel
StephaneLenclud@175
  2309
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2310
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2311
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2312
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2313
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2314
StephaneLenclud@175
  2315
            //Then recreate our rows...
StephaneLenclud@175
  2316
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2317
            {
StephaneLenclud@175
  2318
                iTableLayoutPanel.RowCount++;
sl@63
  2319
            }
sl@63
  2320
StephaneLenclud@175
  2321
            // ...and columns 
StephaneLenclud@175
  2322
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2323
            {
StephaneLenclud@175
  2324
                iTableLayoutPanel.ColumnCount++;
sl@63
  2325
            }
sl@63
  2326
StephaneLenclud@175
  2327
            //For each column
StephaneLenclud@175
  2328
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2329
            {
sl@63
  2330
                //Create our column styles
StephaneLenclud@175
  2331
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2332
StephaneLenclud@175
  2333
                //For each rows
StephaneLenclud@175
  2334
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2335
                {
sl@63
  2336
                    if (i == 0)
sl@63
  2337
                    {
sl@63
  2338
                        //Create our row styles
StephaneLenclud@175
  2339
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2340
                    }
StephaneLenclud@176
  2341
                    else
sl@70
  2342
                    {
sl@70
  2343
                        continue;
sl@70
  2344
                    }
sl@63
  2345
                }
sl@63
  2346
            }
sl@63
  2347
StephaneLenclud@176
  2348
            //For each field
StephaneLenclud@176
  2349
            foreach (DataField field in aClient.Fields)
sl@70
  2350
            {
StephaneLenclud@176
  2351
                if (!field.IsTableField)
StephaneLenclud@176
  2352
                {
StephaneLenclud@176
  2353
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2354
                    continue;
StephaneLenclud@176
  2355
                }
StephaneLenclud@176
  2356
StephaneLenclud@223
  2357
                TableField tableField = (TableField) field;
StephaneLenclud@176
  2358
StephaneLenclud@176
  2359
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2360
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2361
StephaneLenclud@176
  2362
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2363
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2364
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2365
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2366
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2367
            }
sl@70
  2368
StephaneLenclud@176
  2369
sl@63
  2370
            CheckFontHeight();
sl@63
  2371
        }
sl@63
  2372
sl@68
  2373
        /// <summary>
sl@70
  2374
        /// Check our type of data field and create corresponding control
sl@68
  2375
        /// </summary>
sl@68
  2376
        /// <param name="aField"></param>
sl@69
  2377
        private Control CreateControlForDataField(DataField aField)
sl@68
  2378
        {
StephaneLenclud@223
  2379
            Control control = null;
StephaneLenclud@172
  2380
            if (aField.IsTextField)
sl@68
  2381
            {
sl@68
  2382
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2383
                label.AutoEllipsis = true;
sl@68
  2384
                label.AutoSize = true;
sl@68
  2385
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2386
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2387
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2388
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2389
                label.Name = "marqueeLabel" + aField;
sl@68
  2390
                label.OwnTimer = false;
StephaneLenclud@106
  2391
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2392
                label.Separator = cds.Separator;
sl@100
  2393
                label.MinFontSize = cds.MinFontSize;
sl@100
  2394
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2395
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2396
                //control.TabIndex = 2;
sl@68
  2397
                label.Font = cds.Font;
sl@68
  2398
StephaneLenclud@274
  2399
                TextField field = (TextField)aField;
StephaneLenclud@172
  2400
                label.TextAlign = field.Alignment;
sl@68
  2401
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2402
                label.Text = field.Text;
sl@68
  2403
                //
sl@68
  2404
                control = label;
sl@68
  2405
            }
StephaneLenclud@172
  2406
            else if (aField.IsBitmapField)
sl@68
  2407
            {
sl@68
  2408
                //Create picture box
sl@68
  2409
                PictureBox picture = new PictureBox();
sl@68
  2410
                picture.AutoSize = true;
sl@68
  2411
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2412
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2413
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2414
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2415
                picture.Name = "pictureBox" + aField;
sl@68
  2416
                //Set our image
StephaneLenclud@274
  2417
                BitmapField field = (BitmapField)aField;
StephaneLenclud@172
  2418
                picture.Image = field.Bitmap;
sl@68
  2419
                //
sl@68
  2420
                control = picture;
sl@68
  2421
            }
StephaneLenclud@274
  2422
            else if (aField is AudioVisualizerField)
StephaneLenclud@274
  2423
            {
StephaneLenclud@274
  2424
                //Create picture box
StephaneLenclud@274
  2425
                PictureBox picture = new PictureBox();
StephaneLenclud@274
  2426
                picture.AutoSize = true;
StephaneLenclud@274
  2427
                picture.BackColor = System.Drawing.Color.Transparent;
StephaneLenclud@274
  2428
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
StephaneLenclud@274
  2429
                picture.Location = new System.Drawing.Point(1, 1);
StephaneLenclud@274
  2430
                picture.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@274
  2431
                picture.Name = "pictureBox" + aField;
StephaneLenclud@274
  2432
                control = picture;
StephaneLenclud@274
  2433
            }
StephaneLenclud@274
  2434
StephaneLenclud@172
  2435
            //TODO: Handle recording field?
sl@68
  2436
sl@69
  2437
            return control;
sl@68
  2438
        }
sl@68
  2439
StephaneLenclud@223
  2440
        /// <summary>
StephaneLenclud@223
  2441
        /// Called when the user selected a new display type.
StephaneLenclud@223
  2442
        /// </summary>
StephaneLenclud@223
  2443
        /// <param name="sender"></param>
StephaneLenclud@223
  2444
        /// <param name="e"></param>
sl@46
  2445
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2446
        {
StephaneLenclud@223
  2447
            //Store the selected display type in our settings
sl@48
  2448
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2449
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2450
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2451
StephaneLenclud@223
  2452
            //Try re-opening the display connection if we were already connected.
StephaneLenclud@223
  2453
            //Otherwise just update our status to reflect display type change.
sl@51
  2454
            if (iDisplay.IsOpen())
sl@51
  2455
            {
sl@51
  2456
                OpenDisplayConnection();
sl@51
  2457
            }
sl@51
  2458
            else
sl@51
  2459
            {
sl@51
  2460
                UpdateStatus();
sl@51
  2461
            }
sl@46
  2462
        }
sl@46
  2463
sl@47
  2464
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2465
        {
sl@47
  2466
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2467
            {
sl@51
  2468
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2469
sl@51
  2470
                if (interval > 0)
sl@51
  2471
                {
StephaneLenclud@270
  2472
                    iTimerDisplay.Interval = interval;
StephaneLenclud@270
  2473
                    cds.TimerInterval = iTimerDisplay.Interval;
sl@51
  2474
                    Properties.Settings.Default.Save();
sl@51
  2475
                }
sl@47
  2476
            }
sl@47
  2477
        }
sl@47
  2478
sl@100
  2479
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2480
        {
sl@100
  2481
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2482
            {
sl@100
  2483
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2484
sl@100
  2485
                if (minFontSize > 0)
sl@100
  2486
                {
sl@100
  2487
                    cds.MinFontSize = minFontSize;
sl@100
  2488
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2489
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2490
                    UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2491
                }
sl@100
  2492
            }
sl@100
  2493
        }
sl@100
  2494
StephaneLenclud@106
  2495
StephaneLenclud@223
  2496
        private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@223
  2497
        {
StephaneLenclud@223
  2498
            if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@223
  2499
            {
StephaneLenclud@223
  2500
                int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@223
  2501
StephaneLenclud@223
  2502
                if (scrollingSpeed > 0)
StephaneLenclud@223
  2503
                {
StephaneLenclud@223
  2504
                    cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@223
  2505
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2506
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2507
                    UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@223
  2508
                }
StephaneLenclud@223
  2509
            }
StephaneLenclud@223
  2510
        }
StephaneLenclud@106
  2511
sl@100
  2512
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2513
        {
sl@100
  2514
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2515
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2516
StephaneLenclud@223
  2517
            //Update our text fields
StephaneLenclud@223
  2518
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@223
  2519
            {
StephaneLenclud@223
  2520
                ctrl.Separator = cds.Separator;
StephaneLenclud@223
  2521
            }
StephaneLenclud@110
  2522
sl@100
  2523
        }
sl@100
  2524
sl@52
  2525
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2526
        {
sl@52
  2527
            iDisplay.PowerOn();
sl@52
  2528
        }
sl@52
  2529
sl@52
  2530
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2531
        {
sl@52
  2532
            iDisplay.PowerOff();
sl@52
  2533
        }
sl@52
  2534
sl@53
  2535
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2536
        {
StephaneLenclud@223
  2537
            ShowClock();
sl@53
  2538
        }
sl@53
  2539
sl@53
  2540
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2541
        {
StephaneLenclud@223
  2542
            HideClock();
sl@53
  2543
        }
sl@88
  2544
sl@88
  2545
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2546
        {
sl@88
  2547
            InstallUpdateSyncWithInfo();
sl@88
  2548
        }