RemoteControlDevice.cs
changeset 78 72885c950813
parent 77 fb9ea5ad8c2d
child 79 cdc5f8f1b79e
     1.1 --- a/RemoteControlDevice.cs	Sun Mar 15 14:45:40 2015 +0100
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,445 +0,0 @@
     1.4 -//
     1.5 -// Copyright (C) 2014-2015 Stéphane Lenclud.
     1.6 -//
     1.7 -// This file is part of SharpLibHid.
     1.8 -//
     1.9 -// SharpDisplayManager is free software: you can redistribute it and/or modify
    1.10 -// it under the terms of the GNU General Public License as published by
    1.11 -// the Free Software Foundation, either version 3 of the License, or
    1.12 -// (at your option) any later version.
    1.13 -//
    1.14 -// SharpDisplayManager is distributed in the hope that it will be useful,
    1.15 -// but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.16 -// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.17 -// GNU General Public License for more details.
    1.18 -//
    1.19 -// You should have received a copy of the GNU General Public License
    1.20 -// along with SharpDisplayManager.  If not, see <http://www.gnu.org/licenses/>.
    1.21 -//
    1.22 -
    1.23 -using System;
    1.24 -using System.Windows.Forms;
    1.25 -using System.Runtime.InteropServices;
    1.26 -using System.Diagnostics;
    1.27 -using System.Text;
    1.28 -using Microsoft.Win32.SafeHandles;
    1.29 -
    1.30 -using SharpLib.Hid.Usage;
    1.31 -using SharpLib.Win32;
    1.32 -
    1.33 -
    1.34 -
    1.35 -namespace Devices.RemoteControl
    1.36 -{
    1.37 -
    1.38 -    public enum InputDevice
    1.39 -    {
    1.40 -        Key,
    1.41 -        Mouse,
    1.42 -        OEM
    1.43 -    }
    1.44 -
    1.45 -
    1.46 -    public enum RemoteControlButton
    1.47 -    {
    1.48 -        Clear,
    1.49 -        Down,
    1.50 -        Left,
    1.51 -        Digit0,
    1.52 -        Digit1,
    1.53 -        Digit2,
    1.54 -        Digit3,
    1.55 -        Digit4,
    1.56 -        Digit5,
    1.57 -        Digit6,
    1.58 -        Digit7,
    1.59 -        Digit8,
    1.60 -        Digit9,
    1.61 -        Enter,
    1.62 -        Right,
    1.63 -        Up,
    1.64 -
    1.65 -        Back,
    1.66 -        ChannelDown,
    1.67 -        ChannelUp,
    1.68 -        FastForward,
    1.69 -        VolumeMute,
    1.70 -        Pause,
    1.71 -        Play,
    1.72 -        PlayPause,
    1.73 -        Record,
    1.74 -        PreviousTrack,
    1.75 -        Rewind,
    1.76 -        NextTrack,
    1.77 -        Stop,
    1.78 -        VolumeDown,
    1.79 -        VolumeUp,
    1.80 -
    1.81 -        RecordedTV,
    1.82 -        Guide,
    1.83 -        LiveTV,
    1.84 -        MoreInfo,
    1.85 -        Print,
    1.86 -        DVDMenu,
    1.87 -        DVDAngle,
    1.88 -        DVDAudio,
    1.89 -        DVDSubtitle,
    1.90 -        MyMusic,
    1.91 -        MyPictures,
    1.92 -        MyVideos,
    1.93 -        MyTV,
    1.94 -        OEM1,
    1.95 -        OEM2,
    1.96 -        StandBy,
    1.97 -        TVJump,
    1.98 -
    1.99 -        Unknown
   1.100 -    }
   1.101 -
   1.102 -
   1.103 -    #region RemoteControlEventArgs
   1.104 -
   1.105 -    public class RemoteControlEventArgs : EventArgs
   1.106 -    {
   1.107 -        RemoteControlButton _rcb;
   1.108 -        InputDevice _device;
   1.109 -        WindowsMediaCenterRemoteControl iMceButton;
   1.110 -        ConsumerControl iConsumerControl;
   1.111 -
   1.112 -        public RemoteControlEventArgs(RemoteControlButton rcb, InputDevice device)
   1.113 -        {
   1.114 -            SetNullButtons();
   1.115 -            //
   1.116 -            _rcb = rcb;
   1.117 -            _device = device;
   1.118 -        }
   1.119 -
   1.120 -        public RemoteControlEventArgs(ConsumerControl aConsumerControl, InputDevice device)
   1.121 -        {
   1.122 -            SetNullButtons();
   1.123 -            //
   1.124 -            iConsumerControl = aConsumerControl;
   1.125 -            _device = device;
   1.126 -        }
   1.127 -
   1.128 -
   1.129 -        public RemoteControlEventArgs(WindowsMediaCenterRemoteControl mce, InputDevice device)
   1.130 -        {
   1.131 -            SetNullButtons();
   1.132 -            //
   1.133 -            iMceButton = mce;
   1.134 -            _device = device;
   1.135 -        }
   1.136 -
   1.137 -        private void SetNullButtons()
   1.138 -        {
   1.139 -            iConsumerControl = ConsumerControl.Null;
   1.140 -            iMceButton = WindowsMediaCenterRemoteControl.Null;
   1.141 -            _rcb = RemoteControlButton.Unknown;
   1.142 -        }
   1.143 -
   1.144 -        public RemoteControlEventArgs()
   1.145 -        {
   1.146 -            iMceButton = WindowsMediaCenterRemoteControl.Null;
   1.147 -            _rcb = RemoteControlButton.Unknown;
   1.148 -            _device = InputDevice.Key;
   1.149 -        }
   1.150 -
   1.151 -        public RemoteControlButton Button
   1.152 -        {
   1.153 -            get { return _rcb; }
   1.154 -            set { _rcb = value; }
   1.155 -        }
   1.156 -
   1.157 -        public WindowsMediaCenterRemoteControl MceButton
   1.158 -        {
   1.159 -            get { return iMceButton; }
   1.160 -            set { iMceButton = value; }
   1.161 -        }
   1.162 -
   1.163 -        public ConsumerControl ConsumerControl
   1.164 -        {
   1.165 -            get { return iConsumerControl; }
   1.166 -            set { iConsumerControl = value; }
   1.167 -        }
   1.168 -
   1.169 -        public InputDevice Device
   1.170 -        {
   1.171 -            get { return _device; }
   1.172 -            set { _device = value; }
   1.173 -        }
   1.174 -    }
   1.175 -
   1.176 -    #endregion RemoteControlEventArgs
   1.177 -
   1.178 -
   1.179 -    public sealed class RemoteControlDevice
   1.180 -    {
   1.181 -        public delegate bool RemoteControlDeviceEventHandler(object sender, RemoteControlEventArgs e);
   1.182 -        public event RemoteControlDeviceEventHandler ButtonPressed;
   1.183 -
   1.184 -        /// <summary>
   1.185 -        /// Return true if the usage was processed.
   1.186 -        /// </summary>
   1.187 -        /// <param name="aUsage"></param>
   1.188 -        /// <returns></returns>
   1.189 -        public delegate bool HidUsageHandler(ushort aUsage);
   1.190 -
   1.191 -        public SharpLib.Hid.HidHandler iHidHandler;
   1.192 -
   1.193 -
   1.194 -        //-------------------------------------------------------------
   1.195 -        // constructors
   1.196 -        //-------------------------------------------------------------
   1.197 -
   1.198 -        public RemoteControlDevice(IntPtr aHWND)
   1.199 -        {
   1.200 -            // Register the input device to receive the commands from the
   1.201 -            // remote device. See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnwmt/html/remote_control.asp
   1.202 -            // for the vendor defined usage page.
   1.203 -
   1.204 -            RAWINPUTDEVICE[] rid = new RAWINPUTDEVICE[6];
   1.205 -
   1.206 -            int i = 0;
   1.207 -            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl;
   1.208 -            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl;
   1.209 -            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.210 -            rid[i].hwndTarget = aHWND;
   1.211 -
   1.212 -            i++;
   1.213 -            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
   1.214 -            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl;
   1.215 -            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.216 -            rid[i].hwndTarget = aHWND;
   1.217 -
   1.218 -            i++;
   1.219 -            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.Consumer;
   1.220 -            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.Consumer.Selection;
   1.221 -            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.222 -            rid[i].hwndTarget = aHWND;
   1.223 -
   1.224 -            i++;
   1.225 -            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   1.226 -            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.SystemControl;
   1.227 -            rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.228 -            rid[i].hwndTarget = aHWND;
   1.229 -
   1.230 -			i++;
   1.231 -			rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   1.232 -			rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.GamePad;
   1.233 -			rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.234 -			rid[i].hwndTarget = aHWND;
   1.235 -
   1.236 -            i++;
   1.237 -            rid[i].usUsagePage = (ushort)SharpLib.Hid.UsagePage.GenericDesktopControls;
   1.238 -            rid[i].usUsage = (ushort)SharpLib.Hid.UsageCollection.GenericDesktop.Keyboard;
   1.239 -            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.240 -            rid[i].hwndTarget = aHWND;
   1.241 -
   1.242 -            //i++;
   1.243 -            //rid[i].usUsagePage = (ushort)Hid.UsagePage.GenericDesktopControls;
   1.244 -            //rid[i].usUsage = (ushort)Hid.UsageCollection.GenericDesktop.Mouse;
   1.245 -            //rid[i].dwFlags = Const.RIDEV_EXINPUTSINK;
   1.246 -            //rid[i].hwndTarget = aHWND;
   1.247 -
   1.248 -
   1.249 -            iHidHandler = new SharpLib.Hid.HidHandler(rid);
   1.250 -            if (!iHidHandler.IsRegistered)
   1.251 -            {
   1.252 -                Debug.WriteLine("Failed to register raw input devices: " + Marshal.GetLastWin32Error().ToString());
   1.253 -            }
   1.254 -            iHidHandler.OnHidEvent += HandleHidEvent;
   1.255 -        }
   1.256 -
   1.257 -
   1.258 -        //-------------------------------------------------------------
   1.259 -        // methods
   1.260 -        //-------------------------------------------------------------
   1.261 -
   1.262 -        public void ProcessMessage(Message message)
   1.263 -        {
   1.264 -            switch (message.Msg)
   1.265 -            {
   1.266 -                case Const.WM_KEYDOWN:
   1.267 -                    ProcessKeyDown(message.WParam);
   1.268 -                    break;
   1.269 -                case Const.WM_INPUT:
   1.270 -                    //Returning zero means we processed that message.
   1.271 -                    message.Result = new IntPtr(0);
   1.272 -                    ProcessInputCommand(ref message);
   1.273 -                    break;
   1.274 -            }
   1.275 -
   1.276 -        }
   1.277 -
   1.278 -
   1.279 -        //-------------------------------------------------------------
   1.280 -        // methods (helpers)
   1.281 -        //-------------------------------------------------------------
   1.282 -
   1.283 -        private void ProcessKeyDown(IntPtr wParam)
   1.284 -        {
   1.285 -            RemoteControlButton rcb = RemoteControlButton.Unknown;
   1.286 -
   1.287 -            switch (wParam.ToInt32())
   1.288 -            {
   1.289 -                case (int)Keys.Escape:
   1.290 -                    rcb = RemoteControlButton.Clear;
   1.291 -                    break;
   1.292 -                case (int)Keys.Up:
   1.293 -                    rcb = RemoteControlButton.Up;
   1.294 -                    break;
   1.295 -                case (int)Keys.Down:
   1.296 -                    rcb = RemoteControlButton.Down;
   1.297 -                    break;
   1.298 -                case (int)Keys.Left:
   1.299 -                    rcb = RemoteControlButton.Left;
   1.300 -                    break;
   1.301 -                case (int)Keys.Right:
   1.302 -                    rcb = RemoteControlButton.Right;
   1.303 -                    break;
   1.304 -                case (int)Keys.Enter:
   1.305 -                    rcb = RemoteControlButton.Enter;
   1.306 -                    break;
   1.307 -                case (int)Keys.D0:
   1.308 -                    rcb = RemoteControlButton.Digit0;
   1.309 -                    break;
   1.310 -                case (int)Keys.D1:
   1.311 -                    rcb = RemoteControlButton.Digit1;
   1.312 -                    break;
   1.313 -                case (int)Keys.D2:
   1.314 -                    rcb = RemoteControlButton.Digit2;
   1.315 -                    break;
   1.316 -                case (int)Keys.D3:
   1.317 -                    rcb = RemoteControlButton.Digit3;
   1.318 -                    break;
   1.319 -                case (int)Keys.D4:
   1.320 -                    rcb = RemoteControlButton.Digit4;
   1.321 -                    break;
   1.322 -                case (int)Keys.D5:
   1.323 -                    rcb = RemoteControlButton.Digit5;
   1.324 -                    break;
   1.325 -                case (int)Keys.D6:
   1.326 -                    rcb = RemoteControlButton.Digit6;
   1.327 -                    break;
   1.328 -                case (int)Keys.D7:
   1.329 -                    rcb = RemoteControlButton.Digit7;
   1.330 -                    break;
   1.331 -                case (int)Keys.D8:
   1.332 -                    rcb = RemoteControlButton.Digit8;
   1.333 -                    break;
   1.334 -                case (int)Keys.D9:
   1.335 -                    rcb = RemoteControlButton.Digit9;
   1.336 -                    break;
   1.337 -            }
   1.338 -
   1.339 -            if (this.ButtonPressed != null && rcb != RemoteControlButton.Unknown)
   1.340 -            {
   1.341 -                Debug.WriteLine("KeyDown: " + rcb.ToString());
   1.342 -                this.ButtonPressed(this, new RemoteControlEventArgs(rcb, InputDevice.Key));
   1.343 -            }
   1.344 -        }
   1.345 -
   1.346 -
   1.347 -        /// <summary>
   1.348 -        /// 
   1.349 -        /// </summary>
   1.350 -        /// <param name="aUsage"></param>
   1.351 -        private bool HidConsumerDeviceHandler(ushort aUsage)
   1.352 -        {
   1.353 -            if (aUsage == 0)
   1.354 -            {
   1.355 -                //Just skip those
   1.356 -                return false;
   1.357 -            }
   1.358 -
   1.359 -            if (Enum.IsDefined(typeof(ConsumerControl), aUsage) && aUsage != 0) //Our button is a known consumer control
   1.360 -            {
   1.361 -                if (this.ButtonPressed != null)
   1.362 -                {
   1.363 -                    return this.ButtonPressed(this, new RemoteControlEventArgs((ConsumerControl)aUsage, InputDevice.OEM));
   1.364 -                }
   1.365 -                return false;
   1.366 -            }
   1.367 -            else
   1.368 -            {
   1.369 -                Debug.WriteLine("Unknown Consumer Control!");
   1.370 -                return false;
   1.371 -            }
   1.372 -        }
   1.373 -
   1.374 -        /// <summary>
   1.375 -        /// 
   1.376 -        /// </summary>
   1.377 -        /// <param name="aUsage"></param>
   1.378 -        private bool HidMceRemoteHandler(ushort aUsage)
   1.379 -        {
   1.380 -            if (aUsage == 0)
   1.381 -            {
   1.382 -                //Just skip those
   1.383 -                return false;
   1.384 -            }
   1.385 -
   1.386 -
   1.387 -            if (Enum.IsDefined(typeof(WindowsMediaCenterRemoteControl), aUsage) && aUsage != 0) //Our button is a known MCE button
   1.388 -            {
   1.389 -                if (this.ButtonPressed != null)
   1.390 -                {
   1.391 -                    return this.ButtonPressed(this, new RemoteControlEventArgs((WindowsMediaCenterRemoteControl)aUsage, InputDevice.OEM));
   1.392 -                }
   1.393 -                return false;
   1.394 -            }
   1.395 -            else
   1.396 -            {
   1.397 -                Debug.WriteLine("Unknown MCE button!");
   1.398 -                return false;
   1.399 -            }
   1.400 -        }
   1.401 -
   1.402 -        /// <summary>
   1.403 -        /// 
   1.404 -        /// </summary>
   1.405 -        /// <param name="message"></param>
   1.406 -        private void ProcessInputCommand(ref Message message)
   1.407 -        {
   1.408 -            //We received a WM_INPUT message
   1.409 -            Debug.WriteLine("================WM_INPUT================");
   1.410 -
   1.411 -            iHidHandler.ProcessInput(message);
   1.412 -
   1.413 -        }
   1.414 -
   1.415 -        /// <summary>
   1.416 -        /// 
   1.417 -        /// </summary>
   1.418 -        /// <param name="aSender"></param>
   1.419 -        /// <param name="aHidEvent"></param>
   1.420 -        void HandleHidEvent(object aSender, SharpLib.Hid.HidEvent aHidEvent)
   1.421 -        {
   1.422 -            HidUsageHandler usagePageHandler = null;
   1.423 -
   1.424 -            //Check if this an MCE remote HID message
   1.425 -            if (aHidEvent.UsagePage == (ushort)SharpLib.Hid.UsagePage.WindowsMediaCenterRemoteControl && aHidEvent.UsageCollection == (ushort)SharpLib.Hid.UsageCollection.WindowsMediaCenter.WindowsMediaCenterRemoteControl)
   1.426 -            {
   1.427 -                usagePageHandler = HidMceRemoteHandler;
   1.428 -            }
   1.429 -            //Check if this is a consumer control HID message
   1.430 -            else if (aHidEvent.UsagePage == (ushort)SharpLib.Hid.UsagePage.Consumer && aHidEvent.UsageCollection == (ushort)SharpLib.Hid.UsageCollection.Consumer.ConsumerControl)
   1.431 -            {
   1.432 -                usagePageHandler = HidConsumerDeviceHandler;
   1.433 -            }
   1.434 -            //Unknown HID message
   1.435 -            else
   1.436 -            {
   1.437 -                Debug.WriteLine("Unknown HID message.");
   1.438 -                return;
   1.439 -            }
   1.440 -
   1.441 -            foreach (ushort usage in aHidEvent.Usages)
   1.442 -            {
   1.443 -                usagePageHandler(usage);
   1.444 -            }
   1.445 -        }
   1.446 -    }
   1.447 -}
   1.448 -