Adding events for display closed/opened.
authorStephaneLenclud
Wed, 04 Feb 2015 17:44:25 +0100
changeset 104189aac7dd3d6
parent 103 14f6c8d21ec1
child 105 4196b0ca97d9
Adding events for display closed/opened.
Display types now loaded dynamically from our MiniDisplay library.
Adding editor config.
Tested against Futaba GP1212A02.
.editorconfig
Server/Display.cs
Server/MainForm.Designer.cs
Server/MainForm.cs
Server/MiniDisplay.dll
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/.editorconfig	Wed Feb 04 17:44:25 2015 +0100
     1.3 @@ -0,0 +1,12 @@
     1.4 +; Top-most EditorConfig file
     1.5 +root = true
     1.6 +
     1.7 +; Unix-style newlines
     1.8 +[*]
     1.9 +end_of_line = crlf
    1.10 +
    1.11 +; 4-column tab indentation
    1.12 +[*.cs]
    1.13 +indent_style = tab
    1.14 +indent_size = 4
    1.15 +
     2.1 --- a/Server/Display.cs	Wed Feb 04 15:48:08 2015 +0100
     2.2 +++ b/Server/Display.cs	Wed Feb 04 17:44:25 2015 +0100
     2.3 @@ -10,11 +10,33 @@
     2.4  namespace SharpDisplayManager
     2.5  {
     2.6  
     2.7 +
     2.8      /// <summary>
     2.9      /// Provide access to our display hardware through MiniDisplay API.
    2.10      /// </summary>
    2.11      public class Display
    2.12      {
    2.13 +		public delegate void OnOpenedHandler(Display aDisplay);
    2.14 +		public event OnOpenedHandler OnOpened;
    2.15 +
    2.16 +		public delegate void OnClosedHandler(Display aDisplay);
    2.17 +		public event OnClosedHandler OnClosed;
    2.18 +
    2.19 +		//Our display device handle
    2.20 +		IntPtr iDevice;
    2.21 +
    2.22 +		//static functions
    2.23 +		public static int TypeCount()
    2.24 +		{
    2.25 +			return MiniDisplayTypeCount();
    2.26 +		}
    2.27 +
    2.28 +		public static string TypeName(TMiniDisplayType aType)
    2.29 +		{
    2.30 +			IntPtr ptr = MiniDisplayTypeName(aType);
    2.31 +			string str = Marshal.PtrToStringUni(ptr);
    2.32 +			return str;
    2.33 +		}
    2.34  
    2.35          //Constructor
    2.36          public Display()
    2.37 @@ -25,17 +47,36 @@
    2.38          //
    2.39          public bool Open(TMiniDisplayType aType)
    2.40          {
    2.41 -            if (iDevice == IntPtr.Zero)
    2.42 -            {
    2.43 -                iDevice = MiniDisplayOpen(aType);
    2.44 -            }
    2.45 -            return iDevice != IntPtr.Zero;
    2.46 +			if (IsOpen())
    2.47 +			{
    2.48 +				//Already open return an error
    2.49 +				return false;
    2.50 +			}
    2.51 +
    2.52 +            iDevice = MiniDisplayOpen(aType);
    2.53 +
    2.54 +            bool success = iDevice != IntPtr.Zero;
    2.55 +			if (success)
    2.56 +			{
    2.57 +				//Broadcast opened event
    2.58 +				OnOpened(this);
    2.59 +			}
    2.60 +
    2.61 +			return success;
    2.62          }
    2.63  
    2.64          public void Close()
    2.65          {
    2.66 +			if (!IsOpen())
    2.67 +			{
    2.68 +				//Pointless
    2.69 +				return;
    2.70 +			}
    2.71 +
    2.72              MiniDisplayClose(iDevice);
    2.73              iDevice = IntPtr.Zero;
    2.74 +			//Broadcast closed event
    2.75 +			OnClosed(this);
    2.76          }
    2.77  
    2.78          public bool IsOpen()
    2.79 @@ -192,10 +233,6 @@
    2.80              return str;
    2.81          }
    2.82  
    2.83 -        //Our display device handle
    2.84 -        IntPtr iDevice;
    2.85 -
    2.86 -
    2.87          //[Serializable]
    2.88          public enum TMiniDisplayType
    2.89          {
    2.90 @@ -221,6 +258,12 @@
    2.91          [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    2.92          public static extern void MiniDisplayClose(IntPtr aDevice);
    2.93  
    2.94 +		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    2.95 +		public static extern int MiniDisplayTypeCount();
    2.96 +
    2.97 +		[DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
    2.98 +		public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType);
    2.99 +
   2.100          [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
   2.101          public static extern void MiniDisplayClear(IntPtr aDevice);
   2.102  
     3.1 --- a/Server/MainForm.Designer.cs	Wed Feb 04 15:48:08 2015 +0100
     3.2 +++ b/Server/MainForm.Designer.cs	Wed Feb 04 17:44:25 2015 +0100
     3.3 @@ -357,10 +357,6 @@
     3.4              // 
     3.5              this.comboBoxDisplayType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
     3.6              this.comboBoxDisplayType.FormattingEnabled = true;
     3.7 -            this.comboBoxDisplayType.Items.AddRange(new object[] {
     3.8 -            "Auto Detect",
     3.9 -            "Futaba GP1212A01A",
    3.10 -            "Futaba GP1212A02A"});
    3.11              this.comboBoxDisplayType.Location = new System.Drawing.Point(187, 9);
    3.12              this.comboBoxDisplayType.Name = "comboBoxDisplayType";
    3.13              this.comboBoxDisplayType.Size = new System.Drawing.Size(181, 21);
     4.1 --- a/Server/MainForm.cs	Wed Feb 04 15:48:08 2015 +0100
     4.2 +++ b/Server/MainForm.cs	Wed Feb 04 17:44:25 2015 +0100
     4.3 @@ -72,17 +72,29 @@
     4.4              iCurrentClientSessionId = "";
     4.5              iCurrentClientData = null;
     4.6              LastTickTime = DateTime.Now;
     4.7 +			//Instantiate our display and register for events notifications
     4.8              iDisplay = new Display();
     4.9 -            iClients = new Dictionary<string, ClientData>();
    4.10 +			iDisplay.OnOpened += OnDisplayOpened;
    4.11 +			iDisplay.OnClosed += OnDisplayClosed;
    4.12 +			//
    4.13 +			iClients = new Dictionary<string, ClientData>();
    4.14  			iStartupManager = new StartupManager();
    4.15  			iNotifyIcon = new NotifyIconAdv();
    4.16  
    4.17 +			//Have our designer initialize its controls
    4.18              InitializeComponent();
    4.19 +
    4.20 +			//Populate device types
    4.21 +			PopulateDeviceTypes();
    4.22 +
    4.23 +			//Initial status update 
    4.24              UpdateStatus();
    4.25 +
    4.26              //We have a bug when drawing minimized and reusing our bitmap
    4.27              iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb);
    4.28              iCreateBitmap = false;
    4.29  
    4.30 +			//Minimize our window if desired
    4.31  			if (Properties.Settings.Default.StartMinimized)
    4.32  			{
    4.33  				WindowState = FormWindowState.Minimized;
    4.34 @@ -91,29 +103,59 @@
    4.35          }
    4.36  
    4.37  		/// <summary>
    4.38 +		/// Called when our display is opened.
    4.39 +		/// </summary>
    4.40 +		/// <param name="aDisplay"></param>
    4.41 +		private void OnDisplayOpened(Display aDisplay)
    4.42 +		{
    4.43 +			//Our display was just opened, update our UI
    4.44 +			UpdateStatus();
    4.45 +			//Initiate asynchronous request
    4.46 +			iDisplay.RequestFirmwareRevision();
    4.47 +		}
    4.48 +
    4.49 +		/// <summary>
    4.50 +		/// Called when our display is closed.
    4.51 +		/// </summary>
    4.52 +		/// <param name="aDisplay"></param>
    4.53 +		private void OnDisplayClosed(Display aDisplay)
    4.54 +		{
    4.55 +			//Our display was just closed, update our UI consequently
    4.56 +			UpdateStatus();
    4.57 +		}
    4.58 +
    4.59 +		/// <summary>
    4.60 +		/// 
    4.61 +		/// </summary>
    4.62 +		private void PopulateDeviceTypes()
    4.63 +		{
    4.64 +			int count = Display.TypeCount();
    4.65 +
    4.66 +			for (int i=0; i<count; i++)
    4.67 +			{
    4.68 +				comboBoxDisplayType.Items.Add(Display.TypeName((Display.TMiniDisplayType)i));
    4.69 +			}			
    4.70 +		}
    4.71 +
    4.72 +		/// <summary>
    4.73  		///
    4.74  		/// </summary>
    4.75  		/// <param name="sender"></param>
    4.76  		/// <param name="e"></param>
    4.77          private void MainForm_Load(object sender, EventArgs e)
    4.78          {
    4.79 +			//Check if we are running a Click Once deployed application
    4.80  			if (ApplicationDeployment.IsNetworkDeployed)
    4.81  			{
    4.82 +				//This is a proper Click Once installation, fetch and show our version number
    4.83  				this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion;
    4.84  			}
    4.85  			else
    4.86  			{
    4.87 +				//Not a proper Click Once installation, assuming development build then
    4.88  				this.Text += " - development";
    4.89  			}
    4.90  
    4.91 -            StartServer();
    4.92 -
    4.93 -			//Open display connection on start-up if needed
    4.94 -            if (Properties.Settings.Default.DisplayConnectOnStartup)
    4.95 -            {
    4.96 -                OpenDisplayConnection();
    4.97 -            }
    4.98 -
    4.99  			//Setup notification icon
   4.100  			SetupTrayIcon();
   4.101  
   4.102 @@ -127,6 +169,15 @@
   4.103  			//When not debugging we want the screen to be empty until a client takes over
   4.104  			ClearLayout();
   4.105  #endif
   4.106 +
   4.107 +			//Open display connection on start-up if needed
   4.108 +			if (Properties.Settings.Default.DisplayConnectOnStartup)
   4.109 +			{
   4.110 +				OpenDisplayConnection();
   4.111 +			}
   4.112 +
   4.113 +			//Start our server so that we can get client requests
   4.114 +			StartServer();
   4.115          }
   4.116  
   4.117  		/// <summary>
   4.118 @@ -506,22 +557,17 @@
   4.119          {
   4.120              CloseDisplayConnection();
   4.121  
   4.122 -            if (iDisplay.Open((Display.TMiniDisplayType)cds.DisplayType))
   4.123 -            {
   4.124 -                UpdateStatus();
   4.125 -                iDisplay.RequestFirmwareRevision();
   4.126 -            }
   4.127 -            else
   4.128 -            {
   4.129 -                UpdateStatus();
   4.130 -                toolStripStatusLabelConnect.Text = "Connection error";
   4.131 +            if (!iDisplay.Open((Display.TMiniDisplayType)cds.DisplayType))
   4.132 +            {   
   4.133 +				UpdateStatus();               
   4.134 +				toolStripStatusLabelConnect.Text = "Connection error";
   4.135              }
   4.136          }
   4.137  
   4.138          private void CloseDisplayConnection()
   4.139          {
   4.140 +			//Status will be updated upon receiving the closed event
   4.141              iDisplay.Close();
   4.142 -            UpdateStatus();
   4.143          }
   4.144  
   4.145          private void buttonOpen_Click(object sender, EventArgs e)
     5.1 Binary file Server/MiniDisplay.dll has changed