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