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