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