Server/Events/EventHid.cs
author StephaneLenclud
Tue, 30 Aug 2016 03:07:59 +0200
changeset 257 3f1d16d233dc
parent 247 afdbe76ab03b
child 260 d44943088c67
permissions -rw-r--r--
Using Harmony Function Action Command.
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@248
    88
            if (!IsValid())
StephaneLenclud@248
    89
            {
StephaneLenclud@248
    90
                brief += "Press a key";
StephaneLenclud@248
    91
                return brief;
StephaneLenclud@248
    92
            }
StephaneLenclud@248
    93
StephaneLenclud@244
    94
            if (IsKeyboard)
StephaneLenclud@244
    95
            {
StephaneLenclud@244
    96
                brief += Key.ToString();
StephaneLenclud@244
    97
StephaneLenclud@244
    98
                if (HasModifierAlt)
StephaneLenclud@244
    99
                {
StephaneLenclud@244
   100
                    brief += " + ALT";
StephaneLenclud@244
   101
                }
StephaneLenclud@244
   102
StephaneLenclud@244
   103
                if (HasModifierControl)
StephaneLenclud@244
   104
                {
StephaneLenclud@244
   105
                    brief += " + CTRL";
StephaneLenclud@244
   106
                }
StephaneLenclud@244
   107
StephaneLenclud@244
   108
                if (HasModifierShift)
StephaneLenclud@244
   109
                {
StephaneLenclud@244
   110
                    brief += " + SHIFT";
StephaneLenclud@244
   111
                }
StephaneLenclud@244
   112
StephaneLenclud@244
   113
                if (HasModifierWindows)
StephaneLenclud@244
   114
                {
StephaneLenclud@244
   115
                    brief += " + WIN";
StephaneLenclud@244
   116
                }
StephaneLenclud@244
   117
            }
StephaneLenclud@244
   118
            else if (IsGeneric)
StephaneLenclud@244
   119
            {
StephaneLenclud@247
   120
                brief += UsageName;
StephaneLenclud@244
   121
            }
StephaneLenclud@244
   122
StephaneLenclud@244
   123
            if (IsKeyUp)
StephaneLenclud@244
   124
            {
StephaneLenclud@244
   125
                brief += " (UP)";
StephaneLenclud@244
   126
            }
StephaneLenclud@244
   127
            else
StephaneLenclud@244
   128
            {
StephaneLenclud@244
   129
                brief += " (DOWN)";
StephaneLenclud@244
   130
            }
StephaneLenclud@244
   131
StephaneLenclud@244
   132
            return brief;
StephaneLenclud@244
   133
        }
StephaneLenclud@244
   134
StephaneLenclud@244
   135
        /// <summary>
StephaneLenclud@244
   136
        ///
StephaneLenclud@244
   137
        /// </summary>
StephaneLenclud@244
   138
        /// <param name="obj"></param>
StephaneLenclud@244
   139
        /// <returns></returns>
StephaneLenclud@244
   140
        public override bool Equals(object obj)
StephaneLenclud@244
   141
        {
StephaneLenclud@247
   142
            if (obj is EventHid)
StephaneLenclud@244
   143
            {
StephaneLenclud@247
   144
                EventHid e = (EventHid)obj;
StephaneLenclud@244
   145
                return e.Key == Key
StephaneLenclud@247
   146
                    && e.Usage == Usage
StephaneLenclud@247
   147
                    && e.UsagePage == UsagePage
StephaneLenclud@247
   148
                    && e.UsageCollection == UsageCollection
StephaneLenclud@247
   149
                    && e.IsKeyUp == IsKeyUp
StephaneLenclud@247
   150
                    && e.IsGeneric == IsGeneric
StephaneLenclud@247
   151
                    && e.IsKeyboard == IsKeyboard
StephaneLenclud@247
   152
                    && e.IsMouse == IsMouse
StephaneLenclud@247
   153
                    && e.HasModifierAlt == HasModifierAlt
StephaneLenclud@247
   154
                    && e.HasModifierControl == HasModifierControl
StephaneLenclud@247
   155
                    && e.HasModifierShift == HasModifierShift
StephaneLenclud@247
   156
                    && e.HasModifierWindows == HasModifierWindows;
StephaneLenclud@244
   157
            }
StephaneLenclud@244
   158
StephaneLenclud@244
   159
            return false;
StephaneLenclud@244
   160
        }
StephaneLenclud@246
   161
StephaneLenclud@246
   162
StephaneLenclud@246
   163
        /// <summary>
StephaneLenclud@246
   164
        /// 
StephaneLenclud@246
   165
        /// </summary>
StephaneLenclud@246
   166
        protected override void OnStateLeave()
StephaneLenclud@246
   167
        {
StephaneLenclud@246
   168
            if (CurrentState == State.Edit)
StephaneLenclud@246
   169
            {
StephaneLenclud@246
   170
                // Leaving edit mode
StephaneLenclud@246
   171
                // Unhook HID events
StephaneLenclud@246
   172
                Program.HidHandler.OnHidEvent -= HandleHidEvent;
StephaneLenclud@246
   173
StephaneLenclud@246
   174
            }
StephaneLenclud@246
   175
        }
StephaneLenclud@246
   176
StephaneLenclud@246
   177
        /// <summary>
StephaneLenclud@246
   178
        /// 
StephaneLenclud@246
   179
        /// </summary>
StephaneLenclud@246
   180
        protected override void OnStateEnter()
StephaneLenclud@246
   181
        {
StephaneLenclud@246
   182
            if (CurrentState == State.Edit)
StephaneLenclud@246
   183
            {
StephaneLenclud@246
   184
                // Enter edit mode
StephaneLenclud@246
   185
                // Hook-in HID events
StephaneLenclud@246
   186
                Program.HidHandler.OnHidEvent += HandleHidEvent;
StephaneLenclud@246
   187
StephaneLenclud@246
   188
            }
StephaneLenclud@246
   189
        }
StephaneLenclud@246
   190
StephaneLenclud@246
   191
        /// <summary>
StephaneLenclud@246
   192
        /// Here we receive HID events from our HID library.
StephaneLenclud@246
   193
        /// </summary>
StephaneLenclud@246
   194
        /// <param name="aSender"></param>
StephaneLenclud@246
   195
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   196
        public void HandleHidEvent(object aSender, SharpLib.Hid.Event aHidEvent)
StephaneLenclud@246
   197
        {
StephaneLenclud@246
   198
            if (CurrentState != State.Edit
StephaneLenclud@246
   199
                || aHidEvent.IsMouse
StephaneLenclud@246
   200
                || aHidEvent.IsButtonUp
StephaneLenclud@246
   201
                || !aHidEvent.IsValid
StephaneLenclud@246
   202
                || aHidEvent.IsBackground
StephaneLenclud@246
   203
                || aHidEvent.IsRepeat
StephaneLenclud@246
   204
                || aHidEvent.IsStray)
StephaneLenclud@246
   205
            {
StephaneLenclud@246
   206
                return;
StephaneLenclud@246
   207
            }
StephaneLenclud@246
   208
StephaneLenclud@246
   209
            PrivateCopy(aHidEvent);
StephaneLenclud@246
   210
            //
StephaneLenclud@246
   211
StephaneLenclud@246
   212
            //Tell observer the object itself changed
StephaneLenclud@246
   213
            OnPropertyChanged("Brief");
StephaneLenclud@246
   214
        }
StephaneLenclud@246
   215
StephaneLenclud@246
   216
        /// <summary>
StephaneLenclud@246
   217
        /// 
StephaneLenclud@246
   218
        /// </summary>
StephaneLenclud@246
   219
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   220
        public void Copy(Hid.Event aHidEvent)
StephaneLenclud@246
   221
        {
StephaneLenclud@246
   222
            PrivateCopy(aHidEvent);
StephaneLenclud@246
   223
            //We need the key up/down too here
StephaneLenclud@246
   224
            IsKeyUp = aHidEvent.IsButtonUp;
StephaneLenclud@246
   225
        }
StephaneLenclud@246
   226
StephaneLenclud@246
   227
        /// <summary>
StephaneLenclud@246
   228
        /// 
StephaneLenclud@246
   229
        /// </summary>
StephaneLenclud@246
   230
        /// <param name="aHidEvent"></param>
StephaneLenclud@246
   231
        private void PrivateCopy(Hid.Event aHidEvent)
StephaneLenclud@246
   232
        {
StephaneLenclud@246
   233
            //Copy for scan
StephaneLenclud@246
   234
            UsagePage = aHidEvent.UsagePage;
StephaneLenclud@246
   235
            UsageCollection = aHidEvent.UsageCollection;
StephaneLenclud@246
   236
            IsGeneric = aHidEvent.IsGeneric;
StephaneLenclud@246
   237
            IsKeyboard = aHidEvent.IsKeyboard;
StephaneLenclud@246
   238
            IsMouse = aHidEvent.IsMouse;
StephaneLenclud@246
   239
StephaneLenclud@246
   240
            if (IsGeneric)
StephaneLenclud@246
   241
            {
StephaneLenclud@246
   242
                if (aHidEvent.Usages.Count > 0)
StephaneLenclud@246
   243
                {
StephaneLenclud@246
   244
                    Usage = aHidEvent.Usages[0];
StephaneLenclud@247
   245
                    UsageName = aHidEvent.UsageName(0);
StephaneLenclud@246
   246
                }
StephaneLenclud@246
   247
StephaneLenclud@246
   248
                Key = Keys.None;
StephaneLenclud@246
   249
                HasModifierAlt = false;
StephaneLenclud@246
   250
                HasModifierControl = false;
StephaneLenclud@246
   251
                HasModifierShift = false;
StephaneLenclud@246
   252
                HasModifierWindows = false;
StephaneLenclud@246
   253
            }
StephaneLenclud@246
   254
            else if (IsKeyboard)
StephaneLenclud@246
   255
            {
StephaneLenclud@246
   256
                Usage = 0;
StephaneLenclud@246
   257
                Key = aHidEvent.VirtualKey;
StephaneLenclud@246
   258
                HasModifierAlt = aHidEvent.HasModifierAlt;
StephaneLenclud@246
   259
                HasModifierControl = aHidEvent.HasModifierControl;
StephaneLenclud@246
   260
                HasModifierShift = aHidEvent.HasModifierShift;
StephaneLenclud@246
   261
                HasModifierWindows = aHidEvent.HasModifierWindows;
StephaneLenclud@246
   262
            }
StephaneLenclud@246
   263
StephaneLenclud@246
   264
        }
StephaneLenclud@246
   265
StephaneLenclud@247
   266
        /// <summary>
StephaneLenclud@247
   267
        /// 
StephaneLenclud@247
   268
        /// </summary>
StephaneLenclud@247
   269
        /// <returns></returns>
StephaneLenclud@247
   270
        public override bool IsValid()
StephaneLenclud@247
   271
        {
StephaneLenclud@247
   272
            return IsGeneric || IsKeyboard;
StephaneLenclud@247
   273
        }
StephaneLenclud@244
   274
    }
StephaneLenclud@244
   275
}