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