# HG changeset patch # User StephaneLenclud # Date 1423068265 -3600 # Node ID 189aac7dd3d61dfa87ba6b68ba0305c2639dfa70 # Parent 14f6c8d21ec1782577b16429b6afd67a765c46a9 Adding events for display closed/opened. Display types now loaded dynamically from our MiniDisplay library. Adding editor config. Tested against Futaba GP1212A02. diff -r 14f6c8d21ec1 -r 189aac7dd3d6 .editorconfig --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.editorconfig Wed Feb 04 17:44:25 2015 +0100 @@ -0,0 +1,12 @@ +; Top-most EditorConfig file +root = true + +; Unix-style newlines +[*] +end_of_line = crlf + +; 4-column tab indentation +[*.cs] +indent_style = tab +indent_size = 4 + diff -r 14f6c8d21ec1 -r 189aac7dd3d6 Server/Display.cs --- a/Server/Display.cs Wed Feb 04 15:48:08 2015 +0100 +++ b/Server/Display.cs Wed Feb 04 17:44:25 2015 +0100 @@ -10,11 +10,33 @@ namespace SharpDisplayManager { + /// /// Provide access to our display hardware through MiniDisplay API. /// public class Display { + public delegate void OnOpenedHandler(Display aDisplay); + public event OnOpenedHandler OnOpened; + + public delegate void OnClosedHandler(Display aDisplay); + public event OnClosedHandler OnClosed; + + //Our display device handle + IntPtr iDevice; + + //static functions + public static int TypeCount() + { + return MiniDisplayTypeCount(); + } + + public static string TypeName(TMiniDisplayType aType) + { + IntPtr ptr = MiniDisplayTypeName(aType); + string str = Marshal.PtrToStringUni(ptr); + return str; + } //Constructor public Display() @@ -25,17 +47,36 @@ // public bool Open(TMiniDisplayType aType) { - if (iDevice == IntPtr.Zero) - { - iDevice = MiniDisplayOpen(aType); - } - return iDevice != IntPtr.Zero; + if (IsOpen()) + { + //Already open return an error + return false; + } + + iDevice = MiniDisplayOpen(aType); + + bool success = iDevice != IntPtr.Zero; + if (success) + { + //Broadcast opened event + OnOpened(this); + } + + return success; } public void Close() { + if (!IsOpen()) + { + //Pointless + return; + } + MiniDisplayClose(iDevice); iDevice = IntPtr.Zero; + //Broadcast closed event + OnClosed(this); } public bool IsOpen() @@ -192,10 +233,6 @@ return str; } - //Our display device handle - IntPtr iDevice; - - //[Serializable] public enum TMiniDisplayType { @@ -221,6 +258,12 @@ [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] public static extern void MiniDisplayClose(IntPtr aDevice); + [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] + public static extern int MiniDisplayTypeCount(); + + [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] + public static extern IntPtr MiniDisplayTypeName(TMiniDisplayType aType); + [DllImport("MiniDisplay.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)] public static extern void MiniDisplayClear(IntPtr aDevice); diff -r 14f6c8d21ec1 -r 189aac7dd3d6 Server/MainForm.Designer.cs --- a/Server/MainForm.Designer.cs Wed Feb 04 15:48:08 2015 +0100 +++ b/Server/MainForm.Designer.cs Wed Feb 04 17:44:25 2015 +0100 @@ -357,10 +357,6 @@ // this.comboBoxDisplayType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.comboBoxDisplayType.FormattingEnabled = true; - this.comboBoxDisplayType.Items.AddRange(new object[] { - "Auto Detect", - "Futaba GP1212A01A", - "Futaba GP1212A02A"}); this.comboBoxDisplayType.Location = new System.Drawing.Point(187, 9); this.comboBoxDisplayType.Name = "comboBoxDisplayType"; this.comboBoxDisplayType.Size = new System.Drawing.Size(181, 21); diff -r 14f6c8d21ec1 -r 189aac7dd3d6 Server/MainForm.cs --- a/Server/MainForm.cs Wed Feb 04 15:48:08 2015 +0100 +++ b/Server/MainForm.cs Wed Feb 04 17:44:25 2015 +0100 @@ -72,17 +72,29 @@ iCurrentClientSessionId = ""; iCurrentClientData = null; LastTickTime = DateTime.Now; + //Instantiate our display and register for events notifications iDisplay = new Display(); - iClients = new Dictionary(); + iDisplay.OnOpened += OnDisplayOpened; + iDisplay.OnClosed += OnDisplayClosed; + // + iClients = new Dictionary(); iStartupManager = new StartupManager(); iNotifyIcon = new NotifyIconAdv(); + //Have our designer initialize its controls InitializeComponent(); + + //Populate device types + PopulateDeviceTypes(); + + //Initial status update UpdateStatus(); + //We have a bug when drawing minimized and reusing our bitmap iBmp = new System.Drawing.Bitmap(tableLayoutPanel.Width, tableLayoutPanel.Height, PixelFormat.Format32bppArgb); iCreateBitmap = false; + //Minimize our window if desired if (Properties.Settings.Default.StartMinimized) { WindowState = FormWindowState.Minimized; @@ -91,29 +103,59 @@ } /// + /// Called when our display is opened. + /// + /// + private void OnDisplayOpened(Display aDisplay) + { + //Our display was just opened, update our UI + UpdateStatus(); + //Initiate asynchronous request + iDisplay.RequestFirmwareRevision(); + } + + /// + /// Called when our display is closed. + /// + /// + private void OnDisplayClosed(Display aDisplay) + { + //Our display was just closed, update our UI consequently + UpdateStatus(); + } + + /// + /// + /// + private void PopulateDeviceTypes() + { + int count = Display.TypeCount(); + + for (int i=0; i /// /// /// /// private void MainForm_Load(object sender, EventArgs e) { + //Check if we are running a Click Once deployed application if (ApplicationDeployment.IsNetworkDeployed) { + //This is a proper Click Once installation, fetch and show our version number this.Text += " - v" + ApplicationDeployment.CurrentDeployment.CurrentVersion; } else { + //Not a proper Click Once installation, assuming development build then this.Text += " - development"; } - StartServer(); - - //Open display connection on start-up if needed - if (Properties.Settings.Default.DisplayConnectOnStartup) - { - OpenDisplayConnection(); - } - //Setup notification icon SetupTrayIcon(); @@ -127,6 +169,15 @@ //When not debugging we want the screen to be empty until a client takes over ClearLayout(); #endif + + //Open display connection on start-up if needed + if (Properties.Settings.Default.DisplayConnectOnStartup) + { + OpenDisplayConnection(); + } + + //Start our server so that we can get client requests + StartServer(); } /// @@ -506,22 +557,17 @@ { CloseDisplayConnection(); - if (iDisplay.Open((Display.TMiniDisplayType)cds.DisplayType)) - { - UpdateStatus(); - iDisplay.RequestFirmwareRevision(); - } - else - { - UpdateStatus(); - toolStripStatusLabelConnect.Text = "Connection error"; + if (!iDisplay.Open((Display.TMiniDisplayType)cds.DisplayType)) + { + UpdateStatus(); + toolStripStatusLabelConnect.Text = "Connection error"; } } private void CloseDisplayConnection() { + //Status will be updated upon receiving the closed event iDisplay.Close(); - UpdateStatus(); } private void buttonOpen_Click(object sender, EventArgs e) diff -r 14f6c8d21ec1 -r 189aac7dd3d6 Server/MiniDisplay.dll Binary file Server/MiniDisplay.dll has changed