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