Server/Events/EventHid.cs
author StephaneLenclud
Wed, 31 Aug 2016 16:06:47 +0200
changeset 262 c4749a27966d
parent 248 0220cb84df58
permissions -rw-r--r--
Fix crash when trying to select Harmony command without configuration.
Consolidate Named event Trigger.
     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Reflection;
     5 using System.Text;
     6 using System.Threading.Tasks;
     7 using System.Runtime.Serialization;
     8 using System.Windows.Forms;
     9 using Ear = SharpLib.Ear;
    10 using Hid = SharpLib.Hid;
    11 
    12 namespace SharpDisplayManager
    13 {
    14     [DataContract]
    15     [Ear.AttributeObject(Id = "Event.Hid", Name = "HID", Description = "Handle input from Keyboards and Remotes.")]
    16     public class EventHid: Ear.Event
    17     {
    18         public EventHid()
    19         {
    20         }
    21 
    22         [DataMember]
    23         public ushort UsagePage { get; set; }
    24 
    25         [DataMember]
    26         public ushort UsageCollection { get; set; }
    27 
    28         [DataMember]
    29         public ushort Usage { get; set; }
    30 
    31         [DataMember]
    32         public Keys Key { get; set; }
    33 
    34         [DataMember]
    35         [Ear.AttributeObjectProperty
    36             (
    37             Id = "HID.Keyboard.IsKeyUp",
    38             Name = "Key Up",
    39             Description = "Key up if set, key down otherwise."
    40             )]
    41         public bool IsKeyUp { get; set; } = false;
    42 
    43         [DataMember]
    44         public bool IsMouse { get; set; } = false;
    45 
    46         [DataMember]
    47         public bool IsKeyboard { get; set; } = false;
    48 
    49         [DataMember]
    50         public bool IsGeneric { get; set; } = false;
    51 
    52         [DataMember]
    53         public bool HasModifierShift { get; set; } = false;
    54 
    55         [DataMember]
    56         public bool HasModifierControl { get; set; } = false;
    57 
    58         [DataMember]
    59         public bool HasModifierAlt { get; set; } = false;
    60 
    61         [DataMember]
    62         public bool HasModifierWindows { get; set; } = false;
    63 
    64         [DataMember]
    65         public string UsageName { get; set; } = "Press a key";
    66 
    67 
    68 
    69         protected override void DoConstruct()
    70         {
    71             base.DoConstruct();
    72             UpdateDynamicProperties();
    73         }
    74 
    75         private void UpdateDynamicProperties()
    76         {
    77             
    78         }
    79 
    80         /// <summary>
    81         /// Make sure we distinguish between various configuration of this event 
    82         /// </summary>
    83         /// <returns></returns>
    84         public override string Brief()
    85         {
    86             string brief = AttributeName + ": ";
    87 
    88             if (!IsValid())
    89             {
    90                 brief += "Press a key";
    91                 return brief;
    92             }
    93 
    94             if (IsKeyboard)
    95             {
    96                 brief += Key.ToString();
    97 
    98                 if (HasModifierAlt)
    99                 {
   100                     brief += " + ALT";
   101                 }
   102 
   103                 if (HasModifierControl)
   104                 {
   105                     brief += " + CTRL";
   106                 }
   107 
   108                 if (HasModifierShift)
   109                 {
   110                     brief += " + SHIFT";
   111                 }
   112 
   113                 if (HasModifierWindows)
   114                 {
   115                     brief += " + WIN";
   116                 }
   117             }
   118             else if (IsGeneric)
   119             {
   120                 brief += UsageName;
   121             }
   122 
   123             if (IsKeyUp)
   124             {
   125                 brief += " (UP)";
   126             }
   127             else
   128             {
   129                 brief += " (DOWN)";
   130             }
   131 
   132             return brief;
   133         }
   134 
   135         /// <summary>
   136         ///
   137         /// </summary>
   138         /// <param name="obj"></param>
   139         /// <returns></returns>
   140         public override bool Equals(object obj)
   141         {
   142             if (obj is EventHid)
   143             {
   144                 EventHid e = (EventHid)obj;
   145                 return e.Key == Key
   146                     && e.Usage == Usage
   147                     && e.UsagePage == UsagePage
   148                     && e.UsageCollection == UsageCollection
   149                     && e.IsKeyUp == IsKeyUp
   150                     && e.IsGeneric == IsGeneric
   151                     && e.IsKeyboard == IsKeyboard
   152                     && e.IsMouse == IsMouse
   153                     && e.HasModifierAlt == HasModifierAlt
   154                     && e.HasModifierControl == HasModifierControl
   155                     && e.HasModifierShift == HasModifierShift
   156                     && e.HasModifierWindows == HasModifierWindows;
   157             }
   158 
   159             return false;
   160         }
   161 
   162 
   163         /// <summary>
   164         /// 
   165         /// </summary>
   166         protected override void OnStateLeave()
   167         {
   168             if (CurrentState == State.Edit)
   169             {
   170                 // Leaving edit mode
   171                 // Unhook HID events
   172                 Program.HidHandler.OnHidEvent -= HandleHidEvent;
   173 
   174             }
   175         }
   176 
   177         /// <summary>
   178         /// 
   179         /// </summary>
   180         protected override void OnStateEnter()
   181         {
   182             if (CurrentState == State.Edit)
   183             {
   184                 // Enter edit mode
   185                 // Hook-in HID events
   186                 Program.HidHandler.OnHidEvent += HandleHidEvent;
   187 
   188             }
   189         }
   190 
   191         /// <summary>
   192         /// Here we receive HID events from our HID library.
   193         /// </summary>
   194         /// <param name="aSender"></param>
   195         /// <param name="aHidEvent"></param>
   196         public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent)
   197         {
   198             if (CurrentState != State.Edit
   199                 || aHidEvent.IsMouse
   200                 || aHidEvent.IsButtonUp
   201                 || !aHidEvent.IsValid
   202                 || aHidEvent.IsBackground
   203                 || aHidEvent.IsRepeat
   204                 || aHidEvent.IsStray)
   205             {
   206                 return;
   207             }
   208 
   209             PrivateCopy(aHidEvent);
   210             //
   211 
   212             //Tell observer the object itself changed
   213             OnPropertyChanged("Brief");
   214         }
   215 
   216         /// <summary>
   217         /// 
   218         /// </summary>
   219         /// <param name="aHidEvent"></param>
   220         public void Copy(Hid.Event aHidEvent)
   221         {
   222             PrivateCopy(aHidEvent);
   223             //We need the key up/down too here
   224             IsKeyUp = aHidEvent.IsButtonUp;
   225         }
   226 
   227         /// <summary>
   228         /// 
   229         /// </summary>
   230         /// <param name="aHidEvent"></param>
   231         private void PrivateCopy(Hid.Event aHidEvent)
   232         {
   233             //Copy for scan
   234             UsagePage = aHidEvent.UsagePage;
   235             UsageCollection = aHidEvent.UsageCollection;
   236             IsGeneric = aHidEvent.IsGeneric;
   237             IsKeyboard = aHidEvent.IsKeyboard;
   238             IsMouse = aHidEvent.IsMouse;
   239 
   240             if (IsGeneric)
   241             {
   242                 if (aHidEvent.Usages.Count > 0)
   243                 {
   244                     Usage = aHidEvent.Usages[0];
   245                     UsageName = aHidEvent.UsageName(0);
   246                 }
   247 
   248                 Key = Keys.None;
   249                 HasModifierAlt = false;
   250                 HasModifierControl = false;
   251                 HasModifierShift = false;
   252                 HasModifierWindows = false;
   253             }
   254             else if (IsKeyboard)
   255             {
   256                 Usage = 0;
   257                 Key = aHidEvent.VirtualKey;
   258                 HasModifierAlt = aHidEvent.HasModifierAlt;
   259                 HasModifierControl = aHidEvent.HasModifierControl;
   260                 HasModifierShift = aHidEvent.HasModifierShift;
   261                 HasModifierWindows = aHidEvent.HasModifierWindows;
   262             }
   263 
   264         }
   265 
   266         /// <summary>
   267         /// 
   268         /// </summary>
   269         /// <returns></returns>
   270         public override bool IsValid()
   271         {
   272             return IsGeneric || IsKeyboard;
   273         }
   274     }
   275 }