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