Server/FormMain.cs
author StephaneLenclud
Mon, 02 Jan 2017 18:43:45 +0100
changeset 273 e5f85a895a62
parent 272 10de0c7c2fed
child 274 920fea7a6427
permissions -rw-r--r--
Draft audio spectrum 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@273
   677
                UseAverage = true,
StephaneLenclud@273
   678
                BarCount = 32,
StephaneLenclud@273
   679
                BarSpacing = 0,
StephaneLenclud@273
   680
                IsXLogScale = true,
StephaneLenclud@273
   681
                ScalingStrategy = ScalingStrategy.Sqrt
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@273
   730
        private void GenerateAudioVisualization()
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@273
   733
            foreach (Control ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@273
   734
            {
StephaneLenclud@273
   735
                if (ctrl is PictureBox)
StephaneLenclud@273
   736
                {
StephaneLenclud@273
   737
                    PictureBox pb = (PictureBox)ctrl;
StephaneLenclud@273
   738
                    Image image = pb.Image;
StephaneLenclud@273
   739
                    var newImage = iLineSpectrum.CreateSpectrumLine(pb.Size, Color.Black, Color.Black, Color.White, false);
StephaneLenclud@273
   740
                    if (newImage != null)
StephaneLenclud@273
   741
                    {
StephaneLenclud@273
   742
                        pb.Image = newImage;
StephaneLenclud@273
   743
                        if (image != null)
StephaneLenclud@273
   744
                            image.Dispose();
StephaneLenclud@273
   745
                    }
StephaneLenclud@273
   746
StephaneLenclud@273
   747
                    break;
StephaneLenclud@273
   748
                }
StephaneLenclud@273
   749
            }
StephaneLenclud@273
   750
        }
StephaneLenclud@273
   751
StephaneLenclud@273
   752
StephaneLenclud@273
   753
        /// <summary>
StephaneLenclud@273
   754
        /// 
StephaneLenclud@273
   755
        /// </summary>
StephaneLenclud@112
   756
        private void UpdateAudioDeviceAndMasterVolumeThreadSafe()
StephaneLenclud@112
   757
        {
StephaneLenclud@112
   758
            if (this.InvokeRequired)
StephaneLenclud@112
   759
            {
StephaneLenclud@112
   760
                //Not in the proper thread, invoke ourselves
StephaneLenclud@223
   761
                PlainUpdateDelegate d = new PlainUpdateDelegate(UpdateAudioDeviceAndMasterVolumeThreadSafe);
StephaneLenclud@223
   762
                this.Invoke(d, new object[] {});
StephaneLenclud@112
   763
                return;
StephaneLenclud@112
   764
            }
StephaneLenclud@223
   765
StephaneLenclud@112
   766
            //We are in the correct thread just go ahead.
StephaneLenclud@112
   767
            try
StephaneLenclud@223
   768
            {
StephaneLenclud@272
   769
                //Get our master volume
StephaneLenclud@223
   770
                iMultiMediaDevice = iMultiMediaDeviceEnumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Multimedia);
StephaneLenclud@272
   771
                iAudioEndpointVolume = AudioEndpointVolume.FromDevice(iMultiMediaDevice);
StephaneLenclud@273
   772
                
StephaneLenclud@223
   773
                //Update our label
StephaneLenclud@223
   774
                labelDefaultAudioDevice.Text = iMultiMediaDevice.FriendlyName;
StephaneLenclud@116
   775
StephaneLenclud@112
   776
                //Show our volume in our track bar
StephaneLenclud@223
   777
                UpdateMasterVolumeThreadSafe();
StephaneLenclud@112
   778
StephaneLenclud@112
   779
                //Register to get volume modifications
StephaneLenclud@272
   780
                AudioEndpointVolumeCallback callback = new AudioEndpointVolumeCallback();
StephaneLenclud@272
   781
                callback.NotifyRecived += OnVolumeNotificationThreadSafe;
StephaneLenclud@272
   782
                // Do we need to unregister?
StephaneLenclud@273
   783
                iAudioEndpointVolume.RegisterControlChangeNotify(callback);
StephaneLenclud@273
   784
                //
StephaneLenclud@273
   785
                StartAudioVisualization();
StephaneLenclud@112
   786
                //
StephaneLenclud@223
   787
                trackBarMasterVolume.Enabled = true;
StephaneLenclud@112
   788
            }
StephaneLenclud@112
   789
            catch (Exception ex)
StephaneLenclud@112
   790
            {
StephaneLenclud@112
   791
                Debug.WriteLine("Exception thrown in UpdateAudioDeviceAndMasterVolume");
StephaneLenclud@112
   792
                Debug.WriteLine(ex.ToString());
StephaneLenclud@112
   793
                //Something went wrong S/PDIF device ca throw exception I guess
StephaneLenclud@223
   794
                trackBarMasterVolume.Enabled = false;
StephaneLenclud@112
   795
            }
StephaneLenclud@112
   796
        }
StephaneLenclud@104
   797
StephaneLenclud@223
   798
        /// <summary>
StephaneLenclud@223
   799
        /// 
StephaneLenclud@223
   800
        /// </summary>
StephaneLenclud@223
   801
        private void PopulateDeviceTypes()
StephaneLenclud@223
   802
        {
StephaneLenclud@223
   803
            int count = Display.TypeCount();
StephaneLenclud@223
   804
StephaneLenclud@223
   805
            for (int i = 0; i < count; i++)
StephaneLenclud@223
   806
            {
StephaneLenclud@223
   807
                comboBoxDisplayType.Items.Add(Display.TypeName((MiniDisplay.Type) i));
StephaneLenclud@223
   808
            }
StephaneLenclud@223
   809
        }
StephaneLenclud@104
   810
StephaneLenclud@152
   811
        /// <summary>
StephaneLenclud@223
   812
        ///
StephaneLenclud@223
   813
        /// </summary>
StephaneLenclud@223
   814
        private void SetupTrayIcon()
StephaneLenclud@223
   815
        {
StephaneLenclud@223
   816
            iNotifyIcon.Icon = GetIcon("vfd.ico");
StephaneLenclud@223
   817
            iNotifyIcon.Text = "Sharp Display Manager";
StephaneLenclud@223
   818
            iNotifyIcon.Visible = true;
StephaneLenclud@223
   819
StephaneLenclud@223
   820
            //Double click toggles visibility - typically brings up the application
StephaneLenclud@223
   821
            iNotifyIcon.DoubleClick += delegate(object obj, EventArgs args)
StephaneLenclud@223
   822
            {
StephaneLenclud@223
   823
                SysTrayHideShow();
StephaneLenclud@223
   824
            };
StephaneLenclud@223
   825
StephaneLenclud@223
   826
            //Adding a context menu, useful to be able to exit the application
StephaneLenclud@223
   827
            ContextMenu contextMenu = new ContextMenu();
StephaneLenclud@223
   828
            //Context menu item to toggle visibility
StephaneLenclud@223
   829
            MenuItem hideShowItem = new MenuItem("Hide/Show");
StephaneLenclud@223
   830
            hideShowItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   831
            {
StephaneLenclud@223
   832
                SysTrayHideShow();
StephaneLenclud@223
   833
            };
StephaneLenclud@223
   834
            contextMenu.MenuItems.Add(hideShowItem);
StephaneLenclud@223
   835
StephaneLenclud@223
   836
            //Context menu item separator
StephaneLenclud@223
   837
            contextMenu.MenuItems.Add(new MenuItem("-"));
StephaneLenclud@223
   838
StephaneLenclud@223
   839
            //Context menu exit item
StephaneLenclud@223
   840
            MenuItem exitItem = new MenuItem("Exit");
StephaneLenclud@223
   841
            exitItem.Click += delegate(object obj, EventArgs args)
StephaneLenclud@223
   842
            {
StephaneLenclud@223
   843
                Application.Exit();
StephaneLenclud@223
   844
            };
StephaneLenclud@223
   845
            contextMenu.MenuItems.Add(exitItem);
StephaneLenclud@223
   846
StephaneLenclud@223
   847
            iNotifyIcon.ContextMenu = contextMenu;
StephaneLenclud@223
   848
        }
sl@95
   849
StephaneLenclud@178
   850
        /// <summary>
StephaneLenclud@179
   851
        ///
StephaneLenclud@179
   852
        /// </summary>
StephaneLenclud@179
   853
        private void SetupRecordingNotification()
StephaneLenclud@179
   854
        {
StephaneLenclud@179
   855
            iRecordingNotification.Icon = GetIcon("record.ico");
StephaneLenclud@179
   856
            iRecordingNotification.Text = "No recording";
StephaneLenclud@180
   857
            iRecordingNotification.Visible = false;
StephaneLenclud@179
   858
        }
StephaneLenclud@179
   859
StephaneLenclud@179
   860
        /// <summary>
StephaneLenclud@178
   861
        /// Access icons from embedded resources.
StephaneLenclud@178
   862
        /// </summary>
StephaneLenclud@178
   863
        /// <param name="aName"></param>
StephaneLenclud@178
   864
        /// <returns></returns>
StephaneLenclud@178
   865
        public static Icon GetIcon(string aName)
StephaneLenclud@223
   866
        {
StephaneLenclud@223
   867
            string[] names = Assembly.GetExecutingAssembly().GetManifestResourceNames();
StephaneLenclud@223
   868
            foreach (string name in names)
StephaneLenclud@223
   869
            {
StephaneLenclud@178
   870
                //Find a resource name that ends with the given name
StephaneLenclud@223
   871
                if (name.EndsWith(aName))
StephaneLenclud@223
   872
                {
StephaneLenclud@223
   873
                    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name))
StephaneLenclud@223
   874
                    {
StephaneLenclud@223
   875
                        return new Icon(stream);
StephaneLenclud@223
   876
                    }
StephaneLenclud@223
   877
                }
StephaneLenclud@223
   878
            }
StephaneLenclud@223
   879
StephaneLenclud@223
   880
            return null;
StephaneLenclud@223
   881
        }
sl@94
   882
sl@94
   883
sl@65
   884
        /// <summary>
sl@65
   885
        /// Set our current client.
sl@65
   886
        /// This will take care of applying our client layout and set data fields.
sl@65
   887
        /// </summary>
sl@65
   888
        /// <param name="aSessionId"></param>
StephaneLenclud@223
   889
        void SetCurrentClient(string aSessionId, bool aForce = false)
sl@57
   890
        {
sl@65
   891
            if (aSessionId == iCurrentClientSessionId)
sl@57
   892
            {
sl@65
   893
                //Given client is already the current one.
sl@65
   894
                //Don't bother changing anything then.
sl@65
   895
                return;
sl@65
   896
            }
sl@57
   897
StephaneLenclud@185
   898
            ClientData requestedClientData = iClients[aSessionId];
StephaneLenclud@141
   899
StephaneLenclud@141
   900
            //Check when was the last time we switched to that client
StephaneLenclud@142
   901
            if (iCurrentClientData != null)
StephaneLenclud@141
   902
            {
StephaneLenclud@185
   903
                //Do not switch client if priority of current client is higher 
StephaneLenclud@185
   904
                if (!aForce && requestedClientData.Priority < iCurrentClientData.Priority)
StephaneLenclud@185
   905
                {
StephaneLenclud@185
   906
                    return;
StephaneLenclud@185
   907
                }
StephaneLenclud@185
   908
StephaneLenclud@185
   909
StephaneLenclud@142
   910
                double lastSwitchToClientSecondsAgo = (DateTime.Now - iCurrentClientData.LastSwitchTime).TotalSeconds;
StephaneLenclud@142
   911
                //TODO: put that hard coded value as a client property
StephaneLenclud@142
   912
                //Clients should be able to define how often they can be interrupted
StephaneLenclud@142
   913
                //Thus a background client can set this to zero allowing any other client to interrupt at any time
StephaneLenclud@142
   914
                //We could also compute this delay by looking at the requests frequencies?
StephaneLenclud@185
   915
                if (!aForce &&
StephaneLenclud@223
   916
                    requestedClientData.Priority == iCurrentClientData.Priority &&
StephaneLenclud@223
   917
                    //Time sharing is only if clients have the same priority
StephaneLenclud@185
   918
                    (lastSwitchToClientSecondsAgo < 30)) //Make sure a client is on for at least 30 seconds
StephaneLenclud@142
   919
                {
StephaneLenclud@142
   920
                    //Don't switch clients too often
StephaneLenclud@142
   921
                    return;
StephaneLenclud@142
   922
                }
StephaneLenclud@141
   923
            }
StephaneLenclud@141
   924
sl@65
   925
            //Set current client ID.
sl@65
   926
            iCurrentClientSessionId = aSessionId;
StephaneLenclud@141
   927
            //Set the time we last switched to that client
StephaneLenclud@141
   928
            iClients[aSessionId].LastSwitchTime = DateTime.Now;
sl@65
   929
            //Fetch and set current client data.
StephaneLenclud@185
   930
            iCurrentClientData = requestedClientData;
sl@65
   931
            //Apply layout and set data fields.
sl@65
   932
            UpdateTableLayoutPanel(iCurrentClientData);
sl@57
   933
        }
sl@57
   934
sl@0
   935
        private void buttonFont_Click(object sender, EventArgs e)
sl@0
   936
        {
sl@0
   937
            //fontDialog.ShowColor = true;
sl@0
   938
            //fontDialog.ShowApply = true;
sl@0
   939
            fontDialog.ShowEffects = true;
sl@99
   940
            fontDialog.Font = cds.Font;
sl@28
   941
sl@28
   942
            fontDialog.FixedPitchOnly = checkBoxFixedPitchFontOnly.Checked;
sl@28
   943
sl@0
   944
            //fontDialog.ShowHelp = true;
sl@0
   945
sl@0
   946
            //fontDlg.MaxSize = 40;
sl@0
   947
            //fontDlg.MinSize = 22;
sl@0
   948
sl@0
   949
            //fontDialog.Parent = this;
sl@0
   950
            //fontDialog.StartPosition = FormStartPosition.CenterParent;
sl@0
   951
sl@0
   952
            //DlgBox.ShowDialog(fontDialog);
sl@0
   953
sl@0
   954
            //if (fontDialog.ShowDialog(this) != DialogResult.Cancel)
sl@0
   955
            if (DlgBox.ShowDialog(fontDialog) != DialogResult.Cancel)
sl@0
   956
            {
sl@99
   957
                //Set the fonts to all our labels in our layout
StephaneLenclud@175
   958
                foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@99
   959
                {
sl@99
   960
                    if (ctrl is MarqueeLabel)
sl@99
   961
                    {
StephaneLenclud@223
   962
                        ((MarqueeLabel) ctrl).Font = fontDialog.Font;
sl@99
   963
                    }
sl@99
   964
                }
sl@0
   965
sl@99
   966
                //Save font settings
sl@48
   967
                cds.Font = fontDialog.Font;
sl@8
   968
                Properties.Settings.Default.Save();
sl@36
   969
                //
sl@37
   970
                CheckFontHeight();
sl@37
   971
            }
sl@37
   972
        }
sl@36
   973
sl@37
   974
        /// <summary>
sl@38
   975
        ///
sl@37
   976
        /// </summary>
sl@37
   977
        void CheckFontHeight()
sl@37
   978
        {
sl@54
   979
            //Show font height and width
sl@54
   980
            labelFontHeight.Text = "Font height: " + cds.Font.Height;
sl@54
   981
            float charWidth = IsFixedWidth(cds.Font);
sl@54
   982
            if (charWidth == 0.0f)
sl@54
   983
            {
sl@54
   984
                labelFontWidth.Visible = false;
sl@54
   985
            }
sl@54
   986
            else
sl@54
   987
            {
sl@54
   988
                labelFontWidth.Visible = true;
sl@54
   989
                labelFontWidth.Text = "Font width: " + charWidth;
sl@54
   990
            }
sl@54
   991
sl@70
   992
            MarqueeLabel label = null;
sl@68
   993
            //Get the first label control we can find
StephaneLenclud@175
   994
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@68
   995
            {
sl@68
   996
                if (ctrl is MarqueeLabel)
sl@68
   997
                {
StephaneLenclud@223
   998
                    label = (MarqueeLabel) ctrl;
sl@68
   999
                    break;
sl@68
  1000
                }
sl@68
  1001
            }
sl@68
  1002
sl@54
  1003
            //Now check font height and show a warning if needed.
sl@68
  1004
            if (label != null && label.Font.Height > label.Height)
sl@37
  1005
            {
StephaneLenclud@223
  1006
                labelWarning.Text = "WARNING: Selected font is too height by " + (label.Font.Height - label.Height) +
StephaneLenclud@223
  1007
                                    " pixels!";
sl@37
  1008
                labelWarning.Visible = true;
sl@0
  1009
            }
sl@37
  1010
            else
sl@37
  1011
            {
sl@37
  1012
                labelWarning.Visible = false;
sl@37
  1013
            }
sl@37
  1014
sl@0
  1015
        }
sl@0
  1016
sl@0
  1017
        private void buttonCapture_Click(object sender, EventArgs e)
sl@0
  1018
        {
StephaneLenclud@175
  1019
            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height);
StephaneLenclud@175
  1020
            iTableLayoutPanel.DrawToBitmap(bmp, iTableLayoutPanel.ClientRectangle);
sl@14
  1021
            //Bitmap bmpToSave = new Bitmap(bmp);
sl@14
  1022
            bmp.Save("D:\\capture.png");
sl@14
  1023
StephaneLenclud@223
  1024
            ((MarqueeLabel) iTableLayoutPanel.Controls[0]).Text = "Captured";
sl@17
  1025
sl@14
  1026
            /*
sl@14
  1027
            string outputFileName = "d:\\capture.png";
sl@14
  1028
            using (MemoryStream memory = new MemoryStream())
sl@14
  1029
            {
sl@14
  1030
                using (FileStream fs = new FileStream(outputFileName, FileMode.OpenOrCreate, FileAccess.ReadWrite))
sl@14
  1031
                {
sl@14
  1032
                    bmp.Save(memory, System.Drawing.Imaging.ImageFormat.Png);
sl@14
  1033
                    byte[] bytes = memory.ToArray();
sl@14
  1034
                    fs.Write(bytes, 0, bytes.Length);
sl@14
  1035
                }
sl@14
  1036
            }
sl@14
  1037
             */
sl@14
  1038
sl@0
  1039
        }
sl@2
  1040
sl@12
  1041
        private void CheckForRequestResults()
sl@12
  1042
        {
sl@12
  1043
            if (iDisplay.IsRequestPending())
sl@12
  1044
            {
sl@12
  1045
                switch (iDisplay.AttemptRequestCompletion())
sl@12
  1046
                {
StephaneLenclud@135
  1047
                    case MiniDisplay.Request.FirmwareRevision:
sl@51
  1048
                        toolStripStatusLabelConnect.Text += " v" + iDisplay.FirmwareRevision();
sl@51
  1049
                        //Issue next request then
sl@51
  1050
                        iDisplay.RequestPowerSupplyStatus();
sl@51
  1051
                        break;
sl@51
  1052
StephaneLenclud@135
  1053
                    case MiniDisplay.Request.PowerSupplyStatus:
sl@12
  1054
                        if (iDisplay.PowerSupplyStatus())
sl@12
  1055
                        {
sl@12
  1056
                            toolStripStatusLabelPower.Text = "ON";
sl@12
  1057
                        }
sl@12
  1058
                        else
sl@12
  1059
                        {
sl@12
  1060
                            toolStripStatusLabelPower.Text = "OFF";
sl@12
  1061
                        }
sl@12
  1062
                        //Issue next request then
sl@12
  1063
                        iDisplay.RequestDeviceId();
sl@12
  1064
                        break;
sl@12
  1065
StephaneLenclud@135
  1066
                    case MiniDisplay.Request.DeviceId:
sl@12
  1067
                        toolStripStatusLabelConnect.Text += " - " + iDisplay.DeviceId();
sl@12
  1068
                        //No more request to issue
sl@12
  1069
                        break;
sl@12
  1070
                }
sl@12
  1071
            }
sl@12
  1072
        }
sl@12
  1073
sl@58
  1074
        public static uint ColorWhiteIsOn(int aX, int aY, uint aPixel)
sl@58
  1075
        {
sl@58
  1076
            if ((aPixel & 0x00FFFFFF) == 0x00FFFFFF)
sl@58
  1077
            {
sl@58
  1078
                return 0xFFFFFFFF;
sl@58
  1079
            }
sl@58
  1080
            return 0x00000000;
sl@58
  1081
        }
sl@16
  1082
sl@58
  1083
        public static uint ColorUntouched(int aX, int aY, uint aPixel)
sl@57
  1084
        {
sl@57
  1085
            return aPixel;
sl@57
  1086
        }
sl@57
  1087
sl@58
  1088
        public static uint ColorInversed(int aX, int aY, uint aPixel)
sl@57
  1089
        {
sl@57
  1090
            return ~aPixel;
sl@57
  1091
        }
sl@57
  1092
sl@58
  1093
        public static uint ColorChessboard(int aX, int aY, uint aPixel)
sl@58
  1094
        {
StephaneLenclud@223
  1095
            if ((aX%2 == 0) && (aY%2 == 0))
sl@58
  1096
            {
sl@58
  1097
                return ~aPixel;
sl@58
  1098
            }
StephaneLenclud@223
  1099
            else if ((aX%2 != 0) && (aY%2 != 0))
sl@58
  1100
            {
sl@58
  1101
                return ~aPixel;
sl@58
  1102
            }
sl@58
  1103
            return 0x00000000;
sl@58
  1104
        }
sl@58
  1105
sl@16
  1106
sl@16
  1107
        public static int ScreenReversedX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1108
        {
sl@16
  1109
            return aBmp.Width - aX - 1;
sl@16
  1110
        }
sl@16
  1111
sl@16
  1112
        public int ScreenReversedY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1113
        {
sl@16
  1114
            return iBmp.Height - aY - 1;
sl@16
  1115
        }
sl@16
  1116
sl@16
  1117
        public int ScreenX(System.Drawing.Bitmap aBmp, int aX)
sl@16
  1118
        {
sl@16
  1119
            return aX;
sl@16
  1120
        }
sl@16
  1121
sl@16
  1122
        public int ScreenY(System.Drawing.Bitmap aBmp, int aY)
sl@16
  1123
        {
sl@16
  1124
            return aY;
sl@16
  1125
        }
sl@16
  1126
sl@58
  1127
        /// <summary>
sl@58
  1128
        /// Select proper pixel delegates according to our current settings.
sl@58
  1129
        /// </summary>
sl@58
  1130
        private void SetupPixelDelegates()
sl@58
  1131
        {
sl@59
  1132
            //Select our pixel processing routine
sl@58
  1133
            if (cds.InverseColors)
sl@58
  1134
            {
sl@58
  1135
                //iColorFx = ColorChessboard;
sl@58
  1136
                iColorFx = ColorInversed;
sl@58
  1137
            }
sl@58
  1138
            else
sl@58
  1139
            {
sl@58
  1140
                iColorFx = ColorWhiteIsOn;
sl@58
  1141
            }
sl@58
  1142
sl@58
  1143
            //Select proper coordinate translation functions
sl@58
  1144
            //We used delegate/function pointer to support reverse screen without doing an extra test on each pixels
sl@58
  1145
            if (cds.ReverseScreen)
sl@58
  1146
            {
sl@58
  1147
                iScreenX = ScreenReversedX;
sl@58
  1148
                iScreenY = ScreenReversedY;
sl@58
  1149
            }
sl@58
  1150
            else
sl@58
  1151
            {
sl@58
  1152
                iScreenX = ScreenX;
sl@58
  1153
                iScreenY = ScreenY;
sl@58
  1154
            }
sl@58
  1155
sl@58
  1156
        }
sl@16
  1157
StephaneLenclud@270
  1158
        /// <summary>
StephaneLenclud@270
  1159
        /// This is our timer tick responsible to perform our render
StephaneLenclud@270
  1160
        /// TODO: Use a threading timer instead of a Windows form timer.
StephaneLenclud@270
  1161
        /// </summary>
StephaneLenclud@270
  1162
        /// <param name="sender"></param>
StephaneLenclud@270
  1163
        /// <param name="e"></param>
sl@2
  1164
        private void timer_Tick(object sender, EventArgs e)
sl@14
  1165
        {
sl@2
  1166
            //Update our animations
sl@2
  1167
            DateTime NewTickTime = DateTime.Now;
sl@2
  1168
StephaneLenclud@223
  1169
            UpdateNetworkSignal(LastTickTime, NewTickTime);
StephaneLenclud@118
  1170
sl@60
  1171
            //Update animation for all our marquees
StephaneLenclud@175
  1172
            foreach (Control ctrl in iTableLayoutPanel.Controls)
sl@60
  1173
            {
sl@68
  1174
                if (ctrl is MarqueeLabel)
sl@68
  1175
                {
StephaneLenclud@223
  1176
                    ((MarqueeLabel) ctrl).UpdateAnimation(LastTickTime, NewTickTime);
sl@68
  1177
                }
sl@60
  1178
            }
sl@60
  1179
sl@4
  1180
            //Update our display
sl@4
  1181
            if (iDisplay.IsOpen())
sl@4
  1182
            {
sl@12
  1183
                CheckForRequestResults();
sl@12
  1184
StephaneLenclud@223
  1185
                //Check if frame rendering is needed
StephaneLenclud@223
  1186
                //Typically used when showing clock
StephaneLenclud@223
  1187
                if (!iSkipFrameRendering)
StephaneLenclud@223
  1188
                {
StephaneLenclud@223
  1189
                    //Draw to bitmap
StephaneLenclud@223
  1190
                    if (iCreateBitmap)
StephaneLenclud@223
  1191
                    {
StephaneLenclud@223
  1192
                        iBmp = new System.Drawing.Bitmap(iTableLayoutPanel.Width, iTableLayoutPanel.Height,
StephaneLenclud@223
  1193
                            PixelFormat.Format32bppArgb);
StephaneLenclud@158
  1194
                        iCreateBitmap = false;
StephaneLenclud@158
  1195
                    }
StephaneLenclud@223
  1196
                    iTableLayoutPanel.DrawToBitmap(iBmp, iTableLayoutPanel.ClientRectangle);
StephaneLenclud@223
  1197
                    //iBmp.Save("D:\\capture.png");
StephaneLenclud@223
  1198
StephaneLenclud@223
  1199
                    //Send it to our display
StephaneLenclud@223
  1200
                    for (int i = 0; i < iBmp.Width; i++)
StephaneLenclud@223
  1201
                    {
StephaneLenclud@223
  1202
                        for (int j = 0; j < iBmp.Height; j++)
StephaneLenclud@223
  1203
                        {
StephaneLenclud@223
  1204
                            unchecked
StephaneLenclud@223
  1205
                            {
StephaneLenclud@223
  1206
                                //Get our processed pixel coordinates
StephaneLenclud@223
  1207
                                int x = iScreenX(iBmp, i);
StephaneLenclud@223
  1208
                                int y = iScreenY(iBmp, j);
StephaneLenclud@223
  1209
                                //Get pixel color
StephaneLenclud@223
  1210
                                uint color = (uint) iBmp.GetPixel(i, j).ToArgb();
StephaneLenclud@223
  1211
                                //Apply color effects
StephaneLenclud@223
  1212
                                color = iColorFx(x, y, color);
StephaneLenclud@223
  1213
                                //Now set our pixel
StephaneLenclud@223
  1214
                                iDisplay.SetPixel(x, y, color);
StephaneLenclud@223
  1215
                            }
StephaneLenclud@223
  1216
                        }
StephaneLenclud@223
  1217
                    }
StephaneLenclud@223
  1218
StephaneLenclud@223
  1219
                    iDisplay.SwapBuffers();
StephaneLenclud@223
  1220
                }
sl@4
  1221
            }
sl@8
  1222
StephaneLenclud@273
  1223
            GenerateAudioVisualization();
StephaneLenclud@273
  1224
sl@8
  1225
            //Compute instant FPS
StephaneLenclud@273
  1226
      toolStripStatusLabelFps.Text = (1.0/NewTickTime.Subtract(LastTickTime).TotalSeconds).ToString("F0") + " / " +
StephaneLenclud@270
  1227
                                           (1000/iTimerDisplay.Interval).ToString() + " FPS";
sl@8
  1228
sl@8
  1229
            LastTickTime = NewTickTime;
sl@8
  1230
sl@2
  1231
        }
sl@3
  1232
StephaneLenclud@223
  1233
        /// <summary>
StephaneLenclud@223
  1234
        /// Attempt to establish connection with our display hardware.
StephaneLenclud@223
  1235
        /// </summary>
sl@46
  1236
        private void OpenDisplayConnection()
sl@3
  1237
        {
sl@46
  1238
            CloseDisplayConnection();
sl@46
  1239
StephaneLenclud@223
  1240
            if (!iDisplay.Open((MiniDisplay.Type) cds.DisplayType))
StephaneLenclud@223
  1241
            {
StephaneLenclud@223
  1242
                UpdateStatus();
StephaneLenclud@223
  1243
                toolStripStatusLabelConnect.Text = "Connection error";
sl@7
  1244
            }
sl@46
  1245
        }
sl@7
  1246
sl@46
  1247
        private void CloseDisplayConnection()
sl@46
  1248
        {
StephaneLenclud@223
  1249
            //Status will be updated upon receiving the closed event
StephaneLenclud@223
  1250
StephaneLenclud@223
  1251
            if (iDisplay == null || !iDisplay.IsOpen())
StephaneLenclud@223
  1252
            {
StephaneLenclud@223
  1253
                return;
StephaneLenclud@223
  1254
            }
StephaneLenclud@223
  1255
StephaneLenclud@223
  1256
            //Do not clear if we gave up on rendering already.
StephaneLenclud@223
  1257
            //This means we will keep on displaying clock on MDM166AA for instance.
StephaneLenclud@223
  1258
            if (!iSkipFrameRendering)
StephaneLenclud@223
  1259
            {
StephaneLenclud@223
  1260
                iDisplay.Clear();
StephaneLenclud@223
  1261
                iDisplay.SwapBuffers();
StephaneLenclud@223
  1262
            }
StephaneLenclud@223
  1263
StephaneLenclud@223
  1264
            iDisplay.SetAllIconsStatus(0); //Turn off all icons
sl@46
  1265
            iDisplay.Close();
sl@46
  1266
        }
sl@46
  1267
sl@46
  1268
        private void buttonOpen_Click(object sender, EventArgs e)
sl@46
  1269
        {
sl@46
  1270
            OpenDisplayConnection();
sl@3
  1271
        }
sl@3
  1272
sl@3
  1273
        private void buttonClose_Click(object sender, EventArgs e)
sl@3
  1274
        {
sl@46
  1275
            CloseDisplayConnection();
sl@3
  1276
        }
sl@3
  1277
sl@3
  1278
        private void buttonClear_Click(object sender, EventArgs e)
sl@3
  1279
        {
sl@3
  1280
            iDisplay.Clear();
sl@3
  1281
            iDisplay.SwapBuffers();
sl@3
  1282
        }
sl@3
  1283
sl@3
  1284
        private void buttonFill_Click(object sender, EventArgs e)
sl@3
  1285
        {
sl@3
  1286
            iDisplay.Fill();
sl@3
  1287
            iDisplay.SwapBuffers();
sl@3
  1288
        }
sl@3
  1289
sl@3
  1290
        private void trackBarBrightness_Scroll(object sender, EventArgs e)
sl@3
  1291
        {
sl@48
  1292
            cds.Brightness = trackBarBrightness.Value;
sl@9
  1293
            Properties.Settings.Default.Save();
sl@3
  1294
            iDisplay.SetBrightness(trackBarBrightness.Value);
sl@9
  1295
sl@3
  1296
        }
sl@7
  1297
sl@48
  1298
sl@48
  1299
        /// <summary>
sl@48
  1300
        /// CDS stands for Current Display Settings
sl@48
  1301
        /// </summary>
sl@50
  1302
        private DisplaySettings cds
sl@48
  1303
        {
sl@48
  1304
            get
sl@48
  1305
            {
sl@65
  1306
                DisplaysSettings settings = Properties.Settings.Default.DisplaysSettings;
sl@51
  1307
                if (settings == null)
sl@51
  1308
                {
sl@51
  1309
                    settings = new DisplaysSettings();
sl@51
  1310
                    settings.Init();
sl@65
  1311
                    Properties.Settings.Default.DisplaysSettings = settings;
sl@51
  1312
                }
sl@48
  1313
sl@48
  1314
                //Make sure all our settings have been created
sl@48
  1315
                while (settings.Displays.Count <= Properties.Settings.Default.CurrentDisplayIndex)
sl@48
  1316
                {
sl@50
  1317
                    settings.Displays.Add(new DisplaySettings());
sl@48
  1318
                }
sl@48
  1319
sl@50
  1320
                DisplaySettings displaySettings = settings.Displays[Properties.Settings.Default.CurrentDisplayIndex];
sl@48
  1321
                return displaySettings;
sl@48
  1322
            }
sl@48
  1323
        }
sl@48
  1324
sl@54
  1325
        /// <summary>
sl@54
  1326
        /// Check if the given font has a fixed character pitch.
sl@54
  1327
        /// </summary>
sl@54
  1328
        /// <param name="ft"></param>
sl@54
  1329
        /// <returns>0.0f if this is not a monospace font, otherwise returns the character width.</returns>
sl@54
  1330
        public float IsFixedWidth(Font ft)
sl@54
  1331
        {
sl@54
  1332
            Graphics g = CreateGraphics();
StephaneLenclud@223
  1333
            char[] charSizes = new char[] {'i', 'a', 'Z', '%', '#', 'a', 'B', 'l', 'm', ',', '.'};
sl@54
  1334
            float charWidth = g.MeasureString("I", ft, Int32.MaxValue, StringFormat.GenericTypographic).Width;
sl@54
  1335
sl@54
  1336
            bool fixedWidth = true;
sl@54
  1337
sl@54
  1338
            foreach (char c in charSizes)
StephaneLenclud@223
  1339
                if (g.MeasureString(c.ToString(), ft, Int32.MaxValue, StringFormat.GenericTypographic).Width !=
StephaneLenclud@223
  1340
                    charWidth)
sl@54
  1341
                    fixedWidth = false;
sl@54
  1342
sl@54
  1343
            if (fixedWidth)
sl@54
  1344
            {
sl@54
  1345
                return charWidth;
sl@54
  1346
            }
sl@54
  1347
sl@54
  1348
            return 0.0f;
sl@54
  1349
        }
sl@54
  1350
StephaneLenclud@223
  1351
        /// <summary>
StephaneLenclud@223
  1352
        /// Synchronize UI with settings
StephaneLenclud@223
  1353
        /// </summary>
sl@7
  1354
        private void UpdateStatus()
StephaneLenclud@223
  1355
        {
sl@48
  1356
            //Load settings
sl@54
  1357
            checkBoxShowBorders.Checked = cds.ShowBorders;
StephaneLenclud@223
  1358
            iTableLayoutPanel.CellBorderStyle = (cds.ShowBorders
StephaneLenclud@223
  1359
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1360
                : TableLayoutPanelCellBorderStyle.None);
sl@60
  1361
sl@60
  1362
            //Set the proper font to each of our labels
StephaneLenclud@175
  1363
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
sl@60
  1364
            {
sl@60
  1365
                ctrl.Font = cds.Font;
sl@60
  1366
            }
sl@60
  1367
sl@54
  1368
            CheckFontHeight();
StephaneLenclud@223
  1369
            //Check if "run on Windows startup" is enabled
StephaneLenclud@223
  1370
            checkBoxAutoStart.Checked = iStartupManager.Startup;
StephaneLenclud@268
  1371
            
StephaneLenclud@168
  1372
            //CEC settings
StephaneLenclud@168
  1373
            comboBoxHdmiPort.SelectedIndex = Properties.Settings.Default.CecHdmiPort - 1;
StephaneLenclud@153
  1374
StephaneLenclud@168
  1375
            //Mini Display settings
sl@48
  1376
            checkBoxReverseScreen.Checked = cds.ReverseScreen;
sl@57
  1377
            checkBoxInverseColors.Checked = cds.InverseColors;
StephaneLenclud@223
  1378
            checkBoxShowVolumeLabel.Checked = cds.ShowVolumeLabel;
sl@100
  1379
            checkBoxScaleToFit.Checked = cds.ScaleToFit;
sl@100
  1380
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1381
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1382
            maskedTextBoxMinFontSize.Text = cds.MinFontSize.ToString();
StephaneLenclud@223
  1383
            maskedTextBoxScrollingSpeed.Text = cds.ScrollingSpeedInPixelsPerSecond.ToString();
sl@48
  1384
            comboBoxDisplayType.SelectedIndex = cds.DisplayType;
StephaneLenclud@270
  1385
            iTimerDisplay.Interval = cds.TimerInterval;
sl@48
  1386
            maskedTextBoxTimerInterval.Text = cds.TimerInterval.ToString();
sl@100
  1387
            textBoxScrollLoopSeparator.Text = cds.Separator;
sl@58
  1388
            //
sl@58
  1389
            SetupPixelDelegates();
sl@48
  1390
sl@7
  1391
            if (iDisplay.IsOpen())
sl@7
  1392
            {
StephaneLenclud@187
  1393
                //We have a display connection
StephaneLenclud@187
  1394
                //Reflect that in our UI
StephaneLenclud@187
  1395
                StartTimer();
StephaneLenclud@103
  1396
StephaneLenclud@223
  1397
                iTableLayoutPanel.Enabled = true;
StephaneLenclud@223
  1398
                panelDisplay.Enabled = true;
StephaneLenclud@103
  1399
sl@48
  1400
                //Only setup brightness if display is open
sl@48
  1401
                trackBarBrightness.Minimum = iDisplay.MinBrightness();
sl@48
  1402
                trackBarBrightness.Maximum = iDisplay.MaxBrightness();
StephaneLenclud@223
  1403
                if (cds.Brightness < iDisplay.MinBrightness() || cds.Brightness > iDisplay.MaxBrightness())
StephaneLenclud@223
  1404
                {
StephaneLenclud@223
  1405
                    //Brightness out of range, this can occur when using auto-detect
StephaneLenclud@223
  1406
                    //Use max brightness instead
StephaneLenclud@223
  1407
                    trackBarBrightness.Value = iDisplay.MaxBrightness();
StephaneLenclud@223
  1408
                    iDisplay.SetBrightness(iDisplay.MaxBrightness());
StephaneLenclud@223
  1409
                }
StephaneLenclud@223
  1410
                else
StephaneLenclud@223
  1411
                {
StephaneLenclud@223
  1412
                    trackBarBrightness.Value = cds.Brightness;
StephaneLenclud@223
  1413
                    iDisplay.SetBrightness(cds.Brightness);
StephaneLenclud@223
  1414
                }
StephaneLenclud@223
  1415
StephaneLenclud@223
  1416
                //Try compute the steps to something that makes sense
StephaneLenclud@223
  1417
                trackBarBrightness.LargeChange = Math.Max(1, (iDisplay.MaxBrightness() - iDisplay.MinBrightness())/5);
sl@48
  1418
                trackBarBrightness.SmallChange = 1;
StephaneLenclud@223
  1419
sl@48
  1420
                //
sl@7
  1421
                buttonFill.Enabled = true;
sl@7
  1422
                buttonClear.Enabled = true;
sl@7
  1423
                buttonOpen.Enabled = false;
sl@7
  1424
                buttonClose.Enabled = true;
sl@7
  1425
                trackBarBrightness.Enabled = true;
sl@10
  1426
                toolStripStatusLabelConnect.Text = "Connected - " + iDisplay.Vendor() + " - " + iDisplay.Product();
sl@10
  1427
                //+ " - " + iDisplay.SerialNumber();
sl@52
  1428
sl@52
  1429
                if (iDisplay.SupportPowerOnOff())
sl@52
  1430
                {
sl@52
  1431
                    buttonPowerOn.Enabled = true;
sl@52
  1432
                    buttonPowerOff.Enabled = true;
sl@52
  1433
                }
sl@52
  1434
                else
sl@52
  1435
                {
sl@52
  1436
                    buttonPowerOn.Enabled = false;
sl@52
  1437
                    buttonPowerOff.Enabled = false;
sl@52
  1438
                }
sl@53
  1439
sl@53
  1440
                if (iDisplay.SupportClock())
sl@53
  1441
                {
sl@53
  1442
                    buttonShowClock.Enabled = true;
sl@53
  1443
                    buttonHideClock.Enabled = true;
sl@53
  1444
                }
sl@53
  1445
                else
sl@53
  1446
                {
sl@53
  1447
                    buttonShowClock.Enabled = false;
sl@53
  1448
                    buttonHideClock.Enabled = false;
sl@53
  1449
                }
StephaneLenclud@115
  1450
StephaneLenclud@223
  1451
StephaneLenclud@223
  1452
                //Check if Volume Label is supported. To date only MDM166AA supports that crap :)
StephaneLenclud@223
  1453
                checkBoxShowVolumeLabel.Enabled = iDisplay.IconCount(MiniDisplay.IconType.VolumeLabel) > 0;
StephaneLenclud@223
  1454
StephaneLenclud@223
  1455
                if (cds.ShowVolumeLabel)
StephaneLenclud@223
  1456
                {
StephaneLenclud@135
  1457
                    iDisplay.SetIconOn(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1458
                }
StephaneLenclud@223
  1459
                else
StephaneLenclud@223
  1460
                {
StephaneLenclud@135
  1461
                    iDisplay.SetIconOff(MiniDisplay.IconType.VolumeLabel);
StephaneLenclud@223
  1462
                }
sl@7
  1463
            }
sl@7
  1464
            else
sl@7
  1465
            {
StephaneLenclud@187
  1466
                //Display connection not available
StephaneLenclud@187
  1467
                //Reflect that in our UI
StephaneLenclud@187
  1468
#if DEBUG
StephaneLenclud@187
  1469
                //In debug start our timer even if we don't have a display connection
StephaneLenclud@187
  1470
                StartTimer();
StephaneLenclud@187
  1471
#else
StephaneLenclud@223
  1472
    //In production environment we don't need our timer if no display connection
StephaneLenclud@187
  1473
                StopTimer();
StephaneLenclud@187
  1474
#endif
StephaneLenclud@187
  1475
                checkBoxShowVolumeLabel.Enabled = false;
StephaneLenclud@223
  1476
                iTableLayoutPanel.Enabled = false;
StephaneLenclud@223
  1477
                panelDisplay.Enabled = false;
sl@7
  1478
                buttonFill.Enabled = false;
sl@7
  1479
                buttonClear.Enabled = false;
sl@7
  1480
                buttonOpen.Enabled = true;
sl@7
  1481
                buttonClose.Enabled = false;
sl@7
  1482
                trackBarBrightness.Enabled = false;
sl@52
  1483
                buttonPowerOn.Enabled = false;
sl@52
  1484
                buttonPowerOff.Enabled = false;
sl@53
  1485
                buttonShowClock.Enabled = false;
sl@53
  1486
                buttonHideClock.Enabled = false;
sl@9
  1487
                toolStripStatusLabelConnect.Text = "Disconnected";
sl@48
  1488
                toolStripStatusLabelPower.Text = "N/A";
sl@7
  1489
            }
StephaneLenclud@106
  1490
sl@7
  1491
        }
sl@9
  1492
sl@13
  1493
StephaneLenclud@223
  1494
        /// <summary>
StephaneLenclud@223
  1495
        /// 
StephaneLenclud@223
  1496
        /// </summary>
StephaneLenclud@223
  1497
        /// <param name="sender"></param>
StephaneLenclud@223
  1498
        /// <param name="e"></param>
StephaneLenclud@223
  1499
        private void checkBoxShowVolumeLabel_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1500
        {
StephaneLenclud@223
  1501
            cds.ShowVolumeLabel = checkBoxShowVolumeLabel.Checked;
StephaneLenclud@223
  1502
            Properties.Settings.Default.Save();
StephaneLenclud@223
  1503
            UpdateStatus();
StephaneLenclud@223
  1504
        }
sl@13
  1505
sl@9
  1506
        private void checkBoxShowBorders_CheckedChanged(object sender, EventArgs e)
sl@9
  1507
        {
sl@16
  1508
            //Save our show borders setting
StephaneLenclud@223
  1509
            iTableLayoutPanel.CellBorderStyle = (checkBoxShowBorders.Checked
StephaneLenclud@223
  1510
                ? TableLayoutPanelCellBorderStyle.Single
StephaneLenclud@223
  1511
                : TableLayoutPanelCellBorderStyle.None);
sl@48
  1512
            cds.ShowBorders = checkBoxShowBorders.Checked;
sl@9
  1513
            Properties.Settings.Default.Save();
sl@57
  1514
            CheckFontHeight();
sl@9
  1515
        }
sl@13
  1516
StephaneLenclud@194
  1517
        private void checkBoxAutoStart_CheckedChanged(object sender, EventArgs e)
StephaneLenclud@223
  1518
        {
StephaneLenclud@223
  1519
            iStartupManager.Startup = checkBoxAutoStart.Checked;
StephaneLenclud@223
  1520
        }
sl@94
  1521
sl@94
  1522
sl@16
  1523
        private void checkBoxReverseScreen_CheckedChanged(object sender, EventArgs e)
sl@16
  1524
        {
sl@16
  1525
            //Save our reverse screen setting
sl@48
  1526
            cds.ReverseScreen = checkBoxReverseScreen.Checked;
sl@16
  1527
            Properties.Settings.Default.Save();
sl@58
  1528
            SetupPixelDelegates();
sl@16
  1529
        }
sl@16
  1530
sl@57
  1531
        private void checkBoxInverseColors_CheckedChanged(object sender, EventArgs e)
sl@57
  1532
        {
sl@57
  1533
            //Save our inverse colors setting
sl@57
  1534
            cds.InverseColors = checkBoxInverseColors.Checked;
sl@57
  1535
            Properties.Settings.Default.Save();
sl@58
  1536
            SetupPixelDelegates();
sl@57
  1537
        }
sl@57
  1538
sl@100
  1539
        private void checkBoxScaleToFit_CheckedChanged(object sender, EventArgs e)
sl@100
  1540
        {
sl@100
  1541
            //Save our scale to fit setting
sl@100
  1542
            cds.ScaleToFit = checkBoxScaleToFit.Checked;
sl@100
  1543
            Properties.Settings.Default.Save();
sl@100
  1544
            //
sl@100
  1545
            labelMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1546
            maskedTextBoxMinFontSize.Enabled = cds.ScaleToFit;
sl@100
  1547
        }
sl@100
  1548
sl@14
  1549
        private void MainForm_Resize(object sender, EventArgs e)
sl@14
  1550
        {
sl@14
  1551
            if (WindowState == FormWindowState.Minimized)
sl@14
  1552
            {
StephaneLenclud@158
  1553
                // To workaround our empty bitmap bug on Windows 7 we need to recreate our bitmap when the application is minimized
StephaneLenclud@158
  1554
                // That's apparently not needed on Windows 10 but we better leave it in place.
sl@14
  1555
                iCreateBitmap = true;
sl@14
  1556
            }
sl@14
  1557
        }
sl@14
  1558
sl@17
  1559
        private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
sl@17
  1560
        {
StephaneLenclud@273
  1561
            //TODO: discard other CSCore audio objects
StephaneLenclud@273
  1562
            StopAudioVisualization();
StephaneLenclud@167
  1563
            iCecManager.Stop();
StephaneLenclud@223
  1564
            iNetworkManager.Dispose();
StephaneLenclud@223
  1565
            CloseDisplayConnection();
sl@17
  1566
            StopServer();
sl@32
  1567
            e.Cancel = iClosing;
sl@17
  1568
        }
sl@17
  1569
sl@17
  1570
        public void StartServer()
sl@17
  1571
        {
sl@17
  1572
            iServiceHost = new ServiceHost
sl@17
  1573
                (
StephaneLenclud@223
  1574
                typeof(Session),
StephaneLenclud@223
  1575
                new Uri[] {new Uri("net.tcp://localhost:8001/")}
sl@17
  1576
                );
sl@17
  1577
StephaneLenclud@223
  1578
            iServiceHost.AddServiceEndpoint(typeof(IService), new NetTcpBinding(SecurityMode.None, true),
StephaneLenclud@223
  1579
                "DisplayService");
sl@17
  1580
            iServiceHost.Open();
sl@17
  1581
        }
sl@17
  1582
sl@17
  1583
        public void StopServer()
sl@17
  1584
        {
sl@32
  1585
            if (iClients.Count > 0 && !iClosing)
sl@29
  1586
            {
sl@29
  1587
                //Tell our clients
sl@32
  1588
                iClosing = true;
sl@29
  1589
                BroadcastCloseEvent();
sl@29
  1590
            }
sl@32
  1591
            else if (iClosing)
sl@32
  1592
            {
StephaneLenclud@223
  1593
                if (
StephaneLenclud@223
  1594
                    MessageBox.Show("Force exit?", "Waiting for clients...", MessageBoxButtons.YesNo,
StephaneLenclud@223
  1595
                        MessageBoxIcon.Warning) == DialogResult.Yes)
sl@32
  1596
                {
sl@32
  1597
                    iClosing = false; //We make sure we force close if asked twice
sl@32
  1598
                }
sl@32
  1599
            }
sl@32
  1600
            else
sl@36
  1601
            {
sl@32
  1602
                //We removed that as it often lags for some reason
sl@32
  1603
                //iServiceHost.Close();
sl@32
  1604
            }
sl@17
  1605
        }
sl@17
  1606
sl@21
  1607
        public void BroadcastCloseEvent()
sl@21
  1608
        {
sl@31
  1609
            Trace.TraceInformation("BroadcastCloseEvent - start");
sl@31
  1610
sl@21
  1611
            var inactiveClients = new List<string>();
sl@21
  1612
            foreach (var client in iClients)
sl@21
  1613
            {
sl@21
  1614
                //if (client.Key != eventData.ClientName)
sl@21
  1615
                {
sl@21
  1616
                    try
sl@21
  1617
                    {
sl@31
  1618
                        Trace.TraceInformation("BroadcastCloseEvent - " + client.Key);
StephaneLenclud@223
  1619
                        client.Value.Callback.OnCloseOrder( /*eventData*/);
sl@21
  1620
                    }
sl@21
  1621
                    catch (Exception ex)
sl@21
  1622
                    {
sl@21
  1623
                        inactiveClients.Add(client.Key);
sl@21
  1624
                    }
sl@21
  1625
                }
sl@21
  1626
            }
sl@21
  1627
sl@21
  1628
            if (inactiveClients.Count > 0)
sl@21
  1629
            {
sl@21
  1630
                foreach (var client in inactiveClients)
sl@21
  1631
                {
sl@21
  1632
                    iClients.Remove(client);
StephaneLenclud@226
  1633
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1634
                        Program.iFormMain.iTreeViewClients.Nodes.Find(client, false)[0]);
sl@21
  1635
                }
sl@21
  1636
            }
sl@97
  1637
StephaneLenclud@223
  1638
            if (iClients.Count == 0)
StephaneLenclud@223
  1639
            {
StephaneLenclud@223
  1640
                ClearLayout();
StephaneLenclud@223
  1641
            }
sl@21
  1642
        }
sl@21
  1643
StephaneLenclud@223
  1644
        /// <summary>
StephaneLenclud@223
  1645
        /// Just remove all our fields.
StephaneLenclud@223
  1646
        /// </summary>
StephaneLenclud@223
  1647
        private void ClearLayout()
StephaneLenclud@223
  1648
        {
StephaneLenclud@223
  1649
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@223
  1650
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@223
  1651
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@223
  1652
            iCurrentClientData = null;
StephaneLenclud@223
  1653
        }
StephaneLenclud@223
  1654
StephaneLenclud@223
  1655
        /// <summary>
StephaneLenclud@223
  1656
        /// Just launch a demo client.
StephaneLenclud@223
  1657
        /// </summary>
StephaneLenclud@223
  1658
        private void StartNewClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@223
  1659
        {
StephaneLenclud@223
  1660
            Thread clientThread = new Thread(SharpDisplayClient.Program.MainWithParams);
StephaneLenclud@223
  1661
            SharpDisplayClient.StartParams myParams = new SharpDisplayClient.StartParams(
StephaneLenclud@223
  1662
                new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@223
  1663
            clientThread.Start(myParams);
StephaneLenclud@223
  1664
            BringToFront();
StephaneLenclud@223
  1665
        }
StephaneLenclud@106
  1666
StephaneLenclud@189
  1667
        /// <summary>
StephaneLenclud@189
  1668
        /// Just launch our idle client.
StephaneLenclud@189
  1669
        /// </summary>
StephaneLenclud@189
  1670
        private void StartIdleClient(string aTopText = "", string aBottomText = "")
StephaneLenclud@189
  1671
        {
StephaneLenclud@225
  1672
            Thread clientThread = new Thread(SharpDisplayClientIdle.Program.MainWithParams);
StephaneLenclud@225
  1673
            SharpDisplayClientIdle.StartParams myParams =
StephaneLenclud@225
  1674
                new SharpDisplayClientIdle.StartParams(new Point(this.Right, this.Top), aTopText, aBottomText);
StephaneLenclud@189
  1675
            clientThread.Start(myParams);
StephaneLenclud@189
  1676
            BringToFront();
StephaneLenclud@189
  1677
        }
StephaneLenclud@189
  1678
StephaneLenclud@189
  1679
sl@25
  1680
        private void buttonStartClient_Click(object sender, EventArgs e)
sl@25
  1681
        {
StephaneLenclud@223
  1682
            StartNewClient();
sl@25
  1683
        }
sl@25
  1684
sl@27
  1685
        private void buttonSuspend_Click(object sender, EventArgs e)
sl@27
  1686
        {
StephaneLenclud@187
  1687
            ToggleTimer();
StephaneLenclud@187
  1688
        }
StephaneLenclud@187
  1689
StephaneLenclud@187
  1690
        private void StartTimer()
StephaneLenclud@187
  1691
        {
StephaneLenclud@187
  1692
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1693
            iTimerDisplay.Enabled = true;
StephaneLenclud@187
  1694
            UpdateSuspendButton();
StephaneLenclud@187
  1695
        }
StephaneLenclud@187
  1696
StephaneLenclud@187
  1697
        private void StopTimer()
StephaneLenclud@187
  1698
        {
StephaneLenclud@187
  1699
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1700
            iTimerDisplay.Enabled = false;
StephaneLenclud@187
  1701
            UpdateSuspendButton();
StephaneLenclud@187
  1702
        }
StephaneLenclud@187
  1703
StephaneLenclud@187
  1704
        private void ToggleTimer()
StephaneLenclud@187
  1705
        {
sl@52
  1706
            LastTickTime = DateTime.Now; //Reset timer to prevent jump
StephaneLenclud@270
  1707
            iTimerDisplay.Enabled = !iTimerDisplay.Enabled;
StephaneLenclud@187
  1708
            UpdateSuspendButton();
StephaneLenclud@187
  1709
        }
StephaneLenclud@187
  1710
StephaneLenclud@187
  1711
        private void UpdateSuspendButton()
StephaneLenclud@187
  1712
        {
StephaneLenclud@270
  1713
            if (!iTimerDisplay.Enabled)
sl@27
  1714
            {
sl@52
  1715
                buttonSuspend.Text = "Run";
sl@27
  1716
            }
sl@27
  1717
            else
sl@27
  1718
            {
sl@27
  1719
                buttonSuspend.Text = "Pause";
sl@27
  1720
            }
sl@27
  1721
        }
sl@27
  1722
StephaneLenclud@187
  1723
sl@29
  1724
        private void buttonCloseClients_Click(object sender, EventArgs e)
sl@29
  1725
        {
sl@29
  1726
            BroadcastCloseEvent();
sl@29
  1727
        }
sl@29
  1728
sl@30
  1729
        private void treeViewClients_AfterSelect(object sender, TreeViewEventArgs e)
sl@30
  1730
        {
StephaneLenclud@141
  1731
            //Root node must have at least one child
StephaneLenclud@141
  1732
            if (e.Node.Nodes.Count == 0)
StephaneLenclud@141
  1733
            {
StephaneLenclud@141
  1734
                return;
StephaneLenclud@141
  1735
            }
sl@21
  1736
StephaneLenclud@141
  1737
            //If the selected node is the root node of a client then switch to it
StephaneLenclud@223
  1738
            string sessionId = e.Node.Nodes[0].Text; //First child of a root node is the sessionId
StephaneLenclud@141
  1739
            if (iClients.ContainsKey(sessionId)) //Check that's actually what we are looking at
StephaneLenclud@141
  1740
            {
StephaneLenclud@141
  1741
                //We have a valid session just switch to that client
StephaneLenclud@223
  1742
                SetCurrentClient(sessionId, true);
StephaneLenclud@141
  1743
            }
StephaneLenclud@223
  1744
sl@30
  1745
        }
sl@30
  1746
sl@36
  1747
sl@30
  1748
        /// <summary>
sl@36
  1749
        ///
sl@30
  1750
        /// </summary>
sl@30
  1751
        /// <param name="aSessionId"></param>
sl@30
  1752
        /// <param name="aCallback"></param>
sl@55
  1753
        public void AddClientThreadSafe(string aSessionId, ICallback aCallback)
sl@30
  1754
        {
sl@33
  1755
            if (this.InvokeRequired)
sl@30
  1756
            {
sl@30
  1757
                //Not in the proper thread, invoke ourselves
sl@30
  1758
                AddClientDelegate d = new AddClientDelegate(AddClientThreadSafe);
StephaneLenclud@223
  1759
                this.Invoke(d, new object[] {aSessionId, aCallback});
sl@30
  1760
            }
sl@30
  1761
            else
sl@30
  1762
            {
sl@30
  1763
                //We are in the proper thread
sl@30
  1764
                //Add this session to our collection of clients
sl@33
  1765
                ClientData newClient = new ClientData(aSessionId, aCallback);
StephaneLenclud@226
  1766
                Program.iFormMain.iClients.Add(aSessionId, newClient);
sl@30
  1767
                //Add this session to our UI
sl@33
  1768
                UpdateClientTreeViewNode(newClient);
sl@30
  1769
            }
sl@30
  1770
        }
sl@30
  1771
Stephane@186
  1772
Stephane@186
  1773
        /// <summary>
Stephane@186
  1774
        /// Find the client with the highest priority if any.
Stephane@186
  1775
        /// </summary>
Stephane@186
  1776
        /// <returns>Our highest priority client or null if not a single client is connected.</returns>
Stephane@186
  1777
        public ClientData FindHighestPriorityClient()
Stephane@186
  1778
        {
Stephane@186
  1779
            ClientData highestPriorityClient = null;
Stephane@186
  1780
            foreach (var client in iClients)
Stephane@186
  1781
            {
Stephane@186
  1782
                if (highestPriorityClient == null || client.Value.Priority > highestPriorityClient.Priority)
Stephane@186
  1783
                {
Stephane@186
  1784
                    highestPriorityClient = client.Value;
Stephane@186
  1785
                }
Stephane@186
  1786
            }
Stephane@186
  1787
Stephane@186
  1788
            return highestPriorityClient;
Stephane@186
  1789
        }
Stephane@186
  1790
sl@30
  1791
        /// <summary>
sl@36
  1792
        ///
sl@30
  1793
        /// </summary>
sl@30
  1794
        /// <param name="aSessionId"></param>
sl@30
  1795
        public void RemoveClientThreadSafe(string aSessionId)
sl@30
  1796
        {
sl@33
  1797
            if (this.InvokeRequired)
sl@30
  1798
            {
sl@30
  1799
                //Not in the proper thread, invoke ourselves
sl@30
  1800
                RemoveClientDelegate d = new RemoveClientDelegate(RemoveClientThreadSafe);
StephaneLenclud@223
  1801
                this.Invoke(d, new object[] {aSessionId});
sl@30
  1802
            }
sl@30
  1803
            else
sl@30
  1804
            {
sl@30
  1805
                //We are in the proper thread
sl@33
  1806
                //Remove this session from both client collection and UI tree view
StephaneLenclud@226
  1807
                if (Program.iFormMain.iClients.Keys.Contains(aSessionId))
sl@30
  1808
                {
StephaneLenclud@226
  1809
                    Program.iFormMain.iClients.Remove(aSessionId);
StephaneLenclud@226
  1810
                    Program.iFormMain.iTreeViewClients.Nodes.Remove(
StephaneLenclud@226
  1811
                        Program.iFormMain.iTreeViewClients.Nodes.Find(aSessionId, false)[0]);
StephaneLenclud@188
  1812
                    //Update recording status too whenever a client is removed
StephaneLenclud@188
  1813
                    UpdateRecordingNotification();
sl@32
  1814
                }
sl@32
  1815
Stephane@186
  1816
                if (iCurrentClientSessionId == aSessionId)
Stephane@186
  1817
                {
Stephane@186
  1818
                    //The current client is closing
Stephane@186
  1819
                    iCurrentClientData = null;
Stephane@186
  1820
                    //Find the client with the highest priority and set it as current
Stephane@186
  1821
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  1822
                    if (newCurrentClient != null)
Stephane@186
  1823
                    {
Stephane@186
  1824
                        SetCurrentClient(newCurrentClient.SessionId, true);
StephaneLenclud@223
  1825
                    }
Stephane@186
  1826
                }
Stephane@186
  1827
Stephane@186
  1828
                if (iClients.Count == 0)
StephaneLenclud@223
  1829
                {
StephaneLenclud@223
  1830
                    //Clear our screen when last client disconnects
StephaneLenclud@223
  1831
                    ClearLayout();
StephaneLenclud@223
  1832
StephaneLenclud@223
  1833
                    if (iClosing)
StephaneLenclud@223
  1834
                    {
StephaneLenclud@223
  1835
                        //We were closing our form
StephaneLenclud@223
  1836
                        //All clients are now closed
StephaneLenclud@223
  1837
                        //Just resume our close operation
StephaneLenclud@223
  1838
                        iClosing = false;
StephaneLenclud@223
  1839
                        Close();
StephaneLenclud@223
  1840
                    }
StephaneLenclud@223
  1841
                }
sl@30
  1842
            }
sl@30
  1843
        }
sl@30
  1844
sl@30
  1845
        /// <summary>
sl@36
  1846
        ///
sl@30
  1847
        /// </summary>
sl@62
  1848
        /// <param name="aSessionId"></param>
sl@72
  1849
        /// <param name="aLayout"></param>
sl@62
  1850
        public void SetClientLayoutThreadSafe(string aSessionId, TableLayout aLayout)
sl@62
  1851
        {
sl@62
  1852
            if (this.InvokeRequired)
sl@62
  1853
            {
sl@62
  1854
                //Not in the proper thread, invoke ourselves
sl@62
  1855
                SetLayoutDelegate d = new SetLayoutDelegate(SetClientLayoutThreadSafe);
StephaneLenclud@223
  1856
                this.Invoke(d, new object[] {aSessionId, aLayout});
sl@62
  1857
            }
sl@62
  1858
            else
sl@62
  1859
            {
sl@62
  1860
                ClientData client = iClients[aSessionId];
sl@62
  1861
                if (client != null)
sl@62
  1862
                {
StephaneLenclud@148
  1863
                    //Don't change a thing if the layout is the same
StephaneLenclud@148
  1864
                    if (!client.Layout.IsSameAs(aLayout))
StephaneLenclud@148
  1865
                    {
StephaneLenclud@158
  1866
                        Debug.Print("SetClientLayoutThreadSafe: Layout updated.");
StephaneLenclud@148
  1867
                        //Set our client layout then
StephaneLenclud@148
  1868
                        client.Layout = aLayout;
StephaneLenclud@175
  1869
                        //So that next time we update all our fields at ones
StephaneLenclud@175
  1870
                        client.HasNewLayout = true;
StephaneLenclud@158
  1871
                        //Layout has changed clear our fields then
StephaneLenclud@158
  1872
                        client.Fields.Clear();
StephaneLenclud@148
  1873
                        //
StephaneLenclud@148
  1874
                        UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1875
                    }
StephaneLenclud@158
  1876
                    else
StephaneLenclud@158
  1877
                    {
StephaneLenclud@158
  1878
                        Debug.Print("SetClientLayoutThreadSafe: Layout has not changed.");
StephaneLenclud@158
  1879
                    }
sl@62
  1880
                }
sl@62
  1881
            }
sl@62
  1882
        }
sl@62
  1883
sl@62
  1884
        /// <summary>
sl@62
  1885
        ///
sl@62
  1886
        /// </summary>
sl@67
  1887
        /// <param name="aSessionId"></param>
sl@72
  1888
        /// <param name="aField"></param>
sl@75
  1889
        public void SetClientFieldThreadSafe(string aSessionId, DataField aField)
sl@30
  1890
        {
sl@33
  1891
            if (this.InvokeRequired)
sl@30
  1892
            {
sl@30
  1893
                //Not in the proper thread, invoke ourselves
sl@79
  1894
                SetFieldDelegate d = new SetFieldDelegate(SetClientFieldThreadSafe);
StephaneLenclud@223
  1895
                this.Invoke(d, new object[] {aSessionId, aField});
sl@30
  1896
            }
sl@30
  1897
            else
sl@30
  1898
            {
sl@75
  1899
                //We are in the proper thread
sl@75
  1900
                //Call the non-thread-safe variant
sl@75
  1901
                SetClientField(aSessionId, aField);
sl@75
  1902
            }
sl@75
  1903
        }
sl@75
  1904
StephaneLenclud@176
  1905
StephaneLenclud@176
  1906
StephaneLenclud@176
  1907
sl@75
  1908
        /// <summary>
StephaneLenclud@175
  1909
        /// Set a data field in the given client.
sl@75
  1910
        /// </summary>
sl@75
  1911
        /// <param name="aSessionId"></param>
sl@75
  1912
        /// <param name="aField"></param>
sl@75
  1913
        private void SetClientField(string aSessionId, DataField aField)
StephaneLenclud@223
  1914
        {
StephaneLenclud@141
  1915
            //TODO: should check if the field actually changed?
StephaneLenclud@141
  1916
sl@75
  1917
            ClientData client = iClients[aSessionId];
StephaneLenclud@141
  1918
            bool layoutChanged = false;
StephaneLenclud@148
  1919
            bool contentChanged = true;
StephaneLenclud@141
  1920
StephaneLenclud@176
  1921
            //Fetch our field index
StephaneLenclud@176
  1922
            int fieldIndex = client.FindSameFieldIndex(aField);
StephaneLenclud@176
  1923
StephaneLenclud@176
  1924
            if (fieldIndex < 0)
sl@75
  1925
            {
StephaneLenclud@176
  1926
                //No corresponding field, just bail out
StephaneLenclud@176
  1927
                return;
StephaneLenclud@141
  1928
            }
sl@76
  1929
StephaneLenclud@175
  1930
            //Keep our previous field in there
StephaneLenclud@176
  1931
            DataField previousField = client.Fields[fieldIndex];
StephaneLenclud@176
  1932
            //Just update that field then 
StephaneLenclud@176
  1933
            client.Fields[fieldIndex] = aField;
StephaneLenclud@148
  1934
StephaneLenclud@176
  1935
            if (!aField.IsTableField)
StephaneLenclud@176
  1936
            {
StephaneLenclud@176
  1937
                //We are done then if that field is not in our table layout
StephaneLenclud@176
  1938
                return;
StephaneLenclud@176
  1939
            }
StephaneLenclud@176
  1940
StephaneLenclud@176
  1941
            TableField tableField = (TableField) aField;
StephaneLenclud@172
  1942
StephaneLenclud@175
  1943
            if (previousField.IsSameLayout(aField))
StephaneLenclud@141
  1944
            {
StephaneLenclud@141
  1945
                //If we are updating a field in our current client we need to update it in our panel
StephaneLenclud@141
  1946
                if (aSessionId == iCurrentClientSessionId)
sl@30
  1947
                {
StephaneLenclud@223
  1948
                    Control ctrl = iTableLayoutPanel.GetControlFromPosition(tableField.Column, tableField.Row);
StephaneLenclud@176
  1949
                    if (aField.IsTextField && ctrl is MarqueeLabel)
sl@79
  1950
                    {
StephaneLenclud@223
  1951
                        TextField textField = (TextField) aField;
sl@75
  1952
                        //Text field control already in place, just change the text
StephaneLenclud@223
  1953
                        MarqueeLabel label = (MarqueeLabel) ctrl;
StephaneLenclud@172
  1954
                        contentChanged = (label.Text != textField.Text || label.TextAlign != textField.Alignment);
StephaneLenclud@172
  1955
                        label.Text = textField.Text;
StephaneLenclud@172
  1956
                        label.TextAlign = textField.Alignment;
sl@68
  1957
                    }
StephaneLenclud@176
  1958
                    else if (aField.IsBitmapField && ctrl is PictureBox)
sl@75
  1959
                    {
StephaneLenclud@223
  1960
                        BitmapField bitmapField = (BitmapField) aField;
StephaneLenclud@148
  1961
                        contentChanged = true; //TODO: Bitmap comp or should we leave that to clients?
sl@75
  1962
                        //Bitmap field control already in place just change the bitmap
StephaneLenclud@223
  1963
                        PictureBox pictureBox = (PictureBox) ctrl;
StephaneLenclud@172
  1964
                        pictureBox.Image = bitmapField.Bitmap;
sl@75
  1965
                    }
sl@68
  1966
                    else
sl@68
  1967
                    {
StephaneLenclud@141
  1968
                        layoutChanged = true;
sl@68
  1969
                    }
sl@30
  1970
                }
StephaneLenclud@141
  1971
            }
StephaneLenclud@141
  1972
            else
StephaneLenclud@223
  1973
            {
StephaneLenclud@141
  1974
                layoutChanged = true;
StephaneLenclud@141
  1975
            }
StephaneLenclud@141
  1976
StephaneLenclud@148
  1977
            //If either content or layout changed we need to update our tree view to reflect the changes
StephaneLenclud@148
  1978
            if (contentChanged || layoutChanged)
StephaneLenclud@141
  1979
            {
StephaneLenclud@141
  1980
                UpdateClientTreeViewNode(client);
StephaneLenclud@148
  1981
                //
StephaneLenclud@148
  1982
                if (layoutChanged)
sl@75
  1983
                {
StephaneLenclud@148
  1984
                    Debug.Print("Layout changed");
StephaneLenclud@148
  1985
                    //Our layout has changed, if we are already the current client we need to update our panel
StephaneLenclud@148
  1986
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@148
  1987
                    {
StephaneLenclud@148
  1988
                        //Apply layout and set data fields.
StephaneLenclud@148
  1989
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@148
  1990
                    }
sl@75
  1991
                }
StephaneLenclud@158
  1992
                else
StephaneLenclud@158
  1993
                {
StephaneLenclud@158
  1994
                    Debug.Print("Layout has not changed.");
StephaneLenclud@158
  1995
                }
StephaneLenclud@158
  1996
            }
StephaneLenclud@158
  1997
            else
StephaneLenclud@158
  1998
            {
StephaneLenclud@158
  1999
                Debug.Print("WARNING: content and layout have not changed!");
StephaneLenclud@141
  2000
            }
sl@75
  2001
StephaneLenclud@141
  2002
            //When a client field is set we try switching to this client to present the new information to our user
StephaneLenclud@141
  2003
            SetCurrentClient(aSessionId);
sl@30
  2004
        }
sl@30
  2005
sl@30
  2006
        /// <summary>
sl@36
  2007
        ///
sl@30
  2008
        /// </summary>
sl@30
  2009
        /// <param name="aTexts"></param>
sl@75
  2010
        public void SetClientFieldsThreadSafe(string aSessionId, System.Collections.Generic.IList<DataField> aFields)
sl@30
  2011
        {
sl@33
  2012
            if (this.InvokeRequired)
sl@30
  2013
            {
sl@30
  2014
                //Not in the proper thread, invoke ourselves
sl@75
  2015
                SetFieldsDelegate d = new SetFieldsDelegate(SetClientFieldsThreadSafe);
StephaneLenclud@223
  2016
                this.Invoke(d, new object[] {aSessionId, aFields});
sl@30
  2017
            }
sl@30
  2018
            else
sl@30
  2019
            {
StephaneLenclud@175
  2020
                ClientData client = iClients[aSessionId];
StephaneLenclud@175
  2021
StephaneLenclud@175
  2022
                if (client.HasNewLayout)
sl@30
  2023
                {
StephaneLenclud@175
  2024
                    //TODO: Assert client.Count == 0
StephaneLenclud@175
  2025
                    //Our layout was just changed
StephaneLenclud@175
  2026
                    //Do some special handling to avoid re-creating our panel N times, once for each fields
StephaneLenclud@175
  2027
                    client.HasNewLayout = false;
StephaneLenclud@175
  2028
                    //Just set all our fields then
StephaneLenclud@175
  2029
                    client.Fields.AddRange(aFields);
StephaneLenclud@175
  2030
                    //Try switch to that client
StephaneLenclud@175
  2031
                    SetCurrentClient(aSessionId);
StephaneLenclud@175
  2032
StephaneLenclud@175
  2033
                    //If we are updating the current client update our panel
StephaneLenclud@175
  2034
                    if (aSessionId == iCurrentClientSessionId)
StephaneLenclud@175
  2035
                    {
StephaneLenclud@175
  2036
                        //Apply layout and set data fields.
StephaneLenclud@175
  2037
                        UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@175
  2038
                    }
StephaneLenclud@176
  2039
StephaneLenclud@176
  2040
                    UpdateClientTreeViewNode(client);
StephaneLenclud@175
  2041
                }
StephaneLenclud@175
  2042
                else
StephaneLenclud@175
  2043
                {
StephaneLenclud@175
  2044
                    //Put each our text fields in a label control
StephaneLenclud@175
  2045
                    foreach (DataField field in aFields)
StephaneLenclud@175
  2046
                    {
StephaneLenclud@175
  2047
                        SetClientField(aSessionId, field);
StephaneLenclud@175
  2048
                    }
sl@30
  2049
                }
sl@30
  2050
            }
sl@32
  2051
        }
sl@30
  2052
sl@67
  2053
        /// <summary>
sl@67
  2054
        ///
sl@67
  2055
        /// </summary>
sl@67
  2056
        /// <param name="aSessionId"></param>
sl@32
  2057
        /// <param name="aName"></param>
sl@32
  2058
        public void SetClientNameThreadSafe(string aSessionId, string aName)
sl@32
  2059
        {
sl@32
  2060
            if (this.InvokeRequired)
sl@32
  2061
            {
sl@32
  2062
                //Not in the proper thread, invoke ourselves
sl@32
  2063
                SetClientNameDelegate d = new SetClientNameDelegate(SetClientNameThreadSafe);
StephaneLenclud@223
  2064
                this.Invoke(d, new object[] {aSessionId, aName});
sl@32
  2065
            }
sl@32
  2066
            else
sl@32
  2067
            {
sl@32
  2068
                //We are in the proper thread
sl@33
  2069
                //Get our client
sl@33
  2070
                ClientData client = iClients[aSessionId];
sl@33
  2071
                if (client != null)
sl@32
  2072
                {
sl@33
  2073
                    //Set its name
sl@33
  2074
                    client.Name = aName;
sl@33
  2075
                    //Update our tree-view
sl@33
  2076
                    UpdateClientTreeViewNode(client);
sl@33
  2077
                }
sl@33
  2078
            }
sl@33
  2079
        }
sl@33
  2080
StephaneLenclud@184
  2081
        ///
StephaneLenclud@184
  2082
        public void SetClientPriorityThreadSafe(string aSessionId, uint aPriority)
StephaneLenclud@184
  2083
        {
StephaneLenclud@184
  2084
            if (this.InvokeRequired)
StephaneLenclud@184
  2085
            {
StephaneLenclud@184
  2086
                //Not in the proper thread, invoke ourselves
StephaneLenclud@184
  2087
                SetClientPriorityDelegate d = new SetClientPriorityDelegate(SetClientPriorityThreadSafe);
StephaneLenclud@223
  2088
                this.Invoke(d, new object[] {aSessionId, aPriority});
StephaneLenclud@184
  2089
            }
StephaneLenclud@184
  2090
            else
StephaneLenclud@184
  2091
            {
StephaneLenclud@184
  2092
                //We are in the proper thread
StephaneLenclud@184
  2093
                //Get our client
StephaneLenclud@184
  2094
                ClientData client = iClients[aSessionId];
StephaneLenclud@184
  2095
                if (client != null)
StephaneLenclud@184
  2096
                {
StephaneLenclud@184
  2097
                    //Set its name
StephaneLenclud@184
  2098
                    client.Priority = aPriority;
StephaneLenclud@184
  2099
                    //Update our tree-view
StephaneLenclud@184
  2100
                    UpdateClientTreeViewNode(client);
Stephane@186
  2101
                    //Change our current client as per new priority
Stephane@186
  2102
                    ClientData newCurrentClient = FindHighestPriorityClient();
StephaneLenclud@223
  2103
                    if (newCurrentClient != null)
Stephane@186
  2104
                    {
Stephane@186
  2105
                        SetCurrentClient(newCurrentClient.SessionId);
Stephane@186
  2106
                    }
StephaneLenclud@184
  2107
                }
StephaneLenclud@184
  2108
            }
StephaneLenclud@184
  2109
        }
StephaneLenclud@184
  2110
sl@33
  2111
        /// <summary>
StephaneLenclud@183
  2112
        /// 
StephaneLenclud@183
  2113
        /// </summary>
StephaneLenclud@183
  2114
        /// <param name="value"></param>
StephaneLenclud@183
  2115
        /// <param name="maxChars"></param>
StephaneLenclud@183
  2116
        /// <returns></returns>
StephaneLenclud@183
  2117
        public static string Truncate(string value, int maxChars)
StephaneLenclud@183
  2118
        {
StephaneLenclud@223
  2119
            return value.Length <= maxChars ? value : value.Substring(0, maxChars - 3) + "...";
StephaneLenclud@183
  2120
        }
StephaneLenclud@183
  2121
StephaneLenclud@183
  2122
        /// <summary>
StephaneLenclud@180
  2123
        /// Update our recording notification.
StephaneLenclud@180
  2124
        /// </summary>
StephaneLenclud@180
  2125
        private void UpdateRecordingNotification()
StephaneLenclud@180
  2126
        {
StephaneLenclud@180
  2127
            //Go through each 
StephaneLenclud@180
  2128
            bool activeRecording = false;
StephaneLenclud@223
  2129
            string text = "";
StephaneLenclud@223
  2130
            RecordingField recField = new RecordingField();
StephaneLenclud@180
  2131
            foreach (var client in iClients)
StephaneLenclud@180
  2132
            {
StephaneLenclud@223
  2133
                RecordingField rec = (RecordingField) client.Value.FindSameFieldAs(recField);
StephaneLenclud@223
  2134
                if (rec != null && rec.IsActive)
StephaneLenclud@180
  2135
                {
StephaneLenclud@180
  2136
                    activeRecording = true;
StephaneLenclud@183
  2137
                    //Don't break cause we are collecting the names/texts.
StephaneLenclud@183
  2138
                    if (!String.IsNullOrEmpty(rec.Text))
StephaneLenclud@183
  2139
                    {
StephaneLenclud@183
  2140
                        text += (rec.Text + "\n");
StephaneLenclud@183
  2141
                    }
StephaneLenclud@183
  2142
                    else
StephaneLenclud@183
  2143
                    {
StephaneLenclud@183
  2144
                        //Not text for that recording, use client name instead
StephaneLenclud@183
  2145
                        text += client.Value.Name + " recording\n";
StephaneLenclud@183
  2146
                    }
StephaneLenclud@223
  2147
StephaneLenclud@180
  2148
                }
StephaneLenclud@180
  2149
            }
StephaneLenclud@180
  2150
StephaneLenclud@183
  2151
            //Update our text no matter what, can't have more than 63 characters otherwise it throws an exception.
StephaneLenclud@223
  2152
            iRecordingNotification.Text = Truncate(text, 63);
StephaneLenclud@183
  2153
StephaneLenclud@180
  2154
            //Change visibility of notification if needed
StephaneLenclud@180
  2155
            if (iRecordingNotification.Visible != activeRecording)
StephaneLenclud@223
  2156
            {
StephaneLenclud@180
  2157
                iRecordingNotification.Visible = activeRecording;
Stephane@182
  2158
                //Assuming the notification icon is in sync with our display icon
Stephane@182
  2159
                //Take care of our REC icon
StephaneLenclud@183
  2160
                if (iDisplay.IsOpen())
StephaneLenclud@183
  2161
                {
StephaneLenclud@183
  2162
                    iDisplay.SetIconOnOff(MiniDisplay.IconType.Recording, activeRecording);
StephaneLenclud@223
  2163
                }
StephaneLenclud@180
  2164
            }
StephaneLenclud@180
  2165
        }
StephaneLenclud@180
  2166
StephaneLenclud@180
  2167
        /// <summary>
sl@36
  2168
        ///
sl@33
  2169
        /// </summary>
sl@33
  2170
        /// <param name="aClient"></param>
sl@33
  2171
        private void UpdateClientTreeViewNode(ClientData aClient)
sl@33
  2172
        {
StephaneLenclud@148
  2173
            Debug.Print("UpdateClientTreeViewNode");
StephaneLenclud@148
  2174
sl@33
  2175
            if (aClient == null)
sl@33
  2176
            {
sl@33
  2177
                return;
sl@33
  2178
            }
sl@33
  2179
StephaneLenclud@180
  2180
            //Hook in record icon update too
StephaneLenclud@180
  2181
            UpdateRecordingNotification();
StephaneLenclud@180
  2182
sl@33
  2183
            TreeNode node = null;
sl@33
  2184
            //Check that our client node already exists
sl@33
  2185
            //Get our client root node using its key which is our session ID
StephaneLenclud@188
  2186
            TreeNode[] nodes = iTreeViewClients.Nodes.Find(aClient.SessionId, false);
StephaneLenclud@223
  2187
            if (nodes.Count() > 0)
sl@33
  2188
            {
sl@33
  2189
                //We already have a node for that client
sl@33
  2190
                node = nodes[0];
sl@33
  2191
                //Clear children as we are going to recreate them below
sl@33
  2192
                node.Nodes.Clear();
sl@33
  2193
            }
sl@33
  2194
            else
sl@33
  2195
            {
sl@33
  2196
                //Client node does not exists create a new one
StephaneLenclud@188
  2197
                iTreeViewClients.Nodes.Add(aClient.SessionId, aClient.SessionId);
StephaneLenclud@188
  2198
                node = iTreeViewClients.Nodes.Find(aClient.SessionId, false)[0];
sl@33
  2199
            }
sl@33
  2200
sl@33
  2201
            if (node != null)
sl@33
  2202
            {
sl@33
  2203
                //Change its name
StephaneLenclud@184
  2204
                if (!String.IsNullOrEmpty(aClient.Name))
sl@33
  2205
                {
StephaneLenclud@184
  2206
                    //We have a name, use it as text for our root node
sl@33
  2207
                    node.Text = aClient.Name;
sl@32
  2208
                    //Add a child with SessionId
sl@33
  2209
                    node.Nodes.Add(new TreeNode(aClient.SessionId));
sl@33
  2210
                }
sl@33
  2211
                else
sl@33
  2212
                {
sl@33
  2213
                    //No name, use session ID instead
sl@33
  2214
                    node.Text = aClient.SessionId;
sl@33
  2215
                }
sl@36
  2216
StephaneLenclud@184
  2217
                //Display client priority
StephaneLenclud@184
  2218
                node.Nodes.Add(new TreeNode("Priority: " + aClient.Priority));
StephaneLenclud@184
  2219
sl@67
  2220
                if (aClient.Fields.Count > 0)
sl@33
  2221
                {
sl@33
  2222
                    //Create root node for our texts
sl@70
  2223
                    TreeNode textsRoot = new TreeNode("Fields");
sl@33
  2224
                    node.Nodes.Add(textsRoot);
sl@33
  2225
                    //For each text add a new entry
sl@67
  2226
                    foreach (DataField field in aClient.Fields)
sl@33
  2227
                    {
StephaneLenclud@172
  2228
                        if (field.IsTextField)
sl@67
  2229
                        {
StephaneLenclud@223
  2230
                            TextField textField = (TextField) field;
sl@70
  2231
                            textsRoot.Nodes.Add(new TreeNode("[Text]" + textField.Text));
sl@67
  2232
                        }
StephaneLenclud@172
  2233
                        else if (field.IsBitmapField)
sl@67
  2234
                        {
sl@72
  2235
                            textsRoot.Nodes.Add(new TreeNode("[Bitmap]"));
sl@70
  2236
                        }
StephaneLenclud@172
  2237
                        else if (field.IsRecordingField)
StephaneLenclud@172
  2238
                        {
StephaneLenclud@223
  2239
                            RecordingField recordingField = (RecordingField) field;
StephaneLenclud@177
  2240
                            textsRoot.Nodes.Add(new TreeNode("[Recording]" + recordingField.IsActive));
StephaneLenclud@172
  2241
                        }
sl@33
  2242
                    }
sl@32
  2243
                }
sl@34
  2244
sl@34
  2245
                node.ExpandAll();
sl@32
  2246
            }
sl@30
  2247
        }
sl@17
  2248
sl@60
  2249
        /// <summary>
sl@60
  2250
        /// Update our table layout row styles to make sure each rows have similar height
sl@60
  2251
        /// </summary>
sl@60
  2252
        private void UpdateTableLayoutRowStyles()
sl@60
  2253
        {
StephaneLenclud@175
  2254
            foreach (RowStyle rowStyle in iTableLayoutPanel.RowStyles)
sl@60
  2255
            {
sl@60
  2256
                rowStyle.SizeType = SizeType.Percent;
StephaneLenclud@223
  2257
                rowStyle.Height = 100/iTableLayoutPanel.RowCount;
sl@60
  2258
            }
sl@60
  2259
        }
sl@60
  2260
sl@63
  2261
        /// <summary>
sl@63
  2262
        /// Update our display table layout.
StephaneLenclud@175
  2263
        /// Will instanciated every field control as defined by our client.
StephaneLenclud@175
  2264
        /// Fields must be specified by rows from the left.
sl@63
  2265
        /// </summary>
sl@63
  2266
        /// <param name="aLayout"></param>
sl@65
  2267
        private void UpdateTableLayoutPanel(ClientData aClient)
sl@63
  2268
        {
StephaneLenclud@175
  2269
            Debug.Print("UpdateTableLayoutPanel");
StephaneLenclud@148
  2270
StephaneLenclud@223
  2271
            if (aClient == null)
StephaneLenclud@223
  2272
            {
StephaneLenclud@223
  2273
                //Just drop it
StephaneLenclud@223
  2274
                return;
StephaneLenclud@223
  2275
            }
StephaneLenclud@106
  2276
StephaneLenclud@106
  2277
sl@65
  2278
            TableLayout layout = aClient.Layout;
sl@70
  2279
StephaneLenclud@141
  2280
            //First clean our current panel
StephaneLenclud@175
  2281
            iTableLayoutPanel.Controls.Clear();
StephaneLenclud@175
  2282
            iTableLayoutPanel.RowStyles.Clear();
StephaneLenclud@175
  2283
            iTableLayoutPanel.ColumnStyles.Clear();
StephaneLenclud@175
  2284
            iTableLayoutPanel.RowCount = 0;
StephaneLenclud@175
  2285
            iTableLayoutPanel.ColumnCount = 0;
sl@63
  2286
StephaneLenclud@175
  2287
            //Then recreate our rows...
StephaneLenclud@175
  2288
            while (iTableLayoutPanel.RowCount < layout.Rows.Count)
sl@63
  2289
            {
StephaneLenclud@175
  2290
                iTableLayoutPanel.RowCount++;
sl@63
  2291
            }
sl@63
  2292
StephaneLenclud@175
  2293
            // ...and columns 
StephaneLenclud@175
  2294
            while (iTableLayoutPanel.ColumnCount < layout.Columns.Count)
sl@63
  2295
            {
StephaneLenclud@175
  2296
                iTableLayoutPanel.ColumnCount++;
sl@63
  2297
            }
sl@63
  2298
StephaneLenclud@175
  2299
            //For each column
StephaneLenclud@175
  2300
            for (int i = 0; i < iTableLayoutPanel.ColumnCount; i++)
sl@63
  2301
            {
sl@63
  2302
                //Create our column styles
StephaneLenclud@175
  2303
                this.iTableLayoutPanel.ColumnStyles.Add(layout.Columns[i]);
sl@63
  2304
StephaneLenclud@175
  2305
                //For each rows
StephaneLenclud@175
  2306
                for (int j = 0; j < iTableLayoutPanel.RowCount; j++)
sl@63
  2307
                {
sl@63
  2308
                    if (i == 0)
sl@63
  2309
                    {
sl@63
  2310
                        //Create our row styles
StephaneLenclud@175
  2311
                        this.iTableLayoutPanel.RowStyles.Add(layout.Rows[j]);
sl@63
  2312
                    }
StephaneLenclud@176
  2313
                    else
sl@70
  2314
                    {
sl@70
  2315
                        continue;
sl@70
  2316
                    }
sl@63
  2317
                }
sl@63
  2318
            }
sl@63
  2319
StephaneLenclud@176
  2320
            //For each field
StephaneLenclud@176
  2321
            foreach (DataField field in aClient.Fields)
sl@70
  2322
            {
StephaneLenclud@176
  2323
                if (!field.IsTableField)
StephaneLenclud@176
  2324
                {
StephaneLenclud@176
  2325
                    //That field is not taking part in our table layout skip it
StephaneLenclud@176
  2326
                    continue;
StephaneLenclud@176
  2327
                }
StephaneLenclud@176
  2328
StephaneLenclud@223
  2329
                TableField tableField = (TableField) field;
StephaneLenclud@176
  2330
StephaneLenclud@176
  2331
                //Create a control corresponding to the field specified for that cell
StephaneLenclud@176
  2332
                Control control = CreateControlForDataField(tableField);
StephaneLenclud@176
  2333
StephaneLenclud@176
  2334
                //Add newly created control to our table layout at the specified row and column
StephaneLenclud@176
  2335
                iTableLayoutPanel.Controls.Add(control, tableField.Column, tableField.Row);
StephaneLenclud@176
  2336
                //Make sure we specify column and row span for that new control
StephaneLenclud@176
  2337
                iTableLayoutPanel.SetColumnSpan(control, tableField.ColumnSpan);
StephaneLenclud@176
  2338
                iTableLayoutPanel.SetRowSpan(control, tableField.RowSpan);
sl@70
  2339
            }
sl@70
  2340
StephaneLenclud@176
  2341
sl@63
  2342
            CheckFontHeight();
sl@63
  2343
        }
sl@63
  2344
sl@68
  2345
        /// <summary>
sl@70
  2346
        /// Check our type of data field and create corresponding control
sl@68
  2347
        /// </summary>
sl@68
  2348
        /// <param name="aField"></param>
sl@69
  2349
        private Control CreateControlForDataField(DataField aField)
sl@68
  2350
        {
StephaneLenclud@223
  2351
            Control control = null;
StephaneLenclud@172
  2352
            if (aField.IsTextField)
sl@68
  2353
            {
sl@68
  2354
                MarqueeLabel label = new SharpDisplayManager.MarqueeLabel();
sl@68
  2355
                label.AutoEllipsis = true;
sl@68
  2356
                label.AutoSize = true;
sl@68
  2357
                label.BackColor = System.Drawing.Color.Transparent;
sl@68
  2358
                label.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2359
                label.Location = new System.Drawing.Point(1, 1);
sl@68
  2360
                label.Margin = new System.Windows.Forms.Padding(0);
StephaneLenclud@176
  2361
                label.Name = "marqueeLabel" + aField;
sl@68
  2362
                label.OwnTimer = false;
StephaneLenclud@106
  2363
                label.PixelsPerSecond = cds.ScrollingSpeedInPixelsPerSecond;
sl@100
  2364
                label.Separator = cds.Separator;
sl@100
  2365
                label.MinFontSize = cds.MinFontSize;
sl@100
  2366
                label.ScaleToFit = cds.ScaleToFit;
sl@68
  2367
                //control.Size = new System.Drawing.Size(254, 30);
sl@68
  2368
                //control.TabIndex = 2;
sl@68
  2369
                label.Font = cds.Font;
sl@68
  2370
StephaneLenclud@223
  2371
                TextField field = (TextField) aField;
StephaneLenclud@172
  2372
                label.TextAlign = field.Alignment;
sl@68
  2373
                label.UseCompatibleTextRendering = true;
StephaneLenclud@172
  2374
                label.Text = field.Text;
sl@68
  2375
                //
sl@68
  2376
                control = label;
sl@68
  2377
            }
StephaneLenclud@172
  2378
            else if (aField.IsBitmapField)
sl@68
  2379
            {
sl@68
  2380
                //Create picture box
sl@68
  2381
                PictureBox picture = new PictureBox();
sl@68
  2382
                picture.AutoSize = true;
sl@68
  2383
                picture.BackColor = System.Drawing.Color.Transparent;
sl@68
  2384
                picture.Dock = System.Windows.Forms.DockStyle.Fill;
sl@68
  2385
                picture.Location = new System.Drawing.Point(1, 1);
sl@68
  2386
                picture.Margin = new System.Windows.Forms.Padding(0);
sl@68
  2387
                picture.Name = "pictureBox" + aField;
sl@68
  2388
                //Set our image
StephaneLenclud@223
  2389
                BitmapField field = (BitmapField) aField;
StephaneLenclud@172
  2390
                picture.Image = field.Bitmap;
sl@68
  2391
                //
sl@68
  2392
                control = picture;
sl@68
  2393
            }
StephaneLenclud@172
  2394
            //TODO: Handle recording field?
sl@68
  2395
sl@69
  2396
            return control;
sl@68
  2397
        }
sl@68
  2398
StephaneLenclud@223
  2399
        /// <summary>
StephaneLenclud@223
  2400
        /// Called when the user selected a new display type.
StephaneLenclud@223
  2401
        /// </summary>
StephaneLenclud@223
  2402
        /// <param name="sender"></param>
StephaneLenclud@223
  2403
        /// <param name="e"></param>
sl@46
  2404
        private void comboBoxDisplayType_SelectedIndexChanged(object sender, EventArgs e)
sl@46
  2405
        {
StephaneLenclud@223
  2406
            //Store the selected display type in our settings
sl@48
  2407
            Properties.Settings.Default.CurrentDisplayIndex = comboBoxDisplayType.SelectedIndex;
sl@48
  2408
            cds.DisplayType = comboBoxDisplayType.SelectedIndex;
sl@46
  2409
            Properties.Settings.Default.Save();
StephaneLenclud@103
  2410
StephaneLenclud@223
  2411
            //Try re-opening the display connection if we were already connected.
StephaneLenclud@223
  2412
            //Otherwise just update our status to reflect display type change.
sl@51
  2413
            if (iDisplay.IsOpen())
sl@51
  2414
            {
sl@51
  2415
                OpenDisplayConnection();
sl@51
  2416
            }
sl@51
  2417
            else
sl@51
  2418
            {
sl@51
  2419
                UpdateStatus();
sl@51
  2420
            }
sl@46
  2421
        }
sl@46
  2422
sl@47
  2423
        private void maskedTextBoxTimerInterval_TextChanged(object sender, EventArgs e)
sl@47
  2424
        {
sl@47
  2425
            if (maskedTextBoxTimerInterval.Text != "")
sl@47
  2426
            {
sl@51
  2427
                int interval = Convert.ToInt32(maskedTextBoxTimerInterval.Text);
sl@51
  2428
sl@51
  2429
                if (interval > 0)
sl@51
  2430
                {
StephaneLenclud@270
  2431
                    iTimerDisplay.Interval = interval;
StephaneLenclud@270
  2432
                    cds.TimerInterval = iTimerDisplay.Interval;
sl@51
  2433
                    Properties.Settings.Default.Save();
sl@51
  2434
                }
sl@47
  2435
            }
sl@47
  2436
        }
sl@47
  2437
sl@100
  2438
        private void maskedTextBoxMinFontSize_TextChanged(object sender, EventArgs e)
sl@100
  2439
        {
sl@100
  2440
            if (maskedTextBoxMinFontSize.Text != "")
sl@100
  2441
            {
sl@100
  2442
                int minFontSize = Convert.ToInt32(maskedTextBoxMinFontSize.Text);
sl@100
  2443
sl@100
  2444
                if (minFontSize > 0)
sl@100
  2445
                {
sl@100
  2446
                    cds.MinFontSize = minFontSize;
sl@100
  2447
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2448
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2449
                    UpdateTableLayoutPanel(iCurrentClientData);
sl@100
  2450
                }
sl@100
  2451
            }
sl@100
  2452
        }
sl@100
  2453
StephaneLenclud@106
  2454
StephaneLenclud@223
  2455
        private void maskedTextBoxScrollingSpeed_TextChanged(object sender, EventArgs e)
StephaneLenclud@223
  2456
        {
StephaneLenclud@223
  2457
            if (maskedTextBoxScrollingSpeed.Text != "")
StephaneLenclud@223
  2458
            {
StephaneLenclud@223
  2459
                int scrollingSpeed = Convert.ToInt32(maskedTextBoxScrollingSpeed.Text);
StephaneLenclud@223
  2460
StephaneLenclud@223
  2461
                if (scrollingSpeed > 0)
StephaneLenclud@223
  2462
                {
StephaneLenclud@223
  2463
                    cds.ScrollingSpeedInPixelsPerSecond = scrollingSpeed;
StephaneLenclud@223
  2464
                    Properties.Settings.Default.Save();
StephaneLenclud@223
  2465
                    //We need to recreate our layout for that change to take effect
StephaneLenclud@223
  2466
                    UpdateTableLayoutPanel(iCurrentClientData);
StephaneLenclud@223
  2467
                }
StephaneLenclud@223
  2468
            }
StephaneLenclud@223
  2469
        }
StephaneLenclud@106
  2470
sl@100
  2471
        private void textBoxScrollLoopSeparator_TextChanged(object sender, EventArgs e)
sl@100
  2472
        {
sl@100
  2473
            cds.Separator = textBoxScrollLoopSeparator.Text;
sl@100
  2474
            Properties.Settings.Default.Save();
StephaneLenclud@110
  2475
StephaneLenclud@223
  2476
            //Update our text fields
StephaneLenclud@223
  2477
            foreach (MarqueeLabel ctrl in iTableLayoutPanel.Controls)
StephaneLenclud@223
  2478
            {
StephaneLenclud@223
  2479
                ctrl.Separator = cds.Separator;
StephaneLenclud@223
  2480
            }
StephaneLenclud@110
  2481
sl@100
  2482
        }
sl@100
  2483
sl@52
  2484
        private void buttonPowerOn_Click(object sender, EventArgs e)
sl@52
  2485
        {
sl@52
  2486
            iDisplay.PowerOn();
sl@52
  2487
        }
sl@52
  2488
sl@52
  2489
        private void buttonPowerOff_Click(object sender, EventArgs e)
sl@52
  2490
        {
sl@52
  2491
            iDisplay.PowerOff();
sl@52
  2492
        }
sl@52
  2493
sl@53
  2494
        private void buttonShowClock_Click(object sender, EventArgs e)
sl@53
  2495
        {
StephaneLenclud@223
  2496
            ShowClock();
sl@53
  2497
        }
sl@53
  2498
sl@53
  2499
        private void buttonHideClock_Click(object sender, EventArgs e)
sl@53
  2500
        {
StephaneLenclud@223
  2501
            HideClock();
sl@53
  2502
        }
sl@88
  2503
sl@88
  2504
        private void buttonUpdate_Click(object sender, EventArgs e)
sl@88
  2505
        {
sl@88
  2506
            InstallUpdateSyncWithInfo();
sl@88
  2507
        }
sl@88
  2508
StephaneLenclud@223
  2509
        /// <summary>
StephaneLenclud@223
  2510
        /// 
StephaneLenclud@223
  2511
        /// </summary>
StephaneLenclud@223
  2512
        void ShowClock()
StephaneLenclud@223
  2513
        {
StephaneLenclud@223
  2514
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2515
            {
StephaneLenclud@223
  2516
                return;
StephaneLenclud@223
  2517
            }
StephaneLenclud@223
  2518
StephaneLenclud@223
  2519
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2520
            iSkipFrameRendering = true;
StephaneLenclud@223
  2521
            //Clear our screen 
StephaneLenclud@223
  2522
            iDisplay.Clear();
StephaneLenclud@223
  2523
            iDisplay.SwapBuffers();
StephaneLenclud@223
  2524
            //Then show our clock
StephaneLenclud@223
  2525
            iDisplay.ShowClock();
StephaneLenclud@223
  2526
        }
StephaneLenclud@223
  2527
StephaneLenclud@223
  2528
        /// <summary>
StephaneLenclud@223
  2529
        /// 
StephaneLenclud@223
  2530
        /// </summary>
StephaneLenclud@223
  2531
        void HideClock()
StephaneLenclud@223
  2532
        {
StephaneLenclud@223
  2533
            if (!iDisplay.IsOpen())
StephaneLenclud@223
  2534
            {
StephaneLenclud@223
  2535
                return;
StephaneLenclud@223
  2536
            }
StephaneLenclud@223
  2537
StephaneLenclud@223
  2538
            //Devices like MDM166AA don't support windowing and frame rendering must be stopped while showing our clock
StephaneLenclud@223
  2539
            iSkipFrameRendering = false;
StephaneLenclud@223
  2540
            iDisplay.HideClock();
StephaneLenclud@223
  2541
        }
sl@88
  2542
sl@88
  2543
        private void InstallUpdateSyncWithInfo()
sl@88
  2544
        {
sl@88
  2545
            UpdateCheckInfo info = null;
sl@88
  2546
sl@88
  2547
            if (ApplicationDeployment.IsNetworkDeployed)
sl@88
  2548
            {
sl@88
  2549
                ApplicationDeployment ad = ApplicationDeployment.CurrentDeployment;
sl@88
  2550
sl@88
  2551
                try