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