# HG changeset patch # User StephaneLenclud # Date 1471719635 -7200 # Node ID 30a221eecc0605d1f75a51ddcce834066c3054f0 # Parent 448e6a616c22db21eb5c545ef2284009764c6c9e Generic HID event with key recognition functional. diff -r 448e6a616c22 -r 30a221eecc06 Server/Events/EventHid.cs --- a/Server/Events/EventHid.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/Server/Events/EventHid.cs Sat Aug 20 21:00:35 2016 +0200 @@ -41,13 +41,13 @@ public bool IsKeyUp { get; set; } = false; [DataMember] - public bool IsMouse { get; set; } + public bool IsMouse { get; set; } = false; [DataMember] - public bool IsKeyboard { get; set; } + public bool IsKeyboard { get; set; } = false; [DataMember] - public bool IsGeneric { get; set; } + public bool IsGeneric { get; set; } = false; [DataMember] public bool HasModifierShift { get; set; } = false; @@ -61,6 +61,10 @@ [DataMember] public bool HasModifierWindows { get; set; } = false; + [DataMember] + public string PersistedBrief { get; set; } = "Press a key"; + + protected override void DoConstruct() { @@ -73,7 +77,6 @@ } - /// /// Make sure we distinguish between various configuration of this event /// @@ -108,7 +111,7 @@ } else if (IsGeneric) { - + brief += PersistedBrief; } if (IsKeyUp) @@ -143,5 +146,111 @@ return false; } + + + /// + /// + /// + protected override void OnStateLeave() + { + if (CurrentState == State.Edit) + { + // Leaving edit mode + // Unhook HID events + Program.HidHandler.OnHidEvent -= HandleHidEvent; + + } + } + + /// + /// + /// + protected override void OnStateEnter() + { + if (CurrentState == State.Edit) + { + // Enter edit mode + // Hook-in HID events + Program.HidHandler.OnHidEvent += HandleHidEvent; + + } + } + + /// + /// Here we receive HID events from our HID library. + /// + /// + /// + public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent) + { + if (CurrentState != State.Edit + || aHidEvent.IsMouse + || aHidEvent.IsButtonUp + || !aHidEvent.IsValid + || aHidEvent.IsBackground + || aHidEvent.IsRepeat + || aHidEvent.IsStray) + { + return; + } + + PrivateCopy(aHidEvent); + // + + //Tell observer the object itself changed + OnPropertyChanged("Brief"); + } + + /// + /// + /// + /// + public void Copy(Hid.Event aHidEvent) + { + PrivateCopy(aHidEvent); + //We need the key up/down too here + IsKeyUp = aHidEvent.IsButtonUp; + } + + /// + /// + /// + /// + private void PrivateCopy(Hid.Event aHidEvent) + { + //Copy for scan + UsagePage = aHidEvent.UsagePage; + UsageCollection = aHidEvent.UsageCollection; + IsGeneric = aHidEvent.IsGeneric; + IsKeyboard = aHidEvent.IsKeyboard; + IsMouse = aHidEvent.IsMouse; + + if (IsGeneric) + { + if (aHidEvent.Usages.Count > 0) + { + Usage = aHidEvent.Usages[0]; + PersistedBrief = aHidEvent.UsageName(0); + } + + Key = Keys.None; + HasModifierAlt = false; + HasModifierControl = false; + HasModifierShift = false; + HasModifierWindows = false; + } + else if (IsKeyboard) + { + Usage = 0; + Key = aHidEvent.VirtualKey; + HasModifierAlt = aHidEvent.HasModifierAlt; + HasModifierControl = aHidEvent.HasModifierControl; + HasModifierShift = aHidEvent.HasModifierShift; + HasModifierWindows = aHidEvent.HasModifierWindows; + } + + } + + } } diff -r 448e6a616c22 -r 30a221eecc06 Server/FormEditObject.Designer.cs --- a/Server/FormEditObject.Designer.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/Server/FormEditObject.Designer.cs Sat Aug 20 21:00:35 2016 +0200 @@ -36,6 +36,7 @@ this.iTableLayoutPanel = new System.Windows.Forms.TableLayoutPanel(); this.toolTip = new System.Windows.Forms.ToolTip(this.components); this.buttonTest = new System.Windows.Forms.Button(); + this.labelBrief = new System.Windows.Forms.Label(); this.SuspendLayout(); // // comboBoxActionType @@ -64,7 +65,7 @@ // this.buttonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonOk.DialogResult = System.Windows.Forms.DialogResult.OK; - this.buttonOk.Location = new System.Drawing.Point(12, 72); + this.buttonOk.Location = new System.Drawing.Point(12, 151); this.buttonOk.Name = "buttonOk"; this.buttonOk.Size = new System.Drawing.Size(75, 23); this.buttonOk.TabIndex = 21; @@ -76,7 +77,7 @@ // this.buttonCancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.buttonCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel; - this.buttonCancel.Location = new System.Drawing.Point(93, 72); + this.buttonCancel.Location = new System.Drawing.Point(93, 151); this.buttonCancel.Name = "buttonCancel"; this.buttonCancel.Size = new System.Drawing.Size(75, 23); this.buttonCancel.TabIndex = 22; @@ -93,18 +94,18 @@ this.iTableLayoutPanel.ColumnCount = 2; this.iTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.iTableLayoutPanel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.iTableLayoutPanel.Location = new System.Drawing.Point(15, 50); + this.iTableLayoutPanel.Location = new System.Drawing.Point(15, 72); this.iTableLayoutPanel.Name = "iTableLayoutPanel"; this.iTableLayoutPanel.RowCount = 2; this.iTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); this.iTableLayoutPanel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); - this.iTableLayoutPanel.Size = new System.Drawing.Size(312, 16); + this.iTableLayoutPanel.Size = new System.Drawing.Size(312, 60); this.iTableLayoutPanel.TabIndex = 23; // // buttonTest // this.buttonTest.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); - this.buttonTest.Location = new System.Drawing.Point(252, 72); + this.buttonTest.Location = new System.Drawing.Point(252, 151); this.buttonTest.Name = "buttonTest"; this.buttonTest.Size = new System.Drawing.Size(75, 23); this.buttonTest.TabIndex = 24; @@ -112,6 +113,17 @@ this.buttonTest.UseVisualStyleBackColor = true; this.buttonTest.Click += new System.EventHandler(this.buttonTest_Click); // + // labelBrief + // + this.labelBrief.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.labelBrief.AutoSize = true; + this.labelBrief.Location = new System.Drawing.Point(12, 45); + this.labelBrief.Name = "labelBrief"; + this.labelBrief.Size = new System.Drawing.Size(28, 13); + this.labelBrief.TabIndex = 25; + this.labelBrief.Text = "Brief"; + // // FormEditObject // this.AcceptButton = this.buttonOk; @@ -120,13 +132,14 @@ this.AutoSize = true; this.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink; this.CancelButton = this.buttonCancel; - this.ClientSize = new System.Drawing.Size(339, 107); + this.ClientSize = new System.Drawing.Size(339, 186); + this.Controls.Add(this.labelBrief); this.Controls.Add(this.buttonTest); - this.Controls.Add(this.iTableLayoutPanel); this.Controls.Add(this.buttonCancel); this.Controls.Add(this.buttonOk); this.Controls.Add(this.labelActionType); this.Controls.Add(this.comboBoxActionType); + this.Controls.Add(this.iTableLayoutPanel); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog; this.MaximizeBox = false; this.MinimizeBox = false; @@ -148,5 +161,6 @@ private System.Windows.Forms.TableLayoutPanel iTableLayoutPanel; private System.Windows.Forms.ToolTip toolTip; private System.Windows.Forms.Button buttonTest; + private System.Windows.Forms.Label labelBrief; } } \ No newline at end of file diff -r 448e6a616c22 -r 30a221eecc06 Server/FormEditObject.cs --- a/Server/FormEditObject.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/Server/FormEditObject.cs Sat Aug 20 21:00:35 2016 +0200 @@ -53,7 +53,7 @@ else { // Editing existing object - // Look up our item in our combobox + // Look up our item in our object type combobox foreach (ItemObjectType item in comboBoxActionType.Items) { if (item.Type == Object.GetType()) @@ -61,6 +61,7 @@ comboBoxActionType.SelectedItem = item; } } + } } @@ -78,6 +79,13 @@ private void FormEditObject_FormClosing(object sender, FormClosingEventArgs e) { e.Cancel = DialogResult == DialogResult.None; + + if (!e.Cancel) + { + //Exit edit mode + Object.CurrentState = SharpLib.Ear.Object.State.Rest; + Object.PropertyChanged -= PropertyChangedEventHandlerThreadSafe; + } } private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e) @@ -338,8 +346,7 @@ /// /// Update our table layout. - /// Will instantiated every field control as defined by our action. - /// Fields must be specified by rows from the left. + /// Will instantiated every field control as defined by our object. /// /// private void UpdateTableLayoutPanel(T aObject) @@ -363,10 +370,13 @@ //Just drop it return; } - + //IEnumerable properties = aObject.GetType().GetProperties().Where( // prop => Attribute.IsDefined(prop, typeof(AttributeObjectProperty))); + //TODO: Do this whenever a field changes + labelBrief.Text = Object.Brief(); + foreach (PropertyInfo pi in aObject.GetType().GetProperties()) { @@ -404,8 +414,34 @@ //Add tooltip to editor too toolTip.SetToolTip(ctrl, attribute.Description); - } + } + //Entrer object edit mode + Object.CurrentState = SharpLib.Ear.Object.State.Edit; + Object.PropertyChanged += PropertyChangedEventHandlerThreadSafe; + } + + void PropertyChangedEventHandlerThreadSafe(object sender, PropertyChangedEventArgs e) + { + if (this.InvokeRequired) + { + //Not in the proper thread, invoke ourselves + PropertyChangedEventHandler d = new PropertyChangedEventHandler(PropertyChangedEventHandlerThreadSafe); + this.Invoke(d, new object[] { sender, e }); + } + else + { + //Disable ok button if our object is not valid + buttonOk.Enabled = Object.IsValid(); + + if (e.PropertyName == "Brief") + { + labelBrief.Text = Object.Brief(); + } + + //Create input fields + //UpdateTableLayoutPanel(Object); + } } private void buttonTest_Click(object sender, EventArgs e) diff -r 448e6a616c22 -r 30a221eecc06 Server/FormMain.Hid.cs --- a/Server/FormMain.Hid.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/Server/FormMain.Hid.cs Sat Aug 20 21:00:35 2016 +0200 @@ -24,10 +24,6 @@ // public delegate void OnHidEventDelegate(object aSender, Hid.Event aHidEvent); - /// - /// Use notably to handle green start key from IR remote control - /// - private Hid.Handler iHidHandler; /// /// Register HID devices so that we receive corresponding WM_INPUT messages. @@ -89,12 +85,12 @@ //rid[i].hwndTarget = aHWND; - iHidHandler = new SharpLib.Hid.Handler(rid); - if (!iHidHandler.IsRegistered) + Program.HidHandler = new SharpLib.Hid.Handler(rid); + if (!Program.HidHandler.IsRegistered) { Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString()); } - iHidHandler.OnHidEvent += HandleHidEventThreadSafe; + Program.HidHandler.OnHidEvent += HandleHidEventThreadSafe; } @@ -184,7 +180,7 @@ case Const.WM_INPUT: //Returning zero means we processed that message. message.Result = new IntPtr(0); - iHidHandler.ProcessInput(ref message); + Program.HidHandler.ProcessInput(ref message); break; } diff -r 448e6a616c22 -r 30a221eecc06 Server/Program.cs --- a/Server/Program.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/Server/Program.cs Sat Aug 20 21:00:35 2016 +0200 @@ -20,6 +20,7 @@ using System; using System.Windows.Forms; using System.Security.Principal; +using Hid = SharpLib.Hid; namespace SharpDisplayManager @@ -42,6 +43,11 @@ /// + /// Use notably to handle green start key from IR remote control + /// + public static Hid.Handler HidHandler; + + /// /// The main entry point for the application. /// [STAThread] diff -r 448e6a616c22 -r 30a221eecc06 SharpLibEar/Object.cs --- a/SharpLibEar/Object.cs Fri Aug 19 19:28:03 2016 +0200 +++ b/SharpLibEar/Object.cs Sat Aug 20 21:00:35 2016 +0200 @@ -48,6 +48,32 @@ } + + public enum State + { + Rest=0, + Edit + } + + State iCurrentState = State.Rest; + public State CurrentState { get { return iCurrentState; } set { OnStateLeave(); iCurrentState = value; OnStateEnter(); } } + + /// + /// + /// + protected virtual void OnStateLeave() + { + + } + + /// + /// + /// + protected virtual void OnStateEnter() + { + + } + /// /// Static object name. /// @@ -111,6 +137,7 @@ /// public event PropertyChangedEventHandler PropertyChanged; + /// /// Invoke our event. ///